code
stringlengths
87
55.2k
code_codestyle
int64
0
349
style_context
stringlengths
135
49.1k
style_context_codestyle
int64
0
349
label
int64
0
1
"""simple docstring""" from math import cos, sin, sqrt, tau from audio_filters.iir_filter import IIRFilter def lowercase (snake_case__ : int , snake_case__ : int , snake_case__ : float = 1 / sqrt(2 ) ) -> int: '''simple docstring''' lowerCAmelCase = tau * frequency / samplerate lowerCAmelCase = sin(snake_case__ ) lowerCAmelCase = cos(snake_case__ ) lowerCAmelCase = _sin / (2 * q_factor) lowerCAmelCase = (1 - _cos) / 2 lowerCAmelCase = 1 - _cos lowerCAmelCase = 1 + alpha lowerCAmelCase = -2 * _cos lowerCAmelCase = 1 - alpha lowerCAmelCase = IIRFilter(2 ) filt.set_coefficients([aa, aa, aa] , [ba, ba, ba] ) return filt def lowercase (snake_case__ : int , snake_case__ : int , snake_case__ : float = 1 / sqrt(2 ) ) -> Optional[int]: '''simple docstring''' lowerCAmelCase = tau * frequency / samplerate lowerCAmelCase = sin(snake_case__ ) lowerCAmelCase = cos(snake_case__ ) lowerCAmelCase = _sin / (2 * q_factor) lowerCAmelCase = (1 + _cos) / 2 lowerCAmelCase = -1 - _cos lowerCAmelCase = 1 + alpha lowerCAmelCase = -2 * _cos lowerCAmelCase = 1 - alpha lowerCAmelCase = IIRFilter(2 ) filt.set_coefficients([aa, aa, aa] , [ba, ba, ba] ) return filt def lowercase (snake_case__ : int , snake_case__ : int , snake_case__ : float = 1 / sqrt(2 ) ) -> Tuple: '''simple docstring''' lowerCAmelCase = tau * frequency / samplerate lowerCAmelCase = sin(snake_case__ ) lowerCAmelCase = cos(snake_case__ ) lowerCAmelCase = _sin / (2 * q_factor) lowerCAmelCase = _sin / 2 lowerCAmelCase = 0 lowerCAmelCase = -ba lowerCAmelCase = 1 + alpha lowerCAmelCase = -2 * _cos lowerCAmelCase = 1 - alpha lowerCAmelCase = IIRFilter(2 ) filt.set_coefficients([aa, aa, aa] , [ba, ba, ba] ) return filt def lowercase (snake_case__ : int , snake_case__ : int , snake_case__ : float = 1 / sqrt(2 ) ) -> Dict: '''simple docstring''' lowerCAmelCase = tau * frequency / samplerate lowerCAmelCase = sin(snake_case__ ) lowerCAmelCase = cos(snake_case__ ) lowerCAmelCase = _sin / (2 * q_factor) lowerCAmelCase = 1 - alpha lowerCAmelCase = -2 * _cos lowerCAmelCase = 1 + alpha lowerCAmelCase = IIRFilter(2 ) filt.set_coefficients([ba, ba, ba] , [ba, ba, ba] ) return filt def lowercase (snake_case__ : int , snake_case__ : int , snake_case__ : float , snake_case__ : float = 1 / sqrt(2 ) , ) -> str: '''simple docstring''' lowerCAmelCase = tau * frequency / samplerate lowerCAmelCase = sin(snake_case__ ) lowerCAmelCase = cos(snake_case__ ) lowerCAmelCase = _sin / (2 * q_factor) lowerCAmelCase = 10 ** (gain_db / 40) lowerCAmelCase = 1 + alpha * big_a lowerCAmelCase = -2 * _cos lowerCAmelCase = 1 - alpha * big_a lowerCAmelCase = 1 + alpha / big_a lowerCAmelCase = -2 * _cos lowerCAmelCase = 1 - alpha / big_a lowerCAmelCase = IIRFilter(2 ) filt.set_coefficients([aa, aa, aa] , [ba, ba, ba] ) return filt def lowercase (snake_case__ : int , snake_case__ : int , snake_case__ : float , snake_case__ : float = 1 / sqrt(2 ) , ) -> Tuple: '''simple docstring''' lowerCAmelCase = tau * frequency / samplerate lowerCAmelCase = sin(snake_case__ ) lowerCAmelCase = cos(snake_case__ ) lowerCAmelCase = _sin / (2 * q_factor) lowerCAmelCase = 10 ** (gain_db / 40) lowerCAmelCase = (big_a + 1) - (big_a - 1) * _cos lowerCAmelCase = (big_a + 1) + (big_a - 1) * _cos lowerCAmelCase = (big_a - 1) - (big_a + 1) * _cos lowerCAmelCase = (big_a - 1) + (big_a + 1) * _cos lowerCAmelCase = 2 * sqrt(snake_case__ ) * alpha lowerCAmelCase = big_a * (pmc + aaa) lowerCAmelCase = 2 * big_a * mpc lowerCAmelCase = big_a * (pmc - aaa) lowerCAmelCase = ppmc + aaa lowerCAmelCase = -2 * pmpc lowerCAmelCase = ppmc - aaa lowerCAmelCase = IIRFilter(2 ) filt.set_coefficients([aa, aa, aa] , [ba, ba, ba] ) return filt def lowercase (snake_case__ : int , snake_case__ : int , snake_case__ : float , snake_case__ : float = 1 / sqrt(2 ) , ) -> str: '''simple docstring''' lowerCAmelCase = tau * frequency / samplerate lowerCAmelCase = sin(snake_case__ ) lowerCAmelCase = cos(snake_case__ ) lowerCAmelCase = _sin / (2 * q_factor) lowerCAmelCase = 10 ** (gain_db / 40) lowerCAmelCase = (big_a + 1) - (big_a - 1) * _cos lowerCAmelCase = (big_a + 1) + (big_a - 1) * _cos lowerCAmelCase = (big_a - 1) - (big_a + 1) * _cos lowerCAmelCase = (big_a - 1) + (big_a + 1) * _cos lowerCAmelCase = 2 * sqrt(snake_case__ ) * alpha lowerCAmelCase = big_a * (ppmc + aaa) lowerCAmelCase = -2 * big_a * pmpc lowerCAmelCase = big_a * (ppmc - aaa) lowerCAmelCase = pmc + aaa lowerCAmelCase = 2 * mpc lowerCAmelCase = pmc - aaa lowerCAmelCase = IIRFilter(2 ) filt.set_coefficients([aa, aa, aa] , [ba, ba, ba] ) return filt
155
from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available() and is_transformers_version('>=', '4.25.0')): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import UnCLIPImageVariationPipeline, UnCLIPPipeline else: from .pipeline_unclip import UnCLIPPipeline from .pipeline_unclip_image_variation import UnCLIPImageVariationPipeline from .text_proj import UnCLIPTextProjModel
30
0
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) _lowerCamelCase : Dict = { "configuration_rembert": ["REMBERT_PRETRAINED_CONFIG_ARCHIVE_MAP", "RemBertConfig", "RemBertOnnxConfig"] } try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : Tuple = ["RemBertTokenizer"] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : str = ["RemBertTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : Union[str, Any] = [ "REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST", "RemBertForCausalLM", "RemBertForMaskedLM", "RemBertForMultipleChoice", "RemBertForQuestionAnswering", "RemBertForSequenceClassification", "RemBertForTokenClassification", "RemBertLayer", "RemBertModel", "RemBertPreTrainedModel", "load_tf_weights_in_rembert", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCamelCase : Any = [ "TF_REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST", "TFRemBertForCausalLM", "TFRemBertForMaskedLM", "TFRemBertForMultipleChoice", "TFRemBertForQuestionAnswering", "TFRemBertForSequenceClassification", "TFRemBertForTokenClassification", "TFRemBertLayer", "TFRemBertModel", "TFRemBertPreTrainedModel", ] if TYPE_CHECKING: from .configuration_rembert import REMBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, RemBertConfig, RemBertOnnxConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_rembert import RemBertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_rembert_fast import RemBertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_rembert import ( REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST, RemBertForCausalLM, RemBertForMaskedLM, RemBertForMultipleChoice, RemBertForQuestionAnswering, RemBertForSequenceClassification, RemBertForTokenClassification, RemBertLayer, RemBertModel, RemBertPreTrainedModel, load_tf_weights_in_rembert, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_rembert import ( TF_REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST, TFRemBertForCausalLM, TFRemBertForMaskedLM, TFRemBertForMultipleChoice, TFRemBertForQuestionAnswering, TFRemBertForSequenceClassification, TFRemBertForTokenClassification, TFRemBertLayer, TFRemBertModel, TFRemBertPreTrainedModel, ) else: import sys _lowerCamelCase : Optional[Any] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
336
from typing import Any, Dict, List, Optional, Tuple, Union import torch from torch import nn from torch.utils.data import DistributedSampler, RandomSampler from transformers import PreTrainedModel, Trainer, logging from transformers.integrations import is_fairscale_available from transformers.models.fsmt.configuration_fsmt import FSMTConfig from transformers.optimization import ( Adafactor, AdamW, get_constant_schedule, get_constant_schedule_with_warmup, get_cosine_schedule_with_warmup, get_cosine_with_hard_restarts_schedule_with_warmup, get_linear_schedule_with_warmup, get_polynomial_decay_schedule_with_warmup, ) from transformers.trainer_pt_utils import get_tpu_sampler from transformers.training_args import ParallelMode from transformers.utils import is_torch_tpu_available if is_fairscale_available(): from fairscale.optim import OSS __a = logging.get_logger(__name__) __a = { 'linear': get_linear_schedule_with_warmup, 'cosine': get_cosine_schedule_with_warmup, 'cosine_w_restarts': get_cosine_with_hard_restarts_schedule_with_warmup, 'polynomial': get_polynomial_decay_schedule_with_warmup, 'constant': get_constant_schedule, 'constant_w_warmup': get_constant_schedule_with_warmup, } class lowercase__( UpperCAmelCase ): """simple docstring""" def __init__( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : List[str]=None , SCREAMING_SNAKE_CASE_ : Optional[Any]=None , *SCREAMING_SNAKE_CASE_ : str , **SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> List[str]: super().__init__(*SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) if config is None: assert isinstance(self.model , SCREAMING_SNAKE_CASE_ ), ( "If no `config` is passed the model to be trained has to be of type `PreTrainedModel`, but is" f''' {self.model.__class__}''' ) lowercase_ = self.model.config else: lowercase_ = config lowercase_ = data_args lowercase_ = self.config.tgt_vocab_size if isinstance(self.config , SCREAMING_SNAKE_CASE_ ) else self.config.vocab_size if self.args.label_smoothing != 0 or (self.data_args is not None and self.data_args.ignore_pad_token_for_loss): assert self.config.pad_token_id is not None, ( "Make sure that `config.pad_token_id` is correcly defined when ignoring `pad_token` for loss" " calculation or doing label smoothing." ) if self.config.pad_token_id is None and self.config.eos_token_id is not None: logger.warning( f'''The `config.pad_token_id` is `None`. Using `config.eos_token_id` = {self.config.eos_token_id} for''' ''' padding..''' ) if self.args.label_smoothing == 0: lowercase_ = torch.nn.CrossEntropyLoss(ignore_index=self.config.pad_token_id ) else: # dynamically import label_smoothed_nll_loss from utils import label_smoothed_nll_loss lowercase_ = label_smoothed_nll_loss def _lowercase ( self : int , SCREAMING_SNAKE_CASE_ : int ) -> Optional[Any]: if self.optimizer is None: lowercase_ = ['''bias''', '''LayerNorm.weight'''] lowercase_ = [ { '''params''': [p for n, p in self.model.named_parameters() if not any(nd in n for nd in no_decay )], '''weight_decay''': self.args.weight_decay, }, { '''params''': [p for n, p in self.model.named_parameters() if any(nd in n for nd in no_decay )], '''weight_decay''': 0.0, }, ] lowercase_ = Adafactor if self.args.adafactor else AdamW if self.args.adafactor: lowercase_ = Adafactor lowercase_ = {'''scale_parameter''': False, '''relative_step''': False} else: lowercase_ = AdamW lowercase_ = { '''betas''': (self.args.adam_betaa, self.args.adam_betaa), '''eps''': self.args.adam_epsilon, } lowercase_ = self.args.learning_rate if self.sharded_ddp: lowercase_ = OSS( params=SCREAMING_SNAKE_CASE_ , optim=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) else: lowercase_ = optimizer_cls(SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) if self.lr_scheduler is None: lowercase_ = self._get_lr_scheduler(SCREAMING_SNAKE_CASE_ ) else: # ignoring --lr_scheduler logger.warning('''scheduler is passed to `Seq2SeqTrainer`, `--lr_scheduler` arg is ignored.''' ) def _lowercase ( self : List[Any] , SCREAMING_SNAKE_CASE_ : Dict ) -> Dict: lowercase_ = arg_to_scheduler[self.args.lr_scheduler] if self.args.lr_scheduler == "constant": lowercase_ = schedule_func(self.optimizer ) elif self.args.lr_scheduler == "constant_w_warmup": lowercase_ = schedule_func(self.optimizer , num_warmup_steps=self.args.warmup_steps ) else: lowercase_ = schedule_func( self.optimizer , num_warmup_steps=self.args.warmup_steps , num_training_steps=SCREAMING_SNAKE_CASE_ ) return scheduler def _lowercase ( self : Tuple ) -> Optional[torch.utils.data.Sampler]: if isinstance(self.train_dataset , torch.utils.data.IterableDataset ): return None elif is_torch_tpu_available(): return get_tpu_sampler(self.train_dataset ) else: if self.args.sortish_sampler: self.train_dataset.make_sortish_sampler( self.args.per_device_train_batch_size , distributed=(self.args.parallel_mode == ParallelMode.DISTRIBUTED) , ) return ( RandomSampler(self.train_dataset ) if self.args.local_rank == -1 else DistributedSampler(self.train_dataset ) ) def _lowercase ( self : List[Any] , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : List[Any] ) -> Any: if self.args.label_smoothing == 0: if self.data_args is not None and self.data_args.ignore_pad_token_for_loss: # force training to ignore pad token lowercase_ = model(**SCREAMING_SNAKE_CASE_ , use_cache=SCREAMING_SNAKE_CASE_ )[0] lowercase_ = self.loss_fn(logits.view(-1 , logits.shape[-1] ) , labels.view(-1 ) ) else: # compute usual loss via models lowercase_ , lowercase_ = model(**SCREAMING_SNAKE_CASE_ , labels=SCREAMING_SNAKE_CASE_ , use_cache=SCREAMING_SNAKE_CASE_ )[:2] else: # compute label smoothed loss lowercase_ = model(**SCREAMING_SNAKE_CASE_ , use_cache=SCREAMING_SNAKE_CASE_ )[0] lowercase_ = torch.nn.functional.log_softmax(SCREAMING_SNAKE_CASE_ , dim=-1 ) lowercase_ , lowercase_ = self.loss_fn(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , self.args.label_smoothing , ignore_index=self.config.pad_token_id ) return loss, logits def _lowercase ( self : str , SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : Optional[int] ) -> List[Any]: lowercase_ = inputs.pop('''labels''' ) lowercase_ , lowercase_ = self._compute_loss(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) return loss def _lowercase ( self : Optional[int] , SCREAMING_SNAKE_CASE_ : nn.Module , SCREAMING_SNAKE_CASE_ : Dict[str, Union[torch.Tensor, Any]] , SCREAMING_SNAKE_CASE_ : bool , SCREAMING_SNAKE_CASE_ : Optional[List[str]] = None , ) -> Tuple[Optional[float], Optional[torch.Tensor], Optional[torch.Tensor]]: lowercase_ = self._prepare_inputs(SCREAMING_SNAKE_CASE_ ) lowercase_ = { '''max_length''': self.data_args.val_max_target_length if self.data_args is not None else self.config.max_length, '''num_beams''': self.data_args.eval_beams if self.data_args is not None else self.config.num_beams, } if self.args.predict_with_generate and not self.args.prediction_loss_only: lowercase_ = self.model.generate( inputs['''input_ids'''] , attention_mask=inputs['''attention_mask'''] , **SCREAMING_SNAKE_CASE_ , ) # in case the batch is shorter than max length, the output should be padded if generated_tokens.shape[-1] < gen_kwargs["max_length"]: lowercase_ = self._pad_tensors_to_max_len(SCREAMING_SNAKE_CASE_ , gen_kwargs['''max_length'''] ) lowercase_ = inputs.pop('''labels''' ) with torch.no_grad(): # compute loss on predict data lowercase_ , lowercase_ = self._compute_loss(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) lowercase_ = loss.mean().detach() if self.args.prediction_loss_only: return (loss, None, None) lowercase_ = generated_tokens if self.args.predict_with_generate else logits if labels.shape[-1] < gen_kwargs["max_length"]: lowercase_ = self._pad_tensors_to_max_len(SCREAMING_SNAKE_CASE_ , gen_kwargs['''max_length'''] ) return (loss, logits, labels) def _lowercase ( self : str , SCREAMING_SNAKE_CASE_ : Optional[Any] , SCREAMING_SNAKE_CASE_ : int ) -> Tuple: # If PAD token is not defined at least EOS token has to be defined lowercase_ = self.config.pad_token_id if self.config.pad_token_id is not None else self.config.eos_token_id if pad_token_id is None: raise ValueError( '''Make sure that either `config.pad_token_id` or `config.eos_token_id` is defined if tensor has to be''' f''' padded to `max_length`={max_length}''' ) lowercase_ = pad_token_id * torch.ones( (tensor.shape[0], max_length) , dtype=tensor.dtype , device=tensor.device ) lowercase_ = tensor return padded_tensor
30
0
import mpmath # for roots of unity import numpy as np class lowerCamelCase_ : '''simple docstring''' def __init__( self , __lowercase=None , __lowercase=None) -> List[Any]: # Input as list __UpperCamelCase :Optional[int] = list(poly_a or [0])[:] __UpperCamelCase :Dict = list(poly_b or [0])[:] # Remove leading zero coefficients while self.polyA[-1] == 0: self.polyA.pop() __UpperCamelCase :Optional[Any] = len(self.polyA) while self.polyB[-1] == 0: self.polyB.pop() __UpperCamelCase :Dict = len(self.polyB) # Add 0 to make lengths equal a power of 2 __UpperCamelCase :Any = int( 2 ** np.ceil(np.loga(len(self.polyA) + len(self.polyB) - 1))) while len(self.polyA) < self.c_max_length: self.polyA.append(0) while len(self.polyB) < self.c_max_length: self.polyB.append(0) # A complex root used for the fourier transform __UpperCamelCase :Optional[Any] = complex(mpmath.root(x=1 , n=self.c_max_length , k=1)) # The product __UpperCamelCase :Tuple = self.__multiply() def UpperCamelCase__ ( self , __lowercase) -> List[Any]: __UpperCamelCase :List[str] = [[x] for x in self.polyA] if which == '''A''' else [[x] for x in self.polyB] # Corner case if len(SCREAMING_SNAKE_CASE_) <= 1: return dft[0] # __UpperCamelCase :Union[str, Any] = self.c_max_length // 2 while next_ncol > 0: __UpperCamelCase :List[Any] = [[] for i in range(SCREAMING_SNAKE_CASE_)] __UpperCamelCase :str = self.root**next_ncol # First half of next step __UpperCamelCase :List[Any] = 1 for j in range(self.c_max_length // (next_ncol * 2)): for i in range(SCREAMING_SNAKE_CASE_): new_dft[i].append(dft[i][j] + current_root * dft[i + next_ncol][j]) current_root *= root # Second half of next step __UpperCamelCase :Optional[Any] = 1 for j in range(self.c_max_length // (next_ncol * 2)): for i in range(SCREAMING_SNAKE_CASE_): new_dft[i].append(dft[i][j] - current_root * dft[i + next_ncol][j]) current_root *= root # Update __UpperCamelCase :str = new_dft __UpperCamelCase :Union[str, Any] = next_ncol // 2 return dft[0] def UpperCamelCase__ ( self) -> Dict: __UpperCamelCase :Optional[Any] = self.__dft('''A''') __UpperCamelCase :int = self.__dft('''B''') __UpperCamelCase :str = [[dft_a[i] * dft_b[i] for i in range(self.c_max_length)]] del dft_a del dft_b # Corner Case if len(inverce_c[0]) <= 1: return inverce_c[0] # Inverse DFT __UpperCamelCase :Optional[Any] = 2 while next_ncol <= self.c_max_length: __UpperCamelCase :Dict = [[] for i in range(SCREAMING_SNAKE_CASE_)] __UpperCamelCase :List[Any] = self.root ** (next_ncol // 2) __UpperCamelCase :List[Any] = 1 # First half of next step for j in range(self.c_max_length // next_ncol): for i in range(next_ncol // 2): # Even positions new_inverse_c[i].append( ( inverce_c[i][j] + inverce_c[i][j + self.c_max_length // next_ncol] ) / 2) # Odd positions new_inverse_c[i + next_ncol // 2].append( ( inverce_c[i][j] - inverce_c[i][j + self.c_max_length // next_ncol] ) / (2 * current_root)) current_root *= root # Update __UpperCamelCase :Dict = new_inverse_c next_ncol *= 2 # Unpack __UpperCamelCase :Any = [round(x[0].real , 8) + round(x[0].imag , 8) * 1j for x in inverce_c] # Remove leading 0's while inverce_c[-1] == 0: inverce_c.pop() return inverce_c def __str__( self) -> Optional[int]: __UpperCamelCase :Optional[int] = '''A = ''' + ''' + '''.join( f"""{coef}*x^{i}""" for coef, i in enumerate(self.polyA[: self.len_A])) __UpperCamelCase :Dict = '''B = ''' + ''' + '''.join( f"""{coef}*x^{i}""" for coef, i in enumerate(self.polyB[: self.len_B])) __UpperCamelCase :List[str] = '''A*B = ''' + ''' + '''.join( f"""{coef}*x^{i}""" for coef, i in enumerate(self.product)) return f"""{a}\n{b}\n{c}""" # Unit tests if __name__ == "__main__": import doctest doctest.testmod()
43
import unittest import numpy as np import torch from .utils_summarization import build_mask, compute_token_type_ids, process_story, truncate_or_pad class lowercase__( unittest.TestCase ): """simple docstring""" def _lowercase ( self : List[str] ) -> List[Any]: lowercase_ = 1_0 def _lowercase ( self : int ) -> List[str]: lowercase_ = [1, 2, 3, 4] lowercase_ = [1, 2, 3, 4, 0, 0, 0, 0, 0, 0] self.assertEqual(truncate_or_pad(SCREAMING_SNAKE_CASE_ , self.block_size , 0 ) , SCREAMING_SNAKE_CASE_ ) def _lowercase ( self : int ) -> Optional[Any]: lowercase_ = [1, 2, 3, 4, 5, 6, 7, 8, 9, 1_0] lowercase_ = [1, 2, 3, 4, 5, 6, 7, 8, 9, 1_0] self.assertEqual(truncate_or_pad(SCREAMING_SNAKE_CASE_ , self.block_size , 0 ) , SCREAMING_SNAKE_CASE_ ) def _lowercase ( self : Union[str, Any] ) -> Optional[int]: lowercase_ = [1, 2, 3, 4, 5, 6, 7, 8, 9, 1_0, 1_1, 1_2, 1_3] lowercase_ = [1, 2, 3, 4, 5, 6, 7, 8, 9, 1_0] self.assertEqual(truncate_or_pad(SCREAMING_SNAKE_CASE_ , self.block_size , 0 ) , SCREAMING_SNAKE_CASE_ ) def _lowercase ( self : Any ) -> List[Any]: lowercase_ = '''It was the year of Our Lord one thousand seven hundred and seventy-five.\n\nSpiritual revelations were conceded to England at that favoured period, as at this.''' lowercase_ , lowercase_ = process_story(SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ , [] ) def _lowercase ( self : List[str] ) -> List[str]: lowercase_ = '''''' lowercase_ , lowercase_ = process_story(SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ , [] ) self.assertEqual(SCREAMING_SNAKE_CASE_ , [] ) def _lowercase ( self : Union[str, Any] ) -> Union[str, Any]: lowercase_ = ( '''It was the year of Our Lord one thousand seven hundred and ''' '''seventy-five\n\nSpiritual revelations were conceded to England ''' '''at that favoured period, as at this.\n@highlight\n\nIt was the best of times''' ) lowercase_ , lowercase_ = process_story(SCREAMING_SNAKE_CASE_ ) lowercase_ = [ '''It was the year of Our Lord one thousand seven hundred and seventy-five.''', '''Spiritual revelations were conceded to England at that favoured period, as at this.''', ] self.assertEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) lowercase_ = ['''It was the best of times.'''] self.assertEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def _lowercase ( self : Union[str, Any] ) -> Optional[Any]: lowercase_ = torch.tensor([1, 2, 3, 4] ) lowercase_ = torch.tensor([1, 1, 1, 1] ) np.testing.assert_array_equal(build_mask(SCREAMING_SNAKE_CASE_ , 0 ).numpy() , expected.numpy() ) def _lowercase ( self : List[Any] ) -> Tuple: lowercase_ = torch.tensor([1, 2, 3, 4, 2_3, 2_3, 2_3] ) lowercase_ = torch.tensor([1, 1, 1, 1, 0, 0, 0] ) np.testing.assert_array_equal(build_mask(SCREAMING_SNAKE_CASE_ , 2_3 ).numpy() , expected.numpy() ) def _lowercase ( self : int ) -> Dict: lowercase_ = torch.tensor([8, 2, 3, 4, 1, 1, 1] ) lowercase_ = torch.tensor([1, 1, 1, 1, 0, 0, 0] ) np.testing.assert_array_equal(build_mask(SCREAMING_SNAKE_CASE_ , 1 ).numpy() , expected.numpy() ) def _lowercase ( self : List[str] ) -> Tuple: lowercase_ = 1_0_1 lowercase_ = torch.tensor([[1, 2, 3, 4, 5, 6], [1, 2, 3, 1_0_1, 5, 6], [1, 1_0_1, 3, 4, 1_0_1, 6]] ) lowercase_ = torch.tensor([[1, 1, 1, 1, 1, 1], [1, 1, 1, 0, 0, 0], [1, 0, 0, 0, 1, 1]] ) lowercase_ = compute_token_type_ids(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) np.testing.assert_array_equal(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ )
30
0
import unittest import numpy as np import torch from .utils_summarization import build_mask, compute_token_type_ids, process_story, truncate_or_pad class __lowerCamelCase (unittest.TestCase ): def snake_case_ ( self: List[str] ): '''simple docstring''' __UpperCamelCase = 10 def snake_case_ ( self: int ): '''simple docstring''' __UpperCamelCase = [1, 2, 3, 4] __UpperCamelCase = [1, 2, 3, 4, 0, 0, 0, 0, 0, 0] self.assertEqual(truncate_or_pad(SCREAMING_SNAKE_CASE_,self.block_size,0 ),SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self: int ): '''simple docstring''' __UpperCamelCase = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] __UpperCamelCase = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] self.assertEqual(truncate_or_pad(SCREAMING_SNAKE_CASE_,self.block_size,0 ),SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self: Union[str, Any] ): '''simple docstring''' __UpperCamelCase = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] __UpperCamelCase = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] self.assertEqual(truncate_or_pad(SCREAMING_SNAKE_CASE_,self.block_size,0 ),SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self: Any ): '''simple docstring''' __UpperCamelCase = 'It was the year of Our Lord one thousand seven hundred and\n seventy-five.\n\nSpiritual revelations were conceded to England at that\n favoured period, as at this.' __UpperCamelCase, __UpperCamelCase = process_story(SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_,[] ) def snake_case_ ( self: List[str] ): '''simple docstring''' __UpperCamelCase = '' __UpperCamelCase, __UpperCamelCase = process_story(SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_,[] ) self.assertEqual(SCREAMING_SNAKE_CASE_,[] ) def snake_case_ ( self: Union[str, Any] ): '''simple docstring''' __UpperCamelCase = ( 'It was the year of Our Lord one thousand seven hundred and ' 'seventy-five\n\nSpiritual revelations were conceded to England ' 'at that favoured period, as at this.\n@highlight\n\nIt was the best of times' ) __UpperCamelCase, __UpperCamelCase = process_story(SCREAMING_SNAKE_CASE_ ) __UpperCamelCase = [ 'It was the year of Our Lord one thousand seven hundred and seventy-five.', 'Spiritual revelations were conceded to England at that favoured period, as at this.', ] self.assertEqual(SCREAMING_SNAKE_CASE_,SCREAMING_SNAKE_CASE_ ) __UpperCamelCase = ['It was the best of times.'] self.assertEqual(SCREAMING_SNAKE_CASE_,SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self: Union[str, Any] ): '''simple docstring''' __UpperCamelCase = torch.tensor([1, 2, 3, 4] ) __UpperCamelCase = torch.tensor([1, 1, 1, 1] ) np.testing.assert_array_equal(build_mask(SCREAMING_SNAKE_CASE_,0 ).numpy(),expected.numpy() ) def snake_case_ ( self: List[Any] ): '''simple docstring''' __UpperCamelCase = torch.tensor([1, 2, 3, 4, 23, 23, 23] ) __UpperCamelCase = torch.tensor([1, 1, 1, 1, 0, 0, 0] ) np.testing.assert_array_equal(build_mask(SCREAMING_SNAKE_CASE_,23 ).numpy(),expected.numpy() ) def snake_case_ ( self: int ): '''simple docstring''' __UpperCamelCase = torch.tensor([8, 2, 3, 4, 1, 1, 1] ) __UpperCamelCase = torch.tensor([1, 1, 1, 1, 0, 0, 0] ) np.testing.assert_array_equal(build_mask(SCREAMING_SNAKE_CASE_,1 ).numpy(),expected.numpy() ) def snake_case_ ( self: List[str] ): '''simple docstring''' __UpperCamelCase = 101 __UpperCamelCase = torch.tensor([[1, 2, 3, 4, 5, 6], [1, 2, 3, 101, 5, 6], [1, 101, 3, 4, 101, 6]] ) __UpperCamelCase = torch.tensor([[1, 1, 1, 1, 1, 1], [1, 1, 1, 0, 0, 0], [1, 0, 0, 0, 1, 1]] ) __UpperCamelCase = compute_token_type_ids(SCREAMING_SNAKE_CASE_,SCREAMING_SNAKE_CASE_ ) np.testing.assert_array_equal(SCREAMING_SNAKE_CASE_,SCREAMING_SNAKE_CASE_ )
310
def a ( snake_case__: list ): '''simple docstring''' if len(snake_case__ ) <= 1: return [tuple(snake_case__ )] lowercase_ = [] def generate(snake_case__: int , snake_case__: list ): if k == 1: res.append(tuple(arr[:] ) ) return generate(k - 1 , snake_case__ ) for i in range(k - 1 ): if k % 2 == 0: # k is even lowercase_ , lowercase_ = arr[k - 1], arr[i] else: # k is odd lowercase_ , lowercase_ = arr[k - 1], arr[0] generate(k - 1 , snake_case__ ) generate(len(snake_case__ ) , snake_case__ ) return res if __name__ == "__main__": __a = input('Enter numbers separated by a comma:\n').strip() __a = [int(item) for item in user_input.split(',')] print(heaps(arr))
30
0
"""simple docstring""" import os def __lowerCAmelCase (): __lowerCAmelCase : Optional[int] = os.path.join(os.path.dirname(snake_case__ ) , 'num.txt' ) with open(snake_case__ ) as file_hand: return str(sum(int(snake_case__ ) for line in file_hand ) )[:10] if __name__ == "__main__": print(solution())
86
import argparse import math import os import torch from neural_compressor.utils.pytorch import load from PIL import Image from transformers import CLIPTextModel, CLIPTokenizer from diffusers import AutoencoderKL, StableDiffusionPipeline, UNetaDConditionModel def a ( ): '''simple docstring''' lowercase_ = argparse.ArgumentParser() parser.add_argument( '''-m''' , '''--pretrained_model_name_or_path''' , type=snake_case__ , default=snake_case__ , required=snake_case__ , help='''Path to pretrained model or model identifier from huggingface.co/models.''' , ) parser.add_argument( '''-c''' , '''--caption''' , type=snake_case__ , default='''robotic cat with wings''' , help='''Text used to generate images.''' , ) parser.add_argument( '''-n''' , '''--images_num''' , type=snake_case__ , default=4 , help='''How much images to generate.''' , ) parser.add_argument( '''-s''' , '''--seed''' , type=snake_case__ , default=42 , help='''Seed for random process.''' , ) parser.add_argument( '''-ci''' , '''--cuda_id''' , type=snake_case__ , default=0 , help='''cuda_id.''' , ) lowercase_ = parser.parse_args() return args def a ( snake_case__: Optional[Any] , snake_case__: Tuple , snake_case__: Union[str, Any] ): '''simple docstring''' if not len(snake_case__ ) == rows * cols: raise ValueError('''The specified number of rows and columns are not correct.''' ) lowercase_ , lowercase_ = imgs[0].size lowercase_ = Image.new('''RGB''' , size=(cols * w, rows * h) ) lowercase_ , lowercase_ = grid.size for i, img in enumerate(snake_case__ ): grid.paste(snake_case__ , box=(i % cols * w, i // cols * h) ) return grid def a ( snake_case__: Tuple , snake_case__: Union[str, Any]="robotic cat with wings" , snake_case__: Union[str, Any]=7.5 , snake_case__: List[str]=50 , snake_case__: List[Any]=1 , snake_case__: Optional[int]=42 , ): '''simple docstring''' lowercase_ = torch.Generator(pipeline.device ).manual_seed(snake_case__ ) lowercase_ = pipeline( snake_case__ , guidance_scale=snake_case__ , num_inference_steps=snake_case__ , generator=snake_case__ , num_images_per_prompt=snake_case__ , ).images lowercase_ = int(math.sqrt(snake_case__ ) ) lowercase_ = image_grid(snake_case__ , rows=_rows , cols=num_images_per_prompt // _rows ) return grid, images __a = parse_args() # Load models and create wrapper for stable diffusion __a = CLIPTokenizer.from_pretrained(args.pretrained_model_name_or_path, subfolder='tokenizer') __a = CLIPTextModel.from_pretrained(args.pretrained_model_name_or_path, subfolder='text_encoder') __a = AutoencoderKL.from_pretrained(args.pretrained_model_name_or_path, subfolder='vae') __a = UNetaDConditionModel.from_pretrained(args.pretrained_model_name_or_path, subfolder='unet') __a = StableDiffusionPipeline.from_pretrained( args.pretrained_model_name_or_path, text_encoder=text_encoder, vae=vae, unet=unet, tokenizer=tokenizer ) __a = lambda images, clip_input: (images, False) if os.path.exists(os.path.join(args.pretrained_model_name_or_path, 'best_model.pt')): __a = load(args.pretrained_model_name_or_path, model=unet) unet.eval() setattr(pipeline, 'unet', unet) else: __a = unet.to(torch.device('cuda', args.cuda_id)) __a = pipeline.to(unet.device) __a , __a = generate_images(pipeline, prompt=args.caption, num_images_per_prompt=args.images_num, seed=args.seed) grid.save(os.path.join(args.pretrained_model_name_or_path, '{}.png'.format('_'.join(args.caption.split())))) __a = os.path.join(args.pretrained_model_name_or_path, '_'.join(args.caption.split())) os.makedirs(dirname, exist_ok=True) for idx, image in enumerate(images): image.save(os.path.join(dirname, '{}.png'.format(idx + 1)))
30
0
import argparse import torch from torch import nn from transformers import MBartConfig, MBartForConditionalGeneration def lowerCAmelCase ( lowerCAmelCase_ )-> Optional[int]: lowerCAmelCase_ : Union[str, Any] = [ '''encoder.version''', '''decoder.version''', '''model.encoder.version''', '''model.decoder.version''', '''_float_tensor''', '''decoder.output_projection.weight''', ] for k in ignore_keys: state_dict.pop(snake_case__ , snake_case__ ) def lowerCAmelCase ( lowerCAmelCase_ )-> Dict: lowerCAmelCase_ , lowerCAmelCase_ : int = emb.weight.shape lowerCAmelCase_ : List[str] = nn.Linear(snake_case__ , snake_case__ , bias=snake_case__ ) lowerCAmelCase_ : int = emb.weight.data return lin_layer def lowerCAmelCase ( lowerCAmelCase_ , lowerCAmelCase_="facebook/mbart-large-en-ro" , lowerCAmelCase_=False , lowerCAmelCase_=False )-> Dict: lowerCAmelCase_ : Optional[Any] = torch.load(snake_case__ , map_location='''cpu''' )['''model'''] remove_ignore_keys_(snake_case__ ) lowerCAmelCase_ : str = state_dict['''encoder.embed_tokens.weight'''].shape[0] lowerCAmelCase_ : Any = MBartConfig.from_pretrained(snake_case__ , vocab_size=snake_case__ ) if mbart_aa and finetuned: lowerCAmelCase_ : Optional[Any] = '''relu''' lowerCAmelCase_ : Optional[Any] = state_dict['''decoder.embed_tokens.weight'''] lowerCAmelCase_ : List[str] = MBartForConditionalGeneration(snake_case__ ) model.model.load_state_dict(snake_case__ ) if finetuned: lowerCAmelCase_ : Tuple = make_linear_from_emb(model.model.shared ) return model if __name__ == "__main__": _UpperCAmelCase : Optional[Any] =argparse.ArgumentParser() # Required parameters parser.add_argument( """fairseq_path""", type=str, help="""bart.large, bart.large.cnn or a path to a model.pt on local filesystem.""" ) parser.add_argument("""pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""") parser.add_argument( """--hf_config""", default="""facebook/mbart-large-cc25""", type=str, help="""Which huggingface architecture to use: mbart-large""", ) parser.add_argument("""--mbart_50""", action="""store_true""", help="""whether the model is mMART-50 checkpoint""") parser.add_argument("""--finetuned""", action="""store_true""", help="""whether the model is a fine-tuned checkpoint""") _UpperCAmelCase : Tuple =parser.parse_args() _UpperCAmelCase : List[Any] =convert_fairseq_mbart_checkpoint_from_disk( args.fairseq_path, hf_config_path=args.hf_config, finetuned=args.finetuned, mbart_aa=args.mbart_aa ) model.save_pretrained(args.pytorch_dump_folder_path)
262
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) __a = { 'configuration_rembert': ['REMBERT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'RemBertConfig', 'RemBertOnnxConfig'] } try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a = ['RemBertTokenizer'] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a = ['RemBertTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a = [ 'REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST', 'RemBertForCausalLM', 'RemBertForMaskedLM', 'RemBertForMultipleChoice', 'RemBertForQuestionAnswering', 'RemBertForSequenceClassification', 'RemBertForTokenClassification', 'RemBertLayer', 'RemBertModel', 'RemBertPreTrainedModel', 'load_tf_weights_in_rembert', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a = [ 'TF_REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFRemBertForCausalLM', 'TFRemBertForMaskedLM', 'TFRemBertForMultipleChoice', 'TFRemBertForQuestionAnswering', 'TFRemBertForSequenceClassification', 'TFRemBertForTokenClassification', 'TFRemBertLayer', 'TFRemBertModel', 'TFRemBertPreTrainedModel', ] if TYPE_CHECKING: from .configuration_rembert import REMBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, RemBertConfig, RemBertOnnxConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_rembert import RemBertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_rembert_fast import RemBertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_rembert import ( REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST, RemBertForCausalLM, RemBertForMaskedLM, RemBertForMultipleChoice, RemBertForQuestionAnswering, RemBertForSequenceClassification, RemBertForTokenClassification, RemBertLayer, RemBertModel, RemBertPreTrainedModel, load_tf_weights_in_rembert, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_rembert import ( TF_REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST, TFRemBertForCausalLM, TFRemBertForMaskedLM, TFRemBertForMultipleChoice, TFRemBertForQuestionAnswering, TFRemBertForSequenceClassification, TFRemBertForTokenClassification, TFRemBertLayer, TFRemBertModel, TFRemBertPreTrainedModel, ) else: import sys __a = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
30
0
"""simple docstring""" import argparse import json import os import re import shutil import torch from transformers import BioGptConfig, BioGptForCausalLM from transformers.models.biogpt.tokenization_biogpt import VOCAB_FILES_NAMES from transformers.tokenization_utils_base import TOKENIZER_CONFIG_FILE from transformers.utils import WEIGHTS_NAME, logging logging.set_verbosity_warning() lowercase_ = 2 class __lowerCAmelCase : '''simple docstring''' def __init__( self , *, # begin keyword-only arguments _a="<s>" , _a="<pad>" , _a="</s>" , _a="<unk>" , _a=None , ): __a , __a , __a , __a = bos, unk, pad, eos __a = [] __a = [] __a = {} __a = self.add_symbol(SCREAMING_SNAKE_CASE_ ) __a = self.add_symbol(SCREAMING_SNAKE_CASE_ ) __a = self.add_symbol(SCREAMING_SNAKE_CASE_ ) __a = self.add_symbol(SCREAMING_SNAKE_CASE_ ) if extra_special_symbols: for s in extra_special_symbols: self.add_symbol(SCREAMING_SNAKE_CASE_ ) __a = len(self.symbols ) def __eq__( self , _a ): return self.indices == other.indices def __getitem__( self , _a ): if idx < len(self.symbols ): return self.symbols[idx] return self.unk_word def __len__( self ): return len(self.symbols ) def __contains__( self , _a ): return sym in self.indices @classmethod def __UpperCAmelCase ( cls , _a ): __a = cls() d.add_from_file(SCREAMING_SNAKE_CASE_ ) return d def __UpperCAmelCase ( self , _a , _a=1 , _a=False ): if word in self.indices and not overwrite: __a = self.indices[word] __a = self.count[idx] + n return idx else: __a = len(self.symbols ) __a = idx self.symbols.append(SCREAMING_SNAKE_CASE_ ) self.count.append(SCREAMING_SNAKE_CASE_ ) return idx def __UpperCAmelCase ( self , _a ): return 0 def __UpperCAmelCase ( self , _a ): if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): try: with open(SCREAMING_SNAKE_CASE_ , '''r''' , encoding='''utf-8''' ) as fd: self.add_from_file(SCREAMING_SNAKE_CASE_ ) except FileNotFoundError as fnfe: raise fnfe except UnicodeError: raise Exception('''Incorrect encoding detected in {}, please rebuild the dataset'''.format(SCREAMING_SNAKE_CASE_ ) ) return __a = f.readlines() __a = self._load_meta(SCREAMING_SNAKE_CASE_ ) for line in lines[indices_start_line:]: try: __a , __a = line.rstrip().rsplit(''' ''' , 1 ) if field == "#fairseq:overwrite": __a = True __a , __a = line.rsplit(''' ''' , 1 ) else: __a = False __a = int(SCREAMING_SNAKE_CASE_ ) __a = line if word in self and not overwrite: raise RuntimeError( '''Duplicate word found when loading Dictionary: \'{}\'. ''' '''Duplicate words can overwrite earlier ones by adding the ''' '''#fairseq:overwrite flag at the end of the corresponding row ''' '''in the dictionary file. If using the Camembert model, please ''' '''download an updated copy of the model file.'''.format(SCREAMING_SNAKE_CASE_ ) ) self.add_symbol(SCREAMING_SNAKE_CASE_ , n=SCREAMING_SNAKE_CASE_ , overwrite=SCREAMING_SNAKE_CASE_ ) except ValueError: raise ValueError('''Incorrect dictionary format, expected \'<token> <cnt> [flags]\'''' ) def lowercase ( lowerCAmelCase__ : Tuple ) -> Union[str, Any]: __a = dict((re.sub(r'''@@$''' , '''''' , snake_case__ ), v) if k.endswith('''@@''' ) else (re.sub(r'''$''' , '''</w>''' , snake_case__ ), v) for k, v in d.items() ) __a = '''<s> <pad> </s> <unk>'''.split() # restore the special tokens for k in keep_keys: del da[f'''{k}</w>'''] __a = d[k] # restore return da def lowercase ( lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Tuple ) -> Optional[int]: # prep if not os.path.exists(snake_case__ ): raise ValueError(f'''path {biogpt_checkpoint_path} does not exist!''' ) os.makedirs(snake_case__ , exist_ok=snake_case__ ) print(f'''Writing results to {pytorch_dump_folder_path}''' ) # handle various types of models __a = os.path.join(snake_case__ , '''checkpoint.pt''' ) if not os.path.isfile(snake_case__ ): raise ValueError(f'''path to the file {checkpoint_file} does not exist!''' ) __a = torch.load(snake_case__ , map_location='''cpu''' ) __a = chkpt['''cfg''']['''model'''] # dicts __a = os.path.join(snake_case__ , '''dict.txt''' ) if not os.path.isfile(snake_case__ ): raise ValueError(f'''path to the file {dict_file} does not exist!''' ) __a = Dictionary.load(snake_case__ ) __a = rewrite_dict_keys(src_dict.indices ) __a = len(snake_case__ ) __a = os.path.join(snake_case__ , VOCAB_FILES_NAMES['''vocab_file'''] ) print(f'''Generating {src_vocab_file} of {src_vocab_size} records''' ) with open(snake_case__ , '''w''' , encoding='''utf-8''' ) as f: f.write(json.dumps(snake_case__ , ensure_ascii=snake_case__ , indent=snake_case__ ) ) # merges_file (bpecodes) __a = os.path.join(snake_case__ , '''bpecodes''' ) if not os.path.isfile(snake_case__ ): raise ValueError(f'''path to the file {bpecodes_file} does not exist!''' ) __a = os.path.join(snake_case__ , VOCAB_FILES_NAMES['''merges_file'''] ) shutil.copyfile(snake_case__ , snake_case__ ) # model config __a = os.path.join(snake_case__ , '''config.json''' ) __a = { '''activation_dropout''': args['''activation_dropout'''], '''architectures''': ['''BioGptForCausalLM'''], '''attention_probs_dropout_prob''': args['''attention_dropout'''], '''bos_token_id''': 0, '''eos_token_id''': 2, '''hidden_act''': args['''activation_fn'''], '''hidden_dropout_prob''': args['''dropout'''], '''hidden_size''': args['''decoder_embed_dim'''], '''initializer_range''': 0.02, '''intermediate_size''': args['''decoder_ffn_embed_dim'''], '''layer_norm_eps''': 1e-1_2, '''layerdrop''': args['''decoder_layerdrop'''], '''max_position_embeddings''': args['''max_target_positions'''], '''model_type''': '''biogpt''', '''num_attention_heads''': args['''decoder_attention_heads'''], '''num_hidden_layers''': args['''decoder_layers'''], '''pad_token_id''': 1, '''scale_embedding''': not args['''no_scale_embedding'''], '''tie_word_embeddings''': args['''share_decoder_input_output_embed'''], '''vocab_size''': src_vocab_size, } # good hparam defaults to start with print(f'''Generating {biogpt_model_config_file}''' ) with open(snake_case__ , '''w''' , encoding='''utf-8''' ) as f: f.write(json.dumps(snake_case__ , ensure_ascii=snake_case__ , indent=snake_case__ ) ) # tokenizer config __a = os.path.join(snake_case__ , snake_case__ ) __a = { '''bos_token''': '''<s>''', '''eos_token''': '''</s>''', '''model_max_length''': 1024, '''pad_token''': '''<pad>''', '''special_tokens_map_file''': None, '''tokenizer_class''': '''BioGptTokenizer''', '''unk_token''': '''<unk>''', } print(f'''Generating {biogpt_tokenizer_config_file}''' ) with open(snake_case__ , '''w''' , encoding='''utf-8''' ) as f: f.write(json.dumps(snake_case__ , ensure_ascii=snake_case__ , indent=snake_case__ ) ) # model __a = chkpt['''model'''] # remove unneeded keys __a = [ '''decoder.version''', ] for k in ignore_keys: model_state_dict.pop(snake_case__ , snake_case__ ) __a = list(model_state_dict.keys() ) for layer_name in layer_names: if layer_name.endswith('''output_projection.weight''' ): __a = model_state_dict.pop(snake_case__ ) else: __a = model_state_dict.pop(snake_case__ ) __a = BioGptConfig.from_pretrained(snake_case__ ) __a = BioGptForCausalLM(snake_case__ ) # check that it loads ok model_new.load_state_dict(snake_case__ ) # save __a = os.path.join(snake_case__ , snake_case__ ) print(f'''Generating {pytorch_weights_dump_path}''' ) torch.save(snake_case__ , snake_case__ ) print('''Conversion is done!''' ) if __name__ == "__main__": lowercase_ = argparse.ArgumentParser() # Required parameters parser.add_argument( "--biogpt_checkpoint_path", default=None, type=str, required=True, help=( "Path to the official PyTorch checkpoint file which is expected to reside in the dump dir with dicts," " bpecodes, etc." ), ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, required=True, help="Path to the output PyTorch model." ) lowercase_ = parser.parse_args() convert_biogpt_checkpoint_to_pytorch(args.biogpt_checkpoint_path, args.pytorch_dump_folder_path)
45
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) __a = {'configuration_deit': ['DEIT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'DeiTConfig', 'DeiTOnnxConfig']} try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a = ['DeiTFeatureExtractor'] __a = ['DeiTImageProcessor'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a = [ 'DEIT_PRETRAINED_MODEL_ARCHIVE_LIST', 'DeiTForImageClassification', 'DeiTForImageClassificationWithTeacher', 'DeiTForMaskedImageModeling', 'DeiTModel', 'DeiTPreTrainedModel', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a = [ 'TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFDeiTForImageClassification', 'TFDeiTForImageClassificationWithTeacher', 'TFDeiTForMaskedImageModeling', 'TFDeiTModel', 'TFDeiTPreTrainedModel', ] if TYPE_CHECKING: from .configuration_deit import DEIT_PRETRAINED_CONFIG_ARCHIVE_MAP, DeiTConfig, DeiTOnnxConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_deit import DeiTFeatureExtractor from .image_processing_deit import DeiTImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_deit import ( DEIT_PRETRAINED_MODEL_ARCHIVE_LIST, DeiTForImageClassification, DeiTForImageClassificationWithTeacher, DeiTForMaskedImageModeling, DeiTModel, DeiTPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_deit import ( TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST, TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher, TFDeiTForMaskedImageModeling, TFDeiTModel, TFDeiTPreTrainedModel, ) else: import sys __a = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
30
0
import math import time from transformers import Trainer, is_torch_tpu_available from transformers.trainer_utils import PredictionOutput, speed_metrics if is_torch_tpu_available(check_device=False): import torch_xla.core.xla_model as xm import torch_xla.debug.metrics as met class __lowerCAmelCase ( lowerCamelCase__ ): def __init__( self , *_snake_case , _snake_case=None , _snake_case=None , **_snake_case ): """simple docstring""" super().__init__(*SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) _lowerCAmelCase = eval_examples _lowerCAmelCase = post_process_function def snake_case ( self , _snake_case=None , _snake_case=None , _snake_case=None , _snake_case = "eval" ): """simple docstring""" _lowerCAmelCase = self.eval_dataset if eval_dataset is None else eval_dataset _lowerCAmelCase = self.get_eval_dataloader(SCREAMING_SNAKE_CASE_ ) _lowerCAmelCase = self.eval_examples if eval_examples is None else eval_examples # Temporarily disable metric computation, we will do it in the loop here. _lowerCAmelCase = self.compute_metrics _lowerCAmelCase = None _lowerCAmelCase = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop _lowerCAmelCase = time.time() try: _lowerCAmelCase = eval_loop( SCREAMING_SNAKE_CASE_ , description="""Evaluation""" , prediction_loss_only=True if compute_metrics is None else None , ignore_keys=SCREAMING_SNAKE_CASE_ , metric_key_prefix=SCREAMING_SNAKE_CASE_ , ) finally: _lowerCAmelCase = compute_metrics _lowerCAmelCase = self.args.eval_batch_size * self.args.world_size if F'{metric_key_prefix}_jit_compilation_time' in output.metrics: start_time += output.metrics[F'{metric_key_prefix}_jit_compilation_time'] output.metrics.update( speed_metrics( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , num_samples=output.num_samples , num_steps=math.ceil(output.num_samples / total_batch_size ) , ) ) if self.post_process_function is not None and self.compute_metrics is not None and self.args.should_save: # Only the main node write the results by default _lowerCAmelCase = self.post_process_function(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , output.predictions ) _lowerCAmelCase = self.compute_metrics(SCREAMING_SNAKE_CASE_ ) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys() ): if not key.startswith(F'{metric_key_prefix}_' ): _lowerCAmelCase = metrics.pop(SCREAMING_SNAKE_CASE_ ) metrics.update(output.metrics ) else: _lowerCAmelCase = output.metrics if self.args.should_log: # Only the main node log the results by default self.log(SCREAMING_SNAKE_CASE_ ) if self.args.tpu_metrics_debug or self.args.debug: # tpu-comment: Logging debug metrics for PyTorch/XLA (compile, execute times, ops, etc.) xm.master_print(met.metrics_report() ) _lowerCAmelCase = self.callback_handler.on_evaluate(self.args , self.state , self.control , SCREAMING_SNAKE_CASE_ ) return metrics def snake_case ( self , _snake_case , _snake_case , _snake_case=None , _snake_case = "test" ): """simple docstring""" _lowerCAmelCase = self.get_test_dataloader(SCREAMING_SNAKE_CASE_ ) # Temporarily disable metric computation, we will do it in the loop here. _lowerCAmelCase = self.compute_metrics _lowerCAmelCase = None _lowerCAmelCase = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop _lowerCAmelCase = time.time() try: _lowerCAmelCase = eval_loop( SCREAMING_SNAKE_CASE_ , description="""Prediction""" , prediction_loss_only=True if compute_metrics is None else None , ignore_keys=SCREAMING_SNAKE_CASE_ , metric_key_prefix=SCREAMING_SNAKE_CASE_ , ) finally: _lowerCAmelCase = compute_metrics _lowerCAmelCase = self.args.eval_batch_size * self.args.world_size if F'{metric_key_prefix}_jit_compilation_time' in output.metrics: start_time += output.metrics[F'{metric_key_prefix}_jit_compilation_time'] output.metrics.update( speed_metrics( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , num_samples=output.num_samples , num_steps=math.ceil(output.num_samples / total_batch_size ) , ) ) if self.post_process_function is None or self.compute_metrics is None: return output _lowerCAmelCase = self.post_process_function(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , output.predictions , """predict""" ) _lowerCAmelCase = self.compute_metrics(SCREAMING_SNAKE_CASE_ ) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys() ): if not key.startswith(F'{metric_key_prefix}_' ): _lowerCAmelCase = metrics.pop(SCREAMING_SNAKE_CASE_ ) metrics.update(output.metrics ) return PredictionOutput(predictions=predictions.predictions , label_ids=predictions.label_ids , metrics=SCREAMING_SNAKE_CASE_ )
82
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_speech_available, is_torch_available, ) __a = { 'configuration_trocr': ['TROCR_PRETRAINED_CONFIG_ARCHIVE_MAP', 'TrOCRConfig'], 'processing_trocr': ['TrOCRProcessor'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a = [ 'TROCR_PRETRAINED_MODEL_ARCHIVE_LIST', 'TrOCRForCausalLM', 'TrOCRPreTrainedModel', ] if TYPE_CHECKING: from .configuration_trocr import TROCR_PRETRAINED_CONFIG_ARCHIVE_MAP, TrOCRConfig from .processing_trocr import TrOCRProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_trocr import TROCR_PRETRAINED_MODEL_ARCHIVE_LIST, TrOCRForCausalLM, TrOCRPreTrainedModel else: import sys __a = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
30
0
def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : list[int] , SCREAMING_SNAKE_CASE__ : list[int] ): __UpperCamelCase =len(snake_case__ ) print('The following activities are selected:' ) # The first activity is always selected __UpperCamelCase =0 print(snake_case__ , end=',' ) # Consider rest of the activities for j in range(snake_case__ ): # If this activity has start time greater than # or equal to the finish time of previously # selected activity, then select it if start[j] >= finish[i]: print(snake_case__ , end=',' ) __UpperCamelCase =j if __name__ == "__main__": import doctest doctest.testmod() _A = [1, 3, 0, 5, 8, 5] _A = [2, 4, 6, 7, 9, 9] print_max_activities(start, finish)
62
from typing import Optional, Tuple, Union import tensorflow as tf from ...activations_tf import ACTaFN from ...file_utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward from ...modeling_tf_outputs import ( TFBaseModelOutputWithNoAttention, TFBaseModelOutputWithPoolingAndNoAttention, TFSequenceClassifierOutput, ) from ...modeling_tf_utils import TFPreTrainedModel, TFSequenceClassificationLoss, keras_serializable, unpack_inputs from ...tf_utils import shape_list from ...utils import logging from .configuration_regnet import RegNetConfig __a = logging.get_logger(__name__) # General docstring __a = 'RegNetConfig' # Base docstring __a = 'facebook/regnet-y-040' __a = [1, 1_0_8_8, 7, 7] # Image classification docstring __a = 'facebook/regnet-y-040' __a = 'tabby, tabby cat' __a = [ 'facebook/regnet-y-040', # See all regnet models at https://huggingface.co/models?filter=regnet ] class lowercase__( tf.keras.layers.Layer ): """simple docstring""" def __init__( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int = 3 , SCREAMING_SNAKE_CASE_ : int = 1 , SCREAMING_SNAKE_CASE_ : int = 1 , SCREAMING_SNAKE_CASE_ : Optional[str] = "relu" , **SCREAMING_SNAKE_CASE_ : Any , ) -> List[str]: super().__init__(**SCREAMING_SNAKE_CASE_ ) # The padding and conv has been verified in # https://colab.research.google.com/gist/sayakpaul/854bc10eeaf21c9ee2119e0b9f3841a7/scratchpad.ipynb lowercase_ = tf.keras.layers.ZeroPaddingaD(padding=kernel_size // 2 ) lowercase_ = tf.keras.layers.ConvaD( filters=SCREAMING_SNAKE_CASE_ , kernel_size=SCREAMING_SNAKE_CASE_ , strides=SCREAMING_SNAKE_CASE_ , padding='''VALID''' , groups=SCREAMING_SNAKE_CASE_ , use_bias=SCREAMING_SNAKE_CASE_ , name='''convolution''' , ) lowercase_ = tf.keras.layers.BatchNormalization(epsilon=1e-5 , momentum=0.9 , name='''normalization''' ) lowercase_ = ACTaFN[activation] if activation is not None else tf.identity def _lowercase ( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : Optional[int] ) -> Any: lowercase_ = self.convolution(self.padding(SCREAMING_SNAKE_CASE_ ) ) lowercase_ = self.normalization(SCREAMING_SNAKE_CASE_ ) lowercase_ = self.activation(SCREAMING_SNAKE_CASE_ ) return hidden_state class lowercase__( tf.keras.layers.Layer ): """simple docstring""" def __init__( self : str , SCREAMING_SNAKE_CASE_ : RegNetConfig , **SCREAMING_SNAKE_CASE_ : str ) -> Any: super().__init__(**SCREAMING_SNAKE_CASE_ ) lowercase_ = config.num_channels lowercase_ = TFRegNetConvLayer( out_channels=config.embedding_size , kernel_size=3 , stride=2 , activation=config.hidden_act , name='''embedder''' , ) def _lowercase ( self : List[str] , SCREAMING_SNAKE_CASE_ : str ) -> Optional[int]: lowercase_ = shape_list(SCREAMING_SNAKE_CASE_ )[1] if tf.executing_eagerly() and num_channels != self.num_channels: raise ValueError( '''Make sure that the channel dimension of the pixel values match with the one set in the configuration.''' ) # When running on CPU, `tf.keras.layers.Conv2D` doesn't support `NCHW` format. # So change the input format from `NCHW` to `NHWC`. # shape = (batch_size, in_height, in_width, in_channels=num_channels) lowercase_ = tf.transpose(SCREAMING_SNAKE_CASE_ , perm=(0, 2, 3, 1) ) lowercase_ = self.embedder(SCREAMING_SNAKE_CASE_ ) return hidden_state class lowercase__( tf.keras.layers.Layer ): """simple docstring""" def __init__( self : int , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int = 2 , **SCREAMING_SNAKE_CASE_ : List[str] ) -> Union[str, Any]: super().__init__(**SCREAMING_SNAKE_CASE_ ) lowercase_ = tf.keras.layers.ConvaD( filters=SCREAMING_SNAKE_CASE_ , kernel_size=1 , strides=SCREAMING_SNAKE_CASE_ , use_bias=SCREAMING_SNAKE_CASE_ , name='''convolution''' ) lowercase_ = tf.keras.layers.BatchNormalization(epsilon=1e-5 , momentum=0.9 , name='''normalization''' ) def _lowercase ( self : List[Any] , SCREAMING_SNAKE_CASE_ : tf.Tensor , SCREAMING_SNAKE_CASE_ : bool = False ) -> tf.Tensor: return self.normalization(self.convolution(SCREAMING_SNAKE_CASE_ ) , training=SCREAMING_SNAKE_CASE_ ) class lowercase__( tf.keras.layers.Layer ): """simple docstring""" def __init__( self : Optional[int] , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int , **SCREAMING_SNAKE_CASE_ : int ) -> Optional[Any]: super().__init__(**SCREAMING_SNAKE_CASE_ ) lowercase_ = tf.keras.layers.GlobalAveragePoolingaD(keepdims=SCREAMING_SNAKE_CASE_ , name='''pooler''' ) lowercase_ = [ tf.keras.layers.ConvaD(filters=SCREAMING_SNAKE_CASE_ , kernel_size=1 , activation='''relu''' , name='''attention.0''' ), tf.keras.layers.ConvaD(filters=SCREAMING_SNAKE_CASE_ , kernel_size=1 , activation='''sigmoid''' , name='''attention.2''' ), ] def _lowercase ( self : str , SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> Dict: # [batch_size, h, w, num_channels] -> [batch_size, 1, 1, num_channels] lowercase_ = self.pooler(SCREAMING_SNAKE_CASE_ ) for layer_module in self.attention: lowercase_ = layer_module(SCREAMING_SNAKE_CASE_ ) lowercase_ = hidden_state * pooled return hidden_state class lowercase__( tf.keras.layers.Layer ): """simple docstring""" def __init__( self : str , SCREAMING_SNAKE_CASE_ : RegNetConfig , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int = 1 , **SCREAMING_SNAKE_CASE_ : Union[str, Any] ) -> List[str]: super().__init__(**SCREAMING_SNAKE_CASE_ ) lowercase_ = in_channels != out_channels or stride != 1 lowercase_ = max(1 , out_channels // config.groups_width ) lowercase_ = ( TFRegNetShortCut(SCREAMING_SNAKE_CASE_ , stride=SCREAMING_SNAKE_CASE_ , name='''shortcut''' ) if should_apply_shortcut else tf.keras.layers.Activation('''linear''' , name='''shortcut''' ) ) # `self.layers` instead of `self.layer` because that is a reserved argument. lowercase_ = [ TFRegNetConvLayer(SCREAMING_SNAKE_CASE_ , kernel_size=1 , activation=config.hidden_act , name='''layer.0''' ), TFRegNetConvLayer( SCREAMING_SNAKE_CASE_ , stride=SCREAMING_SNAKE_CASE_ , groups=SCREAMING_SNAKE_CASE_ , activation=config.hidden_act , name='''layer.1''' ), TFRegNetConvLayer(SCREAMING_SNAKE_CASE_ , kernel_size=1 , activation=SCREAMING_SNAKE_CASE_ , name='''layer.2''' ), ] lowercase_ = ACTaFN[config.hidden_act] def _lowercase ( self : List[str] , SCREAMING_SNAKE_CASE_ : Optional[int] ) -> Any: lowercase_ = hidden_state for layer_module in self.layers: lowercase_ = layer_module(SCREAMING_SNAKE_CASE_ ) lowercase_ = self.shortcut(SCREAMING_SNAKE_CASE_ ) hidden_state += residual lowercase_ = self.activation(SCREAMING_SNAKE_CASE_ ) return hidden_state class lowercase__( tf.keras.layers.Layer ): """simple docstring""" def __init__( self : Any , SCREAMING_SNAKE_CASE_ : RegNetConfig , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int = 1 , **SCREAMING_SNAKE_CASE_ : List[str] ) -> int: super().__init__(**SCREAMING_SNAKE_CASE_ ) lowercase_ = in_channels != out_channels or stride != 1 lowercase_ = max(1 , out_channels // config.groups_width ) lowercase_ = ( TFRegNetShortCut(SCREAMING_SNAKE_CASE_ , stride=SCREAMING_SNAKE_CASE_ , name='''shortcut''' ) if should_apply_shortcut else tf.keras.layers.Activation('''linear''' , name='''shortcut''' ) ) lowercase_ = [ TFRegNetConvLayer(SCREAMING_SNAKE_CASE_ , kernel_size=1 , activation=config.hidden_act , name='''layer.0''' ), TFRegNetConvLayer( SCREAMING_SNAKE_CASE_ , stride=SCREAMING_SNAKE_CASE_ , groups=SCREAMING_SNAKE_CASE_ , activation=config.hidden_act , name='''layer.1''' ), TFRegNetSELayer(SCREAMING_SNAKE_CASE_ , reduced_channels=int(round(in_channels / 4 ) ) , name='''layer.2''' ), TFRegNetConvLayer(SCREAMING_SNAKE_CASE_ , kernel_size=1 , activation=SCREAMING_SNAKE_CASE_ , name='''layer.3''' ), ] lowercase_ = ACTaFN[config.hidden_act] def _lowercase ( self : int , SCREAMING_SNAKE_CASE_ : Dict ) -> Optional[Any]: lowercase_ = hidden_state for layer_module in self.layers: lowercase_ = layer_module(SCREAMING_SNAKE_CASE_ ) lowercase_ = self.shortcut(SCREAMING_SNAKE_CASE_ ) hidden_state += residual lowercase_ = self.activation(SCREAMING_SNAKE_CASE_ ) return hidden_state class lowercase__( tf.keras.layers.Layer ): """simple docstring""" def __init__( self : str , SCREAMING_SNAKE_CASE_ : RegNetConfig , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int = 2 , SCREAMING_SNAKE_CASE_ : int = 2 , **SCREAMING_SNAKE_CASE_ : List[Any] ) -> List[str]: super().__init__(**SCREAMING_SNAKE_CASE_ ) lowercase_ = TFRegNetXLayer if config.layer_type == '''x''' else TFRegNetYLayer lowercase_ = [ # downsampling is done in the first layer with stride of 2 layer(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , stride=SCREAMING_SNAKE_CASE_ , name='''layers.0''' ), *[layer(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , name=f'''layers.{i+1}''' ) for i in range(depth - 1 )], ] def _lowercase ( self : Tuple , SCREAMING_SNAKE_CASE_ : int ) -> int: for layer_module in self.layers: lowercase_ = layer_module(SCREAMING_SNAKE_CASE_ ) return hidden_state class lowercase__( tf.keras.layers.Layer ): """simple docstring""" def __init__( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : RegNetConfig , **SCREAMING_SNAKE_CASE_ : Dict ) -> Tuple: super().__init__(**SCREAMING_SNAKE_CASE_ ) lowercase_ = [] # based on `downsample_in_first_stage`, the first layer of the first stage may or may not downsample the input self.stages.append( TFRegNetStage( SCREAMING_SNAKE_CASE_ , config.embedding_size , config.hidden_sizes[0] , stride=2 if config.downsample_in_first_stage else 1 , depth=config.depths[0] , name='''stages.0''' , ) ) lowercase_ = zip(config.hidden_sizes , config.hidden_sizes[1:] ) for i, ((in_channels, out_channels), depth) in enumerate(zip(SCREAMING_SNAKE_CASE_ , config.depths[1:] ) ): self.stages.append(TFRegNetStage(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , depth=SCREAMING_SNAKE_CASE_ , name=f'''stages.{i+1}''' ) ) def _lowercase ( self : Dict , SCREAMING_SNAKE_CASE_ : tf.Tensor , SCREAMING_SNAKE_CASE_ : bool = False , SCREAMING_SNAKE_CASE_ : bool = True ) -> TFBaseModelOutputWithNoAttention: lowercase_ = () if output_hidden_states else None for stage_module in self.stages: if output_hidden_states: lowercase_ = hidden_states + (hidden_state,) lowercase_ = stage_module(SCREAMING_SNAKE_CASE_ ) if output_hidden_states: lowercase_ = hidden_states + (hidden_state,) if not return_dict: return tuple(v for v in [hidden_state, hidden_states] if v is not None ) return TFBaseModelOutputWithNoAttention(last_hidden_state=SCREAMING_SNAKE_CASE_ , hidden_states=SCREAMING_SNAKE_CASE_ ) @keras_serializable class lowercase__( tf.keras.layers.Layer ): """simple docstring""" a :str = RegNetConfig def __init__( self : str , SCREAMING_SNAKE_CASE_ : Dict , **SCREAMING_SNAKE_CASE_ : List[str] ) -> Any: super().__init__(**SCREAMING_SNAKE_CASE_ ) lowercase_ = config lowercase_ = TFRegNetEmbeddings(SCREAMING_SNAKE_CASE_ , name='''embedder''' ) lowercase_ = TFRegNetEncoder(SCREAMING_SNAKE_CASE_ , name='''encoder''' ) lowercase_ = tf.keras.layers.GlobalAveragePoolingaD(keepdims=SCREAMING_SNAKE_CASE_ , name='''pooler''' ) @unpack_inputs def _lowercase ( self : Dict , SCREAMING_SNAKE_CASE_ : tf.Tensor , SCREAMING_SNAKE_CASE_ : Optional[bool] = None , SCREAMING_SNAKE_CASE_ : Optional[bool] = None , SCREAMING_SNAKE_CASE_ : bool = False , ) -> TFBaseModelOutputWithPoolingAndNoAttention: lowercase_ = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) lowercase_ = return_dict if return_dict is not None else self.config.use_return_dict lowercase_ = self.embedder(SCREAMING_SNAKE_CASE_ , training=SCREAMING_SNAKE_CASE_ ) lowercase_ = self.encoder( SCREAMING_SNAKE_CASE_ , output_hidden_states=SCREAMING_SNAKE_CASE_ , return_dict=SCREAMING_SNAKE_CASE_ , training=SCREAMING_SNAKE_CASE_ ) lowercase_ = encoder_outputs[0] lowercase_ = self.pooler(SCREAMING_SNAKE_CASE_ ) # Change to NCHW output format have uniformity in the modules lowercase_ = tf.transpose(SCREAMING_SNAKE_CASE_ , perm=(0, 3, 1, 2) ) lowercase_ = tf.transpose(SCREAMING_SNAKE_CASE_ , perm=(0, 3, 1, 2) ) # Change the other hidden state outputs to NCHW as well if output_hidden_states: lowercase_ = tuple([tf.transpose(SCREAMING_SNAKE_CASE_ , perm=(0, 3, 1, 2) ) for h in encoder_outputs[1]] ) if not return_dict: return (last_hidden_state, pooled_output) + encoder_outputs[1:] return TFBaseModelOutputWithPoolingAndNoAttention( last_hidden_state=SCREAMING_SNAKE_CASE_ , pooler_output=SCREAMING_SNAKE_CASE_ , hidden_states=hidden_states if output_hidden_states else encoder_outputs.hidden_states , ) class lowercase__( UpperCAmelCase ): """simple docstring""" a :Tuple = RegNetConfig a :Any = 'regnet' a :List[str] = 'pixel_values' @property def _lowercase ( self : List[str] ) -> str: return {"pixel_values": tf.TensorSpec(shape=(None, self.config.num_channels, 2_2_4, 2_2_4) , dtype=tf.floataa )} __a = r'\n Parameters:\n This model is a Tensorflow\n [tf.keras.layers.Layer](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Layer) sub-class. Use it as a\n regular Tensorflow Module and refer to the Tensorflow documentation for all matter related to general usage and\n behavior.\n config ([`RegNetConfig`]): Model configuration class with all the parameters of the model.\n Initializing with a config file does not load the weights associated with the model, only the\n configuration. Check out the [`~TFPreTrainedModel.from_pretrained`] method to load the model weights.\n' __a = r'\n Args:\n pixel_values (`tf.Tensor` of shape `(batch_size, num_channels, height, width)`):\n Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n [`ConveNextImageProcessor.__call__`] for details.\n output_hidden_states (`bool`, *optional*):\n Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for\n more detail.\n return_dict (`bool`, *optional*):\n Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n' @add_start_docstrings( 'The bare RegNet model outputting raw features without any specific head on top.' , UpperCAmelCase , ) class lowercase__( UpperCAmelCase ): """simple docstring""" def __init__( self : Optional[int] , SCREAMING_SNAKE_CASE_ : RegNetConfig , *SCREAMING_SNAKE_CASE_ : Any , **SCREAMING_SNAKE_CASE_ : str ) -> List[str]: super().__init__(SCREAMING_SNAKE_CASE_ , *SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) lowercase_ = TFRegNetMainLayer(SCREAMING_SNAKE_CASE_ , name='''regnet''' ) @unpack_inputs @add_start_docstrings_to_model_forward(SCREAMING_SNAKE_CASE_ ) @add_code_sample_docstrings( checkpoint=_CHECKPOINT_FOR_DOC , output_type=SCREAMING_SNAKE_CASE_ , config_class=_CONFIG_FOR_DOC , modality='''vision''' , expected_output=_EXPECTED_OUTPUT_SHAPE , ) def _lowercase ( self : List[str] , SCREAMING_SNAKE_CASE_ : tf.Tensor , SCREAMING_SNAKE_CASE_ : Optional[bool] = None , SCREAMING_SNAKE_CASE_ : Optional[bool] = None , SCREAMING_SNAKE_CASE_ : Optional[Any]=False , ) -> Union[TFBaseModelOutputWithPoolingAndNoAttention, Tuple[tf.Tensor]]: lowercase_ = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) lowercase_ = return_dict if return_dict is not None else self.config.use_return_dict lowercase_ = self.regnet( pixel_values=SCREAMING_SNAKE_CASE_ , output_hidden_states=SCREAMING_SNAKE_CASE_ , return_dict=SCREAMING_SNAKE_CASE_ , training=SCREAMING_SNAKE_CASE_ , ) if not return_dict: return (outputs[0],) + outputs[1:] return TFBaseModelOutputWithPoolingAndNoAttention( last_hidden_state=outputs.last_hidden_state , pooler_output=outputs.pooler_output , hidden_states=outputs.hidden_states , ) @add_start_docstrings( '\n RegNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n ImageNet.\n ' , UpperCAmelCase , ) class lowercase__( UpperCAmelCase , UpperCAmelCase ): """simple docstring""" def __init__( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : RegNetConfig , *SCREAMING_SNAKE_CASE_ : str , **SCREAMING_SNAKE_CASE_ : int ) -> Union[str, Any]: super().__init__(SCREAMING_SNAKE_CASE_ , *SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) lowercase_ = config.num_labels lowercase_ = TFRegNetMainLayer(SCREAMING_SNAKE_CASE_ , name='''regnet''' ) # classification head lowercase_ = [ tf.keras.layers.Flatten(), tf.keras.layers.Dense(config.num_labels , name='''classifier.1''' ) if config.num_labels > 0 else tf.identity, ] @unpack_inputs @add_start_docstrings_to_model_forward(SCREAMING_SNAKE_CASE_ ) @add_code_sample_docstrings( checkpoint=_IMAGE_CLASS_CHECKPOINT , output_type=SCREAMING_SNAKE_CASE_ , config_class=_CONFIG_FOR_DOC , expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT , ) def _lowercase ( self : List[Any] , SCREAMING_SNAKE_CASE_ : tf.Tensor = None , SCREAMING_SNAKE_CASE_ : tf.Tensor = None , SCREAMING_SNAKE_CASE_ : bool = None , SCREAMING_SNAKE_CASE_ : bool = None , SCREAMING_SNAKE_CASE_ : Dict=False , ) -> Union[TFSequenceClassifierOutput, Tuple[tf.Tensor]]: lowercase_ = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) lowercase_ = return_dict if return_dict is not None else self.config.use_return_dict lowercase_ = self.regnet( SCREAMING_SNAKE_CASE_ , output_hidden_states=SCREAMING_SNAKE_CASE_ , return_dict=SCREAMING_SNAKE_CASE_ , training=SCREAMING_SNAKE_CASE_ ) lowercase_ = outputs.pooler_output if return_dict else outputs[1] lowercase_ = self.classifier[0](SCREAMING_SNAKE_CASE_ ) lowercase_ = self.classifier[1](SCREAMING_SNAKE_CASE_ ) lowercase_ = None if labels is None else self.hf_compute_loss(labels=SCREAMING_SNAKE_CASE_ , logits=SCREAMING_SNAKE_CASE_ ) if not return_dict: lowercase_ = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return TFSequenceClassifierOutput(loss=SCREAMING_SNAKE_CASE_ , logits=SCREAMING_SNAKE_CASE_ , hidden_states=outputs.hidden_states )
30
0
import multiprocessing import time from arguments import PretokenizationArguments from datasets import load_dataset from transformers import AutoTokenizer, HfArgumentParser def A_ ( _lowerCAmelCase ) -> List[Any]: UpperCamelCase : List[Any] = {} UpperCamelCase : Optional[int] = tokenizer(example["content"] , truncation=snake_case__ )["input_ids"] UpperCamelCase : str = len(example["content"] ) / len(output["input_ids"] ) return output __lowerCamelCase : Union[str, Any] = HfArgumentParser(PretokenizationArguments) __lowerCamelCase : Union[str, Any] = parser.parse_args() if args.num_workers is None: __lowerCamelCase : Optional[Any] = multiprocessing.cpu_count() __lowerCamelCase : str = AutoTokenizer.from_pretrained(args.tokenizer_dir) __lowerCamelCase : Optional[Any] = time.time() __lowerCamelCase : Dict = load_dataset(args.dataset_name, split="""train""") print(f"""Dataset loaded in {time.time()-t_start:.2f}s""") __lowerCamelCase : Optional[int] = time.time() __lowerCamelCase : Optional[int] = ds.map( tokenize, num_proc=args.num_workers, remove_columns=[ """repo_name""", """path""", """copies""", """size""", """content""", """license""", """hash""", """line_mean""", """line_max""", """alpha_frac""", """autogenerated""", ], ) print(f"""Dataset tokenized in {time.time()-t_start:.2f}s""") __lowerCamelCase : str = time.time() ds.push_to_hub(args.tokenized_data_repo) print(f"""Data pushed to the hub in {time.time()-t_start:.2f}s""")
52
import importlib.metadata import warnings from copy import deepcopy from packaging import version from ..utils import logging from .import_utils import is_accelerate_available, is_bitsandbytes_available if is_bitsandbytes_available(): import bitsandbytes as bnb import torch import torch.nn as nn from ..pytorch_utils import ConvaD if is_accelerate_available(): from accelerate import init_empty_weights from accelerate.utils import find_tied_parameters __a = logging.get_logger(__name__) def a ( snake_case__: Optional[int] , snake_case__: Dict , snake_case__: int , snake_case__: List[str]=None , snake_case__: List[Any]=None ): '''simple docstring''' # Recurse if needed if "." in tensor_name: lowercase_ = tensor_name.split('''.''' ) for split in splits[:-1]: lowercase_ = getattr(snake_case__ , snake_case__ ) if new_module is None: raise ValueError(F'''{module} has no attribute {split}.''' ) lowercase_ = new_module lowercase_ = splits[-1] if tensor_name not in module._parameters and tensor_name not in module._buffers: raise ValueError(F'''{module} does not have a parameter or a buffer named {tensor_name}.''' ) lowercase_ = tensor_name in module._buffers lowercase_ = getattr(snake_case__ , snake_case__ ) if old_value.device == torch.device('''meta''' ) and device not in ["meta", torch.device('''meta''' )] and value is None: raise ValueError(F'''{tensor_name} is on the meta device, we need a `value` to put in on {device}.''' ) lowercase_ = False lowercase_ = False if is_buffer or not is_bitsandbytes_available(): lowercase_ = False lowercase_ = False else: lowercase_ = hasattr(bnb.nn , '''Params4bit''' ) and isinstance(module._parameters[tensor_name] , bnb.nn.Paramsabit ) lowercase_ = isinstance(module._parameters[tensor_name] , bnb.nn.IntaParams ) if is_abit or is_abit: lowercase_ = module._parameters[tensor_name] if param.device.type != "cuda": if value is None: lowercase_ = old_value.to(snake_case__ ) elif isinstance(snake_case__ , torch.Tensor ): lowercase_ = value.to('''cpu''' ) if value.dtype == torch.inta: lowercase_ = version.parse(importlib.metadata.version('''bitsandbytes''' ) ) > version.parse( '''0.37.2''' ) if not is_abit_serializable: raise ValueError( '''Detected int8 weights but the version of bitsandbytes is not compatible with int8 serialization. ''' '''Make sure to download the latest `bitsandbytes` version. `pip install --upgrade bitsandbytes`.''' ) else: lowercase_ = torch.tensor(snake_case__ , device='''cpu''' ) # Support models using `Conv1D` in place of `nn.Linear` (e.g. gpt2) by transposing the weight matrix prior to quantization. # Since weights are saved in the correct "orientation", we skip transposing when loading. if issubclass(module.source_cls , snake_case__ ) and fpaa_statistics is None: lowercase_ = new_value.T lowercase_ = old_value.__dict__ if is_abit: lowercase_ = bnb.nn.IntaParams(snake_case__ , requires_grad=snake_case__ , **snake_case__ ).to(snake_case__ ) elif is_abit: lowercase_ = bnb.nn.Paramsabit(snake_case__ , requires_grad=snake_case__ , **snake_case__ ).to(snake_case__ ) lowercase_ = new_value if fpaa_statistics is not None: setattr(module.weight , '''SCB''' , fpaa_statistics.to(snake_case__ ) ) else: if value is None: lowercase_ = old_value.to(snake_case__ ) elif isinstance(snake_case__ , torch.Tensor ): lowercase_ = value.to(snake_case__ ) else: lowercase_ = torch.tensor(snake_case__ , device=snake_case__ ) if is_buffer: lowercase_ = new_value else: lowercase_ = nn.Parameter(snake_case__ , requires_grad=old_value.requires_grad ) lowercase_ = new_value def a ( snake_case__: str , snake_case__: Union[str, Any]=None , snake_case__: Any=None , snake_case__: List[str]=None , snake_case__: Optional[Any]=False ): '''simple docstring''' for name, module in model.named_children(): if current_key_name is None: lowercase_ = [] current_key_name.append(snake_case__ ) if (isinstance(snake_case__ , nn.Linear ) or isinstance(snake_case__ , snake_case__ )) and name not in modules_to_not_convert: # Check if the current key is not in the `modules_to_not_convert` if not any(key in '''.'''.join(snake_case__ ) for key in modules_to_not_convert ): with init_empty_weights(): if isinstance(snake_case__ , snake_case__ ): lowercase_ , lowercase_ = module.weight.shape else: lowercase_ = module.in_features lowercase_ = module.out_features if quantization_config.quantization_method() == "llm_int8": lowercase_ = bnb.nn.LinearabitLt( snake_case__ , snake_case__ , module.bias is not None , has_fpaa_weights=quantization_config.llm_inta_has_fpaa_weight , threshold=quantization_config.llm_inta_threshold , ) lowercase_ = True else: if ( quantization_config.llm_inta_skip_modules is not None and name in quantization_config.llm_inta_skip_modules ): pass else: lowercase_ = bnb.nn.Linearabit( snake_case__ , snake_case__ , module.bias is not None , quantization_config.bnb_abit_compute_dtype , compress_statistics=quantization_config.bnb_abit_use_double_quant , quant_type=quantization_config.bnb_abit_quant_type , ) lowercase_ = True # Store the module class in case we need to transpose the weight later lowercase_ = type(snake_case__ ) # Force requires grad to False to avoid unexpected errors model._modules[name].requires_grad_(snake_case__ ) if len(list(module.children() ) ) > 0: lowercase_ , lowercase_ = _replace_with_bnb_linear( snake_case__ , snake_case__ , snake_case__ , snake_case__ , has_been_replaced=snake_case__ , ) # Remove the last key for recursion current_key_name.pop(-1 ) return model, has_been_replaced def a ( snake_case__: Any , snake_case__: Any=None , snake_case__: Union[str, Any]=None , snake_case__: str=None ): '''simple docstring''' lowercase_ = ['''lm_head'''] if modules_to_not_convert is None else modules_to_not_convert lowercase_ , lowercase_ = _replace_with_bnb_linear( snake_case__ , snake_case__ , snake_case__ , snake_case__ ) if not has_been_replaced: logger.warning( '''You are loading your model in 8bit or 4bit but no linear modules were found in your model.''' ''' Please double check your model architecture, or submit an issue on github if you think this is''' ''' a bug.''' ) return model def a ( *snake_case__: str , **snake_case__: Dict ): '''simple docstring''' warnings.warn( '''`replace_8bit_linear` will be deprecated in a future version, please use `replace_with_bnb_linear` instead''' , snake_case__ , ) return replace_with_bnb_linear(*snake_case__ , **snake_case__ ) def a ( *snake_case__: Any , **snake_case__: List[Any] ): '''simple docstring''' warnings.warn( '''`set_module_8bit_tensor_to_device` will be deprecated in a future version, please use `set_module_quantized_tensor_to_device` instead''' , snake_case__ , ) return set_module_quantized_tensor_to_device(*snake_case__ , **snake_case__ ) def a ( snake_case__: Optional[Any] ): '''simple docstring''' lowercase_ = deepcopy(snake_case__ ) # this has 0 cost since it is done inside `init_empty_weights` context manager` tied_model.tie_weights() lowercase_ = find_tied_parameters(snake_case__ ) # For compatibility with Accelerate < 0.18 if isinstance(snake_case__ , snake_case__ ): lowercase_ = sum(list(tied_params.values() ) , [] ) + list(tied_params.keys() ) else: lowercase_ = sum(snake_case__ , [] ) lowercase_ = len(snake_case__ ) > 0 # Check if it is a base model lowercase_ = not hasattr(snake_case__ , model.base_model_prefix ) # Ignore this for base models (BertModel, GPT2Model, etc.) if (not has_tied_params) and is_base_model: return [] # otherwise they have an attached head lowercase_ = list(model.named_children() ) lowercase_ = [list_modules[-1][0]] # add last module together with tied weights lowercase_ = set(snake_case__ ) - set(snake_case__ ) lowercase_ = list(set(snake_case__ ) ) + list(snake_case__ ) # remove ".weight" from the keys lowercase_ = ['''.weight''', '''.bias'''] lowercase_ = [] for name in list_untouched: for name_to_remove in names_to_remove: if name_to_remove in name: lowercase_ = name.replace(snake_case__ , '''''' ) filtered_module_names.append(snake_case__ ) return filtered_module_names
30
0
"""simple docstring""" from dataclasses import dataclass from typing import Dict, Optional, Union import torch import torch.nn.functional as F from torch import nn from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput from .attention import BasicTransformerBlock from .attention_processor import AttentionProcessor, AttnProcessor from .embeddings import TimestepEmbedding, Timesteps from .modeling_utils import ModelMixin @dataclass class __lowerCamelCase ( a__ ): '''simple docstring''' A_ : torch.FloatTensor class __lowerCamelCase ( a__ , a__ ): '''simple docstring''' @register_to_config def __init__( self , __UpperCAmelCase = 32 , __UpperCAmelCase = 64 , __UpperCAmelCase = 20 , __UpperCAmelCase = 768 , __UpperCAmelCase=77 , __UpperCAmelCase=4 , __UpperCAmelCase = 0.0 , __UpperCAmelCase = "silu" , __UpperCAmelCase = None , __UpperCAmelCase = None , __UpperCAmelCase = "linear" , __UpperCAmelCase = "prd" , __UpperCAmelCase = None , __UpperCAmelCase = None , __UpperCAmelCase = None , ) -> Tuple: super().__init__() _a = num_attention_heads _a = attention_head_dim _a = num_attention_heads * attention_head_dim _a = additional_embeddings _a = time_embed_dim or inner_dim _a = embedding_proj_dim or embedding_dim _a = clip_embed_dim or embedding_dim _a = Timesteps(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , 0 ) _a = TimestepEmbedding(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , out_dim=SCREAMING_SNAKE_CASE_ , act_fn=SCREAMING_SNAKE_CASE_ ) _a = nn.Linear(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if embedding_proj_norm_type is None: _a = None elif embedding_proj_norm_type == "layer": _a = nn.LayerNorm(SCREAMING_SNAKE_CASE_ ) else: raise ValueError(F'unsupported embedding_proj_norm_type: {embedding_proj_norm_type}' ) _a = nn.Linear(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if encoder_hid_proj_type is None: _a = None elif encoder_hid_proj_type == "linear": _a = nn.Linear(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else: raise ValueError(F'unsupported encoder_hid_proj_type: {encoder_hid_proj_type}' ) _a = nn.Parameter(torch.zeros(1 , num_embeddings + additional_embeddings , SCREAMING_SNAKE_CASE_ ) ) if added_emb_type == "prd": _a = nn.Parameter(torch.zeros(1 , 1 , SCREAMING_SNAKE_CASE_ ) ) elif added_emb_type is None: _a = None else: raise ValueError( F'`added_emb_type`: {added_emb_type} is not supported. Make sure to choose one of `\'prd\'` or `None`.' ) _a = nn.ModuleList( [ BasicTransformerBlock( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , dropout=SCREAMING_SNAKE_CASE_ , activation_fn='''gelu''' , attention_bias=SCREAMING_SNAKE_CASE_ , ) for d in range(SCREAMING_SNAKE_CASE_ ) ] ) if norm_in_type == "layer": _a = nn.LayerNorm(SCREAMING_SNAKE_CASE_ ) elif norm_in_type is None: _a = None else: raise ValueError(F'Unsupported norm_in_type: {norm_in_type}.' ) _a = nn.LayerNorm(SCREAMING_SNAKE_CASE_ ) _a = nn.Linear(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) _a = torch.full( [num_embeddings + additional_embeddings, num_embeddings + additional_embeddings] , -10000.0 ) causal_attention_mask.triu_(1 ) _a = causal_attention_mask[None, ...] self.register_buffer('''causal_attention_mask''' , SCREAMING_SNAKE_CASE_ , persistent=SCREAMING_SNAKE_CASE_ ) _a = nn.Parameter(torch.zeros(1 , SCREAMING_SNAKE_CASE_ ) ) _a = nn.Parameter(torch.zeros(1 , SCREAMING_SNAKE_CASE_ ) ) @property # Copied from diffusers.models.unet_2d_condition.UNet2DConditionModel.attn_processors def _UpperCAmelCase ( self ) -> Dict[str, AttentionProcessor]: _a = {} def fn_recursive_add_processors(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ): if hasattr(SCREAMING_SNAKE_CASE_ , '''set_processor''' ): _a = module.processor for sub_name, child in module.named_children(): fn_recursive_add_processors(F'{name}.{sub_name}' , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) return processors for name, module in self.named_children(): fn_recursive_add_processors(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) return processors def _UpperCAmelCase ( self , __UpperCAmelCase ) -> int: _a = len(self.attn_processors.keys() ) if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) and len(SCREAMING_SNAKE_CASE_ ) != count: raise ValueError( F'A dict of processors was passed, but the number of processors {len(SCREAMING_SNAKE_CASE_ )} does not match the' F' number of attention layers: {count}. Please make sure to pass {count} processor classes.' ) def fn_recursive_attn_processor(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ): if hasattr(SCREAMING_SNAKE_CASE_ , '''set_processor''' ): if not isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): module.set_processor(SCREAMING_SNAKE_CASE_ ) else: module.set_processor(processor.pop(F'{name}.processor' ) ) for sub_name, child in module.named_children(): fn_recursive_attn_processor(F'{name}.{sub_name}' , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) for name, module in self.named_children(): fn_recursive_attn_processor(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def _UpperCAmelCase ( self ) -> Optional[int]: self.set_attn_processor(AttnProcessor() ) def _UpperCAmelCase ( self , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase = None , __UpperCAmelCase = None , __UpperCAmelCase = True , ) -> List[Any]: _a = hidden_states.shape[0] _a = timestep if not torch.is_tensor(SCREAMING_SNAKE_CASE_ ): _a = torch.tensor([timesteps] , dtype=torch.long , device=hidden_states.device ) elif torch.is_tensor(SCREAMING_SNAKE_CASE_ ) and len(timesteps.shape ) == 0: _a = timesteps[None].to(hidden_states.device ) # broadcast to batch dimension in a way that's compatible with ONNX/Core ML _a = timesteps * torch.ones(SCREAMING_SNAKE_CASE_ , dtype=timesteps.dtype , device=timesteps.device ) _a = self.time_proj(SCREAMING_SNAKE_CASE_ ) # timesteps does not contain any weights and will always return f32 tensors # but time_embedding might be fp16, so we need to cast here. _a = timesteps_projected.to(dtype=self.dtype ) _a = self.time_embedding(SCREAMING_SNAKE_CASE_ ) if self.embedding_proj_norm is not None: _a = self.embedding_proj_norm(SCREAMING_SNAKE_CASE_ ) _a = self.embedding_proj(SCREAMING_SNAKE_CASE_ ) if self.encoder_hidden_states_proj is not None and encoder_hidden_states is not None: _a = self.encoder_hidden_states_proj(SCREAMING_SNAKE_CASE_ ) elif self.encoder_hidden_states_proj is not None and encoder_hidden_states is None: raise ValueError('''`encoder_hidden_states_proj` requires `encoder_hidden_states` to be set''' ) _a = self.proj_in(SCREAMING_SNAKE_CASE_ ) _a = self.positional_embedding.to(hidden_states.dtype ) _a = [] _a = 0 if encoder_hidden_states is not None: additional_embeds.append(SCREAMING_SNAKE_CASE_ ) additional_embeddings_len += encoder_hidden_states.shape[1] if len(proj_embeddings.shape ) == 2: _a = proj_embeddings[:, None, :] if len(hidden_states.shape ) == 2: _a = hidden_states[:, None, :] _a = additional_embeds + [ proj_embeddings, time_embeddings[:, None, :], hidden_states, ] if self.prd_embedding is not None: _a = self.prd_embedding.to(hidden_states.dtype ).expand(SCREAMING_SNAKE_CASE_ , -1 , -1 ) additional_embeds.append(SCREAMING_SNAKE_CASE_ ) _a = torch.cat( SCREAMING_SNAKE_CASE_ , dim=1 , ) # Allow positional_embedding to not include the `addtional_embeddings` and instead pad it with zeros for these additional tokens _a = additional_embeddings_len + proj_embeddings.shape[1] + 1 if positional_embeddings.shape[1] < hidden_states.shape[1]: _a = F.pad( SCREAMING_SNAKE_CASE_ , ( 0, 0, additional_embeddings_len, self.prd_embedding.shape[1] if self.prd_embedding is not None else 0, ) , value=0.0 , ) _a = hidden_states + positional_embeddings if attention_mask is not None: _a = (1 - attention_mask.to(hidden_states.dtype )) * -10000.0 _a = F.pad(SCREAMING_SNAKE_CASE_ , (0, self.additional_embeddings) , value=0.0 ) _a = (attention_mask[:, None, :] + self.causal_attention_mask).to(hidden_states.dtype ) _a = attention_mask.repeat_interleave(self.config.num_attention_heads , dim=0 ) if self.norm_in is not None: _a = self.norm_in(SCREAMING_SNAKE_CASE_ ) for block in self.transformer_blocks: _a = block(SCREAMING_SNAKE_CASE_ , attention_mask=SCREAMING_SNAKE_CASE_ ) _a = self.norm_out(SCREAMING_SNAKE_CASE_ ) if self.prd_embedding is not None: _a = hidden_states[:, -1] else: _a = hidden_states[:, additional_embeddings_len:] _a = self.proj_to_clip_embeddings(SCREAMING_SNAKE_CASE_ ) if not return_dict: return (predicted_image_embedding,) return PriorTransformerOutput(predicted_image_embedding=SCREAMING_SNAKE_CASE_ ) def _UpperCAmelCase ( self , __UpperCAmelCase ) -> Dict: _a = (prior_latents * self.clip_std) + self.clip_mean return prior_latents
320
import argparse import os import re __a = 'src/transformers/models/auto' # re pattern that matches mapping introductions: # SUPER_MODEL_MAPPING_NAMES = OrderedDict or SUPER_MODEL_MAPPING = OrderedDict __a = re.compile(r'[A-Z_]+_MAPPING(\s+|_[A-Z_]+\s+)=\s+OrderedDict') # re pattern that matches identifiers in mappings __a = re.compile(r'\s*\(\s*"(\S[^"]+)"') def a ( snake_case__: str , snake_case__: bool = False ): '''simple docstring''' with open(snake_case__ , '''r''' , encoding='''utf-8''' ) as f: lowercase_ = f.read() lowercase_ = content.split('''\n''' ) lowercase_ = [] lowercase_ = 0 while line_idx < len(snake_case__ ): if _re_intro_mapping.search(lines[line_idx] ) is not None: lowercase_ = len(re.search(r'''^(\s*)\S''' , lines[line_idx] ).groups()[0] ) + 8 # Start of a new mapping! while not lines[line_idx].startswith(''' ''' * indent + '''(''' ): new_lines.append(lines[line_idx] ) line_idx += 1 lowercase_ = [] while lines[line_idx].strip() != "]": # Blocks either fit in one line or not if lines[line_idx].strip() == "(": lowercase_ = line_idx while not lines[line_idx].startswith(''' ''' * indent + ''')''' ): line_idx += 1 blocks.append('''\n'''.join(lines[start_idx : line_idx + 1] ) ) else: blocks.append(lines[line_idx] ) line_idx += 1 # Sort blocks by their identifiers lowercase_ = sorted(snake_case__ , key=lambda snake_case__ : _re_identifier.search(snake_case__ ).groups()[0] ) new_lines += blocks else: new_lines.append(lines[line_idx] ) line_idx += 1 if overwrite: with open(snake_case__ , '''w''' , encoding='''utf-8''' ) as f: f.write('''\n'''.join(snake_case__ ) ) elif "\n".join(snake_case__ ) != content: return True def a ( snake_case__: bool = False ): '''simple docstring''' lowercase_ = [os.path.join(snake_case__ , snake_case__ ) for f in os.listdir(snake_case__ ) if f.endswith('''.py''' )] lowercase_ = [sort_auto_mapping(snake_case__ , overwrite=snake_case__ ) for fname in fnames] if not overwrite and any(snake_case__ ): lowercase_ = [f for f, d in zip(snake_case__ , snake_case__ ) if d] raise ValueError( F'''The following files have auto mappings that need sorting: {', '.join(snake_case__ )}. Run `make style` to fix''' ''' this.''' ) if __name__ == "__main__": __a = argparse.ArgumentParser() parser.add_argument('--check_only', action='store_true', help='Whether to only check or fix style.') __a = parser.parse_args() sort_all_auto_mappings(not args.check_only)
30
0
"""simple docstring""" from typing import Optional, Union import torch from torch import nn from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss from ...activations import ACTaFN from ...modeling_outputs import BaseModelOutputWithPoolingAndNoAttention, ImageClassifierOutputWithNoAttention from ...modeling_utils import PreTrainedModel from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging from .configuration_mobilenet_va import MobileNetVaConfig a = logging.get_logger(__name__) # General docstring a = 'MobileNetV1Config' # Base docstring a = 'google/mobilenet_v1_1.0_224' a = [1, 1_0_2_4, 7, 7] # Image classification docstring a = 'google/mobilenet_v1_1.0_224' a = 'tabby, tabby cat' a = [ 'google/mobilenet_v1_1.0_224', 'google/mobilenet_v1_0.75_192', # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1 ] def lowercase (snake_case__ : Union[str, Any] , snake_case__ : Union[str, Any] , snake_case__ : List[Any]=None ) -> Optional[int]: '''simple docstring''' lowerCAmelCase = {} if isinstance(snake_case__ , snake_case__ ): lowerCAmelCase = model.mobilenet_va else: lowerCAmelCase = model lowerCAmelCase = """MobilenetV1/Conv2d_0/""" lowerCAmelCase = backbone.conv_stem.convolution.weight lowerCAmelCase = backbone.conv_stem.normalization.bias lowerCAmelCase = backbone.conv_stem.normalization.weight lowerCAmelCase = backbone.conv_stem.normalization.running_mean lowerCAmelCase = backbone.conv_stem.normalization.running_var for i in range(13 ): lowerCAmelCase = i + 1 lowerCAmelCase = i * 2 lowerCAmelCase = backbone.layer[pt_index] lowerCAmelCase = f'''MobilenetV1/Conv2d_{tf_index}_depthwise/''' lowerCAmelCase = pointer.convolution.weight lowerCAmelCase = pointer.normalization.bias lowerCAmelCase = pointer.normalization.weight lowerCAmelCase = pointer.normalization.running_mean lowerCAmelCase = pointer.normalization.running_var lowerCAmelCase = backbone.layer[pt_index + 1] lowerCAmelCase = f'''MobilenetV1/Conv2d_{tf_index}_pointwise/''' lowerCAmelCase = pointer.convolution.weight lowerCAmelCase = pointer.normalization.bias lowerCAmelCase = pointer.normalization.weight lowerCAmelCase = pointer.normalization.running_mean lowerCAmelCase = pointer.normalization.running_var if isinstance(snake_case__ , snake_case__ ): lowerCAmelCase = """MobilenetV1/Logits/Conv2d_1c_1x1/""" lowerCAmelCase = model.classifier.weight lowerCAmelCase = model.classifier.bias return tf_to_pt_map def lowercase (snake_case__ : Dict , snake_case__ : List[str] , snake_case__ : List[Any] ) -> List[Any]: '''simple docstring''' try: import numpy as np import tensorflow as tf except ImportError: logger.error( """Loading a TensorFlow models in PyTorch, requires TensorFlow to be installed. Please see """ """https://www.tensorflow.org/install/ for installation instructions.""" ) raise # Load weights from TF model lowerCAmelCase = tf.train.list_variables(snake_case__ ) lowerCAmelCase = {} for name, shape in init_vars: logger.info(f'''Loading TF weight {name} with shape {shape}''' ) lowerCAmelCase = tf.train.load_variable(snake_case__ , snake_case__ ) lowerCAmelCase = array # Build TF to PyTorch weights loading map lowerCAmelCase = _build_tf_to_pytorch_map(snake_case__ , snake_case__ , snake_case__ ) for name, pointer in tf_to_pt_map.items(): logger.info(f'''Importing {name}''' ) if name not in tf_weights: logger.info(f'''{name} not in tf pre-trained weights, skipping''' ) continue lowerCAmelCase = tf_weights[name] if "depthwise_weights" in name: logger.info("""Transposing depthwise""" ) lowerCAmelCase = np.transpose(snake_case__ , (2, 3, 0, 1) ) elif "weights" in name: logger.info("""Transposing""" ) if len(pointer.shape ) == 2: # copying into linear layer lowerCAmelCase = array.squeeze().transpose() else: lowerCAmelCase = np.transpose(snake_case__ , (3, 2, 0, 1) ) if pointer.shape != array.shape: raise ValueError(f'''Pointer shape {pointer.shape} and array shape {array.shape} mismatched''' ) logger.info(f'''Initialize PyTorch weight {name} {array.shape}''' ) lowerCAmelCase = torch.from_numpy(snake_case__ ) tf_weights.pop(snake_case__ , snake_case__ ) tf_weights.pop(name + """/RMSProp""" , snake_case__ ) tf_weights.pop(name + """/RMSProp_1""" , snake_case__ ) tf_weights.pop(name + """/ExponentialMovingAverage""" , snake_case__ ) logger.info(f'''Weights not copied to PyTorch model: {', '.join(tf_weights.keys() )}''' ) return model def lowercase (snake_case__ : torch.Tensor , snake_case__ : nn.Convad ) -> List[Any]: '''simple docstring''' lowerCAmelCase , lowerCAmelCase = features.shape[-2:] lowerCAmelCase , lowerCAmelCase = conv_layer.stride lowerCAmelCase , lowerCAmelCase = conv_layer.kernel_size if in_height % stride_height == 0: lowerCAmelCase = max(kernel_height - stride_height , 0 ) else: lowerCAmelCase = max(kernel_height - (in_height % stride_height) , 0 ) if in_width % stride_width == 0: lowerCAmelCase = max(kernel_width - stride_width , 0 ) else: lowerCAmelCase = max(kernel_width - (in_width % stride_width) , 0 ) lowerCAmelCase = pad_along_width // 2 lowerCAmelCase = pad_along_width - pad_left lowerCAmelCase = pad_along_height // 2 lowerCAmelCase = pad_along_height - pad_top lowerCAmelCase = (pad_left, pad_right, pad_top, pad_bottom) return nn.functional.pad(snake_case__ , snake_case__ , """constant""" , 0.0 ) class SCREAMING_SNAKE_CASE__ ( nn.Module ): def __init__( self : Tuple , lowerCAmelCase : MobileNetVaConfig , lowerCAmelCase : int , lowerCAmelCase : int , lowerCAmelCase : int , lowerCAmelCase : Optional[int] = 1 , lowerCAmelCase : Optional[int] = 1 , lowerCAmelCase : bool = False , lowerCAmelCase : Optional[bool] = True , lowerCAmelCase : Optional[bool or str] = True , ): super().__init__() lowerCAmelCase = config if in_channels % groups != 0: raise ValueError(f'''Input channels ({in_channels}) are not divisible by {groups} groups.''' ) if out_channels % groups != 0: raise ValueError(f'''Output channels ({out_channels}) are not divisible by {groups} groups.''' ) lowerCAmelCase = 0 if config.tf_padding else int((kernel_size - 1) / 2 ) lowerCAmelCase = nn.Convad( in_channels=SCREAMING_SNAKE_CASE_ , out_channels=SCREAMING_SNAKE_CASE_ , kernel_size=SCREAMING_SNAKE_CASE_ , stride=SCREAMING_SNAKE_CASE_ , padding=SCREAMING_SNAKE_CASE_ , groups=SCREAMING_SNAKE_CASE_ , bias=SCREAMING_SNAKE_CASE_ , padding_mode="""zeros""" , ) if use_normalization: lowerCAmelCase = nn.BatchNormad( num_features=SCREAMING_SNAKE_CASE_ , eps=config.layer_norm_eps , momentum=0.9997 , affine=SCREAMING_SNAKE_CASE_ , track_running_stats=SCREAMING_SNAKE_CASE_ , ) else: lowerCAmelCase = None if use_activation: if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): lowerCAmelCase = ACTaFN[use_activation] elif isinstance(config.hidden_act , SCREAMING_SNAKE_CASE_ ): lowerCAmelCase = ACTaFN[config.hidden_act] else: lowerCAmelCase = config.hidden_act else: lowerCAmelCase = None def __lowercase ( self : List[Any] , lowerCAmelCase : torch.Tensor ): if self.config.tf_padding: lowerCAmelCase = apply_tf_padding(SCREAMING_SNAKE_CASE_ , self.convolution ) lowerCAmelCase = self.convolution(SCREAMING_SNAKE_CASE_ ) if self.normalization is not None: lowerCAmelCase = self.normalization(SCREAMING_SNAKE_CASE_ ) if self.activation is not None: lowerCAmelCase = self.activation(SCREAMING_SNAKE_CASE_ ) return features class SCREAMING_SNAKE_CASE__ ( _a ): _a = MobileNetVaConfig _a = load_tf_weights_in_mobilenet_va _a = 'mobilenet_v1' _a = 'pixel_values' _a = False def __lowercase ( self : Union[str, Any] , lowerCAmelCase : Union[nn.Linear, nn.Convad] ): if isinstance(SCREAMING_SNAKE_CASE_ , (nn.Linear, nn.Convad) ): module.weight.data.normal_(mean=0.0 , std=self.config.initializer_range ) if module.bias is not None: module.bias.data.zero_() elif isinstance(SCREAMING_SNAKE_CASE_ , nn.BatchNormad ): module.bias.data.zero_() module.weight.data.fill_(1.0 ) a = R'\n This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it\n as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and\n behavior.\n\n Parameters:\n config ([`MobileNetV1Config`]): Model configuration class with all the parameters of the model.\n Initializing with a config file does not load the weights associated with the model, only the\n configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.\n' a = R'\n Args:\n pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n [`MobileNetV1ImageProcessor.__call__`] for details.\n output_hidden_states (`bool`, *optional*):\n Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for\n more detail.\n return_dict (`bool`, *optional*):\n Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n' @add_start_docstrings( 'The bare MobileNetV1 model outputting raw hidden-states without any specific head on top.' , _a , ) class SCREAMING_SNAKE_CASE__ ( _a ): def __init__( self : Optional[Any] , lowerCAmelCase : MobileNetVaConfig , lowerCAmelCase : bool = True ): super().__init__(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase = config lowerCAmelCase = 32 lowerCAmelCase = max(int(depth * config.depth_multiplier ) , config.min_depth ) lowerCAmelCase = MobileNetVaConvLayer( SCREAMING_SNAKE_CASE_ , in_channels=config.num_channels , out_channels=SCREAMING_SNAKE_CASE_ , kernel_size=3 , stride=2 , ) lowerCAmelCase = [1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1] lowerCAmelCase = nn.ModuleList() for i in range(13 ): lowerCAmelCase = out_channels if strides[i] == 2 or i == 0: depth *= 2 lowerCAmelCase = max(int(depth * config.depth_multiplier ) , config.min_depth ) self.layer.append( MobileNetVaConvLayer( SCREAMING_SNAKE_CASE_ , in_channels=SCREAMING_SNAKE_CASE_ , out_channels=SCREAMING_SNAKE_CASE_ , kernel_size=3 , stride=strides[i] , groups=SCREAMING_SNAKE_CASE_ , ) ) self.layer.append( MobileNetVaConvLayer( SCREAMING_SNAKE_CASE_ , in_channels=SCREAMING_SNAKE_CASE_ , out_channels=SCREAMING_SNAKE_CASE_ , kernel_size=1 , ) ) lowerCAmelCase = nn.AdaptiveAvgPoolad((1, 1) ) if add_pooling_layer else None # Initialize weights and apply final processing self.post_init() def __lowercase ( self : List[Any] , lowerCAmelCase : Dict ): raise NotImplementedError @add_start_docstrings_to_model_forward(SCREAMING_SNAKE_CASE_ ) @add_code_sample_docstrings( checkpoint=_CHECKPOINT_FOR_DOC , output_type=SCREAMING_SNAKE_CASE_ , config_class=_CONFIG_FOR_DOC , modality="""vision""" , expected_output=_EXPECTED_OUTPUT_SHAPE , ) def __lowercase ( self : List[Any] , lowerCAmelCase : Optional[torch.Tensor] = None , lowerCAmelCase : Optional[bool] = None , lowerCAmelCase : Optional[bool] = None , ): lowerCAmelCase = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) lowerCAmelCase = return_dict if return_dict is not None else self.config.use_return_dict if pixel_values is None: raise ValueError("""You have to specify pixel_values""" ) lowerCAmelCase = self.conv_stem(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase = () if output_hidden_states else None for i, layer_module in enumerate(self.layer ): lowerCAmelCase = layer_module(SCREAMING_SNAKE_CASE_ ) if output_hidden_states: lowerCAmelCase = all_hidden_states + (hidden_states,) lowerCAmelCase = hidden_states if self.pooler is not None: lowerCAmelCase = torch.flatten(self.pooler(SCREAMING_SNAKE_CASE_ ) , start_dim=1 ) else: lowerCAmelCase = None if not return_dict: return tuple(v for v in [last_hidden_state, pooled_output, all_hidden_states] if v is not None ) return BaseModelOutputWithPoolingAndNoAttention( last_hidden_state=SCREAMING_SNAKE_CASE_ , pooler_output=SCREAMING_SNAKE_CASE_ , hidden_states=SCREAMING_SNAKE_CASE_ , ) @add_start_docstrings( '\n MobileNetV1 model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n ImageNet.\n ' , _a , ) class SCREAMING_SNAKE_CASE__ ( _a ): def __init__( self : int , lowerCAmelCase : MobileNetVaConfig ): super().__init__(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase = config.num_labels lowerCAmelCase = MobileNetVaModel(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase = self.mobilenet_va.layer[-1].convolution.out_channels # Classifier head lowerCAmelCase = nn.Dropout(config.classifier_dropout_prob , inplace=SCREAMING_SNAKE_CASE_ ) lowerCAmelCase = nn.Linear(SCREAMING_SNAKE_CASE_ , config.num_labels ) if config.num_labels > 0 else nn.Identity() # Initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(SCREAMING_SNAKE_CASE_ ) @add_code_sample_docstrings( checkpoint=_IMAGE_CLASS_CHECKPOINT , output_type=SCREAMING_SNAKE_CASE_ , config_class=_CONFIG_FOR_DOC , expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT , ) def __lowercase ( self : Tuple , lowerCAmelCase : Optional[torch.Tensor] = None , lowerCAmelCase : Optional[bool] = None , lowerCAmelCase : Optional[torch.Tensor] = None , lowerCAmelCase : Optional[bool] = None , ): lowerCAmelCase = return_dict if return_dict is not None else self.config.use_return_dict lowerCAmelCase = self.mobilenet_va(SCREAMING_SNAKE_CASE_ , output_hidden_states=SCREAMING_SNAKE_CASE_ , return_dict=SCREAMING_SNAKE_CASE_ ) lowerCAmelCase = outputs.pooler_output if return_dict else outputs[1] lowerCAmelCase = self.classifier(self.dropout(SCREAMING_SNAKE_CASE_ ) ) lowerCAmelCase = None if labels is not None: if self.config.problem_type is None: if self.num_labels == 1: lowerCAmelCase = """regression""" elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int): lowerCAmelCase = """single_label_classification""" else: lowerCAmelCase = """multi_label_classification""" if self.config.problem_type == "regression": lowerCAmelCase = MSELoss() if self.num_labels == 1: lowerCAmelCase = loss_fct(logits.squeeze() , labels.squeeze() ) else: lowerCAmelCase = loss_fct(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) elif self.config.problem_type == "single_label_classification": lowerCAmelCase = CrossEntropyLoss() lowerCAmelCase = loss_fct(logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) elif self.config.problem_type == "multi_label_classification": lowerCAmelCase = BCEWithLogitsLoss() lowerCAmelCase = loss_fct(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if not return_dict: lowerCAmelCase = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return ImageClassifierOutputWithNoAttention( loss=SCREAMING_SNAKE_CASE_ , logits=SCREAMING_SNAKE_CASE_ , hidden_states=outputs.hidden_states , )
155
def a ( snake_case__: list , snake_case__: list , snake_case__: int , snake_case__: int , snake_case__: int ): '''simple docstring''' if index == number_of_items: return 0 lowercase_ = 0 lowercase_ = 0 lowercase_ = knapsack(snake_case__ , snake_case__ , snake_case__ , snake_case__ , index + 1 ) if weights[index] <= max_weight: lowercase_ = values[index] + knapsack( snake_case__ , snake_case__ , snake_case__ , max_weight - weights[index] , index + 1 ) return max(snake_case__ , snake_case__ ) if __name__ == "__main__": import doctest doctest.testmod()
30
0
import logging from transformers.configuration_utils import PretrainedConfig _lowerCamelCase : Any = logging.getLogger(__name__) class __UpperCAmelCase ( lowerCamelCase__ ): UpperCamelCase = 'masked_bert' def __init__( self : Optional[int], __A : List[Any]=3_0_5_2_2, __A : List[str]=7_6_8, __A : Optional[int]=1_2, __A : Any=1_2, __A : str=3_0_7_2, __A : Union[str, Any]="gelu", __A : Optional[int]=0.1, __A : List[str]=0.1, __A : Tuple=5_1_2, __A : str=2, __A : Dict=0.0_2, __A : Any=1E-12, __A : Any=0, __A : Optional[int]="topK", __A : Dict="constant", __A : Optional[Any]=0.0, **__A : Optional[int], ): super().__init__(pad_token_id=SCREAMING_SNAKE_CASE_, **SCREAMING_SNAKE_CASE_ ) UpperCAmelCase : Any = vocab_size UpperCAmelCase : int = hidden_size UpperCAmelCase : Tuple = num_hidden_layers UpperCAmelCase : str = num_attention_heads UpperCAmelCase : Optional[int] = hidden_act UpperCAmelCase : Union[str, Any] = intermediate_size UpperCAmelCase : Union[str, Any] = hidden_dropout_prob UpperCAmelCase : Optional[int] = attention_probs_dropout_prob UpperCAmelCase : Union[str, Any] = max_position_embeddings UpperCAmelCase : Optional[Any] = type_vocab_size UpperCAmelCase : List[Any] = initializer_range UpperCAmelCase : List[str] = layer_norm_eps UpperCAmelCase : Tuple = pruning_method UpperCAmelCase : List[str] = mask_init UpperCAmelCase : Union[str, Any] = mask_scale
336
import argparse from collections import defaultdict import yaml __a = 'docs/source/en/_toctree.yml' def a ( snake_case__: Dict ): '''simple docstring''' lowercase_ = defaultdict(snake_case__ ) for doc in model_doc: counts[doc["local"]] += 1 lowercase_ = [key for key, value in counts.items() if value > 1] lowercase_ = [] for duplicate_key in duplicates: lowercase_ = list({doc['''title'''] for doc in model_doc if doc['''local'''] == duplicate_key} ) if len(snake_case__ ) > 1: raise ValueError( F'''{duplicate_key} is present several times in the documentation table of content at ''' '''`docs/source/en/_toctree.yml` with different *Title* values. Choose one of those and remove the ''' '''others.''' ) # Only add this once new_doc.append({'''local''': duplicate_key, '''title''': titles[0]} ) # Add none duplicate-keys new_doc.extend([doc for doc in model_doc if counts[doc['''local''']] == 1] ) # Sort return sorted(snake_case__ , key=lambda snake_case__ : s["title"].lower() ) def a ( snake_case__: List[Any]=False ): '''simple docstring''' with open(snake_case__ , encoding='''utf-8''' ) as f: lowercase_ = yaml.safe_load(f.read() ) # Get to the API doc lowercase_ = 0 while content[api_idx]["title"] != "API": api_idx += 1 lowercase_ = content[api_idx]['''sections'''] # Then to the model doc lowercase_ = 0 while api_doc[model_idx]["title"] != "Models": model_idx += 1 lowercase_ = api_doc[model_idx]['''sections'''] lowercase_ = [(idx, section) for idx, section in enumerate(snake_case__ ) if '''sections''' in section] lowercase_ = False for idx, modality_doc in modalities_docs: lowercase_ = modality_doc['''sections'''] lowercase_ = clean_model_doc_toc(snake_case__ ) if old_modality_doc != new_modality_doc: lowercase_ = True if overwrite: lowercase_ = new_modality_doc if diff: if overwrite: lowercase_ = model_doc lowercase_ = api_doc with open(snake_case__ , '''w''' , encoding='''utf-8''' ) as f: f.write(yaml.dump(snake_case__ , allow_unicode=snake_case__ ) ) else: raise ValueError( '''The model doc part of the table of content is not properly sorted, run `make style` to fix this.''' ) if __name__ == "__main__": __a = argparse.ArgumentParser() parser.add_argument('--fix_and_overwrite', action='store_true', help='Whether to fix inconsistencies.') __a = parser.parse_args() check_model_doc(args.fix_and_overwrite)
30
0
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) __lowercase = {'''configuration_deit''': ['''DEIT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''DeiTConfig''', '''DeiTOnnxConfig''']} try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowercase = ['''DeiTFeatureExtractor'''] __lowercase = ['''DeiTImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowercase = [ '''DEIT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''DeiTForImageClassification''', '''DeiTForImageClassificationWithTeacher''', '''DeiTForMaskedImageModeling''', '''DeiTModel''', '''DeiTPreTrainedModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowercase = [ '''TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFDeiTForImageClassification''', '''TFDeiTForImageClassificationWithTeacher''', '''TFDeiTForMaskedImageModeling''', '''TFDeiTModel''', '''TFDeiTPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_deit import DEIT_PRETRAINED_CONFIG_ARCHIVE_MAP, DeiTConfig, DeiTOnnxConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_deit import DeiTFeatureExtractor from .image_processing_deit import DeiTImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_deit import ( DEIT_PRETRAINED_MODEL_ARCHIVE_LIST, DeiTForImageClassification, DeiTForImageClassificationWithTeacher, DeiTForMaskedImageModeling, DeiTModel, DeiTPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_deit import ( TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST, TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher, TFDeiTForMaskedImageModeling, TFDeiTModel, TFDeiTPreTrainedModel, ) else: import sys __lowercase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
43
import copy from ...configuration_utils import PretrainedConfig from ...utils import logging from ..auto.configuration_auto import CONFIG_MAPPING __a = logging.get_logger(__name__) class lowercase__( UpperCAmelCase ): """simple docstring""" a :Union[str, Any] = 'upernet' def __init__( self : Dict , SCREAMING_SNAKE_CASE_ : Dict=None , SCREAMING_SNAKE_CASE_ : str=5_1_2 , SCREAMING_SNAKE_CASE_ : Tuple=0.02 , SCREAMING_SNAKE_CASE_ : Optional[Any]=[1, 2, 3, 6] , SCREAMING_SNAKE_CASE_ : Optional[int]=True , SCREAMING_SNAKE_CASE_ : Tuple=0.4 , SCREAMING_SNAKE_CASE_ : Optional[int]=3_8_4 , SCREAMING_SNAKE_CASE_ : str=2_5_6 , SCREAMING_SNAKE_CASE_ : Dict=1 , SCREAMING_SNAKE_CASE_ : Tuple=False , SCREAMING_SNAKE_CASE_ : str=2_5_5 , **SCREAMING_SNAKE_CASE_ : str , ) -> int: super().__init__(**SCREAMING_SNAKE_CASE_ ) if backbone_config is None: logger.info('''`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.''' ) lowercase_ = CONFIG_MAPPING['''resnet'''](out_features=['''stage1''', '''stage2''', '''stage3''', '''stage4'''] ) elif isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): lowercase_ = backbone_config.get('''model_type''' ) lowercase_ = CONFIG_MAPPING[backbone_model_type] lowercase_ = config_class.from_dict(SCREAMING_SNAKE_CASE_ ) lowercase_ = backbone_config lowercase_ = hidden_size lowercase_ = initializer_range lowercase_ = pool_scales lowercase_ = use_auxiliary_head lowercase_ = auxiliary_loss_weight lowercase_ = auxiliary_in_channels lowercase_ = auxiliary_channels lowercase_ = auxiliary_num_convs lowercase_ = auxiliary_concat_input lowercase_ = loss_ignore_index def _lowercase ( self : List[str] ) -> List[str]: lowercase_ = copy.deepcopy(self.__dict__ ) lowercase_ = self.backbone_config.to_dict() lowercase_ = self.__class__.model_type return output
30
0
from __future__ import annotations from collections.abc import Iterator from typing import Any class __lowerCamelCase : def __init__( self: Union[str, Any],A_: Any ): '''simple docstring''' __UpperCamelCase = data __UpperCamelCase = None class __lowerCamelCase : def __init__( self: Any ): '''simple docstring''' __UpperCamelCase = None __UpperCamelCase = None def __iter__( self: int ): '''simple docstring''' __UpperCamelCase = self.head while self.head: yield node.data __UpperCamelCase = node.next if node == self.head: break def __len__( self: str ): '''simple docstring''' return sum(1 for _ in self ) def __repr__( self: Optional[Any] ): '''simple docstring''' return "->".join(str(SCREAMING_SNAKE_CASE_ ) for item in iter(self ) ) def snake_case_ ( self: Optional[Any],A_: Any ): '''simple docstring''' self.insert_nth(len(self ),SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self: str,A_: Any ): '''simple docstring''' self.insert_nth(0,SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self: List[str],A_: int,A_: Any ): '''simple docstring''' if index < 0 or index > len(self ): raise IndexError('list index out of range.' ) __UpperCamelCase = Node(SCREAMING_SNAKE_CASE_ ) if self.head is None: __UpperCamelCase = new_node # first node points itself __UpperCamelCase = __UpperCamelCase = new_node elif index == 0: # insert at head __UpperCamelCase = self.head __UpperCamelCase = __UpperCamelCase = new_node else: __UpperCamelCase = self.head for _ in range(index - 1 ): __UpperCamelCase = temp.next __UpperCamelCase = temp.next __UpperCamelCase = new_node if index == len(self ) - 1: # insert at tail __UpperCamelCase = new_node def snake_case_ ( self: int ): '''simple docstring''' return self.delete_nth(0 ) def snake_case_ ( self: str ): '''simple docstring''' return self.delete_nth(len(self ) - 1 ) def snake_case_ ( self: Tuple,A_: int = 0 ): '''simple docstring''' if not 0 <= index < len(self ): raise IndexError('list index out of range.' ) __UpperCamelCase = self.head if self.head == self.tail: # just one node __UpperCamelCase = __UpperCamelCase = None elif index == 0: # delete head node __UpperCamelCase = self.tail.next.next __UpperCamelCase = self.head.next else: __UpperCamelCase = self.head for _ in range(index - 1 ): __UpperCamelCase = temp.next __UpperCamelCase = temp.next __UpperCamelCase = temp.next.next if index == len(self ) - 1: # delete at tail __UpperCamelCase = temp return delete_node.data def snake_case_ ( self: Union[str, Any] ): '''simple docstring''' return len(self ) == 0 def _A ( ) -> Union[str, Any]: """simple docstring""" __UpperCamelCase = CircularLinkedList() assert len(snake_case__ ) == 0 assert circular_linked_list.is_empty() is True assert str(snake_case__ ) == "" try: circular_linked_list.delete_front() raise AssertionError # This should not happen except IndexError: assert True # This should happen try: circular_linked_list.delete_tail() raise AssertionError # This should not happen except IndexError: assert True # This should happen try: circular_linked_list.delete_nth(-1 ) raise AssertionError except IndexError: assert True try: circular_linked_list.delete_nth(0 ) raise AssertionError except IndexError: assert True assert circular_linked_list.is_empty() is True for i in range(5 ): assert len(snake_case__ ) == i circular_linked_list.insert_nth(snake_case__ , i + 1 ) assert str(snake_case__ ) == "->".join(str(snake_case__ ) for i in range(1 , 6 ) ) circular_linked_list.insert_tail(6 ) assert str(snake_case__ ) == "->".join(str(snake_case__ ) for i in range(1 , 7 ) ) circular_linked_list.insert_head(0 ) assert str(snake_case__ ) == "->".join(str(snake_case__ ) for i in range(0 , 7 ) ) assert circular_linked_list.delete_front() == 0 assert circular_linked_list.delete_tail() == 6 assert str(snake_case__ ) == "->".join(str(snake_case__ ) for i in range(1 , 6 ) ) assert circular_linked_list.delete_nth(2 ) == 3 circular_linked_list.insert_nth(2 , 3 ) assert str(snake_case__ ) == "->".join(str(snake_case__ ) for i in range(1 , 6 ) ) assert circular_linked_list.is_empty() is False if __name__ == "__main__": import doctest doctest.testmod()
310
import tempfile import unittest from make_student import create_student_by_copying_alternating_layers from transformers import AutoConfig from transformers.file_utils import cached_property from transformers.testing_utils import require_torch __a = 'sshleifer/bart-tiny-random' __a = 'patrickvonplaten/t5-tiny-random' @require_torch class lowercase__( unittest.TestCase ): """simple docstring""" @cached_property def _lowercase ( self : Any ) -> Tuple: return AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE_ ) def _lowercase ( self : int ) -> List[Any]: lowercase_ , *lowercase_ = create_student_by_copying_alternating_layers(SCREAMING_SNAKE_CASE_ , tempfile.mkdtemp() , e=1 , d=1 ) self.assertEqual(student.config.num_hidden_layers , 1 ) def _lowercase ( self : Union[str, Any] ) -> Optional[int]: lowercase_ , *lowercase_ = create_student_by_copying_alternating_layers(SCREAMING_SNAKE_CASE_ , tempfile.mkdtemp() , e=1 , d=SCREAMING_SNAKE_CASE_ ) def _lowercase ( self : List[str] ) -> List[Any]: lowercase_ , *lowercase_ = create_student_by_copying_alternating_layers(SCREAMING_SNAKE_CASE_ , tempfile.mkdtemp() , e=1 , d=SCREAMING_SNAKE_CASE_ ) self.assertEqual(student.config.encoder_layers , 1 ) self.assertEqual(student.config.decoder_layers , self.teacher_config.encoder_layers ) def _lowercase ( self : List[Any] ) -> List[Any]: lowercase_ , *lowercase_ = create_student_by_copying_alternating_layers(SCREAMING_SNAKE_CASE_ , tempfile.mkdtemp() , e=1 , d=1 ) self.assertEqual(student.config.encoder_layers , 1 ) self.assertEqual(student.config.decoder_layers , 1 ) def _lowercase ( self : Union[str, Any] ) -> Optional[int]: with self.assertRaises(SCREAMING_SNAKE_CASE_ ): create_student_by_copying_alternating_layers(SCREAMING_SNAKE_CASE_ , tempfile.mkdtemp() , e=SCREAMING_SNAKE_CASE_ , d=SCREAMING_SNAKE_CASE_ )
30
0
"""simple docstring""" import collections import os import re from pathlib import Path lowerCamelCase__ = """src/transformers""" # Matches is_xxx_available() lowerCamelCase__ = re.compile(R"""is\_([a-z_]*)_available()""") # Catches a one-line _import_struct = {xxx} lowerCamelCase__ = re.compile(R"""^_import_structure\s+=\s+\{([^\}]+)\}""") # Catches a line with a key-values pattern: "bla": ["foo", "bar"] lowerCamelCase__ = re.compile(R"""\s+\"\S*\":\s+\[([^\]]*)\]""") # Catches a line if not is_foo_available lowerCamelCase__ = re.compile(R"""^\s*if\s+not\s+is\_[a-z_]*\_available\(\)""") # Catches a line _import_struct["bla"].append("foo") lowerCamelCase__ = re.compile(R"""^\s*_import_structure\[\"\S*\"\]\.append\(\"(\S*)\"\)""") # Catches a line _import_struct["bla"].extend(["foo", "bar"]) or _import_struct["bla"] = ["foo", "bar"] lowerCamelCase__ = re.compile(R"""^\s*_import_structure\[\S*\](?:\.extend\(|\s*=\s+)\[([^\]]*)\]""") # Catches a line with an object between quotes and a comma: "MyModel", lowerCamelCase__ = re.compile(R"""^\s+\"([^\"]+)\",""") # Catches a line with objects between brackets only: ["foo", "bar"], lowerCamelCase__ = re.compile(R"""^\s+\[([^\]]+)\]""") # Catches a line with from foo import bar, bla, boo lowerCamelCase__ = re.compile(R"""\s+from\s+\S*\s+import\s+([^\(\s].*)\n""") # Catches a line with try: lowerCamelCase__ = re.compile(R"""^\s*try:""") # Catches a line with else: lowerCamelCase__ = re.compile(R"""^\s*else:""") def __lowerCAmelCase (_UpperCamelCase ): if _re_test_backend.search(snake_case__ ) is None: return None __lowerCAmelCase : Union[str, Any] = [b[0] for b in _re_backend.findall(snake_case__ )] backends.sort() return "_and_".join(snake_case__ ) def __lowerCAmelCase (_UpperCamelCase ): with open(snake_case__ , 'r' , encoding='utf-8' , newline='\n' ) as f: __lowerCAmelCase : str = f.readlines() __lowerCAmelCase : Union[str, Any] = 0 while line_index < len(snake_case__ ) and not lines[line_index].startswith('_import_structure = {' ): line_index += 1 # If this is a traditional init, just return. if line_index >= len(snake_case__ ): return None # First grab the objects without a specific backend in _import_structure __lowerCAmelCase : Union[str, Any] = [] while not lines[line_index].startswith('if TYPE_CHECKING' ) and find_backend(lines[line_index] ) is None: __lowerCAmelCase : Dict = lines[line_index] # If we have everything on a single line, let's deal with it. if _re_one_line_import_struct.search(snake_case__ ): __lowerCAmelCase : str = _re_one_line_import_struct.search(snake_case__ ).groups()[0] __lowerCAmelCase : Union[str, Any] = re.findall(r'\[([^\]]+)\]' , snake_case__ ) for imp in imports: objects.extend([obj[1:-1] for obj in imp.split(', ' )] ) line_index += 1 continue __lowerCAmelCase : Tuple = _re_import_struct_key_value.search(snake_case__ ) if single_line_import_search is not None: __lowerCAmelCase : int = [obj[1:-1] for obj in single_line_import_search.groups()[0].split(', ' ) if len(snake_case__ ) > 0] objects.extend(snake_case__ ) elif line.startswith(' ' * 8 + '"' ): objects.append(line[9:-3] ) line_index += 1 __lowerCAmelCase : Tuple = {'none': objects} # Let's continue with backend-specific objects in _import_structure while not lines[line_index].startswith('if TYPE_CHECKING' ): # If the line is an if not is_backend_available, we grab all objects associated. __lowerCAmelCase : Tuple = find_backend(lines[line_index] ) # Check if the backend declaration is inside a try block: if _re_try.search(lines[line_index - 1] ) is None: __lowerCAmelCase : List[Any] = None if backend is not None: line_index += 1 # Scroll until we hit the else block of try-except-else while _re_else.search(lines[line_index] ) is None: line_index += 1 line_index += 1 __lowerCAmelCase : Any = [] # Until we unindent, add backend objects to the list while len(lines[line_index] ) <= 1 or lines[line_index].startswith(' ' * 4 ): __lowerCAmelCase : Optional[int] = lines[line_index] if _re_import_struct_add_one.search(snake_case__ ) is not None: objects.append(_re_import_struct_add_one.search(snake_case__ ).groups()[0] ) elif _re_import_struct_add_many.search(snake_case__ ) is not None: __lowerCAmelCase : int = _re_import_struct_add_many.search(snake_case__ ).groups()[0].split(', ' ) __lowerCAmelCase : Optional[int] = [obj[1:-1] for obj in imports if len(snake_case__ ) > 0] objects.extend(snake_case__ ) elif _re_between_brackets.search(snake_case__ ) is not None: __lowerCAmelCase : Dict = _re_between_brackets.search(snake_case__ ).groups()[0].split(', ' ) __lowerCAmelCase : Union[str, Any] = [obj[1:-1] for obj in imports if len(snake_case__ ) > 0] objects.extend(snake_case__ ) elif _re_quote_object.search(snake_case__ ) is not None: objects.append(_re_quote_object.search(snake_case__ ).groups()[0] ) elif line.startswith(' ' * 8 + '"' ): objects.append(line[9:-3] ) elif line.startswith(' ' * 12 + '"' ): objects.append(line[13:-3] ) line_index += 1 __lowerCAmelCase : Dict = objects else: line_index += 1 # At this stage we are in the TYPE_CHECKING part, first grab the objects without a specific backend __lowerCAmelCase : Optional[Any] = [] while ( line_index < len(snake_case__ ) and find_backend(lines[line_index] ) is None and not lines[line_index].startswith('else' ) ): __lowerCAmelCase : Tuple = lines[line_index] __lowerCAmelCase : Tuple = _re_import.search(snake_case__ ) if single_line_import_search is not None: objects.extend(single_line_import_search.groups()[0].split(', ' ) ) elif line.startswith(' ' * 8 ): objects.append(line[8:-2] ) line_index += 1 __lowerCAmelCase : Union[str, Any] = {'none': objects} # Let's continue with backend-specific objects while line_index < len(snake_case__ ): # If the line is an if is_backend_available, we grab all objects associated. __lowerCAmelCase : Optional[Any] = find_backend(lines[line_index] ) # Check if the backend declaration is inside a try block: if _re_try.search(lines[line_index - 1] ) is None: __lowerCAmelCase : Optional[int] = None if backend is not None: line_index += 1 # Scroll until we hit the else block of try-except-else while _re_else.search(lines[line_index] ) is None: line_index += 1 line_index += 1 __lowerCAmelCase : Optional[int] = [] # Until we unindent, add backend objects to the list while len(lines[line_index] ) <= 1 or lines[line_index].startswith(' ' * 8 ): __lowerCAmelCase : Tuple = lines[line_index] __lowerCAmelCase : Dict = _re_import.search(snake_case__ ) if single_line_import_search is not None: objects.extend(single_line_import_search.groups()[0].split(', ' ) ) elif line.startswith(' ' * 12 ): objects.append(line[12:-2] ) line_index += 1 __lowerCAmelCase : Any = objects else: line_index += 1 return import_dict_objects, type_hint_objects def __lowerCAmelCase (_UpperCamelCase , _UpperCamelCase ): def find_duplicates(_UpperCamelCase ): return [k for k, v in collections.Counter(snake_case__ ).items() if v > 1] if list(import_dict_objects.keys() ) != list(type_hint_objects.keys() ): return ["Both sides of the init do not have the same backends!"] __lowerCAmelCase : List[Any] = [] for key in import_dict_objects.keys(): __lowerCAmelCase : Union[str, Any] = find_duplicates(import_dict_objects[key] ) if duplicate_imports: errors.append(F"Duplicate _import_structure definitions for: {duplicate_imports}" ) __lowerCAmelCase : List[str] = find_duplicates(type_hint_objects[key] ) if duplicate_type_hints: errors.append(F"Duplicate TYPE_CHECKING objects for: {duplicate_type_hints}" ) if sorted(set(import_dict_objects[key] ) ) != sorted(set(type_hint_objects[key] ) ): __lowerCAmelCase : Dict = 'base imports' if key == 'none' else F"{key} backend" errors.append(F"Differences for {name}:" ) for a in type_hint_objects[key]: if a not in import_dict_objects[key]: errors.append(F" {a} in TYPE_HINT but not in _import_structure." ) for a in import_dict_objects[key]: if a not in type_hint_objects[key]: errors.append(F" {a} in _import_structure but not in TYPE_HINT." ) return errors def __lowerCAmelCase (): __lowerCAmelCase : Optional[int] = [] for root, _, files in os.walk(snake_case__ ): if "__init__.py" in files: __lowerCAmelCase : Optional[Any] = os.path.join(snake_case__ , '__init__.py' ) __lowerCAmelCase : str = parse_init(snake_case__ ) if objects is not None: __lowerCAmelCase : List[Any] = analyze_results(*snake_case__ ) if len(snake_case__ ) > 0: __lowerCAmelCase : str = F"Problem in {fname}, both halves do not define the same objects.\n{errors[0]}" failures.append('\n'.join(snake_case__ ) ) if len(snake_case__ ) > 0: raise ValueError('\n\n'.join(snake_case__ ) ) def __lowerCAmelCase (): __lowerCAmelCase : Optional[Any] = [] for path, directories, files in os.walk(snake_case__ ): for folder in directories: # Ignore private modules if folder.startswith('_' ): directories.remove(snake_case__ ) continue # Ignore leftovers from branches (empty folders apart from pycache) if len(list((Path(snake_case__ ) / folder).glob('*.py' ) ) ) == 0: continue __lowerCAmelCase : Optional[Any] = str((Path(snake_case__ ) / folder).relative_to(snake_case__ ) ) __lowerCAmelCase : Tuple = short_path.replace(os.path.sep , '.' ) submodules.append(snake_case__ ) for fname in files: if fname == "__init__.py": continue __lowerCAmelCase : Optional[int] = str((Path(snake_case__ ) / fname).relative_to(snake_case__ ) ) __lowerCAmelCase : Tuple = short_path.replace('.py' , '' ).replace(os.path.sep , '.' ) if len(submodule.split('.' ) ) == 1: submodules.append(snake_case__ ) return submodules lowerCamelCase__ = [ """convert_pytorch_checkpoint_to_tf2""", """modeling_flax_pytorch_utils""", """models.esm.openfold_utils""", ] def __lowerCAmelCase (): from transformers.utils import direct_transformers_import __lowerCAmelCase : Optional[Any] = direct_transformers_import(snake_case__ ) __lowerCAmelCase : int = set(transformers._import_structure.keys() ) # This contains all the base keys of the _import_structure object defined in the init, but if the user is missing # some optional dependencies, they may not have all of them. Thus we read the init to read all additions and # (potentiall re-) add them. with open(os.path.join(snake_case__ , '__init__.py' ) , 'r' ) as f: __lowerCAmelCase : List[Any] = f.read() import_structure_keys.update(set(re.findall(r'import_structure\[\"([^\"]*)\"\]' , snake_case__ ) ) ) __lowerCAmelCase : int = [ module for module in get_transformers_submodules() if module not in IGNORE_SUBMODULES and module not in import_structure_keys ] if len(snake_case__ ) > 0: __lowerCAmelCase : Any = '\n'.join(F"- {module}" for module in module_not_registered ) raise ValueError( 'The following submodules are not properly registed in the main init of Transformers:\n' F"{list_of_modules}\n" 'Make sure they appear somewhere in the keys of `_import_structure` with an empty list as value.' ) if __name__ == "__main__": check_all_inits() check_submodules()
86
def a ( snake_case__: int = 100 ): '''simple docstring''' lowercase_ = (n * (n + 1) // 2) ** 2 lowercase_ = n * (n + 1) * (2 * n + 1) // 6 return sum_cubes - sum_squares if __name__ == "__main__": print(f"{solution() = }")
30
0
import json import sys import tempfile import unittest from pathlib import Path import transformers from transformers import ( CONFIG_MAPPING, FEATURE_EXTRACTOR_MAPPING, AutoConfig, AutoFeatureExtractor, WavaVecaConfig, WavaVecaFeatureExtractor, ) from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, get_tests_dir sys.path.append(str(Path(__file__).parent.parent.parent.parent / """utils""")) from test_module.custom_configuration import CustomConfig # noqa E402 from test_module.custom_feature_extraction import CustomFeatureExtractor # noqa E402 _UpperCAmelCase : Any =get_tests_dir("""fixtures""") _UpperCAmelCase : List[Any] =get_tests_dir("""fixtures/dummy_feature_extractor_config.json""") _UpperCAmelCase : Optional[int] =get_tests_dir("""fixtures/dummy-config.json""") class snake_case__( unittest.TestCase ): '''simple docstring''' def lowercase_ ( self ) -> Any: lowerCAmelCase_ : List[str] = 0 def lowercase_ ( self ) -> Union[str, Any]: lowerCAmelCase_ : List[Any] = AutoFeatureExtractor.from_pretrained('''facebook/wav2vec2-base-960h''' ) self.assertIsInstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def lowercase_ ( self ) -> Any: lowerCAmelCase_ : Tuple = AutoFeatureExtractor.from_pretrained(SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def lowercase_ ( self ) -> Optional[Any]: with tempfile.TemporaryDirectory() as tmpdirname: lowerCAmelCase_ : List[str] = WavaVecaConfig() # remove feature_extractor_type to make sure config.json alone is enough to load feature processor locally lowerCAmelCase_ : Any = AutoFeatureExtractor.from_pretrained(SCREAMING_SNAKE_CASE_ ).to_dict() config_dict.pop('''feature_extractor_type''' ) lowerCAmelCase_ : int = WavaVecaFeatureExtractor(**SCREAMING_SNAKE_CASE_ ) # save in new folder model_config.save_pretrained(SCREAMING_SNAKE_CASE_ ) config.save_pretrained(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase_ : Optional[Any] = AutoFeatureExtractor.from_pretrained(SCREAMING_SNAKE_CASE_ ) # make sure private variable is not incorrectly saved lowerCAmelCase_ : Union[str, Any] = json.loads(config.to_json_string() ) self.assertTrue('''_processor_class''' not in dict_as_saved ) self.assertIsInstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def lowercase_ ( self ) -> Tuple: lowerCAmelCase_ : Dict = AutoFeatureExtractor.from_pretrained(SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def lowercase_ ( self ) -> List[Any]: with self.assertRaisesRegex( SCREAMING_SNAKE_CASE_ , '''bert-base is not a local folder and is not a valid model identifier''' ): lowerCAmelCase_ : Union[str, Any] = AutoFeatureExtractor.from_pretrained('''bert-base''' ) def lowercase_ ( self ) -> str: with self.assertRaisesRegex( SCREAMING_SNAKE_CASE_ , R'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)''' ): lowerCAmelCase_ : Tuple = AutoFeatureExtractor.from_pretrained(SCREAMING_SNAKE_CASE_ , revision='''aaaaaa''' ) def lowercase_ ( self ) -> Optional[int]: with self.assertRaisesRegex( SCREAMING_SNAKE_CASE_ , '''hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json.''' , ): lowerCAmelCase_ : str = AutoFeatureExtractor.from_pretrained('''hf-internal-testing/config-no-model''' ) def lowercase_ ( self ) -> Tuple: # If remote code is not set, we will time out when asking whether to load the model. with self.assertRaises(SCREAMING_SNAKE_CASE_ ): lowerCAmelCase_ : List[Any] = AutoFeatureExtractor.from_pretrained( '''hf-internal-testing/test_dynamic_feature_extractor''' ) # If remote code is disabled, we can't load this config. with self.assertRaises(SCREAMING_SNAKE_CASE_ ): lowerCAmelCase_ : Any = AutoFeatureExtractor.from_pretrained( '''hf-internal-testing/test_dynamic_feature_extractor''' , trust_remote_code=SCREAMING_SNAKE_CASE_ ) lowerCAmelCase_ : Optional[Any] = AutoFeatureExtractor.from_pretrained( '''hf-internal-testing/test_dynamic_feature_extractor''' , trust_remote_code=SCREAMING_SNAKE_CASE_ ) self.assertEqual(feature_extractor.__class__.__name__ , '''NewFeatureExtractor''' ) # Test feature extractor can be reloaded. with tempfile.TemporaryDirectory() as tmp_dir: feature_extractor.save_pretrained(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase_ : Dict = AutoFeatureExtractor.from_pretrained(SCREAMING_SNAKE_CASE_ , trust_remote_code=SCREAMING_SNAKE_CASE_ ) self.assertEqual(reloaded_feature_extractor.__class__.__name__ , '''NewFeatureExtractor''' ) def lowercase_ ( self ) -> Tuple: try: AutoConfig.register('''custom''' , SCREAMING_SNAKE_CASE_ ) AutoFeatureExtractor.register(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(SCREAMING_SNAKE_CASE_ ): AutoFeatureExtractor.register(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # Now that the config is registered, it can be used as any other config with the auto-API lowerCAmelCase_ : str = CustomFeatureExtractor.from_pretrained(SCREAMING_SNAKE_CASE_ ) with tempfile.TemporaryDirectory() as tmp_dir: feature_extractor.save_pretrained(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase_ : str = AutoFeatureExtractor.from_pretrained(SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content: del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig] def lowercase_ ( self ) -> Dict: class snake_case__( UpperCAmelCase__ ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Optional[int] = True try: AutoConfig.register('''custom''' , SCREAMING_SNAKE_CASE_ ) AutoFeatureExtractor.register(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # If remote code is not set, the default is to use local lowerCAmelCase_ : int = AutoFeatureExtractor.from_pretrained( '''hf-internal-testing/test_dynamic_feature_extractor''' ) self.assertEqual(feature_extractor.__class__.__name__ , '''NewFeatureExtractor''' ) self.assertTrue(feature_extractor.is_local ) # If remote code is disabled, we load the local one. lowerCAmelCase_ : Optional[Any] = AutoFeatureExtractor.from_pretrained( '''hf-internal-testing/test_dynamic_feature_extractor''' , trust_remote_code=SCREAMING_SNAKE_CASE_ ) self.assertEqual(feature_extractor.__class__.__name__ , '''NewFeatureExtractor''' ) self.assertTrue(feature_extractor.is_local ) # If remote is enabled, we load from the Hub lowerCAmelCase_ : List[str] = AutoFeatureExtractor.from_pretrained( '''hf-internal-testing/test_dynamic_feature_extractor''' , trust_remote_code=SCREAMING_SNAKE_CASE_ ) self.assertEqual(feature_extractor.__class__.__name__ , '''NewFeatureExtractor''' ) self.assertTrue(not hasattr(SCREAMING_SNAKE_CASE_ , '''is_local''' ) ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content: del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig]
262
import logging from transformers.configuration_utils import PretrainedConfig __a = logging.getLogger(__name__) class lowercase__( UpperCAmelCase ): """simple docstring""" a :Optional[int] = 'masked_bert' def __init__( self : Optional[int] , SCREAMING_SNAKE_CASE_ : List[Any]=3_0_5_2_2 , SCREAMING_SNAKE_CASE_ : List[str]=7_6_8 , SCREAMING_SNAKE_CASE_ : Optional[int]=1_2 , SCREAMING_SNAKE_CASE_ : Any=1_2 , SCREAMING_SNAKE_CASE_ : str=3_0_7_2 , SCREAMING_SNAKE_CASE_ : Union[str, Any]="gelu" , SCREAMING_SNAKE_CASE_ : Optional[int]=0.1 , SCREAMING_SNAKE_CASE_ : List[str]=0.1 , SCREAMING_SNAKE_CASE_ : Tuple=5_1_2 , SCREAMING_SNAKE_CASE_ : str=2 , SCREAMING_SNAKE_CASE_ : Dict=0.02 , SCREAMING_SNAKE_CASE_ : Any=1e-12 , SCREAMING_SNAKE_CASE_ : Any=0 , SCREAMING_SNAKE_CASE_ : Optional[int]="topK" , SCREAMING_SNAKE_CASE_ : Dict="constant" , SCREAMING_SNAKE_CASE_ : Optional[Any]=0.0 , **SCREAMING_SNAKE_CASE_ : Optional[int] , ) -> Optional[Any]: super().__init__(pad_token_id=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) lowercase_ = vocab_size lowercase_ = hidden_size lowercase_ = num_hidden_layers lowercase_ = num_attention_heads lowercase_ = hidden_act lowercase_ = intermediate_size lowercase_ = hidden_dropout_prob lowercase_ = attention_probs_dropout_prob lowercase_ = max_position_embeddings lowercase_ = type_vocab_size lowercase_ = initializer_range lowercase_ = layer_norm_eps lowercase_ = pruning_method lowercase_ = mask_init lowercase_ = mask_scale
30
0
"""simple docstring""" from math import pow, sqrt def lowercase ( *lowerCAmelCase__ : float ) -> Optional[Any]: __a = len(snake_case__ ) > 0 and all(value > 0.0 for value in values ) return result def lowercase ( lowerCAmelCase__ : float , lowerCAmelCase__ : float ) -> int: return ( round(sqrt(molar_mass_a / molar_mass_a ) , 6 ) if validate(snake_case__ , snake_case__ ) else ValueError('''Input Error: Molar mass values must greater than 0.''' ) ) def lowercase ( lowerCAmelCase__ : float , lowerCAmelCase__ : float , lowerCAmelCase__ : float ) -> Any: return ( round(effusion_rate * sqrt(molar_mass_a / molar_mass_a ) , 6 ) if validate(snake_case__ , snake_case__ , snake_case__ ) else ValueError( '''Input Error: Molar mass and effusion rate values must greater than 0.''' ) ) def lowercase ( lowerCAmelCase__ : float , lowerCAmelCase__ : float , lowerCAmelCase__ : float ) -> int: return ( round(effusion_rate / sqrt(molar_mass_a / molar_mass_a ) , 6 ) if validate(snake_case__ , snake_case__ , snake_case__ ) else ValueError( '''Input Error: Molar mass and effusion rate values must greater than 0.''' ) ) def lowercase ( lowerCAmelCase__ : float , lowerCAmelCase__ : float , lowerCAmelCase__ : float ) -> Optional[Any]: return ( round(molar_mass / pow(effusion_rate_a / effusion_rate_a , 2 ) , 6 ) if validate(snake_case__ , snake_case__ , snake_case__ ) else ValueError( '''Input Error: Molar mass and effusion rate values must greater than 0.''' ) ) def lowercase ( lowerCAmelCase__ : float , lowerCAmelCase__ : float , lowerCAmelCase__ : float ) -> List[str]: return ( round(pow(effusion_rate_a / effusion_rate_a , 2 ) / molar_mass , 6 ) if validate(snake_case__ , snake_case__ , snake_case__ ) else ValueError( '''Input Error: Molar mass and effusion rate values must greater than 0.''' ) )
45
import os def a ( ): '''simple docstring''' lowercase_ = os.path.join(os.path.dirname(snake_case__ ) , '''num.txt''' ) with open(snake_case__ ) as file_hand: return str(sum(int(snake_case__ ) for line in file_hand ) )[:10] if __name__ == "__main__": print(solution())
30
0
from .data_collator import ( DataCollatorForLanguageModeling, DataCollatorForPermutationLanguageModeling, DataCollatorForSeqaSeq, DataCollatorForSOP, DataCollatorForTokenClassification, DataCollatorForWholeWordMask, DataCollatorWithPadding, DefaultDataCollator, default_data_collator, ) from .metrics import glue_compute_metrics, xnli_compute_metrics from .processors import ( DataProcessor, InputExample, InputFeatures, SingleSentenceClassificationProcessor, SquadExample, SquadFeatures, SquadVaProcessor, SquadVaProcessor, glue_convert_examples_to_features, glue_output_modes, glue_processors, glue_tasks_num_labels, squad_convert_examples_to_features, xnli_output_modes, xnli_processors, xnli_tasks_num_labels, )
82
import os import zipfile import pytest from datasets.utils.extract import ( BzipaExtractor, Extractor, GzipExtractor, LzaExtractor, SevenZipExtractor, TarExtractor, XzExtractor, ZipExtractor, ZstdExtractor, ) from .utils import require_lza, require_pyazr, require_zstandard @pytest.mark.parametrize( '''compression_format, is_archive''' , [ ('''7z''', True), ('''bz2''', False), ('''gzip''', False), ('''lz4''', False), ('''tar''', True), ('''xz''', False), ('''zip''', True), ('''zstd''', False), ] , ) def a ( snake_case__: int , snake_case__: Tuple , snake_case__: Dict , snake_case__: Dict , snake_case__: List[Any] , snake_case__: int , snake_case__: List[Any] , snake_case__: Optional[int] , snake_case__: str , snake_case__: Union[str, Any] , snake_case__: List[str] , snake_case__: int , ): '''simple docstring''' lowercase_ = { '''7z''': (seven_zip_file, SevenZipExtractor), '''bz2''': (bza_file, BzipaExtractor), '''gzip''': (gz_file, GzipExtractor), '''lz4''': (lza_file, LzaExtractor), '''tar''': (tar_file, TarExtractor), '''xz''': (xz_file, XzExtractor), '''zip''': (zip_file, ZipExtractor), '''zstd''': (zstd_file, ZstdExtractor), } lowercase_ , lowercase_ = input_paths_and_base_extractors[compression_format] if input_path is None: lowercase_ = F'''for \'{compression_format}\' compression_format, ''' if compression_format == "7z": reason += require_pyazr.kwargs["reason"] elif compression_format == "lz4": reason += require_lza.kwargs["reason"] elif compression_format == "zstd": reason += require_zstandard.kwargs["reason"] pytest.skip(snake_case__ ) assert base_extractor.is_extractable(snake_case__ ) lowercase_ = tmp_path / ('''extracted''' if is_archive else '''extracted.txt''') base_extractor.extract(snake_case__ , snake_case__ ) if is_archive: assert output_path.is_dir() for file_path in output_path.iterdir(): assert file_path.name == text_file.name lowercase_ = file_path.read_text(encoding='''utf-8''' ) else: lowercase_ = output_path.read_text(encoding='''utf-8''' ) lowercase_ = text_file.read_text(encoding='''utf-8''' ) assert extracted_file_content == expected_file_content @pytest.mark.parametrize( '''compression_format, is_archive''' , [ ('''7z''', True), ('''bz2''', False), ('''gzip''', False), ('''lz4''', False), ('''tar''', True), ('''xz''', False), ('''zip''', True), ('''zstd''', False), ] , ) def a ( snake_case__: List[Any] , snake_case__: int , snake_case__: Optional[int] , snake_case__: Union[str, Any] , snake_case__: List[Any] , snake_case__: Tuple , snake_case__: Optional[int] , snake_case__: List[str] , snake_case__: Union[str, Any] , snake_case__: Tuple , snake_case__: int , snake_case__: Optional[int] , ): '''simple docstring''' lowercase_ = { '''7z''': seven_zip_file, '''bz2''': bza_file, '''gzip''': gz_file, '''lz4''': lza_file, '''tar''': tar_file, '''xz''': xz_file, '''zip''': zip_file, '''zstd''': zstd_file, } lowercase_ = input_paths[compression_format] if input_path is None: lowercase_ = F'''for \'{compression_format}\' compression_format, ''' if compression_format == "7z": reason += require_pyazr.kwargs["reason"] elif compression_format == "lz4": reason += require_lza.kwargs["reason"] elif compression_format == "zstd": reason += require_zstandard.kwargs["reason"] pytest.skip(snake_case__ ) lowercase_ = Extractor.infer_extractor_format(snake_case__ ) assert extractor_format is not None lowercase_ = tmp_path / ('''extracted''' if is_archive else '''extracted.txt''') Extractor.extract(snake_case__ , snake_case__ , snake_case__ ) if is_archive: assert output_path.is_dir() for file_path in output_path.iterdir(): assert file_path.name == text_file.name lowercase_ = file_path.read_text(encoding='''utf-8''' ) else: lowercase_ = output_path.read_text(encoding='''utf-8''' ) lowercase_ = text_file.read_text(encoding='''utf-8''' ) assert extracted_file_content == expected_file_content @pytest.fixture def a ( snake_case__: Union[str, Any] , snake_case__: List[Any] ): '''simple docstring''' import tarfile lowercase_ = tmp_path / '''data_dot_dot''' directory.mkdir() lowercase_ = directory / '''tar_file_with_dot_dot.tar''' with tarfile.TarFile(snake_case__ , '''w''' ) as f: f.add(snake_case__ , arcname=os.path.join('''..''' , text_file.name ) ) return path @pytest.fixture def a ( snake_case__: int ): '''simple docstring''' import tarfile lowercase_ = tmp_path / '''data_sym_link''' directory.mkdir() lowercase_ = directory / '''tar_file_with_sym_link.tar''' os.symlink('''..''' , directory / '''subdir''' , target_is_directory=snake_case__ ) with tarfile.TarFile(snake_case__ , '''w''' ) as f: f.add(str(directory / '''subdir''' ) , arcname='''subdir''' ) # str required by os.readlink on Windows and Python < 3.8 return path @pytest.mark.parametrize( '''insecure_tar_file, error_log''' , [('''tar_file_with_dot_dot''', '''illegal path'''), ('''tar_file_with_sym_link''', '''Symlink''')] , ) def a ( snake_case__: List[Any] , snake_case__: Optional[int] , snake_case__: List[str] , snake_case__: List[str] , snake_case__: int , snake_case__: Optional[Any] ): '''simple docstring''' lowercase_ = { '''tar_file_with_dot_dot''': tar_file_with_dot_dot, '''tar_file_with_sym_link''': tar_file_with_sym_link, } lowercase_ = insecure_tar_files[insecure_tar_file] lowercase_ = tmp_path / '''extracted''' TarExtractor.extract(snake_case__ , snake_case__ ) assert caplog.text for record in caplog.records: assert record.levelname == "ERROR" assert error_log in record.msg def a ( snake_case__: Optional[int] ): '''simple docstring''' # We should have less false positives than zipfile.is_zipfile # We do that by checking only the magic number lowercase_ = tmpdir / '''not_a_zip_file''' # From: https://github.com/python/cpython/pull/5053 lowercase_ = ( B'''\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00''' B'''\x00\x02\x08\x06\x00\x00\x00\x99\x81\xb6\'\x00\x00\x00\x15I''' B'''DATx\x01\x01\n\x00\xf5\xff\x00PK\x05\x06\x00PK\x06\x06\x07''' B'''\xac\x01N\xc6|a\r\x00\x00\x00\x00IEND\xaeB`\x82''' ) with not_a_zip_file.open('''wb''' ) as f: f.write(snake_case__ ) assert zipfile.is_zipfile(str(snake_case__ ) ) # is a false positive for `zipfile` assert not ZipExtractor.is_extractable(snake_case__ ) # but we're right
30
0
from ...configuration_utils import PretrainedConfig from ...utils import logging _A = logging.get_logger(__name__) _A = { 'facebook/timesformer': 'https://huggingface.co/facebook/timesformer/resolve/main/config.json', } class UpperCAmelCase__ ( A_ ): """simple docstring""" UpperCAmelCase__ : Any = 'timesformer' def __init__( self , A_=224 , A_=16 , A_=3 , A_=8 , A_=768 , A_=12 , A_=12 , A_=3072 , A_="gelu" , A_=0.0 , A_=0.0 , A_=0.02 , A_=1E-6 , A_=True , A_="divided_space_time" , A_=0 , **A_ , ) -> Optional[Any]: super().__init__(**SCREAMING_SNAKE_CASE_ ) __UpperCamelCase =image_size __UpperCamelCase =patch_size __UpperCamelCase =num_channels __UpperCamelCase =num_frames __UpperCamelCase =hidden_size __UpperCamelCase =num_hidden_layers __UpperCamelCase =num_attention_heads __UpperCamelCase =intermediate_size __UpperCamelCase =hidden_act __UpperCamelCase =hidden_dropout_prob __UpperCamelCase =attention_probs_dropout_prob __UpperCamelCase =initializer_range __UpperCamelCase =layer_norm_eps __UpperCamelCase =qkv_bias __UpperCamelCase =attention_type __UpperCamelCase =drop_path_rate
62
from __future__ import annotations def a ( snake_case__: list[int] , snake_case__: int , snake_case__: int , snake_case__: int ): '''simple docstring''' if (direction == 1 and array[indexa] > array[indexa]) or ( direction == 0 and array[indexa] < array[indexa] ): lowercase_ , lowercase_ = array[indexa], array[indexa] def a ( snake_case__: list[int] , snake_case__: int , snake_case__: int , snake_case__: int ): '''simple docstring''' if length > 1: lowercase_ = int(length / 2 ) for i in range(snake_case__ , low + middle ): comp_and_swap(snake_case__ , snake_case__ , i + middle , snake_case__ ) bitonic_merge(snake_case__ , snake_case__ , snake_case__ , snake_case__ ) bitonic_merge(snake_case__ , low + middle , snake_case__ , snake_case__ ) def a ( snake_case__: list[int] , snake_case__: int , snake_case__: int , snake_case__: int ): '''simple docstring''' if length > 1: lowercase_ = int(length / 2 ) bitonic_sort(snake_case__ , snake_case__ , snake_case__ , 1 ) bitonic_sort(snake_case__ , low + middle , snake_case__ , 0 ) bitonic_merge(snake_case__ , snake_case__ , snake_case__ , snake_case__ ) if __name__ == "__main__": __a = input('Enter numbers separated by a comma:\n').strip() __a = [int(item.strip()) for item in user_input.split(',')] bitonic_sort(unsorted, 0, len(unsorted), 1) print('\nSorted array in ascending order is: ', end='') print(*unsorted, sep=', ') bitonic_merge(unsorted, 0, len(unsorted), 0) print('Sorted array in descending order is: ', end='') print(*unsorted, sep=', ')
30
0
import functools def A_ ( _lowerCAmelCase , _lowerCAmelCase ) -> List[Any]: # Validation if not isinstance(snake_case__ , snake_case__ ) or not all(isinstance(snake_case__ , snake_case__ ) for day in days ): raise ValueError("The parameter days should be a list of integers" ) if len(snake_case__ ) != 3 or not all(isinstance(snake_case__ , snake_case__ ) for cost in costs ): raise ValueError("The parameter costs should be a list of three integers" ) if len(snake_case__ ) == 0: return 0 if min(snake_case__ ) <= 0: raise ValueError("All days elements should be greater than 0" ) if max(snake_case__ ) >= 366: raise ValueError("All days elements should be less than 366" ) UpperCamelCase : Union[str, Any] = set(snake_case__ ) @functools.cache def dynamic_programming(_lowerCAmelCase ) -> int: if index > 365: return 0 if index not in days_set: return dynamic_programming(index + 1 ) return min( costs[0] + dynamic_programming(index + 1 ) , costs[1] + dynamic_programming(index + 7 ) , costs[2] + dynamic_programming(index + 30 ) , ) return dynamic_programming(1 ) if __name__ == "__main__": import doctest doctest.testmod()
52
from __future__ import annotations from collections.abc import MutableSequence class lowercase__: """simple docstring""" def __init__( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : MutableSequence[float] ) -> None: if len(SCREAMING_SNAKE_CASE_ ) != degree + 1: raise ValueError( '''The number of coefficients should be equal to the degree + 1.''' ) lowercase_ = list(SCREAMING_SNAKE_CASE_ ) lowercase_ = degree def __add__( self : Any , SCREAMING_SNAKE_CASE_ : Polynomial ) -> Polynomial: if self.degree > polynomial_a.degree: lowercase_ = self.coefficients[:] for i in range(polynomial_a.degree + 1 ): coefficients[i] += polynomial_a.coefficients[i] return Polynomial(self.degree , SCREAMING_SNAKE_CASE_ ) else: lowercase_ = polynomial_a.coefficients[:] for i in range(self.degree + 1 ): coefficients[i] += self.coefficients[i] return Polynomial(polynomial_a.degree , SCREAMING_SNAKE_CASE_ ) def __sub__( self : str , SCREAMING_SNAKE_CASE_ : Polynomial ) -> Polynomial: return self + polynomial_a * Polynomial(0 , [-1] ) def __neg__( self : int ) -> Polynomial: return Polynomial(self.degree , [-c for c in self.coefficients] ) def __mul__( self : Any , SCREAMING_SNAKE_CASE_ : Polynomial ) -> Polynomial: lowercase_ = [0] * (self.degree + polynomial_a.degree + 1) for i in range(self.degree + 1 ): for j in range(polynomial_a.degree + 1 ): coefficients[i + j] += ( self.coefficients[i] * polynomial_a.coefficients[j] ) return Polynomial(self.degree + polynomial_a.degree , SCREAMING_SNAKE_CASE_ ) def _lowercase ( self : Dict , SCREAMING_SNAKE_CASE_ : int | float ) -> int | float: lowercase_ = 0 for i in range(self.degree + 1 ): result += self.coefficients[i] * (substitution**i) return result def __str__( self : Tuple ) -> str: lowercase_ = '''''' for i in range(self.degree , -1 , -1 ): if self.coefficients[i] == 0: continue elif self.coefficients[i] > 0: if polynomial: polynomial += " + " else: polynomial += " - " if i == 0: polynomial += str(abs(self.coefficients[i] ) ) elif i == 1: polynomial += str(abs(self.coefficients[i] ) ) + "x" else: polynomial += str(abs(self.coefficients[i] ) ) + "x^" + str(SCREAMING_SNAKE_CASE_ ) return polynomial def __repr__( self : Optional[Any] ) -> str: return self.__str__() def _lowercase ( self : int ) -> Polynomial: lowercase_ = [0] * self.degree for i in range(self.degree ): lowercase_ = self.coefficients[i + 1] * (i + 1) return Polynomial(self.degree - 1 , SCREAMING_SNAKE_CASE_ ) def _lowercase ( self : List[str] , SCREAMING_SNAKE_CASE_ : int | float = 0 ) -> Polynomial: lowercase_ = [0] * (self.degree + 2) lowercase_ = constant for i in range(self.degree + 1 ): lowercase_ = self.coefficients[i] / (i + 1) return Polynomial(self.degree + 1 , SCREAMING_SNAKE_CASE_ ) def __eq__( self : str , SCREAMING_SNAKE_CASE_ : object ) -> bool: if not isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): return False if self.degree != polynomial_a.degree: return False for i in range(self.degree + 1 ): if self.coefficients[i] != polynomial_a.coefficients[i]: return False return True def __ne__( self : List[str] , SCREAMING_SNAKE_CASE_ : object ) -> bool: return not self.__eq__(SCREAMING_SNAKE_CASE_ )
30
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, ) __snake_case = { '''configuration_roformer''': ['''ROFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''RoFormerConfig''', '''RoFormerOnnxConfig'''], '''tokenization_roformer''': ['''RoFormerTokenizer'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = ['''RoFormerTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = [ '''ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''RoFormerForCausalLM''', '''RoFormerForMaskedLM''', '''RoFormerForMultipleChoice''', '''RoFormerForQuestionAnswering''', '''RoFormerForSequenceClassification''', '''RoFormerForTokenClassification''', '''RoFormerLayer''', '''RoFormerModel''', '''RoFormerPreTrainedModel''', '''load_tf_weights_in_roformer''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = [ '''TF_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFRoFormerForCausalLM''', '''TFRoFormerForMaskedLM''', '''TFRoFormerForMultipleChoice''', '''TFRoFormerForQuestionAnswering''', '''TFRoFormerForSequenceClassification''', '''TFRoFormerForTokenClassification''', '''TFRoFormerLayer''', '''TFRoFormerModel''', '''TFRoFormerPreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = [ '''FLAX_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''FlaxRoFormerForMaskedLM''', '''FlaxRoFormerForMultipleChoice''', '''FlaxRoFormerForQuestionAnswering''', '''FlaxRoFormerForSequenceClassification''', '''FlaxRoFormerForTokenClassification''', '''FlaxRoFormerModel''', '''FlaxRoFormerPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_roformer import ROFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, RoFormerConfig, RoFormerOnnxConfig from .tokenization_roformer import RoFormerTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_roformer_fast import RoFormerTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_roformer import ( ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, RoFormerForCausalLM, RoFormerForMaskedLM, RoFormerForMultipleChoice, RoFormerForQuestionAnswering, RoFormerForSequenceClassification, RoFormerForTokenClassification, RoFormerLayer, RoFormerModel, RoFormerPreTrainedModel, load_tf_weights_in_roformer, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_roformer import ( TF_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TFRoFormerForCausalLM, TFRoFormerForMaskedLM, TFRoFormerForMultipleChoice, TFRoFormerForQuestionAnswering, TFRoFormerForSequenceClassification, TFRoFormerForTokenClassification, TFRoFormerLayer, TFRoFormerModel, TFRoFormerPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_roformer import ( FLAX_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, FlaxRoFormerForMaskedLM, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerModel, FlaxRoFormerPreTrainedModel, ) else: import sys __snake_case = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
320
import itertools import math def a ( snake_case__: int ): '''simple docstring''' if 1 < number < 4: # 2 and 3 are primes return True elif number < 2 or number % 2 == 0 or number % 3 == 0: # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes return False # All primes number are in format of 6k +/- 1 for i in range(5 , int(math.sqrt(snake_case__ ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True def a ( ): '''simple docstring''' lowercase_ = 2 while True: if is_prime(snake_case__ ): yield num num += 1 def a ( snake_case__: int = 10_001 ): '''simple docstring''' return next(itertools.islice(prime_generator() , nth - 1 , snake_case__ ) ) if __name__ == "__main__": print(f"{solution() = }")
30
0
"""simple docstring""" from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging a = logging.get_logger(__name__) a = { 'facebook/xmod-base': 'https://huggingface.co/facebook/xmod-base/resolve/main/config.json', 'facebook/xmod-large-prenorm': 'https://huggingface.co/facebook/xmod-large-prenorm/resolve/main/config.json', 'facebook/xmod-base-13-125k': 'https://huggingface.co/facebook/xmod-base-13-125k/resolve/main/config.json', 'facebook/xmod-base-30-125k': 'https://huggingface.co/facebook/xmod-base-30-125k/resolve/main/config.json', 'facebook/xmod-base-30-195k': 'https://huggingface.co/facebook/xmod-base-30-195k/resolve/main/config.json', 'facebook/xmod-base-60-125k': 'https://huggingface.co/facebook/xmod-base-60-125k/resolve/main/config.json', 'facebook/xmod-base-60-265k': 'https://huggingface.co/facebook/xmod-base-60-265k/resolve/main/config.json', 'facebook/xmod-base-75-125k': 'https://huggingface.co/facebook/xmod-base-75-125k/resolve/main/config.json', 'facebook/xmod-base-75-269k': 'https://huggingface.co/facebook/xmod-base-75-269k/resolve/main/config.json', } class SCREAMING_SNAKE_CASE__ ( _a ): _a = 'xmod' def __init__( self : Optional[Any] , lowerCAmelCase : Dict=3_0522 , lowerCAmelCase : Dict=768 , lowerCAmelCase : Optional[Any]=12 , lowerCAmelCase : Union[str, Any]=12 , lowerCAmelCase : Optional[int]=3072 , lowerCAmelCase : int="gelu" , lowerCAmelCase : Any=0.1 , lowerCAmelCase : Any=0.1 , lowerCAmelCase : List[Any]=512 , lowerCAmelCase : Any=2 , lowerCAmelCase : Optional[int]=0.02 , lowerCAmelCase : Dict=1e-12 , lowerCAmelCase : Optional[Any]=1 , lowerCAmelCase : List[str]=0 , lowerCAmelCase : List[str]=2 , lowerCAmelCase : List[Any]="absolute" , lowerCAmelCase : List[str]=True , lowerCAmelCase : Dict=None , lowerCAmelCase : Optional[Any]=False , lowerCAmelCase : Tuple=2 , lowerCAmelCase : Any=False , lowerCAmelCase : Optional[Any]=True , lowerCAmelCase : Optional[Any]=True , lowerCAmelCase : Union[str, Any]=("en_XX",) , lowerCAmelCase : List[str]=None , **lowerCAmelCase : List[Any] , ): super().__init__(pad_token_id=SCREAMING_SNAKE_CASE_ , bos_token_id=SCREAMING_SNAKE_CASE_ , eos_token_id=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) lowerCAmelCase = vocab_size lowerCAmelCase = hidden_size lowerCAmelCase = num_hidden_layers lowerCAmelCase = num_attention_heads lowerCAmelCase = hidden_act lowerCAmelCase = intermediate_size lowerCAmelCase = hidden_dropout_prob lowerCAmelCase = attention_probs_dropout_prob lowerCAmelCase = max_position_embeddings lowerCAmelCase = type_vocab_size lowerCAmelCase = initializer_range lowerCAmelCase = layer_norm_eps lowerCAmelCase = position_embedding_type lowerCAmelCase = use_cache lowerCAmelCase = classifier_dropout lowerCAmelCase = pre_norm lowerCAmelCase = adapter_reduction_factor lowerCAmelCase = adapter_layer_norm lowerCAmelCase = adapter_reuse_layer_norm lowerCAmelCase = ln_before_adapter lowerCAmelCase = list(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase = default_language class SCREAMING_SNAKE_CASE__ ( _a ): @property def __lowercase ( self : Union[str, Any] ): if self.task == "multiple-choice": lowerCAmelCase = {0: """batch""", 1: """choice""", 2: """sequence"""} else: lowerCAmelCase = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ] )
155
from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available() and is_transformers_version('>=', '4.25.0')): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import UnCLIPImageVariationPipeline, UnCLIPPipeline else: from .pipeline_unclip import UnCLIPPipeline from .pipeline_unclip_image_variation import UnCLIPImageVariationPipeline from .text_proj import UnCLIPTextProjModel
30
0
import argparse from pathlib import Path import requests import torch from PIL import Image from transformers import ( RobertaTokenizer, TrOCRConfig, TrOCRForCausalLM, TrOCRProcessor, VisionEncoderDecoderModel, ViTConfig, ViTImageProcessor, ViTModel, ) from transformers.utils import logging logging.set_verbosity_info() _lowerCamelCase : List[str] = logging.get_logger(__name__) def a__ ( UpperCAmelCase : Optional[int] , UpperCAmelCase : str ) -> str: UpperCAmelCase : Any = [] for i in range(encoder_config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append( (f'''encoder.deit.blocks.{i}.norm1.weight''', f'''encoder.encoder.layer.{i}.layernorm_before.weight''') ) rename_keys.append((f'''encoder.deit.blocks.{i}.norm1.bias''', f'''encoder.encoder.layer.{i}.layernorm_before.bias''') ) rename_keys.append( (f'''encoder.deit.blocks.{i}.attn.proj.weight''', f'''encoder.encoder.layer.{i}.attention.output.dense.weight''') ) rename_keys.append( (f'''encoder.deit.blocks.{i}.attn.proj.bias''', f'''encoder.encoder.layer.{i}.attention.output.dense.bias''') ) rename_keys.append( (f'''encoder.deit.blocks.{i}.norm2.weight''', f'''encoder.encoder.layer.{i}.layernorm_after.weight''') ) rename_keys.append((f'''encoder.deit.blocks.{i}.norm2.bias''', f'''encoder.encoder.layer.{i}.layernorm_after.bias''') ) rename_keys.append( (f'''encoder.deit.blocks.{i}.mlp.fc1.weight''', f'''encoder.encoder.layer.{i}.intermediate.dense.weight''') ) rename_keys.append( (f'''encoder.deit.blocks.{i}.mlp.fc1.bias''', f'''encoder.encoder.layer.{i}.intermediate.dense.bias''') ) rename_keys.append( (f'''encoder.deit.blocks.{i}.mlp.fc2.weight''', f'''encoder.encoder.layer.{i}.output.dense.weight''') ) rename_keys.append((f'''encoder.deit.blocks.{i}.mlp.fc2.bias''', f'''encoder.encoder.layer.{i}.output.dense.bias''') ) # cls token, position embeddings and patch embeddings of encoder rename_keys.extend( [ ('''encoder.deit.cls_token''', '''encoder.embeddings.cls_token'''), ('''encoder.deit.pos_embed''', '''encoder.embeddings.position_embeddings'''), ('''encoder.deit.patch_embed.proj.weight''', '''encoder.embeddings.patch_embeddings.projection.weight'''), ('''encoder.deit.patch_embed.proj.bias''', '''encoder.embeddings.patch_embeddings.projection.bias'''), ('''encoder.deit.norm.weight''', '''encoder.layernorm.weight'''), ('''encoder.deit.norm.bias''', '''encoder.layernorm.bias'''), ] ) return rename_keys def a__ ( UpperCAmelCase : int , UpperCAmelCase : Dict ) -> Any: for i in range(encoder_config.num_hidden_layers ): # queries, keys and values (only weights, no biases) UpperCAmelCase : int = state_dict.pop(f'''encoder.deit.blocks.{i}.attn.qkv.weight''' ) UpperCAmelCase : Dict = in_proj_weight[ : encoder_config.hidden_size, : ] UpperCAmelCase : Dict = in_proj_weight[ encoder_config.hidden_size : encoder_config.hidden_size * 2, : ] UpperCAmelCase : Dict = in_proj_weight[ -encoder_config.hidden_size :, : ] def a__ ( UpperCAmelCase : Tuple , UpperCAmelCase : Optional[Any] , UpperCAmelCase : List[Any] ) -> int: UpperCAmelCase : List[str] = dct.pop(snake_case__ ) UpperCAmelCase : str = val def a__ ( UpperCAmelCase : Optional[int] ) -> Optional[Any]: if "handwritten" in checkpoint_url: UpperCAmelCase : Dict = '''https://fki.tic.heia-fr.ch/static/img/a01-122-02-00.jpg''' # industry # url = "https://fki.tic.heia-fr.ch/static/img/a01-122-02-12.jpg" # have # url = "https://fki.tic.heia-fr.ch/static/img/a01-122-02-10.jpg" # let # url = "https://fki.tic.heia-fr.ch/static/img/a01-122-02.jpg" # # url = "https://fki.tic.heia-fr.ch/static/img/a01-122.jpg" elif "printed" in checkpoint_url or "stage1" in checkpoint_url: UpperCAmelCase : Any = '''https://www.researchgate.net/profile/Dinh-Sang/publication/338099565/figure/fig8/AS:840413229350922@1577381536857/An-receipt-example-in-the-SROIE-2019-dataset_Q640.jpg''' UpperCAmelCase : Union[str, Any] = Image.open(requests.get(snake_case__ , stream=snake_case__ ).raw ).convert('''RGB''' ) return im @torch.no_grad() def a__ ( UpperCAmelCase : List[Any] , UpperCAmelCase : List[Any] ) -> str: UpperCAmelCase : List[Any] = ViTConfig(image_size=384 , qkv_bias=snake_case__ ) UpperCAmelCase : List[Any] = TrOCRConfig() # size of the architecture if "base" in checkpoint_url: UpperCAmelCase : str = 768 elif "large" in checkpoint_url: # use ViT-large encoder UpperCAmelCase : Dict = 1_024 UpperCAmelCase : Optional[Any] = 4_096 UpperCAmelCase : Any = 24 UpperCAmelCase : Any = 16 UpperCAmelCase : Any = 1_024 else: raise ValueError('''Should either find \'base\' or \'large\' in checkpoint URL''' ) # the large-printed + stage1 checkpoints uses sinusoidal position embeddings, no layernorm afterwards if "large-printed" in checkpoint_url or "stage1" in checkpoint_url: UpperCAmelCase : Optional[int] = False UpperCAmelCase : str = '''relu''' UpperCAmelCase : Optional[Any] = 1_024 UpperCAmelCase : List[str] = True UpperCAmelCase : int = False UpperCAmelCase : List[Any] = False # load HuggingFace model UpperCAmelCase : int = ViTModel(snake_case__ , add_pooling_layer=snake_case__ ) UpperCAmelCase : int = TrOCRForCausalLM(snake_case__ ) UpperCAmelCase : int = VisionEncoderDecoderModel(encoder=snake_case__ , decoder=snake_case__ ) model.eval() # load state_dict of original model, rename some keys UpperCAmelCase : Dict = torch.hub.load_state_dict_from_url(snake_case__ , map_location='''cpu''' , check_hash=snake_case__ )['''model'''] UpperCAmelCase : Tuple = create_rename_keys(snake_case__ , snake_case__ ) for src, dest in rename_keys: rename_key(snake_case__ , snake_case__ , snake_case__ ) read_in_q_k_v(snake_case__ , snake_case__ ) # remove parameters we don't need del state_dict["encoder.deit.head.weight"] del state_dict["encoder.deit.head.bias"] del state_dict["decoder.version"] # add prefix to decoder keys for key, val in state_dict.copy().items(): UpperCAmelCase : Optional[int] = state_dict.pop(snake_case__ ) if key.startswith('''decoder''' ) and "output_projection" not in key: UpperCAmelCase : Tuple = val else: UpperCAmelCase : Union[str, Any] = val # load state dict model.load_state_dict(snake_case__ ) # Check outputs on an image UpperCAmelCase : Union[str, Any] = ViTImageProcessor(size=encoder_config.image_size ) UpperCAmelCase : List[Any] = RobertaTokenizer.from_pretrained('''roberta-large''' ) UpperCAmelCase : Any = TrOCRProcessor(snake_case__ , snake_case__ ) UpperCAmelCase : List[Any] = processor(images=prepare_img(snake_case__ ) , return_tensors='''pt''' ).pixel_values # verify logits UpperCAmelCase : int = torch.tensor([[model.config.decoder.decoder_start_token_id]] ) UpperCAmelCase : str = model(pixel_values=snake_case__ , decoder_input_ids=snake_case__ ) UpperCAmelCase : List[Any] = outputs.logits UpperCAmelCase : Optional[Any] = torch.Size([1, 1, 50_265] ) if "trocr-base-handwritten" in checkpoint_url: UpperCAmelCase : Optional[Any] = torch.tensor( [-1.4502, -4.6683, -0.5347, -2.9291, 9.1435, -3.0571, 8.9764, 1.7560, 8.7358, -1.5311] ) elif "trocr-large-handwritten" in checkpoint_url: UpperCAmelCase : List[Any] = torch.tensor( [-2.6437, -1.3129, -2.2596, -5.3455, 6.3539, 1.7604, 5.4991, 1.4702, 5.6113, 2.0170] ) elif "trocr-base-printed" in checkpoint_url: UpperCAmelCase : Optional[Any] = torch.tensor( [-5.6816, -5.8388, 1.1398, -6.9034, 6.8505, -2.4393, 1.2284, -1.0232, -1.9661, -3.9210] ) elif "trocr-large-printed" in checkpoint_url: UpperCAmelCase : int = torch.tensor( [-6.0162, -7.0959, 4.4155, -5.1063, 7.0468, -3.1631, 2.6466, -0.3081, -0.8106, -1.7535] ) if "stage1" not in checkpoint_url: assert logits.shape == expected_shape, "Shape of logits not as expected" assert torch.allclose(logits[0, 0, :10] , snake_case__ , atol=1E-3 ), "First elements of logits not as expected" Path(snake_case__ ).mkdir(exist_ok=snake_case__ ) print(f'''Saving model to {pytorch_dump_folder_path}''' ) model.save_pretrained(snake_case__ ) print(f'''Saving processor to {pytorch_dump_folder_path}''' ) processor.save_pretrained(snake_case__ ) if __name__ == "__main__": _lowerCamelCase : int = argparse.ArgumentParser() parser.add_argument( "--checkpoint_url", default="https://layoutlm.blob.core.windows.net/trocr/model_zoo/fairseq/trocr-base-handwritten.pt", type=str, help="URL to the original PyTorch checkpoint (.pth file).", ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, help="Path to the folder to output PyTorch model." ) _lowerCamelCase : Optional[int] = parser.parse_args() convert_tr_ocr_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)
336
from typing import Any, Dict, List, Optional, Tuple, Union import torch from torch import nn from torch.utils.data import DistributedSampler, RandomSampler from transformers import PreTrainedModel, Trainer, logging from transformers.integrations import is_fairscale_available from transformers.models.fsmt.configuration_fsmt import FSMTConfig from transformers.optimization import ( Adafactor, AdamW, get_constant_schedule, get_constant_schedule_with_warmup, get_cosine_schedule_with_warmup, get_cosine_with_hard_restarts_schedule_with_warmup, get_linear_schedule_with_warmup, get_polynomial_decay_schedule_with_warmup, ) from transformers.trainer_pt_utils import get_tpu_sampler from transformers.training_args import ParallelMode from transformers.utils import is_torch_tpu_available if is_fairscale_available(): from fairscale.optim import OSS __a = logging.get_logger(__name__) __a = { 'linear': get_linear_schedule_with_warmup, 'cosine': get_cosine_schedule_with_warmup, 'cosine_w_restarts': get_cosine_with_hard_restarts_schedule_with_warmup, 'polynomial': get_polynomial_decay_schedule_with_warmup, 'constant': get_constant_schedule, 'constant_w_warmup': get_constant_schedule_with_warmup, } class lowercase__( UpperCAmelCase ): """simple docstring""" def __init__( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : List[str]=None , SCREAMING_SNAKE_CASE_ : Optional[Any]=None , *SCREAMING_SNAKE_CASE_ : str , **SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> List[str]: super().__init__(*SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) if config is None: assert isinstance(self.model , SCREAMING_SNAKE_CASE_ ), ( "If no `config` is passed the model to be trained has to be of type `PreTrainedModel`, but is" f''' {self.model.__class__}''' ) lowercase_ = self.model.config else: lowercase_ = config lowercase_ = data_args lowercase_ = self.config.tgt_vocab_size if isinstance(self.config , SCREAMING_SNAKE_CASE_ ) else self.config.vocab_size if self.args.label_smoothing != 0 or (self.data_args is not None and self.data_args.ignore_pad_token_for_loss): assert self.config.pad_token_id is not None, ( "Make sure that `config.pad_token_id` is correcly defined when ignoring `pad_token` for loss" " calculation or doing label smoothing." ) if self.config.pad_token_id is None and self.config.eos_token_id is not None: logger.warning( f'''The `config.pad_token_id` is `None`. Using `config.eos_token_id` = {self.config.eos_token_id} for''' ''' padding..''' ) if self.args.label_smoothing == 0: lowercase_ = torch.nn.CrossEntropyLoss(ignore_index=self.config.pad_token_id ) else: # dynamically import label_smoothed_nll_loss from utils import label_smoothed_nll_loss lowercase_ = label_smoothed_nll_loss def _lowercase ( self : int , SCREAMING_SNAKE_CASE_ : int ) -> Optional[Any]: if self.optimizer is None: lowercase_ = ['''bias''', '''LayerNorm.weight'''] lowercase_ = [ { '''params''': [p for n, p in self.model.named_parameters() if not any(nd in n for nd in no_decay )], '''weight_decay''': self.args.weight_decay, }, { '''params''': [p for n, p in self.model.named_parameters() if any(nd in n for nd in no_decay )], '''weight_decay''': 0.0, }, ] lowercase_ = Adafactor if self.args.adafactor else AdamW if self.args.adafactor: lowercase_ = Adafactor lowercase_ = {'''scale_parameter''': False, '''relative_step''': False} else: lowercase_ = AdamW lowercase_ = { '''betas''': (self.args.adam_betaa, self.args.adam_betaa), '''eps''': self.args.adam_epsilon, } lowercase_ = self.args.learning_rate if self.sharded_ddp: lowercase_ = OSS( params=SCREAMING_SNAKE_CASE_ , optim=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) else: lowercase_ = optimizer_cls(SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) if self.lr_scheduler is None: lowercase_ = self._get_lr_scheduler(SCREAMING_SNAKE_CASE_ ) else: # ignoring --lr_scheduler logger.warning('''scheduler is passed to `Seq2SeqTrainer`, `--lr_scheduler` arg is ignored.''' ) def _lowercase ( self : List[Any] , SCREAMING_SNAKE_CASE_ : Dict ) -> Dict: lowercase_ = arg_to_scheduler[self.args.lr_scheduler] if self.args.lr_scheduler == "constant": lowercase_ = schedule_func(self.optimizer ) elif self.args.lr_scheduler == "constant_w_warmup": lowercase_ = schedule_func(self.optimizer , num_warmup_steps=self.args.warmup_steps ) else: lowercase_ = schedule_func( self.optimizer , num_warmup_steps=self.args.warmup_steps , num_training_steps=SCREAMING_SNAKE_CASE_ ) return scheduler def _lowercase ( self : Tuple ) -> Optional[torch.utils.data.Sampler]: if isinstance(self.train_dataset , torch.utils.data.IterableDataset ): return None elif is_torch_tpu_available(): return get_tpu_sampler(self.train_dataset ) else: if self.args.sortish_sampler: self.train_dataset.make_sortish_sampler( self.args.per_device_train_batch_size , distributed=(self.args.parallel_mode == ParallelMode.DISTRIBUTED) , ) return ( RandomSampler(self.train_dataset ) if self.args.local_rank == -1 else DistributedSampler(self.train_dataset ) ) def _lowercase ( self : List[Any] , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : List[Any] ) -> Any: if self.args.label_smoothing == 0: if self.data_args is not None and self.data_args.ignore_pad_token_for_loss: # force training to ignore pad token lowercase_ = model(**SCREAMING_SNAKE_CASE_ , use_cache=SCREAMING_SNAKE_CASE_ )[0] lowercase_ = self.loss_fn(logits.view(-1 , logits.shape[-1] ) , labels.view(-1 ) ) else: # compute usual loss via models lowercase_ , lowercase_ = model(**SCREAMING_SNAKE_CASE_ , labels=SCREAMING_SNAKE_CASE_ , use_cache=SCREAMING_SNAKE_CASE_ )[:2] else: # compute label smoothed loss lowercase_ = model(**SCREAMING_SNAKE_CASE_ , use_cache=SCREAMING_SNAKE_CASE_ )[0] lowercase_ = torch.nn.functional.log_softmax(SCREAMING_SNAKE_CASE_ , dim=-1 ) lowercase_ , lowercase_ = self.loss_fn(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , self.args.label_smoothing , ignore_index=self.config.pad_token_id ) return loss, logits def _lowercase ( self : str , SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : Optional[int] ) -> List[Any]: lowercase_ = inputs.pop('''labels''' ) lowercase_ , lowercase_ = self._compute_loss(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) return loss def _lowercase ( self : Optional[int] , SCREAMING_SNAKE_CASE_ : nn.Module , SCREAMING_SNAKE_CASE_ : Dict[str, Union[torch.Tensor, Any]] , SCREAMING_SNAKE_CASE_ : bool , SCREAMING_SNAKE_CASE_ : Optional[List[str]] = None , ) -> Tuple[Optional[float], Optional[torch.Tensor], Optional[torch.Tensor]]: lowercase_ = self._prepare_inputs(SCREAMING_SNAKE_CASE_ ) lowercase_ = { '''max_length''': self.data_args.val_max_target_length if self.data_args is not None else self.config.max_length, '''num_beams''': self.data_args.eval_beams if self.data_args is not None else self.config.num_beams, } if self.args.predict_with_generate and not self.args.prediction_loss_only: lowercase_ = self.model.generate( inputs['''input_ids'''] , attention_mask=inputs['''attention_mask'''] , **SCREAMING_SNAKE_CASE_ , ) # in case the batch is shorter than max length, the output should be padded if generated_tokens.shape[-1] < gen_kwargs["max_length"]: lowercase_ = self._pad_tensors_to_max_len(SCREAMING_SNAKE_CASE_ , gen_kwargs['''max_length'''] ) lowercase_ = inputs.pop('''labels''' ) with torch.no_grad(): # compute loss on predict data lowercase_ , lowercase_ = self._compute_loss(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) lowercase_ = loss.mean().detach() if self.args.prediction_loss_only: return (loss, None, None) lowercase_ = generated_tokens if self.args.predict_with_generate else logits if labels.shape[-1] < gen_kwargs["max_length"]: lowercase_ = self._pad_tensors_to_max_len(SCREAMING_SNAKE_CASE_ , gen_kwargs['''max_length'''] ) return (loss, logits, labels) def _lowercase ( self : str , SCREAMING_SNAKE_CASE_ : Optional[Any] , SCREAMING_SNAKE_CASE_ : int ) -> Tuple: # If PAD token is not defined at least EOS token has to be defined lowercase_ = self.config.pad_token_id if self.config.pad_token_id is not None else self.config.eos_token_id if pad_token_id is None: raise ValueError( '''Make sure that either `config.pad_token_id` or `config.eos_token_id` is defined if tensor has to be''' f''' padded to `max_length`={max_length}''' ) lowercase_ = pad_token_id * torch.ones( (tensor.shape[0], max_length) , dtype=tensor.dtype , device=tensor.device ) lowercase_ = tensor return padded_tensor
30
0
import unittest from transformers import is_torch_available from transformers.testing_utils import require_torch if is_torch_available(): import torch from transformers.generation import DisjunctiveConstraint @require_torch class lowerCamelCase_ ( unittest.TestCase ): '''simple docstring''' def UpperCamelCase__ ( self) -> List[Any]: # For consistency across different places the DisjunctiveConstraint is called, # dc.token_ids is a list of integers. It is also initialized only by integers. __UpperCamelCase :List[str] = [[1, 2, 4], [1, 2, 3, 4]] __UpperCamelCase :Union[str, Any] = DisjunctiveConstraint(SCREAMING_SNAKE_CASE_) self.assertTrue(isinstance(dc.token_ids , SCREAMING_SNAKE_CASE_)) with self.assertRaises(SCREAMING_SNAKE_CASE_): DisjunctiveConstraint(torch.LongTensor([[1, 2, 4], [1, 2, 3]])) with self.assertRaises(SCREAMING_SNAKE_CASE_): DisjunctiveConstraint([torch.LongTensor([1, 2, 4]), torch.LongTensor([1, 2, 3, 4, 5])]) def UpperCamelCase__ ( self) -> Optional[Any]: # We can't have constraints that are complete subsets of another. This leads to a preverse # interpretation of "constraint fulfillment": does generating [1,2,3] fulfill the constraint? # It would mean that it generated [1,2] which fulfills it, but it's in the middle of potentially # fulfilling [1,2,3,4]. If we believe that [1,2,3] does fulfill the constraint, then the algorithm # will necessarily never reach [1,2,3,4], giving users a false sense of control (better to just not allow it). __UpperCamelCase :int = [[1, 2], [1, 2, 3, 4]] with self.assertRaises(SCREAMING_SNAKE_CASE_): DisjunctiveConstraint(SCREAMING_SNAKE_CASE_) # fails here def UpperCamelCase__ ( self) -> Any: __UpperCamelCase :List[str] = [[1, 2, 3], [1, 2, 4]] __UpperCamelCase :Optional[Any] = DisjunctiveConstraint(SCREAMING_SNAKE_CASE_) __UpperCamelCase , __UpperCamelCase , __UpperCamelCase :Optional[Any] = dc.update(1) __UpperCamelCase :List[Any] = stepped is True and completed is False and reset is False self.assertTrue(SCREAMING_SNAKE_CASE_) self.assertTrue(not dc.completed) self.assertTrue(dc.current_seq == [1]) __UpperCamelCase , __UpperCamelCase , __UpperCamelCase :Optional[int] = dc.update(2) __UpperCamelCase :int = stepped is True and completed is False and reset is False self.assertTrue(SCREAMING_SNAKE_CASE_) self.assertTrue(not dc.completed) self.assertTrue(dc.current_seq == [1, 2]) __UpperCamelCase , __UpperCamelCase , __UpperCamelCase :int = dc.update(3) __UpperCamelCase :List[str] = stepped is True and completed is True and reset is False self.assertTrue(SCREAMING_SNAKE_CASE_) self.assertTrue(dc.completed) # Completed! self.assertTrue(dc.current_seq == [1, 2, 3]) def UpperCamelCase__ ( self) -> int: __UpperCamelCase :List[Any] = [[1, 2, 3], [1, 2, 4, 5], [1, 2, 5]] __UpperCamelCase :List[str] = DisjunctiveConstraint(SCREAMING_SNAKE_CASE_) __UpperCamelCase , __UpperCamelCase , __UpperCamelCase :Any = dc.update(1) self.assertTrue(not dc.completed) self.assertTrue(dc.current_seq == [1]) __UpperCamelCase , __UpperCamelCase , __UpperCamelCase :Any = dc.update(2) self.assertTrue(not dc.completed) self.assertTrue(dc.current_seq == [1, 2]) __UpperCamelCase , __UpperCamelCase , __UpperCamelCase :Optional[Any] = dc.update(4) self.assertTrue(not dc.completed) self.assertTrue(dc.current_seq == [1, 2, 4]) __UpperCamelCase , __UpperCamelCase , __UpperCamelCase :Tuple = dc.update(5) self.assertTrue(dc.completed) # Completed! self.assertTrue(dc.current_seq == [1, 2, 4, 5]) dc.reset() __UpperCamelCase , __UpperCamelCase , __UpperCamelCase :Dict = dc.update(1) self.assertTrue(not dc.completed) self.assertTrue(dc.remaining() == 3) self.assertTrue(dc.current_seq == [1]) __UpperCamelCase , __UpperCamelCase , __UpperCamelCase :Dict = dc.update(2) self.assertTrue(not dc.completed) self.assertTrue(dc.remaining() == 2) self.assertTrue(dc.current_seq == [1, 2]) __UpperCamelCase , __UpperCamelCase , __UpperCamelCase :Any = dc.update(5) self.assertTrue(dc.completed) # Completed! self.assertTrue(dc.remaining() == 0) self.assertTrue(dc.current_seq == [1, 2, 5])
43
import unittest import numpy as np import torch from .utils_summarization import build_mask, compute_token_type_ids, process_story, truncate_or_pad class lowercase__( unittest.TestCase ): """simple docstring""" def _lowercase ( self : List[str] ) -> List[Any]: lowercase_ = 1_0 def _lowercase ( self : int ) -> List[str]: lowercase_ = [1, 2, 3, 4] lowercase_ = [1, 2, 3, 4, 0, 0, 0, 0, 0, 0] self.assertEqual(truncate_or_pad(SCREAMING_SNAKE_CASE_ , self.block_size , 0 ) , SCREAMING_SNAKE_CASE_ ) def _lowercase ( self : int ) -> Optional[Any]: lowercase_ = [1, 2, 3, 4, 5, 6, 7, 8, 9, 1_0] lowercase_ = [1, 2, 3, 4, 5, 6, 7, 8, 9, 1_0] self.assertEqual(truncate_or_pad(SCREAMING_SNAKE_CASE_ , self.block_size , 0 ) , SCREAMING_SNAKE_CASE_ ) def _lowercase ( self : Union[str, Any] ) -> Optional[int]: lowercase_ = [1, 2, 3, 4, 5, 6, 7, 8, 9, 1_0, 1_1, 1_2, 1_3] lowercase_ = [1, 2, 3, 4, 5, 6, 7, 8, 9, 1_0] self.assertEqual(truncate_or_pad(SCREAMING_SNAKE_CASE_ , self.block_size , 0 ) , SCREAMING_SNAKE_CASE_ ) def _lowercase ( self : Any ) -> List[Any]: lowercase_ = '''It was the year of Our Lord one thousand seven hundred and seventy-five.\n\nSpiritual revelations were conceded to England at that favoured period, as at this.''' lowercase_ , lowercase_ = process_story(SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ , [] ) def _lowercase ( self : List[str] ) -> List[str]: lowercase_ = '''''' lowercase_ , lowercase_ = process_story(SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ , [] ) self.assertEqual(SCREAMING_SNAKE_CASE_ , [] ) def _lowercase ( self : Union[str, Any] ) -> Union[str, Any]: lowercase_ = ( '''It was the year of Our Lord one thousand seven hundred and ''' '''seventy-five\n\nSpiritual revelations were conceded to England ''' '''at that favoured period, as at this.\n@highlight\n\nIt was the best of times''' ) lowercase_ , lowercase_ = process_story(SCREAMING_SNAKE_CASE_ ) lowercase_ = [ '''It was the year of Our Lord one thousand seven hundred and seventy-five.''', '''Spiritual revelations were conceded to England at that favoured period, as at this.''', ] self.assertEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) lowercase_ = ['''It was the best of times.'''] self.assertEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def _lowercase ( self : Union[str, Any] ) -> Optional[Any]: lowercase_ = torch.tensor([1, 2, 3, 4] ) lowercase_ = torch.tensor([1, 1, 1, 1] ) np.testing.assert_array_equal(build_mask(SCREAMING_SNAKE_CASE_ , 0 ).numpy() , expected.numpy() ) def _lowercase ( self : List[Any] ) -> Tuple: lowercase_ = torch.tensor([1, 2, 3, 4, 2_3, 2_3, 2_3] ) lowercase_ = torch.tensor([1, 1, 1, 1, 0, 0, 0] ) np.testing.assert_array_equal(build_mask(SCREAMING_SNAKE_CASE_ , 2_3 ).numpy() , expected.numpy() ) def _lowercase ( self : int ) -> Dict: lowercase_ = torch.tensor([8, 2, 3, 4, 1, 1, 1] ) lowercase_ = torch.tensor([1, 1, 1, 1, 0, 0, 0] ) np.testing.assert_array_equal(build_mask(SCREAMING_SNAKE_CASE_ , 1 ).numpy() , expected.numpy() ) def _lowercase ( self : List[str] ) -> Tuple: lowercase_ = 1_0_1 lowercase_ = torch.tensor([[1, 2, 3, 4, 5, 6], [1, 2, 3, 1_0_1, 5, 6], [1, 1_0_1, 3, 4, 1_0_1, 6]] ) lowercase_ = torch.tensor([[1, 1, 1, 1, 1, 1], [1, 1, 1, 0, 0, 0], [1, 0, 0, 0, 1, 1]] ) lowercase_ = compute_token_type_ids(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) np.testing.assert_array_equal(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ )
30
0
import logging import math from functools import partial from typing import Any, Callable, Dict, Iterable, List, Optional, Sequence, Tuple, Union import torch from .tensor_utils import tensor_tree_map, tree_map def _A ( _lowercase ) -> int: """simple docstring""" __UpperCamelCase = [] if isinstance(snake_case__ , snake_case__ ): for v in tree.values(): shapes.extend(_fetch_dims(snake_case__ ) ) elif isinstance(snake_case__ , (list, tuple) ): for t in tree: shapes.extend(_fetch_dims(snake_case__ ) ) elif isinstance(snake_case__ , torch.Tensor ): shapes.append(tree.shape ) else: raise ValueError('Not supported' ) return shapes @torch.jit.ignore def _A ( _lowercase , _lowercase ) -> List[Any]: """simple docstring""" __UpperCamelCase = [] for d in reversed(snake_case__ ): idx.append(flat_idx % d ) __UpperCamelCase = flat_idx // d return tuple(reversed(snake_case__ ) ) @torch.jit.ignore def _A ( _lowercase , _lowercase , _lowercase , _lowercase = None , _lowercase = None , ) -> Optional[int]: """simple docstring""" def reduce_edge_list(_lowercase ) -> None: __UpperCamelCase = True for i in range(len(snake_case__ ) ): __UpperCamelCase = -1 * (i + 1) l[reversed_idx] &= tally __UpperCamelCase = l[reversed_idx] if start_edges is None: __UpperCamelCase = [s == 0 for s in start] reduce_edge_list(snake_case__ ) if end_edges is None: __UpperCamelCase = [e == (d - 1) for e, d in zip(snake_case__ , snake_case__ )] reduce_edge_list(snake_case__ ) # Base cases. Either start/end are empty and we're done, or the final, # one-dimensional tensor can be simply sliced if len(snake_case__ ) == 0: return [()] elif len(snake_case__ ) == 1: return [(slice(start[0] , end[0] + 1 ),)] __UpperCamelCase = [] __UpperCamelCase = [] # Dimensions common to start and end can be selected directly for s, e in zip(snake_case__ , snake_case__ ): if s == e: path_list.append(slice(snake_case__ , s + 1 ) ) else: break __UpperCamelCase = tuple(snake_case__ ) __UpperCamelCase = len(snake_case__ ) # start == end, and we're done if divergence_idx == len(snake_case__ ): return [path] def upper() -> Tuple[Tuple[slice, ...], ...]: assert start_edges is not None assert end_edges is not None __UpperCamelCase = start[divergence_idx] return tuple( path + (slice(snake_case__ , sdi + 1 ),) + s for s in _get_minimal_slice_set( start[divergence_idx + 1 :] , [d - 1 for d in dims[divergence_idx + 1 :]] , dims[divergence_idx + 1 :] , start_edges=start_edges[divergence_idx + 1 :] , end_edges=[True for _ in end_edges[divergence_idx + 1 :]] , ) ) def lower() -> Tuple[Tuple[slice, ...], ...]: assert start_edges is not None assert end_edges is not None __UpperCamelCase = end[divergence_idx] return tuple( path + (slice(snake_case__ , edi + 1 ),) + s for s in _get_minimal_slice_set( [0 for _ in start[divergence_idx + 1 :]] , end[divergence_idx + 1 :] , dims[divergence_idx + 1 :] , start_edges=[True for _ in start_edges[divergence_idx + 1 :]] , end_edges=end_edges[divergence_idx + 1 :] , ) ) # If both start and end are at the edges of the subtree rooted at # divergence_idx, we can just select the whole subtree at once if start_edges[divergence_idx] and end_edges[divergence_idx]: slices.append(path + (slice(start[divergence_idx] , end[divergence_idx] + 1 ),) ) # If just start is at the edge, we can grab almost all of the subtree, # treating only the ragged bottom edge as an edge case elif start_edges[divergence_idx]: slices.append(path + (slice(start[divergence_idx] , end[divergence_idx] ),) ) slices.extend(lower() ) # Analogous to the previous case, but the top is ragged this time elif end_edges[divergence_idx]: slices.extend(upper() ) slices.append(path + (slice(start[divergence_idx] + 1 , end[divergence_idx] + 1 ),) ) # If both sides of the range are ragged, we need to handle both sides # separately. If there's contiguous meat in between them, we can index it # in one big chunk else: slices.extend(upper() ) __UpperCamelCase = end[divergence_idx] - start[divergence_idx] if middle_ground > 1: slices.append(path + (slice(start[divergence_idx] + 1 , end[divergence_idx] ),) ) slices.extend(lower() ) return slices @torch.jit.ignore def _A ( _lowercase , _lowercase , _lowercase , _lowercase ) -> Dict: """simple docstring""" __UpperCamelCase = t.shape[:no_batch_dims] __UpperCamelCase = list(_flat_idx_to_idx(snake_case__ , snake_case__ ) ) # _get_minimal_slice_set is inclusive __UpperCamelCase = list(_flat_idx_to_idx(flat_end - 1 , snake_case__ ) ) # Get an ordered list of slices to perform __UpperCamelCase = _get_minimal_slice_set( snake_case__ , snake_case__ , snake_case__ , ) __UpperCamelCase = [t[s] for s in slices] return torch.cat([s.view((-1,) + t.shape[no_batch_dims:] ) for s in sliced_tensors] ) def _A ( _lowercase , _lowercase , _lowercase , _lowercase , _lowercase = False , _lowercase = None , _lowercase = False , ) -> Dict: """simple docstring""" if not (len(snake_case__ ) > 0): raise ValueError('Must provide at least one input' ) __UpperCamelCase = [shape[:no_batch_dims] for shape in _fetch_dims(snake_case__ )] __UpperCamelCase = tuple([max(snake_case__ ) for s in zip(*snake_case__ )] ) def _prep_inputs(_lowercase ) -> torch.Tensor: if not low_mem: if not sum(t.shape[:no_batch_dims] ) == no_batch_dims: __UpperCamelCase = t.expand(orig_batch_dims + t.shape[no_batch_dims:] ) __UpperCamelCase = t.reshape(-1 , *t.shape[no_batch_dims:] ) else: __UpperCamelCase = t.expand(orig_batch_dims + t.shape[no_batch_dims:] ) return t __UpperCamelCase = tensor_tree_map(_prep_inputs , snake_case__ ) __UpperCamelCase = None if _out is not None: __UpperCamelCase = tensor_tree_map(lambda _lowercase : t.view([-1] + list(t.shape[no_batch_dims:] ) ) , _out ) __UpperCamelCase = 1 for d in orig_batch_dims: flat_batch_dim *= d __UpperCamelCase = flat_batch_dim // chunk_size + (flat_batch_dim % chunk_size != 0) def _select_chunk(_lowercase ) -> torch.Tensor: return t[i : i + chunk_size] if t.shape[0] != 1 else t __UpperCamelCase = 0 __UpperCamelCase = prepped_outputs for _ in range(snake_case__ ): # Chunk the input if not low_mem: __UpperCamelCase = _select_chunk else: __UpperCamelCase = partial( _chunk_slice , flat_start=snake_case__ , flat_end=min(snake_case__ , i + chunk_size ) , no_batch_dims=len(snake_case__ ) , ) __UpperCamelCase = tensor_tree_map(snake_case__ , snake_case__ ) # Run the layer on the chunk __UpperCamelCase = layer(**snake_case__ ) # Allocate space for the output if out is None: __UpperCamelCase = tensor_tree_map(lambda _lowercase : t.new_zeros((flat_batch_dim,) + t.shape[1:] ) , snake_case__ ) # Put the chunk in its pre-allocated space if isinstance(snake_case__ , snake_case__ ): def assign(_lowercase , _lowercase ) -> None: for k, v in da.items(): if isinstance(snake_case__ , snake_case__ ): assign(snake_case__ , da[k] ) else: if _add_into_out: v[i : i + chunk_size] += da[k] else: __UpperCamelCase = da[k] assign(snake_case__ , snake_case__ ) elif isinstance(snake_case__ , snake_case__ ): for xa, xa in zip(snake_case__ , snake_case__ ): if _add_into_out: xa[i : i + chunk_size] += xa else: __UpperCamelCase = xa elif isinstance(snake_case__ , torch.Tensor ): if _add_into_out: out[i : i + chunk_size] += output_chunk else: __UpperCamelCase = output_chunk else: raise ValueError('Not supported' ) i += chunk_size __UpperCamelCase = tensor_tree_map(lambda _lowercase : t.view(orig_batch_dims + t.shape[1:] ) , snake_case__ ) return out class __lowerCamelCase : def __init__( self: Union[str, Any],A_: int = 512,): '''simple docstring''' __UpperCamelCase = max_chunk_size __UpperCamelCase = None __UpperCamelCase = None def snake_case_ ( self: Dict,A_: Callable,A_: tuple,A_: int ): '''simple docstring''' logging.info('Tuning chunk size...' ) if min_chunk_size >= self.max_chunk_size: return min_chunk_size __UpperCamelCase = [2**l for l in range(int(math.log(self.max_chunk_size,2 ) ) + 1 )] __UpperCamelCase = [c for c in candidates if c > min_chunk_size] __UpperCamelCase = [min_chunk_size] + candidates candidates[-1] += 4 def test_chunk_size(A_: int ) -> bool: try: with torch.no_grad(): fn(*SCREAMING_SNAKE_CASE_,chunk_size=SCREAMING_SNAKE_CASE_ ) return True except RuntimeError: return False __UpperCamelCase = 0 __UpperCamelCase = len(SCREAMING_SNAKE_CASE_ ) - 1 while i > min_viable_chunk_size_index: __UpperCamelCase = test_chunk_size(candidates[i] ) if not viable: __UpperCamelCase = (min_viable_chunk_size_index + i) // 2 else: __UpperCamelCase = i __UpperCamelCase = (i + len(SCREAMING_SNAKE_CASE_ ) - 1) // 2 return candidates[min_viable_chunk_size_index] def snake_case_ ( self: Tuple,A_: Iterable,A_: Iterable ): '''simple docstring''' __UpperCamelCase = True for aa, aa in zip(SCREAMING_SNAKE_CASE_,SCREAMING_SNAKE_CASE_ ): assert type(SCREAMING_SNAKE_CASE_ ) == type(SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_,(list, tuple) ): consistent &= self._compare_arg_caches(SCREAMING_SNAKE_CASE_,SCREAMING_SNAKE_CASE_ ) elif isinstance(SCREAMING_SNAKE_CASE_,SCREAMING_SNAKE_CASE_ ): __UpperCamelCase = [v for _, v in sorted(aa.items(),key=lambda A_ : x[0] )] __UpperCamelCase = [v for _, v in sorted(aa.items(),key=lambda A_ : x[0] )] consistent &= self._compare_arg_caches(SCREAMING_SNAKE_CASE_,SCREAMING_SNAKE_CASE_ ) else: consistent &= aa == aa return consistent def snake_case_ ( self: Tuple,A_: Callable,A_: tuple,A_: int,): '''simple docstring''' __UpperCamelCase = True __UpperCamelCase = tree_map(lambda A_ : a.shape if isinstance(SCREAMING_SNAKE_CASE_,torch.Tensor ) else a,SCREAMING_SNAKE_CASE_,SCREAMING_SNAKE_CASE_ ) if self.cached_arg_data is not None: # If args have changed shape/value, we need to re-tune assert len(self.cached_arg_data ) == len(SCREAMING_SNAKE_CASE_ ) __UpperCamelCase = self._compare_arg_caches(self.cached_arg_data,SCREAMING_SNAKE_CASE_ ) else: # Otherwise, we can reuse the precomputed value __UpperCamelCase = False if not consistent: __UpperCamelCase = self._determine_favorable_chunk_size( SCREAMING_SNAKE_CASE_,SCREAMING_SNAKE_CASE_,SCREAMING_SNAKE_CASE_,) __UpperCamelCase = arg_data assert self.cached_chunk_size is not None return self.cached_chunk_size
310
def a ( snake_case__: list ): '''simple docstring''' if len(snake_case__ ) <= 1: return [tuple(snake_case__ )] lowercase_ = [] def generate(snake_case__: int , snake_case__: list ): if k == 1: res.append(tuple(arr[:] ) ) return generate(k - 1 , snake_case__ ) for i in range(k - 1 ): if k % 2 == 0: # k is even lowercase_ , lowercase_ = arr[k - 1], arr[i] else: # k is odd lowercase_ , lowercase_ = arr[k - 1], arr[0] generate(k - 1 , snake_case__ ) generate(len(snake_case__ ) , snake_case__ ) return res if __name__ == "__main__": __a = input('Enter numbers separated by a comma:\n').strip() __a = [int(item) for item in user_input.split(',')] print(heaps(arr))
30
0
"""simple docstring""" import numpy as np import torch from torch.utils.data import Dataset, IterableDataset from ..utils.generic import ModelOutput class A__ ( _lowerCamelCase): def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): __lowerCAmelCase : Tuple = dataset __lowerCAmelCase : List[Any] = process __lowerCAmelCase : List[str] = params def __len__( self ): return len(self.dataset ) def __getitem__( self , _SCREAMING_SNAKE_CASE ): __lowerCAmelCase : str = self.dataset[i] __lowerCAmelCase : str = self.process(SCREAMING_SNAKE_CASE_ , **self.params ) return processed class A__ ( _lowerCamelCase): def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=None ): __lowerCAmelCase : Optional[Any] = loader __lowerCAmelCase : Optional[int] = infer __lowerCAmelCase : str = params if loader_batch_size == 1: # Let's spare some time by deactivating altogether __lowerCAmelCase : List[Any] = None __lowerCAmelCase : Tuple = loader_batch_size # Internal bookkeeping __lowerCAmelCase : Optional[Any] = None __lowerCAmelCase : List[Any] = None def __len__( self ): return len(self.loader ) def __iter__( self ): __lowerCAmelCase : str = iter(self.loader ) return self def __lowerCamelCase ( self ): if isinstance(self._loader_batch_data , torch.Tensor ): # Batch data is simple tensor, just fetch the slice __lowerCAmelCase : str = self._loader_batch_data[self._loader_batch_index] else: # Batch data is assumed to be BaseModelOutput (or dict) __lowerCAmelCase : str = {} for k, element in self._loader_batch_data.items(): if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): # Convert ModelOutput to tuple first __lowerCAmelCase : Optional[int] = element.to_tuple() if isinstance(element[0] , torch.Tensor ): __lowerCAmelCase : Dict = tuple(el[self._loader_batch_index].unsqueeze(0 ) for el in element ) elif isinstance(element[0] , np.ndarray ): __lowerCAmelCase : str = tuple(np.expand_dims(el[self._loader_batch_index] , 0 ) for el in element ) continue if k in {"hidden_states", "past_key_values", "attentions"} and isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): # Those are stored as lists of tensors so need specific unbatching. if isinstance(element[0] , torch.Tensor ): __lowerCAmelCase : Optional[Any] = tuple(el[self._loader_batch_index].unsqueeze(0 ) for el in element ) elif isinstance(element[0] , np.ndarray ): __lowerCAmelCase : Dict = tuple(np.expand_dims(el[self._loader_batch_index] , 0 ) for el in element ) continue if element is None: # This can happen for optional data that get passed around __lowerCAmelCase : Dict = None elif isinstance(element[self._loader_batch_index] , torch.Tensor ): # Take correct batch data, but make it looked like batch_size=1 # For compatibility with other methods within transformers __lowerCAmelCase : int = element[self._loader_batch_index].unsqueeze(0 ) elif isinstance(element[self._loader_batch_index] , np.ndarray ): # Take correct batch data, but make it looked like batch_size=1 # For compatibility with other methods within transformers __lowerCAmelCase : int = np.expand_dims(element[self._loader_batch_index] , 0 ) else: # This is typically a list, so no need to `unsqueeze`. __lowerCAmelCase : Any = element[self._loader_batch_index] # Recreate the element by reusing the original class to make it look # batch_size=1 __lowerCAmelCase : Union[str, Any] = self._loader_batch_data.__class__(SCREAMING_SNAKE_CASE_ ) self._loader_batch_index += 1 return result def __lowerCamelCase ( self ): if self._loader_batch_index is not None and self._loader_batch_index < self.loader_batch_size: # We are currently unrolling a batch so we just need to return # the current item within a batch return self.loader_batch_item() # We're out of items within a batch __lowerCAmelCase : Optional[int] = next(self.iterator ) __lowerCAmelCase : Any = self.infer(SCREAMING_SNAKE_CASE_ , **self.params ) # We now have a batch of "inferred things". if self.loader_batch_size is not None: # Try to infer the size of the batch if isinstance(SCREAMING_SNAKE_CASE_ , torch.Tensor ): __lowerCAmelCase : Optional[Any] = processed else: __lowerCAmelCase : Optional[Any] = list(processed.keys() )[0] __lowerCAmelCase : Dict = processed[key] if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): __lowerCAmelCase : Union[str, Any] = len(SCREAMING_SNAKE_CASE_ ) else: __lowerCAmelCase : Dict = first_tensor.shape[0] if 0 < observed_batch_size < self.loader_batch_size: # could be last batch so we can't unroll as many # elements. __lowerCAmelCase : Tuple = observed_batch_size # Setting internal index to unwrap the batch __lowerCAmelCase : List[Any] = processed __lowerCAmelCase : List[Any] = 0 return self.loader_batch_item() else: # We're not unrolling batches return processed class A__ ( _lowerCamelCase): def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=None ): super().__init__(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def __iter__( self ): __lowerCAmelCase : str = iter(self.loader ) __lowerCAmelCase : Union[str, Any] = None return self def __lowerCamelCase ( self ): if self.subiterator is None: __lowerCAmelCase : Optional[int] = self.infer(next(self.iterator ) , **self.params ) try: # Try to return next item __lowerCAmelCase : Optional[int] = next(self.subiterator ) except StopIteration: # When a preprocess iterator ends, we can start lookig at the next item # ChunkIterator will keep feeding until ALL elements of iterator # all have created their subiterator and have been iterating against. # # Another way to look at it, is we're basically flattening lists of lists # into a single list, but with generators __lowerCAmelCase : List[Any] = self.infer(next(self.iterator ) , **self.params ) __lowerCAmelCase : List[str] = next(self.subiterator ) return processed class A__ ( _lowerCamelCase): def __iter__( self ): __lowerCAmelCase : List[Any] = iter(self.loader ) return self def __lowerCamelCase ( self ): # Extremely similar to PipelineIterator in its unpacking mechanism # BUT, we have an extra required item which is the presence of `is_last` # That is because everything is flattened by `PipelineChunkIterator` we # need to keep track of how to regroup here in the original `process` # boundaries so that `process` and `postprocess` see the same data. # This iterator accumulates items (possibly while unbatching) until it # its a `is_last` and then just passes it on to the caller. __lowerCAmelCase : List[str] = False __lowerCAmelCase : str = [] if self._loader_batch_index is not None and self._loader_batch_index < self.loader_batch_size: while self._loader_batch_index < self.loader_batch_size: __lowerCAmelCase : Any = self.loader_batch_item() __lowerCAmelCase : Union[str, Any] = item.pop('is_last' ) accumulator.append(SCREAMING_SNAKE_CASE_ ) if is_last: return accumulator while not is_last: __lowerCAmelCase : Optional[Any] = self.infer(next(self.iterator ) , **self.params ) if self.loader_batch_size is not None: if isinstance(SCREAMING_SNAKE_CASE_ , torch.Tensor ): __lowerCAmelCase : Optional[int] = processed else: __lowerCAmelCase : List[str] = list(processed.keys() )[0] __lowerCAmelCase : int = processed[key] if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): __lowerCAmelCase : Optional[int] = len(SCREAMING_SNAKE_CASE_ ) else: __lowerCAmelCase : int = first_tensor.shape[0] if 0 < observed_batch_size < self.loader_batch_size: # could be last batch so we can't unroll as many # elements. __lowerCAmelCase : Union[str, Any] = observed_batch_size __lowerCAmelCase : Optional[int] = processed __lowerCAmelCase : List[Any] = 0 while self._loader_batch_index < self.loader_batch_size: __lowerCAmelCase : Dict = self.loader_batch_item() __lowerCAmelCase : List[Any] = item.pop('is_last' ) accumulator.append(SCREAMING_SNAKE_CASE_ ) if is_last: return accumulator else: __lowerCAmelCase : str = processed __lowerCAmelCase : int = item.pop('is_last' ) accumulator.append(SCREAMING_SNAKE_CASE_ ) return accumulator class A__ ( _lowerCamelCase): def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): __lowerCAmelCase : Dict = dataset __lowerCAmelCase : Dict = key def __len__( self ): return len(self.dataset ) def __getitem__( self , _SCREAMING_SNAKE_CASE ): return self.dataset[i][self.key] class A__ ( _lowerCamelCase): def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): __lowerCAmelCase : Optional[Any] = dataset __lowerCAmelCase : Any = keya __lowerCAmelCase : Any = keya def __len__( self ): return len(self.dataset ) def __getitem__( self , _SCREAMING_SNAKE_CASE ): return {"text": self.dataset[i][self.keya], "text_pair": self.dataset[i][self.keya]}
86
import argparse import math import os import torch from neural_compressor.utils.pytorch import load from PIL import Image from transformers import CLIPTextModel, CLIPTokenizer from diffusers import AutoencoderKL, StableDiffusionPipeline, UNetaDConditionModel def a ( ): '''simple docstring''' lowercase_ = argparse.ArgumentParser() parser.add_argument( '''-m''' , '''--pretrained_model_name_or_path''' , type=snake_case__ , default=snake_case__ , required=snake_case__ , help='''Path to pretrained model or model identifier from huggingface.co/models.''' , ) parser.add_argument( '''-c''' , '''--caption''' , type=snake_case__ , default='''robotic cat with wings''' , help='''Text used to generate images.''' , ) parser.add_argument( '''-n''' , '''--images_num''' , type=snake_case__ , default=4 , help='''How much images to generate.''' , ) parser.add_argument( '''-s''' , '''--seed''' , type=snake_case__ , default=42 , help='''Seed for random process.''' , ) parser.add_argument( '''-ci''' , '''--cuda_id''' , type=snake_case__ , default=0 , help='''cuda_id.''' , ) lowercase_ = parser.parse_args() return args def a ( snake_case__: Optional[Any] , snake_case__: Tuple , snake_case__: Union[str, Any] ): '''simple docstring''' if not len(snake_case__ ) == rows * cols: raise ValueError('''The specified number of rows and columns are not correct.''' ) lowercase_ , lowercase_ = imgs[0].size lowercase_ = Image.new('''RGB''' , size=(cols * w, rows * h) ) lowercase_ , lowercase_ = grid.size for i, img in enumerate(snake_case__ ): grid.paste(snake_case__ , box=(i % cols * w, i // cols * h) ) return grid def a ( snake_case__: Tuple , snake_case__: Union[str, Any]="robotic cat with wings" , snake_case__: Union[str, Any]=7.5 , snake_case__: List[str]=50 , snake_case__: List[Any]=1 , snake_case__: Optional[int]=42 , ): '''simple docstring''' lowercase_ = torch.Generator(pipeline.device ).manual_seed(snake_case__ ) lowercase_ = pipeline( snake_case__ , guidance_scale=snake_case__ , num_inference_steps=snake_case__ , generator=snake_case__ , num_images_per_prompt=snake_case__ , ).images lowercase_ = int(math.sqrt(snake_case__ ) ) lowercase_ = image_grid(snake_case__ , rows=_rows , cols=num_images_per_prompt // _rows ) return grid, images __a = parse_args() # Load models and create wrapper for stable diffusion __a = CLIPTokenizer.from_pretrained(args.pretrained_model_name_or_path, subfolder='tokenizer') __a = CLIPTextModel.from_pretrained(args.pretrained_model_name_or_path, subfolder='text_encoder') __a = AutoencoderKL.from_pretrained(args.pretrained_model_name_or_path, subfolder='vae') __a = UNetaDConditionModel.from_pretrained(args.pretrained_model_name_or_path, subfolder='unet') __a = StableDiffusionPipeline.from_pretrained( args.pretrained_model_name_or_path, text_encoder=text_encoder, vae=vae, unet=unet, tokenizer=tokenizer ) __a = lambda images, clip_input: (images, False) if os.path.exists(os.path.join(args.pretrained_model_name_or_path, 'best_model.pt')): __a = load(args.pretrained_model_name_or_path, model=unet) unet.eval() setattr(pipeline, 'unet', unet) else: __a = unet.to(torch.device('cuda', args.cuda_id)) __a = pipeline.to(unet.device) __a , __a = generate_images(pipeline, prompt=args.caption, num_images_per_prompt=args.images_num, seed=args.seed) grid.save(os.path.join(args.pretrained_model_name_or_path, '{}.png'.format('_'.join(args.caption.split())))) __a = os.path.join(args.pretrained_model_name_or_path, '_'.join(args.caption.split())) os.makedirs(dirname, exist_ok=True) for idx, image in enumerate(images): image.save(os.path.join(dirname, '{}.png'.format(idx + 1)))
30
0
from __future__ import annotations import unittest from transformers import EsmConfig, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import numpy import tensorflow as tf from transformers.models.esm.modeling_tf_esm import ( TF_ESM_PRETRAINED_MODEL_ARCHIVE_LIST, TFEsmForMaskedLM, TFEsmForSequenceClassification, TFEsmForTokenClassification, TFEsmModel, ) class snake_case__: '''simple docstring''' def __init__( self , __lowercase , ) -> Any: lowerCAmelCase_ : List[str] = parent lowerCAmelCase_ : Optional[Any] = 1_3 lowerCAmelCase_ : Tuple = 7 lowerCAmelCase_ : Any = True lowerCAmelCase_ : Tuple = True lowerCAmelCase_ : List[Any] = True lowerCAmelCase_ : str = 9_9 lowerCAmelCase_ : Any = 3_2 lowerCAmelCase_ : int = 2 lowerCAmelCase_ : str = 4 lowerCAmelCase_ : int = 3_7 lowerCAmelCase_ : Optional[int] = '''gelu''' lowerCAmelCase_ : str = 0.1 lowerCAmelCase_ : Dict = 0.1 lowerCAmelCase_ : Dict = 5_1_2 lowerCAmelCase_ : int = 1_6 lowerCAmelCase_ : Tuple = 2 lowerCAmelCase_ : List[str] = 0.02 lowerCAmelCase_ : str = 3 lowerCAmelCase_ : str = 4 lowerCAmelCase_ : Dict = None def lowercase_ ( self ) -> Tuple: lowerCAmelCase_ : str = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) lowerCAmelCase_ : List[str] = None if self.use_input_mask: lowerCAmelCase_ : Union[str, Any] = random_attention_mask([self.batch_size, self.seq_length] ) lowerCAmelCase_ : List[Any] = None lowerCAmelCase_ : str = None lowerCAmelCase_ : Union[str, Any] = None if self.use_labels: lowerCAmelCase_ : int = ids_tensor([self.batch_size] , self.type_sequence_label_size ) lowerCAmelCase_ : Optional[int] = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) lowerCAmelCase_ : Dict = ids_tensor([self.batch_size] , self.num_choices ) lowerCAmelCase_ : Dict = EsmConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , pad_token_id=1 , 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 config, input_ids, input_mask, sequence_labels, token_labels, choice_labels def lowercase_ ( self ) -> Optional[Any]: ( ( lowerCAmelCase_ ) , ( lowerCAmelCase_ ) , ( lowerCAmelCase_ ) , ( lowerCAmelCase_ ) , ( lowerCAmelCase_ ) , ( lowerCAmelCase_ ) , ) : str = self.prepare_config_and_inputs() lowerCAmelCase_ : Optional[int] = True lowerCAmelCase_ : List[str] = floats_tensor([self.batch_size, self.seq_length, self.hidden_size] ) lowerCAmelCase_ : Optional[Any] = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) return ( config, input_ids, input_mask, sequence_labels, token_labels, choice_labels, encoder_hidden_states, encoder_attention_mask, ) def lowercase_ ( self , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase ) -> List[str]: lowerCAmelCase_ : Optional[Any] = TFEsmModel(config=SCREAMING_SNAKE_CASE_ ) lowerCAmelCase_ : Union[str, Any] = {'''input_ids''': input_ids, '''attention_mask''': input_mask} lowerCAmelCase_ : Optional[int] = model(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase_ : Any = [input_ids, input_mask] lowerCAmelCase_ : Dict = model(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase_ : str = model(SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def lowercase_ ( self , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , ) -> Union[str, Any]: lowerCAmelCase_ : Optional[Any] = True lowerCAmelCase_ : List[str] = TFEsmModel(config=SCREAMING_SNAKE_CASE_ ) lowerCAmelCase_ : int = { '''input_ids''': input_ids, '''attention_mask''': input_mask, '''encoder_hidden_states''': encoder_hidden_states, '''encoder_attention_mask''': encoder_attention_mask, } lowerCAmelCase_ : List[Any] = model(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase_ : Optional[int] = [input_ids, input_mask] lowerCAmelCase_ : Union[str, Any] = model(SCREAMING_SNAKE_CASE_ , encoder_hidden_states=SCREAMING_SNAKE_CASE_ ) # Also check the case where encoder outputs are not passed lowerCAmelCase_ : Union[str, Any] = 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 lowercase_ ( self , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase ) -> Any: lowerCAmelCase_ : Dict = TFEsmForMaskedLM(config=SCREAMING_SNAKE_CASE_ ) lowerCAmelCase_ : int = model([input_ids, input_mask] ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def lowercase_ ( self , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase ) -> int: lowerCAmelCase_ : Union[str, Any] = self.num_labels lowerCAmelCase_ : Optional[int] = TFEsmForTokenClassification(config=SCREAMING_SNAKE_CASE_ ) lowerCAmelCase_ : List[str] = {'''input_ids''': input_ids, '''attention_mask''': input_mask} lowerCAmelCase_ : Tuple = model(SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def lowercase_ ( self ) -> str: lowerCAmelCase_ : int = self.prepare_config_and_inputs() ( ( lowerCAmelCase_ ) , ( lowerCAmelCase_ ) , ( lowerCAmelCase_ ) , ( lowerCAmelCase_ ) , ( lowerCAmelCase_ ) , ( lowerCAmelCase_ ) , ) : Union[str, Any] = config_and_inputs lowerCAmelCase_ : List[Any] = {'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_tf class snake_case__( UpperCAmelCase__, UpperCAmelCase__, unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : str = ( ( TFEsmModel, TFEsmForMaskedLM, TFEsmForSequenceClassification, TFEsmForTokenClassification, ) if is_tf_available() else () ) SCREAMING_SNAKE_CASE__ : Union[str, Any] = ( { 'feature-extraction': TFEsmModel, 'fill-mask': TFEsmForMaskedLM, 'text-classification': TFEsmForSequenceClassification, 'token-classification': TFEsmForTokenClassification, 'zero-shot': TFEsmForSequenceClassification, } if is_tf_available() else {} ) SCREAMING_SNAKE_CASE__ : Union[str, Any] = False SCREAMING_SNAKE_CASE__ : Dict = False def lowercase_ ( self ) -> List[Any]: lowerCAmelCase_ : List[Any] = TFEsmModelTester(self ) lowerCAmelCase_ : Any = ConfigTester(self , config_class=SCREAMING_SNAKE_CASE_ , hidden_size=3_7 ) def lowercase_ ( self ) -> str: self.config_tester.run_common_tests() def lowercase_ ( self ) -> int: lowerCAmelCase_ : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_ ) def lowercase_ ( self ) -> Union[str, Any]: lowerCAmelCase_ : str = self.model_tester.prepare_config_and_inputs_for_decoder() self.model_tester.create_and_check_model_as_decoder(*SCREAMING_SNAKE_CASE_ ) def lowercase_ ( self ) -> Optional[int]: lowerCAmelCase_ : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*SCREAMING_SNAKE_CASE_ ) def lowercase_ ( self ) -> List[str]: lowerCAmelCase_ : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*SCREAMING_SNAKE_CASE_ ) @slow def lowercase_ ( self ) -> List[str]: for model_name in TF_ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowerCAmelCase_ : List[Any] = TFEsmModel.from_pretrained(SCREAMING_SNAKE_CASE_ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE_ ) @unittest.skip('''Protein models do not support embedding resizing.''' ) def lowercase_ ( self ) -> Tuple: pass @unittest.skip('''Protein models do not support embedding resizing.''' ) def lowercase_ ( self ) -> Any: pass def lowercase_ ( self ) -> Any: lowerCAmelCase_ , lowerCAmelCase_ : Tuple = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCAmelCase_ : Any = model_class(SCREAMING_SNAKE_CASE_ ) assert isinstance(model.get_input_embeddings() , tf.keras.layers.Layer ) if model_class is TFEsmForMaskedLM: # Output embedding test differs from the main test because they're a matrix, not a layer lowerCAmelCase_ : Tuple = model.get_bias() assert isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) for k, v in name.items(): assert isinstance(SCREAMING_SNAKE_CASE_ , tf.Variable ) else: lowerCAmelCase_ : Any = model.get_output_embeddings() assert x is None lowerCAmelCase_ : Any = model.get_bias() assert name is None @require_tf class snake_case__( unittest.TestCase ): '''simple docstring''' @slow def lowercase_ ( self ) -> Optional[int]: lowerCAmelCase_ : Optional[Any] = TFEsmForMaskedLM.from_pretrained('''facebook/esm2_t6_8M_UR50D''' ) lowerCAmelCase_ : List[Any] = tf.constant([[0, 1, 2, 3, 4, 5]] ) lowerCAmelCase_ : List[Any] = model(SCREAMING_SNAKE_CASE_ )[0] lowerCAmelCase_ : Dict = [1, 6, 3_3] self.assertEqual(list(output.numpy().shape ) , SCREAMING_SNAKE_CASE_ ) # compare the actual values for a slice. lowerCAmelCase_ : List[str] = tf.constant( [ [ [8.92_15_18, -10.58_98_14, -6.4_67_13_07], [-6.3_96_71_56, -13.91_13_77, -1.1_21_19_15], [-7.78_12_47, -13.95_15_57, -3.74_05_92], ] ] ) self.assertTrue(numpy.allclose(output[:, :3, :3].numpy() , expected_slice.numpy() , atol=1e-2 ) ) @slow def lowercase_ ( self ) -> Union[str, Any]: lowerCAmelCase_ : str = TFEsmModel.from_pretrained('''facebook/esm2_t6_8M_UR50D''' ) lowerCAmelCase_ : Dict = tf.constant([[0, 6, 4, 1_3, 5, 4, 1_6, 1_2, 1_1, 7, 2]] ) lowerCAmelCase_ : Dict = model(SCREAMING_SNAKE_CASE_ )[0] # compare the actual values for a slice. lowerCAmelCase_ : Dict = tf.constant( [ [ [0.14_44_30_92, 0.54_12_53_27, 0.3_24_77_39], [0.30_34_04_84, 0.00_52_66_76, 0.31_07_77_22], [0.32_27_80_43, -0.24_98_70_96, 0.3_41_46_28], ] ] ) self.assertTrue(numpy.allclose(output[:, :3, :3].numpy() , expected_slice.numpy() , atol=1e-4 ) )
262
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) __a = { 'configuration_rembert': ['REMBERT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'RemBertConfig', 'RemBertOnnxConfig'] } try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a = ['RemBertTokenizer'] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a = ['RemBertTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a = [ 'REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST', 'RemBertForCausalLM', 'RemBertForMaskedLM', 'RemBertForMultipleChoice', 'RemBertForQuestionAnswering', 'RemBertForSequenceClassification', 'RemBertForTokenClassification', 'RemBertLayer', 'RemBertModel', 'RemBertPreTrainedModel', 'load_tf_weights_in_rembert', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a = [ 'TF_REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFRemBertForCausalLM', 'TFRemBertForMaskedLM', 'TFRemBertForMultipleChoice', 'TFRemBertForQuestionAnswering', 'TFRemBertForSequenceClassification', 'TFRemBertForTokenClassification', 'TFRemBertLayer', 'TFRemBertModel', 'TFRemBertPreTrainedModel', ] if TYPE_CHECKING: from .configuration_rembert import REMBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, RemBertConfig, RemBertOnnxConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_rembert import RemBertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_rembert_fast import RemBertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_rembert import ( REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST, RemBertForCausalLM, RemBertForMaskedLM, RemBertForMultipleChoice, RemBertForQuestionAnswering, RemBertForSequenceClassification, RemBertForTokenClassification, RemBertLayer, RemBertModel, RemBertPreTrainedModel, load_tf_weights_in_rembert, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_rembert import ( TF_REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST, TFRemBertForCausalLM, TFRemBertForMaskedLM, TFRemBertForMultipleChoice, TFRemBertForQuestionAnswering, TFRemBertForSequenceClassification, TFRemBertForTokenClassification, TFRemBertLayer, TFRemBertModel, TFRemBertPreTrainedModel, ) else: import sys __a = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
30
0
"""simple docstring""" import random import unittest import torch from diffusers import IFInpaintingSuperResolutionPipeline from diffusers.utils import floats_tensor from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import skip_mps, torch_device from ..pipeline_params import ( TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS, ) from ..test_pipelines_common import PipelineTesterMixin from . import IFPipelineTesterMixin @skip_mps class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : str = IFInpaintingSuperResolutionPipeline __UpperCAmelCase : Optional[int] = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {'width', 'height'} __UpperCAmelCase : int = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS.union({'original_image'} ) __UpperCAmelCase : Optional[Any] = PipelineTesterMixin.required_optional_params - {'latents'} def __UpperCAmelCase ( self ): return self._get_superresolution_dummy_components() def __UpperCAmelCase ( self , _a , _a=0 ): if str(SCREAMING_SNAKE_CASE_ ).startswith('''mps''' ): __a = torch.manual_seed(SCREAMING_SNAKE_CASE_ ) else: __a = torch.Generator(device=SCREAMING_SNAKE_CASE_ ).manual_seed(SCREAMING_SNAKE_CASE_ ) __a = floats_tensor((1, 3, 16, 16) , rng=random.Random(SCREAMING_SNAKE_CASE_ ) ).to(SCREAMING_SNAKE_CASE_ ) __a = floats_tensor((1, 3, 32, 32) , rng=random.Random(SCREAMING_SNAKE_CASE_ ) ).to(SCREAMING_SNAKE_CASE_ ) __a = floats_tensor((1, 3, 32, 32) , rng=random.Random(SCREAMING_SNAKE_CASE_ ) ).to(SCREAMING_SNAKE_CASE_ ) __a = { '''prompt''': '''A painting of a squirrel eating a burger''', '''image''': image, '''original_image''': original_image, '''mask_image''': mask_image, '''generator''': generator, '''num_inference_steps''': 2, '''output_type''': '''numpy''', } return inputs @unittest.skipIf( torch_device != '''cuda''' or not is_xformers_available() , reason='''XFormers attention is only available with CUDA and `xformers` installed''' , ) def __UpperCAmelCase ( self ): self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3 ) def __UpperCAmelCase ( self ): self._test_save_load_optional_components() @unittest.skipIf(torch_device != '''cuda''' , reason='''float16 requires CUDA''' ) def __UpperCAmelCase ( self ): # Due to non-determinism in save load of the hf-internal-testing/tiny-random-t5 text encoder super().test_save_load_floataa(expected_max_diff=1E-1 ) def __UpperCAmelCase ( self ): self._test_attention_slicing_forward_pass(expected_max_diff=1E-2 ) def __UpperCAmelCase ( self ): self._test_save_load_local() def __UpperCAmelCase ( self ): self._test_inference_batch_single_identical( expected_max_diff=1E-2 , )
45
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) __a = {'configuration_deit': ['DEIT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'DeiTConfig', 'DeiTOnnxConfig']} try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a = ['DeiTFeatureExtractor'] __a = ['DeiTImageProcessor'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a = [ 'DEIT_PRETRAINED_MODEL_ARCHIVE_LIST', 'DeiTForImageClassification', 'DeiTForImageClassificationWithTeacher', 'DeiTForMaskedImageModeling', 'DeiTModel', 'DeiTPreTrainedModel', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a = [ 'TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFDeiTForImageClassification', 'TFDeiTForImageClassificationWithTeacher', 'TFDeiTForMaskedImageModeling', 'TFDeiTModel', 'TFDeiTPreTrainedModel', ] if TYPE_CHECKING: from .configuration_deit import DEIT_PRETRAINED_CONFIG_ARCHIVE_MAP, DeiTConfig, DeiTOnnxConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_deit import DeiTFeatureExtractor from .image_processing_deit import DeiTImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_deit import ( DEIT_PRETRAINED_MODEL_ARCHIVE_LIST, DeiTForImageClassification, DeiTForImageClassificationWithTeacher, DeiTForMaskedImageModeling, DeiTModel, DeiTPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_deit import ( TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST, TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher, TFDeiTForMaskedImageModeling, TFDeiTModel, TFDeiTPreTrainedModel, ) else: import sys __a = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
30
0
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 A__ = logging.get_logger(__name__) class __lowerCAmelCase : __lowerCamelCase = 42 __lowerCamelCase = None @staticmethod def snake_case ( ): """simple docstring""" raise NotImplementedError def snake_case ( self , _snake_case , _snake_case , _snake_case , **_snake_case ): """simple docstring""" raise NotImplementedError def snake_case ( self , _snake_case ): """simple docstring""" raise NotImplementedError def snake_case ( self ): """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 ): """simple docstring""" return F'`pip install {cls.pip_package or cls.name}`' class __lowerCAmelCase ( lowerCamelCase__ ): __lowerCamelCase = 'optuna' @staticmethod def snake_case ( ): """simple docstring""" return is_optuna_available() def snake_case ( self , _snake_case , _snake_case , _snake_case , **_snake_case ): """simple docstring""" return run_hp_search_optuna(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) def snake_case ( self , _snake_case ): """simple docstring""" return default_hp_space_optuna(SCREAMING_SNAKE_CASE_ ) class __lowerCAmelCase ( lowerCamelCase__ ): __lowerCamelCase = 'ray' __lowerCamelCase = '\'ray[tune]\'' @staticmethod def snake_case ( ): """simple docstring""" return is_ray_available() def snake_case ( self , _snake_case , _snake_case , _snake_case , **_snake_case ): """simple docstring""" return run_hp_search_ray(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) def snake_case ( self , _snake_case ): """simple docstring""" return default_hp_space_ray(SCREAMING_SNAKE_CASE_ ) class __lowerCAmelCase ( lowerCamelCase__ ): __lowerCamelCase = 'sigopt' @staticmethod def snake_case ( ): """simple docstring""" return is_sigopt_available() def snake_case ( self , _snake_case , _snake_case , _snake_case , **_snake_case ): """simple docstring""" return run_hp_search_sigopt(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) def snake_case ( self , _snake_case ): """simple docstring""" return default_hp_space_sigopt(SCREAMING_SNAKE_CASE_ ) class __lowerCAmelCase ( lowerCamelCase__ ): __lowerCamelCase = 'wandb' @staticmethod def snake_case ( ): """simple docstring""" return is_wandb_available() def snake_case ( self , _snake_case , _snake_case , _snake_case , **_snake_case ): """simple docstring""" return run_hp_search_wandb(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) def snake_case ( self , _snake_case ): """simple docstring""" return default_hp_space_wandb(SCREAMING_SNAKE_CASE_ ) A__ = { HPSearchBackend(backend.name): backend for backend in [OptunaBackend, RayTuneBackend, SigOptBackend, WandbBackend] } def _UpperCAmelCase ( ): """simple docstring""" _lowerCAmelCase = [backend for backend in ALL_HYPERPARAMETER_SEARCH_BACKENDS.values() if backend.is_available()] if len(snake_case__ ) > 0: _lowerCAmelCase = available_backends[0].name if len(snake_case__ ) > 1: logger.info( F'{len(snake_case__ )} 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() ) )
82
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_speech_available, is_torch_available, ) __a = { 'configuration_trocr': ['TROCR_PRETRAINED_CONFIG_ARCHIVE_MAP', 'TrOCRConfig'], 'processing_trocr': ['TrOCRProcessor'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a = [ 'TROCR_PRETRAINED_MODEL_ARCHIVE_LIST', 'TrOCRForCausalLM', 'TrOCRPreTrainedModel', ] if TYPE_CHECKING: from .configuration_trocr import TROCR_PRETRAINED_CONFIG_ARCHIVE_MAP, TrOCRConfig from .processing_trocr import TrOCRProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_trocr import TROCR_PRETRAINED_MODEL_ARCHIVE_LIST, TrOCRForCausalLM, TrOCRPreTrainedModel else: import sys __a = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
30
0
import copy import inspect import unittest import numpy as np from huggingface_hub import hf_hub_download from transformers import VideoMAEConfig from transformers.models.auto import get_values from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ( MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING, VideoMAEForPreTraining, VideoMAEForVideoClassification, VideoMAEModel, ) from transformers.models.videomae.modeling_videomae import VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from transformers import VideoMAEImageProcessor class UpperCAmelCase__ : """simple docstring""" def __init__( self , A_ , A_=13 , A_=10 , A_=3 , A_=2 , A_=2 , A_=2 , A_=True , A_=True , A_=32 , A_=5 , A_=4 , A_=37 , A_="gelu" , A_=0.1 , A_=0.1 , A_=10 , A_=0.02 , A_=0.9 , A_=None , ) -> Any: __UpperCamelCase =parent __UpperCamelCase =batch_size __UpperCamelCase =image_size __UpperCamelCase =num_channels __UpperCamelCase =patch_size __UpperCamelCase =tubelet_size __UpperCamelCase =num_frames __UpperCamelCase =is_training __UpperCamelCase =use_labels __UpperCamelCase =hidden_size __UpperCamelCase =num_hidden_layers __UpperCamelCase =num_attention_heads __UpperCamelCase =intermediate_size __UpperCamelCase =hidden_act __UpperCamelCase =hidden_dropout_prob __UpperCamelCase =attention_probs_dropout_prob __UpperCamelCase =type_sequence_label_size __UpperCamelCase =initializer_range __UpperCamelCase =mask_ratio __UpperCamelCase =scope # in VideoMAE, the number of tokens equals num_frames/tubelet_size * num_patches per frame __UpperCamelCase =(image_size // patch_size) ** 2 __UpperCamelCase =(num_frames // tubelet_size) * self.num_patches_per_frame # use this variable to define bool_masked_pos __UpperCamelCase =int(mask_ratio * self.seq_length ) def _a ( self ) -> int: __UpperCamelCase =floats_tensor( [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size] ) __UpperCamelCase =None if self.use_labels: __UpperCamelCase =ids_tensor([self.batch_size] , self.type_sequence_label_size ) __UpperCamelCase =self.get_config() return config, pixel_values, labels def _a ( self ) -> Tuple: return VideoMAEConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , num_frames=self.num_frames , tubelet_size=self.tubelet_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 , is_decoder=SCREAMING_SNAKE_CASE_ , initializer_range=self.initializer_range , ) def _a ( self , A_ , A_ , A_ ) -> str: __UpperCamelCase =VideoMAEModel(config=SCREAMING_SNAKE_CASE_ ) model.to(SCREAMING_SNAKE_CASE_ ) model.eval() __UpperCamelCase =model(SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _a ( self , A_ , A_ , A_ ) -> Tuple: __UpperCamelCase =VideoMAEForPreTraining(SCREAMING_SNAKE_CASE_ ) model.to(SCREAMING_SNAKE_CASE_ ) model.eval() # important: each video needs to have the same number of masked patches # hence we define a single mask, which we then repeat for each example in the batch __UpperCamelCase =torch.ones((self.num_masks,) ) __UpperCamelCase =torch.cat([mask, torch.zeros(self.seq_length - mask.size(0 ) )] ) __UpperCamelCase =mask.expand(self.batch_size , -1 ).bool() __UpperCamelCase =model(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # model only returns predictions for masked patches __UpperCamelCase =mask.sum().item() __UpperCamelCase =3 * self.tubelet_size * self.patch_size**2 self.parent.assertEqual(result.logits.shape , (self.batch_size, num_masked_patches, decoder_num_labels) ) def _a ( self ) -> Any: __UpperCamelCase =self.prepare_config_and_inputs() __UpperCamelCase , __UpperCamelCase , __UpperCamelCase =config_and_inputs __UpperCamelCase ={'pixel_values': pixel_values} return config, inputs_dict @require_torch class UpperCAmelCase__ ( A_ , A_ , unittest.TestCase ): """simple docstring""" UpperCAmelCase__ : Optional[int] = ( (VideoMAEModel, VideoMAEForPreTraining, VideoMAEForVideoClassification) if is_torch_available() else () ) UpperCAmelCase__ : Optional[int] = ( {'feature-extraction': VideoMAEModel, 'video-classification': VideoMAEForVideoClassification} if is_torch_available() else {} ) UpperCAmelCase__ : Optional[int] = False UpperCAmelCase__ : int = False UpperCAmelCase__ : str = False UpperCAmelCase__ : int = False def _a ( self ) -> Optional[int]: __UpperCamelCase =VideoMAEModelTester(self ) __UpperCamelCase =ConfigTester(self , config_class=SCREAMING_SNAKE_CASE_ , has_text_modality=SCREAMING_SNAKE_CASE_ , hidden_size=37 ) def _a ( self , A_ , A_ , A_=False ) -> str: __UpperCamelCase =copy.deepcopy(SCREAMING_SNAKE_CASE_ ) if model_class == VideoMAEForPreTraining: # important: each video needs to have the same number of masked patches # hence we define a single mask, which we then repeat for each example in the batch __UpperCamelCase =torch.ones((self.model_tester.num_masks,) ) __UpperCamelCase =torch.cat([mask, torch.zeros(self.model_tester.seq_length - mask.size(0 ) )] ) __UpperCamelCase =mask.expand(self.model_tester.batch_size , -1 ).bool() __UpperCamelCase =bool_masked_pos.to(SCREAMING_SNAKE_CASE_ ) if return_labels: if model_class in [ *get_values(SCREAMING_SNAKE_CASE_ ), ]: __UpperCamelCase =torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=SCREAMING_SNAKE_CASE_ ) return inputs_dict def _a ( self ) -> Optional[int]: self.config_tester.run_common_tests() @unittest.skip(reason='VideoMAE does not use inputs_embeds' ) def _a ( self ) -> Union[str, Any]: pass def _a ( self ) -> List[str]: __UpperCamelCase , __UpperCamelCase =self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __UpperCamelCase =model_class(SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) __UpperCamelCase =model.get_output_embeddings() self.assertTrue(x is None or isinstance(SCREAMING_SNAKE_CASE_ , nn.Linear ) ) def _a ( self ) -> Tuple: __UpperCamelCase , __UpperCamelCase =self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __UpperCamelCase =model_class(SCREAMING_SNAKE_CASE_ ) __UpperCamelCase =inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic __UpperCamelCase =[*signature.parameters.keys()] __UpperCamelCase =['pixel_values'] self.assertListEqual(arg_names[:1] , SCREAMING_SNAKE_CASE_ ) def _a ( self ) -> List[Any]: __UpperCamelCase =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_ ) def _a ( self ) -> Dict: __UpperCamelCase =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_pretraining(*SCREAMING_SNAKE_CASE_ ) @slow def _a ( self ) -> Optional[Any]: for model_name in VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __UpperCamelCase =VideoMAEModel.from_pretrained(SCREAMING_SNAKE_CASE_ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE_ ) def _a ( self ) -> List[Any]: if not self.has_attentions: pass else: __UpperCamelCase , __UpperCamelCase =self.model_tester.prepare_config_and_inputs_for_common() __UpperCamelCase =True for model_class in self.all_model_classes: __UpperCamelCase =self.model_tester.seq_length - self.model_tester.num_masks __UpperCamelCase =( num_visible_patches if model_class == VideoMAEForPreTraining else self.model_tester.seq_length ) __UpperCamelCase =True __UpperCamelCase =False __UpperCamelCase =True __UpperCamelCase =model_class(SCREAMING_SNAKE_CASE_ ) model.to(SCREAMING_SNAKE_CASE_ ) model.eval() with torch.no_grad(): __UpperCamelCase =model(**self._prepare_for_class(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) __UpperCamelCase =outputs.attentions self.assertEqual(len(SCREAMING_SNAKE_CASE_ ) , self.model_tester.num_hidden_layers ) # check that output_attentions also work using config del inputs_dict["output_attentions"] __UpperCamelCase =True __UpperCamelCase =model_class(SCREAMING_SNAKE_CASE_ ) model.to(SCREAMING_SNAKE_CASE_ ) model.eval() with torch.no_grad(): __UpperCamelCase =model(**self._prepare_for_class(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) __UpperCamelCase =outputs.attentions self.assertEqual(len(SCREAMING_SNAKE_CASE_ ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, seq_len, seq_len] , ) __UpperCamelCase =len(SCREAMING_SNAKE_CASE_ ) # Check attention is always last and order is fine __UpperCamelCase =True __UpperCamelCase =True __UpperCamelCase =model_class(SCREAMING_SNAKE_CASE_ ) model.to(SCREAMING_SNAKE_CASE_ ) model.eval() with torch.no_grad(): __UpperCamelCase =model(**self._prepare_for_class(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) self.assertEqual(out_len + 1 , len(SCREAMING_SNAKE_CASE_ ) ) __UpperCamelCase =outputs.attentions self.assertEqual(len(SCREAMING_SNAKE_CASE_ ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(self_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, seq_len, seq_len] , ) def _a ( self ) -> List[str]: def check_hidden_states_output(A_ , A_ , A_ ): __UpperCamelCase =model_class(SCREAMING_SNAKE_CASE_ ) model.to(SCREAMING_SNAKE_CASE_ ) model.eval() with torch.no_grad(): __UpperCamelCase =model(**self._prepare_for_class(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) __UpperCamelCase =outputs.hidden_states __UpperCamelCase =self.model_tester.num_hidden_layers + 1 self.assertEqual(len(SCREAMING_SNAKE_CASE_ ) , SCREAMING_SNAKE_CASE_ ) __UpperCamelCase =self.model_tester.seq_length - self.model_tester.num_masks __UpperCamelCase =num_visible_patches if model_class == VideoMAEForPreTraining else self.model_tester.seq_length self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [seq_length, self.model_tester.hidden_size] , ) __UpperCamelCase , __UpperCamelCase =self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __UpperCamelCase =True check_hidden_states_output(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] __UpperCamelCase =True check_hidden_states_output(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) @unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.' ) def _a ( self ) -> List[Any]: pass def _UpperCAmelCase ( ): __UpperCamelCase =hf_hub_download( repo_id='hf-internal-testing/spaghetti-video' , filename='eating_spaghetti.npy' , repo_type='dataset' ) __UpperCamelCase =np.load(snake_case__ ) return list(snake_case__ ) @require_torch @require_vision class UpperCAmelCase__ ( unittest.TestCase ): """simple docstring""" @cached_property def _a ( self ) -> Optional[int]: # logits were tested with a different mean and std, so we use the same here return ( VideoMAEImageProcessor(image_mean=[0.5, 0.5, 0.5] , image_std=[0.5, 0.5, 0.5] ) if is_vision_available() else None ) @slow def _a ( self ) -> int: __UpperCamelCase =VideoMAEForVideoClassification.from_pretrained('MCG-NJU/videomae-base-finetuned-kinetics' ).to( SCREAMING_SNAKE_CASE_ ) __UpperCamelCase =self.default_image_processor __UpperCamelCase =prepare_video() __UpperCamelCase =image_processor(SCREAMING_SNAKE_CASE_ , return_tensors='pt' ).to(SCREAMING_SNAKE_CASE_ ) # forward pass with torch.no_grad(): __UpperCamelCase =model(**SCREAMING_SNAKE_CASE_ ) # verify the logits __UpperCamelCase =torch.Size((1, 400) ) self.assertEqual(outputs.logits.shape , SCREAMING_SNAKE_CASE_ ) __UpperCamelCase =torch.tensor([0.3669, -0.0688, -0.2421] ).to(SCREAMING_SNAKE_CASE_ ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , SCREAMING_SNAKE_CASE_ , atol=1E-4 ) ) @slow def _a ( self ) -> Dict: __UpperCamelCase =VideoMAEForPreTraining.from_pretrained('MCG-NJU/videomae-base-short' ).to(SCREAMING_SNAKE_CASE_ ) __UpperCamelCase =self.default_image_processor __UpperCamelCase =prepare_video() __UpperCamelCase =image_processor(SCREAMING_SNAKE_CASE_ , return_tensors='pt' ).to(SCREAMING_SNAKE_CASE_ ) # add boolean mask, indicating which patches to mask __UpperCamelCase =hf_hub_download(repo_id='hf-internal-testing/bool-masked-pos' , filename='bool_masked_pos.pt' ) __UpperCamelCase =torch.load(SCREAMING_SNAKE_CASE_ ) # forward pass with torch.no_grad(): __UpperCamelCase =model(**SCREAMING_SNAKE_CASE_ ) # verify the logits __UpperCamelCase =torch.Size([1, 1408, 1536] ) __UpperCamelCase =torch.tensor( [[0.7994, 0.9612, 0.8508], [0.7401, 0.8958, 0.8302], [0.5862, 0.7468, 0.7325]] , device=SCREAMING_SNAKE_CASE_ ) self.assertEqual(outputs.logits.shape , SCREAMING_SNAKE_CASE_ ) self.assertTrue(torch.allclose(outputs.logits[0, :3, :3] , SCREAMING_SNAKE_CASE_ , atol=1E-4 ) ) # verify the loss (`config.norm_pix_loss` = `True`) __UpperCamelCase =torch.tensor([0.5142] , device=SCREAMING_SNAKE_CASE_ ) self.assertTrue(torch.allclose(outputs.loss , SCREAMING_SNAKE_CASE_ , atol=1E-4 ) ) # verify the loss (`config.norm_pix_loss` = `False`) __UpperCamelCase =VideoMAEForPreTraining.from_pretrained('MCG-NJU/videomae-base-short' , norm_pix_loss=SCREAMING_SNAKE_CASE_ ).to( SCREAMING_SNAKE_CASE_ ) with torch.no_grad(): __UpperCamelCase =model(**SCREAMING_SNAKE_CASE_ ) __UpperCamelCase =torch.tensor(torch.tensor([0.6469] ) , device=SCREAMING_SNAKE_CASE_ ) self.assertTrue(torch.allclose(outputs.loss , SCREAMING_SNAKE_CASE_ , atol=1E-4 ) )
62
from typing import Optional, Tuple, Union import tensorflow as tf from ...activations_tf import ACTaFN from ...file_utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward from ...modeling_tf_outputs import ( TFBaseModelOutputWithNoAttention, TFBaseModelOutputWithPoolingAndNoAttention, TFSequenceClassifierOutput, ) from ...modeling_tf_utils import TFPreTrainedModel, TFSequenceClassificationLoss, keras_serializable, unpack_inputs from ...tf_utils import shape_list from ...utils import logging from .configuration_regnet import RegNetConfig __a = logging.get_logger(__name__) # General docstring __a = 'RegNetConfig' # Base docstring __a = 'facebook/regnet-y-040' __a = [1, 1_0_8_8, 7, 7] # Image classification docstring __a = 'facebook/regnet-y-040' __a = 'tabby, tabby cat' __a = [ 'facebook/regnet-y-040', # See all regnet models at https://huggingface.co/models?filter=regnet ] class lowercase__( tf.keras.layers.Layer ): """simple docstring""" def __init__( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int = 3 , SCREAMING_SNAKE_CASE_ : int = 1 , SCREAMING_SNAKE_CASE_ : int = 1 , SCREAMING_SNAKE_CASE_ : Optional[str] = "relu" , **SCREAMING_SNAKE_CASE_ : Any , ) -> List[str]: super().__init__(**SCREAMING_SNAKE_CASE_ ) # The padding and conv has been verified in # https://colab.research.google.com/gist/sayakpaul/854bc10eeaf21c9ee2119e0b9f3841a7/scratchpad.ipynb lowercase_ = tf.keras.layers.ZeroPaddingaD(padding=kernel_size // 2 ) lowercase_ = tf.keras.layers.ConvaD( filters=SCREAMING_SNAKE_CASE_ , kernel_size=SCREAMING_SNAKE_CASE_ , strides=SCREAMING_SNAKE_CASE_ , padding='''VALID''' , groups=SCREAMING_SNAKE_CASE_ , use_bias=SCREAMING_SNAKE_CASE_ , name='''convolution''' , ) lowercase_ = tf.keras.layers.BatchNormalization(epsilon=1e-5 , momentum=0.9 , name='''normalization''' ) lowercase_ = ACTaFN[activation] if activation is not None else tf.identity def _lowercase ( self : Optional[Any] , SCREAMING_SNAKE_CASE_ : Optional[int] ) -> Any: lowercase_ = self.convolution(self.padding(SCREAMING_SNAKE_CASE_ ) ) lowercase_ = self.normalization(SCREAMING_SNAKE_CASE_ ) lowercase_ = self.activation(SCREAMING_SNAKE_CASE_ ) return hidden_state class lowercase__( tf.keras.layers.Layer ): """simple docstring""" def __init__( self : str , SCREAMING_SNAKE_CASE_ : RegNetConfig , **SCREAMING_SNAKE_CASE_ : str ) -> Any: super().__init__(**SCREAMING_SNAKE_CASE_ ) lowercase_ = config.num_channels lowercase_ = TFRegNetConvLayer( out_channels=config.embedding_size , kernel_size=3 , stride=2 , activation=config.hidden_act , name='''embedder''' , ) def _lowercase ( self : List[str] , SCREAMING_SNAKE_CASE_ : str ) -> Optional[int]: lowercase_ = shape_list(SCREAMING_SNAKE_CASE_ )[1] if tf.executing_eagerly() and num_channels != self.num_channels: raise ValueError( '''Make sure that the channel dimension of the pixel values match with the one set in the configuration.''' ) # When running on CPU, `tf.keras.layers.Conv2D` doesn't support `NCHW` format. # So change the input format from `NCHW` to `NHWC`. # shape = (batch_size, in_height, in_width, in_channels=num_channels) lowercase_ = tf.transpose(SCREAMING_SNAKE_CASE_ , perm=(0, 2, 3, 1) ) lowercase_ = self.embedder(SCREAMING_SNAKE_CASE_ ) return hidden_state class lowercase__( tf.keras.layers.Layer ): """simple docstring""" def __init__( self : int , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int = 2 , **SCREAMING_SNAKE_CASE_ : List[str] ) -> Union[str, Any]: super().__init__(**SCREAMING_SNAKE_CASE_ ) lowercase_ = tf.keras.layers.ConvaD( filters=SCREAMING_SNAKE_CASE_ , kernel_size=1 , strides=SCREAMING_SNAKE_CASE_ , use_bias=SCREAMING_SNAKE_CASE_ , name='''convolution''' ) lowercase_ = tf.keras.layers.BatchNormalization(epsilon=1e-5 , momentum=0.9 , name='''normalization''' ) def _lowercase ( self : List[Any] , SCREAMING_SNAKE_CASE_ : tf.Tensor , SCREAMING_SNAKE_CASE_ : bool = False ) -> tf.Tensor: return self.normalization(self.convolution(SCREAMING_SNAKE_CASE_ ) , training=SCREAMING_SNAKE_CASE_ ) class lowercase__( tf.keras.layers.Layer ): """simple docstring""" def __init__( self : Optional[int] , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int , **SCREAMING_SNAKE_CASE_ : int ) -> Optional[Any]: super().__init__(**SCREAMING_SNAKE_CASE_ ) lowercase_ = tf.keras.layers.GlobalAveragePoolingaD(keepdims=SCREAMING_SNAKE_CASE_ , name='''pooler''' ) lowercase_ = [ tf.keras.layers.ConvaD(filters=SCREAMING_SNAKE_CASE_ , kernel_size=1 , activation='''relu''' , name='''attention.0''' ), tf.keras.layers.ConvaD(filters=SCREAMING_SNAKE_CASE_ , kernel_size=1 , activation='''sigmoid''' , name='''attention.2''' ), ] def _lowercase ( self : str , SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> Dict: # [batch_size, h, w, num_channels] -> [batch_size, 1, 1, num_channels] lowercase_ = self.pooler(SCREAMING_SNAKE_CASE_ ) for layer_module in self.attention: lowercase_ = layer_module(SCREAMING_SNAKE_CASE_ ) lowercase_ = hidden_state * pooled return hidden_state class lowercase__( tf.keras.layers.Layer ): """simple docstring""" def __init__( self : str , SCREAMING_SNAKE_CASE_ : RegNetConfig , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int = 1 , **SCREAMING_SNAKE_CASE_ : Union[str, Any] ) -> List[str]: super().__init__(**SCREAMING_SNAKE_CASE_ ) lowercase_ = in_channels != out_channels or stride != 1 lowercase_ = max(1 , out_channels // config.groups_width ) lowercase_ = ( TFRegNetShortCut(SCREAMING_SNAKE_CASE_ , stride=SCREAMING_SNAKE_CASE_ , name='''shortcut''' ) if should_apply_shortcut else tf.keras.layers.Activation('''linear''' , name='''shortcut''' ) ) # `self.layers` instead of `self.layer` because that is a reserved argument. lowercase_ = [ TFRegNetConvLayer(SCREAMING_SNAKE_CASE_ , kernel_size=1 , activation=config.hidden_act , name='''layer.0''' ), TFRegNetConvLayer( SCREAMING_SNAKE_CASE_ , stride=SCREAMING_SNAKE_CASE_ , groups=SCREAMING_SNAKE_CASE_ , activation=config.hidden_act , name='''layer.1''' ), TFRegNetConvLayer(SCREAMING_SNAKE_CASE_ , kernel_size=1 , activation=SCREAMING_SNAKE_CASE_ , name='''layer.2''' ), ] lowercase_ = ACTaFN[config.hidden_act] def _lowercase ( self : List[str] , SCREAMING_SNAKE_CASE_ : Optional[int] ) -> Any: lowercase_ = hidden_state for layer_module in self.layers: lowercase_ = layer_module(SCREAMING_SNAKE_CASE_ ) lowercase_ = self.shortcut(SCREAMING_SNAKE_CASE_ ) hidden_state += residual lowercase_ = self.activation(SCREAMING_SNAKE_CASE_ ) return hidden_state class lowercase__( tf.keras.layers.Layer ): """simple docstring""" def __init__( self : Any , SCREAMING_SNAKE_CASE_ : RegNetConfig , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int = 1 , **SCREAMING_SNAKE_CASE_ : List[str] ) -> int: super().__init__(**SCREAMING_SNAKE_CASE_ ) lowercase_ = in_channels != out_channels or stride != 1 lowercase_ = max(1 , out_channels // config.groups_width ) lowercase_ = ( TFRegNetShortCut(SCREAMING_SNAKE_CASE_ , stride=SCREAMING_SNAKE_CASE_ , name='''shortcut''' ) if should_apply_shortcut else tf.keras.layers.Activation('''linear''' , name='''shortcut''' ) ) lowercase_ = [ TFRegNetConvLayer(SCREAMING_SNAKE_CASE_ , kernel_size=1 , activation=config.hidden_act , name='''layer.0''' ), TFRegNetConvLayer( SCREAMING_SNAKE_CASE_ , stride=SCREAMING_SNAKE_CASE_ , groups=SCREAMING_SNAKE_CASE_ , activation=config.hidden_act , name='''layer.1''' ), TFRegNetSELayer(SCREAMING_SNAKE_CASE_ , reduced_channels=int(round(in_channels / 4 ) ) , name='''layer.2''' ), TFRegNetConvLayer(SCREAMING_SNAKE_CASE_ , kernel_size=1 , activation=SCREAMING_SNAKE_CASE_ , name='''layer.3''' ), ] lowercase_ = ACTaFN[config.hidden_act] def _lowercase ( self : int , SCREAMING_SNAKE_CASE_ : Dict ) -> Optional[Any]: lowercase_ = hidden_state for layer_module in self.layers: lowercase_ = layer_module(SCREAMING_SNAKE_CASE_ ) lowercase_ = self.shortcut(SCREAMING_SNAKE_CASE_ ) hidden_state += residual lowercase_ = self.activation(SCREAMING_SNAKE_CASE_ ) return hidden_state class lowercase__( tf.keras.layers.Layer ): """simple docstring""" def __init__( self : str , SCREAMING_SNAKE_CASE_ : RegNetConfig , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int = 2 , SCREAMING_SNAKE_CASE_ : int = 2 , **SCREAMING_SNAKE_CASE_ : List[Any] ) -> List[str]: super().__init__(**SCREAMING_SNAKE_CASE_ ) lowercase_ = TFRegNetXLayer if config.layer_type == '''x''' else TFRegNetYLayer lowercase_ = [ # downsampling is done in the first layer with stride of 2 layer(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , stride=SCREAMING_SNAKE_CASE_ , name='''layers.0''' ), *[layer(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , name=f'''layers.{i+1}''' ) for i in range(depth - 1 )], ] def _lowercase ( self : Tuple , SCREAMING_SNAKE_CASE_ : int ) -> int: for layer_module in self.layers: lowercase_ = layer_module(SCREAMING_SNAKE_CASE_ ) return hidden_state class lowercase__( tf.keras.layers.Layer ): """simple docstring""" def __init__( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : RegNetConfig , **SCREAMING_SNAKE_CASE_ : Dict ) -> Tuple: super().__init__(**SCREAMING_SNAKE_CASE_ ) lowercase_ = [] # based on `downsample_in_first_stage`, the first layer of the first stage may or may not downsample the input self.stages.append( TFRegNetStage( SCREAMING_SNAKE_CASE_ , config.embedding_size , config.hidden_sizes[0] , stride=2 if config.downsample_in_first_stage else 1 , depth=config.depths[0] , name='''stages.0''' , ) ) lowercase_ = zip(config.hidden_sizes , config.hidden_sizes[1:] ) for i, ((in_channels, out_channels), depth) in enumerate(zip(SCREAMING_SNAKE_CASE_ , config.depths[1:] ) ): self.stages.append(TFRegNetStage(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , depth=SCREAMING_SNAKE_CASE_ , name=f'''stages.{i+1}''' ) ) def _lowercase ( self : Dict , SCREAMING_SNAKE_CASE_ : tf.Tensor , SCREAMING_SNAKE_CASE_ : bool = False , SCREAMING_SNAKE_CASE_ : bool = True ) -> TFBaseModelOutputWithNoAttention: lowercase_ = () if output_hidden_states else None for stage_module in self.stages: if output_hidden_states: lowercase_ = hidden_states + (hidden_state,) lowercase_ = stage_module(SCREAMING_SNAKE_CASE_ ) if output_hidden_states: lowercase_ = hidden_states + (hidden_state,) if not return_dict: return tuple(v for v in [hidden_state, hidden_states] if v is not None ) return TFBaseModelOutputWithNoAttention(last_hidden_state=SCREAMING_SNAKE_CASE_ , hidden_states=SCREAMING_SNAKE_CASE_ ) @keras_serializable class lowercase__( tf.keras.layers.Layer ): """simple docstring""" a :str = RegNetConfig def __init__( self : str , SCREAMING_SNAKE_CASE_ : Dict , **SCREAMING_SNAKE_CASE_ : List[str] ) -> Any: super().__init__(**SCREAMING_SNAKE_CASE_ ) lowercase_ = config lowercase_ = TFRegNetEmbeddings(SCREAMING_SNAKE_CASE_ , name='''embedder''' ) lowercase_ = TFRegNetEncoder(SCREAMING_SNAKE_CASE_ , name='''encoder''' ) lowercase_ = tf.keras.layers.GlobalAveragePoolingaD(keepdims=SCREAMING_SNAKE_CASE_ , name='''pooler''' ) @unpack_inputs def _lowercase ( self : Dict , SCREAMING_SNAKE_CASE_ : tf.Tensor , SCREAMING_SNAKE_CASE_ : Optional[bool] = None , SCREAMING_SNAKE_CASE_ : Optional[bool] = None , SCREAMING_SNAKE_CASE_ : bool = False , ) -> TFBaseModelOutputWithPoolingAndNoAttention: lowercase_ = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) lowercase_ = return_dict if return_dict is not None else self.config.use_return_dict lowercase_ = self.embedder(SCREAMING_SNAKE_CASE_ , training=SCREAMING_SNAKE_CASE_ ) lowercase_ = self.encoder( SCREAMING_SNAKE_CASE_ , output_hidden_states=SCREAMING_SNAKE_CASE_ , return_dict=SCREAMING_SNAKE_CASE_ , training=SCREAMING_SNAKE_CASE_ ) lowercase_ = encoder_outputs[0] lowercase_ = self.pooler(SCREAMING_SNAKE_CASE_ ) # Change to NCHW output format have uniformity in the modules lowercase_ = tf.transpose(SCREAMING_SNAKE_CASE_ , perm=(0, 3, 1, 2) ) lowercase_ = tf.transpose(SCREAMING_SNAKE_CASE_ , perm=(0, 3, 1, 2) ) # Change the other hidden state outputs to NCHW as well if output_hidden_states: lowercase_ = tuple([tf.transpose(SCREAMING_SNAKE_CASE_ , perm=(0, 3, 1, 2) ) for h in encoder_outputs[1]] ) if not return_dict: return (last_hidden_state, pooled_output) + encoder_outputs[1:] return TFBaseModelOutputWithPoolingAndNoAttention( last_hidden_state=SCREAMING_SNAKE_CASE_ , pooler_output=SCREAMING_SNAKE_CASE_ , hidden_states=hidden_states if output_hidden_states else encoder_outputs.hidden_states , ) class lowercase__( UpperCAmelCase ): """simple docstring""" a :Tuple = RegNetConfig a :Any = 'regnet' a :List[str] = 'pixel_values' @property def _lowercase ( self : List[str] ) -> str: return {"pixel_values": tf.TensorSpec(shape=(None, self.config.num_channels, 2_2_4, 2_2_4) , dtype=tf.floataa )} __a = r'\n Parameters:\n This model is a Tensorflow\n [tf.keras.layers.Layer](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Layer) sub-class. Use it as a\n regular Tensorflow Module and refer to the Tensorflow documentation for all matter related to general usage and\n behavior.\n config ([`RegNetConfig`]): Model configuration class with all the parameters of the model.\n Initializing with a config file does not load the weights associated with the model, only the\n configuration. Check out the [`~TFPreTrainedModel.from_pretrained`] method to load the model weights.\n' __a = r'\n Args:\n pixel_values (`tf.Tensor` of shape `(batch_size, num_channels, height, width)`):\n Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n [`ConveNextImageProcessor.__call__`] for details.\n output_hidden_states (`bool`, *optional*):\n Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for\n more detail.\n return_dict (`bool`, *optional*):\n Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n' @add_start_docstrings( 'The bare RegNet model outputting raw features without any specific head on top.' , UpperCAmelCase , ) class lowercase__( UpperCAmelCase ): """simple docstring""" def __init__( self : Optional[int] , SCREAMING_SNAKE_CASE_ : RegNetConfig , *SCREAMING_SNAKE_CASE_ : Any , **SCREAMING_SNAKE_CASE_ : str ) -> List[str]: super().__init__(SCREAMING_SNAKE_CASE_ , *SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) lowercase_ = TFRegNetMainLayer(SCREAMING_SNAKE_CASE_ , name='''regnet''' ) @unpack_inputs @add_start_docstrings_to_model_forward(SCREAMING_SNAKE_CASE_ ) @add_code_sample_docstrings( checkpoint=_CHECKPOINT_FOR_DOC , output_type=SCREAMING_SNAKE_CASE_ , config_class=_CONFIG_FOR_DOC , modality='''vision''' , expected_output=_EXPECTED_OUTPUT_SHAPE , ) def _lowercase ( self : List[str] , SCREAMING_SNAKE_CASE_ : tf.Tensor , SCREAMING_SNAKE_CASE_ : Optional[bool] = None , SCREAMING_SNAKE_CASE_ : Optional[bool] = None , SCREAMING_SNAKE_CASE_ : Optional[Any]=False , ) -> Union[TFBaseModelOutputWithPoolingAndNoAttention, Tuple[tf.Tensor]]: lowercase_ = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) lowercase_ = return_dict if return_dict is not None else self.config.use_return_dict lowercase_ = self.regnet( pixel_values=SCREAMING_SNAKE_CASE_ , output_hidden_states=SCREAMING_SNAKE_CASE_ , return_dict=SCREAMING_SNAKE_CASE_ , training=SCREAMING_SNAKE_CASE_ , ) if not return_dict: return (outputs[0],) + outputs[1:] return TFBaseModelOutputWithPoolingAndNoAttention( last_hidden_state=outputs.last_hidden_state , pooler_output=outputs.pooler_output , hidden_states=outputs.hidden_states , ) @add_start_docstrings( '\n RegNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n ImageNet.\n ' , UpperCAmelCase , ) class lowercase__( UpperCAmelCase , UpperCAmelCase ): """simple docstring""" def __init__( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : RegNetConfig , *SCREAMING_SNAKE_CASE_ : str , **SCREAMING_SNAKE_CASE_ : int ) -> Union[str, Any]: super().__init__(SCREAMING_SNAKE_CASE_ , *SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) lowercase_ = config.num_labels lowercase_ = TFRegNetMainLayer(SCREAMING_SNAKE_CASE_ , name='''regnet''' ) # classification head lowercase_ = [ tf.keras.layers.Flatten(), tf.keras.layers.Dense(config.num_labels , name='''classifier.1''' ) if config.num_labels > 0 else tf.identity, ] @unpack_inputs @add_start_docstrings_to_model_forward(SCREAMING_SNAKE_CASE_ ) @add_code_sample_docstrings( checkpoint=_IMAGE_CLASS_CHECKPOINT , output_type=SCREAMING_SNAKE_CASE_ , config_class=_CONFIG_FOR_DOC , expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT , ) def _lowercase ( self : List[Any] , SCREAMING_SNAKE_CASE_ : tf.Tensor = None , SCREAMING_SNAKE_CASE_ : tf.Tensor = None , SCREAMING_SNAKE_CASE_ : bool = None , SCREAMING_SNAKE_CASE_ : bool = None , SCREAMING_SNAKE_CASE_ : Dict=False , ) -> Union[TFSequenceClassifierOutput, Tuple[tf.Tensor]]: lowercase_ = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) lowercase_ = return_dict if return_dict is not None else self.config.use_return_dict lowercase_ = self.regnet( SCREAMING_SNAKE_CASE_ , output_hidden_states=SCREAMING_SNAKE_CASE_ , return_dict=SCREAMING_SNAKE_CASE_ , training=SCREAMING_SNAKE_CASE_ ) lowercase_ = outputs.pooler_output if return_dict else outputs[1] lowercase_ = self.classifier[0](SCREAMING_SNAKE_CASE_ ) lowercase_ = self.classifier[1](SCREAMING_SNAKE_CASE_ ) lowercase_ = None if labels is None else self.hf_compute_loss(labels=SCREAMING_SNAKE_CASE_ , logits=SCREAMING_SNAKE_CASE_ ) if not return_dict: lowercase_ = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return TFSequenceClassifierOutput(loss=SCREAMING_SNAKE_CASE_ , logits=SCREAMING_SNAKE_CASE_ , hidden_states=outputs.hidden_states )
30
0
def A_ ( _lowerCAmelCase ) -> Union[str, Any]: try: UpperCamelCase : Any = float(snake_case__ ) except ValueError: raise ValueError("Please enter a valid number" ) UpperCamelCase : str = decimal - int(snake_case__ ) if fractional_part == 0: return int(snake_case__ ), 1 else: UpperCamelCase : Tuple = len(str(snake_case__ ).split("." )[1] ) UpperCamelCase : int = int(decimal * (10**number_of_frac_digits) ) UpperCamelCase : List[Any] = 10**number_of_frac_digits UpperCamelCase , UpperCamelCase : Union[str, Any] = denominator, numerator while True: UpperCamelCase : Optional[int] = dividend % divisor if remainder == 0: break UpperCamelCase , UpperCamelCase : str = divisor, remainder UpperCamelCase , UpperCamelCase : Optional[int] = numerator / divisor, denominator / divisor return int(snake_case__ ), int(snake_case__ ) if __name__ == "__main__": print(f"""{decimal_to_fraction(2) = }""") print(f"""{decimal_to_fraction(8_9.0) = }""") print(f"""{decimal_to_fraction('67') = }""") print(f"""{decimal_to_fraction('45.0') = }""") print(f"""{decimal_to_fraction(1.5) = }""") print(f"""{decimal_to_fraction('6.25') = }""") print(f"""{decimal_to_fraction('78td') = }""")
52
import importlib.metadata import warnings from copy import deepcopy from packaging import version from ..utils import logging from .import_utils import is_accelerate_available, is_bitsandbytes_available if is_bitsandbytes_available(): import bitsandbytes as bnb import torch import torch.nn as nn from ..pytorch_utils import ConvaD if is_accelerate_available(): from accelerate import init_empty_weights from accelerate.utils import find_tied_parameters __a = logging.get_logger(__name__) def a ( snake_case__: Optional[int] , snake_case__: Dict , snake_case__: int , snake_case__: List[str]=None , snake_case__: List[Any]=None ): '''simple docstring''' # Recurse if needed if "." in tensor_name: lowercase_ = tensor_name.split('''.''' ) for split in splits[:-1]: lowercase_ = getattr(snake_case__ , snake_case__ ) if new_module is None: raise ValueError(F'''{module} has no attribute {split}.''' ) lowercase_ = new_module lowercase_ = splits[-1] if tensor_name not in module._parameters and tensor_name not in module._buffers: raise ValueError(F'''{module} does not have a parameter or a buffer named {tensor_name}.''' ) lowercase_ = tensor_name in module._buffers lowercase_ = getattr(snake_case__ , snake_case__ ) if old_value.device == torch.device('''meta''' ) and device not in ["meta", torch.device('''meta''' )] and value is None: raise ValueError(F'''{tensor_name} is on the meta device, we need a `value` to put in on {device}.''' ) lowercase_ = False lowercase_ = False if is_buffer or not is_bitsandbytes_available(): lowercase_ = False lowercase_ = False else: lowercase_ = hasattr(bnb.nn , '''Params4bit''' ) and isinstance(module._parameters[tensor_name] , bnb.nn.Paramsabit ) lowercase_ = isinstance(module._parameters[tensor_name] , bnb.nn.IntaParams ) if is_abit or is_abit: lowercase_ = module._parameters[tensor_name] if param.device.type != "cuda": if value is None: lowercase_ = old_value.to(snake_case__ ) elif isinstance(snake_case__ , torch.Tensor ): lowercase_ = value.to('''cpu''' ) if value.dtype == torch.inta: lowercase_ = version.parse(importlib.metadata.version('''bitsandbytes''' ) ) > version.parse( '''0.37.2''' ) if not is_abit_serializable: raise ValueError( '''Detected int8 weights but the version of bitsandbytes is not compatible with int8 serialization. ''' '''Make sure to download the latest `bitsandbytes` version. `pip install --upgrade bitsandbytes`.''' ) else: lowercase_ = torch.tensor(snake_case__ , device='''cpu''' ) # Support models using `Conv1D` in place of `nn.Linear` (e.g. gpt2) by transposing the weight matrix prior to quantization. # Since weights are saved in the correct "orientation", we skip transposing when loading. if issubclass(module.source_cls , snake_case__ ) and fpaa_statistics is None: lowercase_ = new_value.T lowercase_ = old_value.__dict__ if is_abit: lowercase_ = bnb.nn.IntaParams(snake_case__ , requires_grad=snake_case__ , **snake_case__ ).to(snake_case__ ) elif is_abit: lowercase_ = bnb.nn.Paramsabit(snake_case__ , requires_grad=snake_case__ , **snake_case__ ).to(snake_case__ ) lowercase_ = new_value if fpaa_statistics is not None: setattr(module.weight , '''SCB''' , fpaa_statistics.to(snake_case__ ) ) else: if value is None: lowercase_ = old_value.to(snake_case__ ) elif isinstance(snake_case__ , torch.Tensor ): lowercase_ = value.to(snake_case__ ) else: lowercase_ = torch.tensor(snake_case__ , device=snake_case__ ) if is_buffer: lowercase_ = new_value else: lowercase_ = nn.Parameter(snake_case__ , requires_grad=old_value.requires_grad ) lowercase_ = new_value def a ( snake_case__: str , snake_case__: Union[str, Any]=None , snake_case__: Any=None , snake_case__: List[str]=None , snake_case__: Optional[Any]=False ): '''simple docstring''' for name, module in model.named_children(): if current_key_name is None: lowercase_ = [] current_key_name.append(snake_case__ ) if (isinstance(snake_case__ , nn.Linear ) or isinstance(snake_case__ , snake_case__ )) and name not in modules_to_not_convert: # Check if the current key is not in the `modules_to_not_convert` if not any(key in '''.'''.join(snake_case__ ) for key in modules_to_not_convert ): with init_empty_weights(): if isinstance(snake_case__ , snake_case__ ): lowercase_ , lowercase_ = module.weight.shape else: lowercase_ = module.in_features lowercase_ = module.out_features if quantization_config.quantization_method() == "llm_int8": lowercase_ = bnb.nn.LinearabitLt( snake_case__ , snake_case__ , module.bias is not None , has_fpaa_weights=quantization_config.llm_inta_has_fpaa_weight , threshold=quantization_config.llm_inta_threshold , ) lowercase_ = True else: if ( quantization_config.llm_inta_skip_modules is not None and name in quantization_config.llm_inta_skip_modules ): pass else: lowercase_ = bnb.nn.Linearabit( snake_case__ , snake_case__ , module.bias is not None , quantization_config.bnb_abit_compute_dtype , compress_statistics=quantization_config.bnb_abit_use_double_quant , quant_type=quantization_config.bnb_abit_quant_type , ) lowercase_ = True # Store the module class in case we need to transpose the weight later lowercase_ = type(snake_case__ ) # Force requires grad to False to avoid unexpected errors model._modules[name].requires_grad_(snake_case__ ) if len(list(module.children() ) ) > 0: lowercase_ , lowercase_ = _replace_with_bnb_linear( snake_case__ , snake_case__ , snake_case__ , snake_case__ , has_been_replaced=snake_case__ , ) # Remove the last key for recursion current_key_name.pop(-1 ) return model, has_been_replaced def a ( snake_case__: Any , snake_case__: Any=None , snake_case__: Union[str, Any]=None , snake_case__: str=None ): '''simple docstring''' lowercase_ = ['''lm_head'''] if modules_to_not_convert is None else modules_to_not_convert lowercase_ , lowercase_ = _replace_with_bnb_linear( snake_case__ , snake_case__ , snake_case__ , snake_case__ ) if not has_been_replaced: logger.warning( '''You are loading your model in 8bit or 4bit but no linear modules were found in your model.''' ''' Please double check your model architecture, or submit an issue on github if you think this is''' ''' a bug.''' ) return model def a ( *snake_case__: str , **snake_case__: Dict ): '''simple docstring''' warnings.warn( '''`replace_8bit_linear` will be deprecated in a future version, please use `replace_with_bnb_linear` instead''' , snake_case__ , ) return replace_with_bnb_linear(*snake_case__ , **snake_case__ ) def a ( *snake_case__: Any , **snake_case__: List[Any] ): '''simple docstring''' warnings.warn( '''`set_module_8bit_tensor_to_device` will be deprecated in a future version, please use `set_module_quantized_tensor_to_device` instead''' , snake_case__ , ) return set_module_quantized_tensor_to_device(*snake_case__ , **snake_case__ ) def a ( snake_case__: Optional[Any] ): '''simple docstring''' lowercase_ = deepcopy(snake_case__ ) # this has 0 cost since it is done inside `init_empty_weights` context manager` tied_model.tie_weights() lowercase_ = find_tied_parameters(snake_case__ ) # For compatibility with Accelerate < 0.18 if isinstance(snake_case__ , snake_case__ ): lowercase_ = sum(list(tied_params.values() ) , [] ) + list(tied_params.keys() ) else: lowercase_ = sum(snake_case__ , [] ) lowercase_ = len(snake_case__ ) > 0 # Check if it is a base model lowercase_ = not hasattr(snake_case__ , model.base_model_prefix ) # Ignore this for base models (BertModel, GPT2Model, etc.) if (not has_tied_params) and is_base_model: return [] # otherwise they have an attached head lowercase_ = list(model.named_children() ) lowercase_ = [list_modules[-1][0]] # add last module together with tied weights lowercase_ = set(snake_case__ ) - set(snake_case__ ) lowercase_ = list(set(snake_case__ ) ) + list(snake_case__ ) # remove ".weight" from the keys lowercase_ = ['''.weight''', '''.bias'''] lowercase_ = [] for name in list_untouched: for name_to_remove in names_to_remove: if name_to_remove in name: lowercase_ = name.replace(snake_case__ , '''''' ) filtered_module_names.append(snake_case__ ) return filtered_module_names
30
0
"""simple docstring""" import argparse import torch from ...utils import logging from . import AlbertConfig, AlbertForPreTraining, load_tf_weights_in_albert logging.set_verbosity_info() def A_ ( _lowerCAmelCase : Optional[int], _lowerCAmelCase : Tuple, _lowerCAmelCase : List[str] ): """simple docstring""" _a = AlbertConfig.from_json_file(snake_case__ ) print(f'Building PyTorch model from configuration: {config}' ) _a = AlbertForPreTraining(snake_case__ ) # Load weights from tf checkpoint load_tf_weights_in_albert(snake_case__, snake_case__, snake_case__ ) # Save pytorch-model print(f'Save PyTorch model to {pytorch_dump_path}' ) torch.save(model.state_dict(), snake_case__ ) if __name__ == "__main__": __snake_case = 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( '''--albert_config_file''', default=None, type=str, required=True, help=( '''The config json file corresponding to the pre-trained ALBERT 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.''' ) __snake_case = parser.parse_args() convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.albert_config_file, args.pytorch_dump_path)
320
import argparse import os import re __a = 'src/transformers/models/auto' # re pattern that matches mapping introductions: # SUPER_MODEL_MAPPING_NAMES = OrderedDict or SUPER_MODEL_MAPPING = OrderedDict __a = re.compile(r'[A-Z_]+_MAPPING(\s+|_[A-Z_]+\s+)=\s+OrderedDict') # re pattern that matches identifiers in mappings __a = re.compile(r'\s*\(\s*"(\S[^"]+)"') def a ( snake_case__: str , snake_case__: bool = False ): '''simple docstring''' with open(snake_case__ , '''r''' , encoding='''utf-8''' ) as f: lowercase_ = f.read() lowercase_ = content.split('''\n''' ) lowercase_ = [] lowercase_ = 0 while line_idx < len(snake_case__ ): if _re_intro_mapping.search(lines[line_idx] ) is not None: lowercase_ = len(re.search(r'''^(\s*)\S''' , lines[line_idx] ).groups()[0] ) + 8 # Start of a new mapping! while not lines[line_idx].startswith(''' ''' * indent + '''(''' ): new_lines.append(lines[line_idx] ) line_idx += 1 lowercase_ = [] while lines[line_idx].strip() != "]": # Blocks either fit in one line or not if lines[line_idx].strip() == "(": lowercase_ = line_idx while not lines[line_idx].startswith(''' ''' * indent + ''')''' ): line_idx += 1 blocks.append('''\n'''.join(lines[start_idx : line_idx + 1] ) ) else: blocks.append(lines[line_idx] ) line_idx += 1 # Sort blocks by their identifiers lowercase_ = sorted(snake_case__ , key=lambda snake_case__ : _re_identifier.search(snake_case__ ).groups()[0] ) new_lines += blocks else: new_lines.append(lines[line_idx] ) line_idx += 1 if overwrite: with open(snake_case__ , '''w''' , encoding='''utf-8''' ) as f: f.write('''\n'''.join(snake_case__ ) ) elif "\n".join(snake_case__ ) != content: return True def a ( snake_case__: bool = False ): '''simple docstring''' lowercase_ = [os.path.join(snake_case__ , snake_case__ ) for f in os.listdir(snake_case__ ) if f.endswith('''.py''' )] lowercase_ = [sort_auto_mapping(snake_case__ , overwrite=snake_case__ ) for fname in fnames] if not overwrite and any(snake_case__ ): lowercase_ = [f for f, d in zip(snake_case__ , snake_case__ ) if d] raise ValueError( F'''The following files have auto mappings that need sorting: {', '.join(snake_case__ )}. Run `make style` to fix''' ''' this.''' ) if __name__ == "__main__": __a = argparse.ArgumentParser() parser.add_argument('--check_only', action='store_true', help='Whether to only check or fix style.') __a = parser.parse_args() sort_all_auto_mappings(not args.check_only)
30
0
"""simple docstring""" import flax.linen as nn import jax import jax.numpy as jnp class SCREAMING_SNAKE_CASE__ ( nn.Module ): _a = 42 _a = jnp.floataa def __lowercase ( self : List[Any] ): lowerCAmelCase = nn.Conv( self.out_channels , kernel_size=(3, 3) , strides=(1, 1) , padding=((1, 1), (1, 1)) , dtype=self.dtype , ) def __call__( self : str , lowerCAmelCase : Optional[Any] ): lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase = hidden_states.shape lowerCAmelCase = jax.image.resize( SCREAMING_SNAKE_CASE_ , shape=(batch, height * 2, width * 2, channels) , method="""nearest""" , ) lowerCAmelCase = self.conv(SCREAMING_SNAKE_CASE_ ) return hidden_states class SCREAMING_SNAKE_CASE__ ( nn.Module ): _a = 42 _a = jnp.floataa def __lowercase ( self : Union[str, Any] ): lowerCAmelCase = nn.Conv( self.out_channels , kernel_size=(3, 3) , strides=(2, 2) , padding=((1, 1), (1, 1)) , dtype=self.dtype , ) def __call__( self : Tuple , lowerCAmelCase : Dict ): # pad = ((0, 0), (0, 1), (0, 1), (0, 0)) # pad height and width dim # hidden_states = jnp.pad(hidden_states, pad_width=pad) lowerCAmelCase = self.conv(SCREAMING_SNAKE_CASE_ ) return hidden_states class SCREAMING_SNAKE_CASE__ ( nn.Module ): _a = 42 _a = None _a = 0.0 _a = None _a = jnp.floataa def __lowercase ( self : Any ): lowerCAmelCase = self.in_channels if self.out_channels is None else self.out_channels lowerCAmelCase = nn.GroupNorm(num_groups=32 , epsilon=1e-5 ) lowerCAmelCase = nn.Conv( SCREAMING_SNAKE_CASE_ , kernel_size=(3, 3) , strides=(1, 1) , padding=((1, 1), (1, 1)) , dtype=self.dtype , ) lowerCAmelCase = nn.Dense(SCREAMING_SNAKE_CASE_ , dtype=self.dtype ) lowerCAmelCase = nn.GroupNorm(num_groups=32 , epsilon=1e-5 ) lowerCAmelCase = nn.Dropout(self.dropout_prob ) lowerCAmelCase = nn.Conv( SCREAMING_SNAKE_CASE_ , kernel_size=(3, 3) , strides=(1, 1) , padding=((1, 1), (1, 1)) , dtype=self.dtype , ) lowerCAmelCase = self.in_channels != out_channels if self.use_nin_shortcut is None else self.use_nin_shortcut lowerCAmelCase = None if use_nin_shortcut: lowerCAmelCase = nn.Conv( SCREAMING_SNAKE_CASE_ , kernel_size=(1, 1) , strides=(1, 1) , padding="""VALID""" , dtype=self.dtype , ) def __call__( self : Union[str, Any] , lowerCAmelCase : Dict , lowerCAmelCase : Any , lowerCAmelCase : Optional[int]=True ): lowerCAmelCase = hidden_states lowerCAmelCase = self.norma(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase = nn.swish(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase = self.conva(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase = self.time_emb_proj(nn.swish(SCREAMING_SNAKE_CASE_ ) ) lowerCAmelCase = jnp.expand_dims(jnp.expand_dims(SCREAMING_SNAKE_CASE_ , 1 ) , 1 ) lowerCAmelCase = hidden_states + temb lowerCAmelCase = self.norma(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase = nn.swish(SCREAMING_SNAKE_CASE_ ) lowerCAmelCase = self.dropout(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) lowerCAmelCase = self.conva(SCREAMING_SNAKE_CASE_ ) if self.conv_shortcut is not None: lowerCAmelCase = self.conv_shortcut(SCREAMING_SNAKE_CASE_ ) return hidden_states + residual
155
def a ( snake_case__: list , snake_case__: list , snake_case__: int , snake_case__: int , snake_case__: int ): '''simple docstring''' if index == number_of_items: return 0 lowercase_ = 0 lowercase_ = 0 lowercase_ = knapsack(snake_case__ , snake_case__ , snake_case__ , snake_case__ , index + 1 ) if weights[index] <= max_weight: lowercase_ = values[index] + knapsack( snake_case__ , snake_case__ , snake_case__ , max_weight - weights[index] , index + 1 ) return max(snake_case__ , snake_case__ ) if __name__ == "__main__": import doctest doctest.testmod()
30
0
from typing import List, Union from ..utils import ( add_end_docstrings, is_tf_available, is_torch_available, is_vision_available, logging, requires_backends, ) from .base import PIPELINE_INIT_ARGS, Pipeline if is_vision_available(): from PIL import Image from ..image_utils import load_image if is_tf_available(): from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_VISION_2_SEQ_MAPPING if is_torch_available(): import torch from ..models.auto.modeling_auto import MODEL_FOR_VISION_2_SEQ_MAPPING _lowerCamelCase : str = logging.get_logger(__name__) @add_end_docstrings(lowerCamelCase__ ) class __UpperCAmelCase ( lowerCamelCase__ ): def __init__( self : Union[str, Any], *__A : List[Any], **__A : List[str] ): super().__init__(*SCREAMING_SNAKE_CASE_, **SCREAMING_SNAKE_CASE_ ) requires_backends(self, '''vision''' ) self.check_model_type( TF_MODEL_FOR_VISION_2_SEQ_MAPPING if self.framework == '''tf''' else MODEL_FOR_VISION_2_SEQ_MAPPING ) def __magic_name__ ( self : Dict, __A : Union[str, Any]=None, __A : str=None, __A : List[str]=None ): UpperCAmelCase : Any = {} UpperCAmelCase : str = {} if prompt is not None: UpperCAmelCase : List[Any] = prompt if generate_kwargs is not None: UpperCAmelCase : Dict = generate_kwargs if max_new_tokens is not None: if "generate_kwargs" not in forward_kwargs: UpperCAmelCase : int = {} if "max_new_tokens" in forward_kwargs["generate_kwargs"]: raise ValueError( '''\'max_new_tokens\' is defined twice, once in \'generate_kwargs\' and once as a direct parameter,''' ''' please use only one''' ) UpperCAmelCase : Optional[Any] = max_new_tokens return preprocess_params, forward_kwargs, {} def __call__( self : Tuple, __A : Union[str, List[str], "Image.Image", List["Image.Image"]], **__A : Dict ): return super().__call__(SCREAMING_SNAKE_CASE_, **SCREAMING_SNAKE_CASE_ ) def __magic_name__ ( self : int, __A : Tuple, __A : int=None ): UpperCAmelCase : Union[str, Any] = load_image(SCREAMING_SNAKE_CASE_ ) if prompt is not None: if not isinstance(SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ ): raise ValueError( F'''Received an invalid text input, got - {type(SCREAMING_SNAKE_CASE_ )} - but expected a single string. ''' '''Note also that one single text can be provided for conditional image to text generation.''' ) UpperCAmelCase : Optional[Any] = self.model.config.model_type if model_type == "git": UpperCAmelCase : List[str] = self.image_processor(images=SCREAMING_SNAKE_CASE_, return_tensors=self.framework ) UpperCAmelCase : Optional[int] = self.tokenizer(text=SCREAMING_SNAKE_CASE_, add_special_tokens=SCREAMING_SNAKE_CASE_ ).input_ids UpperCAmelCase : int = [self.tokenizer.cls_token_id] + input_ids UpperCAmelCase : List[Any] = torch.tensor(SCREAMING_SNAKE_CASE_ ).unsqueeze(0 ) model_inputs.update({'''input_ids''': input_ids} ) elif model_type == "pix2struct": UpperCAmelCase : List[Any] = self.image_processor(images=SCREAMING_SNAKE_CASE_, header_text=SCREAMING_SNAKE_CASE_, return_tensors=self.framework ) elif model_type != "vision-encoder-decoder": # vision-encoder-decoder does not support conditional generation UpperCAmelCase : List[str] = self.image_processor(images=SCREAMING_SNAKE_CASE_, return_tensors=self.framework ) UpperCAmelCase : Dict = self.tokenizer(SCREAMING_SNAKE_CASE_, return_tensors=self.framework ) model_inputs.update(SCREAMING_SNAKE_CASE_ ) else: raise ValueError(F'''Model type {model_type} does not support conditional text generation''' ) else: UpperCAmelCase : Any = self.image_processor(images=SCREAMING_SNAKE_CASE_, return_tensors=self.framework ) if self.model.config.model_type == "git" and prompt is None: UpperCAmelCase : str = None return model_inputs def __magic_name__ ( self : List[Any], __A : List[Any], __A : List[str]=None ): # Git model sets `model_inputs["input_ids"] = None` in `preprocess` (when `prompt=None`). In batch model, the # pipeline will group them into a list of `None`, which fail `_forward`. Avoid this by checking it first. if ( "input_ids" in model_inputs and isinstance(model_inputs['''input_ids'''], SCREAMING_SNAKE_CASE_ ) and all(x is None for x in model_inputs['''input_ids'''] ) ): UpperCAmelCase : Union[str, Any] = None if generate_kwargs is None: UpperCAmelCase : List[str] = {} # FIXME: We need to pop here due to a difference in how `generation.py` and `generation.tf_utils.py` # parse inputs. In the Tensorflow version, `generate` raises an error if we don't use `input_ids` whereas # the PyTorch version matches it with `self.model.main_input_name` or `self.model.encoder.main_input_name` # in the `_prepare_model_inputs` method. UpperCAmelCase : Any = model_inputs.pop(self.model.main_input_name ) UpperCAmelCase : List[Any] = self.model.generate(SCREAMING_SNAKE_CASE_, **SCREAMING_SNAKE_CASE_, **SCREAMING_SNAKE_CASE_ ) return model_outputs def __magic_name__ ( self : Tuple, __A : Optional[int] ): UpperCAmelCase : Optional[int] = [] for output_ids in model_outputs: UpperCAmelCase : List[str] = { '''generated_text''': self.tokenizer.decode( SCREAMING_SNAKE_CASE_, skip_special_tokens=SCREAMING_SNAKE_CASE_, ) } records.append(SCREAMING_SNAKE_CASE_ ) return records
336
import argparse from collections import defaultdict import yaml __a = 'docs/source/en/_toctree.yml' def a ( snake_case__: Dict ): '''simple docstring''' lowercase_ = defaultdict(snake_case__ ) for doc in model_doc: counts[doc["local"]] += 1 lowercase_ = [key for key, value in counts.items() if value > 1] lowercase_ = [] for duplicate_key in duplicates: lowercase_ = list({doc['''title'''] for doc in model_doc if doc['''local'''] == duplicate_key} ) if len(snake_case__ ) > 1: raise ValueError( F'''{duplicate_key} is present several times in the documentation table of content at ''' '''`docs/source/en/_toctree.yml` with different *Title* values. Choose one of those and remove the ''' '''others.''' ) # Only add this once new_doc.append({'''local''': duplicate_key, '''title''': titles[0]} ) # Add none duplicate-keys new_doc.extend([doc for doc in model_doc if counts[doc['''local''']] == 1] ) # Sort return sorted(snake_case__ , key=lambda snake_case__ : s["title"].lower() ) def a ( snake_case__: List[Any]=False ): '''simple docstring''' with open(snake_case__ , encoding='''utf-8''' ) as f: lowercase_ = yaml.safe_load(f.read() ) # Get to the API doc lowercase_ = 0 while content[api_idx]["title"] != "API": api_idx += 1 lowercase_ = content[api_idx]['''sections'''] # Then to the model doc lowercase_ = 0 while api_doc[model_idx]["title"] != "Models": model_idx += 1 lowercase_ = api_doc[model_idx]['''sections'''] lowercase_ = [(idx, section) for idx, section in enumerate(snake_case__ ) if '''sections''' in section] lowercase_ = False for idx, modality_doc in modalities_docs: lowercase_ = modality_doc['''sections'''] lowercase_ = clean_model_doc_toc(snake_case__ ) if old_modality_doc != new_modality_doc: lowercase_ = True if overwrite: lowercase_ = new_modality_doc if diff: if overwrite: lowercase_ = model_doc lowercase_ = api_doc with open(snake_case__ , '''w''' , encoding='''utf-8''' ) as f: f.write(yaml.dump(snake_case__ , allow_unicode=snake_case__ ) ) else: raise ValueError( '''The model doc part of the table of content is not properly sorted, run `make style` to fix this.''' ) if __name__ == "__main__": __a = argparse.ArgumentParser() parser.add_argument('--fix_and_overwrite', action='store_true', help='Whether to fix inconsistencies.') __a = parser.parse_args() check_model_doc(args.fix_and_overwrite)
30
0
import csv import tweepy # Twitter API credentials __lowercase = '''''' __lowercase = '''''' __lowercase = '''''' __lowercase = '''''' def lowerCamelCase ( SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCamelCase :Tuple = tweepy.OAuthHandler(snake_case__ , snake_case__ ) auth.set_access_token(snake_case__ , snake_case__ ) __UpperCamelCase :Tuple = tweepy.API(snake_case__ ) # initialize a list to hold all the tweepy Tweets __UpperCamelCase :Union[str, Any] = [] # make initial request for most recent tweets (200 is the maximum allowed count) __UpperCamelCase :Union[str, Any] = api.user_timeline(screen_name=snake_case__ , count=200 ) # save most recent tweets alltweets.extend(snake_case__ ) # save the id of the oldest tweet less one __UpperCamelCase :Union[str, Any] = alltweets[-1].id - 1 # keep grabbing tweets until there are no tweets left to grab while len(snake_case__ ) > 0: print(f"""getting tweets before {oldest}""" ) # all subsequent requests use the max_id param to prevent duplicates __UpperCamelCase :str = api.user_timeline( screen_name=snake_case__ , count=200 , max_id=snake_case__ ) # save most recent tweets alltweets.extend(snake_case__ ) # update the id of the oldest tweet less one __UpperCamelCase :str = alltweets[-1].id - 1 print(f"""...{len(snake_case__ )} tweets downloaded so far""" ) # transform the tweepy tweets into a 2D array that will populate the csv __UpperCamelCase :List[Any] = [[tweet.id_str, tweet.created_at, tweet.text] for tweet in alltweets] # write the csv with open(f"""new_{screen_name}_tweets.csv""" , '''w''' ) as f: __UpperCamelCase :Optional[int] = csv.writer(snake_case__ ) writer.writerow(['''id''', '''created_at''', '''text'''] ) writer.writerows(snake_case__ ) if __name__ == "__main__": # pass in the username of the account you want to download get_all_tweets('''FirePing32''')
43
import copy from ...configuration_utils import PretrainedConfig from ...utils import logging from ..auto.configuration_auto import CONFIG_MAPPING __a = logging.get_logger(__name__) class lowercase__( UpperCAmelCase ): """simple docstring""" a :Union[str, Any] = 'upernet' def __init__( self : Dict , SCREAMING_SNAKE_CASE_ : Dict=None , SCREAMING_SNAKE_CASE_ : str=5_1_2 , SCREAMING_SNAKE_CASE_ : Tuple=0.02 , SCREAMING_SNAKE_CASE_ : Optional[Any]=[1, 2, 3, 6] , SCREAMING_SNAKE_CASE_ : Optional[int]=True , SCREAMING_SNAKE_CASE_ : Tuple=0.4 , SCREAMING_SNAKE_CASE_ : Optional[int]=3_8_4 , SCREAMING_SNAKE_CASE_ : str=2_5_6 , SCREAMING_SNAKE_CASE_ : Dict=1 , SCREAMING_SNAKE_CASE_ : Tuple=False , SCREAMING_SNAKE_CASE_ : str=2_5_5 , **SCREAMING_SNAKE_CASE_ : str , ) -> int: super().__init__(**SCREAMING_SNAKE_CASE_ ) if backbone_config is None: logger.info('''`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.''' ) lowercase_ = CONFIG_MAPPING['''resnet'''](out_features=['''stage1''', '''stage2''', '''stage3''', '''stage4'''] ) elif isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): lowercase_ = backbone_config.get('''model_type''' ) lowercase_ = CONFIG_MAPPING[backbone_model_type] lowercase_ = config_class.from_dict(SCREAMING_SNAKE_CASE_ ) lowercase_ = backbone_config lowercase_ = hidden_size lowercase_ = initializer_range lowercase_ = pool_scales lowercase_ = use_auxiliary_head lowercase_ = auxiliary_loss_weight lowercase_ = auxiliary_in_channels lowercase_ = auxiliary_channels lowercase_ = auxiliary_num_convs lowercase_ = auxiliary_concat_input lowercase_ = loss_ignore_index def _lowercase ( self : List[str] ) -> List[str]: lowercase_ = copy.deepcopy(self.__dict__ ) lowercase_ = self.backbone_config.to_dict() lowercase_ = self.__class__.model_type return output
30
0
from io import BytesIO from typing import List, Union import requests from ..utils import add_end_docstrings, is_decord_available, is_torch_available, logging, requires_backends from .base import PIPELINE_INIT_ARGS, Pipeline if is_decord_available(): import numpy as np from decord import VideoReader if is_torch_available(): from ..models.auto.modeling_auto import MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING __snake_case = logging.get_logger(__name__) @add_end_docstrings(_a ) class __lowerCamelCase (_a ): def __init__( self: Optional[int],*A_: Tuple,**A_: Union[str, Any] ): '''simple docstring''' super().__init__(*SCREAMING_SNAKE_CASE_,**SCREAMING_SNAKE_CASE_ ) requires_backends(self,'decord' ) self.check_model_type(SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self: Union[str, Any],A_: int=None,A_: str=None,A_: int=None ): '''simple docstring''' __UpperCamelCase = {} if frame_sampling_rate is not None: __UpperCamelCase = frame_sampling_rate if num_frames is not None: __UpperCamelCase = num_frames __UpperCamelCase = {} if top_k is not None: __UpperCamelCase = top_k return preprocess_params, {}, postprocess_params def __call__( self: str,A_: Union[str, List[str]],**A_: Dict ): '''simple docstring''' return super().__call__(SCREAMING_SNAKE_CASE_,**SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self: Any,A_: List[Any],A_: Union[str, Any]=None,A_: Union[str, Any]=1 ): '''simple docstring''' if num_frames is None: __UpperCamelCase = self.model.config.num_frames if video.startswith('http://' ) or video.startswith('https://' ): __UpperCamelCase = BytesIO(requests.get(SCREAMING_SNAKE_CASE_ ).content ) __UpperCamelCase = VideoReader(SCREAMING_SNAKE_CASE_ ) videoreader.seek(0 ) __UpperCamelCase = 0 __UpperCamelCase = num_frames * frame_sampling_rate - 1 __UpperCamelCase = np.linspace(SCREAMING_SNAKE_CASE_,SCREAMING_SNAKE_CASE_,num=SCREAMING_SNAKE_CASE_,dtype=np.intaa ) __UpperCamelCase = videoreader.get_batch(SCREAMING_SNAKE_CASE_ ).asnumpy() __UpperCamelCase = list(SCREAMING_SNAKE_CASE_ ) __UpperCamelCase = self.image_processor(SCREAMING_SNAKE_CASE_,return_tensors=self.framework ) return model_inputs def snake_case_ ( self: Union[str, Any],A_: Optional[int] ): '''simple docstring''' __UpperCamelCase = self.model(**SCREAMING_SNAKE_CASE_ ) return model_outputs def snake_case_ ( self: int,A_: List[Any],A_: List[Any]=5 ): '''simple docstring''' if top_k > self.model.config.num_labels: __UpperCamelCase = self.model.config.num_labels if self.framework == "pt": __UpperCamelCase = model_outputs.logits.softmax(-1 )[0] __UpperCamelCase, __UpperCamelCase = probs.topk(SCREAMING_SNAKE_CASE_ ) else: raise ValueError(F'''Unsupported framework: {self.framework}''' ) __UpperCamelCase = scores.tolist() __UpperCamelCase = ids.tolist() return [{"score": score, "label": self.model.config.idalabel[_id]} for score, _id in zip(SCREAMING_SNAKE_CASE_,SCREAMING_SNAKE_CASE_ )]
310
import tempfile import unittest from make_student import create_student_by_copying_alternating_layers from transformers import AutoConfig from transformers.file_utils import cached_property from transformers.testing_utils import require_torch __a = 'sshleifer/bart-tiny-random' __a = 'patrickvonplaten/t5-tiny-random' @require_torch class lowercase__( unittest.TestCase ): """simple docstring""" @cached_property def _lowercase ( self : Any ) -> Tuple: return AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE_ ) def _lowercase ( self : int ) -> List[Any]: lowercase_ , *lowercase_ = create_student_by_copying_alternating_layers(SCREAMING_SNAKE_CASE_ , tempfile.mkdtemp() , e=1 , d=1 ) self.assertEqual(student.config.num_hidden_layers , 1 ) def _lowercase ( self : Union[str, Any] ) -> Optional[int]: lowercase_ , *lowercase_ = create_student_by_copying_alternating_layers(SCREAMING_SNAKE_CASE_ , tempfile.mkdtemp() , e=1 , d=SCREAMING_SNAKE_CASE_ ) def _lowercase ( self : List[str] ) -> List[Any]: lowercase_ , *lowercase_ = create_student_by_copying_alternating_layers(SCREAMING_SNAKE_CASE_ , tempfile.mkdtemp() , e=1 , d=SCREAMING_SNAKE_CASE_ ) self.assertEqual(student.config.encoder_layers , 1 ) self.assertEqual(student.config.decoder_layers , self.teacher_config.encoder_layers ) def _lowercase ( self : List[Any] ) -> List[Any]: lowercase_ , *lowercase_ = create_student_by_copying_alternating_layers(SCREAMING_SNAKE_CASE_ , tempfile.mkdtemp() , e=1 , d=1 ) self.assertEqual(student.config.encoder_layers , 1 ) self.assertEqual(student.config.decoder_layers , 1 ) def _lowercase ( self : Union[str, Any] ) -> Optional[int]: with self.assertRaises(SCREAMING_SNAKE_CASE_ ): create_student_by_copying_alternating_layers(SCREAMING_SNAKE_CASE_ , tempfile.mkdtemp() , e=SCREAMING_SNAKE_CASE_ , d=SCREAMING_SNAKE_CASE_ )
30
0
"""simple docstring""" from typing import Dict, List from nltk.translate import gleu_score import datasets from datasets import MetricInfo lowerCamelCase__ = """\\n@misc{wu2016googles,\n title={Google\'s Neural Machine Translation System: Bridging the Gap between Human and Machine Translation},\n author={Yonghui Wu and Mike Schuster and Zhifeng Chen and Quoc V. Le and Mohammad Norouzi and Wolfgang Macherey\n and Maxim Krikun and Yuan Cao and Qin Gao and Klaus Macherey and Jeff Klingner and Apurva Shah and Melvin\n Johnson and Xiaobing Liu and Łukasz Kaiser and Stephan Gouws and Yoshikiyo Kato and Taku Kudo and Hideto\n Kazawa and Keith Stevens and George Kurian and Nishant Patil and Wei Wang and Cliff Young and\n Jason Smith and Jason Riesa and Alex Rudnick and Oriol Vinyals and Greg Corrado and Macduff Hughes\n and Jeffrey Dean},\n year={2016},\n eprint={1609.08144},\n archivePrefix={arXiv},\n primaryClass={cs.CL}\n}\n""" lowerCamelCase__ = """\\nThe BLEU score has some undesirable properties when used for single\nsentences, as it was designed to be a corpus measure. We therefore\nuse a slightly different score for our RL experiments which we call\nthe \'GLEU score\'. For the GLEU score, we record all sub-sequences of\n1, 2, 3 or 4 tokens in output and target sequence (n-grams). We then\ncompute a recall, which is the ratio of the number of matching n-grams\nto the number of total n-grams in the target (ground truth) sequence,\nand a precision, which is the ratio of the number of matching n-grams\nto the number of total n-grams in the generated output sequence. Then\nGLEU score is simply the minimum of recall and precision. This GLEU\nscore\'s range is always between 0 (no matches) and 1 (all match) and\nit is symmetrical when switching output and target. According to\nour experiments, GLEU score correlates quite well with the BLEU\nmetric on a corpus level but does not have its drawbacks for our per\nsentence reward objective.\n""" lowerCamelCase__ = """\\nComputes corpus-level Google BLEU (GLEU) score of translated segments against one or more references.\nInstead of averaging the sentence level GLEU scores (i.e. macro-average precision), Wu et al. (2016) sum up the matching\ntokens and the max of hypothesis and reference tokens for each sentence, then compute using the aggregate values.\n\nArgs:\n predictions (list of str): list of translations to score.\n Each translation should be tokenized into a list of tokens.\n references (list of list of str): list of lists of references for each translation.\n Each reference should be tokenized into a list of tokens.\n min_len (int): The minimum order of n-gram this function should extract. Defaults to 1.\n max_len (int): The maximum order of n-gram this function should extract. Defaults to 4.\n\nReturns:\n \'google_bleu\': google_bleu score\n\nExamples:\n Example 1:\n >>> hyp1 = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'which\',\n ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'always\',\n ... \'disobeys\', \'the\', \'commands\', \'of\', \'the\', \'cat\']\n >>> ref1a = [\'It\', \'is\', \'the\', \'guiding\', \'principle\', \'which\',\n ... \'guarantees\', \'the\', \'rubber\', \'duck\', \'forces\', \'never\',\n ... \'being\', \'under\', \'the\', \'command\', \'of\', \'the\', \'cat\']\n\n >>> hyp2 = [\'he\', \'read\', \'the\', \'book\', \'because\', \'he\', \'was\',\n ... \'interested\', \'in\', \'world\', \'history\']\n >>> ref2a = [\'he\', \'was\', \'interested\', \'in\', \'world\', \'history\',\n ... \'because\', \'he\', \'read\', \'the\', \'book\']\n\n >>> list_of_references = [[ref1a], [ref2a]]\n >>> hypotheses = [hyp1, hyp2]\n >>> google_bleu = datasets.load_metric(\"google_bleu\")\n >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references)\n >>> print(round(results[\"google_bleu\"], 2))\n 0.44\n\n Example 2:\n >>> hyp1 = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'which\',\n ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'always\',\n ... \'disobeys\', \'the\', \'commands\', \'of\', \'the\', \'cat\']\n >>> ref1a = [\'It\', \'is\', \'the\', \'guiding\', \'principle\', \'which\',\n ... \'guarantees\', \'the\', \'rubber\', \'duck\', \'forces\', \'never\',\n ... \'being\', \'under\', \'the\', \'command\', \'of\', \'the\', \'cat\']\n >>> ref1b = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'that\',\n ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'will\', \'never\',\n ... \'heed\', \'the\', \'cat\', \'commands\']\n >>> ref1c = [\'It\', \'is\', \'the\', \'practical\', \'guide\', \'for\', \'the\',\n ... \'rubber\', \'duck\', \'army\', \'never\', \'to\', \'heed\', \'the\', \'directions\',\n ... \'of\', \'the\', \'cat\']\n\n >>> hyp2 = [\'he\', \'read\', \'the\', \'book\', \'because\', \'he\', \'was\',\n ... \'interested\', \'in\', \'world\', \'history\']\n >>> ref2a = [\'he\', \'was\', \'interested\', \'in\', \'world\', \'history\',\n ... \'because\', \'he\', \'read\', \'the\', \'book\']\n\n >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]]\n >>> hypotheses = [hyp1, hyp2]\n >>> google_bleu = datasets.load_metric(\"google_bleu\")\n >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references)\n >>> print(round(results[\"google_bleu\"], 2))\n 0.61\n\n Example 3:\n >>> hyp1 = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'which\',\n ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'always\',\n ... \'disobeys\', \'the\', \'commands\', \'of\', \'the\', \'cat\']\n >>> ref1a = [\'It\', \'is\', \'the\', \'guiding\', \'principle\', \'which\',\n ... \'guarantees\', \'the\', \'rubber\', \'duck\', \'forces\', \'never\',\n ... \'being\', \'under\', \'the\', \'command\', \'of\', \'the\', \'cat\']\n >>> ref1b = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'that\',\n ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'will\', \'never\',\n ... \'heed\', \'the\', \'cat\', \'commands\']\n >>> ref1c = [\'It\', \'is\', \'the\', \'practical\', \'guide\', \'for\', \'the\',\n ... \'rubber\', \'duck\', \'army\', \'never\', \'to\', \'heed\', \'the\', \'directions\',\n ... \'of\', \'the\', \'cat\']\n\n >>> hyp2 = [\'he\', \'read\', \'the\', \'book\', \'because\', \'he\', \'was\',\n ... \'interested\', \'in\', \'world\', \'history\']\n >>> ref2a = [\'he\', \'was\', \'interested\', \'in\', \'world\', \'history\',\n ... \'because\', \'he\', \'read\', \'the\', \'book\']\n\n >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]]\n >>> hypotheses = [hyp1, hyp2]\n >>> google_bleu = datasets.load_metric(\"google_bleu\")\n >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references, min_len=2)\n >>> print(round(results[\"google_bleu\"], 2))\n 0.53\n\n Example 4:\n >>> hyp1 = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'which\',\n ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'always\',\n ... \'disobeys\', \'the\', \'commands\', \'of\', \'the\', \'cat\']\n >>> ref1a = [\'It\', \'is\', \'the\', \'guiding\', \'principle\', \'which\',\n ... \'guarantees\', \'the\', \'rubber\', \'duck\', \'forces\', \'never\',\n ... \'being\', \'under\', \'the\', \'command\', \'of\', \'the\', \'cat\']\n >>> ref1b = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'that\',\n ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'will\', \'never\',\n ... \'heed\', \'the\', \'cat\', \'commands\']\n >>> ref1c = [\'It\', \'is\', \'the\', \'practical\', \'guide\', \'for\', \'the\',\n ... \'rubber\', \'duck\', \'army\', \'never\', \'to\', \'heed\', \'the\', \'directions\',\n ... \'of\', \'the\', \'cat\']\n\n >>> hyp2 = [\'he\', \'read\', \'the\', \'book\', \'because\', \'he\', \'was\',\n ... \'interested\', \'in\', \'world\', \'history\']\n >>> ref2a = [\'he\', \'was\', \'interested\', \'in\', \'world\', \'history\',\n ... \'because\', \'he\', \'read\', \'the\', \'book\']\n\n >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]]\n >>> hypotheses = [hyp1, hyp2]\n >>> google_bleu = datasets.load_metric(\"google_bleu\")\n >>> results = google_bleu.compute(predictions=hypotheses,references=list_of_references, min_len=2, max_len=6)\n >>> print(round(results[\"google_bleu\"], 2))\n 0.4\n""" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION) class A__ ( datasets.Metric): def __lowerCamelCase ( self ): return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { 'predictions': datasets.Sequence(datasets.Value('string' , id='token' ) , id='sequence' ), 'references': datasets.Sequence( datasets.Sequence(datasets.Value('string' , id='token' ) , id='sequence' ) , id='references' ), } ) , ) def __lowerCamelCase ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = 1 , _SCREAMING_SNAKE_CASE = 4 , ): return { "google_bleu": gleu_score.corpus_gleu( list_of_references=SCREAMING_SNAKE_CASE_ , hypotheses=SCREAMING_SNAKE_CASE_ , min_len=SCREAMING_SNAKE_CASE_ , max_len=SCREAMING_SNAKE_CASE_ ) }
86
def a ( snake_case__: int = 100 ): '''simple docstring''' lowercase_ = (n * (n + 1) // 2) ** 2 lowercase_ = n * (n + 1) * (2 * n + 1) // 6 return sum_cubes - sum_squares if __name__ == "__main__": print(f"{solution() = }")
30
0
import itertools import math def lowerCAmelCase ( lowerCAmelCase_ )-> Optional[int]: if 1 < number < 4: # 2 and 3 are primes return True elif number < 2 or number % 2 == 0 or number % 3 == 0: # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes return False # All primes number are in format of 6k +/- 1 for i in range(5 , int(math.sqrt(snake_case__ ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True def lowerCAmelCase ( )-> Any: lowerCAmelCase_ : Any = 2 while True: if is_prime(snake_case__ ): yield num num += 1 def lowerCAmelCase ( lowerCAmelCase_ = 10_001 )-> str: return next(itertools.islice(prime_generator() , nth - 1 , snake_case__ ) ) if __name__ == "__main__": print(f"""{solution() = }""")
262
import logging from transformers.configuration_utils import PretrainedConfig __a = logging.getLogger(__name__) class lowercase__( UpperCAmelCase ): """simple docstring""" a :Optional[int] = 'masked_bert' def __init__( self : Optional[int] , SCREAMING_SNAKE_CASE_ : List[Any]=3_0_5_2_2 , SCREAMING_SNAKE_CASE_ : List[str]=7_6_8 , SCREAMING_SNAKE_CASE_ : Optional[int]=1_2 , SCREAMING_SNAKE_CASE_ : Any=1_2 , SCREAMING_SNAKE_CASE_ : str=3_0_7_2 , SCREAMING_SNAKE_CASE_ : Union[str, Any]="gelu" , SCREAMING_SNAKE_CASE_ : Optional[int]=0.1 , SCREAMING_SNAKE_CASE_ : List[str]=0.1 , SCREAMING_SNAKE_CASE_ : Tuple=5_1_2 , SCREAMING_SNAKE_CASE_ : str=2 , SCREAMING_SNAKE_CASE_ : Dict=0.02 , SCREAMING_SNAKE_CASE_ : Any=1e-12 , SCREAMING_SNAKE_CASE_ : Any=0 , SCREAMING_SNAKE_CASE_ : Optional[int]="topK" , SCREAMING_SNAKE_CASE_ : Dict="constant" , SCREAMING_SNAKE_CASE_ : Optional[Any]=0.0 , **SCREAMING_SNAKE_CASE_ : Optional[int] , ) -> Optional[Any]: super().__init__(pad_token_id=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) lowercase_ = vocab_size lowercase_ = hidden_size lowercase_ = num_hidden_layers lowercase_ = num_attention_heads lowercase_ = hidden_act lowercase_ = intermediate_size lowercase_ = hidden_dropout_prob lowercase_ = attention_probs_dropout_prob lowercase_ = max_position_embeddings lowercase_ = type_vocab_size lowercase_ = initializer_range lowercase_ = layer_norm_eps lowercase_ = pruning_method lowercase_ = mask_init lowercase_ = mask_scale
30
0
"""simple docstring""" import argparse import os import re lowercase_ = "src/transformers/models/auto" # re pattern that matches mapping introductions: # SUPER_MODEL_MAPPING_NAMES = OrderedDict or SUPER_MODEL_MAPPING = OrderedDict lowercase_ = re.compile(r"[A-Z_]+_MAPPING(\s+|_[A-Z_]+\s+)=\s+OrderedDict") # re pattern that matches identifiers in mappings lowercase_ = re.compile(r"\s*\(\s*\"(\S[^\"]+)\"") def lowercase ( lowerCAmelCase__ : str , lowerCAmelCase__ : bool = False ) -> Dict: with open(snake_case__ , '''r''' , encoding='''utf-8''' ) as f: __a = f.read() __a = content.split('''\n''' ) __a = [] __a = 0 while line_idx < len(snake_case__ ): if _re_intro_mapping.search(lines[line_idx] ) is not None: __a = len(re.search(r'''^(\s*)\S''' , lines[line_idx] ).groups()[0] ) + 8 # Start of a new mapping! while not lines[line_idx].startswith(''' ''' * indent + '''(''' ): new_lines.append(lines[line_idx] ) line_idx += 1 __a = [] while lines[line_idx].strip() != "]": # Blocks either fit in one line or not if lines[line_idx].strip() == "(": __a = line_idx while not lines[line_idx].startswith(''' ''' * indent + ''')''' ): line_idx += 1 blocks.append('''\n'''.join(lines[start_idx : line_idx + 1] ) ) else: blocks.append(lines[line_idx] ) line_idx += 1 # Sort blocks by their identifiers __a = sorted(snake_case__ , key=lambda lowerCAmelCase__ : _re_identifier.search(snake_case__ ).groups()[0] ) new_lines += blocks else: new_lines.append(lines[line_idx] ) line_idx += 1 if overwrite: with open(snake_case__ , '''w''' , encoding='''utf-8''' ) as f: f.write('''\n'''.join(snake_case__ ) ) elif "\n".join(snake_case__ ) != content: return True def lowercase ( lowerCAmelCase__ : bool = False ) -> Any: __a = [os.path.join(snake_case__ , snake_case__ ) for f in os.listdir(snake_case__ ) if f.endswith('''.py''' )] __a = [sort_auto_mapping(snake_case__ , overwrite=snake_case__ ) for fname in fnames] if not overwrite and any(snake_case__ ): __a = [f for f, d in zip(snake_case__ , snake_case__ ) if d] raise ValueError( f'''The following files have auto mappings that need sorting: {', '.join(snake_case__ )}. Run `make style` to fix''' ''' this.''' ) if __name__ == "__main__": lowercase_ = argparse.ArgumentParser() parser.add_argument("--check_only", action="store_true", help="Whether to only check or fix style.") lowercase_ = parser.parse_args() sort_all_auto_mappings(not args.check_only)
45
import os def a ( ): '''simple docstring''' lowercase_ = os.path.join(os.path.dirname(snake_case__ ) , '''num.txt''' ) with open(snake_case__ ) as file_hand: return str(sum(int(snake_case__ ) for line in file_hand ) )[:10] if __name__ == "__main__": print(solution())
30
0
'''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 MobileViTImageProcessor class lowerCamelCase_ (unittest.TestCase ): '''simple docstring''' def __init__( self : Optional[Any] , A : Dict , A : Dict=7 , A : Optional[int]=3 , A : Optional[int]=18 , A : Dict=30 , A : List[Any]=400 , A : Union[str, Any]=True , A : Tuple=None , A : List[Any]=True , A : int=None , A : Optional[int]=True , ): _UpperCAmelCase : Optional[int] = size if size is not None else {"shortest_edge": 20} _UpperCAmelCase : Optional[Any] = crop_size if crop_size is not None else {"height": 18, "width": 18} _UpperCAmelCase : List[Any] = parent _UpperCAmelCase : Union[str, Any] = batch_size _UpperCAmelCase : Optional[Any] = num_channels _UpperCAmelCase : Union[str, Any] = image_size _UpperCAmelCase : int = min_resolution _UpperCAmelCase : Optional[int] = max_resolution _UpperCAmelCase : List[str] = do_resize _UpperCAmelCase : Optional[Any] = size _UpperCAmelCase : Tuple = do_center_crop _UpperCAmelCase : Optional[int] = crop_size _UpperCAmelCase : Optional[Any] = do_flip_channel_order def _A ( self : Dict ): return { "do_resize": self.do_resize, "size": self.size, "do_center_crop": self.do_center_crop, "crop_size": self.crop_size, "do_flip_channel_order": self.do_flip_channel_order, } @require_torch @require_vision class lowerCamelCase_ (snake_case__ , unittest.TestCase ): '''simple docstring''' __UpperCamelCase: Tuple = MobileViTImageProcessor if is_vision_available() else None def _A ( self : List[Any] ): _UpperCAmelCase : Any = MobileViTImageProcessingTester(self ) @property def _A ( self : int ): return self.image_processor_tester.prepare_image_processor_dict() def _A ( self : Tuple ): _UpperCAmelCase : int = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(A , "do_resize" ) ) self.assertTrue(hasattr(A , "size" ) ) self.assertTrue(hasattr(A , "do_center_crop" ) ) self.assertTrue(hasattr(A , "center_crop" ) ) self.assertTrue(hasattr(A , "do_flip_channel_order" ) ) def _A ( self : Any ): _UpperCAmelCase : List[str] = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"shortest_edge": 20} ) self.assertEqual(image_processor.crop_size , {"height": 18, "width": 18} ) _UpperCAmelCase : Dict = self.image_processing_class.from_dict(self.image_processor_dict , size=42 , crop_size=84 ) self.assertEqual(image_processor.size , {"shortest_edge": 42} ) self.assertEqual(image_processor.crop_size , {"height": 84, "width": 84} ) def _A ( self : Any ): pass def _A ( self : Dict ): # Initialize image_processing _UpperCAmelCase : List[str] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images _UpperCAmelCase : Union[str, Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=A ) for image in image_inputs: self.assertIsInstance(A , Image.Image ) # Test not batched input _UpperCAmelCase : List[Any] = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) # Test batched _UpperCAmelCase : Optional[Any] = image_processing(A , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) def _A ( self : Union[str, Any] ): # Initialize image_processing _UpperCAmelCase : Dict = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors _UpperCAmelCase : Optional[Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=A , numpify=A ) for image in image_inputs: self.assertIsInstance(A , np.ndarray ) # Test not batched input _UpperCAmelCase : 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.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) # Test batched _UpperCAmelCase : Optional[int] = image_processing(A , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) def _A ( self : Any ): # Initialize image_processing _UpperCAmelCase : Any = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors _UpperCAmelCase : Optional[Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=A , torchify=A ) for image in image_inputs: self.assertIsInstance(A , torch.Tensor ) # Test not batched input _UpperCAmelCase : 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.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) # Test batched _UpperCAmelCase : Any = image_processing(A , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , )
31
'''simple docstring''' 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_ (snake_case__ , snake_case__ ): '''simple docstring''' __UpperCamelCase: Optional[Any] = 1 @register_to_config def __init__( self : Optional[int] , A : int = 1000 , A : Optional[Union[np.ndarray, List[float]]] = None ): # set `betas`, `alphas`, `timesteps` self.set_timesteps(A ) # standard deviation of the initial noise distribution _UpperCAmelCase : int = 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. _UpperCAmelCase : int = 4 # running values _UpperCAmelCase : Dict = [] def _A ( self : Optional[int] , A : int , A : Union[str, torch.device] = None ): _UpperCAmelCase : int = num_inference_steps _UpperCAmelCase : Union[str, Any] = torch.linspace(1 , 0 , num_inference_steps + 1 )[:-1] _UpperCAmelCase : Any = torch.cat([steps, torch.tensor([0.0] )] ) if self.config.trained_betas is not None: _UpperCAmelCase : str = torch.tensor(self.config.trained_betas , dtype=torch.floataa ) else: _UpperCAmelCase : Dict = torch.sin(steps * math.pi / 2 ) ** 2 _UpperCAmelCase : List[Any] = (1.0 - self.betas**2) ** 0.5 _UpperCAmelCase : List[str] = (torch.atana(self.betas , self.alphas ) / math.pi * 2)[:-1] _UpperCAmelCase : Dict = timesteps.to(A ) _UpperCAmelCase : Dict = [] def _A ( self : Optional[int] , A : torch.FloatTensor , A : int , A : torch.FloatTensor , A : bool = True , ): 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" ) _UpperCAmelCase : Tuple = (self.timesteps == timestep).nonzero().item() _UpperCAmelCase : Optional[Any] = timestep_index + 1 _UpperCAmelCase : int = sample * self.betas[timestep_index] + model_output * self.alphas[timestep_index] self.ets.append(A ) if len(self.ets ) == 1: _UpperCAmelCase : List[Any] = self.ets[-1] elif len(self.ets ) == 2: _UpperCAmelCase : str = (3 * self.ets[-1] - self.ets[-2]) / 2 elif len(self.ets ) == 3: _UpperCAmelCase : Tuple = (23 * self.ets[-1] - 16 * self.ets[-2] + 5 * self.ets[-3]) / 12 else: _UpperCAmelCase : Union[str, Any] = (1 / 24) * (55 * self.ets[-1] - 59 * self.ets[-2] + 37 * self.ets[-3] - 9 * self.ets[-4]) _UpperCAmelCase : Union[str, Any] = self._get_prev_sample(A , A , A , A ) if not return_dict: return (prev_sample,) return SchedulerOutput(prev_sample=A ) def _A ( self : Union[str, Any] , A : torch.FloatTensor , *A : Union[str, Any] , **A : Dict ): return sample def _A ( self : Optional[Any] , A : Optional[int] , A : int , A : Optional[Any] , A : List[str] ): _UpperCAmelCase : List[str] = self.alphas[timestep_index] _UpperCAmelCase : List[Any] = self.betas[timestep_index] _UpperCAmelCase : Optional[Any] = self.alphas[prev_timestep_index] _UpperCAmelCase : Dict = self.betas[prev_timestep_index] _UpperCAmelCase : Tuple = (sample - sigma * ets) / max(A , 1E-8 ) _UpperCAmelCase : List[str] = next_alpha * pred + ets * next_sigma return prev_sample def __len__( self : Union[str, Any] ): return self.config.num_train_timesteps
31
1
'''simple docstring''' from typing import List, Optional, Union import torch from transformers import ( XLMRobertaTokenizer, ) from ...models import UNetaDConditionModel, VQModel from ...pipelines import DiffusionPipeline from ...pipelines.pipeline_utils import ImagePipelineOutput from ...schedulers import DDIMScheduler, DDPMScheduler from ...utils import ( is_accelerate_available, is_accelerate_version, logging, randn_tensor, replace_example_docstring, ) from .text_encoder import MultilingualCLIP __SCREAMING_SNAKE_CASE : int = logging.get_logger(__name__) # pylint: disable=invalid-name __SCREAMING_SNAKE_CASE : Tuple = """ Examples: ```py >>> from diffusers import KandinskyPipeline, KandinskyPriorPipeline >>> import torch >>> pipe_prior = KandinskyPriorPipeline.from_pretrained(\"kandinsky-community/Kandinsky-2-1-prior\") >>> pipe_prior.to(\"cuda\") >>> prompt = \"red cat, 4k photo\" >>> out = pipe_prior(prompt) >>> image_emb = out.image_embeds >>> negative_image_emb = out.negative_image_embeds >>> pipe = KandinskyPipeline.from_pretrained(\"kandinsky-community/kandinsky-2-1\") >>> pipe.to(\"cuda\") >>> image = pipe( ... prompt, ... image_embeds=image_emb, ... negative_image_embeds=negative_image_emb, ... height=768, ... width=768, ... num_inference_steps=100, ... ).images >>> image[0].save(\"cat.png\") ``` """ def UpperCamelCase_ ( _UpperCAmelCase : int , _UpperCAmelCase : List[Any] , _UpperCAmelCase : Optional[int]=8 ) -> Dict: """simple docstring""" _UpperCAmelCase : Tuple = h // scale_factor**2 if h % scale_factor**2 != 0: new_h += 1 _UpperCAmelCase : Optional[Any] = w // scale_factor**2 if w % scale_factor**2 != 0: new_w += 1 return new_h * scale_factor, new_w * scale_factor class lowerCamelCase_ (snake_case__ ): '''simple docstring''' def __init__( self : int , A : MultilingualCLIP , A : XLMRobertaTokenizer , A : UNetaDConditionModel , A : Union[DDIMScheduler, DDPMScheduler] , A : VQModel , ): super().__init__() self.register_modules( text_encoder=A , tokenizer=A , unet=A , scheduler=A , movq=A , ) _UpperCAmelCase : Any = 2 ** (len(self.movq.config.block_out_channels ) - 1) def _A ( self : Optional[Any] , A : List[str] , A : Optional[Any] , A : int , A : Any , A : List[str] , A : Optional[int] ): if latents is None: _UpperCAmelCase : Any = randn_tensor(A , generator=A , device=A , dtype=A ) else: if latents.shape != shape: raise ValueError(F"""Unexpected latents shape, got {latents.shape}, expected {shape}""" ) _UpperCAmelCase : List[str] = latents.to(A ) _UpperCAmelCase : List[Any] = latents * scheduler.init_noise_sigma return latents def _A ( self : str , A : Optional[Any] , A : List[Any] , A : str , A : Any , A : int=None , ): _UpperCAmelCase : str = len(A ) if isinstance(A , A ) else 1 # get prompt text embeddings _UpperCAmelCase : Tuple = self.tokenizer( A , padding="max_length" , truncation=A , max_length=77 , return_attention_mask=A , add_special_tokens=A , return_tensors="pt" , ) _UpperCAmelCase : int = text_inputs.input_ids _UpperCAmelCase : Dict = self.tokenizer(A , padding="longest" , return_tensors="pt" ).input_ids if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(A , A ): _UpperCAmelCase : List[str] = self.tokenizer.batch_decode(untruncated_ids[:, self.tokenizer.model_max_length - 1 : -1] ) logger.warning( "The following part of your input was truncated because CLIP can only handle sequences up to" F""" {self.tokenizer.model_max_length} tokens: {removed_text}""" ) _UpperCAmelCase : Optional[Any] = text_input_ids.to(A ) _UpperCAmelCase : Tuple = text_inputs.attention_mask.to(A ) _UpperCAmelCase , _UpperCAmelCase : List[Any] = self.text_encoder( input_ids=A , attention_mask=A ) _UpperCAmelCase : Optional[int] = prompt_embeds.repeat_interleave(A , dim=0 ) _UpperCAmelCase : Union[str, Any] = text_encoder_hidden_states.repeat_interleave(A , dim=0 ) _UpperCAmelCase : Optional[Any] = text_mask.repeat_interleave(A , dim=0 ) if do_classifier_free_guidance: _UpperCAmelCase : List[str] if negative_prompt is None: _UpperCAmelCase : Dict = [""] * batch_size elif type(A ) is not type(A ): raise TypeError( F"""`negative_prompt` should be the same type to `prompt`, but got {type(A )} !=""" F""" {type(A )}.""" ) elif isinstance(A , A ): _UpperCAmelCase : Dict = [negative_prompt] elif batch_size != len(A ): raise ValueError( F"""`negative_prompt`: {negative_prompt} has batch size {len(A )}, but `prompt`:""" F""" {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches""" " the batch size of `prompt`." ) else: _UpperCAmelCase : Tuple = negative_prompt _UpperCAmelCase : Optional[Any] = self.tokenizer( A , padding="max_length" , max_length=77 , truncation=A , return_attention_mask=A , add_special_tokens=A , return_tensors="pt" , ) _UpperCAmelCase : Union[str, Any] = uncond_input.input_ids.to(A ) _UpperCAmelCase : List[Any] = uncond_input.attention_mask.to(A ) _UpperCAmelCase , _UpperCAmelCase : int = self.text_encoder( input_ids=A , attention_mask=A ) # duplicate unconditional embeddings for each generation per prompt, using mps friendly method _UpperCAmelCase : Optional[int] = negative_prompt_embeds.shape[1] _UpperCAmelCase : str = negative_prompt_embeds.repeat(1 , A ) _UpperCAmelCase : str = negative_prompt_embeds.view(batch_size * num_images_per_prompt , A ) _UpperCAmelCase : Optional[int] = uncond_text_encoder_hidden_states.shape[1] _UpperCAmelCase : Union[str, Any] = uncond_text_encoder_hidden_states.repeat(1 , A , 1 ) _UpperCAmelCase : List[Any] = uncond_text_encoder_hidden_states.view( batch_size * num_images_per_prompt , A , -1 ) _UpperCAmelCase : Union[str, Any] = uncond_text_mask.repeat_interleave(A , dim=0 ) # done duplicates # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes _UpperCAmelCase : List[Any] = torch.cat([negative_prompt_embeds, prompt_embeds] ) _UpperCAmelCase : str = torch.cat([uncond_text_encoder_hidden_states, text_encoder_hidden_states] ) _UpperCAmelCase : Tuple = torch.cat([uncond_text_mask, text_mask] ) return prompt_embeds, text_encoder_hidden_states, text_mask def _A ( self : List[Any] , A : Union[str, Any]=0 ): if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError("Please install accelerate via `pip install accelerate`" ) _UpperCAmelCase : str = torch.device(F"""cuda:{gpu_id}""" ) _UpperCAmelCase : Optional[Any] = [ self.unet, self.text_encoder, self.movq, ] for cpu_offloaded_model in models: if cpu_offloaded_model is not None: cpu_offload(A , A ) def _A ( self : str , A : Any=0 ): if is_accelerate_available() and is_accelerate_version(">=" , "0.17.0.dev0" ): from accelerate import cpu_offload_with_hook else: raise ImportError("`enable_model_cpu_offload` requires `accelerate v0.17.0` or higher." ) _UpperCAmelCase : Tuple = torch.device(F"""cuda:{gpu_id}""" ) if self.device.type != "cpu": self.to("cpu" , silence_dtype_warnings=A ) torch.cuda.empty_cache() # otherwise we don't see the memory savings (but they probably exist) _UpperCAmelCase : List[str] = None for cpu_offloaded_model in [self.text_encoder, self.unet, self.movq]: _UpperCAmelCase , _UpperCAmelCase : int = cpu_offload_with_hook(A , A , prev_module_hook=A ) if self.safety_checker is not None: _UpperCAmelCase , _UpperCAmelCase : Any = cpu_offload_with_hook(self.safety_checker , A , prev_module_hook=A ) # We'll offload the last model manually. _UpperCAmelCase : Optional[int] = hook @property # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device def _A ( self : Optional[int] ): if not hasattr(self.unet , "_hf_hook" ): return self.device for module in self.unet.modules(): if ( hasattr(A , "_hf_hook" ) and hasattr(module._hf_hook , "execution_device" ) and module._hf_hook.execution_device is not None ): return torch.device(module._hf_hook.execution_device ) return self.device @torch.no_grad() @replace_example_docstring(A ) def __call__( self : str , A : Union[str, List[str]] , A : Union[torch.FloatTensor, List[torch.FloatTensor]] , A : Union[torch.FloatTensor, List[torch.FloatTensor]] , A : Optional[Union[str, List[str]]] = None , A : int = 512 , A : int = 512 , A : int = 100 , A : float = 4.0 , A : int = 1 , A : Optional[Union[torch.Generator, List[torch.Generator]]] = None , A : Optional[torch.FloatTensor] = None , A : Optional[str] = "pil" , A : bool = True , ): if isinstance(A , A ): _UpperCAmelCase : Dict = 1 elif isinstance(A , A ): _UpperCAmelCase : Tuple = len(A ) else: raise ValueError(F"""`prompt` has to be of type `str` or `list` but is {type(A )}""" ) _UpperCAmelCase : Dict = self._execution_device _UpperCAmelCase : Optional[int] = batch_size * num_images_per_prompt _UpperCAmelCase : Optional[int] = guidance_scale > 1.0 _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase : Tuple = self._encode_prompt( A , A , A , A , A ) if isinstance(A , A ): _UpperCAmelCase : Optional[int] = torch.cat(A , dim=0 ) if isinstance(A , A ): _UpperCAmelCase : Tuple = torch.cat(A , dim=0 ) if do_classifier_free_guidance: _UpperCAmelCase : Tuple = image_embeds.repeat_interleave(A , dim=0 ) _UpperCAmelCase : int = negative_image_embeds.repeat_interleave(A , dim=0 ) _UpperCAmelCase : List[str] = torch.cat([negative_image_embeds, image_embeds] , dim=0 ).to( dtype=prompt_embeds.dtype , device=A ) self.scheduler.set_timesteps(A , device=A ) _UpperCAmelCase : int = self.scheduler.timesteps _UpperCAmelCase : Tuple = self.unet.config.in_channels _UpperCAmelCase , _UpperCAmelCase : Union[str, Any] = get_new_h_w(A , A , self.movq_scale_factor ) # create initial latent _UpperCAmelCase : Tuple = self.prepare_latents( (batch_size, num_channels_latents, height, width) , text_encoder_hidden_states.dtype , A , A , A , self.scheduler , ) for i, t in enumerate(self.progress_bar(A ) ): # expand the latents if we are doing classifier free guidance _UpperCAmelCase : Tuple = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents _UpperCAmelCase : List[Any] = {"text_embeds": prompt_embeds, "image_embeds": image_embeds} _UpperCAmelCase : Optional[Any] = self.unet( sample=A , timestep=A , encoder_hidden_states=A , added_cond_kwargs=A , return_dict=A , )[0] if do_classifier_free_guidance: _UpperCAmelCase , _UpperCAmelCase : Union[str, Any] = noise_pred.split(latents.shape[1] , dim=1 ) _UpperCAmelCase , _UpperCAmelCase : Optional[int] = noise_pred.chunk(2 ) _UpperCAmelCase , _UpperCAmelCase : Optional[Any] = variance_pred.chunk(2 ) _UpperCAmelCase : Optional[Any] = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) _UpperCAmelCase : Union[str, Any] = torch.cat([noise_pred, variance_pred_text] , dim=1 ) if not ( hasattr(self.scheduler.config , "variance_type" ) and self.scheduler.config.variance_type in ["learned", "learned_range"] ): _UpperCAmelCase , _UpperCAmelCase : str = noise_pred.split(latents.shape[1] , dim=1 ) # compute the previous noisy sample x_t -> x_t-1 _UpperCAmelCase : Optional[Any] = self.scheduler.step( A , A , A , generator=A , ).prev_sample # post-processing _UpperCAmelCase : Any = self.movq.decode(A , force_not_quantize=A )["sample"] if output_type not in ["pt", "np", "pil"]: raise ValueError(F"""Only the output types `pt`, `pil` and `np` are supported not output_type={output_type}""" ) if output_type in ["np", "pil"]: _UpperCAmelCase : Any = image * 0.5 + 0.5 _UpperCAmelCase : int = image.clamp(0 , 1 ) _UpperCAmelCase : List[str] = image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() if output_type == "pil": _UpperCAmelCase : Optional[int] = self.numpy_to_pil(A ) if not return_dict: return (image,) return ImagePipelineOutput(images=A )
31
'''simple docstring''' import numpy as np from matplotlib import pyplot as plt from sklearn.datasets import load_iris from sklearn.metrics import ConfusionMatrixDisplay from sklearn.model_selection import train_test_split from xgboost import XGBClassifier def UpperCamelCase_ ( _UpperCAmelCase : dict ) -> tuple: """simple docstring""" return (data["data"], data["target"]) def UpperCamelCase_ ( _UpperCAmelCase : np.ndarray , _UpperCAmelCase : np.ndarray ) -> XGBClassifier: """simple docstring""" _UpperCAmelCase : Any = XGBClassifier() classifier.fit(_UpperCAmelCase , _UpperCAmelCase ) return classifier def UpperCamelCase_ ( ) -> None: """simple docstring""" _UpperCAmelCase : List[str] = load_iris() _UpperCAmelCase , _UpperCAmelCase : Dict = data_handling(_UpperCAmelCase ) _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase : str = train_test_split( _UpperCAmelCase , _UpperCAmelCase , test_size=0.2_5 ) _UpperCAmelCase : Optional[Any] = iris["target_names"] # Create an XGBoost Classifier from the training data _UpperCAmelCase : Tuple = xgboost(_UpperCAmelCase , _UpperCAmelCase ) # Display the confusion matrix of the classifier with both training and test sets ConfusionMatrixDisplay.from_estimator( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , display_labels=_UpperCAmelCase , cmap="Blues" , normalize="true" , ) plt.title("Normalized Confusion Matrix - IRIS Dataset" ) plt.show() if __name__ == "__main__": import doctest doctest.testmod(verbose=True) main()
31
1
'''simple docstring''' import qiskit def UpperCamelCase_ ( _UpperCAmelCase : int , _UpperCAmelCase : int ) -> qiskit.result.counts.Counts: """simple docstring""" _UpperCAmelCase : Dict = qiskit.Aer.get_backend("aer_simulator" ) # Create a Quantum Circuit acting on the q register _UpperCAmelCase : List[Any] = qiskit.QuantumCircuit(_UpperCAmelCase , _UpperCAmelCase ) # Apply X (NOT) Gate to Qubits 0 & 1 circuit.x(0 ) circuit.x(1 ) # Map the quantum measurement to the classical bits circuit.measure([0, 1] , [0, 1] ) # Execute the circuit on the qasm simulator _UpperCAmelCase : int = qiskit.execute(_UpperCAmelCase , _UpperCAmelCase , shots=1_000 ) # Return the histogram data of the results of the experiment. return job.result().get_counts(_UpperCAmelCase ) if __name__ == "__main__": __SCREAMING_SNAKE_CASE : Dict = single_qubit_measure(2, 2) print(F'Total count for various states are: {counts}')
31
'''simple docstring''' import math import unittest from transformers import BioGptConfig, 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, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( BioGptForCausalLM, BioGptForSequenceClassification, BioGptForTokenClassification, BioGptModel, BioGptTokenizer, ) from transformers.models.biogpt.modeling_biogpt import BIOGPT_PRETRAINED_MODEL_ARCHIVE_LIST class lowerCamelCase_ : '''simple docstring''' def __init__( self : List[Any] , A : Dict , A : Optional[Any]=13 , A : Optional[Any]=7 , A : Union[str, Any]=True , A : Optional[Any]=True , A : int=False , A : str=True , A : Optional[Any]=99 , A : Union[str, Any]=32 , A : int=5 , A : Tuple=4 , A : Union[str, Any]=37 , A : Dict="gelu" , A : Union[str, Any]=0.1 , A : str=0.1 , A : Union[str, Any]=512 , A : int=16 , A : List[str]=2 , A : Tuple=0.02 , A : int=3 , A : List[str]=4 , A : str=None , ): _UpperCAmelCase : List[Any] = parent _UpperCAmelCase : Any = batch_size _UpperCAmelCase : int = seq_length _UpperCAmelCase : Union[str, Any] = is_training _UpperCAmelCase : Any = use_input_mask _UpperCAmelCase : Optional[Any] = use_token_type_ids _UpperCAmelCase : str = use_labels _UpperCAmelCase : Union[str, Any] = vocab_size _UpperCAmelCase : Tuple = hidden_size _UpperCAmelCase : Union[str, Any] = num_hidden_layers _UpperCAmelCase : Optional[Any] = num_attention_heads _UpperCAmelCase : Union[str, Any] = intermediate_size _UpperCAmelCase : Union[str, Any] = hidden_act _UpperCAmelCase : List[Any] = hidden_dropout_prob _UpperCAmelCase : List[Any] = attention_probs_dropout_prob _UpperCAmelCase : Optional[int] = max_position_embeddings _UpperCAmelCase : str = type_vocab_size _UpperCAmelCase : str = type_sequence_label_size _UpperCAmelCase : int = initializer_range _UpperCAmelCase : Optional[Any] = num_labels _UpperCAmelCase : List[str] = num_choices _UpperCAmelCase : List[str] = scope def _A ( self : Optional[int] ): _UpperCAmelCase : Dict = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _UpperCAmelCase : Union[str, Any] = None if self.use_input_mask: _UpperCAmelCase : List[Any] = random_attention_mask([self.batch_size, self.seq_length] ) _UpperCAmelCase : Any = None if self.use_token_type_ids: _UpperCAmelCase : List[str] = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) _UpperCAmelCase : Optional[int] = None _UpperCAmelCase : Any = None _UpperCAmelCase : Optional[int] = None if self.use_labels: _UpperCAmelCase : Optional[Any] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _UpperCAmelCase : Tuple = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) _UpperCAmelCase : List[str] = ids_tensor([self.batch_size] , self.num_choices ) _UpperCAmelCase : List[Any] = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def _A ( self : Dict ): return BioGptConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=A , initializer_range=self.initializer_range , ) def _A ( self : int , A : List[Any] , A : Any , A : int , A : Union[str, Any] , A : Dict , A : List[Any] , A : Dict ): _UpperCAmelCase : List[str] = BioGptModel(config=A ) model.to(A ) model.eval() _UpperCAmelCase : Tuple = model(A , attention_mask=A ) _UpperCAmelCase : int = model(A ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _A ( self : List[Any] , A : str , A : List[Any] , A : Dict , A : List[Any] , A : List[str] , A : Union[str, Any] , A : int , A : List[str] , A : Dict , ): _UpperCAmelCase : Optional[int] = BioGptForCausalLM(config=A ) model.to(A ) model.eval() _UpperCAmelCase : Optional[int] = model(A , attention_mask=A , token_type_ids=A , labels=A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def _A ( self : List[Any] , A : str , A : str , A : str , A : Any , A : List[str] , *A : Optional[int] ): _UpperCAmelCase : str = BioGptModel(config=A ) model.to(A ) model.eval() # create attention mask _UpperCAmelCase : List[Any] = torch.ones(input_ids.shape , dtype=torch.long , device=A ) _UpperCAmelCase : Optional[int] = self.seq_length // 2 _UpperCAmelCase : List[Any] = 0 # first forward pass _UpperCAmelCase , _UpperCAmelCase : List[str] = model(A , attention_mask=A ).to_tuple() # create hypothetical next token and extent to next_input_ids _UpperCAmelCase : List[str] = ids_tensor((self.batch_size, 1) , config.vocab_size ) # change a random masked slice from input_ids _UpperCAmelCase : List[str] = ids_tensor((1,) , A ).item() + 1 _UpperCAmelCase : str = ids_tensor((self.batch_size, 1) , config.vocab_size ).squeeze(-1 ) _UpperCAmelCase : Any = random_other_next_tokens # append to next input_ids and attn_mask _UpperCAmelCase : List[Any] = torch.cat([input_ids, next_tokens] , dim=-1 ) _UpperCAmelCase : Optional[int] = torch.cat( [attn_mask, torch.ones((attn_mask.shape[0], 1) , dtype=torch.long , device=A )] , dim=1 , ) # get two different outputs _UpperCAmelCase : List[Any] = model(A , attention_mask=A )["last_hidden_state"] _UpperCAmelCase : Optional[Any] = model(A , past_key_values=A , attention_mask=A )["last_hidden_state"] # select random slice _UpperCAmelCase : Any = ids_tensor((1,) , output_from_past.shape[-1] ).item() _UpperCAmelCase : Optional[Any] = output_from_no_past[:, -1, random_slice_idx].detach() _UpperCAmelCase : Any = output_from_past[:, 0, random_slice_idx].detach() # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(A , A , atol=1E-3 ) ) def _A ( self : int , A : Dict , A : str , A : Dict , A : Union[str, Any] , A : Any , *A : Union[str, Any] ): _UpperCAmelCase : Optional[Any] = BioGptModel(config=A ).to(A ).eval() _UpperCAmelCase : List[Any] = torch.ones(input_ids.shape , dtype=torch.long , device=A ) # first forward pass _UpperCAmelCase : Union[str, Any] = model(A , attention_mask=A , use_cache=A ) _UpperCAmelCase , _UpperCAmelCase : Dict = outputs.to_tuple() # create hypothetical multiple next token and extent to next_input_ids _UpperCAmelCase : str = ids_tensor((self.batch_size, 3) , config.vocab_size ) _UpperCAmelCase : Any = ids_tensor((self.batch_size, 3) , 2 ) # append to next input_ids and _UpperCAmelCase : Optional[Any] = torch.cat([input_ids, next_tokens] , dim=-1 ) _UpperCAmelCase : Dict = torch.cat([attention_mask, next_attn_mask] , dim=-1 ) _UpperCAmelCase : Any = model(A , attention_mask=A )["last_hidden_state"] _UpperCAmelCase : Dict = model(A , attention_mask=A , past_key_values=A )[ "last_hidden_state" ] # select random slice _UpperCAmelCase : Dict = ids_tensor((1,) , output_from_past.shape[-1] ).item() _UpperCAmelCase : Dict = output_from_no_past[:, -3:, random_slice_idx].detach() _UpperCAmelCase : 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(A , A , atol=1E-3 ) ) def _A ( self : Optional[Any] , A : Tuple , A : List[str] , A : Tuple , A : Dict , A : List[Any] , *A : Tuple , A : List[str]=False ): _UpperCAmelCase : Optional[int] = BioGptForCausalLM(A ) model.to(A ) if gradient_checkpointing: model.gradient_checkpointing_enable() _UpperCAmelCase : Union[str, Any] = model(A , labels=A ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) result.loss.backward() def _A ( self : Optional[Any] , A : Any , *A : Optional[Any] ): _UpperCAmelCase : Tuple = BioGptModel(A ) _UpperCAmelCase : int = model.config.initializer_range / math.sqrt(2 * model.config.num_hidden_layers ) for key in model.state_dict().keys(): if "c_proj" in key and "weight" in key: self.parent.assertLessEqual(abs(torch.std(model.state_dict()[key] ) - model_std ) , 0.001 ) self.parent.assertLessEqual(abs(torch.mean(model.state_dict()[key] ) - 0.0 ) , 0.01 ) def _A ( self : Optional[int] , A : Dict , A : Tuple , A : Optional[int] , A : int , A : List[str] , *A : Dict ): _UpperCAmelCase : Any = self.num_labels _UpperCAmelCase : Any = BioGptForTokenClassification(A ) model.to(A ) model.eval() _UpperCAmelCase : Optional[int] = model(A , attention_mask=A , token_type_ids=A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def _A ( self : int ): _UpperCAmelCase : Dict = self.prepare_config_and_inputs() ( ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ) : List[str] = config_and_inputs _UpperCAmelCase : Optional[int] = {"input_ids": input_ids, "attention_mask": input_mask} return config, inputs_dict @require_torch class lowerCamelCase_ (snake_case__ , snake_case__ , snake_case__ , unittest.TestCase ): '''simple docstring''' __UpperCamelCase: List[str] = ( (BioGptModel, BioGptForCausalLM, BioGptForSequenceClassification, BioGptForTokenClassification) if is_torch_available() else () ) __UpperCamelCase: List[str] = (BioGptForCausalLM,) if is_torch_available() else () __UpperCamelCase: str = ( { "feature-extraction": BioGptModel, "text-classification": BioGptForSequenceClassification, "text-generation": BioGptForCausalLM, "token-classification": BioGptForTokenClassification, "zero-shot": BioGptForSequenceClassification, } if is_torch_available() else {} ) __UpperCamelCase: Union[str, Any] = False def _A ( self : Optional[Any] ): _UpperCAmelCase : List[Any] = BioGptModelTester(self ) _UpperCAmelCase : str = ConfigTester(self , config_class=A , hidden_size=37 ) def _A ( self : Union[str, Any] ): self.config_tester.run_common_tests() def _A ( self : Any ): _UpperCAmelCase : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*A ) def _A ( self : Any ): _UpperCAmelCase : Optional[int] = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: _UpperCAmelCase : Tuple = type self.model_tester.create_and_check_model(*A ) def _A ( self : int ): _UpperCAmelCase : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_biogpt_model_attention_mask_past(*A ) def _A ( self : Union[str, Any] ): _UpperCAmelCase : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_forward_and_backwards(*A , gradient_checkpointing=A ) def _A ( self : Union[str, Any] ): _UpperCAmelCase : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_biogpt_model_past_large_inputs(*A ) def _A ( self : Dict ): _UpperCAmelCase : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_biogpt_weight_initialization(*A ) def _A ( self : Dict ): _UpperCAmelCase : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_biogpt_for_token_classification(*A ) @slow def _A ( self : List[str] ): _UpperCAmelCase : Optional[Any] = BioGptForCausalLM.from_pretrained("microsoft/biogpt" ) model.to(A ) _UpperCAmelCase : Tuple = BioGptTokenizer.from_pretrained("microsoft/biogpt" ) _UpperCAmelCase : str = "left" # Define PAD Token = EOS Token = 50256 _UpperCAmelCase : Any = tokenizer.eos_token _UpperCAmelCase : int = model.config.eos_token_id # use different length sentences to test batching _UpperCAmelCase : Any = [ "Hello, my dog is a little", "Today, I", ] _UpperCAmelCase : Tuple = tokenizer(A , return_tensors="pt" , padding=A ) _UpperCAmelCase : Optional[Any] = inputs["input_ids"].to(A ) _UpperCAmelCase : Any = model.generate( input_ids=A , attention_mask=inputs["attention_mask"].to(A ) , ) _UpperCAmelCase : int = tokenizer(sentences[0] , return_tensors="pt" ).input_ids.to(A ) _UpperCAmelCase : List[Any] = model.generate(input_ids=A ) _UpperCAmelCase : List[Any] = inputs_non_padded.shape[-1] - inputs["attention_mask"][-1].long().sum().cpu().item() _UpperCAmelCase : int = tokenizer(sentences[1] , return_tensors="pt" ).input_ids.to(A ) _UpperCAmelCase : int = model.generate(input_ids=A , max_length=model.config.max_length - num_paddings ) _UpperCAmelCase : Dict = tokenizer.batch_decode(A , skip_special_tokens=A ) _UpperCAmelCase : Any = tokenizer.decode(output_non_padded[0] , skip_special_tokens=A ) _UpperCAmelCase : Optional[int] = tokenizer.decode(output_padded[0] , skip_special_tokens=A ) _UpperCAmelCase : str = [ "Hello, my dog is a little bit bigger than a little bit.", "Today, I have a good idea of how to use the information", ] self.assertListEqual(A , A ) self.assertListEqual(A , [non_padded_sentence, padded_sentence] ) @slow def _A ( self : str ): for model_name in BIOGPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _UpperCAmelCase : Optional[Any] = BioGptModel.from_pretrained(A ) self.assertIsNotNone(A ) def _A ( self : List[str] ): _UpperCAmelCase , _UpperCAmelCase : int = self.model_tester.prepare_config_and_inputs_for_common() _UpperCAmelCase : str = 3 _UpperCAmelCase : List[str] = input_dict["input_ids"] _UpperCAmelCase : Dict = input_ids.ne(1 ).to(A ) _UpperCAmelCase : List[Any] = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) _UpperCAmelCase : List[str] = BioGptForSequenceClassification(A ) model.to(A ) model.eval() _UpperCAmelCase : List[str] = model(A , attention_mask=A , labels=A ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def _A ( self : int ): _UpperCAmelCase , _UpperCAmelCase : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() _UpperCAmelCase : int = 3 _UpperCAmelCase : Dict = "multi_label_classification" _UpperCAmelCase : Optional[Any] = input_dict["input_ids"] _UpperCAmelCase : Optional[int] = input_ids.ne(1 ).to(A ) _UpperCAmelCase : Tuple = ids_tensor( [self.model_tester.batch_size, config.num_labels] , self.model_tester.type_sequence_label_size ).to(torch.float ) _UpperCAmelCase : Optional[Any] = BioGptForSequenceClassification(A ) model.to(A ) model.eval() _UpperCAmelCase : Tuple = model(A , attention_mask=A , labels=A ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) @require_torch class lowerCamelCase_ (unittest.TestCase ): '''simple docstring''' @slow def _A ( self : List[Any] ): _UpperCAmelCase : Optional[Any] = BioGptForCausalLM.from_pretrained("microsoft/biogpt" ) _UpperCAmelCase : List[str] = torch.tensor([[2, 4805, 9, 656, 21]] ) _UpperCAmelCase : List[Any] = model(A )[0] _UpperCAmelCase : int = 42384 _UpperCAmelCase : int = torch.Size((1, 5, vocab_size) ) self.assertEqual(output.shape , A ) _UpperCAmelCase : Any = torch.tensor( [[[-9.5_236, -9.8_918, 10.4_557], [-11.0_469, -9.6_423, 8.1_022], [-8.8_664, -7.8_826, 5.5_325]]] ) self.assertTrue(torch.allclose(output[:, :3, :3] , A , atol=1E-4 ) ) @slow def _A ( self : Any ): _UpperCAmelCase : str = BioGptTokenizer.from_pretrained("microsoft/biogpt" ) _UpperCAmelCase : Tuple = BioGptForCausalLM.from_pretrained("microsoft/biogpt" ) model.to(A ) torch.manual_seed(0 ) _UpperCAmelCase : Optional[Any] = tokenizer("COVID-19 is" , return_tensors="pt" ).to(A ) _UpperCAmelCase : Dict = model.generate( **A , min_length=100 , max_length=1024 , num_beams=5 , early_stopping=A , ) _UpperCAmelCase : Optional[Any] = tokenizer.decode(output_ids[0] , skip_special_tokens=A ) _UpperCAmelCase : List[str] = ( "COVID-19 is a global pandemic caused by severe acute respiratory syndrome coronavirus 2 (SARS-CoV-2), the" " causative agent of coronavirus disease 2019 (COVID-19), which has spread to more than 200 countries and" " territories, including the United States (US), Canada, Australia, New Zealand, the United Kingdom (UK)," " and the United States of America (USA), as of March 11, 2020, with more than 800,000 confirmed cases and" " more than 800,000 deaths." ) self.assertEqual(A , A )
31
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_torch_available, ) __SCREAMING_SNAKE_CASE : List[str] = { """configuration_falcon""": ["""FALCON_PRETRAINED_CONFIG_ARCHIVE_MAP""", """FalconConfig"""], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __SCREAMING_SNAKE_CASE : Any = [ """FALCON_PRETRAINED_MODEL_ARCHIVE_LIST""", """FalconForCausalLM""", """FalconModel""", """FalconPreTrainedModel""", """FalconForSequenceClassification""", """FalconForTokenClassification""", """FalconForQuestionAnswering""", ] if TYPE_CHECKING: from .configuration_falcon import FALCON_PRETRAINED_CONFIG_ARCHIVE_MAP, FalconConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_falcon import ( FALCON_PRETRAINED_MODEL_ARCHIVE_LIST, FalconForCausalLM, FalconForQuestionAnswering, FalconForSequenceClassification, FalconForTokenClassification, FalconModel, FalconPreTrainedModel, ) else: import sys __SCREAMING_SNAKE_CASE : Tuple = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
31
'''simple docstring''' __SCREAMING_SNAKE_CASE : Dict = 8.3_1_4_4_6_2 # Unit - J mol-1 K-1 def UpperCamelCase_ ( _UpperCAmelCase : float , _UpperCAmelCase : float , _UpperCAmelCase : float ) -> float: """simple docstring""" if moles < 0 or kelvin < 0 or volume < 0: raise ValueError("Invalid inputs. Enter positive value." ) return moles * kelvin * UNIVERSAL_GAS_CONSTANT / volume def UpperCamelCase_ ( _UpperCAmelCase : float , _UpperCAmelCase : float , _UpperCAmelCase : float ) -> float: """simple docstring""" if moles < 0 or kelvin < 0 or pressure < 0: raise ValueError("Invalid inputs. Enter positive value." ) return moles * kelvin * UNIVERSAL_GAS_CONSTANT / pressure if __name__ == "__main__": from doctest import testmod testmod()
31
1
'''simple docstring''' def UpperCamelCase_ ( _UpperCAmelCase : int , _UpperCAmelCase : int ) -> int: """simple docstring""" _UpperCAmelCase : List[str] = 1 # To kept the Calculated Value # Since C(n, k) = C(n, n-k) if k > (n - k): _UpperCAmelCase : Any = n - k # Calculate C(n,k) for i in range(_UpperCAmelCase ): result *= n - i result //= i + 1 return result def UpperCamelCase_ ( _UpperCAmelCase : int ) -> int: """simple docstring""" return binomial_coefficient(2 * node_count , _UpperCAmelCase ) // (node_count + 1) def UpperCamelCase_ ( _UpperCAmelCase : int ) -> int: """simple docstring""" if n < 0: raise ValueError("factorial() not defined for negative values" ) _UpperCAmelCase : List[str] = 1 for i in range(1 , n + 1 ): result *= i return result def UpperCamelCase_ ( _UpperCAmelCase : int ) -> int: """simple docstring""" return catalan_number(_UpperCAmelCase ) * factorial(_UpperCAmelCase ) if __name__ == "__main__": __SCREAMING_SNAKE_CASE : Dict = int(input("""Enter the number of nodes: """).strip() or 0) if node_count <= 0: raise ValueError("""We need some nodes to work with.""") print( F'Given {node_count} nodes, there are {binary_tree_count(node_count)} ' F'binary trees and {catalan_number(node_count)} binary search trees.' )
31
'''simple docstring''' import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_pegasus import PegasusTokenizer else: __SCREAMING_SNAKE_CASE : Optional[Any] = None __SCREAMING_SNAKE_CASE : Dict = logging.get_logger(__name__) __SCREAMING_SNAKE_CASE : Union[str, Any] = """▁""" __SCREAMING_SNAKE_CASE : str = {"""vocab_file""": """spiece.model""", """tokenizer_file""": """tokenizer.json"""} __SCREAMING_SNAKE_CASE : int = { """vocab_file""": {"""google/pegasus-xsum""": """https://huggingface.co/google/pegasus-xsum/resolve/main/spiece.model"""}, """tokenizer_file""": { """google/pegasus-xsum""": """https://huggingface.co/google/pegasus-xsum/resolve/main/tokenizer.json""" }, } __SCREAMING_SNAKE_CASE : str = { """google/pegasus-xsum""": 512, } class lowerCamelCase_ (snake_case__ ): '''simple docstring''' __UpperCamelCase: Optional[int] = VOCAB_FILES_NAMES __UpperCamelCase: Dict = PRETRAINED_VOCAB_FILES_MAP __UpperCamelCase: List[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCamelCase: Optional[int] = PegasusTokenizer __UpperCamelCase: Optional[Any] = ["input_ids", "attention_mask"] def __init__( self : Dict , A : List[str]=None , A : Union[str, Any]=None , A : Optional[int]="<pad>" , A : Tuple="</s>" , A : Union[str, Any]="<unk>" , A : Union[str, Any]="<mask_2>" , A : Dict="<mask_1>" , A : Union[str, Any]=None , A : int=103 , **A : Optional[Any] , ): _UpperCAmelCase : Dict = offset if additional_special_tokens is not None: if not isinstance(A , A ): raise TypeError( F"""additional_special_tokens should be of type {type(A )}, but is""" F""" {type(A )}""" ) _UpperCAmelCase : Optional[int] = ( ([mask_token_sent] + additional_special_tokens) if mask_token_sent not in additional_special_tokens and mask_token_sent is not None else additional_special_tokens ) # fill additional tokens with ..., <unk_token_102> in case not all additional tokens are already taken additional_special_tokens_extended += [ F"""<unk_{i}>""" for i in range(len(A ) , self.offset - 1 ) ] if len(set(A ) ) != len(A ): raise ValueError( "Please make sure that the provided additional_special_tokens do not contain an incorrectly" F""" shifted list of <unk_x> tokens. Found {additional_special_tokens_extended}.""" ) _UpperCAmelCase : Any = additional_special_tokens_extended else: _UpperCAmelCase : Dict = [mask_token_sent] if mask_token_sent is not None else [] additional_special_tokens += [F"""<unk_{i}>""" for i in range(2 , self.offset )] super().__init__( A , tokenizer_file=A , pad_token=A , eos_token=A , unk_token=A , mask_token=A , mask_token_sent=A , offset=A , additional_special_tokens=A , **A , ) _UpperCAmelCase : Optional[Any] = vocab_file _UpperCAmelCase : Optional[Any] = False if not self.vocab_file else True def _A ( self : List[str] , A : Optional[Any] ): _UpperCAmelCase : Any = set(self.all_special_ids ) # call it once instead of inside list comp all_special_ids.remove(self.unk_token_id ) # <unk> is only sometimes special if all_special_ids != set(range(len(self.additional_special_tokens ) + 3 ) ): raise ValueError( "There should be 3 special tokens: mask_token, pad_token, and eos_token +" F""" {len(self.additional_special_tokens )} additional_special_tokens, but got {all_special_ids}""" ) return [1 if x in all_special_ids else 0 for x in seq] def _A ( self : str , A : List , A : Optional[List] = None , A : bool = False ): if already_has_special_tokens: return self._special_token_mask(A ) elif token_ids_a is None: return self._special_token_mask(A ) + [1] else: return self._special_token_mask(token_ids_a + token_ids_a ) + [1] def _A ( self : Optional[int] , A : Union[str, Any] , A : int=None ): if token_ids_a is None: return token_ids_a + [self.eos_token_id] # We don't expect to process pairs, but leave the pair logic for API consistency return token_ids_a + token_ids_a + [self.eos_token_id] def _A ( self : Union[str, Any] , A : str , A : Optional[str] = None ): if not self.can_save_slow_tokenizer: raise ValueError( "Your fast tokenizer does not have the necessary information to save the vocabulary for a slow " "tokenizer." ) if not os.path.isdir(A ): logger.error(F"""Vocabulary path ({save_directory}) should be a directory""" ) return _UpperCAmelCase : List[Any] = os.path.join( A , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(A ): copyfile(self.vocab_file , A ) return (out_vocab_file,)
31
1
'''simple docstring''' def UpperCamelCase_ ( ) -> int: """simple docstring""" return 1 def UpperCamelCase_ ( _UpperCAmelCase : int ) -> int: """simple docstring""" return 0 if x < 0 else two_pence(x - 2 ) + one_pence() def UpperCamelCase_ ( _UpperCAmelCase : int ) -> int: """simple docstring""" return 0 if x < 0 else five_pence(x - 5 ) + two_pence(_UpperCAmelCase ) def UpperCamelCase_ ( _UpperCAmelCase : int ) -> int: """simple docstring""" return 0 if x < 0 else ten_pence(x - 10 ) + five_pence(_UpperCAmelCase ) def UpperCamelCase_ ( _UpperCAmelCase : int ) -> int: """simple docstring""" return 0 if x < 0 else twenty_pence(x - 20 ) + ten_pence(_UpperCAmelCase ) def UpperCamelCase_ ( _UpperCAmelCase : int ) -> int: """simple docstring""" return 0 if x < 0 else fifty_pence(x - 50 ) + twenty_pence(_UpperCAmelCase ) def UpperCamelCase_ ( _UpperCAmelCase : int ) -> int: """simple docstring""" return 0 if x < 0 else one_pound(x - 100 ) + fifty_pence(_UpperCAmelCase ) def UpperCamelCase_ ( _UpperCAmelCase : int ) -> int: """simple docstring""" return 0 if x < 0 else two_pound(x - 200 ) + one_pound(_UpperCAmelCase ) def UpperCamelCase_ ( _UpperCAmelCase : int = 200 ) -> int: """simple docstring""" return two_pound(_UpperCAmelCase ) if __name__ == "__main__": print(solution(int(input().strip())))
31
'''simple docstring''' import shutil import tempfile import unittest from transformers import ( SPIECE_UNDERLINE, AddedToken, BatchEncoding, NllbTokenizer, NllbTokenizerFast, is_torch_available, ) from transformers.testing_utils import ( get_tests_dir, nested_simplify, require_sentencepiece, require_tokenizers, require_torch, ) from ...test_tokenization_common import TokenizerTesterMixin __SCREAMING_SNAKE_CASE : Union[str, Any] = get_tests_dir("""fixtures/test_sentencepiece.model""") if is_torch_available(): from transformers.models.mam_aaa.modeling_mam_aaa import shift_tokens_right __SCREAMING_SNAKE_CASE : Optional[int] = 256_047 __SCREAMING_SNAKE_CASE : Optional[int] = 256_145 @require_sentencepiece @require_tokenizers class lowerCamelCase_ (snake_case__ , unittest.TestCase ): '''simple docstring''' __UpperCamelCase: int = NllbTokenizer __UpperCamelCase: Tuple = NllbTokenizerFast __UpperCamelCase: Union[str, Any] = True __UpperCamelCase: Dict = True __UpperCamelCase: Optional[Any] = {} def _A ( self : Union[str, Any] ): super().setUp() # We have a SentencePiece fixture for testing _UpperCAmelCase : Tuple = NllbTokenizer(A , keep_accents=A ) tokenizer.save_pretrained(self.tmpdirname ) def _A ( self : Dict ): _UpperCAmelCase : Tuple = NllbTokenizer(A , keep_accents=A ) _UpperCAmelCase : Optional[Any] = tokenizer.tokenize("This is a test" ) self.assertListEqual(A , ["▁This", "▁is", "▁a", "▁t", "est"] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(A ) , [value + tokenizer.fairseq_offset for value in [285, 46, 10, 170, 382]] , ) _UpperCAmelCase : List[str] = tokenizer.tokenize("I was born in 92000, and this is falsé." ) self.assertListEqual( A , [ SPIECE_UNDERLINE + "I", SPIECE_UNDERLINE + "was", SPIECE_UNDERLINE + "b", "or", "n", SPIECE_UNDERLINE + "in", SPIECE_UNDERLINE + "", "9", "2", "0", "0", "0", ",", SPIECE_UNDERLINE + "and", SPIECE_UNDERLINE + "this", SPIECE_UNDERLINE + "is", SPIECE_UNDERLINE + "f", "al", "s", "é", ".", ] , ) _UpperCAmelCase : Optional[Any] = tokenizer.convert_tokens_to_ids(A ) self.assertListEqual( A , [ value + tokenizer.fairseq_offset for value in [8, 21, 84, 55, 24, 19, 7, 2, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 2, 4] ] , ) _UpperCAmelCase : Union[str, Any] = tokenizer.convert_ids_to_tokens(A ) self.assertListEqual( A , [ SPIECE_UNDERLINE + "I", SPIECE_UNDERLINE + "was", SPIECE_UNDERLINE + "b", "or", "n", SPIECE_UNDERLINE + "in", SPIECE_UNDERLINE + "", "<unk>", "2", "0", "0", "0", ",", SPIECE_UNDERLINE + "and", SPIECE_UNDERLINE + "this", SPIECE_UNDERLINE + "is", SPIECE_UNDERLINE + "f", "al", "s", "<unk>", ".", ] , ) def _A ( self : List[Any] ): _UpperCAmelCase : Any = (self.rust_tokenizer_class, "hf-internal-testing/tiny-random-nllb", {}) for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F"""{tokenizer.__class__.__name__} ({pretrained_name})""" ): _UpperCAmelCase : Dict = self.rust_tokenizer_class.from_pretrained(A , **A ) _UpperCAmelCase : str = self.tokenizer_class.from_pretrained(A , **A ) _UpperCAmelCase : Optional[int] = tempfile.mkdtemp() _UpperCAmelCase : Dict = tokenizer_r.save_pretrained(A ) _UpperCAmelCase : Dict = tokenizer_p.save_pretrained(A ) # Checks it save with the same files + the tokenizer.json file for the fast one self.assertTrue(any("tokenizer.json" in f for f in tokenizer_r_files ) ) _UpperCAmelCase : Optional[int] = tuple(f for f in tokenizer_r_files if "tokenizer.json" not in f ) self.assertSequenceEqual(A , A ) # Checks everything loads correctly in the same way _UpperCAmelCase : List[Any] = tokenizer_r.from_pretrained(A ) _UpperCAmelCase : List[str] = tokenizer_p.from_pretrained(A ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(A , A ) ) shutil.rmtree(A ) # Save tokenizer rust, legacy_format=True _UpperCAmelCase : Optional[Any] = tempfile.mkdtemp() _UpperCAmelCase : str = tokenizer_r.save_pretrained(A , legacy_format=A ) _UpperCAmelCase : str = tokenizer_p.save_pretrained(A ) # Checks it save with the same files self.assertSequenceEqual(A , A ) # Checks everything loads correctly in the same way _UpperCAmelCase : Optional[int] = tokenizer_r.from_pretrained(A ) _UpperCAmelCase : Dict = tokenizer_p.from_pretrained(A ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(A , A ) ) shutil.rmtree(A ) # Save tokenizer rust, legacy_format=False _UpperCAmelCase : Optional[int] = tempfile.mkdtemp() _UpperCAmelCase : Optional[int] = tokenizer_r.save_pretrained(A , legacy_format=A ) _UpperCAmelCase : Dict = tokenizer_p.save_pretrained(A ) # Checks it saved the tokenizer.json file self.assertTrue(any("tokenizer.json" in f for f in tokenizer_r_files ) ) # Checks everything loads correctly in the same way _UpperCAmelCase : List[Any] = tokenizer_r.from_pretrained(A ) _UpperCAmelCase : Optional[int] = tokenizer_p.from_pretrained(A ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(A , A ) ) shutil.rmtree(A ) @require_torch def _A ( self : Tuple ): if not self.test_seqaseq: return _UpperCAmelCase : Union[str, Any] = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(F"""{tokenizer.__class__.__name__}""" ): # Longer text that will definitely require truncation. _UpperCAmelCase : Optional[Any] = [ " UN Chief Says There Is No Military Solution in Syria", " Secretary-General Ban Ki-moon says his response to Russia's stepped up military support for" " Syria is that 'there is no military solution' to the nearly five-year conflict and more weapons" " will only worsen the violence and misery for millions of people.", ] _UpperCAmelCase : Optional[Any] = [ "Şeful ONU declară că nu există o soluţie militară în Siria", "Secretarul General Ban Ki-moon declară că răspunsul său la intensificarea sprijinului militar al" " Rusiei pentru Siria este că \"nu există o soluţie militară\" la conflictul de aproape cinci ani şi" " că noi arme nu vor face decât să înrăutăţească violenţele şi mizeria pentru milioane de oameni.", ] try: _UpperCAmelCase : Optional[int] = tokenizer.prepare_seqaseq_batch( src_texts=A , tgt_texts=A , max_length=3 , max_target_length=10 , return_tensors="pt" , src_lang="eng_Latn" , tgt_lang="ron_Latn" , ) except NotImplementedError: return self.assertEqual(batch.input_ids.shape[1] , 3 ) self.assertEqual(batch.labels.shape[1] , 10 ) # max_target_length will default to max_length if not specified _UpperCAmelCase : Tuple = tokenizer.prepare_seqaseq_batch( A , tgt_texts=A , max_length=3 , return_tensors="pt" ) self.assertEqual(batch.input_ids.shape[1] , 3 ) self.assertEqual(batch.labels.shape[1] , 3 ) _UpperCAmelCase : Union[str, Any] = tokenizer.prepare_seqaseq_batch( src_texts=A , max_length=3 , max_target_length=10 , return_tensors="pt" ) self.assertEqual(batch_encoder_only.input_ids.shape[1] , 3 ) self.assertEqual(batch_encoder_only.attention_mask.shape[1] , 3 ) self.assertNotIn("decoder_input_ids" , A ) @unittest.skip("Unfortunately way too slow to build a BPE with SentencePiece." ) def _A ( self : List[Any] ): pass def _A ( self : Union[str, Any] ): for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F"""{tokenizer.__class__.__name__} ({pretrained_name})""" ): _UpperCAmelCase : Any = [AddedToken("<special>" , lstrip=A )] _UpperCAmelCase : List[Any] = self.rust_tokenizer_class.from_pretrained( A , additional_special_tokens=A , **A ) _UpperCAmelCase : Dict = tokenizer_r.encode("Hey this is a <special> token" ) _UpperCAmelCase : Any = tokenizer_r.encode("<special>" , add_special_tokens=A )[0] self.assertTrue(special_token_id in r_output ) if self.test_slow_tokenizer: _UpperCAmelCase : Dict = self.rust_tokenizer_class.from_pretrained( A , additional_special_tokens=A , **A , ) _UpperCAmelCase : Optional[int] = self.tokenizer_class.from_pretrained( A , additional_special_tokens=A , **A ) _UpperCAmelCase : Union[str, Any] = tokenizer_p.encode("Hey this is a <special> token" ) _UpperCAmelCase : Any = tokenizer_cr.encode("Hey this is a <special> token" ) self.assertEqual(A , A ) self.assertEqual(A , A ) self.assertTrue(special_token_id in p_output ) self.assertTrue(special_token_id in cr_output ) @require_torch @require_sentencepiece @require_tokenizers class lowerCamelCase_ (unittest.TestCase ): '''simple docstring''' __UpperCamelCase: Dict = "facebook/nllb-200-distilled-600M" __UpperCamelCase: Optional[int] = [ " UN Chief Says There Is No Military Solution in Syria", " Secretary-General Ban Ki-moon says his response to Russia's stepped up military support for Syria is that \"there is no military solution\" to the nearly five-year conflict and more weapons will only worsen the violence and misery for millions of people.", ] __UpperCamelCase: str = [ "Şeful ONU declară că nu există o soluţie militară în Siria", "Secretarul General Ban Ki-moon declară că răspunsul său la intensificarea sprijinului militar al Rusiei" " pentru Siria este că \"nu există o soluţie militară\" la conflictul de aproape cinci ani şi că noi arme nu vor" " face decât să înrăutăţească violenţele şi mizeria pentru milioane de oameni.", ] __UpperCamelCase: str = [ 2_5_6_0_4_7, 1_6_2_9_7, 1_3_4_4_0_8, 8_1_6_5, 2_4_8_0_6_6, 1_4_7_3_4, 9_5_0, 1_1_3_5, 1_0_5_7_2_1, 3_5_7_3, 8_3, 2_7_3_5_2, 1_0_8, 4_9_4_8_6, 2, ] @classmethod def _A ( cls : int ): _UpperCAmelCase : NllbTokenizer = NllbTokenizer.from_pretrained( cls.checkpoint_name , src_lang="eng_Latn" , tgt_lang="ron_Latn" ) _UpperCAmelCase : Union[str, Any] = 1 return cls def _A ( self : Any ): self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["ace_Arab"] , 256001 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["ace_Latn"] , 256002 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["fra_Latn"] , 256057 ) def _A ( self : Union[str, Any] ): _UpperCAmelCase : Optional[int] = self.tokenizer.batch_encode_plus(self.src_text ).input_ids[0] self.assertListEqual(self.expected_src_tokens , A ) def _A ( self : Tuple ): self.assertIn(A , self.tokenizer.all_special_ids ) # fmt: off _UpperCAmelCase : List[Any] = [RO_CODE, 4254, 98068, 112923, 39072, 3909, 713, 102767, 26, 17314, 35642, 14683, 33118, 2022, 66987, 2, 256047] # fmt: on _UpperCAmelCase : Tuple = self.tokenizer.decode(A , skip_special_tokens=A ) _UpperCAmelCase : Optional[Any] = self.tokenizer.decode(generated_ids[1:] , skip_special_tokens=A ) self.assertEqual(A , A ) self.assertNotIn(self.tokenizer.eos_token , A ) def _A ( self : Optional[int] ): _UpperCAmelCase : List[Any] = ["this is gunna be a long sentence " * 20] assert isinstance(src_text[0] , A ) _UpperCAmelCase : Dict = 10 _UpperCAmelCase : Tuple = self.tokenizer(A , max_length=A , truncation=A ).input_ids[0] self.assertEqual(ids[-1] , 2 ) self.assertEqual(ids[0] , A ) self.assertEqual(len(A ) , A ) def _A ( self : Dict ): self.assertListEqual(self.tokenizer.convert_tokens_to_ids(["<mask>", "ar_AR"] ) , [256203, 3] ) def _A ( self : Optional[Any] ): _UpperCAmelCase : Dict = tempfile.mkdtemp() _UpperCAmelCase : str = self.tokenizer.fairseq_tokens_to_ids self.tokenizer.save_pretrained(A ) _UpperCAmelCase : Tuple = NllbTokenizer.from_pretrained(A ) self.assertDictEqual(new_tok.fairseq_tokens_to_ids , A ) @require_torch def _A ( self : Dict ): _UpperCAmelCase : List[str] = self.tokenizer( self.src_text , text_target=self.tgt_text , padding=A , truncation=A , max_length=len(self.expected_src_tokens ) , return_tensors="pt" , ) _UpperCAmelCase : Tuple = shift_tokens_right( batch["labels"] , self.tokenizer.pad_token_id , self.tokenizer.lang_code_to_id["ron_Latn"] ) self.assertIsInstance(A , A ) self.assertEqual((2, 15) , batch.input_ids.shape ) self.assertEqual((2, 15) , batch.attention_mask.shape ) _UpperCAmelCase : Dict = batch.input_ids.tolist()[0] self.assertListEqual(self.expected_src_tokens , A ) self.assertEqual(A , batch.decoder_input_ids[0, 0] ) # EOS # Test that special tokens are reset self.assertEqual(self.tokenizer.prefix_tokens , [EN_CODE] ) self.assertEqual(self.tokenizer.suffix_tokens , [self.tokenizer.eos_token_id] ) def _A ( self : str ): _UpperCAmelCase : Optional[Any] = self.tokenizer(self.src_text , padding=A , truncation=A , max_length=3 , return_tensors="pt" ) _UpperCAmelCase : Dict = self.tokenizer( text_target=self.tgt_text , padding=A , truncation=A , max_length=10 , return_tensors="pt" ) _UpperCAmelCase : List[Any] = targets["input_ids"] _UpperCAmelCase : Union[str, Any] = shift_tokens_right( A , self.tokenizer.pad_token_id , decoder_start_token_id=self.tokenizer.lang_code_to_id[self.tokenizer.tgt_lang] , ) self.assertEqual(batch.input_ids.shape[1] , 3 ) self.assertEqual(batch.decoder_input_ids.shape[1] , 10 ) @require_torch def _A ( self : List[Any] ): _UpperCAmelCase : str = self.tokenizer._build_translation_inputs( "A test" , return_tensors="pt" , src_lang="eng_Latn" , tgt_lang="fra_Latn" ) self.assertEqual( nested_simplify(A ) , { # A, test, EOS, en_XX "input_ids": [[256047, 70, 7356, 2]], "attention_mask": [[1, 1, 1, 1]], # ar_AR "forced_bos_token_id": 256057, } , ) @require_torch def _A ( self : Any ): _UpperCAmelCase : Dict = True _UpperCAmelCase : Any = self.tokenizer( "UN Chief says there is no military solution in Syria" , src_lang="eng_Latn" , tgt_lang="fra_Latn" ) self.assertEqual( inputs.input_ids , [16297, 134408, 25653, 6370, 248, 254, 103929, 94995, 108, 49486, 2, 256047] ) _UpperCAmelCase : Optional[int] = False _UpperCAmelCase : str = self.tokenizer( "UN Chief says there is no military solution in Syria" , src_lang="eng_Latn" , tgt_lang="fra_Latn" ) self.assertEqual( inputs.input_ids , [256047, 16297, 134408, 25653, 6370, 248, 254, 103929, 94995, 108, 49486, 2] )
31
1
'''simple docstring''' from ...configuration_utils import PretrainedConfig class lowerCamelCase_ (snake_case__ ): '''simple docstring''' __UpperCamelCase: Dict = "bert-generation" def __init__( self : str , A : str=50358 , A : int=1024 , A : Optional[Any]=24 , A : Optional[int]=16 , A : str=4096 , A : Tuple="gelu" , A : str=0.1 , A : Dict=0.1 , A : Tuple=512 , A : Tuple=0.02 , A : Optional[int]=1E-12 , A : Union[str, Any]=0 , A : Any=2 , A : Dict=1 , A : Tuple="absolute" , A : List[Any]=True , **A : List[Any] , ): super().__init__(pad_token_id=A , bos_token_id=A , eos_token_id=A , **A ) _UpperCAmelCase : Optional[int] = vocab_size _UpperCAmelCase : List[str] = hidden_size _UpperCAmelCase : int = num_hidden_layers _UpperCAmelCase : List[str] = num_attention_heads _UpperCAmelCase : Optional[Any] = hidden_act _UpperCAmelCase : Tuple = intermediate_size _UpperCAmelCase : Optional[Any] = hidden_dropout_prob _UpperCAmelCase : Any = attention_probs_dropout_prob _UpperCAmelCase : List[str] = max_position_embeddings _UpperCAmelCase : str = initializer_range _UpperCAmelCase : List[Any] = layer_norm_eps _UpperCAmelCase : Optional[int] = position_embedding_type _UpperCAmelCase : int = use_cache
31
'''simple docstring''' def UpperCamelCase_ ( _UpperCAmelCase : list ) -> list: """simple docstring""" _UpperCAmelCase : List[Any] = len(_UpperCAmelCase ) for _ in range(_UpperCAmelCase ): for i in range(_ % 2 , arr_size - 1 , 2 ): if arr[i + 1] < arr[i]: _UpperCAmelCase , _UpperCAmelCase : int = arr[i + 1], arr[i] return arr if __name__ == "__main__": __SCREAMING_SNAKE_CASE : Optional[Any] = list(range(10, 0, -1)) print(F'Original: {arr}. Sorted: {odd_even_transposition(arr)}')
31
1
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging __SCREAMING_SNAKE_CASE : str = logging.get_logger(__name__) __SCREAMING_SNAKE_CASE : Any = { """studio-ousia/luke-base""": """https://huggingface.co/studio-ousia/luke-base/resolve/main/config.json""", """studio-ousia/luke-large""": """https://huggingface.co/studio-ousia/luke-large/resolve/main/config.json""", } class lowerCamelCase_ (snake_case__ ): '''simple docstring''' __UpperCamelCase: Tuple = "luke" def __init__( self : Optional[int] , A : Tuple=50267 , A : Optional[Any]=500000 , A : Dict=768 , A : List[Any]=256 , A : List[Any]=12 , A : List[Any]=12 , A : List[Any]=3072 , A : int="gelu" , A : Optional[int]=0.1 , A : List[Any]=0.1 , A : Dict=512 , A : Dict=2 , A : Optional[int]=0.02 , A : List[str]=1E-12 , A : List[str]=True , A : Dict=None , A : List[Any]=1 , A : Optional[int]=0 , A : Any=2 , **A : Tuple , ): super().__init__(pad_token_id=A , bos_token_id=A , eos_token_id=A , **A ) _UpperCAmelCase : Any = vocab_size _UpperCAmelCase : Optional[Any] = entity_vocab_size _UpperCAmelCase : int = hidden_size _UpperCAmelCase : Optional[Any] = entity_emb_size _UpperCAmelCase : Optional[Any] = num_hidden_layers _UpperCAmelCase : Any = num_attention_heads _UpperCAmelCase : int = hidden_act _UpperCAmelCase : str = intermediate_size _UpperCAmelCase : str = hidden_dropout_prob _UpperCAmelCase : Dict = attention_probs_dropout_prob _UpperCAmelCase : Union[str, Any] = max_position_embeddings _UpperCAmelCase : Tuple = type_vocab_size _UpperCAmelCase : Union[str, Any] = initializer_range _UpperCAmelCase : Dict = layer_norm_eps _UpperCAmelCase : Union[str, Any] = use_entity_aware_attention _UpperCAmelCase : int = classifier_dropout
31
'''simple docstring''' import os from typing import Any, Callable, Dict, List, Optional, Tuple, Union import torch from torch import nn from ...models.controlnet import ControlNetModel, ControlNetOutput from ...models.modeling_utils import ModelMixin from ...utils import logging __SCREAMING_SNAKE_CASE : str = logging.get_logger(__name__) class lowerCamelCase_ (snake_case__ ): '''simple docstring''' def __init__( self : Optional[int] , A : Union[List[ControlNetModel], Tuple[ControlNetModel]] ): super().__init__() _UpperCAmelCase : Optional[int] = nn.ModuleList(A ) def _A ( self : Dict , A : torch.FloatTensor , A : Union[torch.Tensor, float, int] , A : torch.Tensor , A : List[torch.tensor] , A : List[float] , A : Optional[torch.Tensor] = None , A : Optional[torch.Tensor] = None , A : Optional[torch.Tensor] = None , A : Optional[Dict[str, Any]] = None , A : bool = False , A : bool = True , ): for i, (image, scale, controlnet) in enumerate(zip(A , A , self.nets ) ): _UpperCAmelCase , _UpperCAmelCase : str = controlnet( A , A , A , A , A , A , A , A , A , A , A , ) # merge samples if i == 0: _UpperCAmelCase , _UpperCAmelCase : List[Any] = down_samples, mid_sample else: _UpperCAmelCase : Optional[int] = [ samples_prev + samples_curr for samples_prev, samples_curr in zip(A , A ) ] mid_block_res_sample += mid_sample return down_block_res_samples, mid_block_res_sample def _A ( self : List[str] , A : Union[str, os.PathLike] , A : bool = True , A : Callable = None , A : bool = False , A : Optional[str] = None , ): _UpperCAmelCase : str = 0 _UpperCAmelCase : str = save_directory for controlnet in self.nets: controlnet.save_pretrained( A , is_main_process=A , save_function=A , safe_serialization=A , variant=A , ) idx += 1 _UpperCAmelCase : Tuple = model_path_to_save + F"""_{idx}""" @classmethod def _A ( cls : int , A : Optional[Union[str, os.PathLike]] , **A : Tuple ): _UpperCAmelCase : str = 0 _UpperCAmelCase : int = [] # load controlnet and append to list until no controlnet directory exists anymore # first controlnet has to be saved under `./mydirectory/controlnet` to be compliant with `DiffusionPipeline.from_prertained` # second, third, ... controlnets have to be saved under `./mydirectory/controlnet_1`, `./mydirectory/controlnet_2`, ... _UpperCAmelCase : int = pretrained_model_path while os.path.isdir(A ): _UpperCAmelCase : List[str] = ControlNetModel.from_pretrained(A , **A ) controlnets.append(A ) idx += 1 _UpperCAmelCase : Dict = pretrained_model_path + F"""_{idx}""" logger.info(F"""{len(A )} controlnets loaded from {pretrained_model_path}.""" ) if len(A ) == 0: raise ValueError( F"""No ControlNets found under {os.path.dirname(A )}. Expected at least {pretrained_model_path + '_0'}.""" ) return cls(A )
31
1
'''simple docstring''' import math import numpy as np import qiskit from qiskit import Aer, ClassicalRegister, QuantumCircuit, QuantumRegister, execute def UpperCamelCase_ ( _UpperCAmelCase : int = 3 ) -> qiskit.result.counts.Counts: """simple docstring""" if isinstance(_UpperCAmelCase , _UpperCAmelCase ): raise TypeError("number of qubits must be a integer." ) if number_of_qubits <= 0: raise ValueError("number of qubits must be > 0." ) if math.floor(_UpperCAmelCase ) != number_of_qubits: raise ValueError("number of qubits must be exact integer." ) if number_of_qubits > 10: raise ValueError("number of qubits too large to simulate(>10)." ) _UpperCAmelCase : Dict = QuantumRegister(_UpperCAmelCase , "qr" ) _UpperCAmelCase : Any = ClassicalRegister(_UpperCAmelCase , "cr" ) _UpperCAmelCase : int = QuantumCircuit(_UpperCAmelCase , _UpperCAmelCase ) _UpperCAmelCase : List[Any] = number_of_qubits for i in range(_UpperCAmelCase ): quantum_circuit.h(number_of_qubits - i - 1 ) counter -= 1 for j in range(_UpperCAmelCase ): quantum_circuit.cp(np.pi / 2 ** (counter - j) , _UpperCAmelCase , _UpperCAmelCase ) for k in range(number_of_qubits // 2 ): quantum_circuit.swap(_UpperCAmelCase , number_of_qubits - k - 1 ) # measure all the qubits quantum_circuit.measure(_UpperCAmelCase , _UpperCAmelCase ) # simulate with 10000 shots _UpperCAmelCase : str = Aer.get_backend("qasm_simulator" ) _UpperCAmelCase : Dict = execute(_UpperCAmelCase , _UpperCAmelCase , shots=10_000 ) return job.result().get_counts(_UpperCAmelCase ) if __name__ == "__main__": print( F'Total count for quantum fourier transform state is: \ {quantum_fourier_transform(3)}' )
31
'''simple docstring''' import argparse import json import pickle from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import MaskFormerConfig, MaskFormerForInstanceSegmentation, MaskFormerImageProcessor, SwinConfig from transformers.utils import logging logging.set_verbosity_info() __SCREAMING_SNAKE_CASE : Union[str, Any] = logging.get_logger(__name__) def UpperCamelCase_ ( _UpperCAmelCase : str ) -> Optional[Any]: """simple docstring""" _UpperCAmelCase : int = SwinConfig.from_pretrained( "microsoft/swin-tiny-patch4-window7-224" , out_features=["stage1", "stage2", "stage3", "stage4"] ) _UpperCAmelCase : List[Any] = MaskFormerConfig(backbone_config=_UpperCAmelCase ) _UpperCAmelCase : Tuple = "huggingface/label-files" if "ade20k-full" in model_name: # this should be ok _UpperCAmelCase : Dict = 847 _UpperCAmelCase : Any = "maskformer-ade20k-full-id2label.json" elif "ade" in model_name: # this should be ok _UpperCAmelCase : Any = 150 _UpperCAmelCase : Any = "ade20k-id2label.json" elif "coco-stuff" in model_name: # this should be ok _UpperCAmelCase : Tuple = 171 _UpperCAmelCase : Union[str, Any] = "maskformer-coco-stuff-id2label.json" elif "coco" in model_name: # TODO _UpperCAmelCase : Any = 133 _UpperCAmelCase : int = "coco-panoptic-id2label.json" elif "cityscapes" in model_name: # this should be ok _UpperCAmelCase : Optional[int] = 19 _UpperCAmelCase : str = "cityscapes-id2label.json" elif "vistas" in model_name: # this should be ok _UpperCAmelCase : Optional[int] = 65 _UpperCAmelCase : Tuple = "mapillary-vistas-id2label.json" _UpperCAmelCase : List[Any] = json.load(open(hf_hub_download(_UpperCAmelCase , _UpperCAmelCase , repo_type="dataset" ) , "r" ) ) _UpperCAmelCase : Tuple = {int(_UpperCAmelCase ): v for k, v in idalabel.items()} return config def UpperCamelCase_ ( _UpperCAmelCase : Optional[int] ) -> List[Any]: """simple docstring""" _UpperCAmelCase : Dict = [] # stem # fmt: off rename_keys.append(("backbone.patch_embed.proj.weight", "model.pixel_level_module.encoder.model.embeddings.patch_embeddings.projection.weight") ) rename_keys.append(("backbone.patch_embed.proj.bias", "model.pixel_level_module.encoder.model.embeddings.patch_embeddings.projection.bias") ) rename_keys.append(("backbone.patch_embed.norm.weight", "model.pixel_level_module.encoder.model.embeddings.norm.weight") ) rename_keys.append(("backbone.patch_embed.norm.bias", "model.pixel_level_module.encoder.model.embeddings.norm.bias") ) # stages for i in range(len(config.backbone_config.depths ) ): for j in range(config.backbone_config.depths[i] ): rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.norm1.weight""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.layernorm_before.weight""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.norm1.bias""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.layernorm_before.bias""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.attn.relative_position_bias_table""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_bias_table""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.attn.relative_position_index""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_index""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.attn.proj.weight""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.weight""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.attn.proj.bias""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.bias""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.norm2.weight""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.layernorm_after.weight""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.norm2.bias""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.layernorm_after.bias""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.mlp.fc1.weight""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.weight""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.mlp.fc1.bias""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.bias""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.mlp.fc2.weight""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.output.dense.weight""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.mlp.fc2.bias""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.output.dense.bias""") ) if i < 3: rename_keys.append((F"""backbone.layers.{i}.downsample.reduction.weight""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.downsample.reduction.weight""") ) rename_keys.append((F"""backbone.layers.{i}.downsample.norm.weight""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.downsample.norm.weight""") ) rename_keys.append((F"""backbone.layers.{i}.downsample.norm.bias""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.downsample.norm.bias""") ) rename_keys.append((F"""backbone.norm{i}.weight""", F"""model.pixel_level_module.encoder.hidden_states_norms.{i}.weight""") ) rename_keys.append((F"""backbone.norm{i}.bias""", F"""model.pixel_level_module.encoder.hidden_states_norms.{i}.bias""") ) # FPN rename_keys.append(("sem_seg_head.layer_4.weight", "model.pixel_level_module.decoder.fpn.stem.0.weight") ) rename_keys.append(("sem_seg_head.layer_4.norm.weight", "model.pixel_level_module.decoder.fpn.stem.1.weight") ) rename_keys.append(("sem_seg_head.layer_4.norm.bias", "model.pixel_level_module.decoder.fpn.stem.1.bias") ) for source_index, target_index in zip(range(3 , 0 , -1 ) , range(0 , 3 ) ): rename_keys.append((F"""sem_seg_head.adapter_{source_index}.weight""", F"""model.pixel_level_module.decoder.fpn.layers.{target_index}.proj.0.weight""") ) rename_keys.append((F"""sem_seg_head.adapter_{source_index}.norm.weight""", F"""model.pixel_level_module.decoder.fpn.layers.{target_index}.proj.1.weight""") ) rename_keys.append((F"""sem_seg_head.adapter_{source_index}.norm.bias""", F"""model.pixel_level_module.decoder.fpn.layers.{target_index}.proj.1.bias""") ) rename_keys.append((F"""sem_seg_head.layer_{source_index}.weight""", F"""model.pixel_level_module.decoder.fpn.layers.{target_index}.block.0.weight""") ) rename_keys.append((F"""sem_seg_head.layer_{source_index}.norm.weight""", F"""model.pixel_level_module.decoder.fpn.layers.{target_index}.block.1.weight""") ) rename_keys.append((F"""sem_seg_head.layer_{source_index}.norm.bias""", F"""model.pixel_level_module.decoder.fpn.layers.{target_index}.block.1.bias""") ) rename_keys.append(("sem_seg_head.mask_features.weight", "model.pixel_level_module.decoder.mask_projection.weight") ) rename_keys.append(("sem_seg_head.mask_features.bias", "model.pixel_level_module.decoder.mask_projection.bias") ) # Transformer decoder for idx in range(config.decoder_config.decoder_layers ): # self-attention out projection rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.self_attn.out_proj.weight""", F"""model.transformer_module.decoder.layers.{idx}.self_attn.out_proj.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.self_attn.out_proj.bias""", F"""model.transformer_module.decoder.layers.{idx}.self_attn.out_proj.bias""") ) # cross-attention out projection rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.multihead_attn.out_proj.weight""", F"""model.transformer_module.decoder.layers.{idx}.encoder_attn.out_proj.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.multihead_attn.out_proj.bias""", F"""model.transformer_module.decoder.layers.{idx}.encoder_attn.out_proj.bias""") ) # MLP 1 rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.linear1.weight""", F"""model.transformer_module.decoder.layers.{idx}.fc1.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.linear1.bias""", F"""model.transformer_module.decoder.layers.{idx}.fc1.bias""") ) # MLP 2 rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.linear2.weight""", F"""model.transformer_module.decoder.layers.{idx}.fc2.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.linear2.bias""", F"""model.transformer_module.decoder.layers.{idx}.fc2.bias""") ) # layernorm 1 (self-attention layernorm) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm1.weight""", F"""model.transformer_module.decoder.layers.{idx}.self_attn_layer_norm.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm1.bias""", F"""model.transformer_module.decoder.layers.{idx}.self_attn_layer_norm.bias""") ) # layernorm 2 (cross-attention layernorm) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm2.weight""", F"""model.transformer_module.decoder.layers.{idx}.encoder_attn_layer_norm.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm2.bias""", F"""model.transformer_module.decoder.layers.{idx}.encoder_attn_layer_norm.bias""") ) # layernorm 3 (final layernorm) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm3.weight""", F"""model.transformer_module.decoder.layers.{idx}.final_layer_norm.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm3.bias""", F"""model.transformer_module.decoder.layers.{idx}.final_layer_norm.bias""") ) rename_keys.append(("sem_seg_head.predictor.transformer.decoder.norm.weight", "model.transformer_module.decoder.layernorm.weight") ) rename_keys.append(("sem_seg_head.predictor.transformer.decoder.norm.bias", "model.transformer_module.decoder.layernorm.bias") ) # heads on top rename_keys.append(("sem_seg_head.predictor.query_embed.weight", "model.transformer_module.queries_embedder.weight") ) rename_keys.append(("sem_seg_head.predictor.input_proj.weight", "model.transformer_module.input_projection.weight") ) rename_keys.append(("sem_seg_head.predictor.input_proj.bias", "model.transformer_module.input_projection.bias") ) rename_keys.append(("sem_seg_head.predictor.class_embed.weight", "class_predictor.weight") ) rename_keys.append(("sem_seg_head.predictor.class_embed.bias", "class_predictor.bias") ) for i in range(3 ): rename_keys.append((F"""sem_seg_head.predictor.mask_embed.layers.{i}.weight""", F"""mask_embedder.{i}.0.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.mask_embed.layers.{i}.bias""", F"""mask_embedder.{i}.0.bias""") ) # fmt: on return rename_keys def UpperCamelCase_ ( _UpperCAmelCase : int , _UpperCAmelCase : Tuple , _UpperCAmelCase : List[Any] ) -> List[str]: """simple docstring""" _UpperCAmelCase : Optional[int] = dct.pop(_UpperCAmelCase ) _UpperCAmelCase : List[str] = val def UpperCamelCase_ ( _UpperCAmelCase : List[Any] , _UpperCAmelCase : Tuple ) -> Optional[int]: """simple docstring""" _UpperCAmelCase : List[str] = [int(backbone_config.embed_dim * 2**i ) for i in range(len(backbone_config.depths ) )] for i in range(len(backbone_config.depths ) ): _UpperCAmelCase : Optional[int] = num_features[i] for j in range(backbone_config.depths[i] ): # fmt: off # read in weights + bias of input projection layer (in original implementation, this is a single matrix + bias) _UpperCAmelCase : Any = state_dict.pop(F"""backbone.layers.{i}.blocks.{j}.attn.qkv.weight""" ) _UpperCAmelCase : Optional[int] = state_dict.pop(F"""backbone.layers.{i}.blocks.{j}.attn.qkv.bias""" ) # next, add query, keys and values (in that order) to the state dict _UpperCAmelCase : List[str] = in_proj_weight[:dim, :] _UpperCAmelCase : Tuple = in_proj_bias[: dim] _UpperCAmelCase : List[Any] = in_proj_weight[ dim : dim * 2, : ] _UpperCAmelCase : List[str] = in_proj_bias[ dim : dim * 2 ] _UpperCAmelCase : Optional[Any] = in_proj_weight[ -dim :, : ] _UpperCAmelCase : Dict = in_proj_bias[-dim :] # fmt: on def UpperCamelCase_ ( _UpperCAmelCase : Dict , _UpperCAmelCase : str ) -> Dict: """simple docstring""" _UpperCAmelCase : Union[str, Any] = config.decoder_config.hidden_size for idx in range(config.decoder_config.decoder_layers ): # read in weights + bias of self-attention input projection layer (in the original implementation, this is a single matrix + bias) _UpperCAmelCase : Dict = state_dict.pop(F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.self_attn.in_proj_weight""" ) _UpperCAmelCase : Dict = state_dict.pop(F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.self_attn.in_proj_bias""" ) # next, add query, keys and values (in that order) to the state dict _UpperCAmelCase : int = in_proj_weight[: hidden_size, :] _UpperCAmelCase : Union[str, Any] = in_proj_bias[:config.hidden_size] _UpperCAmelCase : List[str] = in_proj_weight[hidden_size : hidden_size * 2, :] _UpperCAmelCase : List[str] = in_proj_bias[hidden_size : hidden_size * 2] _UpperCAmelCase : int = in_proj_weight[-hidden_size :, :] _UpperCAmelCase : Optional[Any] = in_proj_bias[-hidden_size :] # read in weights + bias of cross-attention input projection layer (in the original implementation, this is a single matrix + bias) _UpperCAmelCase : Optional[Any] = state_dict.pop(F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.multihead_attn.in_proj_weight""" ) _UpperCAmelCase : Tuple = state_dict.pop(F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.multihead_attn.in_proj_bias""" ) # next, add query, keys and values (in that order) to the state dict _UpperCAmelCase : Any = in_proj_weight[: hidden_size, :] _UpperCAmelCase : Tuple = in_proj_bias[:config.hidden_size] _UpperCAmelCase : Dict = in_proj_weight[hidden_size : hidden_size * 2, :] _UpperCAmelCase : Dict = in_proj_bias[hidden_size : hidden_size * 2] _UpperCAmelCase : Optional[int] = in_proj_weight[-hidden_size :, :] _UpperCAmelCase : Union[str, Any] = in_proj_bias[-hidden_size :] # fmt: on def UpperCamelCase_ ( ) -> torch.Tensor: """simple docstring""" _UpperCAmelCase : int = "http://images.cocodataset.org/val2017/000000039769.jpg" _UpperCAmelCase : Any = Image.open(requests.get(_UpperCAmelCase , stream=_UpperCAmelCase ).raw ) return im @torch.no_grad() def UpperCamelCase_ ( _UpperCAmelCase : str , _UpperCAmelCase : str , _UpperCAmelCase : str , _UpperCAmelCase : bool = False ) -> List[str]: """simple docstring""" _UpperCAmelCase : Optional[int] = get_maskformer_config(_UpperCAmelCase ) # load original state_dict with open(_UpperCAmelCase , "rb" ) as f: _UpperCAmelCase : Optional[int] = pickle.load(_UpperCAmelCase ) _UpperCAmelCase : Optional[int] = data["model"] # for name, param in state_dict.items(): # print(name, param.shape) # rename keys _UpperCAmelCase : Any = create_rename_keys(_UpperCAmelCase ) for src, dest in rename_keys: rename_key(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) read_in_swin_q_k_v(_UpperCAmelCase , config.backbone_config ) read_in_decoder_q_k_v(_UpperCAmelCase , _UpperCAmelCase ) # update to torch tensors for key, value in state_dict.items(): _UpperCAmelCase : Tuple = torch.from_numpy(_UpperCAmelCase ) # load 🤗 model _UpperCAmelCase : Union[str, Any] = MaskFormerForInstanceSegmentation(_UpperCAmelCase ) model.eval() for name, param in model.named_parameters(): print(_UpperCAmelCase , param.shape ) _UpperCAmelCase , _UpperCAmelCase : Any = model.load_state_dict(_UpperCAmelCase , strict=_UpperCAmelCase ) assert missing_keys == [ "model.pixel_level_module.encoder.model.layernorm.weight", "model.pixel_level_module.encoder.model.layernorm.bias", ] assert len(_UpperCAmelCase ) == 0, F"""Unexpected keys: {unexpected_keys}""" # verify results _UpperCAmelCase : Optional[int] = prepare_img() if "vistas" in model_name: _UpperCAmelCase : int = 65 elif "cityscapes" in model_name: _UpperCAmelCase : Tuple = 65_535 else: _UpperCAmelCase : Any = 255 _UpperCAmelCase : Optional[Any] = True if "ade" in model_name else False _UpperCAmelCase : Optional[int] = MaskFormerImageProcessor(ignore_index=_UpperCAmelCase , reduce_labels=_UpperCAmelCase ) _UpperCAmelCase : Optional[int] = image_processor(_UpperCAmelCase , return_tensors="pt" ) _UpperCAmelCase : List[Any] = model(**_UpperCAmelCase ) print("Logits:" , outputs.class_queries_logits[0, :3, :3] ) if model_name == "maskformer-swin-tiny-ade": _UpperCAmelCase : Tuple = torch.tensor( [[3.6_3_5_3, -4.4_7_7_0, -2.6_0_6_5], [0.5_0_8_1, -4.2_3_9_4, -3.5_3_4_3], [2.1_9_0_9, -5.0_3_5_3, -1.9_3_2_3]] ) assert torch.allclose(outputs.class_queries_logits[0, :3, :3] , _UpperCAmelCase , atol=1e-4 ) print("Looks ok!" ) if pytorch_dump_folder_path is not None: print(F"""Saving model and image processor to {pytorch_dump_folder_path}""" ) Path(_UpperCAmelCase ).mkdir(exist_ok=_UpperCAmelCase ) model.save_pretrained(_UpperCAmelCase ) image_processor.save_pretrained(_UpperCAmelCase ) if push_to_hub: print("Pushing model and image processor to the hub..." ) model.push_to_hub(F"""nielsr/{model_name}""" ) image_processor.push_to_hub(F"""nielsr/{model_name}""" ) if __name__ == "__main__": __SCREAMING_SNAKE_CASE : Optional[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( """--model_name""", default="""maskformer-swin-tiny-ade""", type=str, help=("""Name of the MaskFormer model you'd like to convert""",), ) parser.add_argument( """--checkpoint_path""", default="""/Users/nielsrogge/Documents/MaskFormer_checkpoints/MaskFormer-Swin-tiny-ADE20k/model.pkl""", type=str, help="""Path to the original state dict (.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.""" ) __SCREAMING_SNAKE_CASE : int = parser.parse_args() convert_maskformer_checkpoint( args.model_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub )
31
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) __SCREAMING_SNAKE_CASE : Optional[int] = { """configuration_mobilevit""": ["""MOBILEVIT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """MobileViTConfig""", """MobileViTOnnxConfig"""], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __SCREAMING_SNAKE_CASE : Union[str, Any] = ["""MobileViTFeatureExtractor"""] __SCREAMING_SNAKE_CASE : Union[str, Any] = ["""MobileViTImageProcessor"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __SCREAMING_SNAKE_CASE : Tuple = [ """MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST""", """MobileViTForImageClassification""", """MobileViTForSemanticSegmentation""", """MobileViTModel""", """MobileViTPreTrainedModel""", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __SCREAMING_SNAKE_CASE : str = [ """TF_MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST""", """TFMobileViTForImageClassification""", """TFMobileViTForSemanticSegmentation""", """TFMobileViTModel""", """TFMobileViTPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_mobilevit import MOBILEVIT_PRETRAINED_CONFIG_ARCHIVE_MAP, MobileViTConfig, MobileViTOnnxConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_mobilevit import MobileViTFeatureExtractor from .image_processing_mobilevit import MobileViTImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mobilevit import ( MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST, MobileViTForImageClassification, MobileViTForSemanticSegmentation, MobileViTModel, MobileViTPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_mobilevit import ( TF_MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST, TFMobileViTForImageClassification, TFMobileViTForSemanticSegmentation, TFMobileViTModel, TFMobileViTPreTrainedModel, ) else: import sys __SCREAMING_SNAKE_CASE : List[str] = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
31
'''simple docstring''' import bza import gzip import lzma import os import shutil import struct import tarfile import warnings import zipfile from abc import ABC, abstractmethod from pathlib import Path from typing import Dict, List, Optional, Type, Union from .. import config from .filelock import FileLock from .logging import get_logger __SCREAMING_SNAKE_CASE : Dict = get_logger(__name__) class lowerCamelCase_ : '''simple docstring''' def __init__( self : List[str] , A : Optional[str] = None ): _UpperCAmelCase : Dict = ( os.path.join(A , config.EXTRACTED_DATASETS_DIR ) if cache_dir else config.EXTRACTED_DATASETS_PATH ) _UpperCAmelCase : Union[str, Any] = Extractor def _A ( self : Tuple , A : str ): from .file_utils import hash_url_to_filename # Path where we extract compressed archives # We extract in the cache dir, and get the extracted path name by hashing the original path" _UpperCAmelCase : Dict = os.path.abspath(A ) return os.path.join(self.extract_dir , hash_url_to_filename(A ) ) def _A ( self : int , A : str , A : bool ): return force_extract or ( not os.path.isfile(A ) and not (os.path.isdir(A ) and os.listdir(A )) ) def _A ( self : Optional[int] , A : str , A : bool = False ): _UpperCAmelCase : Union[str, Any] = self.extractor.infer_extractor_format(A ) if not extractor_format: return input_path _UpperCAmelCase : Optional[Any] = self._get_output_path(A ) if self._do_extract(A , A ): self.extractor.extract(A , A , A ) return output_path class lowerCamelCase_ (snake_case__ ): '''simple docstring''' @classmethod @abstractmethod def _A ( cls : str , A : Union[Path, str] , **A : Dict ): ... @staticmethod @abstractmethod def _A ( A : Union[Path, str] , A : Union[Path, str] ): ... class lowerCamelCase_ (snake_case__ , snake_case__ ): '''simple docstring''' __UpperCamelCase: List[bytes] = [] @staticmethod def _A ( A : Union[Path, str] , A : int ): with open(A , "rb" ) as f: return f.read(A ) @classmethod def _A ( cls : Any , A : Union[Path, str] , A : bytes = b"" ): if not magic_number: _UpperCAmelCase : Any = max(len(A ) for cls_magic_number in cls.magic_numbers ) try: _UpperCAmelCase : int = cls.read_magic_number(A , A ) except OSError: return False return any(magic_number.startswith(A ) for cls_magic_number in cls.magic_numbers ) class lowerCamelCase_ (snake_case__ ): '''simple docstring''' @classmethod def _A ( cls : str , A : Union[Path, str] , **A : List[Any] ): return tarfile.is_tarfile(A ) @staticmethod def _A ( A : Union[str, Any] , A : str ): def resolved(A : str ) -> str: return os.path.realpath(os.path.abspath(A ) ) def badpath(A : str , A : str ) -> bool: # joinpath will ignore base if path is absolute return not resolved(os.path.join(A , A ) ).startswith(A ) def badlink(A : str , A : str ) -> bool: # Links are interpreted relative to the directory containing the link _UpperCAmelCase : List[str] = resolved(os.path.join(A , os.path.dirname(info.name ) ) ) return badpath(info.linkname , base=A ) _UpperCAmelCase : Optional[int] = resolved(A ) for finfo in members: if badpath(finfo.name , A ): logger.error(F"""Extraction of {finfo.name} is blocked (illegal path)""" ) elif finfo.issym() and badlink(A , A ): logger.error(F"""Extraction of {finfo.name} is blocked: Symlink to {finfo.linkname}""" ) elif finfo.islnk() and badlink(A , A ): logger.error(F"""Extraction of {finfo.name} is blocked: Hard link to {finfo.linkname}""" ) else: yield finfo @staticmethod def _A ( A : Union[Path, str] , A : Union[Path, str] ): os.makedirs(A , exist_ok=A ) _UpperCAmelCase : int = tarfile.open(A ) tar_file.extractall(A , members=TarExtractor.safemembers(A , A ) ) tar_file.close() class lowerCamelCase_ (snake_case__ ): '''simple docstring''' __UpperCamelCase: Union[str, Any] = [b"\x1F\x8B"] @staticmethod def _A ( A : Union[Path, str] , A : Union[Path, str] ): with gzip.open(A , "rb" ) as gzip_file: with open(A , "wb" ) as extracted_file: shutil.copyfileobj(A , A ) class lowerCamelCase_ (snake_case__ ): '''simple docstring''' __UpperCamelCase: Dict = [ b"PK\x03\x04", b"PK\x05\x06", # empty archive b"PK\x07\x08", # spanned archive ] @classmethod def _A ( cls : Dict , A : Union[Path, str] , A : bytes = b"" ): if super().is_extractable(A , magic_number=A ): return True try: # Alternative version of zipfile.is_zipfile that has less false positives, but misses executable zip archives. # From: https://github.com/python/cpython/pull/5053 from zipfile import ( _CD_SIGNATURE, _ECD_DISK_NUMBER, _ECD_DISK_START, _ECD_ENTRIES_TOTAL, _ECD_OFFSET, _ECD_SIZE, _EndRecData, sizeCentralDir, stringCentralDir, structCentralDir, ) with open(A , "rb" ) as fp: _UpperCAmelCase : Tuple = _EndRecData(A ) if endrec: if endrec[_ECD_ENTRIES_TOTAL] == 0 and endrec[_ECD_SIZE] == 0 and endrec[_ECD_OFFSET] == 0: return True # Empty zipfiles are still zipfiles elif endrec[_ECD_DISK_NUMBER] == endrec[_ECD_DISK_START]: fp.seek(endrec[_ECD_OFFSET] ) # Central directory is on the same disk if fp.tell() == endrec[_ECD_OFFSET] and endrec[_ECD_SIZE] >= sizeCentralDir: _UpperCAmelCase : Dict = fp.read(A ) # CD is where we expect it to be if len(A ) == sizeCentralDir: _UpperCAmelCase : Any = struct.unpack(A , A ) # CD is the right size if centdir[_CD_SIGNATURE] == stringCentralDir: return True # First central directory entry has correct magic number return False except Exception: # catch all errors in case future python versions change the zipfile internals return False @staticmethod def _A ( A : Union[Path, str] , A : Union[Path, str] ): os.makedirs(A , exist_ok=A ) with zipfile.ZipFile(A , "r" ) as zip_file: zip_file.extractall(A ) zip_file.close() class lowerCamelCase_ (snake_case__ ): '''simple docstring''' __UpperCamelCase: Dict = [b"\xFD\x37\x7A\x58\x5A\x00"] @staticmethod def _A ( A : Union[Path, str] , A : Union[Path, str] ): with lzma.open(A ) as compressed_file: with open(A , "wb" ) as extracted_file: shutil.copyfileobj(A , A ) class lowerCamelCase_ (snake_case__ ): '''simple docstring''' __UpperCamelCase: List[str] = [b"Rar!\x1a\x07\x00", b"Rar!\x1a\x07\x01\x00"] # RAR_ID # RAR5_ID @staticmethod def _A ( A : Union[Path, str] , A : Union[Path, str] ): if not config.RARFILE_AVAILABLE: raise ImportError("Please pip install rarfile" ) import rarfile os.makedirs(A , exist_ok=A ) _UpperCAmelCase : List[str] = rarfile.RarFile(A ) rf.extractall(A ) rf.close() class lowerCamelCase_ (snake_case__ ): '''simple docstring''' __UpperCamelCase: Optional[Any] = [b"\x28\xb5\x2F\xFD"] @staticmethod def _A ( A : Union[Path, str] , A : Union[Path, str] ): if not config.ZSTANDARD_AVAILABLE: raise ImportError("Please pip install zstandard" ) import zstandard as zstd _UpperCAmelCase : Optional[Any] = zstd.ZstdDecompressor() with open(A , "rb" ) as ifh, open(A , "wb" ) as ofh: dctx.copy_stream(A , A ) class lowerCamelCase_ (snake_case__ ): '''simple docstring''' __UpperCamelCase: Optional[Any] = [b"\x42\x5A\x68"] @staticmethod def _A ( A : Union[Path, str] , A : Union[Path, str] ): with bza.open(A , "rb" ) as compressed_file: with open(A , "wb" ) as extracted_file: shutil.copyfileobj(A , A ) class lowerCamelCase_ (snake_case__ ): '''simple docstring''' __UpperCamelCase: List[Any] = [b"\x37\x7A\xBC\xAF\x27\x1C"] @staticmethod def _A ( A : Union[Path, str] , A : Union[Path, str] ): if not config.PY7ZR_AVAILABLE: raise ImportError("Please pip install py7zr" ) import pyazr os.makedirs(A , exist_ok=A ) with pyazr.SevenZipFile(A , "r" ) as archive: archive.extractall(A ) class lowerCamelCase_ (snake_case__ ): '''simple docstring''' __UpperCamelCase: Optional[int] = [b"\x04\x22\x4D\x18"] @staticmethod def _A ( A : Union[Path, str] , A : Union[Path, str] ): if not config.LZ4_AVAILABLE: raise ImportError("Please pip install lz4" ) import lza.frame with lza.frame.open(A , "rb" ) as compressed_file: with open(A , "wb" ) as extracted_file: shutil.copyfileobj(A , A ) class lowerCamelCase_ : '''simple docstring''' __UpperCamelCase: Dict[str, Type[BaseExtractor]] = { "tar": TarExtractor, "gzip": GzipExtractor, "zip": ZipExtractor, "xz": XzExtractor, "rar": RarExtractor, "zstd": ZstdExtractor, "bz2": BzipaExtractor, "7z": SevenZipExtractor, # <Added version="2.4.0"/> "lz4": LzaExtractor, # <Added version="2.4.0"/> } @classmethod def _A ( cls : List[Any] ): return max( len(A ) for extractor in cls.extractors.values() if issubclass(A , A ) for extractor_magic_number in extractor.magic_numbers ) @staticmethod def _A ( A : Union[Path, str] , A : int ): try: return MagicNumberBaseExtractor.read_magic_number(A , magic_number_length=A ) except OSError: return b"" @classmethod def _A ( cls : Optional[Any] , A : Union[Path, str] , A : bool = False ): warnings.warn( "Method 'is_extractable' was deprecated in version 2.4.0 and will be removed in 3.0.0. " "Use 'infer_extractor_format' instead." , category=A , ) _UpperCAmelCase : Union[str, Any] = cls.infer_extractor_format(A ) if extractor_format: return True if not return_extractor else (True, cls.extractors[extractor_format]) return False if not return_extractor else (False, None) @classmethod def _A ( cls : Dict , A : Union[Path, str] ): # <Added version="2.4.0"/> _UpperCAmelCase : Optional[int] = cls._get_magic_number_max_length() _UpperCAmelCase : str = cls._read_magic_number(A , A ) for extractor_format, extractor in cls.extractors.items(): if extractor.is_extractable(A , magic_number=A ): return extractor_format @classmethod def _A ( cls : List[str] , A : Union[Path, str] , A : Union[Path, str] , A : Optional[str] = None , A : Optional[BaseExtractor] = "deprecated" , ): os.makedirs(os.path.dirname(A ) , exist_ok=A ) # Prevent parallel extractions _UpperCAmelCase : Tuple = str(Path(A ).with_suffix(".lock" ) ) with FileLock(A ): shutil.rmtree(A , ignore_errors=A ) if extractor_format or extractor != "deprecated": if extractor != "deprecated" or not isinstance(A , A ): # passed as positional arg warnings.warn( "Parameter 'extractor' was deprecated in version 2.4.0 and will be removed in 3.0.0. " "Use 'extractor_format' instead." , category=A , ) _UpperCAmelCase : Tuple = extractor if extractor != "deprecated" else extractor_format else: _UpperCAmelCase : Tuple = cls.extractors[extractor_format] return extractor.extract(A , A ) else: warnings.warn( "Parameter 'extractor_format' was made required in version 2.4.0 and not passing it will raise an " "exception in 3.0.0." , category=A , ) for extractor in cls.extractors.values(): if extractor.is_extractable(A ): return extractor.extract(A , A )
31
1
'''simple docstring''' def UpperCamelCase_ ( _UpperCAmelCase : str ) -> bool: """simple docstring""" return credit_card_number.startswith(("34", "35", "37", "4", "5", "6") ) def UpperCamelCase_ ( _UpperCAmelCase : str ) -> bool: """simple docstring""" _UpperCAmelCase : Tuple = credit_card_number _UpperCAmelCase : str = 0 _UpperCAmelCase : List[str] = len(_UpperCAmelCase ) - 2 for i in range(_UpperCAmelCase , -1 , -2 ): # double the value of every second digit _UpperCAmelCase : Optional[Any] = int(cc_number[i] ) digit *= 2 # If doubling of a number results in a two digit number # i.e greater than 9(e.g., 6 × 2 = 12), # then add the digits of the product (e.g., 12: 1 + 2 = 3, 15: 1 + 5 = 6), # to get a single digit number. if digit > 9: digit %= 10 digit += 1 _UpperCAmelCase : str = cc_number[:i] + str(_UpperCAmelCase ) + cc_number[i + 1 :] total += digit # Sum up the remaining digits for i in range(len(_UpperCAmelCase ) - 1 , -1 , -2 ): total += int(cc_number[i] ) return total % 10 == 0 def UpperCamelCase_ ( _UpperCAmelCase : str ) -> bool: """simple docstring""" _UpperCAmelCase : List[str] = F"""{credit_card_number} is an invalid credit card number because""" if not credit_card_number.isdigit(): print(F"""{error_message} it has nonnumerical characters.""" ) return False if not 13 <= len(_UpperCAmelCase ) <= 16: print(F"""{error_message} of its length.""" ) return False if not validate_initial_digits(_UpperCAmelCase ): print(F"""{error_message} of its first two digits.""" ) return False if not luhn_validation(_UpperCAmelCase ): print(F"""{error_message} it fails the Luhn check.""" ) return False print(F"""{credit_card_number} is a valid credit card number.""" ) return True if __name__ == "__main__": import doctest doctest.testmod() validate_credit_card_number("""4111111111111111""") validate_credit_card_number("""32323""")
31
'''simple docstring''' from typing import Any def UpperCamelCase_ ( _UpperCAmelCase : list , _UpperCAmelCase : list , _UpperCAmelCase : dict , _UpperCAmelCase : dict , _UpperCAmelCase : dict , ) -> list: """simple docstring""" _validation( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , ) # Creates data structures and fill initial step _UpperCAmelCase : dict = {} _UpperCAmelCase : dict = {} for state in states_space: _UpperCAmelCase : Union[str, Any] = observations_space[0] _UpperCAmelCase : Tuple = ( initial_probabilities[state] * emission_probabilities[state][observation] ) _UpperCAmelCase : List[str] = None # Fills the data structure with the probabilities of # different transitions and pointers to previous states for o in range(1 , len(_UpperCAmelCase ) ): _UpperCAmelCase : Optional[Any] = observations_space[o] _UpperCAmelCase : int = observations_space[o - 1] for state in states_space: # Calculates the argmax for probability function _UpperCAmelCase : str = "" _UpperCAmelCase : Tuple = -1 for k_state in states_space: _UpperCAmelCase : Any = ( probabilities[(k_state, prior_observation)] * transition_probabilities[k_state][state] * emission_probabilities[state][observation] ) if probability > max_probability: _UpperCAmelCase : Union[str, Any] = probability _UpperCAmelCase : str = k_state # Update probabilities and pointers dicts _UpperCAmelCase : Optional[int] = ( probabilities[(arg_max, prior_observation)] * transition_probabilities[arg_max][state] * emission_probabilities[state][observation] ) _UpperCAmelCase : Tuple = arg_max # The final observation _UpperCAmelCase : Optional[Any] = observations_space[len(_UpperCAmelCase ) - 1] # argmax for given final observation _UpperCAmelCase : List[str] = "" _UpperCAmelCase : Any = -1 for k_state in states_space: _UpperCAmelCase : Optional[int] = probabilities[(k_state, final_observation)] if probability > max_probability: _UpperCAmelCase : int = probability _UpperCAmelCase : Dict = k_state _UpperCAmelCase : Dict = arg_max # Process pointers backwards _UpperCAmelCase : List[Any] = last_state _UpperCAmelCase : str = [] for o in range(len(_UpperCAmelCase ) - 1 , -1 , -1 ): result.append(_UpperCAmelCase ) _UpperCAmelCase : List[Any] = pointers[previous, observations_space[o]] result.reverse() return result def UpperCamelCase_ ( _UpperCAmelCase : Any , _UpperCAmelCase : Any , _UpperCAmelCase : Any , _UpperCAmelCase : Any , _UpperCAmelCase : Any , ) -> None: """simple docstring""" _validate_not_empty( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , ) _validate_lists(_UpperCAmelCase , _UpperCAmelCase ) _validate_dicts( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) def UpperCamelCase_ ( _UpperCAmelCase : Any , _UpperCAmelCase : Any , _UpperCAmelCase : Any , _UpperCAmelCase : Any , _UpperCAmelCase : Any , ) -> None: """simple docstring""" if not all( [ observations_space, states_space, initial_probabilities, transition_probabilities, emission_probabilities, ] ): raise ValueError("There's an empty parameter" ) def UpperCamelCase_ ( _UpperCAmelCase : Any , _UpperCAmelCase : Any ) -> None: """simple docstring""" _validate_list(_UpperCAmelCase , "observations_space" ) _validate_list(_UpperCAmelCase , "states_space" ) def UpperCamelCase_ ( _UpperCAmelCase : Any , _UpperCAmelCase : str ) -> None: """simple docstring""" if not isinstance(_object , _UpperCAmelCase ): _UpperCAmelCase : Optional[int] = F"""{var_name} must be a list""" raise ValueError(_UpperCAmelCase ) else: for x in _object: if not isinstance(_UpperCAmelCase , _UpperCAmelCase ): _UpperCAmelCase : Optional[int] = F"""{var_name} must be a list of strings""" raise ValueError(_UpperCAmelCase ) def UpperCamelCase_ ( _UpperCAmelCase : Any , _UpperCAmelCase : Any , _UpperCAmelCase : Any , ) -> None: """simple docstring""" _validate_dict(_UpperCAmelCase , "initial_probabilities" , _UpperCAmelCase ) _validate_nested_dict(_UpperCAmelCase , "transition_probabilities" ) _validate_nested_dict(_UpperCAmelCase , "emission_probabilities" ) def UpperCamelCase_ ( _UpperCAmelCase : Any , _UpperCAmelCase : str ) -> None: """simple docstring""" _validate_dict(_object , _UpperCAmelCase , _UpperCAmelCase ) for x in _object.values(): _validate_dict(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) def UpperCamelCase_ ( _UpperCAmelCase : Any , _UpperCAmelCase : str , _UpperCAmelCase : type , _UpperCAmelCase : bool = False ) -> None: """simple docstring""" if not isinstance(_object , _UpperCAmelCase ): _UpperCAmelCase : Any = F"""{var_name} must be a dict""" raise ValueError(_UpperCAmelCase ) if not all(isinstance(_UpperCAmelCase , _UpperCAmelCase ) for x in _object ): _UpperCAmelCase : Tuple = F"""{var_name} all keys must be strings""" raise ValueError(_UpperCAmelCase ) if not all(isinstance(_UpperCAmelCase , _UpperCAmelCase ) for x in _object.values() ): _UpperCAmelCase : List[str] = "nested dictionary " if nested else "" _UpperCAmelCase : List[str] = F"""{var_name} {nested_text}all values must be {value_type.__name__}""" raise ValueError(_UpperCAmelCase ) if __name__ == "__main__": from doctest import testmod testmod()
31
1
'''simple docstring''' import warnings from ...utils import logging from .image_processing_beit import BeitImageProcessor __SCREAMING_SNAKE_CASE : Dict = logging.get_logger(__name__) class lowerCamelCase_ (snake_case__ ): '''simple docstring''' def __init__( self : Any , *A : List[str] , **A : Dict ): warnings.warn( "The class BeitFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please" " use BeitImageProcessor instead." , A , ) super().__init__(*A , **A )
31
'''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 MobileViTImageProcessor class lowerCamelCase_ (unittest.TestCase ): '''simple docstring''' def __init__( self : Optional[Any] , A : Dict , A : Dict=7 , A : Optional[int]=3 , A : Optional[int]=18 , A : Dict=30 , A : List[Any]=400 , A : Union[str, Any]=True , A : Tuple=None , A : List[Any]=True , A : int=None , A : Optional[int]=True , ): _UpperCAmelCase : Optional[int] = size if size is not None else {"shortest_edge": 20} _UpperCAmelCase : Optional[Any] = crop_size if crop_size is not None else {"height": 18, "width": 18} _UpperCAmelCase : List[Any] = parent _UpperCAmelCase : Union[str, Any] = batch_size _UpperCAmelCase : Optional[Any] = num_channels _UpperCAmelCase : Union[str, Any] = image_size _UpperCAmelCase : int = min_resolution _UpperCAmelCase : Optional[int] = max_resolution _UpperCAmelCase : List[str] = do_resize _UpperCAmelCase : Optional[Any] = size _UpperCAmelCase : Tuple = do_center_crop _UpperCAmelCase : Optional[int] = crop_size _UpperCAmelCase : Optional[Any] = do_flip_channel_order def _A ( self : Dict ): return { "do_resize": self.do_resize, "size": self.size, "do_center_crop": self.do_center_crop, "crop_size": self.crop_size, "do_flip_channel_order": self.do_flip_channel_order, } @require_torch @require_vision class lowerCamelCase_ (snake_case__ , unittest.TestCase ): '''simple docstring''' __UpperCamelCase: Tuple = MobileViTImageProcessor if is_vision_available() else None def _A ( self : List[Any] ): _UpperCAmelCase : Any = MobileViTImageProcessingTester(self ) @property def _A ( self : int ): return self.image_processor_tester.prepare_image_processor_dict() def _A ( self : Tuple ): _UpperCAmelCase : int = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(A , "do_resize" ) ) self.assertTrue(hasattr(A , "size" ) ) self.assertTrue(hasattr(A , "do_center_crop" ) ) self.assertTrue(hasattr(A , "center_crop" ) ) self.assertTrue(hasattr(A , "do_flip_channel_order" ) ) def _A ( self : Any ): _UpperCAmelCase : List[str] = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"shortest_edge": 20} ) self.assertEqual(image_processor.crop_size , {"height": 18, "width": 18} ) _UpperCAmelCase : Dict = self.image_processing_class.from_dict(self.image_processor_dict , size=42 , crop_size=84 ) self.assertEqual(image_processor.size , {"shortest_edge": 42} ) self.assertEqual(image_processor.crop_size , {"height": 84, "width": 84} ) def _A ( self : Any ): pass def _A ( self : Dict ): # Initialize image_processing _UpperCAmelCase : List[str] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images _UpperCAmelCase : Union[str, Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=A ) for image in image_inputs: self.assertIsInstance(A , Image.Image ) # Test not batched input _UpperCAmelCase : List[Any] = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) # Test batched _UpperCAmelCase : Optional[Any] = image_processing(A , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) def _A ( self : Union[str, Any] ): # Initialize image_processing _UpperCAmelCase : Dict = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors _UpperCAmelCase : Optional[Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=A , numpify=A ) for image in image_inputs: self.assertIsInstance(A , np.ndarray ) # Test not batched input _UpperCAmelCase : 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.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) # Test batched _UpperCAmelCase : Optional[int] = image_processing(A , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) def _A ( self : Any ): # Initialize image_processing _UpperCAmelCase : Any = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors _UpperCAmelCase : Optional[Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=A , torchify=A ) for image in image_inputs: self.assertIsInstance(A , torch.Tensor ) # Test not batched input _UpperCAmelCase : 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.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) # Test batched _UpperCAmelCase : Any = image_processing(A , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , )
31
1
'''simple docstring''' from collections import defaultdict def UpperCamelCase_ ( _UpperCAmelCase : int ) -> int: """simple docstring""" _UpperCAmelCase : List[str] = 1 _UpperCAmelCase : List[str] = True for v in tree[start]: if v not in visited: ret += dfs(_UpperCAmelCase ) if ret % 2 == 0: cuts.append(_UpperCAmelCase ) return ret def UpperCamelCase_ ( ) -> int: """simple docstring""" dfs(1 ) if __name__ == "__main__": __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Tuple = 10, 9 __SCREAMING_SNAKE_CASE : Dict = defaultdict(list) __SCREAMING_SNAKE_CASE : dict[int, bool] = {} __SCREAMING_SNAKE_CASE : list[int] = [] __SCREAMING_SNAKE_CASE : Union[str, Any] = 0 __SCREAMING_SNAKE_CASE : Any = [(2, 1), (3, 1), (4, 3), (5, 2), (6, 1), (7, 2), (8, 6), (9, 8), (10, 8)] for u, v in edges: tree[u].append(v) tree[v].append(u) even_tree() print(len(cuts) - 1)
31
'''simple docstring''' def UpperCamelCase_ ( _UpperCAmelCase : int , _UpperCAmelCase : int ) -> int: """simple docstring""" _UpperCAmelCase : List[str] = 1 # To kept the Calculated Value # Since C(n, k) = C(n, n-k) if k > (n - k): _UpperCAmelCase : Any = n - k # Calculate C(n,k) for i in range(_UpperCAmelCase ): result *= n - i result //= i + 1 return result def UpperCamelCase_ ( _UpperCAmelCase : int ) -> int: """simple docstring""" return binomial_coefficient(2 * node_count , _UpperCAmelCase ) // (node_count + 1) def UpperCamelCase_ ( _UpperCAmelCase : int ) -> int: """simple docstring""" if n < 0: raise ValueError("factorial() not defined for negative values" ) _UpperCAmelCase : List[str] = 1 for i in range(1 , n + 1 ): result *= i return result def UpperCamelCase_ ( _UpperCAmelCase : int ) -> int: """simple docstring""" return catalan_number(_UpperCAmelCase ) * factorial(_UpperCAmelCase ) if __name__ == "__main__": __SCREAMING_SNAKE_CASE : Dict = int(input("""Enter the number of nodes: """).strip() or 0) if node_count <= 0: raise ValueError("""We need some nodes to work with.""") print( F'Given {node_count} nodes, there are {binary_tree_count(node_count)} ' F'binary trees and {catalan_number(node_count)} binary search trees.' )
31
1
'''simple docstring''' import argparse from copy import deepcopy import numpy as np from datasets import ClassLabel, DatasetDict, load_dataset from evaluate import load from transformers import ( AutoModelForSequenceClassification, AutoTokenizer, DataCollatorWithPadding, Trainer, TrainerCallback, TrainingArguments, set_seed, ) def UpperCamelCase_ ( ) -> Optional[int]: """simple docstring""" _UpperCAmelCase : Any = argparse.ArgumentParser() parser.add_argument("--model_ckpt" , type=_UpperCAmelCase , default="microsoft/unixcoder-base-nine" ) parser.add_argument("--num_epochs" , type=_UpperCAmelCase , default=5 ) parser.add_argument("--batch_size" , type=_UpperCAmelCase , default=6 ) parser.add_argument("--gradient_accumulation_steps" , type=_UpperCAmelCase , default=1 ) parser.add_argument("--freeze" , type=_UpperCAmelCase , default=_UpperCAmelCase ) parser.add_argument("--learning_rate" , type=_UpperCAmelCase , default=5e-4 ) parser.add_argument("--seed" , type=_UpperCAmelCase , default=0 ) parser.add_argument("--lr_scheduler_type" , type=_UpperCAmelCase , default="cosine" ) parser.add_argument("--num_warmup_steps" , type=_UpperCAmelCase , default=10 ) parser.add_argument("--weight_decay" , type=_UpperCAmelCase , default=0.0_1 ) parser.add_argument("--output_dir" , type=_UpperCAmelCase , default="./results" ) return parser.parse_args() __SCREAMING_SNAKE_CASE : int = load("""accuracy""") def UpperCamelCase_ ( _UpperCAmelCase : Tuple ) -> List[Any]: """simple docstring""" _UpperCAmelCase , _UpperCAmelCase : Optional[Any] = eval_pred _UpperCAmelCase : Any = np.argmax(_UpperCAmelCase , axis=1 ) return metric.compute(predictions=_UpperCAmelCase , references=_UpperCAmelCase ) class lowerCamelCase_ (snake_case__ ): '''simple docstring''' def __init__( self : Optional[int] , A : Optional[Any] ): super().__init__() _UpperCAmelCase : int = trainer def _A ( self : Optional[Any] , A : List[str] , A : Union[str, Any] , A : int , **A : int ): if control.should_evaluate: _UpperCAmelCase : Any = deepcopy(A ) self._trainer.evaluate(eval_dataset=self._trainer.train_dataset , metric_key_prefix="train" ) return control_copy def UpperCamelCase_ ( ) -> Optional[Any]: """simple docstring""" _UpperCAmelCase : Optional[int] = get_args() set_seed(args.seed ) _UpperCAmelCase : List[Any] = load_dataset("codeparrot/codecomplex" , split="train" ) _UpperCAmelCase : List[str] = dataset.train_test_split(test_size=0.2 ) _UpperCAmelCase : Any = train_test["test"].train_test_split(test_size=0.5 ) _UpperCAmelCase : List[str] = DatasetDict( { "train": train_test["train"], "test": test_validation["train"], "valid": test_validation["test"], } ) print("Loading tokenizer and model" ) _UpperCAmelCase : Union[str, Any] = AutoTokenizer.from_pretrained(args.model_ckpt ) _UpperCAmelCase : Union[str, Any] = tokenizer.eos_token _UpperCAmelCase : List[str] = AutoModelForSequenceClassification.from_pretrained(args.model_ckpt , num_labels=7 ) _UpperCAmelCase : List[str] = model.config.eos_token_id if args.freeze: for param in model.roberta.parameters(): _UpperCAmelCase : Union[str, Any] = False _UpperCAmelCase : Dict = ClassLabel(num_classes=7 , names=list(set(train_test_validation["train"]["complexity"] ) ) ) def tokenize(_UpperCAmelCase : Dict ): _UpperCAmelCase : Dict = tokenizer(example["src"] , truncation=_UpperCAmelCase , max_length=1_024 ) _UpperCAmelCase : Tuple = labels.straint(example["complexity"] ) return { "input_ids": inputs["input_ids"], "attention_mask": inputs["attention_mask"], "label": label, } _UpperCAmelCase : List[Any] = train_test_validation.map( _UpperCAmelCase , batched=_UpperCAmelCase , remove_columns=train_test_validation["train"].column_names , ) _UpperCAmelCase : List[Any] = DataCollatorWithPadding(tokenizer=_UpperCAmelCase ) _UpperCAmelCase : Union[str, Any] = TrainingArguments( output_dir=args.output_dir , learning_rate=args.learning_rate , lr_scheduler_type=args.lr_scheduler_type , evaluation_strategy="epoch" , save_strategy="epoch" , logging_strategy="epoch" , per_device_train_batch_size=args.batch_size , per_device_eval_batch_size=args.batch_size , num_train_epochs=args.num_epochs , gradient_accumulation_steps=args.gradient_accumulation_steps , weight_decay=0.0_1 , metric_for_best_model="accuracy" , run_name="complexity-java" , report_to="wandb" , ) _UpperCAmelCase : Union[str, Any] = Trainer( model=_UpperCAmelCase , args=_UpperCAmelCase , train_dataset=tokenized_datasets["train"] , eval_dataset=tokenized_datasets["valid"] , tokenizer=_UpperCAmelCase , data_collator=_UpperCAmelCase , compute_metrics=_UpperCAmelCase , ) print("Training..." ) trainer.add_callback(CustomCallback(_UpperCAmelCase ) ) trainer.train() if __name__ == "__main__": main()
31
'''simple docstring''' import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_distilbert import DistilBertTokenizer __SCREAMING_SNAKE_CASE : str = logging.get_logger(__name__) __SCREAMING_SNAKE_CASE : str = {"""vocab_file""": """vocab.txt""", """tokenizer_file""": """tokenizer.json"""} __SCREAMING_SNAKE_CASE : Dict = { """vocab_file""": { """distilbert-base-uncased""": """https://huggingface.co/distilbert-base-uncased/resolve/main/vocab.txt""", """distilbert-base-uncased-distilled-squad""": ( """https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/vocab.txt""" ), """distilbert-base-cased""": """https://huggingface.co/distilbert-base-cased/resolve/main/vocab.txt""", """distilbert-base-cased-distilled-squad""": ( """https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/vocab.txt""" ), """distilbert-base-german-cased""": """https://huggingface.co/distilbert-base-german-cased/resolve/main/vocab.txt""", """distilbert-base-multilingual-cased""": ( """https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/vocab.txt""" ), }, """tokenizer_file""": { """distilbert-base-uncased""": """https://huggingface.co/distilbert-base-uncased/resolve/main/tokenizer.json""", """distilbert-base-uncased-distilled-squad""": ( """https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/tokenizer.json""" ), """distilbert-base-cased""": """https://huggingface.co/distilbert-base-cased/resolve/main/tokenizer.json""", """distilbert-base-cased-distilled-squad""": ( """https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/tokenizer.json""" ), """distilbert-base-german-cased""": ( """https://huggingface.co/distilbert-base-german-cased/resolve/main/tokenizer.json""" ), """distilbert-base-multilingual-cased""": ( """https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/tokenizer.json""" ), }, } __SCREAMING_SNAKE_CASE : Optional[Any] = { """distilbert-base-uncased""": 512, """distilbert-base-uncased-distilled-squad""": 512, """distilbert-base-cased""": 512, """distilbert-base-cased-distilled-squad""": 512, """distilbert-base-german-cased""": 512, """distilbert-base-multilingual-cased""": 512, } __SCREAMING_SNAKE_CASE : List[Any] = { """distilbert-base-uncased""": {"""do_lower_case""": True}, """distilbert-base-uncased-distilled-squad""": {"""do_lower_case""": True}, """distilbert-base-cased""": {"""do_lower_case""": False}, """distilbert-base-cased-distilled-squad""": {"""do_lower_case""": False}, """distilbert-base-german-cased""": {"""do_lower_case""": False}, """distilbert-base-multilingual-cased""": {"""do_lower_case""": False}, } class lowerCamelCase_ (snake_case__ ): '''simple docstring''' __UpperCamelCase: Union[str, Any] = VOCAB_FILES_NAMES __UpperCamelCase: str = PRETRAINED_VOCAB_FILES_MAP __UpperCamelCase: Optional[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCamelCase: Any = PRETRAINED_INIT_CONFIGURATION __UpperCamelCase: str = ["input_ids", "attention_mask"] __UpperCamelCase: List[str] = DistilBertTokenizer def __init__( self : str , A : int=None , A : Tuple=None , A : Tuple=True , A : Dict="[UNK]" , A : List[Any]="[SEP]" , A : Optional[Any]="[PAD]" , A : Dict="[CLS]" , A : Tuple="[MASK]" , A : str=True , A : Dict=None , **A : List[Any] , ): super().__init__( A , tokenizer_file=A , do_lower_case=A , unk_token=A , sep_token=A , pad_token=A , cls_token=A , mask_token=A , tokenize_chinese_chars=A , strip_accents=A , **A , ) _UpperCAmelCase : str = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get("lowercase" , A ) != do_lower_case or normalizer_state.get("strip_accents" , A ) != strip_accents or normalizer_state.get("handle_chinese_chars" , A ) != tokenize_chinese_chars ): _UpperCAmelCase : Dict = getattr(A , normalizer_state.pop("type" ) ) _UpperCAmelCase : int = do_lower_case _UpperCAmelCase : Optional[int] = strip_accents _UpperCAmelCase : str = tokenize_chinese_chars _UpperCAmelCase : List[Any] = normalizer_class(**A ) _UpperCAmelCase : Dict = do_lower_case def _A ( self : List[Any] , A : Tuple , A : Any=None ): _UpperCAmelCase : Optional[int] = [self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def _A ( self : int , A : List[int] , A : Optional[List[int]] = None ): _UpperCAmelCase : Any = [self.sep_token_id] _UpperCAmelCase : str = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def _A ( self : Dict , A : str , A : Optional[str] = None ): _UpperCAmelCase : Any = self._tokenizer.model.save(A , name=A ) return tuple(A )
31
1
'''simple docstring''' # Lint as: python3 import sys from collections.abc import Mapping from typing import TYPE_CHECKING, Dict, Optional import numpy as np import pyarrow as pa from .. import config from ..utils.logging import get_logger from ..utils.py_utils import map_nested from .formatting import TensorFormatter if TYPE_CHECKING: import jax import jaxlib __SCREAMING_SNAKE_CASE : Optional[Any] = get_logger() __SCREAMING_SNAKE_CASE : Optional[dict] = None class lowerCamelCase_ (TensorFormatter[Mapping, "jax.Array", Mapping] ): '''simple docstring''' def __init__( self : Any , A : List[Any]=None , A : List[str]=None , **A : List[str] ): super().__init__(features=A ) import jax from jaxlib.xla_client import Device if isinstance(A , A ): raise ValueError( F"""Expected {device} to be a `str` not {type(A )}, as `jaxlib.xla_extension.Device` """ "is not serializable neither with `pickle` nor with `dill`. Instead you can surround " "the device with `str()` to get its string identifier that will be internally mapped " "to the actual `jaxlib.xla_extension.Device`." ) _UpperCAmelCase : Union[str, Any] = device if isinstance(A , A ) else str(jax.devices()[0] ) # using global variable since `jaxlib.xla_extension.Device` is not serializable neither # with `pickle` nor with `dill`, so we need to use a global variable instead global DEVICE_MAPPING if DEVICE_MAPPING is None: _UpperCAmelCase : Dict = self._map_devices_to_str() if self.device not in list(DEVICE_MAPPING.keys() ): logger.warning( F"""Device with string identifier {self.device} not listed among the available """ F"""devices: {list(DEVICE_MAPPING.keys() )}, so falling back to the default """ F"""device: {str(jax.devices()[0] )}.""" ) _UpperCAmelCase : List[Any] = str(jax.devices()[0] ) _UpperCAmelCase : List[str] = jnp_array_kwargs @staticmethod def _A ( ): import jax return {str(A ): device for device in jax.devices()} def _A ( self : Optional[int] , A : int ): import jax import jax.numpy as jnp if isinstance(A , A ) and column: if all( isinstance(A , jax.Array ) and x.shape == column[0].shape and x.dtype == column[0].dtype for x in column ): return jnp.stack(A , axis=0 ) return column def _A ( self : List[str] , A : Optional[Any] ): import jax import jax.numpy as jnp if isinstance(A , (str, bytes, type(A )) ): return value elif isinstance(A , (np.character, np.ndarray) ) and np.issubdtype(value.dtype , np.character ): return value.tolist() _UpperCAmelCase : Optional[Any] = {} if isinstance(A , (np.number, np.ndarray) ) and np.issubdtype(value.dtype , np.integer ): # the default int precision depends on the jax config # see https://jax.readthedocs.io/en/latest/notebooks/Common_Gotchas_in_JAX.html#double-64bit-precision if jax.config.jax_enable_xaa: _UpperCAmelCase : Optional[Any] = {"dtype": jnp.intaa} else: _UpperCAmelCase : List[Any] = {"dtype": jnp.intaa} elif isinstance(A , (np.number, np.ndarray) ) and np.issubdtype(value.dtype , np.floating ): _UpperCAmelCase : List[str] = {"dtype": jnp.floataa} elif config.PIL_AVAILABLE and "PIL" in sys.modules: import PIL.Image if isinstance(A , PIL.Image.Image ): _UpperCAmelCase : Optional[int] = np.asarray(A ) # using global variable since `jaxlib.xla_extension.Device` is not serializable neither # with `pickle` nor with `dill`, so we need to use a global variable instead global DEVICE_MAPPING if DEVICE_MAPPING is None: _UpperCAmelCase : int = self._map_devices_to_str() with jax.default_device(DEVICE_MAPPING[self.device] ): # calling jnp.array on a np.ndarray does copy the data # see https://github.com/google/jax/issues/4486 return jnp.array(A , **{**default_dtype, **self.jnp_array_kwargs} ) def _A ( self : Optional[int] , A : Optional[int] ): import jax # support for torch, tf, jax etc. if config.TORCH_AVAILABLE and "torch" in sys.modules: import torch if isinstance(A , torch.Tensor ): return self._tensorize(data_struct.detach().cpu().numpy()[()] ) if hasattr(A , "__array__" ) and not isinstance(A , jax.Array ): _UpperCAmelCase : List[Any] = data_struct.__array__() # support for nested types like struct of list of struct if isinstance(A , np.ndarray ): if data_struct.dtype == object: # jax arrays cannot be instantied from an array of objects return self._consolidate([self.recursive_tensorize(A ) for substruct in data_struct] ) elif isinstance(A , (list, tuple) ): return self._consolidate([self.recursive_tensorize(A ) for substruct in data_struct] ) return self._tensorize(A ) def _A ( self : List[str] , A : dict ): return map_nested(self._recursive_tensorize , A , map_list=A ) def _A ( self : Dict , A : pa.Table ): _UpperCAmelCase : Tuple = self.numpy_arrow_extractor().extract_row(A ) _UpperCAmelCase : Optional[int] = self.python_features_decoder.decode_row(A ) return self.recursive_tensorize(A ) def _A ( self : Optional[Any] , A : pa.Table ): _UpperCAmelCase : Optional[Any] = self.numpy_arrow_extractor().extract_column(A ) _UpperCAmelCase : Any = self.python_features_decoder.decode_column(A , pa_table.column_names[0] ) _UpperCAmelCase : Union[str, Any] = self.recursive_tensorize(A ) _UpperCAmelCase : List[Any] = self._consolidate(A ) return column def _A ( self : List[str] , A : pa.Table ): _UpperCAmelCase : Optional[int] = self.numpy_arrow_extractor().extract_batch(A ) _UpperCAmelCase : Optional[int] = self.python_features_decoder.decode_batch(A ) _UpperCAmelCase : Optional[int] = self.recursive_tensorize(A ) for column_name in batch: _UpperCAmelCase : Any = self._consolidate(batch[column_name] ) return batch
31
'''simple docstring''' import shutil import tempfile import unittest from unittest.mock import patch from transformers import ( DefaultFlowCallback, IntervalStrategy, PrinterCallback, ProgressCallback, Trainer, TrainerCallback, TrainingArguments, is_torch_available, ) from transformers.testing_utils import require_torch if is_torch_available(): from transformers.trainer import DEFAULT_CALLBACKS from .test_trainer import RegressionDataset, RegressionModelConfig, RegressionPreTrainedModel class lowerCamelCase_ (snake_case__ ): '''simple docstring''' def __init__( self : List[Any] ): _UpperCAmelCase : Union[str, Any] = [] def _A ( self : Any , A : Union[str, Any] , A : Optional[int] , A : List[str] , **A : Tuple ): self.events.append("on_init_end" ) def _A ( self : Any , A : str , A : List[Any] , A : List[Any] , **A : Tuple ): self.events.append("on_train_begin" ) def _A ( self : Tuple , A : List[str] , A : Tuple , A : int , **A : List[str] ): self.events.append("on_train_end" ) def _A ( self : Optional[Any] , A : Dict , A : Any , A : Optional[Any] , **A : List[Any] ): self.events.append("on_epoch_begin" ) def _A ( self : Optional[Any] , A : List[Any] , A : List[str] , A : Optional[int] , **A : Optional[int] ): self.events.append("on_epoch_end" ) def _A ( self : List[str] , A : Optional[int] , A : List[Any] , A : Union[str, Any] , **A : Any ): self.events.append("on_step_begin" ) def _A ( self : Tuple , A : Union[str, Any] , A : int , A : Optional[int] , **A : int ): self.events.append("on_step_end" ) def _A ( self : Optional[int] , A : Optional[Any] , A : Union[str, Any] , A : str , **A : Union[str, Any] ): self.events.append("on_evaluate" ) def _A ( self : Optional[Any] , A : Optional[int] , A : Dict , A : List[Any] , **A : Dict ): self.events.append("on_predict" ) def _A ( self : Dict , A : Dict , A : List[Any] , A : Dict , **A : str ): self.events.append("on_save" ) def _A ( self : Tuple , A : Optional[Any] , A : Union[str, Any] , A : Optional[int] , **A : Dict ): self.events.append("on_log" ) def _A ( self : Optional[int] , A : Optional[Any] , A : Tuple , A : Tuple , **A : List[str] ): self.events.append("on_prediction_step" ) @require_torch class lowerCamelCase_ (unittest.TestCase ): '''simple docstring''' def _A ( self : Optional[int] ): _UpperCAmelCase : Optional[Any] = tempfile.mkdtemp() def _A ( self : List[Any] ): shutil.rmtree(self.output_dir ) def _A ( self : Union[str, Any] , A : Optional[int]=0 , A : Optional[Any]=0 , A : Optional[Any]=64 , A : Dict=64 , A : Any=None , A : Tuple=False , **A : Optional[int] ): # disable_tqdm in TrainingArguments has a flaky default since it depends on the level of logging. We make sure # its set to False since the tests later on depend on its value. _UpperCAmelCase : str = RegressionDataset(length=A ) _UpperCAmelCase : Union[str, Any] = RegressionDataset(length=A ) _UpperCAmelCase : Any = RegressionModelConfig(a=A , b=A ) _UpperCAmelCase : List[Any] = RegressionPreTrainedModel(A ) _UpperCAmelCase : Dict = TrainingArguments(self.output_dir , disable_tqdm=A , report_to=[] , **A ) return Trainer( A , A , train_dataset=A , eval_dataset=A , callbacks=A , ) def _A ( self : str , A : List[str] , A : List[str] ): self.assertEqual(len(A ) , len(A ) ) # Order doesn't matter _UpperCAmelCase : Tuple = sorted(A , key=lambda A : cb.__name__ if isinstance(A , A ) else cb.__class__.__name__ ) _UpperCAmelCase : Any = sorted(A , key=lambda A : cb.__name__ if isinstance(A , A ) else cb.__class__.__name__ ) for cba, cba in zip(A , A ): if isinstance(A , A ) and isinstance(A , A ): self.assertEqual(A , A ) elif isinstance(A , A ) and not isinstance(A , A ): self.assertEqual(A , cba.__class__ ) elif not isinstance(A , A ) and isinstance(A , A ): self.assertEqual(cba.__class__ , A ) else: self.assertEqual(A , A ) def _A ( self : int , A : List[str] ): _UpperCAmelCase : List[str] = ["on_init_end", "on_train_begin"] _UpperCAmelCase : str = 0 _UpperCAmelCase : Optional[Any] = len(trainer.get_eval_dataloader() ) _UpperCAmelCase : Optional[int] = ["on_prediction_step"] * len(trainer.get_eval_dataloader() ) + ["on_log", "on_evaluate"] for _ in range(trainer.state.num_train_epochs ): expected_events.append("on_epoch_begin" ) for _ in range(A ): step += 1 expected_events += ["on_step_begin", "on_step_end"] if step % trainer.args.logging_steps == 0: expected_events.append("on_log" ) if trainer.args.evaluation_strategy == IntervalStrategy.STEPS and step % trainer.args.eval_steps == 0: expected_events += evaluation_events.copy() if step % trainer.args.save_steps == 0: expected_events.append("on_save" ) expected_events.append("on_epoch_end" ) if trainer.args.evaluation_strategy == IntervalStrategy.EPOCH: expected_events += evaluation_events.copy() expected_events += ["on_log", "on_train_end"] return expected_events def _A ( self : str ): _UpperCAmelCase : Any = self.get_trainer() _UpperCAmelCase : int = DEFAULT_CALLBACKS.copy() + [ProgressCallback] self.check_callbacks_equality(trainer.callback_handler.callbacks , A ) # Callbacks passed at init are added to the default callbacks _UpperCAmelCase : Optional[int] = self.get_trainer(callbacks=[MyTestTrainerCallback] ) expected_callbacks.append(A ) self.check_callbacks_equality(trainer.callback_handler.callbacks , A ) # TrainingArguments.disable_tqdm controls if use ProgressCallback or PrinterCallback _UpperCAmelCase : List[Any] = self.get_trainer(disable_tqdm=A ) _UpperCAmelCase : Tuple = DEFAULT_CALLBACKS.copy() + [PrinterCallback] self.check_callbacks_equality(trainer.callback_handler.callbacks , A ) def _A ( self : Optional[Any] ): _UpperCAmelCase : Dict = DEFAULT_CALLBACKS.copy() + [ProgressCallback] _UpperCAmelCase : Dict = self.get_trainer() # We can add, pop, or remove by class name trainer.remove_callback(A ) expected_callbacks.remove(A ) self.check_callbacks_equality(trainer.callback_handler.callbacks , A ) _UpperCAmelCase : Optional[Any] = self.get_trainer() _UpperCAmelCase : Any = trainer.pop_callback(A ) self.assertEqual(cb.__class__ , A ) self.check_callbacks_equality(trainer.callback_handler.callbacks , A ) trainer.add_callback(A ) expected_callbacks.insert(0 , A ) self.check_callbacks_equality(trainer.callback_handler.callbacks , A ) # We can also add, pop, or remove by instance _UpperCAmelCase : Union[str, Any] = self.get_trainer() _UpperCAmelCase : List[Any] = trainer.callback_handler.callbacks[0] trainer.remove_callback(A ) expected_callbacks.remove(A ) self.check_callbacks_equality(trainer.callback_handler.callbacks , A ) _UpperCAmelCase : List[Any] = self.get_trainer() _UpperCAmelCase : List[Any] = trainer.callback_handler.callbacks[0] _UpperCAmelCase : Union[str, Any] = trainer.pop_callback(A ) self.assertEqual(A , A ) self.check_callbacks_equality(trainer.callback_handler.callbacks , A ) trainer.add_callback(A ) expected_callbacks.insert(0 , A ) self.check_callbacks_equality(trainer.callback_handler.callbacks , A ) def _A ( self : Optional[Any] ): import warnings # XXX: for now ignore scatter_gather warnings in this test since it's not relevant to what's being tested warnings.simplefilter(action="ignore" , category=A ) _UpperCAmelCase : Optional[int] = self.get_trainer(callbacks=[MyTestTrainerCallback] ) trainer.train() _UpperCAmelCase : Union[str, Any] = trainer.callback_handler.callbacks[-2].events self.assertEqual(A , self.get_expected_events(A ) ) # Independent log/save/eval _UpperCAmelCase : Tuple = self.get_trainer(callbacks=[MyTestTrainerCallback] , logging_steps=5 ) trainer.train() _UpperCAmelCase : int = trainer.callback_handler.callbacks[-2].events self.assertEqual(A , self.get_expected_events(A ) ) _UpperCAmelCase : List[str] = self.get_trainer(callbacks=[MyTestTrainerCallback] , save_steps=5 ) trainer.train() _UpperCAmelCase : Tuple = trainer.callback_handler.callbacks[-2].events self.assertEqual(A , self.get_expected_events(A ) ) _UpperCAmelCase : int = self.get_trainer(callbacks=[MyTestTrainerCallback] , eval_steps=5 , evaluation_strategy="steps" ) trainer.train() _UpperCAmelCase : Optional[int] = trainer.callback_handler.callbacks[-2].events self.assertEqual(A , self.get_expected_events(A ) ) _UpperCAmelCase : Optional[int] = self.get_trainer(callbacks=[MyTestTrainerCallback] , evaluation_strategy="epoch" ) trainer.train() _UpperCAmelCase : int = trainer.callback_handler.callbacks[-2].events self.assertEqual(A , self.get_expected_events(A ) ) # A bit of everything _UpperCAmelCase : int = self.get_trainer( callbacks=[MyTestTrainerCallback] , logging_steps=3 , save_steps=10 , eval_steps=5 , evaluation_strategy="steps" , ) trainer.train() _UpperCAmelCase : Optional[int] = trainer.callback_handler.callbacks[-2].events self.assertEqual(A , self.get_expected_events(A ) ) # warning should be emitted for duplicated callbacks with patch("transformers.trainer_callback.logger.warning" ) as warn_mock: _UpperCAmelCase : Optional[Any] = self.get_trainer( callbacks=[MyTestTrainerCallback, MyTestTrainerCallback] , ) assert str(A ) in warn_mock.call_args[0][0]
31
1
'''simple docstring''' def UpperCamelCase_ ( _UpperCAmelCase : str , _UpperCAmelCase : list[str] ) -> str: """simple docstring""" _UpperCAmelCase : Optional[int] = "" for word_or_phrase in separated: if not isinstance(_UpperCAmelCase , _UpperCAmelCase ): raise Exception("join() accepts only strings to be joined" ) joined += word_or_phrase + separator return joined.strip(_UpperCAmelCase ) if __name__ == "__main__": from doctest import testmod testmod()
31
'''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_video_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import VivitImageProcessor class lowerCamelCase_ (unittest.TestCase ): '''simple docstring''' def __init__( self : int , A : Dict , A : Optional[int]=7 , A : Tuple=3 , A : Optional[Any]=10 , A : int=18 , A : Dict=30 , A : List[str]=400 , A : int=True , A : Optional[Any]=None , A : Optional[Any]=True , A : List[Any]=[0.5, 0.5, 0.5] , A : List[str]=[0.5, 0.5, 0.5] , A : Optional[int]=None , ): _UpperCAmelCase : Dict = size if size is not None else {"shortest_edge": 18} _UpperCAmelCase : Optional[Any] = crop_size if crop_size is not None else {"height": 18, "width": 18} _UpperCAmelCase : Tuple = parent _UpperCAmelCase : Any = batch_size _UpperCAmelCase : Optional[int] = num_channels _UpperCAmelCase : Optional[Any] = num_frames _UpperCAmelCase : Any = image_size _UpperCAmelCase : Dict = min_resolution _UpperCAmelCase : Any = max_resolution _UpperCAmelCase : Optional[int] = do_resize _UpperCAmelCase : str = size _UpperCAmelCase : List[Any] = do_normalize _UpperCAmelCase : Any = image_mean _UpperCAmelCase : Tuple = image_std _UpperCAmelCase : Any = crop_size def _A ( self : List[Any] ): return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "size": self.size, "crop_size": self.crop_size, } @require_torch @require_vision class lowerCamelCase_ (snake_case__ , unittest.TestCase ): '''simple docstring''' __UpperCamelCase: Dict = VivitImageProcessor if is_vision_available() else None def _A ( self : int ): _UpperCAmelCase : Tuple = VivitImageProcessingTester(self ) @property def _A ( self : Optional[Any] ): return self.image_processor_tester.prepare_image_processor_dict() def _A ( self : Union[str, Any] ): _UpperCAmelCase : str = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(A , "image_mean" ) ) self.assertTrue(hasattr(A , "image_std" ) ) self.assertTrue(hasattr(A , "do_normalize" ) ) self.assertTrue(hasattr(A , "do_resize" ) ) self.assertTrue(hasattr(A , "do_center_crop" ) ) self.assertTrue(hasattr(A , "size" ) ) def _A ( self : List[Any] ): _UpperCAmelCase : Union[str, Any] = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"shortest_edge": 18} ) self.assertEqual(image_processor.crop_size , {"height": 18, "width": 18} ) _UpperCAmelCase : Optional[int] = self.image_processing_class.from_dict(self.image_processor_dict , size=42 , crop_size=84 ) self.assertEqual(image_processor.size , {"shortest_edge": 42} ) self.assertEqual(image_processor.crop_size , {"height": 84, "width": 84} ) def _A ( self : Tuple ): # Initialize image_processing _UpperCAmelCase : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PIL videos _UpperCAmelCase : Any = prepare_video_inputs(self.image_processor_tester , equal_resolution=A ) for video in video_inputs: self.assertIsInstance(A , A ) self.assertIsInstance(video[0] , Image.Image ) # Test not batched input _UpperCAmelCase : str = image_processing(video_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_videos.shape , ( 1, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) # Test batched _UpperCAmelCase : List[Any] = image_processing(A , return_tensors="pt" ).pixel_values self.assertEqual( encoded_videos.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) def _A ( self : List[Any] ): # Initialize image_processing _UpperCAmelCase : List[str] = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors _UpperCAmelCase : List[Any] = prepare_video_inputs(self.image_processor_tester , equal_resolution=A , numpify=A ) for video in video_inputs: self.assertIsInstance(A , A ) self.assertIsInstance(video[0] , np.ndarray ) # Test not batched input _UpperCAmelCase : Tuple = image_processing(video_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_videos.shape , ( 1, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) # Test batched _UpperCAmelCase : Optional[int] = image_processing(A , return_tensors="pt" ).pixel_values self.assertEqual( encoded_videos.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) def _A ( self : List[Any] ): # Initialize image_processing _UpperCAmelCase : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors _UpperCAmelCase : Optional[int] = prepare_video_inputs(self.image_processor_tester , equal_resolution=A , torchify=A ) for video in video_inputs: self.assertIsInstance(A , A ) self.assertIsInstance(video[0] , torch.Tensor ) # Test not batched input _UpperCAmelCase : Optional[Any] = image_processing(video_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_videos.shape , ( 1, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) # Test batched _UpperCAmelCase : List[Any] = image_processing(A , return_tensors="pt" ).pixel_values self.assertEqual( encoded_videos.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , )
31
1
'''simple docstring''' import logging import os import sys import warnings from dataclasses import dataclass, field from random import randint from typing import Optional import datasets import evaluate import numpy as np from datasets import DatasetDict, load_dataset import transformers from transformers import ( AutoConfig, AutoFeatureExtractor, AutoModelForAudioClassification, HfArgumentParser, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import get_last_checkpoint from transformers.utils import check_min_version, send_example_telemetry from transformers.utils.versions import require_version __SCREAMING_SNAKE_CASE : Optional[Any] = logging.getLogger(__name__) # Will error if the minimal version of Transformers is not installed. Remove at your own risks. check_min_version("""4.31.0""") require_version("""datasets>=1.14.0""", """To fix: pip install -r examples/pytorch/audio-classification/requirements.txt""") def UpperCamelCase_ ( _UpperCAmelCase : np.ndarray , _UpperCAmelCase : float , _UpperCAmelCase : int = 16_000 ) -> Any: """simple docstring""" _UpperCAmelCase : Union[str, Any] = int(round(sample_rate * max_length ) ) if len(_UpperCAmelCase ) <= sample_length: return wav _UpperCAmelCase : Optional[Any] = randint(0 , len(_UpperCAmelCase ) - sample_length - 1 ) return wav[random_offset : random_offset + sample_length] @dataclass class lowerCamelCase_ : '''simple docstring''' __UpperCamelCase: Optional[str] = field(default=snake_case__ , metadata={"help": "Name of a dataset from the datasets package"} ) __UpperCamelCase: Optional[str] = field( default=snake_case__ , metadata={"help": "The configuration name of the dataset to use (via the datasets library)."} ) __UpperCamelCase: Optional[str] = field( default=snake_case__ , metadata={"help": "A file containing the training audio paths and labels."} ) __UpperCamelCase: Optional[str] = field( default=snake_case__ , metadata={"help": "A file containing the validation audio paths and labels."} ) __UpperCamelCase: str = field( default="train" , metadata={ "help": "The name of the training data set split to use (via the datasets library). Defaults to 'train'" } , ) __UpperCamelCase: str = field( default="validation" , metadata={ "help": ( "The name of the training data set split to use (via the datasets library). Defaults to 'validation'" ) } , ) __UpperCamelCase: str = field( default="audio" , metadata={"help": "The name of the dataset column containing the audio data. Defaults to 'audio'"} , ) __UpperCamelCase: str = field( default="label" , metadata={"help": "The name of the dataset column containing the labels. Defaults to 'label'"} ) __UpperCamelCase: Optional[int] = field( default=snake_case__ , metadata={ "help": ( "For debugging purposes or quicker training, truncate the number of training examples to this " "value if set." ) } , ) __UpperCamelCase: Optional[int] = field( default=snake_case__ , metadata={ "help": ( "For debugging purposes or quicker training, truncate the number of evaluation examples to this " "value if set." ) } , ) __UpperCamelCase: float = field( default=2_0 , metadata={"help": "Audio clips will be randomly cut to this length during training if the value is set."} , ) @dataclass class lowerCamelCase_ : '''simple docstring''' __UpperCamelCase: str = field( default="facebook/wav2vec2-base" , metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models"} , ) __UpperCamelCase: Optional[str] = field( default=snake_case__ , metadata={"help": "Pretrained config name or path if not the same as model_name"} ) __UpperCamelCase: Optional[str] = field( default=snake_case__ , metadata={"help": "Where do you want to store the pretrained models downloaded from the Hub"} ) __UpperCamelCase: str = field( default="main" , metadata={"help": "The specific model version to use (can be a branch name, tag name or commit id)."} , ) __UpperCamelCase: Optional[str] = field( default=snake_case__ , metadata={"help": "Name or path of preprocessor config."} ) __UpperCamelCase: bool = field( default=snake_case__ , metadata={"help": "Whether to freeze the feature encoder layers of the model."} ) __UpperCamelCase: bool = field( default=snake_case__ , metadata={"help": "Whether to generate an attention mask in the feature extractor."} ) __UpperCamelCase: bool = field( default=snake_case__ , metadata={ "help": ( "Will use the token generated when running `huggingface-cli login` (necessary to use this script " "with private models)." ) } , ) __UpperCamelCase: Optional[bool] = field( default=snake_case__ , metadata={"help": "Whether to freeze the feature extractor layers of the model."} ) __UpperCamelCase: bool = field( default=snake_case__ , metadata={"help": "Will enable to load a pretrained model whose head dimensions are different."} , ) def _A ( self : str ): if not self.freeze_feature_extractor and self.freeze_feature_encoder: warnings.warn( "The argument `--freeze_feature_extractor` is deprecated and " "will be removed in a future version. Use `--freeze_feature_encoder`" "instead. Setting `freeze_feature_encoder==True`." , A , ) if self.freeze_feature_extractor and not self.freeze_feature_encoder: raise ValueError( "The argument `--freeze_feature_extractor` is deprecated and " "should not be used in combination with `--freeze_feature_encoder`." "Only make use of `--freeze_feature_encoder`." ) def UpperCamelCase_ ( ) -> List[Any]: """simple docstring""" _UpperCAmelCase : int = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) 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. _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase : Dict = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase : str = parser.parse_args_into_dataclasses() # Sending telemetry. Tracking the example usage helps us better allocate resources to maintain them. The # information sent is the one passed as arguments along with your Python/PyTorch versions. send_example_telemetry("run_audio_classification" , _UpperCAmelCase , _UpperCAmelCase ) # Setup logging logging.basicConfig( format="%(asctime)s - %(levelname)s - %(name)s - %(message)s" , datefmt="%m/%d/%Y %H:%M:%S" , handlers=[logging.StreamHandler(sys.stdout )] , ) if training_args.should_log: # The default of training_args.log_level is passive, so we set log level at info here to have that default. transformers.utils.logging.set_verbosity_info() _UpperCAmelCase : Optional[int] = training_args.get_process_log_level() logger.setLevel(_UpperCAmelCase ) transformers.utils.logging.set_verbosity(_UpperCAmelCase ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Log on each process the small summary: logger.warning( F"""Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu} """ + F"""distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}""" ) logger.info(F"""Training/evaluation parameters {training_args}""" ) # Set seed before initializing model. set_seed(training_args.seed ) # Detecting last checkpoint. _UpperCAmelCase : Optional[int] = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: _UpperCAmelCase : Any = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( F"""Output directory ({training_args.output_dir}) already exists and is not empty. """ "Use --overwrite_output_dir to train from scratch." ) elif last_checkpoint is not None and training_args.resume_from_checkpoint is None: logger.info( F"""Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change """ "the `--output_dir` or add `--overwrite_output_dir` to train from scratch." ) # Initialize our dataset and prepare it for the audio classification task. _UpperCAmelCase : Union[str, Any] = DatasetDict() _UpperCAmelCase : Optional[Any] = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=data_args.train_split_name , use_auth_token=True if model_args.use_auth_token else None , ) _UpperCAmelCase : Tuple = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=data_args.eval_split_name , use_auth_token=True if model_args.use_auth_token else None , ) if data_args.audio_column_name not in raw_datasets["train"].column_names: raise ValueError( F"""--audio_column_name {data_args.audio_column_name} not found in dataset '{data_args.dataset_name}'. """ "Make sure to set `--audio_column_name` to the correct audio column - one of " F"""{', '.join(raw_datasets['train'].column_names )}.""" ) if data_args.label_column_name not in raw_datasets["train"].column_names: raise ValueError( F"""--label_column_name {data_args.label_column_name} not found in dataset '{data_args.dataset_name}'. """ "Make sure to set `--label_column_name` to the correct text column - one of " F"""{', '.join(raw_datasets['train'].column_names )}.""" ) # Setting `return_attention_mask=True` is the way to get a correctly masked mean-pooling over # transformer outputs in the classifier, but it doesn't always lead to better accuracy _UpperCAmelCase : Dict = AutoFeatureExtractor.from_pretrained( model_args.feature_extractor_name or model_args.model_name_or_path , return_attention_mask=model_args.attention_mask , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) # `datasets` takes care of automatically loading and resampling the audio, # so we just need to set the correct target sampling rate. _UpperCAmelCase : Any = raw_datasets.cast_column( data_args.audio_column_name , datasets.features.Audio(sampling_rate=feature_extractor.sampling_rate ) ) _UpperCAmelCase : Optional[int] = feature_extractor.model_input_names[0] def train_transforms(_UpperCAmelCase : Optional[Any] ): _UpperCAmelCase : Optional[Any] = [] for audio in batch[data_args.audio_column_name]: _UpperCAmelCase : int = random_subsample( audio["array"] , max_length=data_args.max_length_seconds , sample_rate=feature_extractor.sampling_rate ) subsampled_wavs.append(_UpperCAmelCase ) _UpperCAmelCase : str = feature_extractor(_UpperCAmelCase , sampling_rate=feature_extractor.sampling_rate ) _UpperCAmelCase : Dict = {model_input_name: inputs.get(_UpperCAmelCase )} _UpperCAmelCase : Tuple = list(batch[data_args.label_column_name] ) return output_batch def val_transforms(_UpperCAmelCase : Any ): _UpperCAmelCase : str = [audio["array"] for audio in batch[data_args.audio_column_name]] _UpperCAmelCase : List[Any] = feature_extractor(_UpperCAmelCase , sampling_rate=feature_extractor.sampling_rate ) _UpperCAmelCase : str = {model_input_name: inputs.get(_UpperCAmelCase )} _UpperCAmelCase : List[str] = list(batch[data_args.label_column_name] ) return output_batch # Prepare label mappings. # We'll include these in the model's config to get human readable labels in the Inference API. _UpperCAmelCase : int = raw_datasets["train"].features[data_args.label_column_name].names _UpperCAmelCase , _UpperCAmelCase : Any = {}, {} for i, label in enumerate(_UpperCAmelCase ): _UpperCAmelCase : Union[str, Any] = str(_UpperCAmelCase ) _UpperCAmelCase : str = label # Load the accuracy metric from the datasets package _UpperCAmelCase : Dict = evaluate.load("accuracy" ) # Define our compute_metrics function. It takes an `EvalPrediction` object (a namedtuple with # `predictions` and `label_ids` fields) and has to return a dictionary string to float. def compute_metrics(_UpperCAmelCase : Optional[int] ): _UpperCAmelCase : str = np.argmax(eval_pred.predictions , axis=1 ) return metric.compute(predictions=_UpperCAmelCase , references=eval_pred.label_ids ) _UpperCAmelCase : int = AutoConfig.from_pretrained( model_args.config_name or model_args.model_name_or_path , num_labels=len(_UpperCAmelCase ) , labelaid=_UpperCAmelCase , idalabel=_UpperCAmelCase , finetuning_task="audio-classification" , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) _UpperCAmelCase : Dict = AutoModelForAudioClassification.from_pretrained( model_args.model_name_or_path , from_tf=bool(".ckpt" in model_args.model_name_or_path ) , config=_UpperCAmelCase , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ignore_mismatched_sizes=model_args.ignore_mismatched_sizes , ) # freeze the convolutional waveform encoder if model_args.freeze_feature_encoder: model.freeze_feature_encoder() if training_args.do_train: if data_args.max_train_samples is not None: _UpperCAmelCase : List[str] = ( raw_datasets["train"].shuffle(seed=training_args.seed ).select(range(data_args.max_train_samples ) ) ) # Set the training transforms raw_datasets["train"].set_transform(_UpperCAmelCase , output_all_columns=_UpperCAmelCase ) if training_args.do_eval: if data_args.max_eval_samples is not None: _UpperCAmelCase : Tuple = ( raw_datasets["eval"].shuffle(seed=training_args.seed ).select(range(data_args.max_eval_samples ) ) ) # Set the validation transforms raw_datasets["eval"].set_transform(_UpperCAmelCase , output_all_columns=_UpperCAmelCase ) # Initialize our trainer _UpperCAmelCase : Dict = Trainer( model=_UpperCAmelCase , args=_UpperCAmelCase , train_dataset=raw_datasets["train"] if training_args.do_train else None , eval_dataset=raw_datasets["eval"] if training_args.do_eval else None , compute_metrics=_UpperCAmelCase , tokenizer=_UpperCAmelCase , ) # Training if training_args.do_train: _UpperCAmelCase : List[Any] = None if training_args.resume_from_checkpoint is not None: _UpperCAmelCase : Optional[Any] = training_args.resume_from_checkpoint elif last_checkpoint is not None: _UpperCAmelCase : int = last_checkpoint _UpperCAmelCase : Dict = trainer.train(resume_from_checkpoint=_UpperCAmelCase ) trainer.save_model() trainer.log_metrics("train" , train_result.metrics ) trainer.save_metrics("train" , train_result.metrics ) trainer.save_state() # Evaluation if training_args.do_eval: _UpperCAmelCase : Tuple = trainer.evaluate() trainer.log_metrics("eval" , _UpperCAmelCase ) trainer.save_metrics("eval" , _UpperCAmelCase ) # Write model card and (optionally) push to hub _UpperCAmelCase : Tuple = { "finetuned_from": model_args.model_name_or_path, "tasks": "audio-classification", "dataset": data_args.dataset_name, "tags": ["audio-classification"], } if training_args.push_to_hub: trainer.push_to_hub(**_UpperCAmelCase ) else: trainer.create_model_card(**_UpperCAmelCase ) if __name__ == "__main__": main()
31
'''simple docstring''' import math from typing import Optional import numpy as np from ...configuration_utils import PretrainedConfig from ...utils import logging __SCREAMING_SNAKE_CASE : Dict = logging.get_logger(__name__) __SCREAMING_SNAKE_CASE : List[Any] = { """facebook/encodec_24khz""": """https://huggingface.co/facebook/encodec_24khz/resolve/main/config.json""", """facebook/encodec_48khz""": """https://huggingface.co/facebook/encodec_48khz/resolve/main/config.json""", } class lowerCamelCase_ (snake_case__ ): '''simple docstring''' __UpperCamelCase: str = "encodec" def __init__( self : Optional[int] , A : Union[str, Any]=[1.5, 3.0, 6.0, 12.0, 24.0] , A : List[Any]=24000 , A : Union[str, Any]=1 , A : List[Any]=False , A : Optional[int]=None , A : int=None , A : str=128 , A : List[Any]=32 , A : List[Any]=1 , A : int=[8, 5, 4, 2] , A : Optional[int]="weight_norm" , A : List[Any]=7 , A : Any=7 , A : Dict=3 , A : Optional[int]=2 , A : Dict=True , A : Dict="reflect" , A : Any=2 , A : Dict=2 , A : str=1.0 , A : Optional[int]=1024 , A : Any=None , A : Any=True , **A : str , ): _UpperCAmelCase : Optional[int] = target_bandwidths _UpperCAmelCase : List[str] = sampling_rate _UpperCAmelCase : Optional[int] = audio_channels _UpperCAmelCase : str = normalize _UpperCAmelCase : int = chunk_length_s _UpperCAmelCase : str = overlap _UpperCAmelCase : Optional[Any] = hidden_size _UpperCAmelCase : int = num_filters _UpperCAmelCase : Optional[Any] = num_residual_layers _UpperCAmelCase : Optional[int] = upsampling_ratios _UpperCAmelCase : int = norm_type _UpperCAmelCase : List[Any] = kernel_size _UpperCAmelCase : List[Any] = last_kernel_size _UpperCAmelCase : List[Any] = residual_kernel_size _UpperCAmelCase : List[str] = dilation_growth_rate _UpperCAmelCase : Dict = use_causal_conv _UpperCAmelCase : Tuple = pad_mode _UpperCAmelCase : Tuple = compress _UpperCAmelCase : List[str] = num_lstm_layers _UpperCAmelCase : List[Any] = trim_right_ratio _UpperCAmelCase : int = codebook_size _UpperCAmelCase : Optional[Any] = codebook_dim if codebook_dim is not None else hidden_size _UpperCAmelCase : Optional[int] = use_conv_shortcut if self.norm_type not in ["weight_norm", "time_group_norm"]: raise ValueError( F"""self.norm_type must be one of `\"weight_norm\"`, `\"time_group_norm\"`), got {self.norm_type}""" ) super().__init__(**A ) @property def _A ( self : Any ): if self.chunk_length_s is None: return None else: return int(self.chunk_length_s * self.sampling_rate ) @property def _A ( self : Union[str, Any] ): if self.chunk_length_s is None or self.overlap is None: return None else: return max(1 , int((1.0 - self.overlap) * self.chunk_length ) ) @property def _A ( self : Union[str, Any] ): _UpperCAmelCase : Dict = np.prod(self.upsampling_ratios ) return math.ceil(self.sampling_rate / hop_length ) @property def _A ( self : str ): return int(1000 * self.target_bandwidths[-1] // (self.frame_rate * 10) )
31
1
'''simple docstring''' import argparse import hashlib import os import urllib import warnings import torch from torch import nn from tqdm import tqdm from transformers import WhisperConfig, WhisperForConditionalGeneration __SCREAMING_SNAKE_CASE : List[Any] = { """tiny.en""": """https://openaipublic.azureedge.net/main/whisper/models/d3dd57d32accea0b295c96e26691aa14d8822fac7d9d27d5dc00b4ca2826dd03/tiny.en.pt""", """tiny""": """https://openaipublic.azureedge.net/main/whisper/models/65147644a518d12f04e32d6f3b26facc3f8dd46e5390956a9424a650c0ce22b9/tiny.pt""", """base.en""": """https://openaipublic.azureedge.net/main/whisper/models/25a8566e1d0c1e2231d1c762132cd20e0f96a85d16145c3a00adf5d1ac670ead/base.en.pt""", """base""": """https://openaipublic.azureedge.net/main/whisper/models/ed3a0b6b1c0edf879ad9b11b1af5a0e6ab5db9205f891f668f8b0e6c6326e34e/base.pt""", """small.en""": """https://openaipublic.azureedge.net/main/whisper/models/f953ad0fd29cacd07d5a9eda5624af0f6bcf2258be67c92b79389873d91e0872/small.en.pt""", """small""": """https://openaipublic.azureedge.net/main/whisper/models/9ecf779972d90ba49c06d968637d720dd632c55bbf19d441fb42bf17a411e794/small.pt""", """medium.en""": """https://openaipublic.azureedge.net/main/whisper/models/d7440d1dc186f76616474e0ff0b3b6b879abc9d1a4926b7adfa41db2d497ab4f/medium.en.pt""", """medium""": """https://openaipublic.azureedge.net/main/whisper/models/345ae4da62f9b3d59415adc60127b97c714f32e89e936602e85993674d08dcb1/medium.pt""", """large""": """https://openaipublic.azureedge.net/main/whisper/models/e4b87e7e0bf463eb8e6956e646f1e277e901512310def2c24bf0e11bd3c28e9a/large.pt""", """large-v2""": """https://openaipublic.azureedge.net/main/whisper/models/81f7c96c852ee8fc832187b0132e569d6c3065a3252ed18e56effd0b6a73e524/large-v2.pt""", } def UpperCamelCase_ ( _UpperCAmelCase : int ) -> Tuple: """simple docstring""" _UpperCAmelCase : Union[str, Any] = ["layers", "blocks"] for k in ignore_keys: state_dict.pop(_UpperCAmelCase , _UpperCAmelCase ) __SCREAMING_SNAKE_CASE : Tuple = { """blocks""": """layers""", """mlp.0""": """fc1""", """mlp.2""": """fc2""", """mlp_ln""": """final_layer_norm""", """.attn.query""": """.self_attn.q_proj""", """.attn.key""": """.self_attn.k_proj""", """.attn.value""": """.self_attn.v_proj""", """.attn_ln""": """.self_attn_layer_norm""", """.attn.out""": """.self_attn.out_proj""", """.cross_attn.query""": """.encoder_attn.q_proj""", """.cross_attn.key""": """.encoder_attn.k_proj""", """.cross_attn.value""": """.encoder_attn.v_proj""", """.cross_attn_ln""": """.encoder_attn_layer_norm""", """.cross_attn.out""": """.encoder_attn.out_proj""", """decoder.ln.""": """decoder.layer_norm.""", """encoder.ln.""": """encoder.layer_norm.""", """token_embedding""": """embed_tokens""", """encoder.positional_embedding""": """encoder.embed_positions.weight""", """decoder.positional_embedding""": """decoder.embed_positions.weight""", """ln_post""": """layer_norm""", } def UpperCamelCase_ ( _UpperCAmelCase : str ) -> int: """simple docstring""" _UpperCAmelCase : Tuple = list(s_dict.keys() ) for key in keys: _UpperCAmelCase : Optional[Any] = key for k, v in WHISPER_MAPPING.items(): if k in key: _UpperCAmelCase : List[Any] = new_key.replace(_UpperCAmelCase , _UpperCAmelCase ) print(F"""{key} -> {new_key}""" ) _UpperCAmelCase : Union[str, Any] = s_dict.pop(_UpperCAmelCase ) return s_dict def UpperCamelCase_ ( _UpperCAmelCase : List[Any] ) -> Optional[int]: """simple docstring""" _UpperCAmelCase , _UpperCAmelCase : int = emb.weight.shape _UpperCAmelCase : Union[str, Any] = nn.Linear(_UpperCAmelCase , _UpperCAmelCase , bias=_UpperCAmelCase ) _UpperCAmelCase : Any = emb.weight.data return lin_layer def UpperCamelCase_ ( _UpperCAmelCase : str , _UpperCAmelCase : str ) -> bytes: """simple docstring""" os.makedirs(_UpperCAmelCase , exist_ok=_UpperCAmelCase ) _UpperCAmelCase : Dict = os.path.basename(_UpperCAmelCase ) _UpperCAmelCase : Optional[Any] = url.split("/" )[-2] _UpperCAmelCase : Tuple = os.path.join(_UpperCAmelCase , _UpperCAmelCase ) if os.path.exists(_UpperCAmelCase ) and not os.path.isfile(_UpperCAmelCase ): raise RuntimeError(F"""{download_target} exists and is not a regular file""" ) if os.path.isfile(_UpperCAmelCase ): _UpperCAmelCase : Optional[int] = open(_UpperCAmelCase , "rb" ).read() if hashlib.shaaaa(_UpperCAmelCase ).hexdigest() == expected_shaaaa: return model_bytes else: warnings.warn(F"""{download_target} exists, but the SHA256 checksum does not match; re-downloading the file""" ) with urllib.request.urlopen(_UpperCAmelCase ) as source, open(_UpperCAmelCase , "wb" ) as output: with tqdm( total=int(source.info().get("Content-Length" ) ) , ncols=80 , unit="iB" , unit_scale=_UpperCAmelCase , unit_divisor=1_024 ) as loop: while True: _UpperCAmelCase : List[Any] = source.read(8_192 ) if not buffer: break output.write(_UpperCAmelCase ) loop.update(len(_UpperCAmelCase ) ) _UpperCAmelCase : int = open(_UpperCAmelCase , "rb" ).read() if hashlib.shaaaa(_UpperCAmelCase ).hexdigest() != expected_shaaaa: raise RuntimeError( "Model has been downloaded but the SHA256 checksum does not not match. Please retry loading the model." ) return model_bytes def UpperCamelCase_ ( _UpperCAmelCase : Optional[Any] , _UpperCAmelCase : Any ) -> Dict: """simple docstring""" if ".pt" not in checkpoint_path: _UpperCAmelCase : int = _download(_MODELS[checkpoint_path] ) else: _UpperCAmelCase : Optional[int] = torch.load(_UpperCAmelCase , map_location="cpu" ) _UpperCAmelCase : Optional[Any] = original_checkpoint["dims"] _UpperCAmelCase : str = original_checkpoint["model_state_dict"] _UpperCAmelCase : str = state_dict["decoder.token_embedding.weight"] remove_ignore_keys_(_UpperCAmelCase ) rename_keys(_UpperCAmelCase ) _UpperCAmelCase : Optional[Any] = True _UpperCAmelCase : List[Any] = state_dict["decoder.layers.0.fc1.weight"].shape[0] _UpperCAmelCase : int = WhisperConfig( vocab_size=dimensions["n_vocab"] , encoder_ffn_dim=_UpperCAmelCase , decoder_ffn_dim=_UpperCAmelCase , num_mel_bins=dimensions["n_mels"] , d_model=dimensions["n_audio_state"] , max_target_positions=dimensions["n_text_ctx"] , encoder_layers=dimensions["n_audio_layer"] , encoder_attention_heads=dimensions["n_audio_head"] , decoder_layers=dimensions["n_text_layer"] , decoder_attention_heads=dimensions["n_text_state"] , max_source_positions=dimensions["n_audio_ctx"] , ) _UpperCAmelCase : List[Any] = WhisperForConditionalGeneration(_UpperCAmelCase ) _UpperCAmelCase , _UpperCAmelCase : Union[str, Any] = model.model.load_state_dict(_UpperCAmelCase , strict=_UpperCAmelCase ) if len(_UpperCAmelCase ) > 0 and not set(_UpperCAmelCase ) <= { "encoder.embed_positions.weights", "decoder.embed_positions.weights", }: raise ValueError( "Only `encoder.embed_positions.weights` and `decoder.embed_positions.weights` are allowed to be missing," F""" but all the following weights are missing {missing}""" ) if tie_embeds: _UpperCAmelCase : Any = make_linear_from_emb(model.model.decoder.embed_tokens ) else: _UpperCAmelCase : Dict = proj_out_weights model.save_pretrained(_UpperCAmelCase ) if __name__ == "__main__": __SCREAMING_SNAKE_CASE : List[Any] = argparse.ArgumentParser() # # Required parameters parser.add_argument("""--checkpoint_path""", type=str, help="""Patht to the downloaded checkpoints""") parser.add_argument("""--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""") __SCREAMING_SNAKE_CASE : Optional[Any] = parser.parse_args() convert_openai_whisper_to_tfms(args.checkpoint_path, args.pytorch_dump_folder_path)
31
'''simple docstring''' from typing import Any, Dict, List, Optional, Tuple, Union import torch from torch import nn from torch.utils.data import DistributedSampler, RandomSampler from transformers import PreTrainedModel, Trainer, logging from transformers.integrations import is_fairscale_available from transformers.models.fsmt.configuration_fsmt import FSMTConfig from transformers.optimization import ( Adafactor, AdamW, get_constant_schedule, get_constant_schedule_with_warmup, get_cosine_schedule_with_warmup, get_cosine_with_hard_restarts_schedule_with_warmup, get_linear_schedule_with_warmup, get_polynomial_decay_schedule_with_warmup, ) from transformers.trainer_pt_utils import get_tpu_sampler from transformers.training_args import ParallelMode from transformers.utils import is_torch_tpu_available if is_fairscale_available(): from fairscale.optim import OSS __SCREAMING_SNAKE_CASE : str = logging.get_logger(__name__) __SCREAMING_SNAKE_CASE : Union[str, Any] = { """linear""": get_linear_schedule_with_warmup, """cosine""": get_cosine_schedule_with_warmup, """cosine_w_restarts""": get_cosine_with_hard_restarts_schedule_with_warmup, """polynomial""": get_polynomial_decay_schedule_with_warmup, """constant""": get_constant_schedule, """constant_w_warmup""": get_constant_schedule_with_warmup, } class lowerCamelCase_ (snake_case__ ): '''simple docstring''' def __init__( self : Any , A : Optional[int]=None , A : Tuple=None , *A : Tuple , **A : List[str] ): super().__init__(*A , **A ) if config is None: assert isinstance(self.model , A ), ( "If no `config` is passed the model to be trained has to be of type `PreTrainedModel`, but is" F""" {self.model.__class__}""" ) _UpperCAmelCase : str = self.model.config else: _UpperCAmelCase : List[str] = config _UpperCAmelCase : List[Any] = data_args _UpperCAmelCase : str = self.config.tgt_vocab_size if isinstance(self.config , A ) else self.config.vocab_size if self.args.label_smoothing != 0 or (self.data_args is not None and self.data_args.ignore_pad_token_for_loss): assert self.config.pad_token_id is not None, ( "Make sure that `config.pad_token_id` is correcly defined when ignoring `pad_token` for loss" " calculation or doing label smoothing." ) if self.config.pad_token_id is None and self.config.eos_token_id is not None: logger.warning( F"""The `config.pad_token_id` is `None`. Using `config.eos_token_id` = {self.config.eos_token_id} for""" " padding.." ) if self.args.label_smoothing == 0: _UpperCAmelCase : Optional[Any] = torch.nn.CrossEntropyLoss(ignore_index=self.config.pad_token_id ) else: # dynamically import label_smoothed_nll_loss from utils import label_smoothed_nll_loss _UpperCAmelCase : Dict = label_smoothed_nll_loss def _A ( self : Tuple , A : int ): if self.optimizer is None: _UpperCAmelCase : Tuple = ["bias", "LayerNorm.weight"] _UpperCAmelCase : str = [ { "params": [p for n, p in self.model.named_parameters() if not any(nd in n for nd in no_decay )], "weight_decay": self.args.weight_decay, }, { "params": [p for n, p in self.model.named_parameters() if any(nd in n for nd in no_decay )], "weight_decay": 0.0, }, ] _UpperCAmelCase : int = Adafactor if self.args.adafactor else AdamW if self.args.adafactor: _UpperCAmelCase : List[str] = Adafactor _UpperCAmelCase : List[Any] = {"scale_parameter": False, "relative_step": False} else: _UpperCAmelCase : List[str] = AdamW _UpperCAmelCase : List[str] = { "betas": (self.args.adam_betaa, self.args.adam_betaa), "eps": self.args.adam_epsilon, } _UpperCAmelCase : List[Any] = self.args.learning_rate if self.sharded_ddp: _UpperCAmelCase : List[Any] = OSS( params=A , optim=A , **A , ) else: _UpperCAmelCase : Union[str, Any] = optimizer_cls(A , **A ) if self.lr_scheduler is None: _UpperCAmelCase : List[str] = self._get_lr_scheduler(A ) else: # ignoring --lr_scheduler logger.warning("scheduler is passed to `Seq2SeqTrainer`, `--lr_scheduler` arg is ignored." ) def _A ( self : List[str] , A : Optional[int] ): _UpperCAmelCase : List[str] = arg_to_scheduler[self.args.lr_scheduler] if self.args.lr_scheduler == "constant": _UpperCAmelCase : Optional[Any] = schedule_func(self.optimizer ) elif self.args.lr_scheduler == "constant_w_warmup": _UpperCAmelCase : str = schedule_func(self.optimizer , num_warmup_steps=self.args.warmup_steps ) else: _UpperCAmelCase : str = schedule_func( self.optimizer , num_warmup_steps=self.args.warmup_steps , num_training_steps=A ) return scheduler def _A ( self : Tuple ): if isinstance(self.train_dataset , torch.utils.data.IterableDataset ): return None elif is_torch_tpu_available(): return get_tpu_sampler(self.train_dataset ) else: if self.args.sortish_sampler: self.train_dataset.make_sortish_sampler( self.args.per_device_train_batch_size , distributed=(self.args.parallel_mode == ParallelMode.DISTRIBUTED) , ) return ( RandomSampler(self.train_dataset ) if self.args.local_rank == -1 else DistributedSampler(self.train_dataset ) ) def _A ( self : Any , A : Union[str, Any] , A : Union[str, Any] , A : List[Any] ): if self.args.label_smoothing == 0: if self.data_args is not None and self.data_args.ignore_pad_token_for_loss: # force training to ignore pad token _UpperCAmelCase : List[str] = model(**A , use_cache=A )[0] _UpperCAmelCase : int = self.loss_fn(logits.view(-1 , logits.shape[-1] ) , labels.view(-1 ) ) else: # compute usual loss via models _UpperCAmelCase , _UpperCAmelCase : Any = model(**A , labels=A , use_cache=A )[:2] else: # compute label smoothed loss _UpperCAmelCase : Optional[int] = model(**A , use_cache=A )[0] _UpperCAmelCase : List[str] = torch.nn.functional.log_softmax(A , dim=-1 ) _UpperCAmelCase , _UpperCAmelCase : Union[str, Any] = self.loss_fn(A , A , self.args.label_smoothing , ignore_index=self.config.pad_token_id ) return loss, logits def _A ( self : List[str] , A : Optional[int] , A : Optional[int] ): _UpperCAmelCase : Union[str, Any] = inputs.pop("labels" ) _UpperCAmelCase , _UpperCAmelCase : Optional[int] = self._compute_loss(A , A , A ) return loss def _A ( self : List[str] , A : nn.Module , A : Dict[str, Union[torch.Tensor, Any]] , A : bool , A : Optional[List[str]] = None , ): _UpperCAmelCase : List[str] = self._prepare_inputs(A ) _UpperCAmelCase : Dict = { "max_length": self.data_args.val_max_target_length if self.data_args is not None else self.config.max_length, "num_beams": self.data_args.eval_beams if self.data_args is not None else self.config.num_beams, } if self.args.predict_with_generate and not self.args.prediction_loss_only: _UpperCAmelCase : Dict = self.model.generate( inputs["input_ids"] , attention_mask=inputs["attention_mask"] , **A , ) # in case the batch is shorter than max length, the output should be padded if generated_tokens.shape[-1] < gen_kwargs["max_length"]: _UpperCAmelCase : int = self._pad_tensors_to_max_len(A , gen_kwargs["max_length"] ) _UpperCAmelCase : Any = inputs.pop("labels" ) with torch.no_grad(): # compute loss on predict data _UpperCAmelCase , _UpperCAmelCase : str = self._compute_loss(A , A , A ) _UpperCAmelCase : List[str] = loss.mean().detach() if self.args.prediction_loss_only: return (loss, None, None) _UpperCAmelCase : str = generated_tokens if self.args.predict_with_generate else logits if labels.shape[-1] < gen_kwargs["max_length"]: _UpperCAmelCase : Optional[Any] = self._pad_tensors_to_max_len(A , gen_kwargs["max_length"] ) return (loss, logits, labels) def _A ( self : Dict , A : int , A : List[str] ): # If PAD token is not defined at least EOS token has to be defined _UpperCAmelCase : Union[str, Any] = self.config.pad_token_id if self.config.pad_token_id is not None else self.config.eos_token_id if pad_token_id is None: raise ValueError( "Make sure that either `config.pad_token_id` or `config.eos_token_id` is defined if tensor has to be" F""" padded to `max_length`={max_length}""" ) _UpperCAmelCase : Tuple = pad_token_id * torch.ones( (tensor.shape[0], max_length) , dtype=tensor.dtype , device=tensor.device ) _UpperCAmelCase : Tuple = tensor return padded_tensor
31
1
'''simple docstring''' import unittest from transformers import is_flax_available from transformers.testing_utils import require_flax, require_sentencepiece, require_tokenizers, require_torch, slow if is_flax_available(): import optax from flax.training.common_utils import onehot from transformers import AutoTokenizer, FlaxMTaForConditionalGeneration from transformers.models.ta.modeling_flax_ta import shift_tokens_right @require_torch @require_sentencepiece @require_tokenizers @require_flax class lowerCamelCase_ (unittest.TestCase ): '''simple docstring''' @slow def _A ( self : Any ): _UpperCAmelCase : List[str] = FlaxMTaForConditionalGeneration.from_pretrained("google/mt5-small" ) _UpperCAmelCase : Any = AutoTokenizer.from_pretrained("google/mt5-small" ) _UpperCAmelCase : Any = tokenizer("Hello there" , return_tensors="np" ).input_ids _UpperCAmelCase : Union[str, Any] = tokenizer("Hi I am" , return_tensors="np" ).input_ids _UpperCAmelCase : List[str] = shift_tokens_right(A , model.config.pad_token_id , model.config.decoder_start_token_id ) _UpperCAmelCase : Tuple = model(A , decoder_input_ids=A ).logits _UpperCAmelCase : Optional[int] = optax.softmax_cross_entropy(A , onehot(A , logits.shape[-1] ) ).mean() _UpperCAmelCase : str = -(labels.shape[-1] * loss.item()) _UpperCAmelCase : str = -84.9_127 self.assertTrue(abs(mtf_score - EXPECTED_SCORE ) < 1E-4 )
31
'''simple docstring''' import copy from typing import Any, Dict, List, Optional, Union import numpy as np import torch from ...audio_utils import mel_filter_bank, spectrogram, window_function from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import TensorType, logging __SCREAMING_SNAKE_CASE : List[str] = logging.get_logger(__name__) class lowerCamelCase_ (snake_case__ ): '''simple docstring''' __UpperCamelCase: Optional[int] = ["input_features", "is_longer"] def __init__( self : str , A : int=64 , A : Dict=48000 , A : str=480 , A : List[Any]=10 , A : Optional[Any]=1024 , A : Tuple=0.0 , A : List[Any]=False , A : float = 0 , A : float = 14000 , A : int = None , A : str = "fusion" , A : str = "repeatpad" , **A : Dict , ): super().__init__( feature_size=A , sampling_rate=A , padding_value=A , return_attention_mask=A , **A , ) _UpperCAmelCase : Optional[Any] = top_db _UpperCAmelCase : Dict = truncation _UpperCAmelCase : List[Any] = padding _UpperCAmelCase : Optional[Any] = fft_window_size _UpperCAmelCase : Dict = (fft_window_size >> 1) + 1 _UpperCAmelCase : Any = hop_length _UpperCAmelCase : Tuple = max_length_s _UpperCAmelCase : str = max_length_s * sampling_rate _UpperCAmelCase : Any = sampling_rate _UpperCAmelCase : Optional[int] = frequency_min _UpperCAmelCase : str = frequency_max _UpperCAmelCase : Union[str, Any] = mel_filter_bank( num_frequency_bins=self.nb_frequency_bins , num_mel_filters=A , min_frequency=A , max_frequency=A , sampling_rate=A , norm=A , mel_scale="htk" , ) _UpperCAmelCase : Tuple = mel_filter_bank( num_frequency_bins=self.nb_frequency_bins , num_mel_filters=A , min_frequency=A , max_frequency=A , sampling_rate=A , norm="slaney" , mel_scale="slaney" , ) def _A ( self : List[str] ): _UpperCAmelCase : Union[str, Any] = copy.deepcopy(self.__dict__ ) _UpperCAmelCase : Dict = self.__class__.__name__ if "mel_filters" in output: del output["mel_filters"] if "mel_filters_slaney" in output: del output["mel_filters_slaney"] return output def _A ( self : Optional[Any] , A : np.array , A : Optional[np.array] = None ): _UpperCAmelCase : Dict = spectrogram( A , window_function(self.fft_window_size , "hann" ) , frame_length=self.fft_window_size , hop_length=self.hop_length , power=2.0 , mel_filters=A , log_mel="dB" , ) return log_mel_spectrogram.T def _A ( self : str , A : str , A : List[str] , A : List[Any] ): _UpperCAmelCase : List[str] = np.array_split(list(range(0 , total_frames - chunk_frames + 1 ) ) , 3 ) if len(ranges[1] ) == 0: # if the audio is too short, we just use the first chunk _UpperCAmelCase : Optional[Any] = [0] if len(ranges[2] ) == 0: # if the audio is too short, we just use the first chunk _UpperCAmelCase : Tuple = [0] # randomly choose index for each part _UpperCAmelCase : Dict = np.random.choice(ranges[0] ) _UpperCAmelCase : str = np.random.choice(ranges[1] ) _UpperCAmelCase : Tuple = np.random.choice(ranges[2] ) _UpperCAmelCase : str = mel[idx_front : idx_front + chunk_frames, :] _UpperCAmelCase : str = mel[idx_middle : idx_middle + chunk_frames, :] _UpperCAmelCase : List[Any] = mel[idx_back : idx_back + chunk_frames, :] _UpperCAmelCase : Dict = torch.tensor(mel[None, None, :] ) _UpperCAmelCase : Optional[Any] = torch.nn.functional.interpolate( A , size=[chunk_frames, 64] , mode="bilinear" , align_corners=A ) _UpperCAmelCase : List[str] = mel_shrink[0][0].numpy() _UpperCAmelCase : str = np.stack([mel_shrink, mel_chunk_front, mel_chunk_middle, mel_chunk_back] , axis=0 ) return mel_fusion def _A ( self : List[Any] , A : np.array , A : List[str] , A : Any , A : Optional[int] ): if waveform.shape[0] > max_length: if truncation == "rand_trunc": _UpperCAmelCase : int = True # random crop to max_length (for compatibility) -> this should be handled by self.pad _UpperCAmelCase : str = len(A ) - max_length _UpperCAmelCase : str = np.random.randint(0 , overflow + 1 ) _UpperCAmelCase : int = waveform[idx : idx + max_length] _UpperCAmelCase : Any = self._np_extract_fbank_features(A , self.mel_filters_slaney )[None, :] elif truncation == "fusion": _UpperCAmelCase : Tuple = self._np_extract_fbank_features(A , self.mel_filters ) _UpperCAmelCase : List[str] = max_length // self.hop_length + 1 # the +1 related to how the spectrogram is computed _UpperCAmelCase : Optional[Any] = mel.shape[0] if chunk_frames == total_frames: # there is a corner case where the audio length is larger than max_length but smaller than max_length+hop_length. # In this case, we just use the whole audio. _UpperCAmelCase : Any = np.stack([mel, mel, mel, mel] , axis=0 ) _UpperCAmelCase : int = False else: _UpperCAmelCase : Tuple = self._random_mel_fusion(A , A , A ) _UpperCAmelCase : Any = True else: raise NotImplementedError(F"""data_truncating {truncation} not implemented""" ) else: _UpperCAmelCase : Optional[Any] = False # only use repeat as a new possible value for padding. you repeat the audio before applying the usual max_length padding if waveform.shape[0] < max_length: if padding == "repeat": _UpperCAmelCase : str = int(max_length / len(A ) ) _UpperCAmelCase : Dict = np.stack(np.tile(A , n_repeat + 1 ) )[:max_length] if padding == "repeatpad": _UpperCAmelCase : Dict = int(max_length / len(A ) ) _UpperCAmelCase : List[str] = np.stack(np.tile(A , A ) ) _UpperCAmelCase : Optional[Any] = np.pad(A , (0, max_length - waveform.shape[0]) , mode="constant" , constant_values=0 ) if truncation == "fusion": _UpperCAmelCase : str = self._np_extract_fbank_features(A , self.mel_filters ) _UpperCAmelCase : Optional[int] = np.stack([input_mel, input_mel, input_mel, input_mel] , axis=0 ) else: _UpperCAmelCase : List[str] = self._np_extract_fbank_features(A , self.mel_filters_slaney )[None, :] return input_mel, longer def __call__( self : Union[str, Any] , A : Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] , A : str = None , A : Optional[str] = None , A : Optional[int] = None , A : Optional[int] = None , A : Optional[Union[str, TensorType]] = None , **A : List[str] , ): _UpperCAmelCase : int = truncation if truncation is not None else self.truncation _UpperCAmelCase : Optional[int] = padding if padding else self.padding if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( F"""The model corresponding to this feature extractor: {self.__class__.__name__} was trained using a""" F""" sampling rate of {self.sampling_rate}. Please make sure that the provided `raw_speech` input""" F""" was sampled with {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." ) _UpperCAmelCase : Any = isinstance(A , 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}""" ) _UpperCAmelCase : Optional[Any] = is_batched_numpy or ( isinstance(A , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: _UpperCAmelCase : int = [np.asarray(A , dtype=np.floataa ) for speech in raw_speech] elif not is_batched and not isinstance(A , np.ndarray ): _UpperCAmelCase : List[str] = np.asarray(A , dtype=np.floataa ) elif isinstance(A , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): _UpperCAmelCase : Any = raw_speech.astype(np.floataa ) # always return batch if not is_batched: _UpperCAmelCase : List[str] = [np.asarray(A )] # convert to mel spectrogram, truncate and pad if needed. _UpperCAmelCase : Dict = [ self._get_input_mel(A , max_length if max_length else self.nb_max_samples , A , A ) for waveform in raw_speech ] _UpperCAmelCase : int = [] _UpperCAmelCase : Optional[Any] = [] for mel, longer in padded_inputs: input_mel.append(A ) is_longer.append(A ) if truncation == "fusion" and sum(A ) == 0: # if no audio is longer than 10s, then randomly select one audio to be longer _UpperCAmelCase : Union[str, Any] = np.random.randint(0 , len(A ) ) _UpperCAmelCase : Optional[Any] = True if isinstance(input_mel[0] , A ): _UpperCAmelCase : List[str] = [np.asarray(A , dtype=np.floataa ) for feature in input_mel] # is_longer is a list of bool _UpperCAmelCase : Tuple = [[longer] for longer in is_longer] _UpperCAmelCase : Optional[Any] = {"input_features": input_mel, "is_longer": is_longer} _UpperCAmelCase : Tuple = BatchFeature(A ) if return_tensors is not None: _UpperCAmelCase : List[Any] = input_features.convert_to_tensors(A ) return input_features
31
1
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging __SCREAMING_SNAKE_CASE : List[str] = logging.get_logger(__name__) __SCREAMING_SNAKE_CASE : Dict = { """abeja/gpt-neox-japanese-2.7b""": """https://huggingface.co/abeja/gpt-neox-japanese-2.7b/resolve/main/config.json""", } class lowerCamelCase_ (snake_case__ ): '''simple docstring''' __UpperCamelCase: Dict = "gpt_neox_japanese" def __init__( self : Any , A : str=32000 , A : Tuple=2560 , A : Dict=32 , A : Optional[Any]=32 , A : Any=4 , A : Any="gelu" , A : Tuple=1.00 , A : List[Any]=10000 , A : Optional[Any]=2048 , A : Optional[int]=0.02 , A : Optional[int]=1E-5 , A : Tuple=True , A : str=31996 , A : Tuple=31999 , A : Dict=0.1 , A : Optional[Any]=0.0 , **A : int , ): super().__init__(bos_token_id=A , eos_token_id=A , **A ) _UpperCAmelCase : Optional[int] = vocab_size _UpperCAmelCase : List[Any] = max_position_embeddings _UpperCAmelCase : Dict = hidden_size _UpperCAmelCase : Union[str, Any] = num_hidden_layers _UpperCAmelCase : Tuple = num_attention_heads _UpperCAmelCase : str = intermediate_multiple_size _UpperCAmelCase : Tuple = hidden_act _UpperCAmelCase : Union[str, Any] = rotary_pct _UpperCAmelCase : Dict = rotary_emb_base _UpperCAmelCase : Optional[Any] = initializer_range _UpperCAmelCase : Tuple = layer_norm_eps _UpperCAmelCase : Any = use_cache _UpperCAmelCase : str = attention_dropout _UpperCAmelCase : Optional[Any] = hidden_dropout
31
'''simple docstring''' from typing import TYPE_CHECKING from ...file_utils import _LazyModule, is_tokenizers_available, is_torch_available from ...utils import OptionalDependencyNotAvailable __SCREAMING_SNAKE_CASE : Optional[int] = {"""configuration_gpt_neox""": ["""GPT_NEOX_PRETRAINED_CONFIG_ARCHIVE_MAP""", """GPTNeoXConfig"""]} try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __SCREAMING_SNAKE_CASE : Tuple = ["""GPTNeoXTokenizerFast"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __SCREAMING_SNAKE_CASE : Dict = [ """GPT_NEOX_PRETRAINED_MODEL_ARCHIVE_LIST""", """GPTNeoXForCausalLM""", """GPTNeoXForQuestionAnswering""", """GPTNeoXForSequenceClassification""", """GPTNeoXForTokenClassification""", """GPTNeoXLayer""", """GPTNeoXModel""", """GPTNeoXPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_gpt_neox import GPT_NEOX_PRETRAINED_CONFIG_ARCHIVE_MAP, GPTNeoXConfig try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_gpt_neox_fast import GPTNeoXTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_gpt_neox import ( GPT_NEOX_PRETRAINED_MODEL_ARCHIVE_LIST, GPTNeoXForCausalLM, GPTNeoXForQuestionAnswering, GPTNeoXForSequenceClassification, GPTNeoXForTokenClassification, GPTNeoXLayer, GPTNeoXModel, GPTNeoXPreTrainedModel, ) else: import sys __SCREAMING_SNAKE_CASE : Any = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
31
1
'''simple docstring''' import unittest from transformers import AlbertTokenizer, AlbertTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin __SCREAMING_SNAKE_CASE : Tuple = get_tests_dir("""fixtures/spiece.model""") @require_sentencepiece @require_tokenizers class lowerCamelCase_ (snake_case__ , unittest.TestCase ): '''simple docstring''' __UpperCamelCase: Tuple = AlbertTokenizer __UpperCamelCase: Optional[Any] = AlbertTokenizerFast __UpperCamelCase: str = True __UpperCamelCase: Optional[int] = True __UpperCamelCase: Any = True def _A ( self : int ): super().setUp() # We have a SentencePiece fixture for testing _UpperCAmelCase : Union[str, Any] = AlbertTokenizer(A ) tokenizer.save_pretrained(self.tmpdirname ) def _A ( self : Dict , A : int ): _UpperCAmelCase : Tuple = "this is a test" _UpperCAmelCase : List[str] = "this is a test" return input_text, output_text def _A ( self : int ): _UpperCAmelCase : int = "<pad>" _UpperCAmelCase : Any = 0 self.assertEqual(self.get_tokenizer()._convert_token_to_id(A ) , A ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(A ) , A ) def _A ( self : Optional[int] ): _UpperCAmelCase : Dict = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , "<pad>" ) self.assertEqual(vocab_keys[1] , "<unk>" ) self.assertEqual(vocab_keys[-1] , "▁eloquent" ) self.assertEqual(len(A ) , 30000 ) def _A ( self : Tuple ): self.assertEqual(self.get_tokenizer().vocab_size , 30000 ) def _A ( self : int ): if not self.test_rust_tokenizer: return _UpperCAmelCase : int = self.get_tokenizer() _UpperCAmelCase : str = self.get_rust_tokenizer() _UpperCAmelCase : Optional[Any] = "I was born in 92000, and this is falsé." _UpperCAmelCase : List[str] = tokenizer.tokenize(A ) _UpperCAmelCase : str = rust_tokenizer.tokenize(A ) self.assertListEqual(A , A ) _UpperCAmelCase : List[str] = tokenizer.encode(A , add_special_tokens=A ) _UpperCAmelCase : Optional[int] = rust_tokenizer.encode(A , add_special_tokens=A ) self.assertListEqual(A , A ) _UpperCAmelCase : Optional[int] = self.get_rust_tokenizer() _UpperCAmelCase : Dict = tokenizer.encode(A ) _UpperCAmelCase : str = rust_tokenizer.encode(A ) self.assertListEqual(A , A ) def _A ( self : Tuple ): _UpperCAmelCase : int = AlbertTokenizer(A , keep_accents=A ) _UpperCAmelCase : Any = tokenizer.tokenize("This is a test" ) self.assertListEqual(A , ["▁this", "▁is", "▁a", "▁test"] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(A ) , [48, 25, 21, 1289] ) _UpperCAmelCase : Any = tokenizer.tokenize("I was born in 92000, and this is falsé." ) self.assertListEqual( A , ["▁i", "▁was", "▁born", "▁in", "▁9", "2000", ",", "▁and", "▁this", "▁is", "▁fal", "s", "é", "."] ) _UpperCAmelCase : Optional[int] = tokenizer.convert_tokens_to_ids(A ) self.assertListEqual(A , [31, 23, 386, 19, 561, 3050, 15, 17, 48, 25, 8256, 18, 1, 9] ) _UpperCAmelCase : List[str] = tokenizer.convert_ids_to_tokens(A ) self.assertListEqual( A , ["▁i", "▁was", "▁born", "▁in", "▁9", "2000", ",", "▁and", "▁this", "▁is", "▁fal", "s", "<unk>", "."] , ) def _A ( self : Tuple ): _UpperCAmelCase : List[str] = AlbertTokenizer(A ) _UpperCAmelCase : Optional[int] = tokenizer.encode("sequence builders" ) _UpperCAmelCase : Dict = tokenizer.encode("multi-sequence build" ) _UpperCAmelCase : str = tokenizer.build_inputs_with_special_tokens(A ) _UpperCAmelCase : Optional[int] = tokenizer.build_inputs_with_special_tokens(A , A ) assert encoded_sentence == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] assert encoded_pair == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] + text_a + [ tokenizer.sep_token_id ] @slow def _A ( self : Dict ): # fmt: off _UpperCAmelCase : int = {"attention_mask": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], "input_ids": [[2, 21970, 13, 5, 6092, 167, 28, 7103, 2153, 673, 8, 7028, 12051, 18, 17, 7103, 2153, 673, 8, 3515, 18684, 8, 4461, 6, 1927, 297, 8, 12060, 2607, 18, 13, 5, 4461, 15, 10538, 38, 8, 135, 15, 822, 58, 15, 993, 10363, 15, 1460, 8005, 4461, 15, 993, 255, 2328, 9, 9, 9, 6, 26, 1112, 816, 3260, 13, 5, 103, 2377, 6, 17, 1112, 816, 2782, 13, 5, 103, 10641, 6, 29, 84, 2512, 2430, 782, 18684, 2761, 19, 808, 2430, 2556, 17, 855, 1480, 9477, 4091, 128, 11712, 15, 7103, 2153, 673, 17, 24883, 9990, 9, 3], [2, 11502, 25, 1006, 20, 782, 8, 11809, 855, 1732, 19393, 18667, 37, 367, 21018, 69, 1854, 34, 11860, 19124, 27, 156, 225, 17, 193, 4141, 19, 65, 9124, 9, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [2, 14, 2231, 886, 2385, 17659, 84, 14, 16792, 1952, 9, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], "token_type_ids": [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=A , model_name="albert-base-v2" , revision="6b6560eaf5ff2e250b00c50f380c5389a9c2d82e" , )
31
'''simple docstring''' class lowerCamelCase_ : '''simple docstring''' def __init__( self : Tuple , A : Any , A : str , A : Union[str, Any] ): _UpperCAmelCase : Optional[int] = None _UpperCAmelCase : Optional[int] = None _UpperCAmelCase : Any = graph self._normalize_graph(A , A ) _UpperCAmelCase : List[str] = len(A ) _UpperCAmelCase : Tuple = None def _A ( self : Any , A : List[Any] , A : str ): if sources is int: _UpperCAmelCase : List[Any] = [sources] if sinks is int: _UpperCAmelCase : List[Any] = [sinks] if len(A ) == 0 or len(A ) == 0: return _UpperCAmelCase : str = sources[0] _UpperCAmelCase : Union[str, Any] = sinks[0] # make fake vertex if there are more # than one source or sink if len(A ) > 1 or len(A ) > 1: _UpperCAmelCase : Dict = 0 for i in sources: max_input_flow += sum(self.graph[i] ) _UpperCAmelCase : str = len(self.graph ) + 1 for room in self.graph: room.insert(0 , 0 ) self.graph.insert(0 , [0] * size ) for i in sources: _UpperCAmelCase : Optional[Any] = max_input_flow _UpperCAmelCase : List[str] = 0 _UpperCAmelCase : str = len(self.graph ) + 1 for room in self.graph: room.append(0 ) self.graph.append([0] * size ) for i in sinks: _UpperCAmelCase : Dict = max_input_flow _UpperCAmelCase : List[Any] = size - 1 def _A ( self : Union[str, Any] ): 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 _A ( self : Tuple , A : Dict ): _UpperCAmelCase : str = algorithm(self ) class lowerCamelCase_ : '''simple docstring''' def __init__( self : Any , A : str ): _UpperCAmelCase : Optional[int] = flow_network _UpperCAmelCase : Any = flow_network.verticesCount _UpperCAmelCase : List[str] = flow_network.sourceIndex _UpperCAmelCase : Union[str, Any] = flow_network.sinkIndex # it's just a reference, so you shouldn't change # it in your algorithms, use deep copy before doing that _UpperCAmelCase : Any = flow_network.graph _UpperCAmelCase : Union[str, Any] = False def _A ( self : List[str] ): if not self.executed: self._algorithm() _UpperCAmelCase : int = True def _A ( self : List[Any] ): pass class lowerCamelCase_ (snake_case__ ): '''simple docstring''' def __init__( self : Optional[int] , A : Union[str, Any] ): super().__init__(A ) # use this to save your result _UpperCAmelCase : Any = -1 def _A ( self : Union[str, Any] ): if not self.executed: raise Exception("You should execute algorithm before using its result!" ) return self.maximum_flow class lowerCamelCase_ (snake_case__ ): '''simple docstring''' def __init__( self : Tuple , A : int ): super().__init__(A ) _UpperCAmelCase : List[str] = [[0] * self.verticies_count for i in range(self.verticies_count )] _UpperCAmelCase : Union[str, Any] = [0] * self.verticies_count _UpperCAmelCase : int = [0] * self.verticies_count def _A ( self : Dict ): _UpperCAmelCase : Dict = 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 _UpperCAmelCase : Optional[int] = [ i for i in range(self.verticies_count ) if i != self.source_index and i != self.sink_index ] # move through list _UpperCAmelCase : Any = 0 while i < len(A ): _UpperCAmelCase : int = vertices_list[i] _UpperCAmelCase : int = self.heights[vertex_index] self.process_vertex(A ) 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(A ) ) _UpperCAmelCase : Union[str, Any] = 0 else: i += 1 _UpperCAmelCase : List[Any] = sum(self.preflow[self.source_index] ) def _A ( self : Union[str, Any] , A : str ): 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(A , A ) self.relabel(A ) def _A ( self : int , A : Dict , A : List[str] ): _UpperCAmelCase : 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 _A ( self : Optional[int] , A : Union[str, Any] ): _UpperCAmelCase : str = 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): _UpperCAmelCase : Tuple = self.heights[to_index] if min_height is not None: _UpperCAmelCase : Optional[Any] = min_height + 1 if __name__ == "__main__": __SCREAMING_SNAKE_CASE : Optional[int] = [0] __SCREAMING_SNAKE_CASE : Union[str, Any] = [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], # ] __SCREAMING_SNAKE_CASE : List[Any] = [[0, 7, 0, 0], [0, 0, 6, 0], [0, 0, 0, 8], [9, 0, 0, 0]] # prepare our network __SCREAMING_SNAKE_CASE : Union[str, Any] = FlowNetwork(graph, entrances, exits) # set algorithm flow_network.set_maximum_flow_algorithm(PushRelabelExecutor) # and calculate __SCREAMING_SNAKE_CASE : Optional[Any] = flow_network.find_maximum_flow() print(F'maximum flow is {maximum_flow}')
31
1
'''simple docstring''' import json import logging import os import sys from time import time from unittest.mock import patch from transformers.testing_utils import TestCasePlus, require_torch_tpu logging.basicConfig(level=logging.DEBUG) __SCREAMING_SNAKE_CASE : Dict = logging.getLogger() def UpperCamelCase_ ( _UpperCAmelCase : str ) -> Dict: """simple docstring""" _UpperCAmelCase : List[Any] = {} _UpperCAmelCase : Union[str, Any] = os.path.join(_UpperCAmelCase , "all_results.json" ) if os.path.exists(_UpperCAmelCase ): with open(_UpperCAmelCase , "r" ) as f: _UpperCAmelCase : Optional[Any] = json.load(_UpperCAmelCase ) else: raise ValueError(F"""can't find {path}""" ) return results __SCREAMING_SNAKE_CASE : Optional[Any] = logging.StreamHandler(sys.stdout) logger.addHandler(stream_handler) @require_torch_tpu class lowerCamelCase_ (snake_case__ ): '''simple docstring''' def _A ( self : List[str] ): import xla_spawn _UpperCAmelCase : List[Any] = self.get_auto_remove_tmp_dir() _UpperCAmelCase : Union[str, Any] = F""" ./examples/pytorch/text-classification/run_glue.py --num_cores=8 ./examples/pytorch/text-classification/run_glue.py --model_name_or_path distilbert-base-uncased --output_dir {tmp_dir} --overwrite_output_dir --train_file ./tests/fixtures/tests_samples/MRPC/train.csv --validation_file ./tests/fixtures/tests_samples/MRPC/dev.csv --do_train --do_eval --debug tpu_metrics_debug --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --learning_rate=1e-4 --max_steps=10 --warmup_steps=2 --seed=42 --max_seq_length=128 """.split() with patch.object(A , "argv" , A ): _UpperCAmelCase : Dict = time() xla_spawn.main() _UpperCAmelCase : List[str] = time() _UpperCAmelCase : List[Any] = get_results(A ) self.assertGreaterEqual(result["eval_accuracy"] , 0.75 ) # Assert that the script takes less than 500 seconds to make sure it doesn't hang. self.assertLess(end - start , 500 ) def _A ( self : int ): import xla_spawn _UpperCAmelCase : int = "\n ./tests/test_trainer_tpu.py\n --num_cores=8\n ./tests/test_trainer_tpu.py\n ".split() with patch.object(A , "argv" , A ): xla_spawn.main()
31
'''simple docstring''' def UpperCamelCase_ ( _UpperCAmelCase : str , _UpperCAmelCase : str ) -> float: """simple docstring""" def get_matched_characters(_UpperCAmelCase : str , _UpperCAmelCase : str ) -> str: _UpperCAmelCase : Tuple = [] _UpperCAmelCase : Dict = min(len(_stra ) , len(_stra ) ) // 2 for i, l in enumerate(_stra ): _UpperCAmelCase : int = int(max(0 , i - limit ) ) _UpperCAmelCase : Any = int(min(i + limit + 1 , len(_stra ) ) ) if l in _stra[left:right]: matched.append(_UpperCAmelCase ) _UpperCAmelCase : List[Any] = F"""{_stra[0:_stra.index(_UpperCAmelCase )]} {_stra[_stra.index(_UpperCAmelCase ) + 1:]}""" return "".join(_UpperCAmelCase ) # matching characters _UpperCAmelCase : Union[str, Any] = get_matched_characters(_UpperCAmelCase , _UpperCAmelCase ) _UpperCAmelCase : Tuple = get_matched_characters(_UpperCAmelCase , _UpperCAmelCase ) _UpperCAmelCase : Tuple = len(_UpperCAmelCase ) # transposition _UpperCAmelCase : Optional[Any] = ( len([(ca, ca) for ca, ca in zip(_UpperCAmelCase , _UpperCAmelCase ) if ca != ca] ) // 2 ) if not match_count: _UpperCAmelCase : Dict = 0.0 else: _UpperCAmelCase : Optional[int] = ( 1 / 3 * ( match_count / len(_UpperCAmelCase ) + match_count / len(_UpperCAmelCase ) + (match_count - transpositions) / match_count ) ) # common prefix up to 4 characters _UpperCAmelCase : str = 0 for ca, ca in zip(stra[:4] , stra[:4] ): if ca == ca: prefix_len += 1 else: break return jaro + 0.1 * prefix_len * (1 - jaro) if __name__ == "__main__": import doctest doctest.testmod() print(jaro_winkler("""hello""", """world"""))
31
1
'''simple docstring''' import logging import os from dataclasses import dataclass from typing import List, Optional, Union import tqdm from filelock import FileLock from transformers import ( BartTokenizer, BartTokenizerFast, DataProcessor, PreTrainedTokenizer, RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, is_tf_available, is_torch_available, ) __SCREAMING_SNAKE_CASE : Any = logging.getLogger(__name__) @dataclass(frozen=snake_case__ ) class lowerCamelCase_ : '''simple docstring''' __UpperCamelCase: str __UpperCamelCase: str __UpperCamelCase: Optional[str] = None __UpperCamelCase: Optional[str] = None __UpperCamelCase: Optional[str] = None @dataclass(frozen=snake_case__ ) class lowerCamelCase_ : '''simple docstring''' __UpperCamelCase: List[int] __UpperCamelCase: Optional[List[int]] = None __UpperCamelCase: Optional[List[int]] = None __UpperCamelCase: Optional[Union[int, float]] = None __UpperCamelCase: Optional[int] = None if is_torch_available(): import torch from torch.utils.data import Dataset class lowerCamelCase_ (snake_case__ ): '''simple docstring''' __UpperCamelCase: List[InputFeatures] def __init__( self : Optional[Any] , A : str , A : PreTrainedTokenizer , A : str , A : Optional[int] = None , A : List[Any]=False , A : bool = False , ): _UpperCAmelCase : Optional[int] = hans_processors[task]() _UpperCAmelCase : int = os.path.join( A , "cached_{}_{}_{}_{}".format( "dev" if evaluate else "train" , tokenizer.__class__.__name__ , str(A ) , A , ) , ) _UpperCAmelCase : List[Any] = processor.get_labels() if tokenizer.__class__ in ( RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, BartTokenizer, BartTokenizerFast, ): # HACK(label indices are swapped in RoBERTa pretrained model) _UpperCAmelCase , _UpperCAmelCase : Tuple = label_list[2], label_list[1] _UpperCAmelCase : Tuple = label_list # Make sure only the first process in distributed training processes the dataset, # and the others will use the cache. _UpperCAmelCase : List[str] = cached_features_file + ".lock" with FileLock(A ): if os.path.exists(A ) and not overwrite_cache: logger.info(F"""Loading features from cached file {cached_features_file}""" ) _UpperCAmelCase : str = torch.load(A ) else: logger.info(F"""Creating features from dataset file at {data_dir}""" ) _UpperCAmelCase : Optional[int] = ( processor.get_dev_examples(A ) if evaluate else processor.get_train_examples(A ) ) logger.info("Training examples: %s" , len(A ) ) _UpperCAmelCase : Optional[Any] = hans_convert_examples_to_features(A , A , A , A ) logger.info("Saving features into cached file %s" , A ) torch.save(self.features , A ) def __len__( self : Tuple ): return len(self.features ) def __getitem__( self : Optional[int] , A : str ): return self.features[i] def _A ( self : Optional[int] ): return self.label_list if is_tf_available(): import tensorflow as tf class lowerCamelCase_ : '''simple docstring''' __UpperCamelCase: List[InputFeatures] def __init__( self : Any , A : str , A : PreTrainedTokenizer , A : str , A : Optional[int] = 128 , A : str=False , A : bool = False , ): _UpperCAmelCase : List[str] = hans_processors[task]() _UpperCAmelCase : Optional[Any] = processor.get_labels() if tokenizer.__class__ in ( RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, BartTokenizer, BartTokenizerFast, ): # HACK(label indices are swapped in RoBERTa pretrained model) _UpperCAmelCase , _UpperCAmelCase : int = label_list[2], label_list[1] _UpperCAmelCase : Optional[Any] = label_list _UpperCAmelCase : Tuple = processor.get_dev_examples(A ) if evaluate else processor.get_train_examples(A ) _UpperCAmelCase : List[str] = hans_convert_examples_to_features(A , A , A , A ) def gen(): for ex_index, ex in tqdm.tqdm(enumerate(self.features ) , desc="convert examples to features" ): if ex_index % 10000 == 0: logger.info("Writing example %d of %d" % (ex_index, len(A )) ) yield ( { "example_id": 0, "input_ids": ex.input_ids, "attention_mask": ex.attention_mask, "token_type_ids": ex.token_type_ids, }, ex.label, ) _UpperCAmelCase : List[str] = tf.data.Dataset.from_generator( A , ( { "example_id": tf.intaa, "input_ids": tf.intaa, "attention_mask": tf.intaa, "token_type_ids": tf.intaa, }, tf.intaa, ) , ( { "example_id": tf.TensorShape([] ), "input_ids": tf.TensorShape([None, None] ), "attention_mask": tf.TensorShape([None, None] ), "token_type_ids": tf.TensorShape([None, None] ), }, tf.TensorShape([] ), ) , ) def _A ( self : Optional[int] ): return self.dataset def __len__( self : Optional[Any] ): return len(self.features ) def __getitem__( self : int , A : List[Any] ): return self.features[i] def _A ( self : Optional[int] ): return self.label_list class lowerCamelCase_ (snake_case__ ): '''simple docstring''' def _A ( self : Optional[Any] , A : Union[str, Any] ): return self._create_examples(self._read_tsv(os.path.join(A , "heuristics_train_set.txt" ) ) , "train" ) def _A ( self : Optional[int] , A : List[str] ): return self._create_examples(self._read_tsv(os.path.join(A , "heuristics_evaluation_set.txt" ) ) , "dev" ) def _A ( self : Optional[int] ): return ["contradiction", "entailment", "neutral"] def _A ( self : Tuple , A : Optional[int] , A : Tuple ): _UpperCAmelCase : List[Any] = [] for i, line in enumerate(A ): if i == 0: continue _UpperCAmelCase : int = "%s-%s" % (set_type, line[0]) _UpperCAmelCase : int = line[5] _UpperCAmelCase : Tuple = line[6] _UpperCAmelCase : Optional[Any] = line[7][2:] if line[7].startswith("ex" ) else line[7] _UpperCAmelCase : int = line[0] examples.append(InputExample(guid=A , text_a=A , text_b=A , label=A , pairID=A ) ) return examples def UpperCamelCase_ ( _UpperCAmelCase : List[InputExample] , _UpperCAmelCase : List[str] , _UpperCAmelCase : int , _UpperCAmelCase : PreTrainedTokenizer , ) -> str: """simple docstring""" _UpperCAmelCase : Optional[Any] = {label: i for i, label in enumerate(_UpperCAmelCase )} _UpperCAmelCase : str = [] for ex_index, example in tqdm.tqdm(enumerate(_UpperCAmelCase ) , desc="convert examples to features" ): if ex_index % 10_000 == 0: logger.info("Writing example %d" % (ex_index) ) _UpperCAmelCase : str = tokenizer( example.text_a , example.text_b , add_special_tokens=_UpperCAmelCase , max_length=_UpperCAmelCase , padding="max_length" , truncation=_UpperCAmelCase , return_overflowing_tokens=_UpperCAmelCase , ) _UpperCAmelCase : Dict = label_map[example.label] if example.label in label_map else 0 _UpperCAmelCase : Dict = int(example.pairID ) features.append(InputFeatures(**_UpperCAmelCase , label=_UpperCAmelCase , pairID=_UpperCAmelCase ) ) for i, example in enumerate(examples[:5] ): logger.info("*** Example ***" ) logger.info(F"""guid: {example}""" ) logger.info(F"""features: {features[i]}""" ) return features __SCREAMING_SNAKE_CASE : Optional[Any] = { """hans""": 3, } __SCREAMING_SNAKE_CASE : Optional[Any] = { """hans""": HansProcessor, }
31
'''simple docstring''' 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_ (snake_case__ , snake_case__ ): '''simple docstring''' __UpperCamelCase: Optional[Any] = 1 @register_to_config def __init__( self : Optional[int] , A : int = 1000 , A : Optional[Union[np.ndarray, List[float]]] = None ): # set `betas`, `alphas`, `timesteps` self.set_timesteps(A ) # standard deviation of the initial noise distribution _UpperCAmelCase : int = 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. _UpperCAmelCase : int = 4 # running values _UpperCAmelCase : Dict = [] def _A ( self : Optional[int] , A : int , A : Union[str, torch.device] = None ): _UpperCAmelCase : int = num_inference_steps _UpperCAmelCase : Union[str, Any] = torch.linspace(1 , 0 , num_inference_steps + 1 )[:-1] _UpperCAmelCase : Any = torch.cat([steps, torch.tensor([0.0] )] ) if self.config.trained_betas is not None: _UpperCAmelCase : str = torch.tensor(self.config.trained_betas , dtype=torch.floataa ) else: _UpperCAmelCase : Dict = torch.sin(steps * math.pi / 2 ) ** 2 _UpperCAmelCase : List[Any] = (1.0 - self.betas**2) ** 0.5 _UpperCAmelCase : List[str] = (torch.atana(self.betas , self.alphas ) / math.pi * 2)[:-1] _UpperCAmelCase : Dict = timesteps.to(A ) _UpperCAmelCase : Dict = [] def _A ( self : Optional[int] , A : torch.FloatTensor , A : int , A : torch.FloatTensor , A : bool = True , ): 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" ) _UpperCAmelCase : Tuple = (self.timesteps == timestep).nonzero().item() _UpperCAmelCase : Optional[Any] = timestep_index + 1 _UpperCAmelCase : int = sample * self.betas[timestep_index] + model_output * self.alphas[timestep_index] self.ets.append(A ) if len(self.ets ) == 1: _UpperCAmelCase : List[Any] = self.ets[-1] elif len(self.ets ) == 2: _UpperCAmelCase : str = (3 * self.ets[-1] - self.ets[-2]) / 2 elif len(self.ets ) == 3: _UpperCAmelCase : Tuple = (23 * self.ets[-1] - 16 * self.ets[-2] + 5 * self.ets[-3]) / 12 else: _UpperCAmelCase : Union[str, Any] = (1 / 24) * (55 * self.ets[-1] - 59 * self.ets[-2] + 37 * self.ets[-3] - 9 * self.ets[-4]) _UpperCAmelCase : Union[str, Any] = self._get_prev_sample(A , A , A , A ) if not return_dict: return (prev_sample,) return SchedulerOutput(prev_sample=A ) def _A ( self : Union[str, Any] , A : torch.FloatTensor , *A : Union[str, Any] , **A : Dict ): return sample def _A ( self : Optional[Any] , A : Optional[int] , A : int , A : Optional[Any] , A : List[str] ): _UpperCAmelCase : List[str] = self.alphas[timestep_index] _UpperCAmelCase : List[Any] = self.betas[timestep_index] _UpperCAmelCase : Optional[Any] = self.alphas[prev_timestep_index] _UpperCAmelCase : Dict = self.betas[prev_timestep_index] _UpperCAmelCase : Tuple = (sample - sigma * ets) / max(A , 1E-8 ) _UpperCAmelCase : List[str] = next_alpha * pred + ets * next_sigma return prev_sample def __len__( self : Union[str, Any] ): return self.config.num_train_timesteps
31
1
'''simple docstring''' import os from typing import Dict, List, Tuple, TypeVar, Union __SCREAMING_SNAKE_CASE : str = TypeVar("""T""") __SCREAMING_SNAKE_CASE : int = Union[List[T], Tuple[T, ...]] __SCREAMING_SNAKE_CASE : Any = Union[T, List[T], Dict[str, T]] __SCREAMING_SNAKE_CASE : str = Union[str, bytes, os.PathLike]
31
'''simple docstring''' import numpy as np from matplotlib import pyplot as plt from sklearn.datasets import load_iris from sklearn.metrics import ConfusionMatrixDisplay from sklearn.model_selection import train_test_split from xgboost import XGBClassifier def UpperCamelCase_ ( _UpperCAmelCase : dict ) -> tuple: """simple docstring""" return (data["data"], data["target"]) def UpperCamelCase_ ( _UpperCAmelCase : np.ndarray , _UpperCAmelCase : np.ndarray ) -> XGBClassifier: """simple docstring""" _UpperCAmelCase : Any = XGBClassifier() classifier.fit(_UpperCAmelCase , _UpperCAmelCase ) return classifier def UpperCamelCase_ ( ) -> None: """simple docstring""" _UpperCAmelCase : List[str] = load_iris() _UpperCAmelCase , _UpperCAmelCase : Dict = data_handling(_UpperCAmelCase ) _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase : str = train_test_split( _UpperCAmelCase , _UpperCAmelCase , test_size=0.2_5 ) _UpperCAmelCase : Optional[Any] = iris["target_names"] # Create an XGBoost Classifier from the training data _UpperCAmelCase : Tuple = xgboost(_UpperCAmelCase , _UpperCAmelCase ) # Display the confusion matrix of the classifier with both training and test sets ConfusionMatrixDisplay.from_estimator( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , display_labels=_UpperCAmelCase , cmap="Blues" , normalize="true" , ) plt.title("Normalized Confusion Matrix - IRIS Dataset" ) plt.show() if __name__ == "__main__": import doctest doctest.testmod(verbose=True) main()
31
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, apply_forward_hook from .modeling_utils import ModelMixin from .vae import Decoder, DecoderOutput, Encoder, VectorQuantizer @dataclass class lowerCamelCase_ (snake_case__ ): '''simple docstring''' __UpperCamelCase: torch.FloatTensor class lowerCamelCase_ (snake_case__ , snake_case__ ): '''simple docstring''' @register_to_config def __init__( self : Optional[Any] , A : int = 3 , A : int = 3 , A : Tuple[str] = ("DownEncoderBlock2D",) , A : Tuple[str] = ("UpDecoderBlock2D",) , A : Tuple[int] = (64,) , A : int = 1 , A : str = "silu" , A : int = 3 , A : int = 32 , A : int = 256 , A : int = 32 , A : Optional[int] = None , A : float = 0.18_215 , A : str = "group" , ): super().__init__() # pass init params to Encoder _UpperCAmelCase : Any = Encoder( in_channels=A , out_channels=A , down_block_types=A , block_out_channels=A , layers_per_block=A , act_fn=A , norm_num_groups=A , double_z=A , ) _UpperCAmelCase : Dict = vq_embed_dim if vq_embed_dim is not None else latent_channels _UpperCAmelCase : Tuple = nn.Convad(A , A , 1 ) _UpperCAmelCase : Union[str, Any] = VectorQuantizer(A , A , beta=0.25 , remap=A , sane_index_shape=A ) _UpperCAmelCase : str = nn.Convad(A , A , 1 ) # pass init params to Decoder _UpperCAmelCase : List[Any] = Decoder( in_channels=A , out_channels=A , up_block_types=A , block_out_channels=A , layers_per_block=A , act_fn=A , norm_num_groups=A , norm_type=A , ) @apply_forward_hook def _A ( self : List[str] , A : torch.FloatTensor , A : bool = True ): _UpperCAmelCase : List[Any] = self.encoder(A ) _UpperCAmelCase : List[Any] = self.quant_conv(A ) if not return_dict: return (h,) return VQEncoderOutput(latents=A ) @apply_forward_hook def _A ( self : Optional[Any] , A : torch.FloatTensor , A : bool = False , A : bool = True ): # also go through quantization layer if not force_not_quantize: _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase : Dict = self.quantize(A ) else: _UpperCAmelCase : Tuple = h _UpperCAmelCase : Dict = self.post_quant_conv(A ) _UpperCAmelCase : Tuple = self.decoder(A , quant if self.config.norm_type == "spatial" else None ) if not return_dict: return (dec,) return DecoderOutput(sample=A ) def _A ( self : Union[str, Any] , A : torch.FloatTensor , A : bool = True ): _UpperCAmelCase : str = sample _UpperCAmelCase : Optional[Any] = self.encode(A ).latents _UpperCAmelCase : List[Any] = self.decode(A ).sample if not return_dict: return (dec,) return DecoderOutput(sample=A )
31
'''simple docstring''' import math import unittest from transformers import BioGptConfig, 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, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( BioGptForCausalLM, BioGptForSequenceClassification, BioGptForTokenClassification, BioGptModel, BioGptTokenizer, ) from transformers.models.biogpt.modeling_biogpt import BIOGPT_PRETRAINED_MODEL_ARCHIVE_LIST class lowerCamelCase_ : '''simple docstring''' def __init__( self : List[Any] , A : Dict , A : Optional[Any]=13 , A : Optional[Any]=7 , A : Union[str, Any]=True , A : Optional[Any]=True , A : int=False , A : str=True , A : Optional[Any]=99 , A : Union[str, Any]=32 , A : int=5 , A : Tuple=4 , A : Union[str, Any]=37 , A : Dict="gelu" , A : Union[str, Any]=0.1 , A : str=0.1 , A : Union[str, Any]=512 , A : int=16 , A : List[str]=2 , A : Tuple=0.02 , A : int=3 , A : List[str]=4 , A : str=None , ): _UpperCAmelCase : List[Any] = parent _UpperCAmelCase : Any = batch_size _UpperCAmelCase : int = seq_length _UpperCAmelCase : Union[str, Any] = is_training _UpperCAmelCase : Any = use_input_mask _UpperCAmelCase : Optional[Any] = use_token_type_ids _UpperCAmelCase : str = use_labels _UpperCAmelCase : Union[str, Any] = vocab_size _UpperCAmelCase : Tuple = hidden_size _UpperCAmelCase : Union[str, Any] = num_hidden_layers _UpperCAmelCase : Optional[Any] = num_attention_heads _UpperCAmelCase : Union[str, Any] = intermediate_size _UpperCAmelCase : Union[str, Any] = hidden_act _UpperCAmelCase : List[Any] = hidden_dropout_prob _UpperCAmelCase : List[Any] = attention_probs_dropout_prob _UpperCAmelCase : Optional[int] = max_position_embeddings _UpperCAmelCase : str = type_vocab_size _UpperCAmelCase : str = type_sequence_label_size _UpperCAmelCase : int = initializer_range _UpperCAmelCase : Optional[Any] = num_labels _UpperCAmelCase : List[str] = num_choices _UpperCAmelCase : List[str] = scope def _A ( self : Optional[int] ): _UpperCAmelCase : Dict = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _UpperCAmelCase : Union[str, Any] = None if self.use_input_mask: _UpperCAmelCase : List[Any] = random_attention_mask([self.batch_size, self.seq_length] ) _UpperCAmelCase : Any = None if self.use_token_type_ids: _UpperCAmelCase : List[str] = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) _UpperCAmelCase : Optional[int] = None _UpperCAmelCase : Any = None _UpperCAmelCase : Optional[int] = None if self.use_labels: _UpperCAmelCase : Optional[Any] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _UpperCAmelCase : Tuple = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) _UpperCAmelCase : List[str] = ids_tensor([self.batch_size] , self.num_choices ) _UpperCAmelCase : List[Any] = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def _A ( self : Dict ): return BioGptConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=A , initializer_range=self.initializer_range , ) def _A ( self : int , A : List[Any] , A : Any , A : int , A : Union[str, Any] , A : Dict , A : List[Any] , A : Dict ): _UpperCAmelCase : List[str] = BioGptModel(config=A ) model.to(A ) model.eval() _UpperCAmelCase : Tuple = model(A , attention_mask=A ) _UpperCAmelCase : int = model(A ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _A ( self : List[Any] , A : str , A : List[Any] , A : Dict , A : List[Any] , A : List[str] , A : Union[str, Any] , A : int , A : List[str] , A : Dict , ): _UpperCAmelCase : Optional[int] = BioGptForCausalLM(config=A ) model.to(A ) model.eval() _UpperCAmelCase : Optional[int] = model(A , attention_mask=A , token_type_ids=A , labels=A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def _A ( self : List[Any] , A : str , A : str , A : str , A : Any , A : List[str] , *A : Optional[int] ): _UpperCAmelCase : str = BioGptModel(config=A ) model.to(A ) model.eval() # create attention mask _UpperCAmelCase : List[Any] = torch.ones(input_ids.shape , dtype=torch.long , device=A ) _UpperCAmelCase : Optional[int] = self.seq_length // 2 _UpperCAmelCase : List[Any] = 0 # first forward pass _UpperCAmelCase , _UpperCAmelCase : List[str] = model(A , attention_mask=A ).to_tuple() # create hypothetical next token and extent to next_input_ids _UpperCAmelCase : List[str] = ids_tensor((self.batch_size, 1) , config.vocab_size ) # change a random masked slice from input_ids _UpperCAmelCase : List[str] = ids_tensor((1,) , A ).item() + 1 _UpperCAmelCase : str = ids_tensor((self.batch_size, 1) , config.vocab_size ).squeeze(-1 ) _UpperCAmelCase : Any = random_other_next_tokens # append to next input_ids and attn_mask _UpperCAmelCase : List[Any] = torch.cat([input_ids, next_tokens] , dim=-1 ) _UpperCAmelCase : Optional[int] = torch.cat( [attn_mask, torch.ones((attn_mask.shape[0], 1) , dtype=torch.long , device=A )] , dim=1 , ) # get two different outputs _UpperCAmelCase : List[Any] = model(A , attention_mask=A )["last_hidden_state"] _UpperCAmelCase : Optional[Any] = model(A , past_key_values=A , attention_mask=A )["last_hidden_state"] # select random slice _UpperCAmelCase : Any = ids_tensor((1,) , output_from_past.shape[-1] ).item() _UpperCAmelCase : Optional[Any] = output_from_no_past[:, -1, random_slice_idx].detach() _UpperCAmelCase : Any = output_from_past[:, 0, random_slice_idx].detach() # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(A , A , atol=1E-3 ) ) def _A ( self : int , A : Dict , A : str , A : Dict , A : Union[str, Any] , A : Any , *A : Union[str, Any] ): _UpperCAmelCase : Optional[Any] = BioGptModel(config=A ).to(A ).eval() _UpperCAmelCase : List[Any] = torch.ones(input_ids.shape , dtype=torch.long , device=A ) # first forward pass _UpperCAmelCase : Union[str, Any] = model(A , attention_mask=A , use_cache=A ) _UpperCAmelCase , _UpperCAmelCase : Dict = outputs.to_tuple() # create hypothetical multiple next token and extent to next_input_ids _UpperCAmelCase : str = ids_tensor((self.batch_size, 3) , config.vocab_size ) _UpperCAmelCase : Any = ids_tensor((self.batch_size, 3) , 2 ) # append to next input_ids and _UpperCAmelCase : Optional[Any] = torch.cat([input_ids, next_tokens] , dim=-1 ) _UpperCAmelCase : Dict = torch.cat([attention_mask, next_attn_mask] , dim=-1 ) _UpperCAmelCase : Any = model(A , attention_mask=A )["last_hidden_state"] _UpperCAmelCase : Dict = model(A , attention_mask=A , past_key_values=A )[ "last_hidden_state" ] # select random slice _UpperCAmelCase : Dict = ids_tensor((1,) , output_from_past.shape[-1] ).item() _UpperCAmelCase : Dict = output_from_no_past[:, -3:, random_slice_idx].detach() _UpperCAmelCase : 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(A , A , atol=1E-3 ) ) def _A ( self : Optional[Any] , A : Tuple , A : List[str] , A : Tuple , A : Dict , A : List[Any] , *A : Tuple , A : List[str]=False ): _UpperCAmelCase : Optional[int] = BioGptForCausalLM(A ) model.to(A ) if gradient_checkpointing: model.gradient_checkpointing_enable() _UpperCAmelCase : Union[str, Any] = model(A , labels=A ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) result.loss.backward() def _A ( self : Optional[Any] , A : Any , *A : Optional[Any] ): _UpperCAmelCase : Tuple = BioGptModel(A ) _UpperCAmelCase : int = model.config.initializer_range / math.sqrt(2 * model.config.num_hidden_layers ) for key in model.state_dict().keys(): if "c_proj" in key and "weight" in key: self.parent.assertLessEqual(abs(torch.std(model.state_dict()[key] ) - model_std ) , 0.001 ) self.parent.assertLessEqual(abs(torch.mean(model.state_dict()[key] ) - 0.0 ) , 0.01 ) def _A ( self : Optional[int] , A : Dict , A : Tuple , A : Optional[int] , A : int , A : List[str] , *A : Dict ): _UpperCAmelCase : Any = self.num_labels _UpperCAmelCase : Any = BioGptForTokenClassification(A ) model.to(A ) model.eval() _UpperCAmelCase : Optional[int] = model(A , attention_mask=A , token_type_ids=A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def _A ( self : int ): _UpperCAmelCase : Dict = self.prepare_config_and_inputs() ( ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ) : List[str] = config_and_inputs _UpperCAmelCase : Optional[int] = {"input_ids": input_ids, "attention_mask": input_mask} return config, inputs_dict @require_torch class lowerCamelCase_ (snake_case__ , snake_case__ , snake_case__ , unittest.TestCase ): '''simple docstring''' __UpperCamelCase: List[str] = ( (BioGptModel, BioGptForCausalLM, BioGptForSequenceClassification, BioGptForTokenClassification) if is_torch_available() else () ) __UpperCamelCase: List[str] = (BioGptForCausalLM,) if is_torch_available() else () __UpperCamelCase: str = ( { "feature-extraction": BioGptModel, "text-classification": BioGptForSequenceClassification, "text-generation": BioGptForCausalLM, "token-classification": BioGptForTokenClassification, "zero-shot": BioGptForSequenceClassification, } if is_torch_available() else {} ) __UpperCamelCase: Union[str, Any] = False def _A ( self : Optional[Any] ): _UpperCAmelCase : List[Any] = BioGptModelTester(self ) _UpperCAmelCase : str = ConfigTester(self , config_class=A , hidden_size=37 ) def _A ( self : Union[str, Any] ): self.config_tester.run_common_tests() def _A ( self : Any ): _UpperCAmelCase : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*A ) def _A ( self : Any ): _UpperCAmelCase : Optional[int] = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: _UpperCAmelCase : Tuple = type self.model_tester.create_and_check_model(*A ) def _A ( self : int ): _UpperCAmelCase : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_biogpt_model_attention_mask_past(*A ) def _A ( self : Union[str, Any] ): _UpperCAmelCase : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_forward_and_backwards(*A , gradient_checkpointing=A ) def _A ( self : Union[str, Any] ): _UpperCAmelCase : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_biogpt_model_past_large_inputs(*A ) def _A ( self : Dict ): _UpperCAmelCase : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_biogpt_weight_initialization(*A ) def _A ( self : Dict ): _UpperCAmelCase : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_biogpt_for_token_classification(*A ) @slow def _A ( self : List[str] ): _UpperCAmelCase : Optional[Any] = BioGptForCausalLM.from_pretrained("microsoft/biogpt" ) model.to(A ) _UpperCAmelCase : Tuple = BioGptTokenizer.from_pretrained("microsoft/biogpt" ) _UpperCAmelCase : str = "left" # Define PAD Token = EOS Token = 50256 _UpperCAmelCase : Any = tokenizer.eos_token _UpperCAmelCase : int = model.config.eos_token_id # use different length sentences to test batching _UpperCAmelCase : Any = [ "Hello, my dog is a little", "Today, I", ] _UpperCAmelCase : Tuple = tokenizer(A , return_tensors="pt" , padding=A ) _UpperCAmelCase : Optional[Any] = inputs["input_ids"].to(A ) _UpperCAmelCase : Any = model.generate( input_ids=A , attention_mask=inputs["attention_mask"].to(A ) , ) _UpperCAmelCase : int = tokenizer(sentences[0] , return_tensors="pt" ).input_ids.to(A ) _UpperCAmelCase : List[Any] = model.generate(input_ids=A ) _UpperCAmelCase : List[Any] = inputs_non_padded.shape[-1] - inputs["attention_mask"][-1].long().sum().cpu().item() _UpperCAmelCase : int = tokenizer(sentences[1] , return_tensors="pt" ).input_ids.to(A ) _UpperCAmelCase : int = model.generate(input_ids=A , max_length=model.config.max_length - num_paddings ) _UpperCAmelCase : Dict = tokenizer.batch_decode(A , skip_special_tokens=A ) _UpperCAmelCase : Any = tokenizer.decode(output_non_padded[0] , skip_special_tokens=A ) _UpperCAmelCase : Optional[int] = tokenizer.decode(output_padded[0] , skip_special_tokens=A ) _UpperCAmelCase : str = [ "Hello, my dog is a little bit bigger than a little bit.", "Today, I have a good idea of how to use the information", ] self.assertListEqual(A , A ) self.assertListEqual(A , [non_padded_sentence, padded_sentence] ) @slow def _A ( self : str ): for model_name in BIOGPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _UpperCAmelCase : Optional[Any] = BioGptModel.from_pretrained(A ) self.assertIsNotNone(A ) def _A ( self : List[str] ): _UpperCAmelCase , _UpperCAmelCase : int = self.model_tester.prepare_config_and_inputs_for_common() _UpperCAmelCase : str = 3 _UpperCAmelCase : List[str] = input_dict["input_ids"] _UpperCAmelCase : Dict = input_ids.ne(1 ).to(A ) _UpperCAmelCase : List[Any] = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) _UpperCAmelCase : List[str] = BioGptForSequenceClassification(A ) model.to(A ) model.eval() _UpperCAmelCase : List[str] = model(A , attention_mask=A , labels=A ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def _A ( self : int ): _UpperCAmelCase , _UpperCAmelCase : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() _UpperCAmelCase : int = 3 _UpperCAmelCase : Dict = "multi_label_classification" _UpperCAmelCase : Optional[Any] = input_dict["input_ids"] _UpperCAmelCase : Optional[int] = input_ids.ne(1 ).to(A ) _UpperCAmelCase : Tuple = ids_tensor( [self.model_tester.batch_size, config.num_labels] , self.model_tester.type_sequence_label_size ).to(torch.float ) _UpperCAmelCase : Optional[Any] = BioGptForSequenceClassification(A ) model.to(A ) model.eval() _UpperCAmelCase : Tuple = model(A , attention_mask=A , labels=A ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) @require_torch class lowerCamelCase_ (unittest.TestCase ): '''simple docstring''' @slow def _A ( self : List[Any] ): _UpperCAmelCase : Optional[Any] = BioGptForCausalLM.from_pretrained("microsoft/biogpt" ) _UpperCAmelCase : List[str] = torch.tensor([[2, 4805, 9, 656, 21]] ) _UpperCAmelCase : List[Any] = model(A )[0] _UpperCAmelCase : int = 42384 _UpperCAmelCase : int = torch.Size((1, 5, vocab_size) ) self.assertEqual(output.shape , A ) _UpperCAmelCase : Any = torch.tensor( [[[-9.5_236, -9.8_918, 10.4_557], [-11.0_469, -9.6_423, 8.1_022], [-8.8_664, -7.8_826, 5.5_325]]] ) self.assertTrue(torch.allclose(output[:, :3, :3] , A , atol=1E-4 ) ) @slow def _A ( self : Any ): _UpperCAmelCase : str = BioGptTokenizer.from_pretrained("microsoft/biogpt" ) _UpperCAmelCase : Tuple = BioGptForCausalLM.from_pretrained("microsoft/biogpt" ) model.to(A ) torch.manual_seed(0 ) _UpperCAmelCase : Optional[Any] = tokenizer("COVID-19 is" , return_tensors="pt" ).to(A ) _UpperCAmelCase : Dict = model.generate( **A , min_length=100 , max_length=1024 , num_beams=5 , early_stopping=A , ) _UpperCAmelCase : Optional[Any] = tokenizer.decode(output_ids[0] , skip_special_tokens=A ) _UpperCAmelCase : List[str] = ( "COVID-19 is a global pandemic caused by severe acute respiratory syndrome coronavirus 2 (SARS-CoV-2), the" " causative agent of coronavirus disease 2019 (COVID-19), which has spread to more than 200 countries and" " territories, including the United States (US), Canada, Australia, New Zealand, the United Kingdom (UK)," " and the United States of America (USA), as of March 11, 2020, with more than 800,000 confirmed cases and" " more than 800,000 deaths." ) self.assertEqual(A , A )
31
1
'''simple docstring''' class lowerCamelCase_ : '''simple docstring''' def __init__( self : Tuple , A : Any , A : str , A : Union[str, Any] ): _UpperCAmelCase : Optional[int] = None _UpperCAmelCase : Optional[int] = None _UpperCAmelCase : Any = graph self._normalize_graph(A , A ) _UpperCAmelCase : List[str] = len(A ) _UpperCAmelCase : Tuple = None def _A ( self : Any , A : List[Any] , A : str ): if sources is int: _UpperCAmelCase : List[Any] = [sources] if sinks is int: _UpperCAmelCase : List[Any] = [sinks] if len(A ) == 0 or len(A ) == 0: return _UpperCAmelCase : str = sources[0] _UpperCAmelCase : Union[str, Any] = sinks[0] # make fake vertex if there are more # than one source or sink if len(A ) > 1 or len(A ) > 1: _UpperCAmelCase : Dict = 0 for i in sources: max_input_flow += sum(self.graph[i] ) _UpperCAmelCase : str = len(self.graph ) + 1 for room in self.graph: room.insert(0 , 0 ) self.graph.insert(0 , [0] * size ) for i in sources: _UpperCAmelCase : Optional[Any] = max_input_flow _UpperCAmelCase : List[str] = 0 _UpperCAmelCase : str = len(self.graph ) + 1 for room in self.graph: room.append(0 ) self.graph.append([0] * size ) for i in sinks: _UpperCAmelCase : Dict = max_input_flow _UpperCAmelCase : List[Any] = size - 1 def _A ( self : Union[str, Any] ): 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 _A ( self : Tuple , A : Dict ): _UpperCAmelCase : str = algorithm(self ) class lowerCamelCase_ : '''simple docstring''' def __init__( self : Any , A : str ): _UpperCAmelCase : Optional[int] = flow_network _UpperCAmelCase : Any = flow_network.verticesCount _UpperCAmelCase : List[str] = flow_network.sourceIndex _UpperCAmelCase : Union[str, Any] = flow_network.sinkIndex # it's just a reference, so you shouldn't change # it in your algorithms, use deep copy before doing that _UpperCAmelCase : Any = flow_network.graph _UpperCAmelCase : Union[str, Any] = False def _A ( self : List[str] ): if not self.executed: self._algorithm() _UpperCAmelCase : int = True def _A ( self : List[Any] ): pass class lowerCamelCase_ (snake_case__ ): '''simple docstring''' def __init__( self : Optional[int] , A : Union[str, Any] ): super().__init__(A ) # use this to save your result _UpperCAmelCase : Any = -1 def _A ( self : Union[str, Any] ): if not self.executed: raise Exception("You should execute algorithm before using its result!" ) return self.maximum_flow class lowerCamelCase_ (snake_case__ ): '''simple docstring''' def __init__( self : Tuple , A : int ): super().__init__(A ) _UpperCAmelCase : List[str] = [[0] * self.verticies_count for i in range(self.verticies_count )] _UpperCAmelCase : Union[str, Any] = [0] * self.verticies_count _UpperCAmelCase : int = [0] * self.verticies_count def _A ( self : Dict ): _UpperCAmelCase : Dict = 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 _UpperCAmelCase : Optional[int] = [ i for i in range(self.verticies_count ) if i != self.source_index and i != self.sink_index ] # move through list _UpperCAmelCase : Any = 0 while i < len(A ): _UpperCAmelCase : int = vertices_list[i] _UpperCAmelCase : int = self.heights[vertex_index] self.process_vertex(A ) 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(A ) ) _UpperCAmelCase : Union[str, Any] = 0 else: i += 1 _UpperCAmelCase : List[Any] = sum(self.preflow[self.source_index] ) def _A ( self : Union[str, Any] , A : str ): 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(A , A ) self.relabel(A ) def _A ( self : int , A : Dict , A : List[str] ): _UpperCAmelCase : 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 _A ( self : Optional[int] , A : Union[str, Any] ): _UpperCAmelCase : str = 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): _UpperCAmelCase : Tuple = self.heights[to_index] if min_height is not None: _UpperCAmelCase : Optional[Any] = min_height + 1 if __name__ == "__main__": __SCREAMING_SNAKE_CASE : Optional[int] = [0] __SCREAMING_SNAKE_CASE : Union[str, Any] = [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], # ] __SCREAMING_SNAKE_CASE : List[Any] = [[0, 7, 0, 0], [0, 0, 6, 0], [0, 0, 0, 8], [9, 0, 0, 0]] # prepare our network __SCREAMING_SNAKE_CASE : Union[str, Any] = FlowNetwork(graph, entrances, exits) # set algorithm flow_network.set_maximum_flow_algorithm(PushRelabelExecutor) # and calculate __SCREAMING_SNAKE_CASE : Optional[Any] = flow_network.find_maximum_flow() print(F'maximum flow is {maximum_flow}')
31
'''simple docstring''' __SCREAMING_SNAKE_CASE : Dict = 8.3_1_4_4_6_2 # Unit - J mol-1 K-1 def UpperCamelCase_ ( _UpperCAmelCase : float , _UpperCAmelCase : float , _UpperCAmelCase : float ) -> float: """simple docstring""" if moles < 0 or kelvin < 0 or volume < 0: raise ValueError("Invalid inputs. Enter positive value." ) return moles * kelvin * UNIVERSAL_GAS_CONSTANT / volume def UpperCamelCase_ ( _UpperCAmelCase : float , _UpperCAmelCase : float , _UpperCAmelCase : float ) -> float: """simple docstring""" if moles < 0 or kelvin < 0 or pressure < 0: raise ValueError("Invalid inputs. Enter positive value." ) return moles * kelvin * UNIVERSAL_GAS_CONSTANT / pressure if __name__ == "__main__": from doctest import testmod testmod()
31
1
'''simple docstring''' def UpperCamelCase_ ( _UpperCAmelCase : str , _UpperCAmelCase : str ) -> float: """simple docstring""" def get_matched_characters(_UpperCAmelCase : str , _UpperCAmelCase : str ) -> str: _UpperCAmelCase : Tuple = [] _UpperCAmelCase : Dict = min(len(_stra ) , len(_stra ) ) // 2 for i, l in enumerate(_stra ): _UpperCAmelCase : int = int(max(0 , i - limit ) ) _UpperCAmelCase : Any = int(min(i + limit + 1 , len(_stra ) ) ) if l in _stra[left:right]: matched.append(_UpperCAmelCase ) _UpperCAmelCase : List[Any] = F"""{_stra[0:_stra.index(_UpperCAmelCase )]} {_stra[_stra.index(_UpperCAmelCase ) + 1:]}""" return "".join(_UpperCAmelCase ) # matching characters _UpperCAmelCase : Union[str, Any] = get_matched_characters(_UpperCAmelCase , _UpperCAmelCase ) _UpperCAmelCase : Tuple = get_matched_characters(_UpperCAmelCase , _UpperCAmelCase ) _UpperCAmelCase : Tuple = len(_UpperCAmelCase ) # transposition _UpperCAmelCase : Optional[Any] = ( len([(ca, ca) for ca, ca in zip(_UpperCAmelCase , _UpperCAmelCase ) if ca != ca] ) // 2 ) if not match_count: _UpperCAmelCase : Dict = 0.0 else: _UpperCAmelCase : Optional[int] = ( 1 / 3 * ( match_count / len(_UpperCAmelCase ) + match_count / len(_UpperCAmelCase ) + (match_count - transpositions) / match_count ) ) # common prefix up to 4 characters _UpperCAmelCase : str = 0 for ca, ca in zip(stra[:4] , stra[:4] ): if ca == ca: prefix_len += 1 else: break return jaro + 0.1 * prefix_len * (1 - jaro) if __name__ == "__main__": import doctest doctest.testmod() print(jaro_winkler("""hello""", """world"""))
31
'''simple docstring''' import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_pegasus import PegasusTokenizer else: __SCREAMING_SNAKE_CASE : Optional[Any] = None __SCREAMING_SNAKE_CASE : Dict = logging.get_logger(__name__) __SCREAMING_SNAKE_CASE : Union[str, Any] = """▁""" __SCREAMING_SNAKE_CASE : str = {"""vocab_file""": """spiece.model""", """tokenizer_file""": """tokenizer.json"""} __SCREAMING_SNAKE_CASE : int = { """vocab_file""": {"""google/pegasus-xsum""": """https://huggingface.co/google/pegasus-xsum/resolve/main/spiece.model"""}, """tokenizer_file""": { """google/pegasus-xsum""": """https://huggingface.co/google/pegasus-xsum/resolve/main/tokenizer.json""" }, } __SCREAMING_SNAKE_CASE : str = { """google/pegasus-xsum""": 512, } class lowerCamelCase_ (snake_case__ ): '''simple docstring''' __UpperCamelCase: Optional[int] = VOCAB_FILES_NAMES __UpperCamelCase: Dict = PRETRAINED_VOCAB_FILES_MAP __UpperCamelCase: List[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCamelCase: Optional[int] = PegasusTokenizer __UpperCamelCase: Optional[Any] = ["input_ids", "attention_mask"] def __init__( self : Dict , A : List[str]=None , A : Union[str, Any]=None , A : Optional[int]="<pad>" , A : Tuple="</s>" , A : Union[str, Any]="<unk>" , A : Union[str, Any]="<mask_2>" , A : Dict="<mask_1>" , A : Union[str, Any]=None , A : int=103 , **A : Optional[Any] , ): _UpperCAmelCase : Dict = offset if additional_special_tokens is not None: if not isinstance(A , A ): raise TypeError( F"""additional_special_tokens should be of type {type(A )}, but is""" F""" {type(A )}""" ) _UpperCAmelCase : Optional[int] = ( ([mask_token_sent] + additional_special_tokens) if mask_token_sent not in additional_special_tokens and mask_token_sent is not None else additional_special_tokens ) # fill additional tokens with ..., <unk_token_102> in case not all additional tokens are already taken additional_special_tokens_extended += [ F"""<unk_{i}>""" for i in range(len(A ) , self.offset - 1 ) ] if len(set(A ) ) != len(A ): raise ValueError( "Please make sure that the provided additional_special_tokens do not contain an incorrectly" F""" shifted list of <unk_x> tokens. Found {additional_special_tokens_extended}.""" ) _UpperCAmelCase : Any = additional_special_tokens_extended else: _UpperCAmelCase : Dict = [mask_token_sent] if mask_token_sent is not None else [] additional_special_tokens += [F"""<unk_{i}>""" for i in range(2 , self.offset )] super().__init__( A , tokenizer_file=A , pad_token=A , eos_token=A , unk_token=A , mask_token=A , mask_token_sent=A , offset=A , additional_special_tokens=A , **A , ) _UpperCAmelCase : Optional[Any] = vocab_file _UpperCAmelCase : Optional[Any] = False if not self.vocab_file else True def _A ( self : List[str] , A : Optional[Any] ): _UpperCAmelCase : Any = set(self.all_special_ids ) # call it once instead of inside list comp all_special_ids.remove(self.unk_token_id ) # <unk> is only sometimes special if all_special_ids != set(range(len(self.additional_special_tokens ) + 3 ) ): raise ValueError( "There should be 3 special tokens: mask_token, pad_token, and eos_token +" F""" {len(self.additional_special_tokens )} additional_special_tokens, but got {all_special_ids}""" ) return [1 if x in all_special_ids else 0 for x in seq] def _A ( self : str , A : List , A : Optional[List] = None , A : bool = False ): if already_has_special_tokens: return self._special_token_mask(A ) elif token_ids_a is None: return self._special_token_mask(A ) + [1] else: return self._special_token_mask(token_ids_a + token_ids_a ) + [1] def _A ( self : Optional[int] , A : Union[str, Any] , A : int=None ): if token_ids_a is None: return token_ids_a + [self.eos_token_id] # We don't expect to process pairs, but leave the pair logic for API consistency return token_ids_a + token_ids_a + [self.eos_token_id] def _A ( self : Union[str, Any] , A : str , A : Optional[str] = None ): if not self.can_save_slow_tokenizer: raise ValueError( "Your fast tokenizer does not have the necessary information to save the vocabulary for a slow " "tokenizer." ) if not os.path.isdir(A ): logger.error(F"""Vocabulary path ({save_directory}) should be a directory""" ) return _UpperCAmelCase : List[Any] = os.path.join( A , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(A ): copyfile(self.vocab_file , A ) return (out_vocab_file,)
31
1
'''simple docstring''' from __future__ import annotations from decimal import Decimal from math import * # noqa: F403 from sympy import diff def UpperCamelCase_ ( _UpperCAmelCase : str , _UpperCAmelCase : float | Decimal , _UpperCAmelCase : float = 10**-10 ) -> float: """simple docstring""" _UpperCAmelCase : Optional[int] = a while True: _UpperCAmelCase : Optional[int] = Decimal(_UpperCAmelCase ) - ( Decimal(eval(_UpperCAmelCase ) ) / Decimal(eval(str(diff(_UpperCAmelCase ) ) ) ) # noqa: S307 ) # This number dictates the accuracy of the answer if abs(eval(_UpperCAmelCase ) ) < precision: # noqa: S307 return float(_UpperCAmelCase ) # Let's Execute if __name__ == "__main__": # Find root of trigonometric function # Find value of pi print(F'The root of sin(x) = 0 is {newton_raphson("sin(x)", 2)}') # Find root of polynomial print(F'The root of x**2 - 5*x + 2 = 0 is {newton_raphson("x**2 - 5*x + 2", 0.4)}') # Find Square Root of 5 print(F'The root of log(x) - 1 = 0 is {newton_raphson("log(x) - 1", 2)}') # Exponential Roots print(F'The root of exp(x) - 1 = 0 is {newton_raphson("exp(x) - 1", 0)}')
31
'''simple docstring''' import shutil import tempfile import unittest from transformers import ( SPIECE_UNDERLINE, AddedToken, BatchEncoding, NllbTokenizer, NllbTokenizerFast, is_torch_available, ) from transformers.testing_utils import ( get_tests_dir, nested_simplify, require_sentencepiece, require_tokenizers, require_torch, ) from ...test_tokenization_common import TokenizerTesterMixin __SCREAMING_SNAKE_CASE : Union[str, Any] = get_tests_dir("""fixtures/test_sentencepiece.model""") if is_torch_available(): from transformers.models.mam_aaa.modeling_mam_aaa import shift_tokens_right __SCREAMING_SNAKE_CASE : Optional[int] = 256_047 __SCREAMING_SNAKE_CASE : Optional[int] = 256_145 @require_sentencepiece @require_tokenizers class lowerCamelCase_ (snake_case__ , unittest.TestCase ): '''simple docstring''' __UpperCamelCase: int = NllbTokenizer __UpperCamelCase: Tuple = NllbTokenizerFast __UpperCamelCase: Union[str, Any] = True __UpperCamelCase: Dict = True __UpperCamelCase: Optional[Any] = {} def _A ( self : Union[str, Any] ): super().setUp() # We have a SentencePiece fixture for testing _UpperCAmelCase : Tuple = NllbTokenizer(A , keep_accents=A ) tokenizer.save_pretrained(self.tmpdirname ) def _A ( self : Dict ): _UpperCAmelCase : Tuple = NllbTokenizer(A , keep_accents=A ) _UpperCAmelCase : Optional[Any] = tokenizer.tokenize("This is a test" ) self.assertListEqual(A , ["▁This", "▁is", "▁a", "▁t", "est"] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(A ) , [value + tokenizer.fairseq_offset for value in [285, 46, 10, 170, 382]] , ) _UpperCAmelCase : List[str] = tokenizer.tokenize("I was born in 92000, and this is falsé." ) self.assertListEqual( A , [ SPIECE_UNDERLINE + "I", SPIECE_UNDERLINE + "was", SPIECE_UNDERLINE + "b", "or", "n", SPIECE_UNDERLINE + "in", SPIECE_UNDERLINE + "", "9", "2", "0", "0", "0", ",", SPIECE_UNDERLINE + "and", SPIECE_UNDERLINE + "this", SPIECE_UNDERLINE + "is", SPIECE_UNDERLINE + "f", "al", "s", "é", ".", ] , ) _UpperCAmelCase : Optional[Any] = tokenizer.convert_tokens_to_ids(A ) self.assertListEqual( A , [ value + tokenizer.fairseq_offset for value in [8, 21, 84, 55, 24, 19, 7, 2, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 2, 4] ] , ) _UpperCAmelCase : Union[str, Any] = tokenizer.convert_ids_to_tokens(A ) self.assertListEqual( A , [ SPIECE_UNDERLINE + "I", SPIECE_UNDERLINE + "was", SPIECE_UNDERLINE + "b", "or", "n", SPIECE_UNDERLINE + "in", SPIECE_UNDERLINE + "", "<unk>", "2", "0", "0", "0", ",", SPIECE_UNDERLINE + "and", SPIECE_UNDERLINE + "this", SPIECE_UNDERLINE + "is", SPIECE_UNDERLINE + "f", "al", "s", "<unk>", ".", ] , ) def _A ( self : List[Any] ): _UpperCAmelCase : Any = (self.rust_tokenizer_class, "hf-internal-testing/tiny-random-nllb", {}) for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F"""{tokenizer.__class__.__name__} ({pretrained_name})""" ): _UpperCAmelCase : Dict = self.rust_tokenizer_class.from_pretrained(A , **A ) _UpperCAmelCase : str = self.tokenizer_class.from_pretrained(A , **A ) _UpperCAmelCase : Optional[int] = tempfile.mkdtemp() _UpperCAmelCase : Dict = tokenizer_r.save_pretrained(A ) _UpperCAmelCase : Dict = tokenizer_p.save_pretrained(A ) # Checks it save with the same files + the tokenizer.json file for the fast one self.assertTrue(any("tokenizer.json" in f for f in tokenizer_r_files ) ) _UpperCAmelCase : Optional[int] = tuple(f for f in tokenizer_r_files if "tokenizer.json" not in f ) self.assertSequenceEqual(A , A ) # Checks everything loads correctly in the same way _UpperCAmelCase : List[Any] = tokenizer_r.from_pretrained(A ) _UpperCAmelCase : List[str] = tokenizer_p.from_pretrained(A ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(A , A ) ) shutil.rmtree(A ) # Save tokenizer rust, legacy_format=True _UpperCAmelCase : Optional[Any] = tempfile.mkdtemp() _UpperCAmelCase : str = tokenizer_r.save_pretrained(A , legacy_format=A ) _UpperCAmelCase : str = tokenizer_p.save_pretrained(A ) # Checks it save with the same files self.assertSequenceEqual(A , A ) # Checks everything loads correctly in the same way _UpperCAmelCase : Optional[int] = tokenizer_r.from_pretrained(A ) _UpperCAmelCase : Dict = tokenizer_p.from_pretrained(A ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(A , A ) ) shutil.rmtree(A ) # Save tokenizer rust, legacy_format=False _UpperCAmelCase : Optional[int] = tempfile.mkdtemp() _UpperCAmelCase : Optional[int] = tokenizer_r.save_pretrained(A , legacy_format=A ) _UpperCAmelCase : Dict = tokenizer_p.save_pretrained(A ) # Checks it saved the tokenizer.json file self.assertTrue(any("tokenizer.json" in f for f in tokenizer_r_files ) ) # Checks everything loads correctly in the same way _UpperCAmelCase : List[Any] = tokenizer_r.from_pretrained(A ) _UpperCAmelCase : Optional[int] = tokenizer_p.from_pretrained(A ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(A , A ) ) shutil.rmtree(A ) @require_torch def _A ( self : Tuple ): if not self.test_seqaseq: return _UpperCAmelCase : Union[str, Any] = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(F"""{tokenizer.__class__.__name__}""" ): # Longer text that will definitely require truncation. _UpperCAmelCase : Optional[Any] = [ " UN Chief Says There Is No Military Solution in Syria", " Secretary-General Ban Ki-moon says his response to Russia's stepped up military support for" " Syria is that 'there is no military solution' to the nearly five-year conflict and more weapons" " will only worsen the violence and misery for millions of people.", ] _UpperCAmelCase : Optional[Any] = [ "Şeful ONU declară că nu există o soluţie militară în Siria", "Secretarul General Ban Ki-moon declară că răspunsul său la intensificarea sprijinului militar al" " Rusiei pentru Siria este că \"nu există o soluţie militară\" la conflictul de aproape cinci ani şi" " că noi arme nu vor face decât să înrăutăţească violenţele şi mizeria pentru milioane de oameni.", ] try: _UpperCAmelCase : Optional[int] = tokenizer.prepare_seqaseq_batch( src_texts=A , tgt_texts=A , max_length=3 , max_target_length=10 , return_tensors="pt" , src_lang="eng_Latn" , tgt_lang="ron_Latn" , ) except NotImplementedError: return self.assertEqual(batch.input_ids.shape[1] , 3 ) self.assertEqual(batch.labels.shape[1] , 10 ) # max_target_length will default to max_length if not specified _UpperCAmelCase : Tuple = tokenizer.prepare_seqaseq_batch( A , tgt_texts=A , max_length=3 , return_tensors="pt" ) self.assertEqual(batch.input_ids.shape[1] , 3 ) self.assertEqual(batch.labels.shape[1] , 3 ) _UpperCAmelCase : Union[str, Any] = tokenizer.prepare_seqaseq_batch( src_texts=A , max_length=3 , max_target_length=10 , return_tensors="pt" ) self.assertEqual(batch_encoder_only.input_ids.shape[1] , 3 ) self.assertEqual(batch_encoder_only.attention_mask.shape[1] , 3 ) self.assertNotIn("decoder_input_ids" , A ) @unittest.skip("Unfortunately way too slow to build a BPE with SentencePiece." ) def _A ( self : List[Any] ): pass def _A ( self : Union[str, Any] ): for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F"""{tokenizer.__class__.__name__} ({pretrained_name})""" ): _UpperCAmelCase : Any = [AddedToken("<special>" , lstrip=A )] _UpperCAmelCase : List[Any] = self.rust_tokenizer_class.from_pretrained( A , additional_special_tokens=A , **A ) _UpperCAmelCase : Dict = tokenizer_r.encode("Hey this is a <special> token" ) _UpperCAmelCase : Any = tokenizer_r.encode("<special>" , add_special_tokens=A )[0] self.assertTrue(special_token_id in r_output ) if self.test_slow_tokenizer: _UpperCAmelCase : Dict = self.rust_tokenizer_class.from_pretrained( A , additional_special_tokens=A , **A , ) _UpperCAmelCase : Optional[int] = self.tokenizer_class.from_pretrained( A , additional_special_tokens=A , **A ) _UpperCAmelCase : Union[str, Any] = tokenizer_p.encode("Hey this is a <special> token" ) _UpperCAmelCase : Any = tokenizer_cr.encode("Hey this is a <special> token" ) self.assertEqual(A , A ) self.assertEqual(A , A ) self.assertTrue(special_token_id in p_output ) self.assertTrue(special_token_id in cr_output ) @require_torch @require_sentencepiece @require_tokenizers class lowerCamelCase_ (unittest.TestCase ): '''simple docstring''' __UpperCamelCase: Dict = "facebook/nllb-200-distilled-600M" __UpperCamelCase: Optional[int] = [ " UN Chief Says There Is No Military Solution in Syria", " Secretary-General Ban Ki-moon says his response to Russia's stepped up military support for Syria is that \"there is no military solution\" to the nearly five-year conflict and more weapons will only worsen the violence and misery for millions of people.", ] __UpperCamelCase: str = [ "Şeful ONU declară că nu există o soluţie militară în Siria", "Secretarul General Ban Ki-moon declară că răspunsul său la intensificarea sprijinului militar al Rusiei" " pentru Siria este că \"nu există o soluţie militară\" la conflictul de aproape cinci ani şi că noi arme nu vor" " face decât să înrăutăţească violenţele şi mizeria pentru milioane de oameni.", ] __UpperCamelCase: str = [ 2_5_6_0_4_7, 1_6_2_9_7, 1_3_4_4_0_8, 8_1_6_5, 2_4_8_0_6_6, 1_4_7_3_4, 9_5_0, 1_1_3_5, 1_0_5_7_2_1, 3_5_7_3, 8_3, 2_7_3_5_2, 1_0_8, 4_9_4_8_6, 2, ] @classmethod def _A ( cls : int ): _UpperCAmelCase : NllbTokenizer = NllbTokenizer.from_pretrained( cls.checkpoint_name , src_lang="eng_Latn" , tgt_lang="ron_Latn" ) _UpperCAmelCase : Union[str, Any] = 1 return cls def _A ( self : Any ): self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["ace_Arab"] , 256001 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["ace_Latn"] , 256002 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["fra_Latn"] , 256057 ) def _A ( self : Union[str, Any] ): _UpperCAmelCase : Optional[int] = self.tokenizer.batch_encode_plus(self.src_text ).input_ids[0] self.assertListEqual(self.expected_src_tokens , A ) def _A ( self : Tuple ): self.assertIn(A , self.tokenizer.all_special_ids ) # fmt: off _UpperCAmelCase : List[Any] = [RO_CODE, 4254, 98068, 112923, 39072, 3909, 713, 102767, 26, 17314, 35642, 14683, 33118, 2022, 66987, 2, 256047] # fmt: on _UpperCAmelCase : Tuple = self.tokenizer.decode(A , skip_special_tokens=A ) _UpperCAmelCase : Optional[Any] = self.tokenizer.decode(generated_ids[1:] , skip_special_tokens=A ) self.assertEqual(A , A ) self.assertNotIn(self.tokenizer.eos_token , A ) def _A ( self : Optional[int] ): _UpperCAmelCase : List[Any] = ["this is gunna be a long sentence " * 20] assert isinstance(src_text[0] , A ) _UpperCAmelCase : Dict = 10 _UpperCAmelCase : Tuple = self.tokenizer(A , max_length=A , truncation=A ).input_ids[0] self.assertEqual(ids[-1] , 2 ) self.assertEqual(ids[0] , A ) self.assertEqual(len(A ) , A ) def _A ( self : Dict ): self.assertListEqual(self.tokenizer.convert_tokens_to_ids(["<mask>", "ar_AR"] ) , [256203, 3] ) def _A ( self : Optional[Any] ): _UpperCAmelCase : Dict = tempfile.mkdtemp() _UpperCAmelCase : str = self.tokenizer.fairseq_tokens_to_ids self.tokenizer.save_pretrained(A ) _UpperCAmelCase : Tuple = NllbTokenizer.from_pretrained(A ) self.assertDictEqual(new_tok.fairseq_tokens_to_ids , A ) @require_torch def _A ( self : Dict ): _UpperCAmelCase : List[str] = self.tokenizer( self.src_text , text_target=self.tgt_text , padding=A , truncation=A , max_length=len(self.expected_src_tokens ) , return_tensors="pt" , ) _UpperCAmelCase : Tuple = shift_tokens_right( batch["labels"] , self.tokenizer.pad_token_id , self.tokenizer.lang_code_to_id["ron_Latn"] ) self.assertIsInstance(A , A ) self.assertEqual((2, 15) , batch.input_ids.shape ) self.assertEqual((2, 15) , batch.attention_mask.shape ) _UpperCAmelCase : Dict = batch.input_ids.tolist()[0] self.assertListEqual(self.expected_src_tokens , A ) self.assertEqual(A , batch.decoder_input_ids[0, 0] ) # EOS # Test that special tokens are reset self.assertEqual(self.tokenizer.prefix_tokens , [EN_CODE] ) self.assertEqual(self.tokenizer.suffix_tokens , [self.tokenizer.eos_token_id] ) def _A ( self : str ): _UpperCAmelCase : Optional[Any] = self.tokenizer(self.src_text , padding=A , truncation=A , max_length=3 , return_tensors="pt" ) _UpperCAmelCase : Dict = self.tokenizer( text_target=self.tgt_text , padding=A , truncation=A , max_length=10 , return_tensors="pt" ) _UpperCAmelCase : List[Any] = targets["input_ids"] _UpperCAmelCase : Union[str, Any] = shift_tokens_right( A , self.tokenizer.pad_token_id , decoder_start_token_id=self.tokenizer.lang_code_to_id[self.tokenizer.tgt_lang] , ) self.assertEqual(batch.input_ids.shape[1] , 3 ) self.assertEqual(batch.decoder_input_ids.shape[1] , 10 ) @require_torch def _A ( self : List[Any] ): _UpperCAmelCase : str = self.tokenizer._build_translation_inputs( "A test" , return_tensors="pt" , src_lang="eng_Latn" , tgt_lang="fra_Latn" ) self.assertEqual( nested_simplify(A ) , { # A, test, EOS, en_XX "input_ids": [[256047, 70, 7356, 2]], "attention_mask": [[1, 1, 1, 1]], # ar_AR "forced_bos_token_id": 256057, } , ) @require_torch def _A ( self : Any ): _UpperCAmelCase : Dict = True _UpperCAmelCase : Any = self.tokenizer( "UN Chief says there is no military solution in Syria" , src_lang="eng_Latn" , tgt_lang="fra_Latn" ) self.assertEqual( inputs.input_ids , [16297, 134408, 25653, 6370, 248, 254, 103929, 94995, 108, 49486, 2, 256047] ) _UpperCAmelCase : Optional[int] = False _UpperCAmelCase : str = self.tokenizer( "UN Chief says there is no military solution in Syria" , src_lang="eng_Latn" , tgt_lang="fra_Latn" ) self.assertEqual( inputs.input_ids , [256047, 16297, 134408, 25653, 6370, 248, 254, 103929, 94995, 108, 49486, 2] )
31
1
'''simple docstring''' import time from contextlib import contextmanager from pathlib import Path import pytest import requests from huggingface_hub.hf_api import HfApi, HfFolder __SCREAMING_SNAKE_CASE : Any = """__DUMMY_TRANSFORMERS_USER__""" __SCREAMING_SNAKE_CASE : Any = """Dummy User""" __SCREAMING_SNAKE_CASE : Dict = """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""" __SCREAMING_SNAKE_CASE : Tuple = """https://hub-ci.huggingface.co""" __SCREAMING_SNAKE_CASE : str = CI_HUB_ENDPOINT + """/datasets/{repo_id}/resolve/{revision}/{path}""" __SCREAMING_SNAKE_CASE : Union[str, Any] = CI_HUB_ENDPOINT + """/{repo_id}/resolve/{revision}/{filename}""" __SCREAMING_SNAKE_CASE : str = Path("""~/.huggingface/hub_ci_token""").expanduser() @pytest.fixture def UpperCamelCase_ ( _UpperCAmelCase : str ) -> List[str]: """simple docstring""" monkeypatch.setattr( "huggingface_hub.file_download.HUGGINGFACE_CO_URL_TEMPLATE" , _UpperCAmelCase ) @pytest.fixture def UpperCamelCase_ ( _UpperCAmelCase : Optional[Any] ) -> Any: """simple docstring""" monkeypatch.setattr("datasets.config.HF_ENDPOINT" , _UpperCAmelCase ) monkeypatch.setattr("datasets.config.HUB_DATASETS_URL" , _UpperCAmelCase ) @pytest.fixture def UpperCamelCase_ ( _UpperCAmelCase : Optional[int] ) -> List[str]: """simple docstring""" monkeypatch.setattr("huggingface_hub.hf_api.HfFolder.path_token" , _UpperCAmelCase ) @pytest.fixture def UpperCamelCase_ ( _UpperCAmelCase : Union[str, Any] , _UpperCAmelCase : List[str] ) -> List[Any]: """simple docstring""" HfFolder.save_token(_UpperCAmelCase ) yield HfFolder.delete_token() @pytest.fixture(scope="session" ) def UpperCamelCase_ ( ) -> List[str]: """simple docstring""" return HfApi(endpoint=_UpperCAmelCase ) @pytest.fixture(scope="session" ) def UpperCamelCase_ ( _UpperCAmelCase : HfApi ) -> Any: """simple docstring""" _UpperCAmelCase : str = HfFolder.get_token() HfFolder.save_token(_UpperCAmelCase ) yield CI_HUB_USER_TOKEN if previous_token is not None: HfFolder.save_token(_UpperCAmelCase ) @pytest.fixture def UpperCamelCase_ ( _UpperCAmelCase : int ) -> Tuple: """simple docstring""" def _cleanup_repo(_UpperCAmelCase : Optional[Any] ): hf_api.delete_repo(_UpperCAmelCase , token=_UpperCAmelCase , repo_type="dataset" ) return _cleanup_repo @pytest.fixture def UpperCamelCase_ ( _UpperCAmelCase : Tuple ) -> Any: """simple docstring""" @contextmanager def _temporary_repo(_UpperCAmelCase : List[Any] ): try: yield repo_id finally: cleanup_repo(_UpperCAmelCase ) return _temporary_repo @pytest.fixture(scope="session" ) def UpperCamelCase_ ( _UpperCAmelCase : HfApi , _UpperCAmelCase : Optional[Any] , _UpperCAmelCase : str ) -> str: """simple docstring""" _UpperCAmelCase : Dict = F"""repo_txt_data-{int(time.time() * 10e3 )}""" _UpperCAmelCase : List[str] = F"""{CI_HUB_USER}/{repo_name}""" hf_api.create_repo(_UpperCAmelCase , token=_UpperCAmelCase , repo_type="dataset" , private=_UpperCAmelCase ) hf_api.upload_file( token=_UpperCAmelCase , path_or_fileobj=str(_UpperCAmelCase ) , path_in_repo="data/text_data.txt" , repo_id=_UpperCAmelCase , repo_type="dataset" , ) yield repo_id try: hf_api.delete_repo(_UpperCAmelCase , token=_UpperCAmelCase , repo_type="dataset" ) except (requests.exceptions.HTTPError, ValueError): # catch http error and token invalid error pass @pytest.fixture() def UpperCamelCase_ ( _UpperCAmelCase : Optional[int] , _UpperCAmelCase : List[Any] , _UpperCAmelCase : Optional[int] ) -> Union[str, Any]: """simple docstring""" return hf_private_dataset_repo_txt_data_ @pytest.fixture(scope="session" ) def UpperCamelCase_ ( _UpperCAmelCase : HfApi , _UpperCAmelCase : int , _UpperCAmelCase : Any ) -> Tuple: """simple docstring""" _UpperCAmelCase : Union[str, Any] = F"""repo_zipped_txt_data-{int(time.time() * 10e3 )}""" _UpperCAmelCase : List[str] = F"""{CI_HUB_USER}/{repo_name}""" hf_api.create_repo(_UpperCAmelCase , token=_UpperCAmelCase , repo_type="dataset" , private=_UpperCAmelCase ) hf_api.upload_file( token=_UpperCAmelCase , path_or_fileobj=str(_UpperCAmelCase ) , path_in_repo="data.zip" , repo_id=_UpperCAmelCase , repo_type="dataset" , ) yield repo_id try: hf_api.delete_repo(_UpperCAmelCase , token=_UpperCAmelCase , repo_type="dataset" ) except (requests.exceptions.HTTPError, ValueError): # catch http error and token invalid error pass @pytest.fixture() def UpperCamelCase_ ( _UpperCAmelCase : Tuple , _UpperCAmelCase : Optional[int] , _UpperCAmelCase : List[str] ) -> Dict: """simple docstring""" return hf_private_dataset_repo_zipped_txt_data_ @pytest.fixture(scope="session" ) def UpperCamelCase_ ( _UpperCAmelCase : HfApi , _UpperCAmelCase : int , _UpperCAmelCase : int ) -> Dict: """simple docstring""" _UpperCAmelCase : List[str] = F"""repo_zipped_img_data-{int(time.time() * 10e3 )}""" _UpperCAmelCase : Tuple = F"""{CI_HUB_USER}/{repo_name}""" hf_api.create_repo(_UpperCAmelCase , token=_UpperCAmelCase , repo_type="dataset" , private=_UpperCAmelCase ) hf_api.upload_file( token=_UpperCAmelCase , path_or_fileobj=str(_UpperCAmelCase ) , path_in_repo="data.zip" , repo_id=_UpperCAmelCase , repo_type="dataset" , ) yield repo_id try: hf_api.delete_repo(_UpperCAmelCase , token=_UpperCAmelCase , repo_type="dataset" ) except (requests.exceptions.HTTPError, ValueError): # catch http error and token invalid error pass @pytest.fixture() def UpperCamelCase_ ( _UpperCAmelCase : Optional[int] , _UpperCAmelCase : Optional[Any] , _UpperCAmelCase : List[str] ) -> Optional[int]: """simple docstring""" return hf_private_dataset_repo_zipped_img_data_
31
'''simple docstring''' def UpperCamelCase_ ( _UpperCAmelCase : list ) -> list: """simple docstring""" _UpperCAmelCase : List[Any] = len(_UpperCAmelCase ) for _ in range(_UpperCAmelCase ): for i in range(_ % 2 , arr_size - 1 , 2 ): if arr[i + 1] < arr[i]: _UpperCAmelCase , _UpperCAmelCase : int = arr[i + 1], arr[i] return arr if __name__ == "__main__": __SCREAMING_SNAKE_CASE : Optional[Any] = list(range(10, 0, -1)) print(F'Original: {arr}. Sorted: {odd_even_transposition(arr)}')
31
1
'''simple docstring''' import unittest import numpy as np import timeout_decorator # noqa from transformers import BlenderbotSmallConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...generation.test_flax_utils import FlaxGenerationTesterMixin from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor if is_flax_available(): import os # The slow tests are often failing with OOM error on GPU # This makes JAX allocate exactly what is needed on demand, and deallocate memory that is no longer needed # but will be slower as stated here https://jax.readthedocs.io/en/latest/gpu_memory_allocation.html __SCREAMING_SNAKE_CASE : List[str] = """platform""" import jax import jax.numpy as jnp from transformers.models.blenderbot_small.modeling_flax_blenderbot_small import ( FlaxBlenderbotSmallForConditionalGeneration, FlaxBlenderbotSmallModel, shift_tokens_right, ) def UpperCamelCase_ ( _UpperCAmelCase : Dict , _UpperCAmelCase : Any , _UpperCAmelCase : List[Any]=None , _UpperCAmelCase : str=None , _UpperCAmelCase : Tuple=None , _UpperCAmelCase : Tuple=None , _UpperCAmelCase : List[str]=None , _UpperCAmelCase : List[str]=None , ) -> Dict: """simple docstring""" if attention_mask is None: _UpperCAmelCase : List[str] = np.where(input_ids != config.pad_token_id , 1 , 0 ) if decoder_attention_mask is None: _UpperCAmelCase : Union[str, Any] = np.where(decoder_input_ids != config.pad_token_id , 1 , 0 ) if head_mask is None: _UpperCAmelCase : Union[str, Any] = np.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: _UpperCAmelCase : List[str] = np.ones((config.decoder_layers, config.decoder_attention_heads) ) if cross_attn_head_mask is None: _UpperCAmelCase : Union[str, Any] = np.ones((config.decoder_layers, config.decoder_attention_heads) ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": attention_mask, } class lowerCamelCase_ : '''simple docstring''' def __init__( self : Tuple , A : List[str] , A : List[Any]=13 , A : Tuple=7 , A : Union[str, Any]=True , A : int=False , A : Union[str, Any]=99 , A : Dict=16 , A : Any=2 , A : int=4 , A : int=4 , A : str="gelu" , A : List[Any]=0.1 , A : Optional[int]=0.1 , A : Any=32 , A : List[str]=2 , A : Any=1 , A : int=0 , A : int=0.02 , ): _UpperCAmelCase : int = parent _UpperCAmelCase : Tuple = batch_size _UpperCAmelCase : Any = seq_length _UpperCAmelCase : int = is_training _UpperCAmelCase : Optional[Any] = use_labels _UpperCAmelCase : Any = vocab_size _UpperCAmelCase : List[str] = hidden_size _UpperCAmelCase : List[str] = num_hidden_layers _UpperCAmelCase : Optional[Any] = num_attention_heads _UpperCAmelCase : int = intermediate_size _UpperCAmelCase : List[Any] = hidden_act _UpperCAmelCase : List[str] = hidden_dropout_prob _UpperCAmelCase : Tuple = attention_probs_dropout_prob _UpperCAmelCase : str = max_position_embeddings _UpperCAmelCase : List[Any] = eos_token_id _UpperCAmelCase : Any = pad_token_id _UpperCAmelCase : Tuple = bos_token_id _UpperCAmelCase : Tuple = initializer_range def _A ( self : Optional[Any] ): _UpperCAmelCase : List[Any] = np.clip(ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size ) , 3 , self.vocab_size ) _UpperCAmelCase : Optional[int] = np.concatenate((input_ids, 2 * np.ones((self.batch_size, 1) , dtype=np.intaa )) , -1 ) _UpperCAmelCase : str = shift_tokens_right(A , 1 , 2 ) _UpperCAmelCase : str = BlenderbotSmallConfig( vocab_size=self.vocab_size , d_model=self.hidden_size , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , eos_token_id=self.eos_token_id , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , initializer_range=self.initializer_range , use_cache=A , ) _UpperCAmelCase : Tuple = prepare_blenderbot_inputs_dict(A , A , A ) return config, inputs_dict def _A ( self : List[Any] ): _UpperCAmelCase , _UpperCAmelCase : List[str] = self.prepare_config_and_inputs() return config, inputs_dict def _A ( self : Optional[Any] , A : Optional[Any] , A : Optional[int] , A : List[str] ): _UpperCAmelCase : str = 20 _UpperCAmelCase : Optional[Any] = model_class_name(A ) _UpperCAmelCase : List[Any] = model.encode(inputs_dict["input_ids"] ) _UpperCAmelCase , _UpperCAmelCase : Optional[Any] = ( inputs_dict["decoder_input_ids"], inputs_dict["decoder_attention_mask"], ) _UpperCAmelCase : List[Any] = model.init_cache(decoder_input_ids.shape[0] , A , A ) _UpperCAmelCase : Tuple = jnp.ones((decoder_input_ids.shape[0], max_decoder_length) , dtype="i4" ) _UpperCAmelCase : Optional[Any] = jnp.broadcast_to( jnp.arange(decoder_input_ids.shape[-1] - 1 )[None, :] , (decoder_input_ids.shape[0], decoder_input_ids.shape[-1] - 1) , ) _UpperCAmelCase : List[Any] = model.decode( decoder_input_ids[:, :-1] , A , decoder_attention_mask=A , past_key_values=A , decoder_position_ids=A , ) _UpperCAmelCase : int = jnp.array(decoder_input_ids.shape[0] * [[decoder_input_ids.shape[-1] - 1]] , dtype="i4" ) _UpperCAmelCase : Dict = model.decode( decoder_input_ids[:, -1:] , A , decoder_attention_mask=A , past_key_values=outputs_cache.past_key_values , decoder_position_ids=A , ) _UpperCAmelCase : Optional[Any] = model.decode(A , A ) _UpperCAmelCase : int = np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1E-3 , msg=F"""Max diff is {diff}""" ) def _A ( self : Optional[int] , A : List[str] , A : List[str] , A : Optional[Any] ): _UpperCAmelCase : Union[str, Any] = 20 _UpperCAmelCase : List[str] = model_class_name(A ) _UpperCAmelCase : Dict = model.encode(inputs_dict["input_ids"] ) _UpperCAmelCase , _UpperCAmelCase : Union[str, Any] = ( inputs_dict["decoder_input_ids"], inputs_dict["decoder_attention_mask"], ) _UpperCAmelCase : int = jnp.concatenate( [ decoder_attention_mask, jnp.zeros((decoder_attention_mask.shape[0], max_decoder_length - decoder_attention_mask.shape[1]) ), ] , axis=-1 , ) _UpperCAmelCase : Any = model.init_cache(decoder_input_ids.shape[0] , A , A ) _UpperCAmelCase : Optional[Any] = jnp.broadcast_to( jnp.arange(decoder_input_ids.shape[-1] - 1 )[None, :] , (decoder_input_ids.shape[0], decoder_input_ids.shape[-1] - 1) , ) _UpperCAmelCase : List[str] = model.decode( decoder_input_ids[:, :-1] , A , decoder_attention_mask=A , past_key_values=A , decoder_position_ids=A , ) _UpperCAmelCase : int = jnp.array(decoder_input_ids.shape[0] * [[decoder_input_ids.shape[-1] - 1]] , dtype="i4" ) _UpperCAmelCase : Union[str, Any] = model.decode( decoder_input_ids[:, -1:] , A , past_key_values=outputs_cache.past_key_values , decoder_attention_mask=A , decoder_position_ids=A , ) _UpperCAmelCase : Dict = model.decode(A , A , decoder_attention_mask=A ) _UpperCAmelCase : int = np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1E-3 , msg=F"""Max diff is {diff}""" ) @require_flax class lowerCamelCase_ (unittest.TestCase ): '''simple docstring''' __UpperCamelCase: Optional[int] = 9_9 def _A ( self : List[str] ): _UpperCAmelCase : Tuple = np.array( [ [71, 82, 18, 33, 46, 91, 2], [68, 34, 26, 58, 30, 82, 2], [5, 97, 17, 39, 94, 40, 2], [76, 83, 94, 25, 70, 78, 2], [87, 59, 41, 35, 48, 66, 2], [55, 13, 16, 58, 5, 2, 1], # note padding [64, 27, 31, 51, 12, 75, 2], [52, 64, 86, 17, 83, 39, 2], [48, 61, 9, 24, 71, 82, 2], [26, 1, 60, 48, 22, 13, 2], [21, 5, 62, 28, 14, 76, 2], [45, 98, 37, 86, 59, 48, 2], [70, 70, 50, 9, 28, 0, 2], ] , dtype=np.intaa , ) _UpperCAmelCase : int = input_ids.shape[0] _UpperCAmelCase : Any = BlenderbotSmallConfig( vocab_size=self.vocab_size , d_model=24 , encoder_layers=2 , decoder_layers=2 , encoder_attention_heads=2 , decoder_attention_heads=2 , encoder_ffn_dim=32 , decoder_ffn_dim=32 , max_position_embeddings=48 , eos_token_id=2 , pad_token_id=1 , bos_token_id=0 , ) return config, input_ids, batch_size def _A ( self : Optional[Any] ): _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase : Optional[Any] = self._get_config_and_data() _UpperCAmelCase : int = FlaxBlenderbotSmallForConditionalGeneration(A ) _UpperCAmelCase : int = lm_model(input_ids=A ) _UpperCAmelCase : List[str] = (batch_size, input_ids.shape[1], config.vocab_size) self.assertEqual(outputs["logits"].shape , A ) def _A ( self : Union[str, Any] ): _UpperCAmelCase : Union[str, Any] = BlenderbotSmallConfig( vocab_size=self.vocab_size , d_model=14 , encoder_layers=2 , decoder_layers=2 , encoder_attention_heads=2 , decoder_attention_heads=2 , encoder_ffn_dim=8 , decoder_ffn_dim=8 , max_position_embeddings=48 , ) _UpperCAmelCase : Optional[int] = FlaxBlenderbotSmallForConditionalGeneration(A ) _UpperCAmelCase : Dict = np.array([[71, 82, 18, 33, 46, 91, 2], [68, 34, 26, 58, 30, 2, 1]] , dtype=np.intaa ) _UpperCAmelCase : List[str] = np.array([[82, 71, 82, 18, 2], [58, 68, 2, 1, 1]] , dtype=np.intaa ) _UpperCAmelCase : List[str] = lm_model(input_ids=A , decoder_input_ids=A ) _UpperCAmelCase : Union[str, Any] = (*summary.shape, config.vocab_size) self.assertEqual(outputs["logits"].shape , A ) def _A ( self : Optional[int] ): _UpperCAmelCase : Union[str, Any] = np.array([[71, 82, 18, 33, 2, 1, 1], [68, 34, 26, 58, 30, 82, 2]] , dtype=np.intaa ) _UpperCAmelCase : Dict = shift_tokens_right(A , 1 , 2 ) _UpperCAmelCase : Dict = np.equal(A , 1 ).astype(np.floataa ).sum() _UpperCAmelCase : str = np.equal(A , 1 ).astype(np.floataa ).sum() self.assertEqual(shifted.shape , input_ids.shape ) self.assertEqual(A , n_pad_before - 1 ) self.assertTrue(np.equal(shifted[:, 0] , 2 ).all() ) @require_flax class lowerCamelCase_ (snake_case__ , unittest.TestCase , snake_case__ ): '''simple docstring''' __UpperCamelCase: List[Any] = True __UpperCamelCase: Tuple = ( ( FlaxBlenderbotSmallModel, FlaxBlenderbotSmallForConditionalGeneration, ) if is_flax_available() else () ) __UpperCamelCase: List[Any] = (FlaxBlenderbotSmallForConditionalGeneration,) if is_flax_available() else () def _A ( self : Optional[int] ): _UpperCAmelCase : Union[str, Any] = FlaxBlenderbotSmallModelTester(self ) def _A ( self : Optional[int] ): _UpperCAmelCase , _UpperCAmelCase : Union[str, Any] = self.model_tester.prepare_config_and_inputs() for model_class in self.all_model_classes: self.model_tester.check_use_cache_forward(A , A , A ) def _A ( self : Any ): _UpperCAmelCase , _UpperCAmelCase : Optional[int] = self.model_tester.prepare_config_and_inputs() for model_class in self.all_model_classes: self.model_tester.check_use_cache_forward_with_attn_mask(A , A , A ) def _A ( self : Dict ): _UpperCAmelCase , _UpperCAmelCase : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): _UpperCAmelCase : Any = self._prepare_for_class(A , A ) _UpperCAmelCase : Any = model_class(A ) @jax.jit def encode_jitted(A : Dict , A : str=None , **A : List[Any] ): return model.encode(input_ids=A , attention_mask=A ) with self.subTest("JIT Enabled" ): _UpperCAmelCase : int = encode_jitted(**A ).to_tuple() with self.subTest("JIT Disabled" ): with jax.disable_jit(): _UpperCAmelCase : Optional[int] = encode_jitted(**A ).to_tuple() self.assertEqual(len(A ) , len(A ) ) for jitted_output, output in zip(A , A ): self.assertEqual(jitted_output.shape , output.shape ) def _A ( self : Tuple ): _UpperCAmelCase , _UpperCAmelCase : Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): _UpperCAmelCase : Dict = model_class(A ) _UpperCAmelCase : Union[str, Any] = model.encode(inputs_dict["input_ids"] , inputs_dict["attention_mask"] ) _UpperCAmelCase : str = { "decoder_input_ids": inputs_dict["decoder_input_ids"], "decoder_attention_mask": inputs_dict["decoder_attention_mask"], "encoder_outputs": encoder_outputs, } @jax.jit def decode_jitted(A : Dict , A : Tuple , A : List[str] ): return model.decode( decoder_input_ids=A , decoder_attention_mask=A , encoder_outputs=A , ) with self.subTest("JIT Enabled" ): _UpperCAmelCase : str = decode_jitted(**A ).to_tuple() with self.subTest("JIT Disabled" ): with jax.disable_jit(): _UpperCAmelCase : Dict = decode_jitted(**A ).to_tuple() self.assertEqual(len(A ) , len(A ) ) for jitted_output, output in zip(A , A ): self.assertEqual(jitted_output.shape , output.shape ) @slow def _A ( self : Union[str, Any] ): for model_class_name in self.all_model_classes: _UpperCAmelCase : int = model_class_name.from_pretrained("facebook/blenderbot_small-90M" ) # FlaxBlenderbotForSequenceClassification expects eos token in input_ids _UpperCAmelCase : Dict = np.ones((1, 1) ) * model.config.eos_token_id _UpperCAmelCase : Optional[int] = model(A ) self.assertIsNotNone(A )
31
'''simple docstring''' import os from typing import Any, Callable, Dict, List, Optional, Tuple, Union import torch from torch import nn from ...models.controlnet import ControlNetModel, ControlNetOutput from ...models.modeling_utils import ModelMixin from ...utils import logging __SCREAMING_SNAKE_CASE : str = logging.get_logger(__name__) class lowerCamelCase_ (snake_case__ ): '''simple docstring''' def __init__( self : Optional[int] , A : Union[List[ControlNetModel], Tuple[ControlNetModel]] ): super().__init__() _UpperCAmelCase : Optional[int] = nn.ModuleList(A ) def _A ( self : Dict , A : torch.FloatTensor , A : Union[torch.Tensor, float, int] , A : torch.Tensor , A : List[torch.tensor] , A : List[float] , A : Optional[torch.Tensor] = None , A : Optional[torch.Tensor] = None , A : Optional[torch.Tensor] = None , A : Optional[Dict[str, Any]] = None , A : bool = False , A : bool = True , ): for i, (image, scale, controlnet) in enumerate(zip(A , A , self.nets ) ): _UpperCAmelCase , _UpperCAmelCase : str = controlnet( A , A , A , A , A , A , A , A , A , A , A , ) # merge samples if i == 0: _UpperCAmelCase , _UpperCAmelCase : List[Any] = down_samples, mid_sample else: _UpperCAmelCase : Optional[int] = [ samples_prev + samples_curr for samples_prev, samples_curr in zip(A , A ) ] mid_block_res_sample += mid_sample return down_block_res_samples, mid_block_res_sample def _A ( self : List[str] , A : Union[str, os.PathLike] , A : bool = True , A : Callable = None , A : bool = False , A : Optional[str] = None , ): _UpperCAmelCase : str = 0 _UpperCAmelCase : str = save_directory for controlnet in self.nets: controlnet.save_pretrained( A , is_main_process=A , save_function=A , safe_serialization=A , variant=A , ) idx += 1 _UpperCAmelCase : Tuple = model_path_to_save + F"""_{idx}""" @classmethod def _A ( cls : int , A : Optional[Union[str, os.PathLike]] , **A : Tuple ): _UpperCAmelCase : str = 0 _UpperCAmelCase : int = [] # load controlnet and append to list until no controlnet directory exists anymore # first controlnet has to be saved under `./mydirectory/controlnet` to be compliant with `DiffusionPipeline.from_prertained` # second, third, ... controlnets have to be saved under `./mydirectory/controlnet_1`, `./mydirectory/controlnet_2`, ... _UpperCAmelCase : int = pretrained_model_path while os.path.isdir(A ): _UpperCAmelCase : List[str] = ControlNetModel.from_pretrained(A , **A ) controlnets.append(A ) idx += 1 _UpperCAmelCase : Dict = pretrained_model_path + F"""_{idx}""" logger.info(F"""{len(A )} controlnets loaded from {pretrained_model_path}.""" ) if len(A ) == 0: raise ValueError( F"""No ControlNets found under {os.path.dirname(A )}. Expected at least {pretrained_model_path + '_0'}.""" ) return cls(A )
31
1
'''simple docstring''' import os import sys import warnings from dataclasses import dataclass, field from io import BytesIO from typing import TYPE_CHECKING, Any, ClassVar, Dict, List, Optional, Union import numpy as np import pyarrow as pa from .. import config from ..download.streaming_download_manager import xopen from ..table import array_cast from ..utils.file_utils import is_local_path from ..utils.py_utils import first_non_null_value, no_op_if_value_is_null, string_to_dict if TYPE_CHECKING: import PIL.Image from .features import FeatureType __SCREAMING_SNAKE_CASE : Optional[List[str]] = None __SCREAMING_SNAKE_CASE : Any = """<""" if sys.byteorder == """little""" else """>""" # Origin: https://github.com/python-pillow/Pillow/blob/698951e19e19972aeed56df686868f1329981c12/src/PIL/Image.py#L3126 minus "|i1" which values are not preserved correctly when saving and loading an image __SCREAMING_SNAKE_CASE : Dict = [ np.dtype("""|b1"""), np.dtype("""|u1"""), np.dtype("""<u2"""), np.dtype(""">u2"""), np.dtype("""<i2"""), np.dtype(""">i2"""), np.dtype("""<u4"""), np.dtype(""">u4"""), np.dtype("""<i4"""), np.dtype(""">i4"""), np.dtype("""<f4"""), np.dtype(""">f4"""), np.dtype("""<f8"""), np.dtype(""">f8"""), ] @dataclass class lowerCamelCase_ : '''simple docstring''' __UpperCamelCase: bool = True __UpperCamelCase: Optional[str] = None # Automatically constructed __UpperCamelCase: ClassVar[str] = "PIL.Image.Image" __UpperCamelCase: ClassVar[Any] = pa.struct({"bytes": pa.binary(), "path": pa.string()} ) __UpperCamelCase: str = field(default="Image" , init=snake_case__ , repr=snake_case__ ) def __call__( self : Any ): return self.pa_type def _A ( self : Tuple , A : Union[str, bytes, dict, np.ndarray, "PIL.Image.Image"] ): if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError("To support encoding images, please install 'Pillow'." ) if isinstance(A , A ): _UpperCAmelCase : Optional[int] = np.array(A ) if isinstance(A , A ): return {"path": value, "bytes": None} elif isinstance(A , A ): return {"path": None, "bytes": value} elif isinstance(A , np.ndarray ): # convert the image array to PNG/TIFF bytes return encode_np_array(A ) elif isinstance(A , PIL.Image.Image ): # convert the PIL image to bytes (default format is PNG/TIFF) return encode_pil_image(A ) elif value.get("path" ) is not None and os.path.isfile(value["path"] ): # we set "bytes": None to not duplicate the data if they're already available locally return {"bytes": None, "path": value.get("path" )} elif value.get("bytes" ) is not None or value.get("path" ) is not None: # store the image bytes, and path is used to infer the image format using the file extension return {"bytes": value.get("bytes" ), "path": value.get("path" )} else: raise ValueError( F"""An image sample should have one of 'path' or 'bytes' but they are missing or None in {value}.""" ) def _A ( self : List[str] , A : dict , A : Optional[Any]=None ): if not self.decode: raise RuntimeError("Decoding is disabled for this feature. Please use Image(decode=True) instead." ) if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError("To support decoding images, please install 'Pillow'." ) if token_per_repo_id is None: _UpperCAmelCase : List[Any] = {} _UpperCAmelCase , _UpperCAmelCase : Optional[Any] = value["path"], value["bytes"] if bytes_ is None: if path is None: raise ValueError(F"""An image should have one of 'path' or 'bytes' but both are None in {value}.""" ) else: if is_local_path(A ): _UpperCAmelCase : List[str] = PIL.Image.open(A ) else: _UpperCAmelCase : int = path.split("::" )[-1] try: _UpperCAmelCase : Optional[Any] = string_to_dict(A , config.HUB_DATASETS_URL )["repo_id"] _UpperCAmelCase : int = token_per_repo_id.get(A ) except ValueError: _UpperCAmelCase : Optional[Any] = None with xopen(A , "rb" , use_auth_token=A ) as f: _UpperCAmelCase : str = BytesIO(f.read() ) _UpperCAmelCase : List[str] = PIL.Image.open(bytes_ ) else: _UpperCAmelCase : Any = PIL.Image.open(BytesIO(bytes_ ) ) image.load() # to avoid "Too many open files" errors return image def _A ( self : Optional[Any] ): from .features import Value return ( self if self.decode else { "bytes": Value("binary" ), "path": Value("string" ), } ) def _A ( self : Optional[int] , A : Union[pa.StringArray, pa.StructArray, pa.ListArray] ): if pa.types.is_string(storage.type ): _UpperCAmelCase : Any = pa.array([None] * len(A ) , type=pa.binary() ) _UpperCAmelCase : List[Any] = pa.StructArray.from_arrays([bytes_array, storage] , ["bytes", "path"] , mask=storage.is_null() ) elif pa.types.is_binary(storage.type ): _UpperCAmelCase : str = pa.array([None] * len(A ) , type=pa.string() ) _UpperCAmelCase : Union[str, Any] = pa.StructArray.from_arrays([storage, path_array] , ["bytes", "path"] , mask=storage.is_null() ) elif pa.types.is_struct(storage.type ): if storage.type.get_field_index("bytes" ) >= 0: _UpperCAmelCase : Tuple = storage.field("bytes" ) else: _UpperCAmelCase : str = pa.array([None] * len(A ) , type=pa.binary() ) if storage.type.get_field_index("path" ) >= 0: _UpperCAmelCase : Tuple = storage.field("path" ) else: _UpperCAmelCase : Optional[Any] = pa.array([None] * len(A ) , type=pa.string() ) _UpperCAmelCase : List[Any] = pa.StructArray.from_arrays([bytes_array, path_array] , ["bytes", "path"] , mask=storage.is_null() ) elif pa.types.is_list(storage.type ): _UpperCAmelCase : List[Any] = pa.array( [encode_np_array(np.array(A ) )["bytes"] if arr is not None else None for arr in storage.to_pylist()] , type=pa.binary() , ) _UpperCAmelCase : Union[str, Any] = pa.array([None] * len(A ) , type=pa.string() ) _UpperCAmelCase : Union[str, Any] = pa.StructArray.from_arrays( [bytes_array, path_array] , ["bytes", "path"] , mask=bytes_array.is_null() ) return array_cast(A , self.pa_type ) def _A ( self : List[str] , A : pa.StructArray ): @no_op_if_value_is_null def path_to_bytes(A : Dict ): with xopen(A , "rb" ) as f: _UpperCAmelCase : List[str] = f.read() return bytes_ _UpperCAmelCase : str = pa.array( [ (path_to_bytes(x["path"] ) if x["bytes"] is None else x["bytes"]) if x is not None else None for x in storage.to_pylist() ] , type=pa.binary() , ) _UpperCAmelCase : int = pa.array( [os.path.basename(A ) if path is not None else None for path in storage.field("path" ).to_pylist()] , type=pa.string() , ) _UpperCAmelCase : Union[str, Any] = pa.StructArray.from_arrays([bytes_array, path_array] , ["bytes", "path"] , mask=bytes_array.is_null() ) return array_cast(A , self.pa_type ) def UpperCamelCase_ ( ) -> List[str]: """simple docstring""" if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError("To support encoding images, please install 'Pillow'." ) global _IMAGE_COMPRESSION_FORMATS if _IMAGE_COMPRESSION_FORMATS is None: PIL.Image.init() _UpperCAmelCase : Any = list(set(PIL.Image.OPEN.keys() ) & set(PIL.Image.SAVE.keys() ) ) return _IMAGE_COMPRESSION_FORMATS def UpperCamelCase_ ( _UpperCAmelCase : "PIL.Image.Image" ) -> bytes: """simple docstring""" _UpperCAmelCase : List[str] = BytesIO() if image.format in list_image_compression_formats(): _UpperCAmelCase : Tuple = image.format else: _UpperCAmelCase : Optional[Any] = "PNG" if image.mode in ["1", "L", "LA", "RGB", "RGBA"] else "TIFF" image.save(_UpperCAmelCase , format=_UpperCAmelCase ) return buffer.getvalue() def UpperCamelCase_ ( _UpperCAmelCase : "PIL.Image.Image" ) -> dict: """simple docstring""" if hasattr(_UpperCAmelCase , "filename" ) and image.filename != "": return {"path": image.filename, "bytes": None} else: return {"path": None, "bytes": image_to_bytes(_UpperCAmelCase )} def UpperCamelCase_ ( _UpperCAmelCase : np.ndarray ) -> dict: """simple docstring""" if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError("To support encoding images, please install 'Pillow'." ) _UpperCAmelCase : Union[str, Any] = array.dtype _UpperCAmelCase : Tuple = dtype.byteorder if dtype.byteorder != "=" else _NATIVE_BYTEORDER _UpperCAmelCase : List[str] = dtype.kind _UpperCAmelCase : Dict = dtype.itemsize _UpperCAmelCase : Any = None # Multi-channel array case (only np.dtype("|u1") is allowed) if array.shape[2:]: _UpperCAmelCase : Dict = np.dtype("|u1" ) if dtype_kind not in ["u", "i"]: raise TypeError( F"""Unsupported array dtype {dtype} for image encoding. Only {dest_dtype} is supported for multi-channel arrays.""" ) if dtype is not dest_dtype: warnings.warn(F"""Downcasting array dtype {dtype} to {dest_dtype} to be compatible with 'Pillow'""" ) # Exact match elif dtype in _VALID_IMAGE_ARRAY_DTPYES: _UpperCAmelCase : Optional[Any] = dtype else: # Downcast the type within the kind (np.can_cast(from_type, to_type, casting="same_kind") doesn't behave as expected, so do it manually) while dtype_itemsize >= 1: _UpperCAmelCase : Union[str, Any] = dtype_byteorder + dtype_kind + str(_UpperCAmelCase ) _UpperCAmelCase : List[Any] = np.dtype(_UpperCAmelCase ) if dest_dtype in _VALID_IMAGE_ARRAY_DTPYES: warnings.warn(F"""Downcasting array dtype {dtype} to {dest_dtype} to be compatible with 'Pillow'""" ) break else: dtype_itemsize //= 2 if dest_dtype is None: raise TypeError( F"""Cannot convert dtype {dtype} to a valid image dtype. Valid image dtypes: {_VALID_IMAGE_ARRAY_DTPYES}""" ) _UpperCAmelCase : Optional[Any] = PIL.Image.fromarray(array.astype(_UpperCAmelCase ) ) return {"path": None, "bytes": image_to_bytes(_UpperCAmelCase )} def UpperCamelCase_ ( _UpperCAmelCase : Union[List[str], List[dict], List[np.ndarray], List["PIL.Image.Image"]] ) -> List[dict]: """simple docstring""" if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError("To support encoding images, please install 'Pillow'." ) if objs: _UpperCAmelCase , _UpperCAmelCase : Any = first_non_null_value(_UpperCAmelCase ) if isinstance(_UpperCAmelCase , _UpperCAmelCase ): return [{"path": obj, "bytes": None} if obj is not None else None for obj in objs] if isinstance(_UpperCAmelCase , np.ndarray ): _UpperCAmelCase : Dict = no_op_if_value_is_null(_UpperCAmelCase ) return [obj_to_image_dict_func(_UpperCAmelCase ) for obj in objs] elif isinstance(_UpperCAmelCase , PIL.Image.Image ): _UpperCAmelCase : Optional[int] = no_op_if_value_is_null(_UpperCAmelCase ) return [obj_to_image_dict_func(_UpperCAmelCase ) for obj in objs] else: return objs else: return objs
31
'''simple docstring''' import argparse import json import pickle from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import MaskFormerConfig, MaskFormerForInstanceSegmentation, MaskFormerImageProcessor, SwinConfig from transformers.utils import logging logging.set_verbosity_info() __SCREAMING_SNAKE_CASE : Union[str, Any] = logging.get_logger(__name__) def UpperCamelCase_ ( _UpperCAmelCase : str ) -> Optional[Any]: """simple docstring""" _UpperCAmelCase : int = SwinConfig.from_pretrained( "microsoft/swin-tiny-patch4-window7-224" , out_features=["stage1", "stage2", "stage3", "stage4"] ) _UpperCAmelCase : List[Any] = MaskFormerConfig(backbone_config=_UpperCAmelCase ) _UpperCAmelCase : Tuple = "huggingface/label-files" if "ade20k-full" in model_name: # this should be ok _UpperCAmelCase : Dict = 847 _UpperCAmelCase : Any = "maskformer-ade20k-full-id2label.json" elif "ade" in model_name: # this should be ok _UpperCAmelCase : Any = 150 _UpperCAmelCase : Any = "ade20k-id2label.json" elif "coco-stuff" in model_name: # this should be ok _UpperCAmelCase : Tuple = 171 _UpperCAmelCase : Union[str, Any] = "maskformer-coco-stuff-id2label.json" elif "coco" in model_name: # TODO _UpperCAmelCase : Any = 133 _UpperCAmelCase : int = "coco-panoptic-id2label.json" elif "cityscapes" in model_name: # this should be ok _UpperCAmelCase : Optional[int] = 19 _UpperCAmelCase : str = "cityscapes-id2label.json" elif "vistas" in model_name: # this should be ok _UpperCAmelCase : Optional[int] = 65 _UpperCAmelCase : Tuple = "mapillary-vistas-id2label.json" _UpperCAmelCase : List[Any] = json.load(open(hf_hub_download(_UpperCAmelCase , _UpperCAmelCase , repo_type="dataset" ) , "r" ) ) _UpperCAmelCase : Tuple = {int(_UpperCAmelCase ): v for k, v in idalabel.items()} return config def UpperCamelCase_ ( _UpperCAmelCase : Optional[int] ) -> List[Any]: """simple docstring""" _UpperCAmelCase : Dict = [] # stem # fmt: off rename_keys.append(("backbone.patch_embed.proj.weight", "model.pixel_level_module.encoder.model.embeddings.patch_embeddings.projection.weight") ) rename_keys.append(("backbone.patch_embed.proj.bias", "model.pixel_level_module.encoder.model.embeddings.patch_embeddings.projection.bias") ) rename_keys.append(("backbone.patch_embed.norm.weight", "model.pixel_level_module.encoder.model.embeddings.norm.weight") ) rename_keys.append(("backbone.patch_embed.norm.bias", "model.pixel_level_module.encoder.model.embeddings.norm.bias") ) # stages for i in range(len(config.backbone_config.depths ) ): for j in range(config.backbone_config.depths[i] ): rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.norm1.weight""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.layernorm_before.weight""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.norm1.bias""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.layernorm_before.bias""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.attn.relative_position_bias_table""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_bias_table""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.attn.relative_position_index""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_index""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.attn.proj.weight""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.weight""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.attn.proj.bias""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.bias""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.norm2.weight""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.layernorm_after.weight""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.norm2.bias""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.layernorm_after.bias""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.mlp.fc1.weight""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.weight""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.mlp.fc1.bias""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.bias""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.mlp.fc2.weight""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.output.dense.weight""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.mlp.fc2.bias""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.output.dense.bias""") ) if i < 3: rename_keys.append((F"""backbone.layers.{i}.downsample.reduction.weight""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.downsample.reduction.weight""") ) rename_keys.append((F"""backbone.layers.{i}.downsample.norm.weight""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.downsample.norm.weight""") ) rename_keys.append((F"""backbone.layers.{i}.downsample.norm.bias""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.downsample.norm.bias""") ) rename_keys.append((F"""backbone.norm{i}.weight""", F"""model.pixel_level_module.encoder.hidden_states_norms.{i}.weight""") ) rename_keys.append((F"""backbone.norm{i}.bias""", F"""model.pixel_level_module.encoder.hidden_states_norms.{i}.bias""") ) # FPN rename_keys.append(("sem_seg_head.layer_4.weight", "model.pixel_level_module.decoder.fpn.stem.0.weight") ) rename_keys.append(("sem_seg_head.layer_4.norm.weight", "model.pixel_level_module.decoder.fpn.stem.1.weight") ) rename_keys.append(("sem_seg_head.layer_4.norm.bias", "model.pixel_level_module.decoder.fpn.stem.1.bias") ) for source_index, target_index in zip(range(3 , 0 , -1 ) , range(0 , 3 ) ): rename_keys.append((F"""sem_seg_head.adapter_{source_index}.weight""", F"""model.pixel_level_module.decoder.fpn.layers.{target_index}.proj.0.weight""") ) rename_keys.append((F"""sem_seg_head.adapter_{source_index}.norm.weight""", F"""model.pixel_level_module.decoder.fpn.layers.{target_index}.proj.1.weight""") ) rename_keys.append((F"""sem_seg_head.adapter_{source_index}.norm.bias""", F"""model.pixel_level_module.decoder.fpn.layers.{target_index}.proj.1.bias""") ) rename_keys.append((F"""sem_seg_head.layer_{source_index}.weight""", F"""model.pixel_level_module.decoder.fpn.layers.{target_index}.block.0.weight""") ) rename_keys.append((F"""sem_seg_head.layer_{source_index}.norm.weight""", F"""model.pixel_level_module.decoder.fpn.layers.{target_index}.block.1.weight""") ) rename_keys.append((F"""sem_seg_head.layer_{source_index}.norm.bias""", F"""model.pixel_level_module.decoder.fpn.layers.{target_index}.block.1.bias""") ) rename_keys.append(("sem_seg_head.mask_features.weight", "model.pixel_level_module.decoder.mask_projection.weight") ) rename_keys.append(("sem_seg_head.mask_features.bias", "model.pixel_level_module.decoder.mask_projection.bias") ) # Transformer decoder for idx in range(config.decoder_config.decoder_layers ): # self-attention out projection rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.self_attn.out_proj.weight""", F"""model.transformer_module.decoder.layers.{idx}.self_attn.out_proj.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.self_attn.out_proj.bias""", F"""model.transformer_module.decoder.layers.{idx}.self_attn.out_proj.bias""") ) # cross-attention out projection rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.multihead_attn.out_proj.weight""", F"""model.transformer_module.decoder.layers.{idx}.encoder_attn.out_proj.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.multihead_attn.out_proj.bias""", F"""model.transformer_module.decoder.layers.{idx}.encoder_attn.out_proj.bias""") ) # MLP 1 rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.linear1.weight""", F"""model.transformer_module.decoder.layers.{idx}.fc1.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.linear1.bias""", F"""model.transformer_module.decoder.layers.{idx}.fc1.bias""") ) # MLP 2 rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.linear2.weight""", F"""model.transformer_module.decoder.layers.{idx}.fc2.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.linear2.bias""", F"""model.transformer_module.decoder.layers.{idx}.fc2.bias""") ) # layernorm 1 (self-attention layernorm) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm1.weight""", F"""model.transformer_module.decoder.layers.{idx}.self_attn_layer_norm.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm1.bias""", F"""model.transformer_module.decoder.layers.{idx}.self_attn_layer_norm.bias""") ) # layernorm 2 (cross-attention layernorm) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm2.weight""", F"""model.transformer_module.decoder.layers.{idx}.encoder_attn_layer_norm.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm2.bias""", F"""model.transformer_module.decoder.layers.{idx}.encoder_attn_layer_norm.bias""") ) # layernorm 3 (final layernorm) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm3.weight""", F"""model.transformer_module.decoder.layers.{idx}.final_layer_norm.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm3.bias""", F"""model.transformer_module.decoder.layers.{idx}.final_layer_norm.bias""") ) rename_keys.append(("sem_seg_head.predictor.transformer.decoder.norm.weight", "model.transformer_module.decoder.layernorm.weight") ) rename_keys.append(("sem_seg_head.predictor.transformer.decoder.norm.bias", "model.transformer_module.decoder.layernorm.bias") ) # heads on top rename_keys.append(("sem_seg_head.predictor.query_embed.weight", "model.transformer_module.queries_embedder.weight") ) rename_keys.append(("sem_seg_head.predictor.input_proj.weight", "model.transformer_module.input_projection.weight") ) rename_keys.append(("sem_seg_head.predictor.input_proj.bias", "model.transformer_module.input_projection.bias") ) rename_keys.append(("sem_seg_head.predictor.class_embed.weight", "class_predictor.weight") ) rename_keys.append(("sem_seg_head.predictor.class_embed.bias", "class_predictor.bias") ) for i in range(3 ): rename_keys.append((F"""sem_seg_head.predictor.mask_embed.layers.{i}.weight""", F"""mask_embedder.{i}.0.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.mask_embed.layers.{i}.bias""", F"""mask_embedder.{i}.0.bias""") ) # fmt: on return rename_keys def UpperCamelCase_ ( _UpperCAmelCase : int , _UpperCAmelCase : Tuple , _UpperCAmelCase : List[Any] ) -> List[str]: """simple docstring""" _UpperCAmelCase : Optional[int] = dct.pop(_UpperCAmelCase ) _UpperCAmelCase : List[str] = val def UpperCamelCase_ ( _UpperCAmelCase : List[Any] , _UpperCAmelCase : Tuple ) -> Optional[int]: """simple docstring""" _UpperCAmelCase : List[str] = [int(backbone_config.embed_dim * 2**i ) for i in range(len(backbone_config.depths ) )] for i in range(len(backbone_config.depths ) ): _UpperCAmelCase : Optional[int] = num_features[i] for j in range(backbone_config.depths[i] ): # fmt: off # read in weights + bias of input projection layer (in original implementation, this is a single matrix + bias) _UpperCAmelCase : Any = state_dict.pop(F"""backbone.layers.{i}.blocks.{j}.attn.qkv.weight""" ) _UpperCAmelCase : Optional[int] = state_dict.pop(F"""backbone.layers.{i}.blocks.{j}.attn.qkv.bias""" ) # next, add query, keys and values (in that order) to the state dict _UpperCAmelCase : List[str] = in_proj_weight[:dim, :] _UpperCAmelCase : Tuple = in_proj_bias[: dim] _UpperCAmelCase : List[Any] = in_proj_weight[ dim : dim * 2, : ] _UpperCAmelCase : List[str] = in_proj_bias[ dim : dim * 2 ] _UpperCAmelCase : Optional[Any] = in_proj_weight[ -dim :, : ] _UpperCAmelCase : Dict = in_proj_bias[-dim :] # fmt: on def UpperCamelCase_ ( _UpperCAmelCase : Dict , _UpperCAmelCase : str ) -> Dict: """simple docstring""" _UpperCAmelCase : Union[str, Any] = config.decoder_config.hidden_size for idx in range(config.decoder_config.decoder_layers ): # read in weights + bias of self-attention input projection layer (in the original implementation, this is a single matrix + bias) _UpperCAmelCase : Dict = state_dict.pop(F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.self_attn.in_proj_weight""" ) _UpperCAmelCase : Dict = state_dict.pop(F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.self_attn.in_proj_bias""" ) # next, add query, keys and values (in that order) to the state dict _UpperCAmelCase : int = in_proj_weight[: hidden_size, :] _UpperCAmelCase : Union[str, Any] = in_proj_bias[:config.hidden_size] _UpperCAmelCase : List[str] = in_proj_weight[hidden_size : hidden_size * 2, :] _UpperCAmelCase : List[str] = in_proj_bias[hidden_size : hidden_size * 2] _UpperCAmelCase : int = in_proj_weight[-hidden_size :, :] _UpperCAmelCase : Optional[Any] = in_proj_bias[-hidden_size :] # read in weights + bias of cross-attention input projection layer (in the original implementation, this is a single matrix + bias) _UpperCAmelCase : Optional[Any] = state_dict.pop(F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.multihead_attn.in_proj_weight""" ) _UpperCAmelCase : Tuple = state_dict.pop(F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.multihead_attn.in_proj_bias""" ) # next, add query, keys and values (in that order) to the state dict _UpperCAmelCase : Any = in_proj_weight[: hidden_size, :] _UpperCAmelCase : Tuple = in_proj_bias[:config.hidden_size] _UpperCAmelCase : Dict = in_proj_weight[hidden_size : hidden_size * 2, :] _UpperCAmelCase : Dict = in_proj_bias[hidden_size : hidden_size * 2] _UpperCAmelCase : Optional[int] = in_proj_weight[-hidden_size :, :] _UpperCAmelCase : Union[str, Any] = in_proj_bias[-hidden_size :] # fmt: on def UpperCamelCase_ ( ) -> torch.Tensor: """simple docstring""" _UpperCAmelCase : int = "http://images.cocodataset.org/val2017/000000039769.jpg" _UpperCAmelCase : Any = Image.open(requests.get(_UpperCAmelCase , stream=_UpperCAmelCase ).raw ) return im @torch.no_grad() def UpperCamelCase_ ( _UpperCAmelCase : str , _UpperCAmelCase : str , _UpperCAmelCase : str , _UpperCAmelCase : bool = False ) -> List[str]: """simple docstring""" _UpperCAmelCase : Optional[int] = get_maskformer_config(_UpperCAmelCase ) # load original state_dict with open(_UpperCAmelCase , "rb" ) as f: _UpperCAmelCase : Optional[int] = pickle.load(_UpperCAmelCase ) _UpperCAmelCase : Optional[int] = data["model"] # for name, param in state_dict.items(): # print(name, param.shape) # rename keys _UpperCAmelCase : Any = create_rename_keys(_UpperCAmelCase ) for src, dest in rename_keys: rename_key(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) read_in_swin_q_k_v(_UpperCAmelCase , config.backbone_config ) read_in_decoder_q_k_v(_UpperCAmelCase , _UpperCAmelCase ) # update to torch tensors for key, value in state_dict.items(): _UpperCAmelCase : Tuple = torch.from_numpy(_UpperCAmelCase ) # load 🤗 model _UpperCAmelCase : Union[str, Any] = MaskFormerForInstanceSegmentation(_UpperCAmelCase ) model.eval() for name, param in model.named_parameters(): print(_UpperCAmelCase , param.shape ) _UpperCAmelCase , _UpperCAmelCase : Any = model.load_state_dict(_UpperCAmelCase , strict=_UpperCAmelCase ) assert missing_keys == [ "model.pixel_level_module.encoder.model.layernorm.weight", "model.pixel_level_module.encoder.model.layernorm.bias", ] assert len(_UpperCAmelCase ) == 0, F"""Unexpected keys: {unexpected_keys}""" # verify results _UpperCAmelCase : Optional[int] = prepare_img() if "vistas" in model_name: _UpperCAmelCase : int = 65 elif "cityscapes" in model_name: _UpperCAmelCase : Tuple = 65_535 else: _UpperCAmelCase : Any = 255 _UpperCAmelCase : Optional[Any] = True if "ade" in model_name else False _UpperCAmelCase : Optional[int] = MaskFormerImageProcessor(ignore_index=_UpperCAmelCase , reduce_labels=_UpperCAmelCase ) _UpperCAmelCase : Optional[int] = image_processor(_UpperCAmelCase , return_tensors="pt" ) _UpperCAmelCase : List[Any] = model(**_UpperCAmelCase ) print("Logits:" , outputs.class_queries_logits[0, :3, :3] ) if model_name == "maskformer-swin-tiny-ade": _UpperCAmelCase : Tuple = torch.tensor( [[3.6_3_5_3, -4.4_7_7_0, -2.6_0_6_5], [0.5_0_8_1, -4.2_3_9_4, -3.5_3_4_3], [2.1_9_0_9, -5.0_3_5_3, -1.9_3_2_3]] ) assert torch.allclose(outputs.class_queries_logits[0, :3, :3] , _UpperCAmelCase , atol=1e-4 ) print("Looks ok!" ) if pytorch_dump_folder_path is not None: print(F"""Saving model and image processor to {pytorch_dump_folder_path}""" ) Path(_UpperCAmelCase ).mkdir(exist_ok=_UpperCAmelCase ) model.save_pretrained(_UpperCAmelCase ) image_processor.save_pretrained(_UpperCAmelCase ) if push_to_hub: print("Pushing model and image processor to the hub..." ) model.push_to_hub(F"""nielsr/{model_name}""" ) image_processor.push_to_hub(F"""nielsr/{model_name}""" ) if __name__ == "__main__": __SCREAMING_SNAKE_CASE : Optional[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( """--model_name""", default="""maskformer-swin-tiny-ade""", type=str, help=("""Name of the MaskFormer model you'd like to convert""",), ) parser.add_argument( """--checkpoint_path""", default="""/Users/nielsrogge/Documents/MaskFormer_checkpoints/MaskFormer-Swin-tiny-ADE20k/model.pkl""", type=str, help="""Path to the original state dict (.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.""" ) __SCREAMING_SNAKE_CASE : int = parser.parse_args() convert_maskformer_checkpoint( args.model_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub )
31
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_torch_available, is_vision_available, ) __SCREAMING_SNAKE_CASE : Tuple = {"""configuration_beit""": ["""BEIT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """BeitConfig""", """BeitOnnxConfig"""]} try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __SCREAMING_SNAKE_CASE : Tuple = ["""BeitFeatureExtractor"""] __SCREAMING_SNAKE_CASE : List[str] = ["""BeitImageProcessor"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __SCREAMING_SNAKE_CASE : str = [ """BEIT_PRETRAINED_MODEL_ARCHIVE_LIST""", """BeitForImageClassification""", """BeitForMaskedImageModeling""", """BeitForSemanticSegmentation""", """BeitModel""", """BeitPreTrainedModel""", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __SCREAMING_SNAKE_CASE : int = [ """FlaxBeitForImageClassification""", """FlaxBeitForMaskedImageModeling""", """FlaxBeitModel""", """FlaxBeitPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_beit import BEIT_PRETRAINED_CONFIG_ARCHIVE_MAP, BeitConfig, BeitOnnxConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_beit import BeitFeatureExtractor from .image_processing_beit import BeitImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_beit import ( BEIT_PRETRAINED_MODEL_ARCHIVE_LIST, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation, BeitModel, BeitPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_beit import ( FlaxBeitForImageClassification, FlaxBeitForMaskedImageModeling, FlaxBeitModel, FlaxBeitPreTrainedModel, ) else: import sys __SCREAMING_SNAKE_CASE : List[str] = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
31
'''simple docstring''' import bza import gzip import lzma import os import shutil import struct import tarfile import warnings import zipfile from abc import ABC, abstractmethod from pathlib import Path from typing import Dict, List, Optional, Type, Union from .. import config from .filelock import FileLock from .logging import get_logger __SCREAMING_SNAKE_CASE : Dict = get_logger(__name__) class lowerCamelCase_ : '''simple docstring''' def __init__( self : List[str] , A : Optional[str] = None ): _UpperCAmelCase : Dict = ( os.path.join(A , config.EXTRACTED_DATASETS_DIR ) if cache_dir else config.EXTRACTED_DATASETS_PATH ) _UpperCAmelCase : Union[str, Any] = Extractor def _A ( self : Tuple , A : str ): from .file_utils import hash_url_to_filename # Path where we extract compressed archives # We extract in the cache dir, and get the extracted path name by hashing the original path" _UpperCAmelCase : Dict = os.path.abspath(A ) return os.path.join(self.extract_dir , hash_url_to_filename(A ) ) def _A ( self : int , A : str , A : bool ): return force_extract or ( not os.path.isfile(A ) and not (os.path.isdir(A ) and os.listdir(A )) ) def _A ( self : Optional[int] , A : str , A : bool = False ): _UpperCAmelCase : Union[str, Any] = self.extractor.infer_extractor_format(A ) if not extractor_format: return input_path _UpperCAmelCase : Optional[Any] = self._get_output_path(A ) if self._do_extract(A , A ): self.extractor.extract(A , A , A ) return output_path class lowerCamelCase_ (snake_case__ ): '''simple docstring''' @classmethod @abstractmethod def _A ( cls : str , A : Union[Path, str] , **A : Dict ): ... @staticmethod @abstractmethod def _A ( A : Union[Path, str] , A : Union[Path, str] ): ... class lowerCamelCase_ (snake_case__ , snake_case__ ): '''simple docstring''' __UpperCamelCase: List[bytes] = [] @staticmethod def _A ( A : Union[Path, str] , A : int ): with open(A , "rb" ) as f: return f.read(A ) @classmethod def _A ( cls : Any , A : Union[Path, str] , A : bytes = b"" ): if not magic_number: _UpperCAmelCase : Any = max(len(A ) for cls_magic_number in cls.magic_numbers ) try: _UpperCAmelCase : int = cls.read_magic_number(A , A ) except OSError: return False return any(magic_number.startswith(A ) for cls_magic_number in cls.magic_numbers ) class lowerCamelCase_ (snake_case__ ): '''simple docstring''' @classmethod def _A ( cls : str , A : Union[Path, str] , **A : List[Any] ): return tarfile.is_tarfile(A ) @staticmethod def _A ( A : Union[str, Any] , A : str ): def resolved(A : str ) -> str: return os.path.realpath(os.path.abspath(A ) ) def badpath(A : str , A : str ) -> bool: # joinpath will ignore base if path is absolute return not resolved(os.path.join(A , A ) ).startswith(A ) def badlink(A : str , A : str ) -> bool: # Links are interpreted relative to the directory containing the link _UpperCAmelCase : List[str] = resolved(os.path.join(A , os.path.dirname(info.name ) ) ) return badpath(info.linkname , base=A ) _UpperCAmelCase : Optional[int] = resolved(A ) for finfo in members: if badpath(finfo.name , A ): logger.error(F"""Extraction of {finfo.name} is blocked (illegal path)""" ) elif finfo.issym() and badlink(A , A ): logger.error(F"""Extraction of {finfo.name} is blocked: Symlink to {finfo.linkname}""" ) elif finfo.islnk() and badlink(A , A ): logger.error(F"""Extraction of {finfo.name} is blocked: Hard link to {finfo.linkname}""" ) else: yield finfo @staticmethod def _A ( A : Union[Path, str] , A : Union[Path, str] ): os.makedirs(A , exist_ok=A ) _UpperCAmelCase : int = tarfile.open(A ) tar_file.extractall(A , members=TarExtractor.safemembers(A , A ) ) tar_file.close() class lowerCamelCase_ (snake_case__ ): '''simple docstring''' __UpperCamelCase: Union[str, Any] = [b"\x1F\x8B"] @staticmethod def _A ( A : Union[Path, str] , A : Union[Path, str] ): with gzip.open(A , "rb" ) as gzip_file: with open(A , "wb" ) as extracted_file: shutil.copyfileobj(A , A ) class lowerCamelCase_ (snake_case__ ): '''simple docstring''' __UpperCamelCase: Dict = [ b"PK\x03\x04", b"PK\x05\x06", # empty archive b"PK\x07\x08", # spanned archive ] @classmethod def _A ( cls : Dict , A : Union[Path, str] , A : bytes = b"" ): if super().is_extractable(A , magic_number=A ): return True try: # Alternative version of zipfile.is_zipfile that has less false positives, but misses executable zip archives. # From: https://github.com/python/cpython/pull/5053 from zipfile import ( _CD_SIGNATURE, _ECD_DISK_NUMBER, _ECD_DISK_START, _ECD_ENTRIES_TOTAL, _ECD_OFFSET, _ECD_SIZE, _EndRecData, sizeCentralDir, stringCentralDir, structCentralDir, ) with open(A , "rb" ) as fp: _UpperCAmelCase : Tuple = _EndRecData(A ) if endrec: if endrec[_ECD_ENTRIES_TOTAL] == 0 and endrec[_ECD_SIZE] == 0 and endrec[_ECD_OFFSET] == 0: return True # Empty zipfiles are still zipfiles elif endrec[_ECD_DISK_NUMBER] == endrec[_ECD_DISK_START]: fp.seek(endrec[_ECD_OFFSET] ) # Central directory is on the same disk if fp.tell() == endrec[_ECD_OFFSET] and endrec[_ECD_SIZE] >= sizeCentralDir: _UpperCAmelCase : Dict = fp.read(A ) # CD is where we expect it to be if len(A ) == sizeCentralDir: _UpperCAmelCase : Any = struct.unpack(A , A ) # CD is the right size if centdir[_CD_SIGNATURE] == stringCentralDir: return True # First central directory entry has correct magic number return False except Exception: # catch all errors in case future python versions change the zipfile internals return False @staticmethod def _A ( A : Union[Path, str] , A : Union[Path, str] ): os.makedirs(A , exist_ok=A ) with zipfile.ZipFile(A , "r" ) as zip_file: zip_file.extractall(A ) zip_file.close() class lowerCamelCase_ (snake_case__ ): '''simple docstring''' __UpperCamelCase: Dict = [b"\xFD\x37\x7A\x58\x5A\x00"] @staticmethod def _A ( A : Union[Path, str] , A : Union[Path, str] ): with lzma.open(A ) as compressed_file: with open(A , "wb" ) as extracted_file: shutil.copyfileobj(A , A ) class lowerCamelCase_ (snake_case__ ): '''simple docstring''' __UpperCamelCase: List[str] = [b"Rar!\x1a\x07\x00", b"Rar!\x1a\x07\x01\x00"] # RAR_ID # RAR5_ID @staticmethod def _A ( A : Union[Path, str] , A : Union[Path, str] ): if not config.RARFILE_AVAILABLE: raise ImportError("Please pip install rarfile" ) import rarfile os.makedirs(A , exist_ok=A ) _UpperCAmelCase : List[str] = rarfile.RarFile(A ) rf.extractall(A ) rf.close() class lowerCamelCase_ (snake_case__ ): '''simple docstring''' __UpperCamelCase: Optional[Any] = [b"\x28\xb5\x2F\xFD"] @staticmethod def _A ( A : Union[Path, str] , A : Union[Path, str] ): if not config.ZSTANDARD_AVAILABLE: raise ImportError("Please pip install zstandard" ) import zstandard as zstd _UpperCAmelCase : Optional[Any] = zstd.ZstdDecompressor() with open(A , "rb" ) as ifh, open(A , "wb" ) as ofh: dctx.copy_stream(A , A ) class lowerCamelCase_ (snake_case__ ): '''simple docstring''' __UpperCamelCase: Optional[Any] = [b"\x42\x5A\x68"] @staticmethod def _A ( A : Union[Path, str] , A : Union[Path, str] ): with bza.open(A , "rb" ) as compressed_file: with open(A , "wb" ) as extracted_file: shutil.copyfileobj(A , A ) class lowerCamelCase_ (snake_case__ ): '''simple docstring''' __UpperCamelCase: List[Any] = [b"\x37\x7A\xBC\xAF\x27\x1C"] @staticmethod def _A ( A : Union[Path, str] , A : Union[Path, str] ): if not config.PY7ZR_AVAILABLE: raise ImportError("Please pip install py7zr" ) import pyazr os.makedirs(A , exist_ok=A ) with pyazr.SevenZipFile(A , "r" ) as archive: archive.extractall(A ) class lowerCamelCase_ (snake_case__ ): '''simple docstring''' __UpperCamelCase: Optional[int] = [b"\x04\x22\x4D\x18"] @staticmethod def _A ( A : Union[Path, str] , A : Union[Path, str] ): if not config.LZ4_AVAILABLE: raise ImportError("Please pip install lz4" ) import lza.frame with lza.frame.open(A , "rb" ) as compressed_file: with open(A , "wb" ) as extracted_file: shutil.copyfileobj(A , A ) class lowerCamelCase_ : '''simple docstring''' __UpperCamelCase: Dict[str, Type[BaseExtractor]] = { "tar": TarExtractor, "gzip": GzipExtractor, "zip": ZipExtractor, "xz": XzExtractor, "rar": RarExtractor, "zstd": ZstdExtractor, "bz2": BzipaExtractor, "7z": SevenZipExtractor, # <Added version="2.4.0"/> "lz4": LzaExtractor, # <Added version="2.4.0"/> } @classmethod def _A ( cls : List[Any] ): return max( len(A ) for extractor in cls.extractors.values() if issubclass(A , A ) for extractor_magic_number in extractor.magic_numbers ) @staticmethod def _A ( A : Union[Path, str] , A : int ): try: return MagicNumberBaseExtractor.read_magic_number(A , magic_number_length=A ) except OSError: return b"" @classmethod def _A ( cls : Optional[Any] , A : Union[Path, str] , A : bool = False ): warnings.warn( "Method 'is_extractable' was deprecated in version 2.4.0 and will be removed in 3.0.0. " "Use 'infer_extractor_format' instead." , category=A , ) _UpperCAmelCase : Union[str, Any] = cls.infer_extractor_format(A ) if extractor_format: return True if not return_extractor else (True, cls.extractors[extractor_format]) return False if not return_extractor else (False, None) @classmethod def _A ( cls : Dict , A : Union[Path, str] ): # <Added version="2.4.0"/> _UpperCAmelCase : Optional[int] = cls._get_magic_number_max_length() _UpperCAmelCase : str = cls._read_magic_number(A , A ) for extractor_format, extractor in cls.extractors.items(): if extractor.is_extractable(A , magic_number=A ): return extractor_format @classmethod def _A ( cls : List[str] , A : Union[Path, str] , A : Union[Path, str] , A : Optional[str] = None , A : Optional[BaseExtractor] = "deprecated" , ): os.makedirs(os.path.dirname(A ) , exist_ok=A ) # Prevent parallel extractions _UpperCAmelCase : Tuple = str(Path(A ).with_suffix(".lock" ) ) with FileLock(A ): shutil.rmtree(A , ignore_errors=A ) if extractor_format or extractor != "deprecated": if extractor != "deprecated" or not isinstance(A , A ): # passed as positional arg warnings.warn( "Parameter 'extractor' was deprecated in version 2.4.0 and will be removed in 3.0.0. " "Use 'extractor_format' instead." , category=A , ) _UpperCAmelCase : Tuple = extractor if extractor != "deprecated" else extractor_format else: _UpperCAmelCase : Tuple = cls.extractors[extractor_format] return extractor.extract(A , A ) else: warnings.warn( "Parameter 'extractor_format' was made required in version 2.4.0 and not passing it will raise an " "exception in 3.0.0." , category=A , ) for extractor in cls.extractors.values(): if extractor.is_extractable(A ): return extractor.extract(A , A )
31
1
'''simple docstring''' import torch from transformers import CamembertForMaskedLM, CamembertTokenizer def UpperCamelCase_ ( _UpperCAmelCase : List[Any] , _UpperCAmelCase : List[Any] , _UpperCAmelCase : int , _UpperCAmelCase : str=5 ) -> int: """simple docstring""" assert masked_input.count("<mask>" ) == 1 _UpperCAmelCase : Tuple = torch.tensor(tokenizer.encode(_UpperCAmelCase , add_special_tokens=_UpperCAmelCase ) ).unsqueeze(0 ) # Batch size 1 _UpperCAmelCase : Union[str, Any] = model(_UpperCAmelCase )[0] # The last hidden-state is the first element of the output tuple _UpperCAmelCase : List[str] = (input_ids.squeeze() == tokenizer.mask_token_id).nonzero().item() _UpperCAmelCase : Dict = logits[0, masked_index, :] _UpperCAmelCase : Optional[Any] = logits.softmax(dim=0 ) _UpperCAmelCase , _UpperCAmelCase : int = prob.topk(k=_UpperCAmelCase , dim=0 ) _UpperCAmelCase : Any = " ".join( [tokenizer.convert_ids_to_tokens(indices[i].item() ) for i in range(len(_UpperCAmelCase ) )] ) _UpperCAmelCase : str = tokenizer.mask_token _UpperCAmelCase : int = [] for index, predicted_token_bpe in enumerate(topk_predicted_token_bpe.split(" " ) ): _UpperCAmelCase : str = predicted_token_bpe.replace("\u2581" , " " ) if " {0}".format(_UpperCAmelCase ) in masked_input: topk_filled_outputs.append( ( masked_input.replace(" {0}".format(_UpperCAmelCase ) , _UpperCAmelCase ), values[index].item(), predicted_token, ) ) else: topk_filled_outputs.append( ( masked_input.replace(_UpperCAmelCase , _UpperCAmelCase ), values[index].item(), predicted_token, ) ) return topk_filled_outputs __SCREAMING_SNAKE_CASE : List[Any] = CamembertTokenizer.from_pretrained("""camembert-base""") __SCREAMING_SNAKE_CASE : Union[str, Any] = CamembertForMaskedLM.from_pretrained("""camembert-base""") model.eval() __SCREAMING_SNAKE_CASE : Union[str, Any] = """Le camembert est <mask> :)""" print(fill_mask(masked_input, model, tokenizer, topk=3))
31
'''simple docstring''' from typing import Any def UpperCamelCase_ ( _UpperCAmelCase : list , _UpperCAmelCase : list , _UpperCAmelCase : dict , _UpperCAmelCase : dict , _UpperCAmelCase : dict , ) -> list: """simple docstring""" _validation( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , ) # Creates data structures and fill initial step _UpperCAmelCase : dict = {} _UpperCAmelCase : dict = {} for state in states_space: _UpperCAmelCase : Union[str, Any] = observations_space[0] _UpperCAmelCase : Tuple = ( initial_probabilities[state] * emission_probabilities[state][observation] ) _UpperCAmelCase : List[str] = None # Fills the data structure with the probabilities of # different transitions and pointers to previous states for o in range(1 , len(_UpperCAmelCase ) ): _UpperCAmelCase : Optional[Any] = observations_space[o] _UpperCAmelCase : int = observations_space[o - 1] for state in states_space: # Calculates the argmax for probability function _UpperCAmelCase : str = "" _UpperCAmelCase : Tuple = -1 for k_state in states_space: _UpperCAmelCase : Any = ( probabilities[(k_state, prior_observation)] * transition_probabilities[k_state][state] * emission_probabilities[state][observation] ) if probability > max_probability: _UpperCAmelCase : Union[str, Any] = probability _UpperCAmelCase : str = k_state # Update probabilities and pointers dicts _UpperCAmelCase : Optional[int] = ( probabilities[(arg_max, prior_observation)] * transition_probabilities[arg_max][state] * emission_probabilities[state][observation] ) _UpperCAmelCase : Tuple = arg_max # The final observation _UpperCAmelCase : Optional[Any] = observations_space[len(_UpperCAmelCase ) - 1] # argmax for given final observation _UpperCAmelCase : List[str] = "" _UpperCAmelCase : Any = -1 for k_state in states_space: _UpperCAmelCase : Optional[int] = probabilities[(k_state, final_observation)] if probability > max_probability: _UpperCAmelCase : int = probability _UpperCAmelCase : Dict = k_state _UpperCAmelCase : Dict = arg_max # Process pointers backwards _UpperCAmelCase : List[Any] = last_state _UpperCAmelCase : str = [] for o in range(len(_UpperCAmelCase ) - 1 , -1 , -1 ): result.append(_UpperCAmelCase ) _UpperCAmelCase : List[Any] = pointers[previous, observations_space[o]] result.reverse() return result def UpperCamelCase_ ( _UpperCAmelCase : Any , _UpperCAmelCase : Any , _UpperCAmelCase : Any , _UpperCAmelCase : Any , _UpperCAmelCase : Any , ) -> None: """simple docstring""" _validate_not_empty( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , ) _validate_lists(_UpperCAmelCase , _UpperCAmelCase ) _validate_dicts( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) def UpperCamelCase_ ( _UpperCAmelCase : Any , _UpperCAmelCase : Any , _UpperCAmelCase : Any , _UpperCAmelCase : Any , _UpperCAmelCase : Any , ) -> None: """simple docstring""" if not all( [ observations_space, states_space, initial_probabilities, transition_probabilities, emission_probabilities, ] ): raise ValueError("There's an empty parameter" ) def UpperCamelCase_ ( _UpperCAmelCase : Any , _UpperCAmelCase : Any ) -> None: """simple docstring""" _validate_list(_UpperCAmelCase , "observations_space" ) _validate_list(_UpperCAmelCase , "states_space" ) def UpperCamelCase_ ( _UpperCAmelCase : Any , _UpperCAmelCase : str ) -> None: """simple docstring""" if not isinstance(_object , _UpperCAmelCase ): _UpperCAmelCase : Optional[int] = F"""{var_name} must be a list""" raise ValueError(_UpperCAmelCase ) else: for x in _object: if not isinstance(_UpperCAmelCase , _UpperCAmelCase ): _UpperCAmelCase : Optional[int] = F"""{var_name} must be a list of strings""" raise ValueError(_UpperCAmelCase ) def UpperCamelCase_ ( _UpperCAmelCase : Any , _UpperCAmelCase : Any , _UpperCAmelCase : Any , ) -> None: """simple docstring""" _validate_dict(_UpperCAmelCase , "initial_probabilities" , _UpperCAmelCase ) _validate_nested_dict(_UpperCAmelCase , "transition_probabilities" ) _validate_nested_dict(_UpperCAmelCase , "emission_probabilities" ) def UpperCamelCase_ ( _UpperCAmelCase : Any , _UpperCAmelCase : str ) -> None: """simple docstring""" _validate_dict(_object , _UpperCAmelCase , _UpperCAmelCase ) for x in _object.values(): _validate_dict(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) def UpperCamelCase_ ( _UpperCAmelCase : Any , _UpperCAmelCase : str , _UpperCAmelCase : type , _UpperCAmelCase : bool = False ) -> None: """simple docstring""" if not isinstance(_object , _UpperCAmelCase ): _UpperCAmelCase : Any = F"""{var_name} must be a dict""" raise ValueError(_UpperCAmelCase ) if not all(isinstance(_UpperCAmelCase , _UpperCAmelCase ) for x in _object ): _UpperCAmelCase : Tuple = F"""{var_name} all keys must be strings""" raise ValueError(_UpperCAmelCase ) if not all(isinstance(_UpperCAmelCase , _UpperCAmelCase ) for x in _object.values() ): _UpperCAmelCase : List[str] = "nested dictionary " if nested else "" _UpperCAmelCase : List[str] = F"""{var_name} {nested_text}all values must be {value_type.__name__}""" raise ValueError(_UpperCAmelCase ) if __name__ == "__main__": from doctest import testmod testmod()
31
1
'''simple docstring''' from typing import TYPE_CHECKING from ...file_utils import _LazyModule, is_tokenizers_available, is_torch_available from ...utils import OptionalDependencyNotAvailable __SCREAMING_SNAKE_CASE : Optional[int] = {"""configuration_gpt_neox""": ["""GPT_NEOX_PRETRAINED_CONFIG_ARCHIVE_MAP""", """GPTNeoXConfig"""]} try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __SCREAMING_SNAKE_CASE : Tuple = ["""GPTNeoXTokenizerFast"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __SCREAMING_SNAKE_CASE : Dict = [ """GPT_NEOX_PRETRAINED_MODEL_ARCHIVE_LIST""", """GPTNeoXForCausalLM""", """GPTNeoXForQuestionAnswering""", """GPTNeoXForSequenceClassification""", """GPTNeoXForTokenClassification""", """GPTNeoXLayer""", """GPTNeoXModel""", """GPTNeoXPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_gpt_neox import GPT_NEOX_PRETRAINED_CONFIG_ARCHIVE_MAP, GPTNeoXConfig try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_gpt_neox_fast import GPTNeoXTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_gpt_neox import ( GPT_NEOX_PRETRAINED_MODEL_ARCHIVE_LIST, GPTNeoXForCausalLM, GPTNeoXForQuestionAnswering, GPTNeoXForSequenceClassification, GPTNeoXForTokenClassification, GPTNeoXLayer, GPTNeoXModel, GPTNeoXPreTrainedModel, ) else: import sys __SCREAMING_SNAKE_CASE : Any = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
31
'''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 MobileViTImageProcessor class lowerCamelCase_ (unittest.TestCase ): '''simple docstring''' def __init__( self : Optional[Any] , A : Dict , A : Dict=7 , A : Optional[int]=3 , A : Optional[int]=18 , A : Dict=30 , A : List[Any]=400 , A : Union[str, Any]=True , A : Tuple=None , A : List[Any]=True , A : int=None , A : Optional[int]=True , ): _UpperCAmelCase : Optional[int] = size if size is not None else {"shortest_edge": 20} _UpperCAmelCase : Optional[Any] = crop_size if crop_size is not None else {"height": 18, "width": 18} _UpperCAmelCase : List[Any] = parent _UpperCAmelCase : Union[str, Any] = batch_size _UpperCAmelCase : Optional[Any] = num_channels _UpperCAmelCase : Union[str, Any] = image_size _UpperCAmelCase : int = min_resolution _UpperCAmelCase : Optional[int] = max_resolution _UpperCAmelCase : List[str] = do_resize _UpperCAmelCase : Optional[Any] = size _UpperCAmelCase : Tuple = do_center_crop _UpperCAmelCase : Optional[int] = crop_size _UpperCAmelCase : Optional[Any] = do_flip_channel_order def _A ( self : Dict ): return { "do_resize": self.do_resize, "size": self.size, "do_center_crop": self.do_center_crop, "crop_size": self.crop_size, "do_flip_channel_order": self.do_flip_channel_order, } @require_torch @require_vision class lowerCamelCase_ (snake_case__ , unittest.TestCase ): '''simple docstring''' __UpperCamelCase: Tuple = MobileViTImageProcessor if is_vision_available() else None def _A ( self : List[Any] ): _UpperCAmelCase : Any = MobileViTImageProcessingTester(self ) @property def _A ( self : int ): return self.image_processor_tester.prepare_image_processor_dict() def _A ( self : Tuple ): _UpperCAmelCase : int = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(A , "do_resize" ) ) self.assertTrue(hasattr(A , "size" ) ) self.assertTrue(hasattr(A , "do_center_crop" ) ) self.assertTrue(hasattr(A , "center_crop" ) ) self.assertTrue(hasattr(A , "do_flip_channel_order" ) ) def _A ( self : Any ): _UpperCAmelCase : List[str] = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"shortest_edge": 20} ) self.assertEqual(image_processor.crop_size , {"height": 18, "width": 18} ) _UpperCAmelCase : Dict = self.image_processing_class.from_dict(self.image_processor_dict , size=42 , crop_size=84 ) self.assertEqual(image_processor.size , {"shortest_edge": 42} ) self.assertEqual(image_processor.crop_size , {"height": 84, "width": 84} ) def _A ( self : Any ): pass def _A ( self : Dict ): # Initialize image_processing _UpperCAmelCase : List[str] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images _UpperCAmelCase : Union[str, Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=A ) for image in image_inputs: self.assertIsInstance(A , Image.Image ) # Test not batched input _UpperCAmelCase : List[Any] = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) # Test batched _UpperCAmelCase : Optional[Any] = image_processing(A , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) def _A ( self : Union[str, Any] ): # Initialize image_processing _UpperCAmelCase : Dict = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors _UpperCAmelCase : Optional[Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=A , numpify=A ) for image in image_inputs: self.assertIsInstance(A , np.ndarray ) # Test not batched input _UpperCAmelCase : 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.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) # Test batched _UpperCAmelCase : Optional[int] = image_processing(A , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) def _A ( self : Any ): # Initialize image_processing _UpperCAmelCase : Any = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors _UpperCAmelCase : Optional[Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=A , torchify=A ) for image in image_inputs: self.assertIsInstance(A , torch.Tensor ) # Test not batched input _UpperCAmelCase : 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.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) # Test batched _UpperCAmelCase : Any = image_processing(A , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , )
31
1
'''simple docstring''' from typing import List, Optional, TypeVar from .arrow_dataset import Dataset, _concatenate_map_style_datasets, _interleave_map_style_datasets from .dataset_dict import DatasetDict, IterableDatasetDict from .info import DatasetInfo from .iterable_dataset import IterableDataset, _concatenate_iterable_datasets, _interleave_iterable_datasets from .splits import NamedSplit from .utils import logging from .utils.py_utils import Literal __SCREAMING_SNAKE_CASE : Union[str, Any] = logging.get_logger(__name__) __SCREAMING_SNAKE_CASE : Tuple = TypeVar("""DatasetType""", Dataset, IterableDataset) def UpperCamelCase_ ( _UpperCAmelCase : List[DatasetType] , _UpperCAmelCase : Optional[List[float]] = None , _UpperCAmelCase : Optional[int] = None , _UpperCAmelCase : Optional[DatasetInfo] = None , _UpperCAmelCase : Optional[NamedSplit] = None , _UpperCAmelCase : Literal["first_exhausted", "all_exhausted"] = "first_exhausted" , ) -> DatasetType: """simple docstring""" from .arrow_dataset import Dataset from .iterable_dataset import IterableDataset if not datasets: raise ValueError("Unable to interleave an empty list of datasets." ) for i, dataset in enumerate(_UpperCAmelCase ): if not isinstance(_UpperCAmelCase , (Dataset, IterableDataset) ): if isinstance(_UpperCAmelCase , (DatasetDict, IterableDatasetDict) ): if not dataset: raise ValueError( F"""Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} """ "is an empty dataset dictionary." ) raise ValueError( F"""Dataset at position {i} has at least one split: {list(_UpperCAmelCase )}\n""" F"""Please pick one to interleave with the other datasets, for example: dataset['{next(iter(_UpperCAmelCase ) )}']""" ) raise ValueError( F"""Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} is a {type(_UpperCAmelCase ).__name__}.""" ) if i == 0: _UpperCAmelCase , _UpperCAmelCase : Dict = ( (Dataset, IterableDataset) if isinstance(_UpperCAmelCase , _UpperCAmelCase ) else (IterableDataset, Dataset) ) elif not isinstance(_UpperCAmelCase , _UpperCAmelCase ): raise ValueError( F"""Unable to interleave a {dataset_type.__name__} (at position 0) with a {other_type.__name__} (at position {i}). Expected a list of Dataset objects or a list of IterableDataset objects.""" ) if stopping_strategy not in ["first_exhausted", "all_exhausted"]: raise ValueError(F"""{stopping_strategy} is not supported. Please enter a valid stopping_strategy.""" ) if dataset_type is Dataset: return _interleave_map_style_datasets( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , info=_UpperCAmelCase , split=_UpperCAmelCase , stopping_strategy=_UpperCAmelCase ) else: return _interleave_iterable_datasets( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , info=_UpperCAmelCase , split=_UpperCAmelCase , stopping_strategy=_UpperCAmelCase ) def UpperCamelCase_ ( _UpperCAmelCase : List[DatasetType] , _UpperCAmelCase : Optional[DatasetInfo] = None , _UpperCAmelCase : Optional[NamedSplit] = None , _UpperCAmelCase : int = 0 , ) -> DatasetType: """simple docstring""" if not dsets: raise ValueError("Unable to concatenate an empty list of datasets." ) for i, dataset in enumerate(_UpperCAmelCase ): if not isinstance(_UpperCAmelCase , (Dataset, IterableDataset) ): if isinstance(_UpperCAmelCase , (DatasetDict, IterableDatasetDict) ): if not dataset: raise ValueError( F"""Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} """ "is an empty dataset dictionary." ) raise ValueError( F"""Dataset at position {i} has at least one split: {list(_UpperCAmelCase )}\n""" F"""Please pick one to interleave with the other datasets, for example: dataset['{next(iter(_UpperCAmelCase ) )}']""" ) raise ValueError( F"""Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} is a {type(_UpperCAmelCase ).__name__}.""" ) if i == 0: _UpperCAmelCase , _UpperCAmelCase : Dict = ( (Dataset, IterableDataset) if isinstance(_UpperCAmelCase , _UpperCAmelCase ) else (IterableDataset, Dataset) ) elif not isinstance(_UpperCAmelCase , _UpperCAmelCase ): raise ValueError( F"""Unable to interleave a {dataset_type.__name__} (at position 0) with a {other_type.__name__} (at position {i}). Expected a list of Dataset objects or a list of IterableDataset objects.""" ) if dataset_type is Dataset: return _concatenate_map_style_datasets(_UpperCAmelCase , info=_UpperCAmelCase , split=_UpperCAmelCase , axis=_UpperCAmelCase ) else: return _concatenate_iterable_datasets(_UpperCAmelCase , info=_UpperCAmelCase , split=_UpperCAmelCase , axis=_UpperCAmelCase )
31
'''simple docstring''' def UpperCamelCase_ ( _UpperCAmelCase : int , _UpperCAmelCase : int ) -> int: """simple docstring""" _UpperCAmelCase : List[str] = 1 # To kept the Calculated Value # Since C(n, k) = C(n, n-k) if k > (n - k): _UpperCAmelCase : Any = n - k # Calculate C(n,k) for i in range(_UpperCAmelCase ): result *= n - i result //= i + 1 return result def UpperCamelCase_ ( _UpperCAmelCase : int ) -> int: """simple docstring""" return binomial_coefficient(2 * node_count , _UpperCAmelCase ) // (node_count + 1) def UpperCamelCase_ ( _UpperCAmelCase : int ) -> int: """simple docstring""" if n < 0: raise ValueError("factorial() not defined for negative values" ) _UpperCAmelCase : List[str] = 1 for i in range(1 , n + 1 ): result *= i return result def UpperCamelCase_ ( _UpperCAmelCase : int ) -> int: """simple docstring""" return catalan_number(_UpperCAmelCase ) * factorial(_UpperCAmelCase ) if __name__ == "__main__": __SCREAMING_SNAKE_CASE : Dict = int(input("""Enter the number of nodes: """).strip() or 0) if node_count <= 0: raise ValueError("""We need some nodes to work with.""") print( F'Given {node_count} nodes, there are {binary_tree_count(node_count)} ' F'binary trees and {catalan_number(node_count)} binary search trees.' )
31
1
'''simple docstring''' def UpperCamelCase_ ( ) -> str: """simple docstring""" _UpperCAmelCase : Dict = 0 for i in range(1 , 1_001 ): total += i**i return str(_UpperCAmelCase )[-10:] if __name__ == "__main__": print(solution())
31
'''simple docstring''' import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_distilbert import DistilBertTokenizer __SCREAMING_SNAKE_CASE : str = logging.get_logger(__name__) __SCREAMING_SNAKE_CASE : str = {"""vocab_file""": """vocab.txt""", """tokenizer_file""": """tokenizer.json"""} __SCREAMING_SNAKE_CASE : Dict = { """vocab_file""": { """distilbert-base-uncased""": """https://huggingface.co/distilbert-base-uncased/resolve/main/vocab.txt""", """distilbert-base-uncased-distilled-squad""": ( """https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/vocab.txt""" ), """distilbert-base-cased""": """https://huggingface.co/distilbert-base-cased/resolve/main/vocab.txt""", """distilbert-base-cased-distilled-squad""": ( """https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/vocab.txt""" ), """distilbert-base-german-cased""": """https://huggingface.co/distilbert-base-german-cased/resolve/main/vocab.txt""", """distilbert-base-multilingual-cased""": ( """https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/vocab.txt""" ), }, """tokenizer_file""": { """distilbert-base-uncased""": """https://huggingface.co/distilbert-base-uncased/resolve/main/tokenizer.json""", """distilbert-base-uncased-distilled-squad""": ( """https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/tokenizer.json""" ), """distilbert-base-cased""": """https://huggingface.co/distilbert-base-cased/resolve/main/tokenizer.json""", """distilbert-base-cased-distilled-squad""": ( """https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/tokenizer.json""" ), """distilbert-base-german-cased""": ( """https://huggingface.co/distilbert-base-german-cased/resolve/main/tokenizer.json""" ), """distilbert-base-multilingual-cased""": ( """https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/tokenizer.json""" ), }, } __SCREAMING_SNAKE_CASE : Optional[Any] = { """distilbert-base-uncased""": 512, """distilbert-base-uncased-distilled-squad""": 512, """distilbert-base-cased""": 512, """distilbert-base-cased-distilled-squad""": 512, """distilbert-base-german-cased""": 512, """distilbert-base-multilingual-cased""": 512, } __SCREAMING_SNAKE_CASE : List[Any] = { """distilbert-base-uncased""": {"""do_lower_case""": True}, """distilbert-base-uncased-distilled-squad""": {"""do_lower_case""": True}, """distilbert-base-cased""": {"""do_lower_case""": False}, """distilbert-base-cased-distilled-squad""": {"""do_lower_case""": False}, """distilbert-base-german-cased""": {"""do_lower_case""": False}, """distilbert-base-multilingual-cased""": {"""do_lower_case""": False}, } class lowerCamelCase_ (snake_case__ ): '''simple docstring''' __UpperCamelCase: Union[str, Any] = VOCAB_FILES_NAMES __UpperCamelCase: str = PRETRAINED_VOCAB_FILES_MAP __UpperCamelCase: Optional[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCamelCase: Any = PRETRAINED_INIT_CONFIGURATION __UpperCamelCase: str = ["input_ids", "attention_mask"] __UpperCamelCase: List[str] = DistilBertTokenizer def __init__( self : str , A : int=None , A : Tuple=None , A : Tuple=True , A : Dict="[UNK]" , A : List[Any]="[SEP]" , A : Optional[Any]="[PAD]" , A : Dict="[CLS]" , A : Tuple="[MASK]" , A : str=True , A : Dict=None , **A : List[Any] , ): super().__init__( A , tokenizer_file=A , do_lower_case=A , unk_token=A , sep_token=A , pad_token=A , cls_token=A , mask_token=A , tokenize_chinese_chars=A , strip_accents=A , **A , ) _UpperCAmelCase : str = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get("lowercase" , A ) != do_lower_case or normalizer_state.get("strip_accents" , A ) != strip_accents or normalizer_state.get("handle_chinese_chars" , A ) != tokenize_chinese_chars ): _UpperCAmelCase : Dict = getattr(A , normalizer_state.pop("type" ) ) _UpperCAmelCase : int = do_lower_case _UpperCAmelCase : Optional[int] = strip_accents _UpperCAmelCase : str = tokenize_chinese_chars _UpperCAmelCase : List[Any] = normalizer_class(**A ) _UpperCAmelCase : Dict = do_lower_case def _A ( self : List[Any] , A : Tuple , A : Any=None ): _UpperCAmelCase : Optional[int] = [self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def _A ( self : int , A : List[int] , A : Optional[List[int]] = None ): _UpperCAmelCase : Any = [self.sep_token_id] _UpperCAmelCase : str = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def _A ( self : Dict , A : str , A : Optional[str] = None ): _UpperCAmelCase : Any = self._tokenizer.model.save(A , name=A ) return tuple(A )
31
1
'''simple docstring''' from typing import List, Optional from ...configuration_utils import PretrainedConfig from ...utils import logging __SCREAMING_SNAKE_CASE : int = logging.get_logger(__name__) __SCREAMING_SNAKE_CASE : int = { """huggingface/autoformer-tourism-monthly""": """https://huggingface.co/huggingface/autoformer-tourism-monthly/resolve/main/config.json""", } class lowerCamelCase_ (snake_case__ ): '''simple docstring''' __UpperCamelCase: Optional[int] = "autoformer" __UpperCamelCase: List[str] = { "hidden_size": "d_model", "num_attention_heads": "encoder_attention_heads", "num_hidden_layers": "encoder_layers", } def __init__( self : List[str] , A : Optional[int] = None , A : Optional[int] = None , A : str = "student_t" , A : str = "nll" , A : int = 1 , A : List[int] = [1, 2, 3, 4, 5, 6, 7] , A : bool = True , A : int = 0 , A : int = 0 , A : int = 0 , A : int = 0 , A : Optional[List[int]] = None , A : Optional[List[int]] = None , A : int = 64 , A : int = 2 , A : int = 2 , A : int = 2 , A : int = 2 , A : int = 32 , A : int = 32 , A : str = "gelu" , A : float = 0.1 , A : float = 0.1 , A : float = 0.1 , A : float = 0.1 , A : float = 0.1 , A : int = 100 , A : float = 0.02 , A : bool = True , A : int=True , A : int = 10 , A : int = 25 , A : int = 3 , **A : Tuple , ): # time series specific configuration _UpperCAmelCase : Optional[int] = prediction_length _UpperCAmelCase : Any = context_length if context_length is not None else prediction_length _UpperCAmelCase : Tuple = distribution_output _UpperCAmelCase : Union[str, Any] = loss _UpperCAmelCase : Union[str, Any] = input_size _UpperCAmelCase : Optional[int] = num_time_features _UpperCAmelCase : List[Any] = lags_sequence _UpperCAmelCase : Optional[Any] = scaling _UpperCAmelCase : Any = num_dynamic_real_features _UpperCAmelCase : Any = num_static_real_features _UpperCAmelCase : Optional[int] = num_static_categorical_features if cardinality is not None and num_static_categorical_features > 0: if len(A ) != num_static_categorical_features: raise ValueError( "The cardinality should be a list of the same length as `num_static_categorical_features`" ) _UpperCAmelCase : Optional[Any] = cardinality else: _UpperCAmelCase : List[str] = [0] if embedding_dimension is not None and num_static_categorical_features > 0: if len(A ) != num_static_categorical_features: raise ValueError( "The embedding dimension should be a list of the same length as `num_static_categorical_features`" ) _UpperCAmelCase : Any = embedding_dimension else: _UpperCAmelCase : List[str] = [min(50 , (cat + 1) // 2 ) for cat in self.cardinality] _UpperCAmelCase : Any = num_parallel_samples # Transformer architecture configuration _UpperCAmelCase : List[str] = input_size * len(self.lags_sequence ) + self._number_of_features _UpperCAmelCase : Any = d_model _UpperCAmelCase : Union[str, Any] = encoder_attention_heads _UpperCAmelCase : Tuple = decoder_attention_heads _UpperCAmelCase : str = encoder_ffn_dim _UpperCAmelCase : Optional[Any] = decoder_ffn_dim _UpperCAmelCase : Dict = encoder_layers _UpperCAmelCase : Dict = decoder_layers _UpperCAmelCase : Dict = dropout _UpperCAmelCase : Dict = attention_dropout _UpperCAmelCase : Tuple = activation_dropout _UpperCAmelCase : Optional[int] = encoder_layerdrop _UpperCAmelCase : Optional[int] = decoder_layerdrop _UpperCAmelCase : Dict = activation_function _UpperCAmelCase : Tuple = init_std _UpperCAmelCase : List[str] = use_cache # Autoformer _UpperCAmelCase : int = label_length _UpperCAmelCase : List[Any] = moving_average _UpperCAmelCase : Union[str, Any] = autocorrelation_factor super().__init__(is_encoder_decoder=A , **A ) @property def _A ( self : Optional[int] ): return ( sum(self.embedding_dimension ) + self.num_dynamic_real_features + self.num_time_features + self.num_static_real_features + self.input_size * 2 # the log1p(abs(loc)) and log(scale) features )
31
'''simple docstring''' import shutil import tempfile import unittest from unittest.mock import patch from transformers import ( DefaultFlowCallback, IntervalStrategy, PrinterCallback, ProgressCallback, Trainer, TrainerCallback, TrainingArguments, is_torch_available, ) from transformers.testing_utils import require_torch if is_torch_available(): from transformers.trainer import DEFAULT_CALLBACKS from .test_trainer import RegressionDataset, RegressionModelConfig, RegressionPreTrainedModel class lowerCamelCase_ (snake_case__ ): '''simple docstring''' def __init__( self : List[Any] ): _UpperCAmelCase : Union[str, Any] = [] def _A ( self : Any , A : Union[str, Any] , A : Optional[int] , A : List[str] , **A : Tuple ): self.events.append("on_init_end" ) def _A ( self : Any , A : str , A : List[Any] , A : List[Any] , **A : Tuple ): self.events.append("on_train_begin" ) def _A ( self : Tuple , A : List[str] , A : Tuple , A : int , **A : List[str] ): self.events.append("on_train_end" ) def _A ( self : Optional[Any] , A : Dict , A : Any , A : Optional[Any] , **A : List[Any] ): self.events.append("on_epoch_begin" ) def _A ( self : Optional[Any] , A : List[Any] , A : List[str] , A : Optional[int] , **A : Optional[int] ): self.events.append("on_epoch_end" ) def _A ( self : List[str] , A : Optional[int] , A : List[Any] , A : Union[str, Any] , **A : Any ): self.events.append("on_step_begin" ) def _A ( self : Tuple , A : Union[str, Any] , A : int , A : Optional[int] , **A : int ): self.events.append("on_step_end" ) def _A ( self : Optional[int] , A : Optional[Any] , A : Union[str, Any] , A : str , **A : Union[str, Any] ): self.events.append("on_evaluate" ) def _A ( self : Optional[Any] , A : Optional[int] , A : Dict , A : List[Any] , **A : Dict ): self.events.append("on_predict" ) def _A ( self : Dict , A : Dict , A : List[Any] , A : Dict , **A : str ): self.events.append("on_save" ) def _A ( self : Tuple , A : Optional[Any] , A : Union[str, Any] , A : Optional[int] , **A : Dict ): self.events.append("on_log" ) def _A ( self : Optional[int] , A : Optional[Any] , A : Tuple , A : Tuple , **A : List[str] ): self.events.append("on_prediction_step" ) @require_torch class lowerCamelCase_ (unittest.TestCase ): '''simple docstring''' def _A ( self : Optional[int] ): _UpperCAmelCase : Optional[Any] = tempfile.mkdtemp() def _A ( self : List[Any] ): shutil.rmtree(self.output_dir ) def _A ( self : Union[str, Any] , A : Optional[int]=0 , A : Optional[Any]=0 , A : Optional[Any]=64 , A : Dict=64 , A : Any=None , A : Tuple=False , **A : Optional[int] ): # disable_tqdm in TrainingArguments has a flaky default since it depends on the level of logging. We make sure # its set to False since the tests later on depend on its value. _UpperCAmelCase : str = RegressionDataset(length=A ) _UpperCAmelCase : Union[str, Any] = RegressionDataset(length=A ) _UpperCAmelCase : Any = RegressionModelConfig(a=A , b=A ) _UpperCAmelCase : List[Any] = RegressionPreTrainedModel(A ) _UpperCAmelCase : Dict = TrainingArguments(self.output_dir , disable_tqdm=A , report_to=[] , **A ) return Trainer( A , A , train_dataset=A , eval_dataset=A , callbacks=A , ) def _A ( self : str , A : List[str] , A : List[str] ): self.assertEqual(len(A ) , len(A ) ) # Order doesn't matter _UpperCAmelCase : Tuple = sorted(A , key=lambda A : cb.__name__ if isinstance(A , A ) else cb.__class__.__name__ ) _UpperCAmelCase : Any = sorted(A , key=lambda A : cb.__name__ if isinstance(A , A ) else cb.__class__.__name__ ) for cba, cba in zip(A , A ): if isinstance(A , A ) and isinstance(A , A ): self.assertEqual(A , A ) elif isinstance(A , A ) and not isinstance(A , A ): self.assertEqual(A , cba.__class__ ) elif not isinstance(A , A ) and isinstance(A , A ): self.assertEqual(cba.__class__ , A ) else: self.assertEqual(A , A ) def _A ( self : int , A : List[str] ): _UpperCAmelCase : List[str] = ["on_init_end", "on_train_begin"] _UpperCAmelCase : str = 0 _UpperCAmelCase : Optional[Any] = len(trainer.get_eval_dataloader() ) _UpperCAmelCase : Optional[int] = ["on_prediction_step"] * len(trainer.get_eval_dataloader() ) + ["on_log", "on_evaluate"] for _ in range(trainer.state.num_train_epochs ): expected_events.append("on_epoch_begin" ) for _ in range(A ): step += 1 expected_events += ["on_step_begin", "on_step_end"] if step % trainer.args.logging_steps == 0: expected_events.append("on_log" ) if trainer.args.evaluation_strategy == IntervalStrategy.STEPS and step % trainer.args.eval_steps == 0: expected_events += evaluation_events.copy() if step % trainer.args.save_steps == 0: expected_events.append("on_save" ) expected_events.append("on_epoch_end" ) if trainer.args.evaluation_strategy == IntervalStrategy.EPOCH: expected_events += evaluation_events.copy() expected_events += ["on_log", "on_train_end"] return expected_events def _A ( self : str ): _UpperCAmelCase : Any = self.get_trainer() _UpperCAmelCase : int = DEFAULT_CALLBACKS.copy() + [ProgressCallback] self.check_callbacks_equality(trainer.callback_handler.callbacks , A ) # Callbacks passed at init are added to the default callbacks _UpperCAmelCase : Optional[int] = self.get_trainer(callbacks=[MyTestTrainerCallback] ) expected_callbacks.append(A ) self.check_callbacks_equality(trainer.callback_handler.callbacks , A ) # TrainingArguments.disable_tqdm controls if use ProgressCallback or PrinterCallback _UpperCAmelCase : List[Any] = self.get_trainer(disable_tqdm=A ) _UpperCAmelCase : Tuple = DEFAULT_CALLBACKS.copy() + [PrinterCallback] self.check_callbacks_equality(trainer.callback_handler.callbacks , A ) def _A ( self : Optional[Any] ): _UpperCAmelCase : Dict = DEFAULT_CALLBACKS.copy() + [ProgressCallback] _UpperCAmelCase : Dict = self.get_trainer() # We can add, pop, or remove by class name trainer.remove_callback(A ) expected_callbacks.remove(A ) self.check_callbacks_equality(trainer.callback_handler.callbacks , A ) _UpperCAmelCase : Optional[Any] = self.get_trainer() _UpperCAmelCase : Any = trainer.pop_callback(A ) self.assertEqual(cb.__class__ , A ) self.check_callbacks_equality(trainer.callback_handler.callbacks , A ) trainer.add_callback(A ) expected_callbacks.insert(0 , A ) self.check_callbacks_equality(trainer.callback_handler.callbacks , A ) # We can also add, pop, or remove by instance _UpperCAmelCase : Union[str, Any] = self.get_trainer() _UpperCAmelCase : List[Any] = trainer.callback_handler.callbacks[0] trainer.remove_callback(A ) expected_callbacks.remove(A ) self.check_callbacks_equality(trainer.callback_handler.callbacks , A ) _UpperCAmelCase : List[Any] = self.get_trainer() _UpperCAmelCase : List[Any] = trainer.callback_handler.callbacks[0] _UpperCAmelCase : Union[str, Any] = trainer.pop_callback(A ) self.assertEqual(A , A ) self.check_callbacks_equality(trainer.callback_handler.callbacks , A ) trainer.add_callback(A ) expected_callbacks.insert(0 , A ) self.check_callbacks_equality(trainer.callback_handler.callbacks , A ) def _A ( self : Optional[Any] ): import warnings # XXX: for now ignore scatter_gather warnings in this test since it's not relevant to what's being tested warnings.simplefilter(action="ignore" , category=A ) _UpperCAmelCase : Optional[int] = self.get_trainer(callbacks=[MyTestTrainerCallback] ) trainer.train() _UpperCAmelCase : Union[str, Any] = trainer.callback_handler.callbacks[-2].events self.assertEqual(A , self.get_expected_events(A ) ) # Independent log/save/eval _UpperCAmelCase : Tuple = self.get_trainer(callbacks=[MyTestTrainerCallback] , logging_steps=5 ) trainer.train() _UpperCAmelCase : int = trainer.callback_handler.callbacks[-2].events self.assertEqual(A , self.get_expected_events(A ) ) _UpperCAmelCase : List[str] = self.get_trainer(callbacks=[MyTestTrainerCallback] , save_steps=5 ) trainer.train() _UpperCAmelCase : Tuple = trainer.callback_handler.callbacks[-2].events self.assertEqual(A , self.get_expected_events(A ) ) _UpperCAmelCase : int = self.get_trainer(callbacks=[MyTestTrainerCallback] , eval_steps=5 , evaluation_strategy="steps" ) trainer.train() _UpperCAmelCase : Optional[int] = trainer.callback_handler.callbacks[-2].events self.assertEqual(A , self.get_expected_events(A ) ) _UpperCAmelCase : Optional[int] = self.get_trainer(callbacks=[MyTestTrainerCallback] , evaluation_strategy="epoch" ) trainer.train() _UpperCAmelCase : int = trainer.callback_handler.callbacks[-2].events self.assertEqual(A , self.get_expected_events(A ) ) # A bit of everything _UpperCAmelCase : int = self.get_trainer( callbacks=[MyTestTrainerCallback] , logging_steps=3 , save_steps=10 , eval_steps=5 , evaluation_strategy="steps" , ) trainer.train() _UpperCAmelCase : Optional[int] = trainer.callback_handler.callbacks[-2].events self.assertEqual(A , self.get_expected_events(A ) ) # warning should be emitted for duplicated callbacks with patch("transformers.trainer_callback.logger.warning" ) as warn_mock: _UpperCAmelCase : Optional[Any] = self.get_trainer( callbacks=[MyTestTrainerCallback, MyTestTrainerCallback] , ) assert str(A ) in warn_mock.call_args[0][0]
31
1
'''simple docstring''' from __future__ import annotations import typing from collections import Counter def UpperCamelCase_ ( _UpperCAmelCase : int ) -> typing.Counter[int]: """simple docstring""" _UpperCAmelCase : typing.Counter[int] = Counter() for base in range(1 , max_perimeter + 1 ): for perpendicular in range(_UpperCAmelCase , max_perimeter + 1 ): _UpperCAmelCase : Tuple = (base * base + perpendicular * perpendicular) ** 0.5 if hypotenuse == int(_UpperCAmelCase ): _UpperCAmelCase : Optional[int] = int(base + perpendicular + hypotenuse ) if perimeter > max_perimeter: continue triplets[perimeter] += 1 return triplets def UpperCamelCase_ ( _UpperCAmelCase : int = 1_000 ) -> int: """simple docstring""" _UpperCAmelCase : int = pythagorean_triple(_UpperCAmelCase ) return triplets.most_common(1 )[0][0] if __name__ == "__main__": print(F'Perimeter {solution()} has maximum solutions')
31
'''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_video_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import VivitImageProcessor class lowerCamelCase_ (unittest.TestCase ): '''simple docstring''' def __init__( self : int , A : Dict , A : Optional[int]=7 , A : Tuple=3 , A : Optional[Any]=10 , A : int=18 , A : Dict=30 , A : List[str]=400 , A : int=True , A : Optional[Any]=None , A : Optional[Any]=True , A : List[Any]=[0.5, 0.5, 0.5] , A : List[str]=[0.5, 0.5, 0.5] , A : Optional[int]=None , ): _UpperCAmelCase : Dict = size if size is not None else {"shortest_edge": 18} _UpperCAmelCase : Optional[Any] = crop_size if crop_size is not None else {"height": 18, "width": 18} _UpperCAmelCase : Tuple = parent _UpperCAmelCase : Any = batch_size _UpperCAmelCase : Optional[int] = num_channels _UpperCAmelCase : Optional[Any] = num_frames _UpperCAmelCase : Any = image_size _UpperCAmelCase : Dict = min_resolution _UpperCAmelCase : Any = max_resolution _UpperCAmelCase : Optional[int] = do_resize _UpperCAmelCase : str = size _UpperCAmelCase : List[Any] = do_normalize _UpperCAmelCase : Any = image_mean _UpperCAmelCase : Tuple = image_std _UpperCAmelCase : Any = crop_size def _A ( self : List[Any] ): return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "size": self.size, "crop_size": self.crop_size, } @require_torch @require_vision class lowerCamelCase_ (snake_case__ , unittest.TestCase ): '''simple docstring''' __UpperCamelCase: Dict = VivitImageProcessor if is_vision_available() else None def _A ( self : int ): _UpperCAmelCase : Tuple = VivitImageProcessingTester(self ) @property def _A ( self : Optional[Any] ): return self.image_processor_tester.prepare_image_processor_dict() def _A ( self : Union[str, Any] ): _UpperCAmelCase : str = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(A , "image_mean" ) ) self.assertTrue(hasattr(A , "image_std" ) ) self.assertTrue(hasattr(A , "do_normalize" ) ) self.assertTrue(hasattr(A , "do_resize" ) ) self.assertTrue(hasattr(A , "do_center_crop" ) ) self.assertTrue(hasattr(A , "size" ) ) def _A ( self : List[Any] ): _UpperCAmelCase : Union[str, Any] = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"shortest_edge": 18} ) self.assertEqual(image_processor.crop_size , {"height": 18, "width": 18} ) _UpperCAmelCase : Optional[int] = self.image_processing_class.from_dict(self.image_processor_dict , size=42 , crop_size=84 ) self.assertEqual(image_processor.size , {"shortest_edge": 42} ) self.assertEqual(image_processor.crop_size , {"height": 84, "width": 84} ) def _A ( self : Tuple ): # Initialize image_processing _UpperCAmelCase : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PIL videos _UpperCAmelCase : Any = prepare_video_inputs(self.image_processor_tester , equal_resolution=A ) for video in video_inputs: self.assertIsInstance(A , A ) self.assertIsInstance(video[0] , Image.Image ) # Test not batched input _UpperCAmelCase : str = image_processing(video_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_videos.shape , ( 1, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) # Test batched _UpperCAmelCase : List[Any] = image_processing(A , return_tensors="pt" ).pixel_values self.assertEqual( encoded_videos.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) def _A ( self : List[Any] ): # Initialize image_processing _UpperCAmelCase : List[str] = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors _UpperCAmelCase : List[Any] = prepare_video_inputs(self.image_processor_tester , equal_resolution=A , numpify=A ) for video in video_inputs: self.assertIsInstance(A , A ) self.assertIsInstance(video[0] , np.ndarray ) # Test not batched input _UpperCAmelCase : Tuple = image_processing(video_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_videos.shape , ( 1, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) # Test batched _UpperCAmelCase : Optional[int] = image_processing(A , return_tensors="pt" ).pixel_values self.assertEqual( encoded_videos.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) def _A ( self : List[Any] ): # Initialize image_processing _UpperCAmelCase : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors _UpperCAmelCase : Optional[int] = prepare_video_inputs(self.image_processor_tester , equal_resolution=A , torchify=A ) for video in video_inputs: self.assertIsInstance(A , A ) self.assertIsInstance(video[0] , torch.Tensor ) # Test not batched input _UpperCAmelCase : Optional[Any] = image_processing(video_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_videos.shape , ( 1, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) # Test batched _UpperCAmelCase : List[Any] = image_processing(A , return_tensors="pt" ).pixel_values self.assertEqual( encoded_videos.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , )
31
1
'''simple docstring''' import inspect from typing import List, Optional, Tuple, Union import torch from ...models import UNetaDModel, VQModel from ...schedulers import DDIMScheduler from ...utils import randn_tensor from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput class lowerCamelCase_ (snake_case__ ): '''simple docstring''' def __init__( self : str , A : VQModel , A : UNetaDModel , A : DDIMScheduler ): super().__init__() self.register_modules(vqvae=A , unet=A , scheduler=A ) @torch.no_grad() def __call__( self : int , A : int = 1 , A : Optional[Union[torch.Generator, List[torch.Generator]]] = None , A : float = 0.0 , A : int = 50 , A : Optional[str] = "pil" , A : bool = True , **A : Union[str, Any] , ): _UpperCAmelCase : Optional[int] = randn_tensor( (batch_size, self.unet.config.in_channels, self.unet.config.sample_size, self.unet.config.sample_size) , generator=A , ) _UpperCAmelCase : Optional[int] = latents.to(self.device ) # scale the initial noise by the standard deviation required by the scheduler _UpperCAmelCase : str = latents * self.scheduler.init_noise_sigma self.scheduler.set_timesteps(A ) # prepare extra kwargs for the scheduler step, since not all schedulers have the same signature _UpperCAmelCase : List[Any] = "eta" in set(inspect.signature(self.scheduler.step ).parameters.keys() ) _UpperCAmelCase : Any = {} if accepts_eta: _UpperCAmelCase : List[str] = eta for t in self.progress_bar(self.scheduler.timesteps ): _UpperCAmelCase : Any = self.scheduler.scale_model_input(A , A ) # predict the noise residual _UpperCAmelCase : Dict = self.unet(A , A ).sample # compute the previous noisy sample x_t -> x_t-1 _UpperCAmelCase : Dict = self.scheduler.step(A , A , A , **A ).prev_sample # decode the image latents with the VAE _UpperCAmelCase : Tuple = self.vqvae.decode(A ).sample _UpperCAmelCase : Dict = (image / 2 + 0.5).clamp(0 , 1 ) _UpperCAmelCase : List[Any] = image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": _UpperCAmelCase : Optional[Any] = self.numpy_to_pil(A ) if not return_dict: return (image,) return ImagePipelineOutput(images=A )
31
'''simple docstring''' import math from typing import Optional import numpy as np from ...configuration_utils import PretrainedConfig from ...utils import logging __SCREAMING_SNAKE_CASE : Dict = logging.get_logger(__name__) __SCREAMING_SNAKE_CASE : List[Any] = { """facebook/encodec_24khz""": """https://huggingface.co/facebook/encodec_24khz/resolve/main/config.json""", """facebook/encodec_48khz""": """https://huggingface.co/facebook/encodec_48khz/resolve/main/config.json""", } class lowerCamelCase_ (snake_case__ ): '''simple docstring''' __UpperCamelCase: str = "encodec" def __init__( self : Optional[int] , A : Union[str, Any]=[1.5, 3.0, 6.0, 12.0, 24.0] , A : List[Any]=24000 , A : Union[str, Any]=1 , A : List[Any]=False , A : Optional[int]=None , A : int=None , A : str=128 , A : List[Any]=32 , A : List[Any]=1 , A : int=[8, 5, 4, 2] , A : Optional[int]="weight_norm" , A : List[Any]=7 , A : Any=7 , A : Dict=3 , A : Optional[int]=2 , A : Dict=True , A : Dict="reflect" , A : Any=2 , A : Dict=2 , A : str=1.0 , A : Optional[int]=1024 , A : Any=None , A : Any=True , **A : str , ): _UpperCAmelCase : Optional[int] = target_bandwidths _UpperCAmelCase : List[str] = sampling_rate _UpperCAmelCase : Optional[int] = audio_channels _UpperCAmelCase : str = normalize _UpperCAmelCase : int = chunk_length_s _UpperCAmelCase : str = overlap _UpperCAmelCase : Optional[Any] = hidden_size _UpperCAmelCase : int = num_filters _UpperCAmelCase : Optional[Any] = num_residual_layers _UpperCAmelCase : Optional[int] = upsampling_ratios _UpperCAmelCase : int = norm_type _UpperCAmelCase : List[Any] = kernel_size _UpperCAmelCase : List[Any] = last_kernel_size _UpperCAmelCase : List[Any] = residual_kernel_size _UpperCAmelCase : List[str] = dilation_growth_rate _UpperCAmelCase : Dict = use_causal_conv _UpperCAmelCase : Tuple = pad_mode _UpperCAmelCase : Tuple = compress _UpperCAmelCase : List[str] = num_lstm_layers _UpperCAmelCase : List[Any] = trim_right_ratio _UpperCAmelCase : int = codebook_size _UpperCAmelCase : Optional[Any] = codebook_dim if codebook_dim is not None else hidden_size _UpperCAmelCase : Optional[int] = use_conv_shortcut if self.norm_type not in ["weight_norm", "time_group_norm"]: raise ValueError( F"""self.norm_type must be one of `\"weight_norm\"`, `\"time_group_norm\"`), got {self.norm_type}""" ) super().__init__(**A ) @property def _A ( self : Any ): if self.chunk_length_s is None: return None else: return int(self.chunk_length_s * self.sampling_rate ) @property def _A ( self : Union[str, Any] ): if self.chunk_length_s is None or self.overlap is None: return None else: return max(1 , int((1.0 - self.overlap) * self.chunk_length ) ) @property def _A ( self : Union[str, Any] ): _UpperCAmelCase : Dict = np.prod(self.upsampling_ratios ) return math.ceil(self.sampling_rate / hop_length ) @property def _A ( self : str ): return int(1000 * self.target_bandwidths[-1] // (self.frame_rate * 10) )
31
1
'''simple docstring''' import json import os import unittest from transformers import CLIPTokenizer, CLIPTokenizerFast from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES from transformers.testing_utils import require_ftfy, require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class lowerCamelCase_ (snake_case__ , unittest.TestCase ): '''simple docstring''' __UpperCamelCase: Optional[Any] = CLIPTokenizer __UpperCamelCase: List[Any] = CLIPTokenizerFast __UpperCamelCase: List[str] = True __UpperCamelCase: Any = {} __UpperCamelCase: Any = False def _A ( self : Any ): super().setUp() # fmt: off _UpperCAmelCase : Any = ["l", "o", "w", "e", "r", "s", "t", "i", "d", "n", "lo", "l</w>", "w</w>", "r</w>", "t</w>", "low</w>", "er</w>", "lowest</w>", "newer</w>", "wider", "<unk>", "<|startoftext|>", "<|endoftext|>"] # fmt: on _UpperCAmelCase : str = dict(zip(A , range(len(A ) ) ) ) _UpperCAmelCase : Union[str, Any] = ["#version: 0.2", "l o", "lo w</w>", "e r</w>"] _UpperCAmelCase : Union[str, Any] = {"unk_token": "<unk>"} _UpperCAmelCase : List[Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["vocab_file"] ) _UpperCAmelCase : Optional[Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["merges_file"] ) with open(self.vocab_file , "w" , encoding="utf-8" ) as fp: fp.write(json.dumps(A ) + "\n" ) with open(self.merges_file , "w" , encoding="utf-8" ) as fp: fp.write("\n".join(A ) ) def _A ( self : Optional[int] , **A : Optional[Any] ): kwargs.update(self.special_tokens_map ) return CLIPTokenizer.from_pretrained(self.tmpdirname , **A ) def _A ( self : Tuple , **A : int ): kwargs.update(self.special_tokens_map ) return CLIPTokenizerFast.from_pretrained(self.tmpdirname , **A ) def _A ( self : Tuple , A : Any ): _UpperCAmelCase : int = "lower newer" _UpperCAmelCase : Union[str, Any] = "lower newer" return input_text, output_text def _A ( self : Any ): _UpperCAmelCase : List[Any] = CLIPTokenizer(self.vocab_file , self.merges_file , **self.special_tokens_map ) _UpperCAmelCase : Dict = "lower newer" _UpperCAmelCase : str = ["lo", "w", "er</w>", "n", "e", "w", "er</w>"] _UpperCAmelCase : List[str] = tokenizer.tokenize(A ) self.assertListEqual(A , A ) _UpperCAmelCase : List[str] = tokens + [tokenizer.unk_token] _UpperCAmelCase : List[Any] = [10, 2, 16, 9, 3, 2, 16, 20] self.assertListEqual(tokenizer.convert_tokens_to_ids(A ) , A ) @require_ftfy def _A ( self : Any ): for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F"""{tokenizer.__class__.__name__} ({pretrained_name})""" ): _UpperCAmelCase : Union[str, Any] = self.tokenizer_class.from_pretrained(A , **A ) _UpperCAmelCase : Optional[int] = self.rust_tokenizer_class.from_pretrained(A , **A ) _UpperCAmelCase : Optional[Any] = "A\n'll 11p223RF☆ho!!to?'d'd''d of a cat to-$''d." _UpperCAmelCase : Any = tokenizer_s.tokenize(A ) _UpperCAmelCase : List[str] = tokenizer_r.tokenize(A ) self.assertListEqual(A , A ) # Test that the tokenization is identical on an example containing a character (Latin Small Letter A # with Tilde) encoded in 2 different ways _UpperCAmelCase : List[Any] = "xa\u0303y" + " " + "x\xe3y" _UpperCAmelCase : List[str] = tokenizer_s.tokenize(A ) _UpperCAmelCase : Optional[Any] = tokenizer_r.tokenize(A ) self.assertListEqual(A , A ) # Test that the tokenization is identical on unicode of space type _UpperCAmelCase : List[str] = [ "\u0009", # (horizontal tab, '\t') "\u000B", # (vertical tab) "\u000C", # (form feed) "\u0020", # (space, ' ') "\u200E", # (left-to-right mark):w "\u200F", # (right-to-left mark) ] for unicode_seq in spaces_unicodes: _UpperCAmelCase : Any = tokenizer_s.tokenize(A ) _UpperCAmelCase : int = tokenizer_r.tokenize(A ) self.assertListEqual(A , A ) # Test that the tokenization is identical on unicode of line break type _UpperCAmelCase : Dict = [ "\u000A", # (line feed, '\n') "\r\n", # (carriage return and line feed, '\r\n') "\u000D", # (carriage return, '\r') "\r", # (carriage return, '\r') "\u000D", # (carriage return, '\r') "\u2028", # (line separator) "\u2029", # (paragraph separator) # "\u0085", # (next line) ] # The tokenization is not identical for the character "\u0085" (next line). The slow version using ftfy transforms # it into the Horizontal Ellipsis character "…" ("\u2026") while the fast version transforms it into a # space (and thus into an empty list). for unicode_seq in line_break_unicodes: _UpperCAmelCase : Optional[int] = tokenizer_s.tokenize(A ) _UpperCAmelCase : List[Any] = tokenizer_r.tokenize(A ) self.assertListEqual(A , A ) def _A ( self : List[Any] ): # Test which aims to verify that the offsets are well adapted to the argument `add_prefix_space` for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F"""{tokenizer.__class__.__name__} ({pretrained_name})""" ): _UpperCAmelCase : Optional[int] = "hello" # `hello` is a token in the vocabulary of `pretrained_name` _UpperCAmelCase : Optional[Any] = F"""{text_of_1_token} {text_of_1_token}""" _UpperCAmelCase : Optional[Any] = self.rust_tokenizer_class.from_pretrained( A , use_fast=A , ) _UpperCAmelCase : int = tokenizer_r(A , return_offsets_mapping=A , add_special_tokens=A ) self.assertEqual(encoding.offset_mapping[0] , (0, len(A )) ) self.assertEqual( encoding.offset_mapping[1] , (len(A ) + 1, len(A ) + 1 + len(A )) , ) _UpperCAmelCase : str = F""" {text}""" _UpperCAmelCase : List[Any] = self.rust_tokenizer_class.from_pretrained( A , use_fast=A , ) _UpperCAmelCase : Optional[Any] = tokenizer_r(A , return_offsets_mapping=A , add_special_tokens=A ) self.assertEqual(encoding.offset_mapping[0] , (1, 1 + len(A )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(A ) + 1, 1 + len(A ) + 1 + len(A )) , ) def _A ( self : Dict ): # Test related to the breaking change introduced in transformers v4.17.0 # We need to check that an error in raised when the user try to load a previous version of the tokenizer. with self.assertRaises(A ) as context: self.rust_tokenizer_class.from_pretrained("robot-test/old-clip-tokenizer" ) self.assertTrue( context.exception.args[0].startswith( "The `backend_tokenizer` provided does not match the expected format." ) ) @require_ftfy def _A ( self : Dict ): super().test_tokenization_python_rust_equals() def _A ( self : str ): # CLIP always lower cases letters pass
31
'''simple docstring''' from typing import Any, Dict, List, Optional, Tuple, Union import torch from torch import nn from torch.utils.data import DistributedSampler, RandomSampler from transformers import PreTrainedModel, Trainer, logging from transformers.integrations import is_fairscale_available from transformers.models.fsmt.configuration_fsmt import FSMTConfig from transformers.optimization import ( Adafactor, AdamW, get_constant_schedule, get_constant_schedule_with_warmup, get_cosine_schedule_with_warmup, get_cosine_with_hard_restarts_schedule_with_warmup, get_linear_schedule_with_warmup, get_polynomial_decay_schedule_with_warmup, ) from transformers.trainer_pt_utils import get_tpu_sampler from transformers.training_args import ParallelMode from transformers.utils import is_torch_tpu_available if is_fairscale_available(): from fairscale.optim import OSS __SCREAMING_SNAKE_CASE : str = logging.get_logger(__name__) __SCREAMING_SNAKE_CASE : Union[str, Any] = { """linear""": get_linear_schedule_with_warmup, """cosine""": get_cosine_schedule_with_warmup, """cosine_w_restarts""": get_cosine_with_hard_restarts_schedule_with_warmup, """polynomial""": get_polynomial_decay_schedule_with_warmup, """constant""": get_constant_schedule, """constant_w_warmup""": get_constant_schedule_with_warmup, } class lowerCamelCase_ (snake_case__ ): '''simple docstring''' def __init__( self : Any , A : Optional[int]=None , A : Tuple=None , *A : Tuple , **A : List[str] ): super().__init__(*A , **A ) if config is None: assert isinstance(self.model , A ), ( "If no `config` is passed the model to be trained has to be of type `PreTrainedModel`, but is" F""" {self.model.__class__}""" ) _UpperCAmelCase : str = self.model.config else: _UpperCAmelCase : List[str] = config _UpperCAmelCase : List[Any] = data_args _UpperCAmelCase : str = self.config.tgt_vocab_size if isinstance(self.config , A ) else self.config.vocab_size if self.args.label_smoothing != 0 or (self.data_args is not None and self.data_args.ignore_pad_token_for_loss): assert self.config.pad_token_id is not None, ( "Make sure that `config.pad_token_id` is correcly defined when ignoring `pad_token` for loss" " calculation or doing label smoothing." ) if self.config.pad_token_id is None and self.config.eos_token_id is not None: logger.warning( F"""The `config.pad_token_id` is `None`. Using `config.eos_token_id` = {self.config.eos_token_id} for""" " padding.." ) if self.args.label_smoothing == 0: _UpperCAmelCase : Optional[Any] = torch.nn.CrossEntropyLoss(ignore_index=self.config.pad_token_id ) else: # dynamically import label_smoothed_nll_loss from utils import label_smoothed_nll_loss _UpperCAmelCase : Dict = label_smoothed_nll_loss def _A ( self : Tuple , A : int ): if self.optimizer is None: _UpperCAmelCase : Tuple = ["bias", "LayerNorm.weight"] _UpperCAmelCase : str = [ { "params": [p for n, p in self.model.named_parameters() if not any(nd in n for nd in no_decay )], "weight_decay": self.args.weight_decay, }, { "params": [p for n, p in self.model.named_parameters() if any(nd in n for nd in no_decay )], "weight_decay": 0.0, }, ] _UpperCAmelCase : int = Adafactor if self.args.adafactor else AdamW if self.args.adafactor: _UpperCAmelCase : List[str] = Adafactor _UpperCAmelCase : List[Any] = {"scale_parameter": False, "relative_step": False} else: _UpperCAmelCase : List[str] = AdamW _UpperCAmelCase : List[str] = { "betas": (self.args.adam_betaa, self.args.adam_betaa), "eps": self.args.adam_epsilon, } _UpperCAmelCase : List[Any] = self.args.learning_rate if self.sharded_ddp: _UpperCAmelCase : List[Any] = OSS( params=A , optim=A , **A , ) else: _UpperCAmelCase : Union[str, Any] = optimizer_cls(A , **A ) if self.lr_scheduler is None: _UpperCAmelCase : List[str] = self._get_lr_scheduler(A ) else: # ignoring --lr_scheduler logger.warning("scheduler is passed to `Seq2SeqTrainer`, `--lr_scheduler` arg is ignored." ) def _A ( self : List[str] , A : Optional[int] ): _UpperCAmelCase : List[str] = arg_to_scheduler[self.args.lr_scheduler] if self.args.lr_scheduler == "constant": _UpperCAmelCase : Optional[Any] = schedule_func(self.optimizer ) elif self.args.lr_scheduler == "constant_w_warmup": _UpperCAmelCase : str = schedule_func(self.optimizer , num_warmup_steps=self.args.warmup_steps ) else: _UpperCAmelCase : str = schedule_func( self.optimizer , num_warmup_steps=self.args.warmup_steps , num_training_steps=A ) return scheduler def _A ( self : Tuple ): if isinstance(self.train_dataset , torch.utils.data.IterableDataset ): return None elif is_torch_tpu_available(): return get_tpu_sampler(self.train_dataset ) else: if self.args.sortish_sampler: self.train_dataset.make_sortish_sampler( self.args.per_device_train_batch_size , distributed=(self.args.parallel_mode == ParallelMode.DISTRIBUTED) , ) return ( RandomSampler(self.train_dataset ) if self.args.local_rank == -1 else DistributedSampler(self.train_dataset ) ) def _A ( self : Any , A : Union[str, Any] , A : Union[str, Any] , A : List[Any] ): if self.args.label_smoothing == 0: if self.data_args is not None and self.data_args.ignore_pad_token_for_loss: # force training to ignore pad token _UpperCAmelCase : List[str] = model(**A , use_cache=A )[0] _UpperCAmelCase : int = self.loss_fn(logits.view(-1 , logits.shape[-1] ) , labels.view(-1 ) ) else: # compute usual loss via models _UpperCAmelCase , _UpperCAmelCase : Any = model(**A , labels=A , use_cache=A )[:2] else: # compute label smoothed loss _UpperCAmelCase : Optional[int] = model(**A , use_cache=A )[0] _UpperCAmelCase : List[str] = torch.nn.functional.log_softmax(A , dim=-1 ) _UpperCAmelCase , _UpperCAmelCase : Union[str, Any] = self.loss_fn(A , A , self.args.label_smoothing , ignore_index=self.config.pad_token_id ) return loss, logits def _A ( self : List[str] , A : Optional[int] , A : Optional[int] ): _UpperCAmelCase : Union[str, Any] = inputs.pop("labels" ) _UpperCAmelCase , _UpperCAmelCase : Optional[int] = self._compute_loss(A , A , A ) return loss def _A ( self : List[str] , A : nn.Module , A : Dict[str, Union[torch.Tensor, Any]] , A : bool , A : Optional[List[str]] = None , ): _UpperCAmelCase : List[str] = self._prepare_inputs(A ) _UpperCAmelCase : Dict = { "max_length": self.data_args.val_max_target_length if self.data_args is not None else self.config.max_length, "num_beams": self.data_args.eval_beams if self.data_args is not None else self.config.num_beams, } if self.args.predict_with_generate and not self.args.prediction_loss_only: _UpperCAmelCase : Dict = self.model.generate( inputs["input_ids"] , attention_mask=inputs["attention_mask"] , **A , ) # in case the batch is shorter than max length, the output should be padded if generated_tokens.shape[-1] < gen_kwargs["max_length"]: _UpperCAmelCase : int = self._pad_tensors_to_max_len(A , gen_kwargs["max_length"] ) _UpperCAmelCase : Any = inputs.pop("labels" ) with torch.no_grad(): # compute loss on predict data _UpperCAmelCase , _UpperCAmelCase : str = self._compute_loss(A , A , A ) _UpperCAmelCase : List[str] = loss.mean().detach() if self.args.prediction_loss_only: return (loss, None, None) _UpperCAmelCase : str = generated_tokens if self.args.predict_with_generate else logits if labels.shape[-1] < gen_kwargs["max_length"]: _UpperCAmelCase : Optional[Any] = self._pad_tensors_to_max_len(A , gen_kwargs["max_length"] ) return (loss, logits, labels) def _A ( self : Dict , A : int , A : List[str] ): # If PAD token is not defined at least EOS token has to be defined _UpperCAmelCase : Union[str, Any] = self.config.pad_token_id if self.config.pad_token_id is not None else self.config.eos_token_id if pad_token_id is None: raise ValueError( "Make sure that either `config.pad_token_id` or `config.eos_token_id` is defined if tensor has to be" F""" padded to `max_length`={max_length}""" ) _UpperCAmelCase : Tuple = pad_token_id * torch.ones( (tensor.shape[0], max_length) , dtype=tensor.dtype , device=tensor.device ) _UpperCAmelCase : Tuple = tensor return padded_tensor
31
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __SCREAMING_SNAKE_CASE : Optional[int] = { """configuration_swinv2""": ["""SWINV2_PRETRAINED_CONFIG_ARCHIVE_MAP""", """Swinv2Config"""], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __SCREAMING_SNAKE_CASE : Tuple = [ """SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST""", """Swinv2ForImageClassification""", """Swinv2ForMaskedImageModeling""", """Swinv2Model""", """Swinv2PreTrainedModel""", ] if TYPE_CHECKING: from .configuration_swinva import SWINV2_PRETRAINED_CONFIG_ARCHIVE_MAP, SwinvaConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_swinva import ( SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST, SwinvaForImageClassification, SwinvaForMaskedImageModeling, SwinvaModel, SwinvaPreTrainedModel, ) else: import sys __SCREAMING_SNAKE_CASE : str = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
31
'''simple docstring''' import copy from typing import Any, Dict, List, Optional, Union import numpy as np import torch from ...audio_utils import mel_filter_bank, spectrogram, window_function from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import TensorType, logging __SCREAMING_SNAKE_CASE : List[str] = logging.get_logger(__name__) class lowerCamelCase_ (snake_case__ ): '''simple docstring''' __UpperCamelCase: Optional[int] = ["input_features", "is_longer"] def __init__( self : str , A : int=64 , A : Dict=48000 , A : str=480 , A : List[Any]=10 , A : Optional[Any]=1024 , A : Tuple=0.0 , A : List[Any]=False , A : float = 0 , A : float = 14000 , A : int = None , A : str = "fusion" , A : str = "repeatpad" , **A : Dict , ): super().__init__( feature_size=A , sampling_rate=A , padding_value=A , return_attention_mask=A , **A , ) _UpperCAmelCase : Optional[Any] = top_db _UpperCAmelCase : Dict = truncation _UpperCAmelCase : List[Any] = padding _UpperCAmelCase : Optional[Any] = fft_window_size _UpperCAmelCase : Dict = (fft_window_size >> 1) + 1 _UpperCAmelCase : Any = hop_length _UpperCAmelCase : Tuple = max_length_s _UpperCAmelCase : str = max_length_s * sampling_rate _UpperCAmelCase : Any = sampling_rate _UpperCAmelCase : Optional[int] = frequency_min _UpperCAmelCase : str = frequency_max _UpperCAmelCase : Union[str, Any] = mel_filter_bank( num_frequency_bins=self.nb_frequency_bins , num_mel_filters=A , min_frequency=A , max_frequency=A , sampling_rate=A , norm=A , mel_scale="htk" , ) _UpperCAmelCase : Tuple = mel_filter_bank( num_frequency_bins=self.nb_frequency_bins , num_mel_filters=A , min_frequency=A , max_frequency=A , sampling_rate=A , norm="slaney" , mel_scale="slaney" , ) def _A ( self : List[str] ): _UpperCAmelCase : Union[str, Any] = copy.deepcopy(self.__dict__ ) _UpperCAmelCase : Dict = self.__class__.__name__ if "mel_filters" in output: del output["mel_filters"] if "mel_filters_slaney" in output: del output["mel_filters_slaney"] return output def _A ( self : Optional[Any] , A : np.array , A : Optional[np.array] = None ): _UpperCAmelCase : Dict = spectrogram( A , window_function(self.fft_window_size , "hann" ) , frame_length=self.fft_window_size , hop_length=self.hop_length , power=2.0 , mel_filters=A , log_mel="dB" , ) return log_mel_spectrogram.T def _A ( self : str , A : str , A : List[str] , A : List[Any] ): _UpperCAmelCase : List[str] = np.array_split(list(range(0 , total_frames - chunk_frames + 1 ) ) , 3 ) if len(ranges[1] ) == 0: # if the audio is too short, we just use the first chunk _UpperCAmelCase : Optional[Any] = [0] if len(ranges[2] ) == 0: # if the audio is too short, we just use the first chunk _UpperCAmelCase : Tuple = [0] # randomly choose index for each part _UpperCAmelCase : Dict = np.random.choice(ranges[0] ) _UpperCAmelCase : str = np.random.choice(ranges[1] ) _UpperCAmelCase : Tuple = np.random.choice(ranges[2] ) _UpperCAmelCase : str = mel[idx_front : idx_front + chunk_frames, :] _UpperCAmelCase : str = mel[idx_middle : idx_middle + chunk_frames, :] _UpperCAmelCase : List[Any] = mel[idx_back : idx_back + chunk_frames, :] _UpperCAmelCase : Dict = torch.tensor(mel[None, None, :] ) _UpperCAmelCase : Optional[Any] = torch.nn.functional.interpolate( A , size=[chunk_frames, 64] , mode="bilinear" , align_corners=A ) _UpperCAmelCase : List[str] = mel_shrink[0][0].numpy() _UpperCAmelCase : str = np.stack([mel_shrink, mel_chunk_front, mel_chunk_middle, mel_chunk_back] , axis=0 ) return mel_fusion def _A ( self : List[Any] , A : np.array , A : List[str] , A : Any , A : Optional[int] ): if waveform.shape[0] > max_length: if truncation == "rand_trunc": _UpperCAmelCase : int = True # random crop to max_length (for compatibility) -> this should be handled by self.pad _UpperCAmelCase : str = len(A ) - max_length _UpperCAmelCase : str = np.random.randint(0 , overflow + 1 ) _UpperCAmelCase : int = waveform[idx : idx + max_length] _UpperCAmelCase : Any = self._np_extract_fbank_features(A , self.mel_filters_slaney )[None, :] elif truncation == "fusion": _UpperCAmelCase : Tuple = self._np_extract_fbank_features(A , self.mel_filters ) _UpperCAmelCase : List[str] = max_length // self.hop_length + 1 # the +1 related to how the spectrogram is computed _UpperCAmelCase : Optional[Any] = mel.shape[0] if chunk_frames == total_frames: # there is a corner case where the audio length is larger than max_length but smaller than max_length+hop_length. # In this case, we just use the whole audio. _UpperCAmelCase : Any = np.stack([mel, mel, mel, mel] , axis=0 ) _UpperCAmelCase : int = False else: _UpperCAmelCase : Tuple = self._random_mel_fusion(A , A , A ) _UpperCAmelCase : Any = True else: raise NotImplementedError(F"""data_truncating {truncation} not implemented""" ) else: _UpperCAmelCase : Optional[Any] = False # only use repeat as a new possible value for padding. you repeat the audio before applying the usual max_length padding if waveform.shape[0] < max_length: if padding == "repeat": _UpperCAmelCase : str = int(max_length / len(A ) ) _UpperCAmelCase : Dict = np.stack(np.tile(A , n_repeat + 1 ) )[:max_length] if padding == "repeatpad": _UpperCAmelCase : Dict = int(max_length / len(A ) ) _UpperCAmelCase : List[str] = np.stack(np.tile(A , A ) ) _UpperCAmelCase : Optional[Any] = np.pad(A , (0, max_length - waveform.shape[0]) , mode="constant" , constant_values=0 ) if truncation == "fusion": _UpperCAmelCase : str = self._np_extract_fbank_features(A , self.mel_filters ) _UpperCAmelCase : Optional[int] = np.stack([input_mel, input_mel, input_mel, input_mel] , axis=0 ) else: _UpperCAmelCase : List[str] = self._np_extract_fbank_features(A , self.mel_filters_slaney )[None, :] return input_mel, longer def __call__( self : Union[str, Any] , A : Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] , A : str = None , A : Optional[str] = None , A : Optional[int] = None , A : Optional[int] = None , A : Optional[Union[str, TensorType]] = None , **A : List[str] , ): _UpperCAmelCase : int = truncation if truncation is not None else self.truncation _UpperCAmelCase : Optional[int] = padding if padding else self.padding if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( F"""The model corresponding to this feature extractor: {self.__class__.__name__} was trained using a""" F""" sampling rate of {self.sampling_rate}. Please make sure that the provided `raw_speech` input""" F""" was sampled with {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." ) _UpperCAmelCase : Any = isinstance(A , 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}""" ) _UpperCAmelCase : Optional[Any] = is_batched_numpy or ( isinstance(A , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: _UpperCAmelCase : int = [np.asarray(A , dtype=np.floataa ) for speech in raw_speech] elif not is_batched and not isinstance(A , np.ndarray ): _UpperCAmelCase : List[str] = np.asarray(A , dtype=np.floataa ) elif isinstance(A , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): _UpperCAmelCase : Any = raw_speech.astype(np.floataa ) # always return batch if not is_batched: _UpperCAmelCase : List[str] = [np.asarray(A )] # convert to mel spectrogram, truncate and pad if needed. _UpperCAmelCase : Dict = [ self._get_input_mel(A , max_length if max_length else self.nb_max_samples , A , A ) for waveform in raw_speech ] _UpperCAmelCase : int = [] _UpperCAmelCase : Optional[Any] = [] for mel, longer in padded_inputs: input_mel.append(A ) is_longer.append(A ) if truncation == "fusion" and sum(A ) == 0: # if no audio is longer than 10s, then randomly select one audio to be longer _UpperCAmelCase : Union[str, Any] = np.random.randint(0 , len(A ) ) _UpperCAmelCase : Optional[Any] = True if isinstance(input_mel[0] , A ): _UpperCAmelCase : List[str] = [np.asarray(A , dtype=np.floataa ) for feature in input_mel] # is_longer is a list of bool _UpperCAmelCase : Tuple = [[longer] for longer in is_longer] _UpperCAmelCase : Optional[Any] = {"input_features": input_mel, "is_longer": is_longer} _UpperCAmelCase : Tuple = BatchFeature(A ) if return_tensors is not None: _UpperCAmelCase : List[Any] = input_features.convert_to_tensors(A ) return input_features
31
1
'''simple docstring''' import unittest from transformers import load_tool from .test_tools_common import ToolTesterMixin __SCREAMING_SNAKE_CASE : Optional[int] = """ Hugging Face was founded in 2016 by French entrepreneurs Clément Delangue, Julien Chaumond, and Thomas Wolf originally as a company that developed a chatbot app targeted at teenagers.[2] After open-sourcing the model behind the chatbot, the company pivoted to focus on being a platform for machine learning. In March 2021, Hugging Face raised $40 million in a Series B funding round.[3] On April 28, 2021, the company launched the BigScience Research Workshop in collaboration with several other research groups to release an open large language model.[4] In 2022, the workshop concluded with the announcement of BLOOM, a multilingual large language model with 176 billion parameters.[5] """ class lowerCamelCase_ (unittest.TestCase , snake_case__ ): '''simple docstring''' def _A ( self : int ): _UpperCAmelCase : Optional[Any] = load_tool("text-question-answering" ) self.tool.setup() _UpperCAmelCase : Dict = load_tool("text-question-answering" , remote=A ) def _A ( self : List[Any] ): _UpperCAmelCase : Optional[int] = self.tool(A , "What did Hugging Face do in April 2021?" ) self.assertEqual(A , "launched the BigScience Research Workshop" ) def _A ( self : int ): _UpperCAmelCase : Dict = self.remote_tool(A , "What did Hugging Face do in April 2021?" ) self.assertEqual(A , "launched the BigScience Research Workshop" ) def _A ( self : int ): _UpperCAmelCase : Optional[Any] = self.tool(text=A , question="What did Hugging Face do in April 2021?" ) self.assertEqual(A , "launched the BigScience Research Workshop" ) def _A ( self : Tuple ): _UpperCAmelCase : Any = self.remote_tool(text=A , question="What did Hugging Face do in April 2021?" ) self.assertEqual(A , "launched the BigScience Research Workshop" )
31
'''simple docstring''' from typing import TYPE_CHECKING from ...file_utils import _LazyModule, is_tokenizers_available, is_torch_available from ...utils import OptionalDependencyNotAvailable __SCREAMING_SNAKE_CASE : Optional[int] = {"""configuration_gpt_neox""": ["""GPT_NEOX_PRETRAINED_CONFIG_ARCHIVE_MAP""", """GPTNeoXConfig"""]} try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __SCREAMING_SNAKE_CASE : Tuple = ["""GPTNeoXTokenizerFast"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __SCREAMING_SNAKE_CASE : Dict = [ """GPT_NEOX_PRETRAINED_MODEL_ARCHIVE_LIST""", """GPTNeoXForCausalLM""", """GPTNeoXForQuestionAnswering""", """GPTNeoXForSequenceClassification""", """GPTNeoXForTokenClassification""", """GPTNeoXLayer""", """GPTNeoXModel""", """GPTNeoXPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_gpt_neox import GPT_NEOX_PRETRAINED_CONFIG_ARCHIVE_MAP, GPTNeoXConfig try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_gpt_neox_fast import GPTNeoXTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_gpt_neox import ( GPT_NEOX_PRETRAINED_MODEL_ARCHIVE_LIST, GPTNeoXForCausalLM, GPTNeoXForQuestionAnswering, GPTNeoXForSequenceClassification, GPTNeoXForTokenClassification, GPTNeoXLayer, GPTNeoXModel, GPTNeoXPreTrainedModel, ) else: import sys __SCREAMING_SNAKE_CASE : Any = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
31
1
'''simple docstring''' import datasets from .evaluate import evaluate __SCREAMING_SNAKE_CASE : Tuple = """\ @article{hendrycks2021cuad, title={CUAD: An Expert-Annotated NLP Dataset for Legal Contract Review}, author={Dan Hendrycks and Collin Burns and Anya Chen and Spencer Ball}, journal={arXiv preprint arXiv:2103.06268}, year={2021} } """ __SCREAMING_SNAKE_CASE : str = """ This metric wrap the official scoring script for version 1 of the Contract Understanding Atticus Dataset (CUAD). Contract Understanding Atticus Dataset (CUAD) v1 is a corpus of more than 13,000 labels in 510 commercial legal contracts that have been manually labeled to identify 41 categories of important clauses that lawyers look for when reviewing contracts in connection with corporate transactions. """ __SCREAMING_SNAKE_CASE : Optional[int] = """ Computes CUAD scores (EM, F1, AUPR, Precision@80%Recall, and Precision@90%Recall). Args: predictions: List of question-answers dictionaries with the following key-values: - 'id': id of the question-answer pair as given in the references (see below) - 'prediction_text': list of possible texts for the answer, as a list of strings depending on a threshold on the confidence probability of each prediction. references: List of question-answers dictionaries with the following key-values: - 'id': id of the question-answer pair (see above), - 'answers': a Dict in the CUAD dataset format { 'text': list of possible texts for the answer, as a list of strings 'answer_start': list of start positions for the answer, as a list of ints } Note that answer_start values are not taken into account to compute the metric. Returns: 'exact_match': Exact match (the normalized answer exactly match the gold answer) 'f1': The F-score of predicted tokens versus the gold answer 'aupr': Area Under the Precision-Recall curve 'prec_at_80_recall': Precision at 80% recall 'prec_at_90_recall': Precision at 90% recall Examples: >>> predictions = [{'prediction_text': ['The seller:', 'The buyer/End-User: Shenzhen LOHAS Supply Chain Management Co., Ltd.'], 'id': 'LohaCompanyltd_20191209_F-1_EX-10.16_11917878_EX-10.16_Supply Agreement__Parties'}] >>> references = [{'answers': {'answer_start': [143, 49], 'text': ['The seller:', 'The buyer/End-User: Shenzhen LOHAS Supply Chain Management Co., Ltd.']}, 'id': 'LohaCompanyltd_20191209_F-1_EX-10.16_11917878_EX-10.16_Supply Agreement__Parties'}] >>> cuad_metric = datasets.load_metric(\"cuad\") >>> results = cuad_metric.compute(predictions=predictions, references=references) >>> print(results) {'exact_match': 100.0, 'f1': 100.0, 'aupr': 0.0, 'prec_at_80_recall': 1.0, 'prec_at_90_recall': 1.0} """ @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class lowerCamelCase_ (datasets.Metric ): '''simple docstring''' def _A ( self : Optional[Any] ): return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "predictions": { "id": datasets.Value("string" ), "prediction_text": datasets.features.Sequence(datasets.Value("string" ) ), }, "references": { "id": datasets.Value("string" ), "answers": datasets.features.Sequence( { "text": datasets.Value("string" ), "answer_start": datasets.Value("int32" ), } ), }, } ) , codebase_urls=["https://www.atticusprojectai.org/cuad"] , reference_urls=["https://www.atticusprojectai.org/cuad"] , ) def _A ( self : Dict , A : Dict , A : Optional[int] ): _UpperCAmelCase : int = {prediction["id"]: prediction["prediction_text"] for prediction in predictions} _UpperCAmelCase : str = [ { "paragraphs": [ { "qas": [ { "answers": [{"text": answer_text} for answer_text in ref["answers"]["text"]], "id": ref["id"], } for ref in references ] } ] } ] _UpperCAmelCase : Tuple = evaluate(dataset=A , predictions=A ) return score
31
'''simple docstring''' class lowerCamelCase_ : '''simple docstring''' def __init__( self : Tuple , A : Any , A : str , A : Union[str, Any] ): _UpperCAmelCase : Optional[int] = None _UpperCAmelCase : Optional[int] = None _UpperCAmelCase : Any = graph self._normalize_graph(A , A ) _UpperCAmelCase : List[str] = len(A ) _UpperCAmelCase : Tuple = None def _A ( self : Any , A : List[Any] , A : str ): if sources is int: _UpperCAmelCase : List[Any] = [sources] if sinks is int: _UpperCAmelCase : List[Any] = [sinks] if len(A ) == 0 or len(A ) == 0: return _UpperCAmelCase : str = sources[0] _UpperCAmelCase : Union[str, Any] = sinks[0] # make fake vertex if there are more # than one source or sink if len(A ) > 1 or len(A ) > 1: _UpperCAmelCase : Dict = 0 for i in sources: max_input_flow += sum(self.graph[i] ) _UpperCAmelCase : str = len(self.graph ) + 1 for room in self.graph: room.insert(0 , 0 ) self.graph.insert(0 , [0] * size ) for i in sources: _UpperCAmelCase : Optional[Any] = max_input_flow _UpperCAmelCase : List[str] = 0 _UpperCAmelCase : str = len(self.graph ) + 1 for room in self.graph: room.append(0 ) self.graph.append([0] * size ) for i in sinks: _UpperCAmelCase : Dict = max_input_flow _UpperCAmelCase : List[Any] = size - 1 def _A ( self : Union[str, Any] ): 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 _A ( self : Tuple , A : Dict ): _UpperCAmelCase : str = algorithm(self ) class lowerCamelCase_ : '''simple docstring''' def __init__( self : Any , A : str ): _UpperCAmelCase : Optional[int] = flow_network _UpperCAmelCase : Any = flow_network.verticesCount _UpperCAmelCase : List[str] = flow_network.sourceIndex _UpperCAmelCase : Union[str, Any] = flow_network.sinkIndex # it's just a reference, so you shouldn't change # it in your algorithms, use deep copy before doing that _UpperCAmelCase : Any = flow_network.graph _UpperCAmelCase : Union[str, Any] = False def _A ( self : List[str] ): if not self.executed: self._algorithm() _UpperCAmelCase : int = True def _A ( self : List[Any] ): pass class lowerCamelCase_ (snake_case__ ): '''simple docstring''' def __init__( self : Optional[int] , A : Union[str, Any] ): super().__init__(A ) # use this to save your result _UpperCAmelCase : Any = -1 def _A ( self : Union[str, Any] ): if not self.executed: raise Exception("You should execute algorithm before using its result!" ) return self.maximum_flow class lowerCamelCase_ (snake_case__ ): '''simple docstring''' def __init__( self : Tuple , A : int ): super().__init__(A ) _UpperCAmelCase : List[str] = [[0] * self.verticies_count for i in range(self.verticies_count )] _UpperCAmelCase : Union[str, Any] = [0] * self.verticies_count _UpperCAmelCase : int = [0] * self.verticies_count def _A ( self : Dict ): _UpperCAmelCase : Dict = 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 _UpperCAmelCase : Optional[int] = [ i for i in range(self.verticies_count ) if i != self.source_index and i != self.sink_index ] # move through list _UpperCAmelCase : Any = 0 while i < len(A ): _UpperCAmelCase : int = vertices_list[i] _UpperCAmelCase : int = self.heights[vertex_index] self.process_vertex(A ) 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(A ) ) _UpperCAmelCase : Union[str, Any] = 0 else: i += 1 _UpperCAmelCase : List[Any] = sum(self.preflow[self.source_index] ) def _A ( self : Union[str, Any] , A : str ): 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(A , A ) self.relabel(A ) def _A ( self : int , A : Dict , A : List[str] ): _UpperCAmelCase : 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 _A ( self : Optional[int] , A : Union[str, Any] ): _UpperCAmelCase : str = 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): _UpperCAmelCase : Tuple = self.heights[to_index] if min_height is not None: _UpperCAmelCase : Optional[Any] = min_height + 1 if __name__ == "__main__": __SCREAMING_SNAKE_CASE : Optional[int] = [0] __SCREAMING_SNAKE_CASE : Union[str, Any] = [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], # ] __SCREAMING_SNAKE_CASE : List[Any] = [[0, 7, 0, 0], [0, 0, 6, 0], [0, 0, 0, 8], [9, 0, 0, 0]] # prepare our network __SCREAMING_SNAKE_CASE : Union[str, Any] = FlowNetwork(graph, entrances, exits) # set algorithm flow_network.set_maximum_flow_algorithm(PushRelabelExecutor) # and calculate __SCREAMING_SNAKE_CASE : Optional[Any] = flow_network.find_maximum_flow() print(F'maximum flow is {maximum_flow}')
31
1
'''simple docstring''' import unittest from transformers import MPNetConfig, is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( MPNetForMaskedLM, MPNetForMultipleChoice, MPNetForQuestionAnswering, MPNetForSequenceClassification, MPNetForTokenClassification, MPNetModel, ) class lowerCamelCase_ : '''simple docstring''' def __init__( self : str , A : str , A : Optional[Any]=13 , A : Union[str, Any]=7 , A : List[str]=True , A : Union[str, Any]=True , A : Optional[int]=False , A : Any=True , A : Optional[int]=99 , A : str=64 , A : Any=5 , A : int=4 , A : List[str]=64 , A : Optional[int]="gelu" , A : Tuple=0.1 , A : Optional[Any]=0.1 , A : Dict=512 , A : List[Any]=16 , A : Any=2 , A : Optional[int]=0.02 , A : Any=3 , A : Any=4 , A : Optional[int]=None , ): _UpperCAmelCase : Tuple = parent _UpperCAmelCase : Dict = batch_size _UpperCAmelCase : Optional[int] = seq_length _UpperCAmelCase : int = is_training _UpperCAmelCase : Optional[Any] = use_input_mask _UpperCAmelCase : Optional[int] = use_token_type_ids _UpperCAmelCase : List[str] = use_labels _UpperCAmelCase : Optional[int] = vocab_size _UpperCAmelCase : int = hidden_size _UpperCAmelCase : List[Any] = num_hidden_layers _UpperCAmelCase : List[str] = num_attention_heads _UpperCAmelCase : Union[str, Any] = intermediate_size _UpperCAmelCase : str = hidden_act _UpperCAmelCase : Optional[Any] = hidden_dropout_prob _UpperCAmelCase : Dict = attention_probs_dropout_prob _UpperCAmelCase : int = max_position_embeddings _UpperCAmelCase : int = type_vocab_size _UpperCAmelCase : Optional[int] = type_sequence_label_size _UpperCAmelCase : Any = initializer_range _UpperCAmelCase : Dict = num_labels _UpperCAmelCase : str = num_choices _UpperCAmelCase : List[str] = scope def _A ( self : Optional[int] ): return MPNetConfig.from_pretrained("microsoft/mpnet-base" ) def _A ( self : int ): _UpperCAmelCase : Optional[int] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _UpperCAmelCase : List[str] = None if self.use_input_mask: _UpperCAmelCase : Any = random_attention_mask([self.batch_size, self.seq_length] ) _UpperCAmelCase : Tuple = None _UpperCAmelCase : Tuple = None _UpperCAmelCase : str = None if self.use_labels: _UpperCAmelCase : str = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _UpperCAmelCase : Optional[Any] = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) _UpperCAmelCase : List[Any] = ids_tensor([self.batch_size] , self.num_choices ) _UpperCAmelCase : List[str] = self.get_config() return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels def _A ( self : List[Any] ): return MPNetConfig( 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 , initializer_range=self.initializer_range , ) def _A ( self : Union[str, Any] , A : Union[str, Any] , A : Optional[int] , A : List[str] , A : Optional[int] , A : Any , A : str ): _UpperCAmelCase : Dict = MPNetModel(config=A ) model.to(A ) model.eval() _UpperCAmelCase : Optional[int] = model(A , A ) _UpperCAmelCase : str = model(A ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def _A ( self : int , A : int , A : Optional[int] , A : Union[str, Any] , A : Any , A : List[Any] , A : Optional[int] ): _UpperCAmelCase : Any = MPNetForQuestionAnswering(config=A ) model.to(A ) model.eval() _UpperCAmelCase : List[str] = model( A , attention_mask=A , start_positions=A , end_positions=A , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def _A ( self : List[str] , A : str , A : Optional[Any] , A : int , A : List[Any] , A : Union[str, Any] , A : int ): _UpperCAmelCase : Union[str, Any] = self.num_labels _UpperCAmelCase : Tuple = MPNetForSequenceClassification(A ) model.to(A ) model.eval() _UpperCAmelCase : str = model(A , attention_mask=A , labels=A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def _A ( self : str , A : Optional[Any] , A : Tuple , A : Union[str, Any] , A : Union[str, Any] , A : str , A : int ): _UpperCAmelCase : List[str] = self.num_choices _UpperCAmelCase : Optional[int] = MPNetForMultipleChoice(config=A ) model.to(A ) model.eval() _UpperCAmelCase : int = input_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() _UpperCAmelCase : List[str] = input_mask.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() _UpperCAmelCase : str = model( A , attention_mask=A , labels=A , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def _A ( self : Optional[Any] , A : List[Any] , A : List[Any] , A : int , A : List[Any] , A : Any , A : Dict ): _UpperCAmelCase : Union[str, Any] = self.num_labels _UpperCAmelCase : Union[str, Any] = MPNetForTokenClassification(config=A ) model.to(A ) model.eval() _UpperCAmelCase : str = model(A , attention_mask=A , labels=A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def _A ( self : List[str] ): _UpperCAmelCase : Optional[Any] = self.prepare_config_and_inputs() ((_UpperCAmelCase) , (_UpperCAmelCase) , (_UpperCAmelCase) , (_UpperCAmelCase) , (_UpperCAmelCase) , (_UpperCAmelCase)) : Optional[Any] = config_and_inputs _UpperCAmelCase : Dict = {"input_ids": input_ids, "attention_mask": input_mask} return config, inputs_dict @require_torch class lowerCamelCase_ (snake_case__ , snake_case__ , unittest.TestCase ): '''simple docstring''' __UpperCamelCase: Any = ( ( MPNetForMaskedLM, MPNetForMultipleChoice, MPNetForQuestionAnswering, MPNetForSequenceClassification, MPNetForTokenClassification, MPNetModel, ) if is_torch_available() else () ) __UpperCamelCase: List[Any] = ( { "feature-extraction": MPNetModel, "fill-mask": MPNetForMaskedLM, "question-answering": MPNetForQuestionAnswering, "text-classification": MPNetForSequenceClassification, "token-classification": MPNetForTokenClassification, "zero-shot": MPNetForSequenceClassification, } if is_torch_available() else {} ) __UpperCamelCase: Union[str, Any] = False __UpperCamelCase: Optional[int] = True def _A ( self : Optional[Any] ): _UpperCAmelCase : Optional[Any] = MPNetModelTester(self ) _UpperCAmelCase : Any = ConfigTester(self , config_class=A , hidden_size=37 ) def _A ( self : Union[str, Any] ): self.config_tester.run_common_tests() def _A ( self : Optional[int] ): _UpperCAmelCase : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mpnet_model(*A ) def _A ( self : List[Any] ): _UpperCAmelCase : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mpnet_for_sequence_classification(*A ) def _A ( self : Tuple ): _UpperCAmelCase : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mpnet_for_multiple_choice(*A ) def _A ( self : str ): _UpperCAmelCase : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mpnet_for_token_classification(*A ) def _A ( self : str ): _UpperCAmelCase : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mpnet_for_question_answering(*A ) @require_torch class lowerCamelCase_ (unittest.TestCase ): '''simple docstring''' @slow def _A ( self : int ): _UpperCAmelCase : Optional[int] = MPNetModel.from_pretrained("microsoft/mpnet-base" ) _UpperCAmelCase : Optional[Any] = torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]] ) _UpperCAmelCase : Dict = model(A )[0] _UpperCAmelCase : List[str] = torch.Size((1, 11, 768) ) self.assertEqual(output.shape , A ) _UpperCAmelCase : Union[str, Any] = torch.tensor( [[[-0.0_550, 0.1_943, -0.0_740], [-0.0_562, 0.2_211, -0.0_579], [-0.0_437, 0.3_337, -0.0_641]]] ) # compare the actual values for a slice. self.assertTrue(torch.allclose(output[:, :3, :3] , A , atol=1E-4 ) )
31
'''simple docstring''' def UpperCamelCase_ ( _UpperCAmelCase : str , _UpperCAmelCase : str ) -> float: """simple docstring""" def get_matched_characters(_UpperCAmelCase : str , _UpperCAmelCase : str ) -> str: _UpperCAmelCase : Tuple = [] _UpperCAmelCase : Dict = min(len(_stra ) , len(_stra ) ) // 2 for i, l in enumerate(_stra ): _UpperCAmelCase : int = int(max(0 , i - limit ) ) _UpperCAmelCase : Any = int(min(i + limit + 1 , len(_stra ) ) ) if l in _stra[left:right]: matched.append(_UpperCAmelCase ) _UpperCAmelCase : List[Any] = F"""{_stra[0:_stra.index(_UpperCAmelCase )]} {_stra[_stra.index(_UpperCAmelCase ) + 1:]}""" return "".join(_UpperCAmelCase ) # matching characters _UpperCAmelCase : Union[str, Any] = get_matched_characters(_UpperCAmelCase , _UpperCAmelCase ) _UpperCAmelCase : Tuple = get_matched_characters(_UpperCAmelCase , _UpperCAmelCase ) _UpperCAmelCase : Tuple = len(_UpperCAmelCase ) # transposition _UpperCAmelCase : Optional[Any] = ( len([(ca, ca) for ca, ca in zip(_UpperCAmelCase , _UpperCAmelCase ) if ca != ca] ) // 2 ) if not match_count: _UpperCAmelCase : Dict = 0.0 else: _UpperCAmelCase : Optional[int] = ( 1 / 3 * ( match_count / len(_UpperCAmelCase ) + match_count / len(_UpperCAmelCase ) + (match_count - transpositions) / match_count ) ) # common prefix up to 4 characters _UpperCAmelCase : str = 0 for ca, ca in zip(stra[:4] , stra[:4] ): if ca == ca: prefix_len += 1 else: break return jaro + 0.1 * prefix_len * (1 - jaro) if __name__ == "__main__": import doctest doctest.testmod() print(jaro_winkler("""hello""", """world"""))
31
1
'''simple docstring''' import argparse import json import os import re import shutil import torch from transformers import BioGptConfig, BioGptForCausalLM from transformers.models.biogpt.tokenization_biogpt import VOCAB_FILES_NAMES from transformers.tokenization_utils_base import TOKENIZER_CONFIG_FILE from transformers.utils import WEIGHTS_NAME, logging logging.set_verbosity_warning() __SCREAMING_SNAKE_CASE : str = 2 class lowerCamelCase_ : '''simple docstring''' def __init__( self : List[Any] , *, # begin keyword-only arguments A : Union[str, Any]="<s>" , A : Dict="<pad>" , A : Any="</s>" , A : Tuple="<unk>" , A : List[Any]=None , ): _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase : Dict = bos, unk, pad, eos _UpperCAmelCase : int = [] _UpperCAmelCase : Dict = [] _UpperCAmelCase : Dict = {} _UpperCAmelCase : Optional[Any] = self.add_symbol(A ) _UpperCAmelCase : Dict = self.add_symbol(A ) _UpperCAmelCase : int = self.add_symbol(A ) _UpperCAmelCase : List[Any] = self.add_symbol(A ) if extra_special_symbols: for s in extra_special_symbols: self.add_symbol(A ) _UpperCAmelCase : Tuple = len(self.symbols ) def __eq__( self : Optional[Any] , A : Tuple ): return self.indices == other.indices def __getitem__( self : int , A : Optional[Any] ): if idx < len(self.symbols ): return self.symbols[idx] return self.unk_word def __len__( self : Union[str, Any] ): return len(self.symbols ) def __contains__( self : List[Any] , A : Dict ): return sym in self.indices @classmethod def _A ( cls : int , A : Union[str, Any] ): _UpperCAmelCase : List[Any] = cls() d.add_from_file(A ) return d def _A ( self : int , A : Tuple , A : Optional[Any]=1 , A : str=False ): if word in self.indices and not overwrite: _UpperCAmelCase : Union[str, Any] = self.indices[word] _UpperCAmelCase : Tuple = self.count[idx] + n return idx else: _UpperCAmelCase : List[Any] = len(self.symbols ) _UpperCAmelCase : int = idx self.symbols.append(A ) self.count.append(A ) return idx def _A ( self : int , A : List[Any] ): return 0 def _A ( self : Dict , A : Optional[int] ): if isinstance(A , A ): try: with open(A , "r" , encoding="utf-8" ) as fd: self.add_from_file(A ) except FileNotFoundError as fnfe: raise fnfe except UnicodeError: raise Exception("Incorrect encoding detected in {}, please rebuild the dataset".format(A ) ) return _UpperCAmelCase : Union[str, Any] = f.readlines() _UpperCAmelCase : Optional[Any] = self._load_meta(A ) for line in lines[indices_start_line:]: try: _UpperCAmelCase , _UpperCAmelCase : Any = line.rstrip().rsplit(" " , 1 ) if field == "#fairseq:overwrite": _UpperCAmelCase : Tuple = True _UpperCAmelCase , _UpperCAmelCase : List[Any] = line.rsplit(" " , 1 ) else: _UpperCAmelCase : Optional[Any] = False _UpperCAmelCase : str = int(A ) _UpperCAmelCase : Any = line if word in self and not overwrite: raise RuntimeError( "Duplicate word found when loading Dictionary: '{}'. " "Duplicate words can overwrite earlier ones by adding the " "#fairseq:overwrite flag at the end of the corresponding row " "in the dictionary file. If using the Camembert model, please " "download an updated copy of the model file.".format(A ) ) self.add_symbol(A , n=A , overwrite=A ) except ValueError: raise ValueError("Incorrect dictionary format, expected '<token> <cnt> [flags]'" ) def UpperCamelCase_ ( _UpperCAmelCase : Tuple ) -> Any: """simple docstring""" _UpperCAmelCase : str = dict((re.sub(R"@@$" , "" , _UpperCAmelCase ), v) if k.endswith("@@" ) else (re.sub(R"$" , "</w>" , _UpperCAmelCase ), v) for k, v in d.items() ) _UpperCAmelCase : str = "<s> <pad> </s> <unk>".split() # restore the special tokens for k in keep_keys: del da[F"""{k}</w>"""] _UpperCAmelCase : Any = d[k] # restore return da def UpperCamelCase_ ( _UpperCAmelCase : Union[str, Any] , _UpperCAmelCase : Optional[Any] ) -> Optional[int]: """simple docstring""" if not os.path.exists(_UpperCAmelCase ): raise ValueError(F"""path {biogpt_checkpoint_path} does not exist!""" ) os.makedirs(_UpperCAmelCase , exist_ok=_UpperCAmelCase ) print(F"""Writing results to {pytorch_dump_folder_path}""" ) # handle various types of models _UpperCAmelCase : List[str] = os.path.join(_UpperCAmelCase , "checkpoint.pt" ) if not os.path.isfile(_UpperCAmelCase ): raise ValueError(F"""path to the file {checkpoint_file} does not exist!""" ) _UpperCAmelCase : List[str] = torch.load(_UpperCAmelCase , map_location="cpu" ) _UpperCAmelCase : Any = chkpt["cfg"]["model"] # dicts _UpperCAmelCase : Tuple = os.path.join(_UpperCAmelCase , "dict.txt" ) if not os.path.isfile(_UpperCAmelCase ): raise ValueError(F"""path to the file {dict_file} does not exist!""" ) _UpperCAmelCase : Any = Dictionary.load(_UpperCAmelCase ) _UpperCAmelCase : Dict = rewrite_dict_keys(src_dict.indices ) _UpperCAmelCase : Dict = len(_UpperCAmelCase ) _UpperCAmelCase : Any = os.path.join(_UpperCAmelCase , VOCAB_FILES_NAMES["vocab_file"] ) print(F"""Generating {src_vocab_file} of {src_vocab_size} records""" ) with open(_UpperCAmelCase , "w" , encoding="utf-8" ) as f: f.write(json.dumps(_UpperCAmelCase , ensure_ascii=_UpperCAmelCase , indent=_UpperCAmelCase ) ) # merges_file (bpecodes) _UpperCAmelCase : Tuple = os.path.join(_UpperCAmelCase , "bpecodes" ) if not os.path.isfile(_UpperCAmelCase ): raise ValueError(F"""path to the file {bpecodes_file} does not exist!""" ) _UpperCAmelCase : Optional[int] = os.path.join(_UpperCAmelCase , VOCAB_FILES_NAMES["merges_file"] ) shutil.copyfile(_UpperCAmelCase , _UpperCAmelCase ) # model config _UpperCAmelCase : Any = os.path.join(_UpperCAmelCase , "config.json" ) _UpperCAmelCase : Optional[int] = { "activation_dropout": args["activation_dropout"], "architectures": ["BioGptForCausalLM"], "attention_probs_dropout_prob": args["attention_dropout"], "bos_token_id": 0, "eos_token_id": 2, "hidden_act": args["activation_fn"], "hidden_dropout_prob": args["dropout"], "hidden_size": args["decoder_embed_dim"], "initializer_range": 0.0_2, "intermediate_size": args["decoder_ffn_embed_dim"], "layer_norm_eps": 1e-12, "layerdrop": args["decoder_layerdrop"], "max_position_embeddings": args["max_target_positions"], "model_type": "biogpt", "num_attention_heads": args["decoder_attention_heads"], "num_hidden_layers": args["decoder_layers"], "pad_token_id": 1, "scale_embedding": not args["no_scale_embedding"], "tie_word_embeddings": args["share_decoder_input_output_embed"], "vocab_size": src_vocab_size, } # good hparam defaults to start with print(F"""Generating {biogpt_model_config_file}""" ) with open(_UpperCAmelCase , "w" , encoding="utf-8" ) as f: f.write(json.dumps(_UpperCAmelCase , ensure_ascii=_UpperCAmelCase , indent=_UpperCAmelCase ) ) # tokenizer config _UpperCAmelCase : Tuple = os.path.join(_UpperCAmelCase , _UpperCAmelCase ) _UpperCAmelCase : List[Any] = { "bos_token": "<s>", "eos_token": "</s>", "model_max_length": 1_024, "pad_token": "<pad>", "special_tokens_map_file": None, "tokenizer_class": "BioGptTokenizer", "unk_token": "<unk>", } print(F"""Generating {biogpt_tokenizer_config_file}""" ) with open(_UpperCAmelCase , "w" , encoding="utf-8" ) as f: f.write(json.dumps(_UpperCAmelCase , ensure_ascii=_UpperCAmelCase , indent=_UpperCAmelCase ) ) # model _UpperCAmelCase : str = chkpt["model"] # remove unneeded keys _UpperCAmelCase : Optional[Any] = [ "decoder.version", ] for k in ignore_keys: model_state_dict.pop(_UpperCAmelCase , _UpperCAmelCase ) _UpperCAmelCase : Any = list(model_state_dict.keys() ) for layer_name in layer_names: if layer_name.endswith("output_projection.weight" ): _UpperCAmelCase : Optional[Any] = model_state_dict.pop(_UpperCAmelCase ) else: _UpperCAmelCase : Optional[Any] = model_state_dict.pop(_UpperCAmelCase ) _UpperCAmelCase : Any = BioGptConfig.from_pretrained(_UpperCAmelCase ) _UpperCAmelCase : str = BioGptForCausalLM(_UpperCAmelCase ) # check that it loads ok model_new.load_state_dict(_UpperCAmelCase ) # save _UpperCAmelCase : Tuple = os.path.join(_UpperCAmelCase , _UpperCAmelCase ) print(F"""Generating {pytorch_weights_dump_path}""" ) torch.save(_UpperCAmelCase , _UpperCAmelCase ) print("Conversion is done!" ) if __name__ == "__main__": __SCREAMING_SNAKE_CASE : Any = argparse.ArgumentParser() # Required parameters parser.add_argument( """--biogpt_checkpoint_path""", default=None, type=str, required=True, help=( """Path to the official PyTorch checkpoint file which is expected to reside in the dump dir with dicts,""" """ bpecodes, etc.""" ), ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, required=True, help="""Path to the output PyTorch model.""" ) __SCREAMING_SNAKE_CASE : List[str] = parser.parse_args() convert_biogpt_checkpoint_to_pytorch(args.biogpt_checkpoint_path, args.pytorch_dump_folder_path)
31
'''simple docstring''' 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_ (snake_case__ , snake_case__ ): '''simple docstring''' __UpperCamelCase: Optional[Any] = 1 @register_to_config def __init__( self : Optional[int] , A : int = 1000 , A : Optional[Union[np.ndarray, List[float]]] = None ): # set `betas`, `alphas`, `timesteps` self.set_timesteps(A ) # standard deviation of the initial noise distribution _UpperCAmelCase : int = 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. _UpperCAmelCase : int = 4 # running values _UpperCAmelCase : Dict = [] def _A ( self : Optional[int] , A : int , A : Union[str, torch.device] = None ): _UpperCAmelCase : int = num_inference_steps _UpperCAmelCase : Union[str, Any] = torch.linspace(1 , 0 , num_inference_steps + 1 )[:-1] _UpperCAmelCase : Any = torch.cat([steps, torch.tensor([0.0] )] ) if self.config.trained_betas is not None: _UpperCAmelCase : str = torch.tensor(self.config.trained_betas , dtype=torch.floataa ) else: _UpperCAmelCase : Dict = torch.sin(steps * math.pi / 2 ) ** 2 _UpperCAmelCase : List[Any] = (1.0 - self.betas**2) ** 0.5 _UpperCAmelCase : List[str] = (torch.atana(self.betas , self.alphas ) / math.pi * 2)[:-1] _UpperCAmelCase : Dict = timesteps.to(A ) _UpperCAmelCase : Dict = [] def _A ( self : Optional[int] , A : torch.FloatTensor , A : int , A : torch.FloatTensor , A : bool = True , ): 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" ) _UpperCAmelCase : Tuple = (self.timesteps == timestep).nonzero().item() _UpperCAmelCase : Optional[Any] = timestep_index + 1 _UpperCAmelCase : int = sample * self.betas[timestep_index] + model_output * self.alphas[timestep_index] self.ets.append(A ) if len(self.ets ) == 1: _UpperCAmelCase : List[Any] = self.ets[-1] elif len(self.ets ) == 2: _UpperCAmelCase : str = (3 * self.ets[-1] - self.ets[-2]) / 2 elif len(self.ets ) == 3: _UpperCAmelCase : Tuple = (23 * self.ets[-1] - 16 * self.ets[-2] + 5 * self.ets[-3]) / 12 else: _UpperCAmelCase : Union[str, Any] = (1 / 24) * (55 * self.ets[-1] - 59 * self.ets[-2] + 37 * self.ets[-3] - 9 * self.ets[-4]) _UpperCAmelCase : Union[str, Any] = self._get_prev_sample(A , A , A , A ) if not return_dict: return (prev_sample,) return SchedulerOutput(prev_sample=A ) def _A ( self : Union[str, Any] , A : torch.FloatTensor , *A : Union[str, Any] , **A : Dict ): return sample def _A ( self : Optional[Any] , A : Optional[int] , A : int , A : Optional[Any] , A : List[str] ): _UpperCAmelCase : List[str] = self.alphas[timestep_index] _UpperCAmelCase : List[Any] = self.betas[timestep_index] _UpperCAmelCase : Optional[Any] = self.alphas[prev_timestep_index] _UpperCAmelCase : Dict = self.betas[prev_timestep_index] _UpperCAmelCase : Tuple = (sample - sigma * ets) / max(A , 1E-8 ) _UpperCAmelCase : List[str] = next_alpha * pred + ets * next_sigma return prev_sample def __len__( self : Union[str, Any] ): return self.config.num_train_timesteps
31
1
'''simple docstring''' def UpperCamelCase_ ( _UpperCAmelCase : list , _UpperCAmelCase : int = 0 ) -> list: """simple docstring""" _UpperCAmelCase : str = length or len(_UpperCAmelCase ) _UpperCAmelCase : Tuple = False for i in range(length - 1 ): if list_data[i] > list_data[i + 1]: _UpperCAmelCase , _UpperCAmelCase : Union[str, Any] = list_data[i + 1], list_data[i] _UpperCAmelCase : str = True return list_data if not swapped else bubble_sort(_UpperCAmelCase , length - 1 ) if __name__ == "__main__": import doctest doctest.testmod()
31
'''simple docstring''' import numpy as np from matplotlib import pyplot as plt from sklearn.datasets import load_iris from sklearn.metrics import ConfusionMatrixDisplay from sklearn.model_selection import train_test_split from xgboost import XGBClassifier def UpperCamelCase_ ( _UpperCAmelCase : dict ) -> tuple: """simple docstring""" return (data["data"], data["target"]) def UpperCamelCase_ ( _UpperCAmelCase : np.ndarray , _UpperCAmelCase : np.ndarray ) -> XGBClassifier: """simple docstring""" _UpperCAmelCase : Any = XGBClassifier() classifier.fit(_UpperCAmelCase , _UpperCAmelCase ) return classifier def UpperCamelCase_ ( ) -> None: """simple docstring""" _UpperCAmelCase : List[str] = load_iris() _UpperCAmelCase , _UpperCAmelCase : Dict = data_handling(_UpperCAmelCase ) _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase : str = train_test_split( _UpperCAmelCase , _UpperCAmelCase , test_size=0.2_5 ) _UpperCAmelCase : Optional[Any] = iris["target_names"] # Create an XGBoost Classifier from the training data _UpperCAmelCase : Tuple = xgboost(_UpperCAmelCase , _UpperCAmelCase ) # Display the confusion matrix of the classifier with both training and test sets ConfusionMatrixDisplay.from_estimator( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , display_labels=_UpperCAmelCase , cmap="Blues" , normalize="true" , ) plt.title("Normalized Confusion Matrix - IRIS Dataset" ) plt.show() if __name__ == "__main__": import doctest doctest.testmod(verbose=True) main()
31
1
'''simple docstring''' import json import os from functools import lru_cache from typing import List, Optional, Tuple import regex as re from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging __SCREAMING_SNAKE_CASE : str = logging.get_logger(__name__) __SCREAMING_SNAKE_CASE : Optional[int] = {"""vocab_file""": """vocab.json""", """merges_file""": """merges.txt"""} __SCREAMING_SNAKE_CASE : Dict = { """vocab_file""": { """allenai/longformer-base-4096""": """https://huggingface.co/allenai/longformer-base-4096/resolve/main/vocab.json""", """allenai/longformer-large-4096""": ( """https://huggingface.co/allenai/longformer-large-4096/resolve/main/vocab.json""" ), """allenai/longformer-large-4096-finetuned-triviaqa""": ( """https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/vocab.json""" ), """allenai/longformer-base-4096-extra.pos.embd.only""": ( """https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/vocab.json""" ), """allenai/longformer-large-4096-extra.pos.embd.only""": ( """https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/vocab.json""" ), }, """merges_file""": { """allenai/longformer-base-4096""": """https://huggingface.co/allenai/longformer-base-4096/resolve/main/merges.txt""", """allenai/longformer-large-4096""": ( """https://huggingface.co/allenai/longformer-large-4096/resolve/main/merges.txt""" ), """allenai/longformer-large-4096-finetuned-triviaqa""": ( """https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/merges.txt""" ), """allenai/longformer-base-4096-extra.pos.embd.only""": ( """https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/merges.txt""" ), """allenai/longformer-large-4096-extra.pos.embd.only""": ( """https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/merges.txt""" ), }, } __SCREAMING_SNAKE_CASE : Tuple = { """allenai/longformer-base-4096""": 4_096, """allenai/longformer-large-4096""": 4_096, """allenai/longformer-large-4096-finetuned-triviaqa""": 4_096, """allenai/longformer-base-4096-extra.pos.embd.only""": 4_096, """allenai/longformer-large-4096-extra.pos.embd.only""": 4_096, } @lru_cache() # Copied from transformers.models.roberta.tokenization_roberta.bytes_to_unicode def UpperCamelCase_ ( ) -> Tuple: """simple docstring""" _UpperCAmelCase : Tuple = ( list(range(ord("!" ) , ord("~" ) + 1 ) ) + list(range(ord("¡" ) , ord("¬" ) + 1 ) ) + list(range(ord("®" ) , ord("ÿ" ) + 1 ) ) ) _UpperCAmelCase : Tuple = bs[:] _UpperCAmelCase : str = 0 for b in range(2**8 ): if b not in bs: bs.append(_UpperCAmelCase ) cs.append(2**8 + n ) n += 1 _UpperCAmelCase : Optional[int] = [chr(_UpperCAmelCase ) for n in cs] return dict(zip(_UpperCAmelCase , _UpperCAmelCase ) ) def UpperCamelCase_ ( _UpperCAmelCase : Any ) -> List[Any]: """simple docstring""" _UpperCAmelCase : Union[str, Any] = set() _UpperCAmelCase : int = word[0] for char in word[1:]: pairs.add((prev_char, char) ) _UpperCAmelCase : Dict = char return pairs class lowerCamelCase_ (snake_case__ ): '''simple docstring''' __UpperCamelCase: Any = VOCAB_FILES_NAMES __UpperCamelCase: str = PRETRAINED_VOCAB_FILES_MAP __UpperCamelCase: Optional[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCamelCase: Tuple = ["input_ids", "attention_mask"] def __init__( self : int , A : List[Any] , A : str , A : Union[str, Any]="replace" , A : Any="<s>" , A : Tuple="</s>" , A : Tuple="</s>" , A : Dict="<s>" , A : str="<unk>" , A : Dict="<pad>" , A : List[Any]="<mask>" , A : Union[str, Any]=False , **A : List[str] , ): _UpperCAmelCase : str = AddedToken(A , lstrip=A , rstrip=A ) if isinstance(A , A ) else bos_token _UpperCAmelCase : List[str] = AddedToken(A , lstrip=A , rstrip=A ) if isinstance(A , A ) else eos_token _UpperCAmelCase : Dict = AddedToken(A , lstrip=A , rstrip=A ) if isinstance(A , A ) else sep_token _UpperCAmelCase : Union[str, Any] = AddedToken(A , lstrip=A , rstrip=A ) if isinstance(A , A ) else cls_token _UpperCAmelCase : List[Any] = AddedToken(A , lstrip=A , rstrip=A ) if isinstance(A , A ) else unk_token _UpperCAmelCase : Optional[Any] = AddedToken(A , lstrip=A , rstrip=A ) if isinstance(A , A ) else pad_token # Mask token behave like a normal word, i.e. include the space before it _UpperCAmelCase : str = AddedToken(A , lstrip=A , rstrip=A ) if isinstance(A , A ) else mask_token super().__init__( errors=A , bos_token=A , eos_token=A , unk_token=A , sep_token=A , cls_token=A , pad_token=A , mask_token=A , add_prefix_space=A , **A , ) with open(A , encoding="utf-8" ) as vocab_handle: _UpperCAmelCase : Union[str, Any] = json.load(A ) _UpperCAmelCase : Any = {v: k for k, v in self.encoder.items()} _UpperCAmelCase : Tuple = errors # how to handle errors in decoding _UpperCAmelCase : Any = bytes_to_unicode() _UpperCAmelCase : Dict = {v: k for k, v in self.byte_encoder.items()} with open(A , encoding="utf-8" ) as merges_handle: _UpperCAmelCase : Any = merges_handle.read().split("\n" )[1:-1] _UpperCAmelCase : List[Any] = [tuple(merge.split() ) for merge in bpe_merges] _UpperCAmelCase : Any = dict(zip(A , range(len(A ) ) ) ) _UpperCAmelCase : Any = {} _UpperCAmelCase : List[Any] = add_prefix_space # Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions _UpperCAmelCase : Optional[Any] = re.compile(R"'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+" ) @property def _A ( self : Tuple ): return len(self.encoder ) def _A ( self : Optional[Any] ): return dict(self.encoder , **self.added_tokens_encoder ) def _A ( self : Any , A : Optional[int] ): if token in self.cache: return self.cache[token] _UpperCAmelCase : int = tuple(A ) _UpperCAmelCase : List[str] = get_pairs(A ) if not pairs: return token while True: _UpperCAmelCase : str = min(A , key=lambda A : self.bpe_ranks.get(A , float("inf" ) ) ) if bigram not in self.bpe_ranks: break _UpperCAmelCase , _UpperCAmelCase : Tuple = bigram _UpperCAmelCase : List[Any] = [] _UpperCAmelCase : Dict = 0 while i < len(A ): try: _UpperCAmelCase : str = word.index(A , A ) except ValueError: new_word.extend(word[i:] ) break else: new_word.extend(word[i:j] ) _UpperCAmelCase : Union[str, Any] = j if word[i] == first and i < len(A ) - 1 and word[i + 1] == second: new_word.append(first + second ) i += 2 else: new_word.append(word[i] ) i += 1 _UpperCAmelCase : Optional[Any] = tuple(A ) _UpperCAmelCase : List[Any] = new_word if len(A ) == 1: break else: _UpperCAmelCase : Any = get_pairs(A ) _UpperCAmelCase : Union[str, Any] = " ".join(A ) _UpperCAmelCase : str = word return word def _A ( self : str , A : Dict ): _UpperCAmelCase : Tuple = [] for token in re.findall(self.pat , A ): _UpperCAmelCase : Any = "".join( self.byte_encoder[b] for b in token.encode("utf-8" ) ) # Maps all our bytes to unicode strings, avoiding control tokens of the BPE (spaces in our case) bpe_tokens.extend(bpe_token for bpe_token in self.bpe(A ).split(" " ) ) return bpe_tokens def _A ( self : Any , A : Dict ): return self.encoder.get(A , self.encoder.get(self.unk_token ) ) def _A ( self : Tuple , A : Optional[Any] ): return self.decoder.get(A ) def _A ( self : Tuple , A : str ): _UpperCAmelCase : Optional[Any] = "".join(A ) _UpperCAmelCase : str = bytearray([self.byte_decoder[c] for c in text] ).decode("utf-8" , errors=self.errors ) return text def _A ( self : int , A : str , A : Optional[str] = None ): if not os.path.isdir(A ): logger.error(F"""Vocabulary path ({save_directory}) should be a directory""" ) return _UpperCAmelCase : str = os.path.join( A , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) _UpperCAmelCase : int = os.path.join( A , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["merges_file"] ) with open(A , "w" , encoding="utf-8" ) as f: f.write(json.dumps(self.encoder , indent=2 , sort_keys=A , ensure_ascii=A ) + "\n" ) _UpperCAmelCase : Optional[Any] = 0 with open(A , "w" , encoding="utf-8" ) as writer: writer.write("#version: 0.2\n" ) for bpe_tokens, token_index in sorted(self.bpe_ranks.items() , key=lambda A : kv[1] ): if index != token_index: logger.warning( F"""Saving vocabulary to {merge_file}: BPE merge indices are not consecutive.""" " Please check that the tokenizer is not corrupted!" ) _UpperCAmelCase : Any = token_index writer.write(" ".join(A ) + "\n" ) index += 1 return vocab_file, merge_file def _A ( self : List[Any] , A : List[int] , A : Optional[List[int]] = None ): if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] _UpperCAmelCase : int = [self.cls_token_id] _UpperCAmelCase : Tuple = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def _A ( self : Optional[int] , A : List[int] , A : Optional[List[int]] = None , A : bool = False ): if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=A , token_ids_a=A , already_has_special_tokens=A ) if token_ids_a is None: return [1] + ([0] * len(A )) + [1] return [1] + ([0] * len(A )) + [1, 1] + ([0] * len(A )) + [1] def _A ( self : Union[str, Any] , A : List[int] , A : Optional[List[int]] = None ): _UpperCAmelCase : int = [self.sep_token_id] _UpperCAmelCase : int = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def _A ( self : List[str] , A : Optional[int] , A : Optional[Any]=False , **A : List[str] ): _UpperCAmelCase : Optional[int] = kwargs.pop("add_prefix_space" , self.add_prefix_space ) if (is_split_into_words or add_prefix_space) and (len(A ) > 0 and not text[0].isspace()): _UpperCAmelCase : Union[str, Any] = " " + text return (text, kwargs)
31
'''simple docstring''' import math import unittest from transformers import BioGptConfig, 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, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( BioGptForCausalLM, BioGptForSequenceClassification, BioGptForTokenClassification, BioGptModel, BioGptTokenizer, ) from transformers.models.biogpt.modeling_biogpt import BIOGPT_PRETRAINED_MODEL_ARCHIVE_LIST class lowerCamelCase_ : '''simple docstring''' def __init__( self : List[Any] , A : Dict , A : Optional[Any]=13 , A : Optional[Any]=7 , A : Union[str, Any]=True , A : Optional[Any]=True , A : int=False , A : str=True , A : Optional[Any]=99 , A : Union[str, Any]=32 , A : int=5 , A : Tuple=4 , A : Union[str, Any]=37 , A : Dict="gelu" , A : Union[str, Any]=0.1 , A : str=0.1 , A : Union[str, Any]=512 , A : int=16 , A : List[str]=2 , A : Tuple=0.02 , A : int=3 , A : List[str]=4 , A : str=None , ): _UpperCAmelCase : List[Any] = parent _UpperCAmelCase : Any = batch_size _UpperCAmelCase : int = seq_length _UpperCAmelCase : Union[str, Any] = is_training _UpperCAmelCase : Any = use_input_mask _UpperCAmelCase : Optional[Any] = use_token_type_ids _UpperCAmelCase : str = use_labels _UpperCAmelCase : Union[str, Any] = vocab_size _UpperCAmelCase : Tuple = hidden_size _UpperCAmelCase : Union[str, Any] = num_hidden_layers _UpperCAmelCase : Optional[Any] = num_attention_heads _UpperCAmelCase : Union[str, Any] = intermediate_size _UpperCAmelCase : Union[str, Any] = hidden_act _UpperCAmelCase : List[Any] = hidden_dropout_prob _UpperCAmelCase : List[Any] = attention_probs_dropout_prob _UpperCAmelCase : Optional[int] = max_position_embeddings _UpperCAmelCase : str = type_vocab_size _UpperCAmelCase : str = type_sequence_label_size _UpperCAmelCase : int = initializer_range _UpperCAmelCase : Optional[Any] = num_labels _UpperCAmelCase : List[str] = num_choices _UpperCAmelCase : List[str] = scope def _A ( self : Optional[int] ): _UpperCAmelCase : Dict = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _UpperCAmelCase : Union[str, Any] = None if self.use_input_mask: _UpperCAmelCase : List[Any] = random_attention_mask([self.batch_size, self.seq_length] ) _UpperCAmelCase : Any = None if self.use_token_type_ids: _UpperCAmelCase : List[str] = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) _UpperCAmelCase : Optional[int] = None _UpperCAmelCase : Any = None _UpperCAmelCase : Optional[int] = None if self.use_labels: _UpperCAmelCase : Optional[Any] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _UpperCAmelCase : Tuple = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) _UpperCAmelCase : List[str] = ids_tensor([self.batch_size] , self.num_choices ) _UpperCAmelCase : List[Any] = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def _A ( self : Dict ): return BioGptConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=A , initializer_range=self.initializer_range , ) def _A ( self : int , A : List[Any] , A : Any , A : int , A : Union[str, Any] , A : Dict , A : List[Any] , A : Dict ): _UpperCAmelCase : List[str] = BioGptModel(config=A ) model.to(A ) model.eval() _UpperCAmelCase : Tuple = model(A , attention_mask=A ) _UpperCAmelCase : int = model(A ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _A ( self : List[Any] , A : str , A : List[Any] , A : Dict , A : List[Any] , A : List[str] , A : Union[str, Any] , A : int , A : List[str] , A : Dict , ): _UpperCAmelCase : Optional[int] = BioGptForCausalLM(config=A ) model.to(A ) model.eval() _UpperCAmelCase : Optional[int] = model(A , attention_mask=A , token_type_ids=A , labels=A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def _A ( self : List[Any] , A : str , A : str , A : str , A : Any , A : List[str] , *A : Optional[int] ): _UpperCAmelCase : str = BioGptModel(config=A ) model.to(A ) model.eval() # create attention mask _UpperCAmelCase : List[Any] = torch.ones(input_ids.shape , dtype=torch.long , device=A ) _UpperCAmelCase : Optional[int] = self.seq_length // 2 _UpperCAmelCase : List[Any] = 0 # first forward pass _UpperCAmelCase , _UpperCAmelCase : List[str] = model(A , attention_mask=A ).to_tuple() # create hypothetical next token and extent to next_input_ids _UpperCAmelCase : List[str] = ids_tensor((self.batch_size, 1) , config.vocab_size ) # change a random masked slice from input_ids _UpperCAmelCase : List[str] = ids_tensor((1,) , A ).item() + 1 _UpperCAmelCase : str = ids_tensor((self.batch_size, 1) , config.vocab_size ).squeeze(-1 ) _UpperCAmelCase : Any = random_other_next_tokens # append to next input_ids and attn_mask _UpperCAmelCase : List[Any] = torch.cat([input_ids, next_tokens] , dim=-1 ) _UpperCAmelCase : Optional[int] = torch.cat( [attn_mask, torch.ones((attn_mask.shape[0], 1) , dtype=torch.long , device=A )] , dim=1 , ) # get two different outputs _UpperCAmelCase : List[Any] = model(A , attention_mask=A )["last_hidden_state"] _UpperCAmelCase : Optional[Any] = model(A , past_key_values=A , attention_mask=A )["last_hidden_state"] # select random slice _UpperCAmelCase : Any = ids_tensor((1,) , output_from_past.shape[-1] ).item() _UpperCAmelCase : Optional[Any] = output_from_no_past[:, -1, random_slice_idx].detach() _UpperCAmelCase : Any = output_from_past[:, 0, random_slice_idx].detach() # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(A , A , atol=1E-3 ) ) def _A ( self : int , A : Dict , A : str , A : Dict , A : Union[str, Any] , A : Any , *A : Union[str, Any] ): _UpperCAmelCase : Optional[Any] = BioGptModel(config=A ).to(A ).eval() _UpperCAmelCase : List[Any] = torch.ones(input_ids.shape , dtype=torch.long , device=A ) # first forward pass _UpperCAmelCase : Union[str, Any] = model(A , attention_mask=A , use_cache=A ) _UpperCAmelCase , _UpperCAmelCase : Dict = outputs.to_tuple() # create hypothetical multiple next token and extent to next_input_ids _UpperCAmelCase : str = ids_tensor((self.batch_size, 3) , config.vocab_size ) _UpperCAmelCase : Any = ids_tensor((self.batch_size, 3) , 2 ) # append to next input_ids and _UpperCAmelCase : Optional[Any] = torch.cat([input_ids, next_tokens] , dim=-1 ) _UpperCAmelCase : Dict = torch.cat([attention_mask, next_attn_mask] , dim=-1 ) _UpperCAmelCase : Any = model(A , attention_mask=A )["last_hidden_state"] _UpperCAmelCase : Dict = model(A , attention_mask=A , past_key_values=A )[ "last_hidden_state" ] # select random slice _UpperCAmelCase : Dict = ids_tensor((1,) , output_from_past.shape[-1] ).item() _UpperCAmelCase : Dict = output_from_no_past[:, -3:, random_slice_idx].detach() _UpperCAmelCase : 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(A , A , atol=1E-3 ) ) def _A ( self : Optional[Any] , A : Tuple , A : List[str] , A : Tuple , A : Dict , A : List[Any] , *A : Tuple , A : List[str]=False ): _UpperCAmelCase : Optional[int] = BioGptForCausalLM(A ) model.to(A ) if gradient_checkpointing: model.gradient_checkpointing_enable() _UpperCAmelCase : Union[str, Any] = model(A , labels=A ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) result.loss.backward() def _A ( self : Optional[Any] , A : Any , *A : Optional[Any] ): _UpperCAmelCase : Tuple = BioGptModel(A ) _UpperCAmelCase : int = model.config.initializer_range / math.sqrt(2 * model.config.num_hidden_layers ) for key in model.state_dict().keys(): if "c_proj" in key and "weight" in key: self.parent.assertLessEqual(abs(torch.std(model.state_dict()[key] ) - model_std ) , 0.001 ) self.parent.assertLessEqual(abs(torch.mean(model.state_dict()[key] ) - 0.0 ) , 0.01 ) def _A ( self : Optional[int] , A : Dict , A : Tuple , A : Optional[int] , A : int , A : List[str] , *A : Dict ): _UpperCAmelCase : Any = self.num_labels _UpperCAmelCase : Any = BioGptForTokenClassification(A ) model.to(A ) model.eval() _UpperCAmelCase : Optional[int] = model(A , attention_mask=A , token_type_ids=A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def _A ( self : int ): _UpperCAmelCase : Dict = self.prepare_config_and_inputs() ( ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ( _UpperCAmelCase ) , ) : List[str] = config_and_inputs _UpperCAmelCase : Optional[int] = {"input_ids": input_ids, "attention_mask": input_mask} return config, inputs_dict @require_torch class lowerCamelCase_ (snake_case__ , snake_case__ , snake_case__ , unittest.TestCase ): '''simple docstring''' __UpperCamelCase: List[str] = ( (BioGptModel, BioGptForCausalLM, BioGptForSequenceClassification, BioGptForTokenClassification) if is_torch_available() else () ) __UpperCamelCase: List[str] = (BioGptForCausalLM,) if is_torch_available() else () __UpperCamelCase: str = ( { "feature-extraction": BioGptModel, "text-classification": BioGptForSequenceClassification, "text-generation": BioGptForCausalLM, "token-classification": BioGptForTokenClassification, "zero-shot": BioGptForSequenceClassification, } if is_torch_available() else {} ) __UpperCamelCase: Union[str, Any] = False def _A ( self : Optional[Any] ): _UpperCAmelCase : List[Any] = BioGptModelTester(self ) _UpperCAmelCase : str = ConfigTester(self , config_class=A , hidden_size=37 ) def _A ( self : Union[str, Any] ): self.config_tester.run_common_tests() def _A ( self : Any ): _UpperCAmelCase : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*A ) def _A ( self : Any ): _UpperCAmelCase : Optional[int] = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: _UpperCAmelCase : Tuple = type self.model_tester.create_and_check_model(*A ) def _A ( self : int ): _UpperCAmelCase : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_biogpt_model_attention_mask_past(*A ) def _A ( self : Union[str, Any] ): _UpperCAmelCase : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_forward_and_backwards(*A , gradient_checkpointing=A ) def _A ( self : Union[str, Any] ): _UpperCAmelCase : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_biogpt_model_past_large_inputs(*A ) def _A ( self : Dict ): _UpperCAmelCase : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_biogpt_weight_initialization(*A ) def _A ( self : Dict ): _UpperCAmelCase : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_biogpt_for_token_classification(*A ) @slow def _A ( self : List[str] ): _UpperCAmelCase : Optional[Any] = BioGptForCausalLM.from_pretrained("microsoft/biogpt" ) model.to(A ) _UpperCAmelCase : Tuple = BioGptTokenizer.from_pretrained("microsoft/biogpt" ) _UpperCAmelCase : str = "left" # Define PAD Token = EOS Token = 50256 _UpperCAmelCase : Any = tokenizer.eos_token _UpperCAmelCase : int = model.config.eos_token_id # use different length sentences to test batching _UpperCAmelCase : Any = [ "Hello, my dog is a little", "Today, I", ] _UpperCAmelCase : Tuple = tokenizer(A , return_tensors="pt" , padding=A ) _UpperCAmelCase : Optional[Any] = inputs["input_ids"].to(A ) _UpperCAmelCase : Any = model.generate( input_ids=A , attention_mask=inputs["attention_mask"].to(A ) , ) _UpperCAmelCase : int = tokenizer(sentences[0] , return_tensors="pt" ).input_ids.to(A ) _UpperCAmelCase : List[Any] = model.generate(input_ids=A ) _UpperCAmelCase : List[Any] = inputs_non_padded.shape[-1] - inputs["attention_mask"][-1].long().sum().cpu().item() _UpperCAmelCase : int = tokenizer(sentences[1] , return_tensors="pt" ).input_ids.to(A ) _UpperCAmelCase : int = model.generate(input_ids=A , max_length=model.config.max_length - num_paddings ) _UpperCAmelCase : Dict = tokenizer.batch_decode(A , skip_special_tokens=A ) _UpperCAmelCase : Any = tokenizer.decode(output_non_padded[0] , skip_special_tokens=A ) _UpperCAmelCase : Optional[int] = tokenizer.decode(output_padded[0] , skip_special_tokens=A ) _UpperCAmelCase : str = [ "Hello, my dog is a little bit bigger than a little bit.", "Today, I have a good idea of how to use the information", ] self.assertListEqual(A , A ) self.assertListEqual(A , [non_padded_sentence, padded_sentence] ) @slow def _A ( self : str ): for model_name in BIOGPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _UpperCAmelCase : Optional[Any] = BioGptModel.from_pretrained(A ) self.assertIsNotNone(A ) def _A ( self : List[str] ): _UpperCAmelCase , _UpperCAmelCase : int = self.model_tester.prepare_config_and_inputs_for_common() _UpperCAmelCase : str = 3 _UpperCAmelCase : List[str] = input_dict["input_ids"] _UpperCAmelCase : Dict = input_ids.ne(1 ).to(A ) _UpperCAmelCase : List[Any] = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) _UpperCAmelCase : List[str] = BioGptForSequenceClassification(A ) model.to(A ) model.eval() _UpperCAmelCase : List[str] = model(A , attention_mask=A , labels=A ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def _A ( self : int ): _UpperCAmelCase , _UpperCAmelCase : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() _UpperCAmelCase : int = 3 _UpperCAmelCase : Dict = "multi_label_classification" _UpperCAmelCase : Optional[Any] = input_dict["input_ids"] _UpperCAmelCase : Optional[int] = input_ids.ne(1 ).to(A ) _UpperCAmelCase : Tuple = ids_tensor( [self.model_tester.batch_size, config.num_labels] , self.model_tester.type_sequence_label_size ).to(torch.float ) _UpperCAmelCase : Optional[Any] = BioGptForSequenceClassification(A ) model.to(A ) model.eval() _UpperCAmelCase : Tuple = model(A , attention_mask=A , labels=A ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) @require_torch class lowerCamelCase_ (unittest.TestCase ): '''simple docstring''' @slow def _A ( self : List[Any] ): _UpperCAmelCase : Optional[Any] = BioGptForCausalLM.from_pretrained("microsoft/biogpt" ) _UpperCAmelCase : List[str] = torch.tensor([[2, 4805, 9, 656, 21]] ) _UpperCAmelCase : List[Any] = model(A )[0] _UpperCAmelCase : int = 42384 _UpperCAmelCase : int = torch.Size((1, 5, vocab_size) ) self.assertEqual(output.shape , A ) _UpperCAmelCase : Any = torch.tensor( [[[-9.5_236, -9.8_918, 10.4_557], [-11.0_469, -9.6_423, 8.1_022], [-8.8_664, -7.8_826, 5.5_325]]] ) self.assertTrue(torch.allclose(output[:, :3, :3] , A , atol=1E-4 ) ) @slow def _A ( self : Any ): _UpperCAmelCase : str = BioGptTokenizer.from_pretrained("microsoft/biogpt" ) _UpperCAmelCase : Tuple = BioGptForCausalLM.from_pretrained("microsoft/biogpt" ) model.to(A ) torch.manual_seed(0 ) _UpperCAmelCase : Optional[Any] = tokenizer("COVID-19 is" , return_tensors="pt" ).to(A ) _UpperCAmelCase : Dict = model.generate( **A , min_length=100 , max_length=1024 , num_beams=5 , early_stopping=A , ) _UpperCAmelCase : Optional[Any] = tokenizer.decode(output_ids[0] , skip_special_tokens=A ) _UpperCAmelCase : List[str] = ( "COVID-19 is a global pandemic caused by severe acute respiratory syndrome coronavirus 2 (SARS-CoV-2), the" " causative agent of coronavirus disease 2019 (COVID-19), which has spread to more than 200 countries and" " territories, including the United States (US), Canada, Australia, New Zealand, the United Kingdom (UK)," " and the United States of America (USA), as of March 11, 2020, with more than 800,000 confirmed cases and" " more than 800,000 deaths." ) self.assertEqual(A , A )
31
1
'''simple docstring''' from __future__ import annotations from fractions import Fraction from math import gcd, sqrt def UpperCamelCase_ ( _UpperCAmelCase : int ) -> bool: """simple docstring""" _UpperCAmelCase : int = int(number**0.5 ) return number == sq * sq def UpperCamelCase_ ( _UpperCAmelCase : int , _UpperCAmelCase : int , _UpperCAmelCase : int , _UpperCAmelCase : int , _UpperCAmelCase : int , _UpperCAmelCase : int ) -> tuple[int, int]: """simple docstring""" _UpperCAmelCase : int = x_num * y_den * z_den + y_num * x_den * z_den + z_num * x_den * y_den _UpperCAmelCase : int = x_den * y_den * z_den _UpperCAmelCase : int = gcd(_UpperCAmelCase , _UpperCAmelCase ) top //= hcf bottom //= hcf return top, bottom def UpperCamelCase_ ( _UpperCAmelCase : int = 35 ) -> int: """simple docstring""" _UpperCAmelCase : set = set() _UpperCAmelCase : int _UpperCAmelCase : Fraction = Fraction(0 ) _UpperCAmelCase : tuple[int, int] for x_num in range(1 , order + 1 ): for x_den in range(x_num + 1 , order + 1 ): for y_num in range(1 , order + 1 ): for y_den in range(y_num + 1 , order + 1 ): # n=1 _UpperCAmelCase : Dict = x_num * y_den + x_den * y_num _UpperCAmelCase : Any = x_den * y_den _UpperCAmelCase : List[str] = gcd(_UpperCAmelCase , _UpperCAmelCase ) z_num //= hcf z_den //= hcf if 0 < z_num < z_den <= order: _UpperCAmelCase : Optional[Any] = add_three( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) unique_s.add(_UpperCAmelCase ) # n=2 _UpperCAmelCase : int = ( x_num * x_num * y_den * y_den + x_den * x_den * y_num * y_num ) _UpperCAmelCase : int = x_den * x_den * y_den * y_den if is_sq(_UpperCAmelCase ) and is_sq(_UpperCAmelCase ): _UpperCAmelCase : List[str] = int(sqrt(_UpperCAmelCase ) ) _UpperCAmelCase : List[str] = int(sqrt(_UpperCAmelCase ) ) _UpperCAmelCase : Tuple = gcd(_UpperCAmelCase , _UpperCAmelCase ) z_num //= hcf z_den //= hcf if 0 < z_num < z_den <= order: _UpperCAmelCase : List[Any] = add_three( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) unique_s.add(_UpperCAmelCase ) # n=-1 _UpperCAmelCase : Dict = x_num * y_num _UpperCAmelCase : str = x_den * y_num + x_num * y_den _UpperCAmelCase : Dict = gcd(_UpperCAmelCase , _UpperCAmelCase ) z_num //= hcf z_den //= hcf if 0 < z_num < z_den <= order: _UpperCAmelCase : Optional[Any] = add_three( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) unique_s.add(_UpperCAmelCase ) # n=2 _UpperCAmelCase : Any = x_num * x_num * y_num * y_num _UpperCAmelCase : List[Any] = ( x_den * x_den * y_num * y_num + x_num * x_num * y_den * y_den ) if is_sq(_UpperCAmelCase ) and is_sq(_UpperCAmelCase ): _UpperCAmelCase : int = int(sqrt(_UpperCAmelCase ) ) _UpperCAmelCase : Dict = int(sqrt(_UpperCAmelCase ) ) _UpperCAmelCase : Dict = gcd(_UpperCAmelCase , _UpperCAmelCase ) z_num //= hcf z_den //= hcf if 0 < z_num < z_den <= order: _UpperCAmelCase : str = add_three( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) unique_s.add(_UpperCAmelCase ) for num, den in unique_s: total += Fraction(_UpperCAmelCase , _UpperCAmelCase ) return total.denominator + total.numerator if __name__ == "__main__": print(F'{solution() = }')
31
'''simple docstring''' __SCREAMING_SNAKE_CASE : Dict = 8.3_1_4_4_6_2 # Unit - J mol-1 K-1 def UpperCamelCase_ ( _UpperCAmelCase : float , _UpperCAmelCase : float , _UpperCAmelCase : float ) -> float: """simple docstring""" if moles < 0 or kelvin < 0 or volume < 0: raise ValueError("Invalid inputs. Enter positive value." ) return moles * kelvin * UNIVERSAL_GAS_CONSTANT / volume def UpperCamelCase_ ( _UpperCAmelCase : float , _UpperCAmelCase : float , _UpperCAmelCase : float ) -> float: """simple docstring""" if moles < 0 or kelvin < 0 or pressure < 0: raise ValueError("Invalid inputs. Enter positive value." ) return moles * kelvin * UNIVERSAL_GAS_CONSTANT / pressure if __name__ == "__main__": from doctest import testmod testmod()
31
1
'''simple docstring''' import unittest from transformers import ( MODEL_FOR_CAUSAL_LM_MAPPING, TF_MODEL_FOR_CAUSAL_LM_MAPPING, TextGenerationPipeline, logging, pipeline, ) from transformers.testing_utils import ( CaptureLogger, is_pipeline_test, require_accelerate, require_tf, require_torch, require_torch_gpu, require_torch_or_tf, ) from .test_pipelines_common import ANY @is_pipeline_test @require_torch_or_tf class lowerCamelCase_ (unittest.TestCase ): '''simple docstring''' __UpperCamelCase: Dict = MODEL_FOR_CAUSAL_LM_MAPPING __UpperCamelCase: Optional[Any] = TF_MODEL_FOR_CAUSAL_LM_MAPPING @require_torch def _A ( self : Tuple ): _UpperCAmelCase : Union[str, Any] = pipeline(task="text-generation" , model="sshleifer/tiny-ctrl" , framework="pt" ) # Using `do_sample=False` to force deterministic output _UpperCAmelCase : Optional[int] = text_generator("This is a test" , do_sample=A ) self.assertEqual( A , [ { "generated_text": ( "This is a test ☃ ☃ segmental segmental segmental 议议eski eski flutter flutter Lacy oscope." " oscope. FiliFili@@" ) } ] , ) _UpperCAmelCase : Dict = text_generator(["This is a test", "This is a second test"] ) self.assertEqual( A , [ [ { "generated_text": ( "This is a test ☃ ☃ segmental segmental segmental 议议eski eski flutter flutter Lacy oscope." " oscope. FiliFili@@" ) } ], [ { "generated_text": ( "This is a second test ☃ segmental segmental segmental 议议eski eski flutter flutter Lacy" " oscope. oscope. FiliFili@@" ) } ], ] , ) _UpperCAmelCase : Any = text_generator("This is a test" , do_sample=A , num_return_sequences=2 , return_tensors=A ) self.assertEqual( A , [ {"generated_token_ids": ANY(A )}, {"generated_token_ids": ANY(A )}, ] , ) _UpperCAmelCase : Any = text_generator.model.config.eos_token_id _UpperCAmelCase : List[Any] = "<pad>" _UpperCAmelCase : Dict = text_generator( ["This is a test", "This is a second test"] , do_sample=A , num_return_sequences=2 , batch_size=2 , return_tensors=A , ) self.assertEqual( A , [ [ {"generated_token_ids": ANY(A )}, {"generated_token_ids": ANY(A )}, ], [ {"generated_token_ids": ANY(A )}, {"generated_token_ids": ANY(A )}, ], ] , ) @require_tf def _A ( self : Union[str, Any] ): _UpperCAmelCase : int = pipeline(task="text-generation" , model="sshleifer/tiny-ctrl" , framework="tf" ) # Using `do_sample=False` to force deterministic output _UpperCAmelCase : int = text_generator("This is a test" , do_sample=A ) self.assertEqual( A , [ { "generated_text": ( "This is a test FeyFeyFey(Croatis.), s.), Cannes Cannes Cannes 閲閲Cannes Cannes Cannes 攵" " please," ) } ] , ) _UpperCAmelCase : List[str] = text_generator(["This is a test", "This is a second test"] , do_sample=A ) self.assertEqual( A , [ [ { "generated_text": ( "This is a test FeyFeyFey(Croatis.), s.), Cannes Cannes Cannes 閲閲Cannes Cannes Cannes 攵" " please," ) } ], [ { "generated_text": ( "This is a second test Chieftain Chieftain prefecture prefecture prefecture Cannes Cannes" " Cannes 閲閲Cannes Cannes Cannes 攵 please," ) } ], ] , ) def _A ( self : Any , A : List[str] , A : List[str] , A : Union[str, Any] ): _UpperCAmelCase : Optional[Any] = TextGenerationPipeline(model=A , tokenizer=A ) return text_generator, ["This is a test", "Another test"] def _A ( self : Any ): _UpperCAmelCase : int = "Hello I believe in" _UpperCAmelCase : int = pipeline("text-generation" , model="hf-internal-testing/tiny-random-gpt2" ) _UpperCAmelCase : Optional[int] = text_generator(A ) self.assertEqual( A , [{"generated_text": "Hello I believe in fe fe fe fe fe fe fe fe fe fe fe fe"}] , ) _UpperCAmelCase : List[str] = text_generator(A , stop_sequence=" fe" ) self.assertEqual(A , [{"generated_text": "Hello I believe in fe"}] ) def _A ( self : Optional[int] , A : Optional[Any] , A : List[Any] ): _UpperCAmelCase : List[str] = text_generator.model _UpperCAmelCase : str = text_generator.tokenizer _UpperCAmelCase : Dict = text_generator("This is a test" ) self.assertEqual(A , [{"generated_text": ANY(A )}] ) self.assertTrue(outputs[0]["generated_text"].startswith("This is a test" ) ) _UpperCAmelCase : Dict = text_generator("This is a test" , return_full_text=A ) self.assertEqual(A , [{"generated_text": ANY(A )}] ) self.assertNotIn("This is a test" , outputs[0]["generated_text"] ) _UpperCAmelCase : int = pipeline(task="text-generation" , model=A , tokenizer=A , return_full_text=A ) _UpperCAmelCase : Optional[Any] = text_generator("This is a test" ) self.assertEqual(A , [{"generated_text": ANY(A )}] ) self.assertNotIn("This is a test" , outputs[0]["generated_text"] ) _UpperCAmelCase : str = text_generator("This is a test" , return_full_text=A ) self.assertEqual(A , [{"generated_text": ANY(A )}] ) self.assertTrue(outputs[0]["generated_text"].startswith("This is a test" ) ) _UpperCAmelCase : Dict = text_generator(["This is great !", "Something else"] , num_return_sequences=2 , do_sample=A ) self.assertEqual( A , [ [{"generated_text": ANY(A )}, {"generated_text": ANY(A )}], [{"generated_text": ANY(A )}, {"generated_text": ANY(A )}], ] , ) if text_generator.tokenizer.pad_token is not None: _UpperCAmelCase : Optional[Any] = text_generator( ["This is great !", "Something else"] , num_return_sequences=2 , batch_size=2 , do_sample=A ) self.assertEqual( A , [ [{"generated_text": ANY(A )}, {"generated_text": ANY(A )}], [{"generated_text": ANY(A )}, {"generated_text": ANY(A )}], ] , ) with self.assertRaises(A ): _UpperCAmelCase : List[str] = text_generator("test" , return_full_text=A , return_text=A ) with self.assertRaises(A ): _UpperCAmelCase : Tuple = text_generator("test" , return_full_text=A , return_tensors=A ) with self.assertRaises(A ): _UpperCAmelCase : List[str] = text_generator("test" , return_text=A , return_tensors=A ) # Empty prompt is slighly special # it requires BOS token to exist. # Special case for Pegasus which will always append EOS so will # work even without BOS. if ( text_generator.tokenizer.bos_token_id is not None or "Pegasus" in tokenizer.__class__.__name__ or "Git" in model.__class__.__name__ ): _UpperCAmelCase : Any = text_generator("" ) self.assertEqual(A , [{"generated_text": ANY(A )}] ) else: with self.assertRaises((ValueError, AssertionError) ): _UpperCAmelCase : Dict = text_generator("" ) if text_generator.framework == "tf": # TF generation does not support max_new_tokens, and it's impossible # to control long generation with only max_length without # fancy calculation, dismissing tests for now. return # We don't care about infinite range models. # They already work. # Skip this test for XGLM, since it uses sinusoidal positional embeddings which are resized on-the-fly. _UpperCAmelCase : Any = ["RwkvForCausalLM", "XGLMForCausalLM", "GPTNeoXForCausalLM"] if ( tokenizer.model_max_length < 10000 and text_generator.model.__class__.__name__ not in EXTRA_MODELS_CAN_HANDLE_LONG_INPUTS ): # Handling of large generations with self.assertRaises((RuntimeError, IndexError, ValueError, AssertionError) ): text_generator("This is a test" * 500 , max_new_tokens=20 ) _UpperCAmelCase : Any = text_generator("This is a test" * 500 , handle_long_generation="hole" , max_new_tokens=20 ) # Hole strategy cannot work with self.assertRaises(A ): text_generator( "This is a test" * 500 , handle_long_generation="hole" , max_new_tokens=tokenizer.model_max_length + 10 , ) @require_torch @require_accelerate @require_torch_gpu def _A ( self : str ): import torch # Classic `model_kwargs` _UpperCAmelCase : Any = pipeline( model="hf-internal-testing/tiny-random-bloom" , model_kwargs={"device_map": "auto", "torch_dtype": torch.bfloataa} , ) self.assertEqual(pipe.model.device , torch.device(0 ) ) self.assertEqual(pipe.model.lm_head.weight.dtype , torch.bfloataa ) _UpperCAmelCase : List[str] = pipe("This is a test" ) self.assertEqual( A , [ { "generated_text": ( "This is a test test test test test test test test test test test test test test test test" " test" ) } ] , ) # Upgraded those two to real pipeline arguments (they just get sent for the model as they're unlikely to mean anything else.) _UpperCAmelCase : Tuple = pipeline(model="hf-internal-testing/tiny-random-bloom" , device_map="auto" , torch_dtype=torch.bfloataa ) self.assertEqual(pipe.model.device , torch.device(0 ) ) self.assertEqual(pipe.model.lm_head.weight.dtype , torch.bfloataa ) _UpperCAmelCase : List[Any] = pipe("This is a test" ) self.assertEqual( A , [ { "generated_text": ( "This is a test test test test test test test test test test test test test test test test" " test" ) } ] , ) # torch_dtype will be automatically set to float32 if not provided - check: https://github.com/huggingface/transformers/pull/20602 _UpperCAmelCase : Tuple = pipeline(model="hf-internal-testing/tiny-random-bloom" , device_map="auto" ) self.assertEqual(pipe.model.device , torch.device(0 ) ) self.assertEqual(pipe.model.lm_head.weight.dtype , torch.floataa ) _UpperCAmelCase : Optional[Any] = pipe("This is a test" ) self.assertEqual( A , [ { "generated_text": ( "This is a test test test test test test test test test test test test test test test test" " test" ) } ] , ) @require_torch @require_torch_gpu def _A ( self : Tuple ): import torch _UpperCAmelCase : Optional[int] = pipeline(model="hf-internal-testing/tiny-random-bloom" , device=0 , torch_dtype=torch.floataa ) pipe("This is a test" ) @require_torch @require_accelerate @require_torch_gpu def _A ( self : Optional[Any] ): import torch _UpperCAmelCase : int = pipeline(model="hf-internal-testing/tiny-random-bloom" , device_map="auto" , torch_dtype=torch.floataa ) pipe("This is a test" , do_sample=A , top_p=0.5 ) def _A ( self : str ): _UpperCAmelCase : Any = "Hello world" _UpperCAmelCase : Any = pipeline("text-generation" , model="hf-internal-testing/tiny-random-gpt2" ) if text_generator.model.framework == "tf": _UpperCAmelCase : Tuple = logging.get_logger("transformers.generation.tf_utils" ) else: _UpperCAmelCase : Optional[Any] = logging.get_logger("transformers.generation.utils" ) _UpperCAmelCase : int = "Both `max_new_tokens`" # The beggining of the message to be checked in this test # Both are set by the user -> log warning with CaptureLogger(A ) as cl: _UpperCAmelCase : int = text_generator(A , max_length=10 , max_new_tokens=1 ) self.assertIn(A , cl.out ) # The user only sets one -> no warning with CaptureLogger(A ) as cl: _UpperCAmelCase : Tuple = text_generator(A , max_new_tokens=1 ) self.assertNotIn(A , cl.out ) with CaptureLogger(A ) as cl: _UpperCAmelCase : List[str] = text_generator(A , max_length=10 ) self.assertNotIn(A , cl.out )
31
'''simple docstring''' import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_pegasus import PegasusTokenizer else: __SCREAMING_SNAKE_CASE : Optional[Any] = None __SCREAMING_SNAKE_CASE : Dict = logging.get_logger(__name__) __SCREAMING_SNAKE_CASE : Union[str, Any] = """▁""" __SCREAMING_SNAKE_CASE : str = {"""vocab_file""": """spiece.model""", """tokenizer_file""": """tokenizer.json"""} __SCREAMING_SNAKE_CASE : int = { """vocab_file""": {"""google/pegasus-xsum""": """https://huggingface.co/google/pegasus-xsum/resolve/main/spiece.model"""}, """tokenizer_file""": { """google/pegasus-xsum""": """https://huggingface.co/google/pegasus-xsum/resolve/main/tokenizer.json""" }, } __SCREAMING_SNAKE_CASE : str = { """google/pegasus-xsum""": 512, } class lowerCamelCase_ (snake_case__ ): '''simple docstring''' __UpperCamelCase: Optional[int] = VOCAB_FILES_NAMES __UpperCamelCase: Dict = PRETRAINED_VOCAB_FILES_MAP __UpperCamelCase: List[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCamelCase: Optional[int] = PegasusTokenizer __UpperCamelCase: Optional[Any] = ["input_ids", "attention_mask"] def __init__( self : Dict , A : List[str]=None , A : Union[str, Any]=None , A : Optional[int]="<pad>" , A : Tuple="</s>" , A : Union[str, Any]="<unk>" , A : Union[str, Any]="<mask_2>" , A : Dict="<mask_1>" , A : Union[str, Any]=None , A : int=103 , **A : Optional[Any] , ): _UpperCAmelCase : Dict = offset if additional_special_tokens is not None: if not isinstance(A , A ): raise TypeError( F"""additional_special_tokens should be of type {type(A )}, but is""" F""" {type(A )}""" ) _UpperCAmelCase : Optional[int] = ( ([mask_token_sent] + additional_special_tokens) if mask_token_sent not in additional_special_tokens and mask_token_sent is not None else additional_special_tokens ) # fill additional tokens with ..., <unk_token_102> in case not all additional tokens are already taken additional_special_tokens_extended += [ F"""<unk_{i}>""" for i in range(len(A ) , self.offset - 1 ) ] if len(set(A ) ) != len(A ): raise ValueError( "Please make sure that the provided additional_special_tokens do not contain an incorrectly" F""" shifted list of <unk_x> tokens. Found {additional_special_tokens_extended}.""" ) _UpperCAmelCase : Any = additional_special_tokens_extended else: _UpperCAmelCase : Dict = [mask_token_sent] if mask_token_sent is not None else [] additional_special_tokens += [F"""<unk_{i}>""" for i in range(2 , self.offset )] super().__init__( A , tokenizer_file=A , pad_token=A , eos_token=A , unk_token=A , mask_token=A , mask_token_sent=A , offset=A , additional_special_tokens=A , **A , ) _UpperCAmelCase : Optional[Any] = vocab_file _UpperCAmelCase : Optional[Any] = False if not self.vocab_file else True def _A ( self : List[str] , A : Optional[Any] ): _UpperCAmelCase : Any = set(self.all_special_ids ) # call it once instead of inside list comp all_special_ids.remove(self.unk_token_id ) # <unk> is only sometimes special if all_special_ids != set(range(len(self.additional_special_tokens ) + 3 ) ): raise ValueError( "There should be 3 special tokens: mask_token, pad_token, and eos_token +" F""" {len(self.additional_special_tokens )} additional_special_tokens, but got {all_special_ids}""" ) return [1 if x in all_special_ids else 0 for x in seq] def _A ( self : str , A : List , A : Optional[List] = None , A : bool = False ): if already_has_special_tokens: return self._special_token_mask(A ) elif token_ids_a is None: return self._special_token_mask(A ) + [1] else: return self._special_token_mask(token_ids_a + token_ids_a ) + [1] def _A ( self : Optional[int] , A : Union[str, Any] , A : int=None ): if token_ids_a is None: return token_ids_a + [self.eos_token_id] # We don't expect to process pairs, but leave the pair logic for API consistency return token_ids_a + token_ids_a + [self.eos_token_id] def _A ( self : Union[str, Any] , A : str , A : Optional[str] = None ): if not self.can_save_slow_tokenizer: raise ValueError( "Your fast tokenizer does not have the necessary information to save the vocabulary for a slow " "tokenizer." ) if not os.path.isdir(A ): logger.error(F"""Vocabulary path ({save_directory}) should be a directory""" ) return _UpperCAmelCase : List[Any] = os.path.join( A , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(A ): copyfile(self.vocab_file , A ) return (out_vocab_file,)
31
1
'''simple docstring''' import os import sys __SCREAMING_SNAKE_CASE : List[Any] = os.path.join(os.path.dirname(__file__), """src""") sys.path.append(SRC_DIR) from transformers import ( AutoConfig, AutoModel, AutoModelForCausalLM, AutoModelForMaskedLM, AutoModelForQuestionAnswering, AutoModelForSequenceClassification, AutoTokenizer, add_start_docstrings, ) __SCREAMING_SNAKE_CASE : List[str] = [ """torch""", """numpy""", """tokenizers""", """filelock""", """requests""", """tqdm""", """regex""", """sentencepiece""", """sacremoses""", """importlib_metadata""", """huggingface_hub""", ] @add_start_docstrings(AutoConfig.__doc__ ) def UpperCamelCase_ ( *_UpperCAmelCase : Dict , **_UpperCAmelCase : Union[str, Any] ) -> Union[str, Any]: """simple docstring""" return AutoConfig.from_pretrained(*_UpperCAmelCase , **_UpperCAmelCase ) @add_start_docstrings(AutoTokenizer.__doc__ ) def UpperCamelCase_ ( *_UpperCAmelCase : Union[str, Any] , **_UpperCAmelCase : str ) -> str: """simple docstring""" return AutoTokenizer.from_pretrained(*_UpperCAmelCase , **_UpperCAmelCase ) @add_start_docstrings(AutoModel.__doc__ ) def UpperCamelCase_ ( *_UpperCAmelCase : Any , **_UpperCAmelCase : Union[str, Any] ) -> str: """simple docstring""" return AutoModel.from_pretrained(*_UpperCAmelCase , **_UpperCAmelCase ) @add_start_docstrings(AutoModelForCausalLM.__doc__ ) def UpperCamelCase_ ( *_UpperCAmelCase : Tuple , **_UpperCAmelCase : Optional[int] ) -> Tuple: """simple docstring""" return AutoModelForCausalLM.from_pretrained(*_UpperCAmelCase , **_UpperCAmelCase ) @add_start_docstrings(AutoModelForMaskedLM.__doc__ ) def UpperCamelCase_ ( *_UpperCAmelCase : Union[str, Any] , **_UpperCAmelCase : List[Any] ) -> Any: """simple docstring""" return AutoModelForMaskedLM.from_pretrained(*_UpperCAmelCase , **_UpperCAmelCase ) @add_start_docstrings(AutoModelForSequenceClassification.__doc__ ) def UpperCamelCase_ ( *_UpperCAmelCase : Tuple , **_UpperCAmelCase : Any ) -> int: """simple docstring""" return AutoModelForSequenceClassification.from_pretrained(*_UpperCAmelCase , **_UpperCAmelCase ) @add_start_docstrings(AutoModelForQuestionAnswering.__doc__ ) def UpperCamelCase_ ( *_UpperCAmelCase : List[str] , **_UpperCAmelCase : List[Any] ) -> List[str]: """simple docstring""" return AutoModelForQuestionAnswering.from_pretrained(*_UpperCAmelCase , **_UpperCAmelCase )
31
'''simple docstring''' import shutil import tempfile import unittest from transformers import ( SPIECE_UNDERLINE, AddedToken, BatchEncoding, NllbTokenizer, NllbTokenizerFast, is_torch_available, ) from transformers.testing_utils import ( get_tests_dir, nested_simplify, require_sentencepiece, require_tokenizers, require_torch, ) from ...test_tokenization_common import TokenizerTesterMixin __SCREAMING_SNAKE_CASE : Union[str, Any] = get_tests_dir("""fixtures/test_sentencepiece.model""") if is_torch_available(): from transformers.models.mam_aaa.modeling_mam_aaa import shift_tokens_right __SCREAMING_SNAKE_CASE : Optional[int] = 256_047 __SCREAMING_SNAKE_CASE : Optional[int] = 256_145 @require_sentencepiece @require_tokenizers class lowerCamelCase_ (snake_case__ , unittest.TestCase ): '''simple docstring''' __UpperCamelCase: int = NllbTokenizer __UpperCamelCase: Tuple = NllbTokenizerFast __UpperCamelCase: Union[str, Any] = True __UpperCamelCase: Dict = True __UpperCamelCase: Optional[Any] = {} def _A ( self : Union[str, Any] ): super().setUp() # We have a SentencePiece fixture for testing _UpperCAmelCase : Tuple = NllbTokenizer(A , keep_accents=A ) tokenizer.save_pretrained(self.tmpdirname ) def _A ( self : Dict ): _UpperCAmelCase : Tuple = NllbTokenizer(A , keep_accents=A ) _UpperCAmelCase : Optional[Any] = tokenizer.tokenize("This is a test" ) self.assertListEqual(A , ["▁This", "▁is", "▁a", "▁t", "est"] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(A ) , [value + tokenizer.fairseq_offset for value in [285, 46, 10, 170, 382]] , ) _UpperCAmelCase : List[str] = tokenizer.tokenize("I was born in 92000, and this is falsé." ) self.assertListEqual( A , [ SPIECE_UNDERLINE + "I", SPIECE_UNDERLINE + "was", SPIECE_UNDERLINE + "b", "or", "n", SPIECE_UNDERLINE + "in", SPIECE_UNDERLINE + "", "9", "2", "0", "0", "0", ",", SPIECE_UNDERLINE + "and", SPIECE_UNDERLINE + "this", SPIECE_UNDERLINE + "is", SPIECE_UNDERLINE + "f", "al", "s", "é", ".", ] , ) _UpperCAmelCase : Optional[Any] = tokenizer.convert_tokens_to_ids(A ) self.assertListEqual( A , [ value + tokenizer.fairseq_offset for value in [8, 21, 84, 55, 24, 19, 7, 2, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 2, 4] ] , ) _UpperCAmelCase : Union[str, Any] = tokenizer.convert_ids_to_tokens(A ) self.assertListEqual( A , [ SPIECE_UNDERLINE + "I", SPIECE_UNDERLINE + "was", SPIECE_UNDERLINE + "b", "or", "n", SPIECE_UNDERLINE + "in", SPIECE_UNDERLINE + "", "<unk>", "2", "0", "0", "0", ",", SPIECE_UNDERLINE + "and", SPIECE_UNDERLINE + "this", SPIECE_UNDERLINE + "is", SPIECE_UNDERLINE + "f", "al", "s", "<unk>", ".", ] , ) def _A ( self : List[Any] ): _UpperCAmelCase : Any = (self.rust_tokenizer_class, "hf-internal-testing/tiny-random-nllb", {}) for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F"""{tokenizer.__class__.__name__} ({pretrained_name})""" ): _UpperCAmelCase : Dict = self.rust_tokenizer_class.from_pretrained(A , **A ) _UpperCAmelCase : str = self.tokenizer_class.from_pretrained(A , **A ) _UpperCAmelCase : Optional[int] = tempfile.mkdtemp() _UpperCAmelCase : Dict = tokenizer_r.save_pretrained(A ) _UpperCAmelCase : Dict = tokenizer_p.save_pretrained(A ) # Checks it save with the same files + the tokenizer.json file for the fast one self.assertTrue(any("tokenizer.json" in f for f in tokenizer_r_files ) ) _UpperCAmelCase : Optional[int] = tuple(f for f in tokenizer_r_files if "tokenizer.json" not in f ) self.assertSequenceEqual(A , A ) # Checks everything loads correctly in the same way _UpperCAmelCase : List[Any] = tokenizer_r.from_pretrained(A ) _UpperCAmelCase : List[str] = tokenizer_p.from_pretrained(A ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(A , A ) ) shutil.rmtree(A ) # Save tokenizer rust, legacy_format=True _UpperCAmelCase : Optional[Any] = tempfile.mkdtemp() _UpperCAmelCase : str = tokenizer_r.save_pretrained(A , legacy_format=A ) _UpperCAmelCase : str = tokenizer_p.save_pretrained(A ) # Checks it save with the same files self.assertSequenceEqual(A , A ) # Checks everything loads correctly in the same way _UpperCAmelCase : Optional[int] = tokenizer_r.from_pretrained(A ) _UpperCAmelCase : Dict = tokenizer_p.from_pretrained(A ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(A , A ) ) shutil.rmtree(A ) # Save tokenizer rust, legacy_format=False _UpperCAmelCase : Optional[int] = tempfile.mkdtemp() _UpperCAmelCase : Optional[int] = tokenizer_r.save_pretrained(A , legacy_format=A ) _UpperCAmelCase : Dict = tokenizer_p.save_pretrained(A ) # Checks it saved the tokenizer.json file self.assertTrue(any("tokenizer.json" in f for f in tokenizer_r_files ) ) # Checks everything loads correctly in the same way _UpperCAmelCase : List[Any] = tokenizer_r.from_pretrained(A ) _UpperCAmelCase : Optional[int] = tokenizer_p.from_pretrained(A ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(A , A ) ) shutil.rmtree(A ) @require_torch def _A ( self : Tuple ): if not self.test_seqaseq: return _UpperCAmelCase : Union[str, Any] = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(F"""{tokenizer.__class__.__name__}""" ): # Longer text that will definitely require truncation. _UpperCAmelCase : Optional[Any] = [ " UN Chief Says There Is No Military Solution in Syria", " Secretary-General Ban Ki-moon says his response to Russia's stepped up military support for" " Syria is that 'there is no military solution' to the nearly five-year conflict and more weapons" " will only worsen the violence and misery for millions of people.", ] _UpperCAmelCase : Optional[Any] = [ "Şeful ONU declară că nu există o soluţie militară în Siria", "Secretarul General Ban Ki-moon declară că răspunsul său la intensificarea sprijinului militar al" " Rusiei pentru Siria este că \"nu există o soluţie militară\" la conflictul de aproape cinci ani şi" " că noi arme nu vor face decât să înrăutăţească violenţele şi mizeria pentru milioane de oameni.", ] try: _UpperCAmelCase : Optional[int] = tokenizer.prepare_seqaseq_batch( src_texts=A , tgt_texts=A , max_length=3 , max_target_length=10 , return_tensors="pt" , src_lang="eng_Latn" , tgt_lang="ron_Latn" , ) except NotImplementedError: return self.assertEqual(batch.input_ids.shape[1] , 3 ) self.assertEqual(batch.labels.shape[1] , 10 ) # max_target_length will default to max_length if not specified _UpperCAmelCase : Tuple = tokenizer.prepare_seqaseq_batch( A , tgt_texts=A , max_length=3 , return_tensors="pt" ) self.assertEqual(batch.input_ids.shape[1] , 3 ) self.assertEqual(batch.labels.shape[1] , 3 ) _UpperCAmelCase : Union[str, Any] = tokenizer.prepare_seqaseq_batch( src_texts=A , max_length=3 , max_target_length=10 , return_tensors="pt" ) self.assertEqual(batch_encoder_only.input_ids.shape[1] , 3 ) self.assertEqual(batch_encoder_only.attention_mask.shape[1] , 3 ) self.assertNotIn("decoder_input_ids" , A ) @unittest.skip("Unfortunately way too slow to build a BPE with SentencePiece." ) def _A ( self : List[Any] ): pass def _A ( self : Union[str, Any] ): for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F"""{tokenizer.__class__.__name__} ({pretrained_name})""" ): _UpperCAmelCase : Any = [AddedToken("<special>" , lstrip=A )] _UpperCAmelCase : List[Any] = self.rust_tokenizer_class.from_pretrained( A , additional_special_tokens=A , **A ) _UpperCAmelCase : Dict = tokenizer_r.encode("Hey this is a <special> token" ) _UpperCAmelCase : Any = tokenizer_r.encode("<special>" , add_special_tokens=A )[0] self.assertTrue(special_token_id in r_output ) if self.test_slow_tokenizer: _UpperCAmelCase : Dict = self.rust_tokenizer_class.from_pretrained( A , additional_special_tokens=A , **A , ) _UpperCAmelCase : Optional[int] = self.tokenizer_class.from_pretrained( A , additional_special_tokens=A , **A ) _UpperCAmelCase : Union[str, Any] = tokenizer_p.encode("Hey this is a <special> token" ) _UpperCAmelCase : Any = tokenizer_cr.encode("Hey this is a <special> token" ) self.assertEqual(A , A ) self.assertEqual(A , A ) self.assertTrue(special_token_id in p_output ) self.assertTrue(special_token_id in cr_output ) @require_torch @require_sentencepiece @require_tokenizers class lowerCamelCase_ (unittest.TestCase ): '''simple docstring''' __UpperCamelCase: Dict = "facebook/nllb-200-distilled-600M" __UpperCamelCase: Optional[int] = [ " UN Chief Says There Is No Military Solution in Syria", " Secretary-General Ban Ki-moon says his response to Russia's stepped up military support for Syria is that \"there is no military solution\" to the nearly five-year conflict and more weapons will only worsen the violence and misery for millions of people.", ] __UpperCamelCase: str = [ "Şeful ONU declară că nu există o soluţie militară în Siria", "Secretarul General Ban Ki-moon declară că răspunsul său la intensificarea sprijinului militar al Rusiei" " pentru Siria este că \"nu există o soluţie militară\" la conflictul de aproape cinci ani şi că noi arme nu vor" " face decât să înrăutăţească violenţele şi mizeria pentru milioane de oameni.", ] __UpperCamelCase: str = [ 2_5_6_0_4_7, 1_6_2_9_7, 1_3_4_4_0_8, 8_1_6_5, 2_4_8_0_6_6, 1_4_7_3_4, 9_5_0, 1_1_3_5, 1_0_5_7_2_1, 3_5_7_3, 8_3, 2_7_3_5_2, 1_0_8, 4_9_4_8_6, 2, ] @classmethod def _A ( cls : int ): _UpperCAmelCase : NllbTokenizer = NllbTokenizer.from_pretrained( cls.checkpoint_name , src_lang="eng_Latn" , tgt_lang="ron_Latn" ) _UpperCAmelCase : Union[str, Any] = 1 return cls def _A ( self : Any ): self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["ace_Arab"] , 256001 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["ace_Latn"] , 256002 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["fra_Latn"] , 256057 ) def _A ( self : Union[str, Any] ): _UpperCAmelCase : Optional[int] = self.tokenizer.batch_encode_plus(self.src_text ).input_ids[0] self.assertListEqual(self.expected_src_tokens , A ) def _A ( self : Tuple ): self.assertIn(A , self.tokenizer.all_special_ids ) # fmt: off _UpperCAmelCase : List[Any] = [RO_CODE, 4254, 98068, 112923, 39072, 3909, 713, 102767, 26, 17314, 35642, 14683, 33118, 2022, 66987, 2, 256047] # fmt: on _UpperCAmelCase : Tuple = self.tokenizer.decode(A , skip_special_tokens=A ) _UpperCAmelCase : Optional[Any] = self.tokenizer.decode(generated_ids[1:] , skip_special_tokens=A ) self.assertEqual(A , A ) self.assertNotIn(self.tokenizer.eos_token , A ) def _A ( self : Optional[int] ): _UpperCAmelCase : List[Any] = ["this is gunna be a long sentence " * 20] assert isinstance(src_text[0] , A ) _UpperCAmelCase : Dict = 10 _UpperCAmelCase : Tuple = self.tokenizer(A , max_length=A , truncation=A ).input_ids[0] self.assertEqual(ids[-1] , 2 ) self.assertEqual(ids[0] , A ) self.assertEqual(len(A ) , A ) def _A ( self : Dict ): self.assertListEqual(self.tokenizer.convert_tokens_to_ids(["<mask>", "ar_AR"] ) , [256203, 3] ) def _A ( self : Optional[Any] ): _UpperCAmelCase : Dict = tempfile.mkdtemp() _UpperCAmelCase : str = self.tokenizer.fairseq_tokens_to_ids self.tokenizer.save_pretrained(A ) _UpperCAmelCase : Tuple = NllbTokenizer.from_pretrained(A ) self.assertDictEqual(new_tok.fairseq_tokens_to_ids , A ) @require_torch def _A ( self : Dict ): _UpperCAmelCase : List[str] = self.tokenizer( self.src_text , text_target=self.tgt_text , padding=A , truncation=A , max_length=len(self.expected_src_tokens ) , return_tensors="pt" , ) _UpperCAmelCase : Tuple = shift_tokens_right( batch["labels"] , self.tokenizer.pad_token_id , self.tokenizer.lang_code_to_id["ron_Latn"] ) self.assertIsInstance(A , A ) self.assertEqual((2, 15) , batch.input_ids.shape ) self.assertEqual((2, 15) , batch.attention_mask.shape ) _UpperCAmelCase : Dict = batch.input_ids.tolist()[0] self.assertListEqual(self.expected_src_tokens , A ) self.assertEqual(A , batch.decoder_input_ids[0, 0] ) # EOS # Test that special tokens are reset self.assertEqual(self.tokenizer.prefix_tokens , [EN_CODE] ) self.assertEqual(self.tokenizer.suffix_tokens , [self.tokenizer.eos_token_id] ) def _A ( self : str ): _UpperCAmelCase : Optional[Any] = self.tokenizer(self.src_text , padding=A , truncation=A , max_length=3 , return_tensors="pt" ) _UpperCAmelCase : Dict = self.tokenizer( text_target=self.tgt_text , padding=A , truncation=A , max_length=10 , return_tensors="pt" ) _UpperCAmelCase : List[Any] = targets["input_ids"] _UpperCAmelCase : Union[str, Any] = shift_tokens_right( A , self.tokenizer.pad_token_id , decoder_start_token_id=self.tokenizer.lang_code_to_id[self.tokenizer.tgt_lang] , ) self.assertEqual(batch.input_ids.shape[1] , 3 ) self.assertEqual(batch.decoder_input_ids.shape[1] , 10 ) @require_torch def _A ( self : List[Any] ): _UpperCAmelCase : str = self.tokenizer._build_translation_inputs( "A test" , return_tensors="pt" , src_lang="eng_Latn" , tgt_lang="fra_Latn" ) self.assertEqual( nested_simplify(A ) , { # A, test, EOS, en_XX "input_ids": [[256047, 70, 7356, 2]], "attention_mask": [[1, 1, 1, 1]], # ar_AR "forced_bos_token_id": 256057, } , ) @require_torch def _A ( self : Any ): _UpperCAmelCase : Dict = True _UpperCAmelCase : Any = self.tokenizer( "UN Chief says there is no military solution in Syria" , src_lang="eng_Latn" , tgt_lang="fra_Latn" ) self.assertEqual( inputs.input_ids , [16297, 134408, 25653, 6370, 248, 254, 103929, 94995, 108, 49486, 2, 256047] ) _UpperCAmelCase : Optional[int] = False _UpperCAmelCase : str = self.tokenizer( "UN Chief says there is no military solution in Syria" , src_lang="eng_Latn" , tgt_lang="fra_Latn" ) self.assertEqual( inputs.input_ids , [256047, 16297, 134408, 25653, 6370, 248, 254, 103929, 94995, 108, 49486, 2] )
31
1
'''simple docstring''' import unittest from transformers import BarthezTokenizer, BarthezTokenizerFast, BatchEncoding from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers @require_sentencepiece @slow # see https://github.com/huggingface/transformers/issues/11457 class lowerCamelCase_ (snake_case__ , unittest.TestCase ): '''simple docstring''' __UpperCamelCase: Any = BarthezTokenizer __UpperCamelCase: Union[str, Any] = BarthezTokenizerFast __UpperCamelCase: Dict = True __UpperCamelCase: Any = True def _A ( self : Optional[Any] ): super().setUp() _UpperCAmelCase : Union[str, Any] = BarthezTokenizerFast.from_pretrained("moussaKam/mbarthez" ) tokenizer.save_pretrained(self.tmpdirname ) tokenizer.save_pretrained(self.tmpdirname , legacy_format=A ) _UpperCAmelCase : Union[str, Any] = tokenizer def _A ( self : Optional[Any] ): _UpperCAmelCase : str = "<pad>" _UpperCAmelCase : int = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(A ) , A ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(A ) , A ) def _A ( self : List[Any] ): _UpperCAmelCase : Any = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , "<s>" ) self.assertEqual(vocab_keys[1] , "<pad>" ) self.assertEqual(vocab_keys[-1] , "<mask>" ) self.assertEqual(len(A ) , 101122 ) def _A ( self : int ): self.assertEqual(self.get_tokenizer().vocab_size , 101122 ) @require_torch def _A ( self : Tuple ): _UpperCAmelCase : int = ["A long paragraph for summarization.", "Another paragraph for summarization."] _UpperCAmelCase : Any = [0, 57, 3018, 70307, 91, 2] _UpperCAmelCase : List[str] = self.tokenizer( A , max_length=len(A ) , padding=A , truncation=A , return_tensors="pt" ) self.assertIsInstance(A , A ) self.assertEqual((2, 6) , batch.input_ids.shape ) self.assertEqual((2, 6) , batch.attention_mask.shape ) _UpperCAmelCase : Dict = batch.input_ids.tolist()[0] self.assertListEqual(A , A ) def _A ( self : List[str] ): if not self.test_rust_tokenizer: return _UpperCAmelCase : Optional[int] = self.get_tokenizer() _UpperCAmelCase : Union[str, Any] = self.get_rust_tokenizer() _UpperCAmelCase : List[Any] = "I was born in 92000, and this is falsé." _UpperCAmelCase : str = tokenizer.tokenize(A ) _UpperCAmelCase : Dict = rust_tokenizer.tokenize(A ) self.assertListEqual(A , A ) _UpperCAmelCase : List[Any] = tokenizer.encode(A , add_special_tokens=A ) _UpperCAmelCase : List[str] = rust_tokenizer.encode(A , add_special_tokens=A ) self.assertListEqual(A , A ) _UpperCAmelCase : Dict = self.get_rust_tokenizer() _UpperCAmelCase : Any = tokenizer.encode(A ) _UpperCAmelCase : List[str] = rust_tokenizer.encode(A ) self.assertListEqual(A , A ) @slow def _A ( self : List[Any] ): # fmt: off _UpperCAmelCase : List[Any] = {"input_ids": [[0, 490, 14328, 4507, 354, 47, 43669, 95, 25, 78117, 20215, 19779, 190, 22, 400, 4, 35343, 80310, 603, 86, 24937, 105, 33438, 94762, 196, 39642, 7, 15, 15933, 173, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 10534, 87, 25, 66, 3358, 196, 55289, 8, 82961, 81, 2204, 75203, 7, 15, 763, 12956, 216, 178, 14328, 9595, 1377, 69693, 7, 448, 71021, 196, 18106, 1437, 13974, 108, 9083, 4, 49315, 7, 39, 86, 1326, 2793, 46333, 4, 448, 196, 74588, 7, 49315, 7, 39, 21, 822, 38470, 74, 21, 66723, 62480, 8, 22050, 5, 2]], "attention_mask": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]} # noqa: E501 # fmt: on # moussaKam/mbarthez is a french model. So we also use french texts. _UpperCAmelCase : int = [ "Le transformeur est un modèle d'apprentissage profond introduit en 2017, " "utilisé principalement dans le domaine du traitement automatique des langues (TAL).", "À l'instar des réseaux de neurones récurrents (RNN), les transformeurs sont conçus " "pour gérer des données séquentielles, telles que le langage naturel, pour des tâches " "telles que la traduction et la synthèse de texte.", ] self.tokenizer_integration_test_util( expected_encoding=A , model_name="moussaKam/mbarthez" , revision="c2e4ecbca5e3cd2c37fe1ac285ca4fbdf1366fb6" , sequences=A , )
31
'''simple docstring''' def UpperCamelCase_ ( _UpperCAmelCase : list ) -> list: """simple docstring""" _UpperCAmelCase : List[Any] = len(_UpperCAmelCase ) for _ in range(_UpperCAmelCase ): for i in range(_ % 2 , arr_size - 1 , 2 ): if arr[i + 1] < arr[i]: _UpperCAmelCase , _UpperCAmelCase : int = arr[i + 1], arr[i] return arr if __name__ == "__main__": __SCREAMING_SNAKE_CASE : Optional[Any] = list(range(10, 0, -1)) print(F'Original: {arr}. Sorted: {odd_even_transposition(arr)}')
31
1
'''simple docstring''' def UpperCamelCase_ ( _UpperCAmelCase : list ) -> list: """simple docstring""" _UpperCAmelCase : List[Any] = len(_UpperCAmelCase ) for _ in range(_UpperCAmelCase ): for i in range(_ % 2 , arr_size - 1 , 2 ): if arr[i + 1] < arr[i]: _UpperCAmelCase , _UpperCAmelCase : int = arr[i + 1], arr[i] return arr if __name__ == "__main__": __SCREAMING_SNAKE_CASE : Optional[Any] = list(range(10, 0, -1)) print(F'Original: {arr}. Sorted: {odd_even_transposition(arr)}')
31
'''simple docstring''' import os from typing import Any, Callable, Dict, List, Optional, Tuple, Union import torch from torch import nn from ...models.controlnet import ControlNetModel, ControlNetOutput from ...models.modeling_utils import ModelMixin from ...utils import logging __SCREAMING_SNAKE_CASE : str = logging.get_logger(__name__) class lowerCamelCase_ (snake_case__ ): '''simple docstring''' def __init__( self : Optional[int] , A : Union[List[ControlNetModel], Tuple[ControlNetModel]] ): super().__init__() _UpperCAmelCase : Optional[int] = nn.ModuleList(A ) def _A ( self : Dict , A : torch.FloatTensor , A : Union[torch.Tensor, float, int] , A : torch.Tensor , A : List[torch.tensor] , A : List[float] , A : Optional[torch.Tensor] = None , A : Optional[torch.Tensor] = None , A : Optional[torch.Tensor] = None , A : Optional[Dict[str, Any]] = None , A : bool = False , A : bool = True , ): for i, (image, scale, controlnet) in enumerate(zip(A , A , self.nets ) ): _UpperCAmelCase , _UpperCAmelCase : str = controlnet( A , A , A , A , A , A , A , A , A , A , A , ) # merge samples if i == 0: _UpperCAmelCase , _UpperCAmelCase : List[Any] = down_samples, mid_sample else: _UpperCAmelCase : Optional[int] = [ samples_prev + samples_curr for samples_prev, samples_curr in zip(A , A ) ] mid_block_res_sample += mid_sample return down_block_res_samples, mid_block_res_sample def _A ( self : List[str] , A : Union[str, os.PathLike] , A : bool = True , A : Callable = None , A : bool = False , A : Optional[str] = None , ): _UpperCAmelCase : str = 0 _UpperCAmelCase : str = save_directory for controlnet in self.nets: controlnet.save_pretrained( A , is_main_process=A , save_function=A , safe_serialization=A , variant=A , ) idx += 1 _UpperCAmelCase : Tuple = model_path_to_save + F"""_{idx}""" @classmethod def _A ( cls : int , A : Optional[Union[str, os.PathLike]] , **A : Tuple ): _UpperCAmelCase : str = 0 _UpperCAmelCase : int = [] # load controlnet and append to list until no controlnet directory exists anymore # first controlnet has to be saved under `./mydirectory/controlnet` to be compliant with `DiffusionPipeline.from_prertained` # second, third, ... controlnets have to be saved under `./mydirectory/controlnet_1`, `./mydirectory/controlnet_2`, ... _UpperCAmelCase : int = pretrained_model_path while os.path.isdir(A ): _UpperCAmelCase : List[str] = ControlNetModel.from_pretrained(A , **A ) controlnets.append(A ) idx += 1 _UpperCAmelCase : Dict = pretrained_model_path + F"""_{idx}""" logger.info(F"""{len(A )} controlnets loaded from {pretrained_model_path}.""" ) if len(A ) == 0: raise ValueError( F"""No ControlNets found under {os.path.dirname(A )}. Expected at least {pretrained_model_path + '_0'}.""" ) return cls(A )
31
1
'''simple docstring''' import unittest from transformers import PegasusTokenizer, PegasusTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, require_torch, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin __SCREAMING_SNAKE_CASE : Any = get_tests_dir("""fixtures/test_sentencepiece_no_bos.model""") @require_sentencepiece @require_tokenizers class lowerCamelCase_ (snake_case__ , unittest.TestCase ): '''simple docstring''' __UpperCamelCase: Optional[Any] = PegasusTokenizer __UpperCamelCase: Optional[Any] = PegasusTokenizerFast __UpperCamelCase: Union[str, Any] = True __UpperCamelCase: Any = True def _A ( self : int ): super().setUp() # We have a SentencePiece fixture for testing _UpperCAmelCase : Optional[int] = PegasusTokenizer(A ) tokenizer.save_pretrained(self.tmpdirname ) @cached_property def _A ( self : Optional[Any] ): return PegasusTokenizer.from_pretrained("google/pegasus-large" ) def _A ( self : Optional[int] , **A : int ): return PegasusTokenizer.from_pretrained(self.tmpdirname , **A ) def _A ( self : int , A : List[str] ): return ("This is a test", "This is a test") def _A ( self : Any ): _UpperCAmelCase : List[str] = "</s>" _UpperCAmelCase : List[Any] = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(A ) , A ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(A ) , A ) def _A ( self : Any ): _UpperCAmelCase : Optional[int] = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , "<pad>" ) self.assertEqual(vocab_keys[1] , "</s>" ) self.assertEqual(vocab_keys[-1] , "v" ) self.assertEqual(len(A ) , 1103 ) def _A ( self : List[str] ): self.assertEqual(self.get_tokenizer().vocab_size , 1103 ) def _A ( self : Union[str, Any] ): _UpperCAmelCase : List[str] = self.rust_tokenizer_class.from_pretrained(self.tmpdirname ) _UpperCAmelCase : Optional[Any] = self.tokenizer_class.from_pretrained(self.tmpdirname ) _UpperCAmelCase : List[Any] = ( "Let's see which <unk> is the better <unk_token_11> one <mask_1> It seems like this <mask_2> was important" " </s> <pad> <pad> <pad>" ) _UpperCAmelCase : Optional[Any] = rust_tokenizer([raw_input_str] , return_tensors=A , add_special_tokens=A ).input_ids[0] _UpperCAmelCase : Dict = py_tokenizer([raw_input_str] , return_tensors=A , add_special_tokens=A ).input_ids[0] self.assertListEqual(A , A ) def _A ( self : Dict ): _UpperCAmelCase : str = self._large_tokenizer # <mask_1> masks whole sentence while <mask_2> masks single word _UpperCAmelCase : Tuple = "<mask_1> To ensure a <mask_2> flow of bank resolutions." _UpperCAmelCase : Optional[Any] = [2, 413, 615, 114, 3, 1971, 113, 1679, 10710, 107, 1] _UpperCAmelCase : Optional[Any] = tokenizer([raw_input_str] , return_tensors=A ).input_ids[0] self.assertListEqual(A , A ) def _A ( self : int ): _UpperCAmelCase : Tuple = self._large_tokenizer # The tracebacks for the following asserts are **better** without messages or self.assertEqual assert tokenizer.vocab_size == 96103 assert tokenizer.pad_token_id == 0 assert tokenizer.eos_token_id == 1 assert tokenizer.offset == 103 assert tokenizer.unk_token_id == tokenizer.offset + 2 == 105 assert tokenizer.unk_token == "<unk>" assert tokenizer.model_max_length == 1024 _UpperCAmelCase : str = "To ensure a smooth flow of bank resolutions." _UpperCAmelCase : Dict = [413, 615, 114, 2291, 1971, 113, 1679, 10710, 107, 1] _UpperCAmelCase : Dict = tokenizer([raw_input_str] , return_tensors=A ).input_ids[0] self.assertListEqual(A , A ) assert tokenizer.convert_ids_to_tokens([0, 1, 2, 3] ) == ["<pad>", "</s>", "<mask_1>", "<mask_2>"] @require_torch def _A ( self : List[Any] ): _UpperCAmelCase : str = ["This is going to be way too long." * 150, "short example"] _UpperCAmelCase : int = ["not super long but more than 5 tokens", "tiny"] _UpperCAmelCase : Tuple = self._large_tokenizer(A , padding=A , truncation=A , return_tensors="pt" ) _UpperCAmelCase : Dict = self._large_tokenizer( text_target=A , max_length=5 , padding=A , truncation=A , return_tensors="pt" ) assert batch.input_ids.shape == (2, 1024) assert batch.attention_mask.shape == (2, 1024) assert targets["input_ids"].shape == (2, 5) assert len(A ) == 2 # input_ids, attention_mask. @slow def _A ( self : Union[str, Any] ): # fmt: off _UpperCAmelCase : Optional[int] = {"input_ids": [[38979, 143, 18485, 606, 130, 26669, 87686, 121, 54189, 1129, 111, 26669, 87686, 121, 9114, 14787, 121, 13249, 158, 592, 956, 121, 14621, 31576, 143, 62613, 108, 9688, 930, 43430, 11562, 62613, 304, 108, 11443, 897, 108, 9314, 17415, 63399, 108, 11443, 7614, 18316, 118, 4284, 7148, 12430, 143, 1400, 25703, 158, 111, 4284, 7148, 11772, 143, 21297, 1064, 158, 122, 204, 3506, 1754, 1133, 14787, 1581, 115, 33224, 4482, 111, 1355, 110, 29173, 317, 50833, 108, 20147, 94665, 111, 77198, 107, 1], [110, 62613, 117, 638, 112, 1133, 121, 20098, 1355, 79050, 13872, 135, 1596, 53541, 1352, 141, 13039, 5542, 124, 302, 518, 111, 268, 2956, 115, 149, 4427, 107, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [139, 1235, 2799, 18289, 17780, 204, 109, 9474, 1296, 107, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], "attention_mask": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=A , model_name="google/bigbird-pegasus-large-arxiv" , revision="ba85d0851d708441f91440d509690f1ab6353415" , ) @require_sentencepiece @require_tokenizers class lowerCamelCase_ (snake_case__ , unittest.TestCase ): '''simple docstring''' __UpperCamelCase: Dict = PegasusTokenizer __UpperCamelCase: List[str] = PegasusTokenizerFast __UpperCamelCase: Optional[int] = True __UpperCamelCase: Optional[int] = True def _A ( self : List[str] ): super().setUp() # We have a SentencePiece fixture for testing _UpperCAmelCase : Optional[Any] = PegasusTokenizer(A , offset=0 , mask_token_sent=A , mask_token="[MASK]" ) tokenizer.save_pretrained(self.tmpdirname ) @cached_property def _A ( self : Optional[Any] ): return PegasusTokenizer.from_pretrained("google/bigbird-pegasus-large-arxiv" ) def _A ( self : Optional[Any] , **A : Dict ): return PegasusTokenizer.from_pretrained(self.tmpdirname , **A ) def _A ( self : Dict , A : Dict ): return ("This is a test", "This is a test") def _A ( self : Dict ): _UpperCAmelCase : Tuple = self.rust_tokenizer_class.from_pretrained(self.tmpdirname ) _UpperCAmelCase : Any = self.tokenizer_class.from_pretrained(self.tmpdirname ) _UpperCAmelCase : List[Any] = ( "Let's see which <unk> is the better <unk_token> one [MASK] It seems like this [MASK] was important </s>" " <pad> <pad> <pad>" ) _UpperCAmelCase : List[str] = rust_tokenizer([raw_input_str] , return_tensors=A , add_special_tokens=A ).input_ids[0] _UpperCAmelCase : Union[str, Any] = py_tokenizer([raw_input_str] , return_tensors=A , add_special_tokens=A ).input_ids[0] self.assertListEqual(A , A ) @require_torch def _A ( self : int ): _UpperCAmelCase : Optional[Any] = ["This is going to be way too long." * 1000, "short example"] _UpperCAmelCase : Any = ["not super long but more than 5 tokens", "tiny"] _UpperCAmelCase : List[str] = self._large_tokenizer(A , padding=A , truncation=A , return_tensors="pt" ) _UpperCAmelCase : Any = self._large_tokenizer( text_target=A , max_length=5 , padding=A , truncation=A , return_tensors="pt" ) assert batch.input_ids.shape == (2, 4096) assert batch.attention_mask.shape == (2, 4096) assert targets["input_ids"].shape == (2, 5) assert len(A ) == 2 # input_ids, attention_mask. def _A ( self : Tuple ): _UpperCAmelCase : List[Any] = ( "This is an example string that is used to test the original TF implementation against the HF" " implementation" ) _UpperCAmelCase : Optional[Any] = self._large_tokenizer(A ).input_ids self.assertListEqual( A , [182, 117, 142, 587, 4211, 120, 117, 263, 112, 804, 109, 856, 25016, 3137, 464, 109, 26955, 3137, 1] , )
31
'''simple docstring''' import argparse import json import pickle from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import MaskFormerConfig, MaskFormerForInstanceSegmentation, MaskFormerImageProcessor, SwinConfig from transformers.utils import logging logging.set_verbosity_info() __SCREAMING_SNAKE_CASE : Union[str, Any] = logging.get_logger(__name__) def UpperCamelCase_ ( _UpperCAmelCase : str ) -> Optional[Any]: """simple docstring""" _UpperCAmelCase : int = SwinConfig.from_pretrained( "microsoft/swin-tiny-patch4-window7-224" , out_features=["stage1", "stage2", "stage3", "stage4"] ) _UpperCAmelCase : List[Any] = MaskFormerConfig(backbone_config=_UpperCAmelCase ) _UpperCAmelCase : Tuple = "huggingface/label-files" if "ade20k-full" in model_name: # this should be ok _UpperCAmelCase : Dict = 847 _UpperCAmelCase : Any = "maskformer-ade20k-full-id2label.json" elif "ade" in model_name: # this should be ok _UpperCAmelCase : Any = 150 _UpperCAmelCase : Any = "ade20k-id2label.json" elif "coco-stuff" in model_name: # this should be ok _UpperCAmelCase : Tuple = 171 _UpperCAmelCase : Union[str, Any] = "maskformer-coco-stuff-id2label.json" elif "coco" in model_name: # TODO _UpperCAmelCase : Any = 133 _UpperCAmelCase : int = "coco-panoptic-id2label.json" elif "cityscapes" in model_name: # this should be ok _UpperCAmelCase : Optional[int] = 19 _UpperCAmelCase : str = "cityscapes-id2label.json" elif "vistas" in model_name: # this should be ok _UpperCAmelCase : Optional[int] = 65 _UpperCAmelCase : Tuple = "mapillary-vistas-id2label.json" _UpperCAmelCase : List[Any] = json.load(open(hf_hub_download(_UpperCAmelCase , _UpperCAmelCase , repo_type="dataset" ) , "r" ) ) _UpperCAmelCase : Tuple = {int(_UpperCAmelCase ): v for k, v in idalabel.items()} return config def UpperCamelCase_ ( _UpperCAmelCase : Optional[int] ) -> List[Any]: """simple docstring""" _UpperCAmelCase : Dict = [] # stem # fmt: off rename_keys.append(("backbone.patch_embed.proj.weight", "model.pixel_level_module.encoder.model.embeddings.patch_embeddings.projection.weight") ) rename_keys.append(("backbone.patch_embed.proj.bias", "model.pixel_level_module.encoder.model.embeddings.patch_embeddings.projection.bias") ) rename_keys.append(("backbone.patch_embed.norm.weight", "model.pixel_level_module.encoder.model.embeddings.norm.weight") ) rename_keys.append(("backbone.patch_embed.norm.bias", "model.pixel_level_module.encoder.model.embeddings.norm.bias") ) # stages for i in range(len(config.backbone_config.depths ) ): for j in range(config.backbone_config.depths[i] ): rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.norm1.weight""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.layernorm_before.weight""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.norm1.bias""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.layernorm_before.bias""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.attn.relative_position_bias_table""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_bias_table""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.attn.relative_position_index""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_index""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.attn.proj.weight""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.weight""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.attn.proj.bias""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.bias""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.norm2.weight""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.layernorm_after.weight""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.norm2.bias""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.layernorm_after.bias""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.mlp.fc1.weight""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.weight""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.mlp.fc1.bias""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.bias""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.mlp.fc2.weight""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.output.dense.weight""") ) rename_keys.append((F"""backbone.layers.{i}.blocks.{j}.mlp.fc2.bias""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.blocks.{j}.output.dense.bias""") ) if i < 3: rename_keys.append((F"""backbone.layers.{i}.downsample.reduction.weight""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.downsample.reduction.weight""") ) rename_keys.append((F"""backbone.layers.{i}.downsample.norm.weight""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.downsample.norm.weight""") ) rename_keys.append((F"""backbone.layers.{i}.downsample.norm.bias""", F"""model.pixel_level_module.encoder.model.encoder.layers.{i}.downsample.norm.bias""") ) rename_keys.append((F"""backbone.norm{i}.weight""", F"""model.pixel_level_module.encoder.hidden_states_norms.{i}.weight""") ) rename_keys.append((F"""backbone.norm{i}.bias""", F"""model.pixel_level_module.encoder.hidden_states_norms.{i}.bias""") ) # FPN rename_keys.append(("sem_seg_head.layer_4.weight", "model.pixel_level_module.decoder.fpn.stem.0.weight") ) rename_keys.append(("sem_seg_head.layer_4.norm.weight", "model.pixel_level_module.decoder.fpn.stem.1.weight") ) rename_keys.append(("sem_seg_head.layer_4.norm.bias", "model.pixel_level_module.decoder.fpn.stem.1.bias") ) for source_index, target_index in zip(range(3 , 0 , -1 ) , range(0 , 3 ) ): rename_keys.append((F"""sem_seg_head.adapter_{source_index}.weight""", F"""model.pixel_level_module.decoder.fpn.layers.{target_index}.proj.0.weight""") ) rename_keys.append((F"""sem_seg_head.adapter_{source_index}.norm.weight""", F"""model.pixel_level_module.decoder.fpn.layers.{target_index}.proj.1.weight""") ) rename_keys.append((F"""sem_seg_head.adapter_{source_index}.norm.bias""", F"""model.pixel_level_module.decoder.fpn.layers.{target_index}.proj.1.bias""") ) rename_keys.append((F"""sem_seg_head.layer_{source_index}.weight""", F"""model.pixel_level_module.decoder.fpn.layers.{target_index}.block.0.weight""") ) rename_keys.append((F"""sem_seg_head.layer_{source_index}.norm.weight""", F"""model.pixel_level_module.decoder.fpn.layers.{target_index}.block.1.weight""") ) rename_keys.append((F"""sem_seg_head.layer_{source_index}.norm.bias""", F"""model.pixel_level_module.decoder.fpn.layers.{target_index}.block.1.bias""") ) rename_keys.append(("sem_seg_head.mask_features.weight", "model.pixel_level_module.decoder.mask_projection.weight") ) rename_keys.append(("sem_seg_head.mask_features.bias", "model.pixel_level_module.decoder.mask_projection.bias") ) # Transformer decoder for idx in range(config.decoder_config.decoder_layers ): # self-attention out projection rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.self_attn.out_proj.weight""", F"""model.transformer_module.decoder.layers.{idx}.self_attn.out_proj.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.self_attn.out_proj.bias""", F"""model.transformer_module.decoder.layers.{idx}.self_attn.out_proj.bias""") ) # cross-attention out projection rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.multihead_attn.out_proj.weight""", F"""model.transformer_module.decoder.layers.{idx}.encoder_attn.out_proj.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.multihead_attn.out_proj.bias""", F"""model.transformer_module.decoder.layers.{idx}.encoder_attn.out_proj.bias""") ) # MLP 1 rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.linear1.weight""", F"""model.transformer_module.decoder.layers.{idx}.fc1.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.linear1.bias""", F"""model.transformer_module.decoder.layers.{idx}.fc1.bias""") ) # MLP 2 rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.linear2.weight""", F"""model.transformer_module.decoder.layers.{idx}.fc2.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.linear2.bias""", F"""model.transformer_module.decoder.layers.{idx}.fc2.bias""") ) # layernorm 1 (self-attention layernorm) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm1.weight""", F"""model.transformer_module.decoder.layers.{idx}.self_attn_layer_norm.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm1.bias""", F"""model.transformer_module.decoder.layers.{idx}.self_attn_layer_norm.bias""") ) # layernorm 2 (cross-attention layernorm) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm2.weight""", F"""model.transformer_module.decoder.layers.{idx}.encoder_attn_layer_norm.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm2.bias""", F"""model.transformer_module.decoder.layers.{idx}.encoder_attn_layer_norm.bias""") ) # layernorm 3 (final layernorm) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm3.weight""", F"""model.transformer_module.decoder.layers.{idx}.final_layer_norm.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.norm3.bias""", F"""model.transformer_module.decoder.layers.{idx}.final_layer_norm.bias""") ) rename_keys.append(("sem_seg_head.predictor.transformer.decoder.norm.weight", "model.transformer_module.decoder.layernorm.weight") ) rename_keys.append(("sem_seg_head.predictor.transformer.decoder.norm.bias", "model.transformer_module.decoder.layernorm.bias") ) # heads on top rename_keys.append(("sem_seg_head.predictor.query_embed.weight", "model.transformer_module.queries_embedder.weight") ) rename_keys.append(("sem_seg_head.predictor.input_proj.weight", "model.transformer_module.input_projection.weight") ) rename_keys.append(("sem_seg_head.predictor.input_proj.bias", "model.transformer_module.input_projection.bias") ) rename_keys.append(("sem_seg_head.predictor.class_embed.weight", "class_predictor.weight") ) rename_keys.append(("sem_seg_head.predictor.class_embed.bias", "class_predictor.bias") ) for i in range(3 ): rename_keys.append((F"""sem_seg_head.predictor.mask_embed.layers.{i}.weight""", F"""mask_embedder.{i}.0.weight""") ) rename_keys.append((F"""sem_seg_head.predictor.mask_embed.layers.{i}.bias""", F"""mask_embedder.{i}.0.bias""") ) # fmt: on return rename_keys def UpperCamelCase_ ( _UpperCAmelCase : int , _UpperCAmelCase : Tuple , _UpperCAmelCase : List[Any] ) -> List[str]: """simple docstring""" _UpperCAmelCase : Optional[int] = dct.pop(_UpperCAmelCase ) _UpperCAmelCase : List[str] = val def UpperCamelCase_ ( _UpperCAmelCase : List[Any] , _UpperCAmelCase : Tuple ) -> Optional[int]: """simple docstring""" _UpperCAmelCase : List[str] = [int(backbone_config.embed_dim * 2**i ) for i in range(len(backbone_config.depths ) )] for i in range(len(backbone_config.depths ) ): _UpperCAmelCase : Optional[int] = num_features[i] for j in range(backbone_config.depths[i] ): # fmt: off # read in weights + bias of input projection layer (in original implementation, this is a single matrix + bias) _UpperCAmelCase : Any = state_dict.pop(F"""backbone.layers.{i}.blocks.{j}.attn.qkv.weight""" ) _UpperCAmelCase : Optional[int] = state_dict.pop(F"""backbone.layers.{i}.blocks.{j}.attn.qkv.bias""" ) # next, add query, keys and values (in that order) to the state dict _UpperCAmelCase : List[str] = in_proj_weight[:dim, :] _UpperCAmelCase : Tuple = in_proj_bias[: dim] _UpperCAmelCase : List[Any] = in_proj_weight[ dim : dim * 2, : ] _UpperCAmelCase : List[str] = in_proj_bias[ dim : dim * 2 ] _UpperCAmelCase : Optional[Any] = in_proj_weight[ -dim :, : ] _UpperCAmelCase : Dict = in_proj_bias[-dim :] # fmt: on def UpperCamelCase_ ( _UpperCAmelCase : Dict , _UpperCAmelCase : str ) -> Dict: """simple docstring""" _UpperCAmelCase : Union[str, Any] = config.decoder_config.hidden_size for idx in range(config.decoder_config.decoder_layers ): # read in weights + bias of self-attention input projection layer (in the original implementation, this is a single matrix + bias) _UpperCAmelCase : Dict = state_dict.pop(F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.self_attn.in_proj_weight""" ) _UpperCAmelCase : Dict = state_dict.pop(F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.self_attn.in_proj_bias""" ) # next, add query, keys and values (in that order) to the state dict _UpperCAmelCase : int = in_proj_weight[: hidden_size, :] _UpperCAmelCase : Union[str, Any] = in_proj_bias[:config.hidden_size] _UpperCAmelCase : List[str] = in_proj_weight[hidden_size : hidden_size * 2, :] _UpperCAmelCase : List[str] = in_proj_bias[hidden_size : hidden_size * 2] _UpperCAmelCase : int = in_proj_weight[-hidden_size :, :] _UpperCAmelCase : Optional[Any] = in_proj_bias[-hidden_size :] # read in weights + bias of cross-attention input projection layer (in the original implementation, this is a single matrix + bias) _UpperCAmelCase : Optional[Any] = state_dict.pop(F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.multihead_attn.in_proj_weight""" ) _UpperCAmelCase : Tuple = state_dict.pop(F"""sem_seg_head.predictor.transformer.decoder.layers.{idx}.multihead_attn.in_proj_bias""" ) # next, add query, keys and values (in that order) to the state dict _UpperCAmelCase : Any = in_proj_weight[: hidden_size, :] _UpperCAmelCase : Tuple = in_proj_bias[:config.hidden_size] _UpperCAmelCase : Dict = in_proj_weight[hidden_size : hidden_size * 2, :] _UpperCAmelCase : Dict = in_proj_bias[hidden_size : hidden_size * 2] _UpperCAmelCase : Optional[int] = in_proj_weight[-hidden_size :, :] _UpperCAmelCase : Union[str, Any] = in_proj_bias[-hidden_size :] # fmt: on def UpperCamelCase_ ( ) -> torch.Tensor: """simple docstring""" _UpperCAmelCase : int = "http://images.cocodataset.org/val2017/000000039769.jpg" _UpperCAmelCase : Any = Image.open(requests.get(_UpperCAmelCase , stream=_UpperCAmelCase ).raw ) return im @torch.no_grad() def UpperCamelCase_ ( _UpperCAmelCase : str , _UpperCAmelCase : str , _UpperCAmelCase : str , _UpperCAmelCase : bool = False ) -> List[str]: """simple docstring""" _UpperCAmelCase : Optional[int] = get_maskformer_config(_UpperCAmelCase ) # load original state_dict with open(_UpperCAmelCase , "rb" ) as f: _UpperCAmelCase : Optional[int] = pickle.load(_UpperCAmelCase ) _UpperCAmelCase : Optional[int] = data["model"] # for name, param in state_dict.items(): # print(name, param.shape) # rename keys _UpperCAmelCase : Any = create_rename_keys(_UpperCAmelCase ) for src, dest in rename_keys: rename_key(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) read_in_swin_q_k_v(_UpperCAmelCase , config.backbone_config ) read_in_decoder_q_k_v(_UpperCAmelCase , _UpperCAmelCase ) # update to torch tensors for key, value in state_dict.items(): _UpperCAmelCase : Tuple = torch.from_numpy(_UpperCAmelCase ) # load 🤗 model _UpperCAmelCase : Union[str, Any] = MaskFormerForInstanceSegmentation(_UpperCAmelCase ) model.eval() for name, param in model.named_parameters(): print(_UpperCAmelCase , param.shape ) _UpperCAmelCase , _UpperCAmelCase : Any = model.load_state_dict(_UpperCAmelCase , strict=_UpperCAmelCase ) assert missing_keys == [ "model.pixel_level_module.encoder.model.layernorm.weight", "model.pixel_level_module.encoder.model.layernorm.bias", ] assert len(_UpperCAmelCase ) == 0, F"""Unexpected keys: {unexpected_keys}""" # verify results _UpperCAmelCase : Optional[int] = prepare_img() if "vistas" in model_name: _UpperCAmelCase : int = 65 elif "cityscapes" in model_name: _UpperCAmelCase : Tuple = 65_535 else: _UpperCAmelCase : Any = 255 _UpperCAmelCase : Optional[Any] = True if "ade" in model_name else False _UpperCAmelCase : Optional[int] = MaskFormerImageProcessor(ignore_index=_UpperCAmelCase , reduce_labels=_UpperCAmelCase ) _UpperCAmelCase : Optional[int] = image_processor(_UpperCAmelCase , return_tensors="pt" ) _UpperCAmelCase : List[Any] = model(**_UpperCAmelCase ) print("Logits:" , outputs.class_queries_logits[0, :3, :3] ) if model_name == "maskformer-swin-tiny-ade": _UpperCAmelCase : Tuple = torch.tensor( [[3.6_3_5_3, -4.4_7_7_0, -2.6_0_6_5], [0.5_0_8_1, -4.2_3_9_4, -3.5_3_4_3], [2.1_9_0_9, -5.0_3_5_3, -1.9_3_2_3]] ) assert torch.allclose(outputs.class_queries_logits[0, :3, :3] , _UpperCAmelCase , atol=1e-4 ) print("Looks ok!" ) if pytorch_dump_folder_path is not None: print(F"""Saving model and image processor to {pytorch_dump_folder_path}""" ) Path(_UpperCAmelCase ).mkdir(exist_ok=_UpperCAmelCase ) model.save_pretrained(_UpperCAmelCase ) image_processor.save_pretrained(_UpperCAmelCase ) if push_to_hub: print("Pushing model and image processor to the hub..." ) model.push_to_hub(F"""nielsr/{model_name}""" ) image_processor.push_to_hub(F"""nielsr/{model_name}""" ) if __name__ == "__main__": __SCREAMING_SNAKE_CASE : Optional[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( """--model_name""", default="""maskformer-swin-tiny-ade""", type=str, help=("""Name of the MaskFormer model you'd like to convert""",), ) parser.add_argument( """--checkpoint_path""", default="""/Users/nielsrogge/Documents/MaskFormer_checkpoints/MaskFormer-Swin-tiny-ADE20k/model.pkl""", type=str, help="""Path to the original state dict (.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.""" ) __SCREAMING_SNAKE_CASE : int = parser.parse_args() convert_maskformer_checkpoint( args.model_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub )
31
1
'''simple docstring''' import unittest from transformers import ( MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, Pipeline, ZeroShotClassificationPipeline, pipeline, ) from transformers.testing_utils import is_pipeline_test, nested_simplify, require_tf, require_torch, slow from .test_pipelines_common import ANY # These 2 model types require different inputs than those of the usual text models. __SCREAMING_SNAKE_CASE : Optional[int] = {"""LayoutLMv2Config""", """LayoutLMv3Config"""} @is_pipeline_test class lowerCamelCase_ (unittest.TestCase ): '''simple docstring''' __UpperCamelCase: str = MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING __UpperCamelCase: Any = TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING if model_mapping is not None: __UpperCamelCase: int = {config: model for config, model in model_mapping.items() if config.__name__ not in _TO_SKIP} if tf_model_mapping is not None: __UpperCamelCase: List[Any] = { config: model for config, model in tf_model_mapping.items() if config.__name__ not in _TO_SKIP } def _A ( self : Union[str, Any] , A : int , A : Tuple , A : Optional[Any] ): _UpperCAmelCase : Dict = ZeroShotClassificationPipeline( model=A , tokenizer=A , candidate_labels=["polics", "health"] ) return classifier, ["Who are you voting for in 2020?", "My stomach hurts."] def _A ( self : Any , A : Optional[int] , A : Union[str, Any] ): _UpperCAmelCase : List[Any] = classifier("Who are you voting for in 2020?" , candidate_labels="politics" ) self.assertEqual(A , {"sequence": ANY(A ), "labels": [ANY(A )], "scores": [ANY(A )]} ) # No kwarg _UpperCAmelCase : int = classifier("Who are you voting for in 2020?" , ["politics"] ) self.assertEqual(A , {"sequence": ANY(A ), "labels": [ANY(A )], "scores": [ANY(A )]} ) _UpperCAmelCase : Dict = classifier("Who are you voting for in 2020?" , candidate_labels=["politics"] ) self.assertEqual(A , {"sequence": ANY(A ), "labels": [ANY(A )], "scores": [ANY(A )]} ) _UpperCAmelCase : Tuple = classifier("Who are you voting for in 2020?" , candidate_labels="politics, public health" ) self.assertEqual( A , {"sequence": ANY(A ), "labels": [ANY(A ), ANY(A )], "scores": [ANY(A ), ANY(A )]} ) self.assertAlmostEqual(sum(nested_simplify(outputs["scores"] ) ) , 1.0 ) _UpperCAmelCase : Any = classifier("Who are you voting for in 2020?" , candidate_labels=["politics", "public health"] ) self.assertEqual( A , {"sequence": ANY(A ), "labels": [ANY(A ), ANY(A )], "scores": [ANY(A ), ANY(A )]} ) self.assertAlmostEqual(sum(nested_simplify(outputs["scores"] ) ) , 1.0 ) _UpperCAmelCase : Tuple = classifier( "Who are you voting for in 2020?" , candidate_labels="politics" , hypothesis_template="This text is about {}" ) self.assertEqual(A , {"sequence": ANY(A ), "labels": [ANY(A )], "scores": [ANY(A )]} ) # https://github.com/huggingface/transformers/issues/13846 _UpperCAmelCase : Union[str, Any] = classifier(["I am happy"] , ["positive", "negative"] ) self.assertEqual( A , [ {"sequence": ANY(A ), "labels": [ANY(A ), ANY(A )], "scores": [ANY(A ), ANY(A )]} for i in range(1 ) ] , ) _UpperCAmelCase : Dict = classifier(["I am happy", "I am sad"] , ["positive", "negative"] ) self.assertEqual( A , [ {"sequence": ANY(A ), "labels": [ANY(A ), ANY(A )], "scores": [ANY(A ), ANY(A )]} for i in range(2 ) ] , ) with self.assertRaises(A ): classifier("" , candidate_labels="politics" ) with self.assertRaises(A ): classifier(A , candidate_labels="politics" ) with self.assertRaises(A ): classifier("Who are you voting for in 2020?" , candidate_labels="" ) with self.assertRaises(A ): classifier("Who are you voting for in 2020?" , candidate_labels=A ) with self.assertRaises(A ): classifier( "Who are you voting for in 2020?" , candidate_labels="politics" , hypothesis_template="Not formatting template" , ) with self.assertRaises(A ): classifier( "Who are you voting for in 2020?" , candidate_labels="politics" , hypothesis_template=A , ) self.run_entailment_id(A ) def _A ( self : Tuple , A : Pipeline ): _UpperCAmelCase : Tuple = zero_shot_classifier.model.config _UpperCAmelCase : Optional[int] = config.labelaid _UpperCAmelCase : Dict = zero_shot_classifier.entailment_id _UpperCAmelCase : Optional[Any] = {"LABEL_0": 0, "LABEL_1": 1, "LABEL_2": 2} self.assertEqual(zero_shot_classifier.entailment_id , -1 ) _UpperCAmelCase : Any = {"entailment": 0, "neutral": 1, "contradiction": 2} self.assertEqual(zero_shot_classifier.entailment_id , 0 ) _UpperCAmelCase : List[Any] = {"ENTAIL": 0, "NON-ENTAIL": 1} self.assertEqual(zero_shot_classifier.entailment_id , 0 ) _UpperCAmelCase : List[Any] = {"ENTAIL": 2, "NEUTRAL": 1, "CONTR": 0} self.assertEqual(zero_shot_classifier.entailment_id , 2 ) _UpperCAmelCase : Optional[int] = original_labelaid self.assertEqual(A , zero_shot_classifier.entailment_id ) @require_torch def _A ( self : Tuple ): _UpperCAmelCase : Union[str, Any] = pipeline( "zero-shot-classification" , model="sshleifer/tiny-distilbert-base-cased-distilled-squad" , framework="pt" , ) # There was a regression in 4.10 for this # Adding a test so we don't make the mistake again. # https://github.com/huggingface/transformers/issues/13381#issuecomment-912343499 zero_shot_classifier( "Who are you voting for in 2020?" * 100 , candidate_labels=["politics", "public health", "science"] ) @require_torch def _A ( self : Optional[int] ): _UpperCAmelCase : Optional[Any] = pipeline( "zero-shot-classification" , model="sshleifer/tiny-distilbert-base-cased-distilled-squad" , framework="pt" , ) _UpperCAmelCase : Optional[Any] = zero_shot_classifier( "Who are you voting for in 2020?" , candidate_labels=["politics", "public health", "science"] ) self.assertEqual( nested_simplify(A ) , { "sequence": "Who are you voting for in 2020?", "labels": ["science", "public health", "politics"], "scores": [0.333, 0.333, 0.333], } , ) @require_tf def _A ( self : int ): _UpperCAmelCase : Tuple = pipeline( "zero-shot-classification" , model="sshleifer/tiny-distilbert-base-cased-distilled-squad" , framework="tf" , ) _UpperCAmelCase : Union[str, Any] = zero_shot_classifier( "Who are you voting for in 2020?" , candidate_labels=["politics", "public health", "science"] ) self.assertEqual( nested_simplify(A ) , { "sequence": "Who are you voting for in 2020?", "labels": ["science", "public health", "politics"], "scores": [0.333, 0.333, 0.333], } , ) @slow @require_torch def _A ( self : Union[str, Any] ): _UpperCAmelCase : Any = pipeline("zero-shot-classification" , model="roberta-large-mnli" , framework="pt" ) _UpperCAmelCase : Any = zero_shot_classifier( "Who are you voting for in 2020?" , candidate_labels=["politics", "public health", "science"] ) self.assertEqual( nested_simplify(A ) , { "sequence": "Who are you voting for in 2020?", "labels": ["politics", "public health", "science"], "scores": [0.976, 0.015, 0.009], } , ) _UpperCAmelCase : Optional[int] = zero_shot_classifier( "The dominant sequence transduction models are based on complex recurrent or convolutional neural networks" " in an encoder-decoder configuration. The best performing models also connect the encoder and decoder" " through an attention mechanism. We propose a new simple network architecture, the Transformer, based" " solely on attention mechanisms, dispensing with recurrence and convolutions entirely. Experiments on two" " machine translation tasks show these models to be superior in quality while being more parallelizable" " and requiring significantly less time to train. Our model achieves 28.4 BLEU on the WMT 2014" " English-to-German translation task, improving over the existing best results, including ensembles by" " over 2 BLEU. On the WMT 2014 English-to-French translation task, our model establishes a new" " single-model state-of-the-art BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small" " fraction of the training costs of the best models from the literature. We show that the Transformer" " generalizes well to other tasks by applying it successfully to English constituency parsing both with" " large and limited training data." , candidate_labels=["machine learning", "statistics", "translation", "vision"] , multi_label=A , ) self.assertEqual( nested_simplify(A ) , { "sequence": ( "The dominant sequence transduction models are based on complex recurrent or convolutional neural" " networks in an encoder-decoder configuration. The best performing models also connect the" " encoder and decoder through an attention mechanism. We propose a new simple network" " architecture, the Transformer, based solely on attention mechanisms, dispensing with recurrence" " and convolutions entirely. Experiments on two machine translation tasks show these models to be" " superior in quality while being more parallelizable and requiring significantly less time to" " train. Our model achieves 28.4 BLEU on the WMT 2014 English-to-German translation task," " improving over the existing best results, including ensembles by over 2 BLEU. On the WMT 2014" " English-to-French translation task, our model establishes a new single-model state-of-the-art" " BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small fraction of the training" " costs of the best models from the literature. We show that the Transformer generalizes well to" " other tasks by applying it successfully to English constituency parsing both with large and" " limited training data." ), "labels": ["translation", "machine learning", "vision", "statistics"], "scores": [0.817, 0.713, 0.018, 0.018], } , ) @slow @require_tf def _A ( self : str ): _UpperCAmelCase : Tuple = pipeline("zero-shot-classification" , model="roberta-large-mnli" , framework="tf" ) _UpperCAmelCase : Optional[int] = zero_shot_classifier( "Who are you voting for in 2020?" , candidate_labels=["politics", "public health", "science"] ) self.assertEqual( nested_simplify(A ) , { "sequence": "Who are you voting for in 2020?", "labels": ["politics", "public health", "science"], "scores": [0.976, 0.015, 0.009], } , ) _UpperCAmelCase : Optional[int] = zero_shot_classifier( "The dominant sequence transduction models are based on complex recurrent or convolutional neural networks" " in an encoder-decoder configuration. The best performing models also connect the encoder and decoder" " through an attention mechanism. We propose a new simple network architecture, the Transformer, based" " solely on attention mechanisms, dispensing with recurrence and convolutions entirely. Experiments on two" " machine translation tasks show these models to be superior in quality while being more parallelizable" " and requiring significantly less time to train. Our model achieves 28.4 BLEU on the WMT 2014" " English-to-German translation task, improving over the existing best results, including ensembles by" " over 2 BLEU. On the WMT 2014 English-to-French translation task, our model establishes a new" " single-model state-of-the-art BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small" " fraction of the training costs of the best models from the literature. We show that the Transformer" " generalizes well to other tasks by applying it successfully to English constituency parsing both with" " large and limited training data." , candidate_labels=["machine learning", "statistics", "translation", "vision"] , multi_label=A , ) self.assertEqual( nested_simplify(A ) , { "sequence": ( "The dominant sequence transduction models are based on complex recurrent or convolutional neural" " networks in an encoder-decoder configuration. The best performing models also connect the" " encoder and decoder through an attention mechanism. We propose a new simple network" " architecture, the Transformer, based solely on attention mechanisms, dispensing with recurrence" " and convolutions entirely. Experiments on two machine translation tasks show these models to be" " superior in quality while being more parallelizable and requiring significantly less time to" " train. Our model achieves 28.4 BLEU on the WMT 2014 English-to-German translation task," " improving over the existing best results, including ensembles by over 2 BLEU. On the WMT 2014" " English-to-French translation task, our model establishes a new single-model state-of-the-art" " BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small fraction of the training" " costs of the best models from the literature. We show that the Transformer generalizes well to" " other tasks by applying it successfully to English constituency parsing both with large and" " limited training data." ), "labels": ["translation", "machine learning", "vision", "statistics"], "scores": [0.817, 0.713, 0.018, 0.018], } , )
31
'''simple docstring''' import bza import gzip import lzma import os import shutil import struct import tarfile import warnings import zipfile from abc import ABC, abstractmethod from pathlib import Path from typing import Dict, List, Optional, Type, Union from .. import config from .filelock import FileLock from .logging import get_logger __SCREAMING_SNAKE_CASE : Dict = get_logger(__name__) class lowerCamelCase_ : '''simple docstring''' def __init__( self : List[str] , A : Optional[str] = None ): _UpperCAmelCase : Dict = ( os.path.join(A , config.EXTRACTED_DATASETS_DIR ) if cache_dir else config.EXTRACTED_DATASETS_PATH ) _UpperCAmelCase : Union[str, Any] = Extractor def _A ( self : Tuple , A : str ): from .file_utils import hash_url_to_filename # Path where we extract compressed archives # We extract in the cache dir, and get the extracted path name by hashing the original path" _UpperCAmelCase : Dict = os.path.abspath(A ) return os.path.join(self.extract_dir , hash_url_to_filename(A ) ) def _A ( self : int , A : str , A : bool ): return force_extract or ( not os.path.isfile(A ) and not (os.path.isdir(A ) and os.listdir(A )) ) def _A ( self : Optional[int] , A : str , A : bool = False ): _UpperCAmelCase : Union[str, Any] = self.extractor.infer_extractor_format(A ) if not extractor_format: return input_path _UpperCAmelCase : Optional[Any] = self._get_output_path(A ) if self._do_extract(A , A ): self.extractor.extract(A , A , A ) return output_path class lowerCamelCase_ (snake_case__ ): '''simple docstring''' @classmethod @abstractmethod def _A ( cls : str , A : Union[Path, str] , **A : Dict ): ... @staticmethod @abstractmethod def _A ( A : Union[Path, str] , A : Union[Path, str] ): ... class lowerCamelCase_ (snake_case__ , snake_case__ ): '''simple docstring''' __UpperCamelCase: List[bytes] = [] @staticmethod def _A ( A : Union[Path, str] , A : int ): with open(A , "rb" ) as f: return f.read(A ) @classmethod def _A ( cls : Any , A : Union[Path, str] , A : bytes = b"" ): if not magic_number: _UpperCAmelCase : Any = max(len(A ) for cls_magic_number in cls.magic_numbers ) try: _UpperCAmelCase : int = cls.read_magic_number(A , A ) except OSError: return False return any(magic_number.startswith(A ) for cls_magic_number in cls.magic_numbers ) class lowerCamelCase_ (snake_case__ ): '''simple docstring''' @classmethod def _A ( cls : str , A : Union[Path, str] , **A : List[Any] ): return tarfile.is_tarfile(A ) @staticmethod def _A ( A : Union[str, Any] , A : str ): def resolved(A : str ) -> str: return os.path.realpath(os.path.abspath(A ) ) def badpath(A : str , A : str ) -> bool: # joinpath will ignore base if path is absolute return not resolved(os.path.join(A , A ) ).startswith(A ) def badlink(A : str , A : str ) -> bool: # Links are interpreted relative to the directory containing the link _UpperCAmelCase : List[str] = resolved(os.path.join(A , os.path.dirname(info.name ) ) ) return badpath(info.linkname , base=A ) _UpperCAmelCase : Optional[int] = resolved(A ) for finfo in members: if badpath(finfo.name , A ): logger.error(F"""Extraction of {finfo.name} is blocked (illegal path)""" ) elif finfo.issym() and badlink(A , A ): logger.error(F"""Extraction of {finfo.name} is blocked: Symlink to {finfo.linkname}""" ) elif finfo.islnk() and badlink(A , A ): logger.error(F"""Extraction of {finfo.name} is blocked: Hard link to {finfo.linkname}""" ) else: yield finfo @staticmethod def _A ( A : Union[Path, str] , A : Union[Path, str] ): os.makedirs(A , exist_ok=A ) _UpperCAmelCase : int = tarfile.open(A ) tar_file.extractall(A , members=TarExtractor.safemembers(A , A ) ) tar_file.close() class lowerCamelCase_ (snake_case__ ): '''simple docstring''' __UpperCamelCase: Union[str, Any] = [b"\x1F\x8B"] @staticmethod def _A ( A : Union[Path, str] , A : Union[Path, str] ): with gzip.open(A , "rb" ) as gzip_file: with open(A , "wb" ) as extracted_file: shutil.copyfileobj(A , A ) class lowerCamelCase_ (snake_case__ ): '''simple docstring''' __UpperCamelCase: Dict = [ b"PK\x03\x04", b"PK\x05\x06", # empty archive b"PK\x07\x08", # spanned archive ] @classmethod def _A ( cls : Dict , A : Union[Path, str] , A : bytes = b"" ): if super().is_extractable(A , magic_number=A ): return True try: # Alternative version of zipfile.is_zipfile that has less false positives, but misses executable zip archives. # From: https://github.com/python/cpython/pull/5053 from zipfile import ( _CD_SIGNATURE, _ECD_DISK_NUMBER, _ECD_DISK_START, _ECD_ENTRIES_TOTAL, _ECD_OFFSET, _ECD_SIZE, _EndRecData, sizeCentralDir, stringCentralDir, structCentralDir, ) with open(A , "rb" ) as fp: _UpperCAmelCase : Tuple = _EndRecData(A ) if endrec: if endrec[_ECD_ENTRIES_TOTAL] == 0 and endrec[_ECD_SIZE] == 0 and endrec[_ECD_OFFSET] == 0: return True # Empty zipfiles are still zipfiles elif endrec[_ECD_DISK_NUMBER] == endrec[_ECD_DISK_START]: fp.seek(endrec[_ECD_OFFSET] ) # Central directory is on the same disk if fp.tell() == endrec[_ECD_OFFSET] and endrec[_ECD_SIZE] >= sizeCentralDir: _UpperCAmelCase : Dict = fp.read(A ) # CD is where we expect it to be if len(A ) == sizeCentralDir: _UpperCAmelCase : Any = struct.unpack(A , A ) # CD is the right size if centdir[_CD_SIGNATURE] == stringCentralDir: return True # First central directory entry has correct magic number return False except Exception: # catch all errors in case future python versions change the zipfile internals return False @staticmethod def _A ( A : Union[Path, str] , A : Union[Path, str] ): os.makedirs(A , exist_ok=A ) with zipfile.ZipFile(A , "r" ) as zip_file: zip_file.extractall(A ) zip_file.close() class lowerCamelCase_ (snake_case__ ): '''simple docstring''' __UpperCamelCase: Dict = [b"\xFD\x37\x7A\x58\x5A\x00"] @staticmethod def _A ( A : Union[Path, str] , A : Union[Path, str] ): with lzma.open(A ) as compressed_file: with open(A , "wb" ) as extracted_file: shutil.copyfileobj(A , A ) class lowerCamelCase_ (snake_case__ ): '''simple docstring''' __UpperCamelCase: List[str] = [b"Rar!\x1a\x07\x00", b"Rar!\x1a\x07\x01\x00"] # RAR_ID # RAR5_ID @staticmethod def _A ( A : Union[Path, str] , A : Union[Path, str] ): if not config.RARFILE_AVAILABLE: raise ImportError("Please pip install rarfile" ) import rarfile os.makedirs(A , exist_ok=A ) _UpperCAmelCase : List[str] = rarfile.RarFile(A ) rf.extractall(A ) rf.close() class lowerCamelCase_ (snake_case__ ): '''simple docstring''' __UpperCamelCase: Optional[Any] = [b"\x28\xb5\x2F\xFD"] @staticmethod def _A ( A : Union[Path, str] , A : Union[Path, str] ): if not config.ZSTANDARD_AVAILABLE: raise ImportError("Please pip install zstandard" ) import zstandard as zstd _UpperCAmelCase : Optional[Any] = zstd.ZstdDecompressor() with open(A , "rb" ) as ifh, open(A , "wb" ) as ofh: dctx.copy_stream(A , A ) class lowerCamelCase_ (snake_case__ ): '''simple docstring''' __UpperCamelCase: Optional[Any] = [b"\x42\x5A\x68"] @staticmethod def _A ( A : Union[Path, str] , A : Union[Path, str] ): with bza.open(A , "rb" ) as compressed_file: with open(A , "wb" ) as extracted_file: shutil.copyfileobj(A , A ) class lowerCamelCase_ (snake_case__ ): '''simple docstring''' __UpperCamelCase: List[Any] = [b"\x37\x7A\xBC\xAF\x27\x1C"] @staticmethod def _A ( A : Union[Path, str] , A : Union[Path, str] ): if not config.PY7ZR_AVAILABLE: raise ImportError("Please pip install py7zr" ) import pyazr os.makedirs(A , exist_ok=A ) with pyazr.SevenZipFile(A , "r" ) as archive: archive.extractall(A ) class lowerCamelCase_ (snake_case__ ): '''simple docstring''' __UpperCamelCase: Optional[int] = [b"\x04\x22\x4D\x18"] @staticmethod def _A ( A : Union[Path, str] , A : Union[Path, str] ): if not config.LZ4_AVAILABLE: raise ImportError("Please pip install lz4" ) import lza.frame with lza.frame.open(A , "rb" ) as compressed_file: with open(A , "wb" ) as extracted_file: shutil.copyfileobj(A , A ) class lowerCamelCase_ : '''simple docstring''' __UpperCamelCase: Dict[str, Type[BaseExtractor]] = { "tar": TarExtractor, "gzip": GzipExtractor, "zip": ZipExtractor, "xz": XzExtractor, "rar": RarExtractor, "zstd": ZstdExtractor, "bz2": BzipaExtractor, "7z": SevenZipExtractor, # <Added version="2.4.0"/> "lz4": LzaExtractor, # <Added version="2.4.0"/> } @classmethod def _A ( cls : List[Any] ): return max( len(A ) for extractor in cls.extractors.values() if issubclass(A , A ) for extractor_magic_number in extractor.magic_numbers ) @staticmethod def _A ( A : Union[Path, str] , A : int ): try: return MagicNumberBaseExtractor.read_magic_number(A , magic_number_length=A ) except OSError: return b"" @classmethod def _A ( cls : Optional[Any] , A : Union[Path, str] , A : bool = False ): warnings.warn( "Method 'is_extractable' was deprecated in version 2.4.0 and will be removed in 3.0.0. " "Use 'infer_extractor_format' instead." , category=A , ) _UpperCAmelCase : Union[str, Any] = cls.infer_extractor_format(A ) if extractor_format: return True if not return_extractor else (True, cls.extractors[extractor_format]) return False if not return_extractor else (False, None) @classmethod def _A ( cls : Dict , A : Union[Path, str] ): # <Added version="2.4.0"/> _UpperCAmelCase : Optional[int] = cls._get_magic_number_max_length() _UpperCAmelCase : str = cls._read_magic_number(A , A ) for extractor_format, extractor in cls.extractors.items(): if extractor.is_extractable(A , magic_number=A ): return extractor_format @classmethod def _A ( cls : List[str] , A : Union[Path, str] , A : Union[Path, str] , A : Optional[str] = None , A : Optional[BaseExtractor] = "deprecated" , ): os.makedirs(os.path.dirname(A ) , exist_ok=A ) # Prevent parallel extractions _UpperCAmelCase : Tuple = str(Path(A ).with_suffix(".lock" ) ) with FileLock(A ): shutil.rmtree(A , ignore_errors=A ) if extractor_format or extractor != "deprecated": if extractor != "deprecated" or not isinstance(A , A ): # passed as positional arg warnings.warn( "Parameter 'extractor' was deprecated in version 2.4.0 and will be removed in 3.0.0. " "Use 'extractor_format' instead." , category=A , ) _UpperCAmelCase : Tuple = extractor if extractor != "deprecated" else extractor_format else: _UpperCAmelCase : Tuple = cls.extractors[extractor_format] return extractor.extract(A , A ) else: warnings.warn( "Parameter 'extractor_format' was made required in version 2.4.0 and not passing it will raise an " "exception in 3.0.0." , category=A , ) for extractor in cls.extractors.values(): if extractor.is_extractable(A ): return extractor.extract(A , A )
31
1
'''simple docstring''' import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import MgpstrTokenizer from transformers.models.mgp_str.tokenization_mgp_str import VOCAB_FILES_NAMES from transformers.testing_utils import require_torch, require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_torch_available, is_vision_available if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import MgpstrProcessor, ViTImageProcessor @require_torch @require_vision class lowerCamelCase_ (unittest.TestCase ): '''simple docstring''' __UpperCamelCase: Optional[Any] = ViTImageProcessor if is_vision_available() else None @property def _A ( self : Union[str, Any] ): return self.image_processor_tester.prepare_image_processor_dict() def _A ( self : Optional[int] ): _UpperCAmelCase : Any = (3, 32, 128) _UpperCAmelCase : Any = tempfile.mkdtemp() # fmt: off _UpperCAmelCase : Dict = ["[GO]", "[s]", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"] # fmt: on _UpperCAmelCase : List[str] = dict(zip(A , range(len(A ) ) ) ) _UpperCAmelCase : Optional[int] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["vocab_file"] ) with open(self.vocab_file , "w" , encoding="utf-8" ) as fp: fp.write(json.dumps(A ) + "\n" ) _UpperCAmelCase : str = { "do_normalize": False, "do_resize": True, "image_processor_type": "ViTImageProcessor", "resample": 3, "size": {"height": 32, "width": 128}, } _UpperCAmelCase : List[str] = os.path.join(self.tmpdirname , A ) with open(self.image_processor_file , "w" , encoding="utf-8" ) as fp: json.dump(A , A ) def _A ( self : Optional[Any] , **A : List[Any] ): return MgpstrTokenizer.from_pretrained(self.tmpdirname , **A ) def _A ( self : Dict , **A : List[str] ): return ViTImageProcessor.from_pretrained(self.tmpdirname , **A ) def _A ( self : List[Any] ): shutil.rmtree(self.tmpdirname ) def _A ( self : str ): _UpperCAmelCase : Union[str, Any] = np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta ) _UpperCAmelCase : Union[str, Any] = Image.fromarray(np.moveaxis(A , 0 , -1 ) ) return image_input def _A ( self : List[Any] ): _UpperCAmelCase : Tuple = self.get_tokenizer() _UpperCAmelCase : List[Any] = self.get_image_processor() _UpperCAmelCase : List[Any] = MgpstrProcessor(tokenizer=A , image_processor=A ) processor.save_pretrained(self.tmpdirname ) _UpperCAmelCase : Union[str, Any] = MgpstrProcessor.from_pretrained(self.tmpdirname , use_fast=A ) self.assertEqual(processor.char_tokenizer.get_vocab() , tokenizer.get_vocab() ) self.assertIsInstance(processor.char_tokenizer , A ) self.assertEqual(processor.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertIsInstance(processor.image_processor , A ) def _A ( self : Optional[int] ): _UpperCAmelCase : str = self.get_tokenizer() _UpperCAmelCase : Optional[Any] = self.get_image_processor() _UpperCAmelCase : str = MgpstrProcessor(tokenizer=A , image_processor=A ) processor.save_pretrained(self.tmpdirname ) _UpperCAmelCase : Dict = self.get_tokenizer(bos_token="(BOS)" , eos_token="(EOS)" ) _UpperCAmelCase : Dict = self.get_image_processor(do_normalize=A , padding_value=1.0 ) _UpperCAmelCase : Any = MgpstrProcessor.from_pretrained( self.tmpdirname , bos_token="(BOS)" , eos_token="(EOS)" , do_normalize=A , padding_value=1.0 ) self.assertEqual(processor.char_tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.char_tokenizer , A ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , A ) def _A ( self : int ): _UpperCAmelCase : int = self.get_image_processor() _UpperCAmelCase : str = self.get_tokenizer() _UpperCAmelCase : Optional[int] = MgpstrProcessor(tokenizer=A , image_processor=A ) _UpperCAmelCase : int = self.prepare_image_inputs() _UpperCAmelCase : str = image_processor(A , return_tensors="np" ) _UpperCAmelCase : List[Any] = processor(images=A , 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 _A ( self : Optional[Any] ): _UpperCAmelCase : Dict = self.get_image_processor() _UpperCAmelCase : Any = self.get_tokenizer() _UpperCAmelCase : str = MgpstrProcessor(tokenizer=A , image_processor=A ) _UpperCAmelCase : Any = "test" _UpperCAmelCase : Tuple = processor(text=A ) _UpperCAmelCase : Union[str, Any] = tokenizer(A ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def _A ( self : Any ): _UpperCAmelCase : Dict = self.get_image_processor() _UpperCAmelCase : Tuple = self.get_tokenizer() _UpperCAmelCase : Optional[Any] = MgpstrProcessor(tokenizer=A , image_processor=A ) _UpperCAmelCase : Any = "test" _UpperCAmelCase : Union[str, Any] = self.prepare_image_inputs() _UpperCAmelCase : str = processor(text=A , images=A ) self.assertListEqual(list(inputs.keys() ) , ["pixel_values", "labels"] ) # test if it raises when no input is passed with pytest.raises(A ): processor() def _A ( self : List[str] ): _UpperCAmelCase : Dict = self.get_image_processor() _UpperCAmelCase : int = self.get_tokenizer() _UpperCAmelCase : str = MgpstrProcessor(tokenizer=A , image_processor=A ) _UpperCAmelCase : List[Any] = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9], [3, 4, 3, 1, 1, 8, 9]] _UpperCAmelCase : Optional[int] = processor.char_decode(A ) _UpperCAmelCase : Tuple = tokenizer.batch_decode(A ) _UpperCAmelCase : Union[str, Any] = [seq.replace(" " , "" ) for seq in decoded_tok] self.assertListEqual(A , A ) def _A ( self : Optional[int] ): _UpperCAmelCase : Dict = self.get_image_processor() _UpperCAmelCase : str = self.get_tokenizer() _UpperCAmelCase : List[str] = MgpstrProcessor(tokenizer=A , image_processor=A ) _UpperCAmelCase : Any = None _UpperCAmelCase : int = self.prepare_image_inputs() _UpperCAmelCase : Dict = processor(text=A , images=A ) self.assertListEqual(list(inputs.keys() ) , processor.model_input_names ) def _A ( self : Union[str, Any] ): _UpperCAmelCase : Optional[int] = self.get_image_processor() _UpperCAmelCase : List[str] = self.get_tokenizer() _UpperCAmelCase : str = MgpstrProcessor(tokenizer=A , image_processor=A ) _UpperCAmelCase : List[Any] = torch.randn(1 , 27 , 38 ) _UpperCAmelCase : List[Any] = torch.randn(1 , 27 , 50257 ) _UpperCAmelCase : List[str] = torch.randn(1 , 27 , 30522 ) _UpperCAmelCase : Any = processor.batch_decode([char_input, bpe_input, wp_input] ) self.assertListEqual(list(results.keys() ) , ["generated_text", "scores", "char_preds", "bpe_preds", "wp_preds"] )
31
'''simple docstring''' from typing import Any def UpperCamelCase_ ( _UpperCAmelCase : list , _UpperCAmelCase : list , _UpperCAmelCase : dict , _UpperCAmelCase : dict , _UpperCAmelCase : dict , ) -> list: """simple docstring""" _validation( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , ) # Creates data structures and fill initial step _UpperCAmelCase : dict = {} _UpperCAmelCase : dict = {} for state in states_space: _UpperCAmelCase : Union[str, Any] = observations_space[0] _UpperCAmelCase : Tuple = ( initial_probabilities[state] * emission_probabilities[state][observation] ) _UpperCAmelCase : List[str] = None # Fills the data structure with the probabilities of # different transitions and pointers to previous states for o in range(1 , len(_UpperCAmelCase ) ): _UpperCAmelCase : Optional[Any] = observations_space[o] _UpperCAmelCase : int = observations_space[o - 1] for state in states_space: # Calculates the argmax for probability function _UpperCAmelCase : str = "" _UpperCAmelCase : Tuple = -1 for k_state in states_space: _UpperCAmelCase : Any = ( probabilities[(k_state, prior_observation)] * transition_probabilities[k_state][state] * emission_probabilities[state][observation] ) if probability > max_probability: _UpperCAmelCase : Union[str, Any] = probability _UpperCAmelCase : str = k_state # Update probabilities and pointers dicts _UpperCAmelCase : Optional[int] = ( probabilities[(arg_max, prior_observation)] * transition_probabilities[arg_max][state] * emission_probabilities[state][observation] ) _UpperCAmelCase : Tuple = arg_max # The final observation _UpperCAmelCase : Optional[Any] = observations_space[len(_UpperCAmelCase ) - 1] # argmax for given final observation _UpperCAmelCase : List[str] = "" _UpperCAmelCase : Any = -1 for k_state in states_space: _UpperCAmelCase : Optional[int] = probabilities[(k_state, final_observation)] if probability > max_probability: _UpperCAmelCase : int = probability _UpperCAmelCase : Dict = k_state _UpperCAmelCase : Dict = arg_max # Process pointers backwards _UpperCAmelCase : List[Any] = last_state _UpperCAmelCase : str = [] for o in range(len(_UpperCAmelCase ) - 1 , -1 , -1 ): result.append(_UpperCAmelCase ) _UpperCAmelCase : List[Any] = pointers[previous, observations_space[o]] result.reverse() return result def UpperCamelCase_ ( _UpperCAmelCase : Any , _UpperCAmelCase : Any , _UpperCAmelCase : Any , _UpperCAmelCase : Any , _UpperCAmelCase : Any , ) -> None: """simple docstring""" _validate_not_empty( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , ) _validate_lists(_UpperCAmelCase , _UpperCAmelCase ) _validate_dicts( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) def UpperCamelCase_ ( _UpperCAmelCase : Any , _UpperCAmelCase : Any , _UpperCAmelCase : Any , _UpperCAmelCase : Any , _UpperCAmelCase : Any , ) -> None: """simple docstring""" if not all( [ observations_space, states_space, initial_probabilities, transition_probabilities, emission_probabilities, ] ): raise ValueError("There's an empty parameter" ) def UpperCamelCase_ ( _UpperCAmelCase : Any , _UpperCAmelCase : Any ) -> None: """simple docstring""" _validate_list(_UpperCAmelCase , "observations_space" ) _validate_list(_UpperCAmelCase , "states_space" ) def UpperCamelCase_ ( _UpperCAmelCase : Any , _UpperCAmelCase : str ) -> None: """simple docstring""" if not isinstance(_object , _UpperCAmelCase ): _UpperCAmelCase : Optional[int] = F"""{var_name} must be a list""" raise ValueError(_UpperCAmelCase ) else: for x in _object: if not isinstance(_UpperCAmelCase , _UpperCAmelCase ): _UpperCAmelCase : Optional[int] = F"""{var_name} must be a list of strings""" raise ValueError(_UpperCAmelCase ) def UpperCamelCase_ ( _UpperCAmelCase : Any , _UpperCAmelCase : Any , _UpperCAmelCase : Any , ) -> None: """simple docstring""" _validate_dict(_UpperCAmelCase , "initial_probabilities" , _UpperCAmelCase ) _validate_nested_dict(_UpperCAmelCase , "transition_probabilities" ) _validate_nested_dict(_UpperCAmelCase , "emission_probabilities" ) def UpperCamelCase_ ( _UpperCAmelCase : Any , _UpperCAmelCase : str ) -> None: """simple docstring""" _validate_dict(_object , _UpperCAmelCase , _UpperCAmelCase ) for x in _object.values(): _validate_dict(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) def UpperCamelCase_ ( _UpperCAmelCase : Any , _UpperCAmelCase : str , _UpperCAmelCase : type , _UpperCAmelCase : bool = False ) -> None: """simple docstring""" if not isinstance(_object , _UpperCAmelCase ): _UpperCAmelCase : Any = F"""{var_name} must be a dict""" raise ValueError(_UpperCAmelCase ) if not all(isinstance(_UpperCAmelCase , _UpperCAmelCase ) for x in _object ): _UpperCAmelCase : Tuple = F"""{var_name} all keys must be strings""" raise ValueError(_UpperCAmelCase ) if not all(isinstance(_UpperCAmelCase , _UpperCAmelCase ) for x in _object.values() ): _UpperCAmelCase : List[str] = "nested dictionary " if nested else "" _UpperCAmelCase : List[str] = F"""{var_name} {nested_text}all values must be {value_type.__name__}""" raise ValueError(_UpperCAmelCase ) if __name__ == "__main__": from doctest import testmod testmod()
31
1
'''simple docstring''' from collections import defaultdict from typing import Optional from ..image_utils import load_image from ..utils import ( add_end_docstrings, is_torch_available, logging, requires_backends, ) from .base import PIPELINE_INIT_ARGS, ChunkPipeline if is_torch_available(): import torch from ..models.auto.modeling_auto import MODEL_FOR_MASK_GENERATION_MAPPING __SCREAMING_SNAKE_CASE : str = logging.get_logger(__name__) @add_end_docstrings(snake_case__ ) class lowerCamelCase_ (snake_case__ ): '''simple docstring''' def __init__( self : Dict , **A : int ): super().__init__(**A ) requires_backends(self , "vision" ) requires_backends(self , "torch" ) if self.framework != "pt": raise ValueError(F"""The {self.__class__} is only available in PyTorch.""" ) self.check_model_type(A ) def _A ( self : Optional[int] , **A : Optional[Any] ): _UpperCAmelCase : Any = {} _UpperCAmelCase : Optional[Any] = {} _UpperCAmelCase : Union[str, Any] = {} # preprocess args if "points_per_batch" in kwargs: _UpperCAmelCase : Tuple = kwargs["points_per_batch"] if "points_per_crop" in kwargs: _UpperCAmelCase : List[Any] = kwargs["points_per_crop"] if "crops_n_layers" in kwargs: _UpperCAmelCase : List[str] = kwargs["crops_n_layers"] if "crop_overlap_ratio" in kwargs: _UpperCAmelCase : List[str] = kwargs["crop_overlap_ratio"] if "crop_n_points_downscale_factor" in kwargs: _UpperCAmelCase : List[str] = kwargs["crop_n_points_downscale_factor"] # postprocess args if "pred_iou_thresh" in kwargs: _UpperCAmelCase : Optional[Any] = kwargs["pred_iou_thresh"] if "stability_score_offset" in kwargs: _UpperCAmelCase : str = kwargs["stability_score_offset"] if "mask_threshold" in kwargs: _UpperCAmelCase : str = kwargs["mask_threshold"] if "stability_score_thresh" in kwargs: _UpperCAmelCase : str = kwargs["stability_score_thresh"] if "crops_nms_thresh" in kwargs: _UpperCAmelCase : List[Any] = kwargs["crops_nms_thresh"] if "output_rle_mask" in kwargs: _UpperCAmelCase : Any = kwargs["output_rle_mask"] if "output_bboxes_mask" in kwargs: _UpperCAmelCase : str = kwargs["output_bboxes_mask"] return preprocess_kwargs, forward_params, postprocess_kwargs def __call__( self : Optional[Any] , A : List[str] , *A : List[Any] , A : str=None , A : str=None , **A : str ): return super().__call__(A , *A , num_workers=A , batch_size=A , **A ) def _A ( self : Optional[Any] , A : Optional[Any] , A : List[Any]=64 , A : int = 0 , A : float = 512 / 1500 , A : Optional[int] = 32 , A : Optional[int] = 1 , ): _UpperCAmelCase : List[str] = load_image(A ) _UpperCAmelCase : int = self.image_processor.size["longest_edge"] _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase : Optional[int] = self.image_processor.generate_crop_boxes( A , A , A , A , A , A ) _UpperCAmelCase : List[Any] = self.image_processor(images=A , return_tensors="pt" ) with self.device_placement(): if self.framework == "pt": _UpperCAmelCase : List[str] = self.get_inference_context() with inference_context(): _UpperCAmelCase : Any = self._ensure_tensor_on_device(A , device=self.device ) _UpperCAmelCase : Dict = self.model.get_image_embeddings(model_inputs.pop("pixel_values" ) ) _UpperCAmelCase : List[str] = image_embeddings _UpperCAmelCase : Union[str, Any] = grid_points.shape[1] _UpperCAmelCase : Union[str, Any] = points_per_batch if points_per_batch is not None else n_points if points_per_batch <= 0: raise ValueError( "Cannot have points_per_batch<=0. Must be >=1 to returned batched outputs. " "To return all points at once, set points_per_batch to None" ) for i in range(0 , A , A ): _UpperCAmelCase : int = grid_points[:, i : i + points_per_batch, :, :] _UpperCAmelCase : Dict = input_labels[:, i : i + points_per_batch] _UpperCAmelCase : Optional[Any] = i == n_points - points_per_batch yield { "input_points": batched_points, "input_labels": labels, "input_boxes": crop_boxes, "is_last": is_last, **model_inputs, } def _A ( self : List[str] , A : List[str] , A : Tuple=0.88 , A : Union[str, Any]=0.95 , A : Union[str, Any]=0 , A : Optional[Any]=1 , ): _UpperCAmelCase : Any = model_inputs.pop("input_boxes" ) _UpperCAmelCase : Optional[Any] = model_inputs.pop("is_last" ) _UpperCAmelCase : str = model_inputs.pop("original_sizes" ).tolist() _UpperCAmelCase : Optional[int] = model_inputs.pop("reshaped_input_sizes" ).tolist() _UpperCAmelCase : List[Any] = self.model(**A ) # post processing happens here in order to avoid CPU GPU copies of ALL the masks _UpperCAmelCase : Union[str, Any] = model_outputs["pred_masks"] _UpperCAmelCase : List[str] = self.image_processor.post_process_masks( A , A , A , A , binarize=A ) _UpperCAmelCase : Any = model_outputs["iou_scores"] _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase : Tuple = self.image_processor.filter_masks( masks[0] , iou_scores[0] , original_sizes[0] , input_boxes[0] , A , A , A , A , ) return { "masks": masks, "is_last": is_last, "boxes": boxes, "iou_scores": iou_scores, } def _A ( self : List[str] , A : List[str] , A : Optional[int]=False , A : List[str]=False , A : Tuple=0.7 , ): _UpperCAmelCase : Tuple = [] _UpperCAmelCase : int = [] _UpperCAmelCase : Tuple = [] for model_output in model_outputs: all_scores.append(model_output.pop("iou_scores" ) ) all_masks.extend(model_output.pop("masks" ) ) all_boxes.append(model_output.pop("boxes" ) ) _UpperCAmelCase : int = torch.cat(A ) _UpperCAmelCase : Tuple = torch.cat(A ) _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase : Optional[int] = self.image_processor.post_process_for_mask_generation( A , A , A , A ) _UpperCAmelCase : Any = defaultdict(A ) for output in model_outputs: for k, v in output.items(): extra[k].append(A ) _UpperCAmelCase : List[str] = {} if output_rle_mask: _UpperCAmelCase : Optional[Any] = rle_mask if output_bboxes_mask: _UpperCAmelCase : str = bounding_boxes return {"masks": output_masks, "scores": iou_scores, **optional, **extra}
31
'''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 MobileViTImageProcessor class lowerCamelCase_ (unittest.TestCase ): '''simple docstring''' def __init__( self : Optional[Any] , A : Dict , A : Dict=7 , A : Optional[int]=3 , A : Optional[int]=18 , A : Dict=30 , A : List[Any]=400 , A : Union[str, Any]=True , A : Tuple=None , A : List[Any]=True , A : int=None , A : Optional[int]=True , ): _UpperCAmelCase : Optional[int] = size if size is not None else {"shortest_edge": 20} _UpperCAmelCase : Optional[Any] = crop_size if crop_size is not None else {"height": 18, "width": 18} _UpperCAmelCase : List[Any] = parent _UpperCAmelCase : Union[str, Any] = batch_size _UpperCAmelCase : Optional[Any] = num_channels _UpperCAmelCase : Union[str, Any] = image_size _UpperCAmelCase : int = min_resolution _UpperCAmelCase : Optional[int] = max_resolution _UpperCAmelCase : List[str] = do_resize _UpperCAmelCase : Optional[Any] = size _UpperCAmelCase : Tuple = do_center_crop _UpperCAmelCase : Optional[int] = crop_size _UpperCAmelCase : Optional[Any] = do_flip_channel_order def _A ( self : Dict ): return { "do_resize": self.do_resize, "size": self.size, "do_center_crop": self.do_center_crop, "crop_size": self.crop_size, "do_flip_channel_order": self.do_flip_channel_order, } @require_torch @require_vision class lowerCamelCase_ (snake_case__ , unittest.TestCase ): '''simple docstring''' __UpperCamelCase: Tuple = MobileViTImageProcessor if is_vision_available() else None def _A ( self : List[Any] ): _UpperCAmelCase : Any = MobileViTImageProcessingTester(self ) @property def _A ( self : int ): return self.image_processor_tester.prepare_image_processor_dict() def _A ( self : Tuple ): _UpperCAmelCase : int = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(A , "do_resize" ) ) self.assertTrue(hasattr(A , "size" ) ) self.assertTrue(hasattr(A , "do_center_crop" ) ) self.assertTrue(hasattr(A , "center_crop" ) ) self.assertTrue(hasattr(A , "do_flip_channel_order" ) ) def _A ( self : Any ): _UpperCAmelCase : List[str] = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"shortest_edge": 20} ) self.assertEqual(image_processor.crop_size , {"height": 18, "width": 18} ) _UpperCAmelCase : Dict = self.image_processing_class.from_dict(self.image_processor_dict , size=42 , crop_size=84 ) self.assertEqual(image_processor.size , {"shortest_edge": 42} ) self.assertEqual(image_processor.crop_size , {"height": 84, "width": 84} ) def _A ( self : Any ): pass def _A ( self : Dict ): # Initialize image_processing _UpperCAmelCase : List[str] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images _UpperCAmelCase : Union[str, Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=A ) for image in image_inputs: self.assertIsInstance(A , Image.Image ) # Test not batched input _UpperCAmelCase : List[Any] = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) # Test batched _UpperCAmelCase : Optional[Any] = image_processing(A , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) def _A ( self : Union[str, Any] ): # Initialize image_processing _UpperCAmelCase : Dict = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors _UpperCAmelCase : Optional[Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=A , numpify=A ) for image in image_inputs: self.assertIsInstance(A , np.ndarray ) # Test not batched input _UpperCAmelCase : 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.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) # Test batched _UpperCAmelCase : Optional[int] = image_processing(A , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) def _A ( self : Any ): # Initialize image_processing _UpperCAmelCase : Any = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors _UpperCAmelCase : Optional[Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=A , torchify=A ) for image in image_inputs: self.assertIsInstance(A , torch.Tensor ) # Test not batched input _UpperCAmelCase : 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.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) # Test batched _UpperCAmelCase : Any = image_processing(A , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , )
31
1
'''simple docstring''' import html from ...feature_extraction_utils import BatchFeature, FeatureExtractionMixin from ...utils import is_bsa_available, logging, requires_backends if is_bsa_available(): import bsa from bsa import BeautifulSoup __SCREAMING_SNAKE_CASE : Any = logging.get_logger(__name__) class lowerCamelCase_ (snake_case__ ): '''simple docstring''' def __init__( self : List[str] , **A : Tuple ): requires_backends(self , ["bs4"] ) super().__init__(**A ) def _A ( self : Any , A : Any ): _UpperCAmelCase : List[str] = [] _UpperCAmelCase : str = [] _UpperCAmelCase : int = element if element.name else element.parent for parent in child.parents: # type: bs4.element.Tag _UpperCAmelCase : Any = parent.find_all(child.name , recursive=A ) xpath_tags.append(child.name ) xpath_subscripts.append( 0 if 1 == len(A ) else next(i for i, s in enumerate(A , 1 ) if s is child ) ) _UpperCAmelCase : Dict = parent xpath_tags.reverse() xpath_subscripts.reverse() return xpath_tags, xpath_subscripts def _A ( self : List[str] , A : List[Any] ): _UpperCAmelCase : Tuple = BeautifulSoup(A , "html.parser" ) _UpperCAmelCase : Optional[int] = [] _UpperCAmelCase : Union[str, Any] = [] _UpperCAmelCase : Optional[int] = [] for element in html_code.descendants: if type(A ) == bsa.element.NavigableString: if type(element.parent ) != bsa.element.Tag: continue _UpperCAmelCase : Optional[Any] = html.unescape(A ).strip() if not text_in_this_tag: continue all_doc_strings.append(A ) _UpperCAmelCase , _UpperCAmelCase : int = self.xpath_soup(A ) stringaxtag_seq.append(A ) stringaxsubs_seq.append(A ) if len(A ) != len(A ): raise ValueError("Number of doc strings and xtags does not correspond" ) if len(A ) != len(A ): raise ValueError("Number of doc strings and xsubs does not correspond" ) return all_doc_strings, stringaxtag_seq, stringaxsubs_seq def _A ( self : Optional[int] , A : Tuple , A : Tuple ): _UpperCAmelCase : str = "" for tagname, subs in zip(A , A ): xpath += F"""/{tagname}""" if subs != 0: xpath += F"""[{subs}]""" return xpath def __call__( self : Optional[Any] , A : str ): _UpperCAmelCase : int = False # Check that strings has a valid type if isinstance(A , A ): _UpperCAmelCase : Optional[int] = True elif isinstance(A , (list, tuple) ): if len(A ) == 0 or isinstance(html_strings[0] , A ): _UpperCAmelCase : List[Any] = True if not valid_strings: raise ValueError( "HTML strings must of type `str`, `List[str]` (batch of examples), " F"""but is of type {type(A )}.""" ) _UpperCAmelCase : List[str] = bool(isinstance(A , (list, tuple) ) and (isinstance(html_strings[0] , A )) ) if not is_batched: _UpperCAmelCase : Tuple = [html_strings] # Get nodes + xpaths _UpperCAmelCase : List[str] = [] _UpperCAmelCase : str = [] for html_string in html_strings: _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase : Dict = self.get_three_from_single(A ) nodes.append(A ) _UpperCAmelCase : Optional[int] = [] for node, tag_list, sub_list in zip(A , A , A ): _UpperCAmelCase : Dict = self.construct_xpath(A , A ) xpath_strings.append(A ) xpaths.append(A ) # return as Dict _UpperCAmelCase : str = {"nodes": nodes, "xpaths": xpaths} _UpperCAmelCase : Any = BatchFeature(data=A , tensor_type=A ) return encoded_inputs
31
'''simple docstring''' def UpperCamelCase_ ( _UpperCAmelCase : int , _UpperCAmelCase : int ) -> int: """simple docstring""" _UpperCAmelCase : List[str] = 1 # To kept the Calculated Value # Since C(n, k) = C(n, n-k) if k > (n - k): _UpperCAmelCase : Any = n - k # Calculate C(n,k) for i in range(_UpperCAmelCase ): result *= n - i result //= i + 1 return result def UpperCamelCase_ ( _UpperCAmelCase : int ) -> int: """simple docstring""" return binomial_coefficient(2 * node_count , _UpperCAmelCase ) // (node_count + 1) def UpperCamelCase_ ( _UpperCAmelCase : int ) -> int: """simple docstring""" if n < 0: raise ValueError("factorial() not defined for negative values" ) _UpperCAmelCase : List[str] = 1 for i in range(1 , n + 1 ): result *= i return result def UpperCamelCase_ ( _UpperCAmelCase : int ) -> int: """simple docstring""" return catalan_number(_UpperCAmelCase ) * factorial(_UpperCAmelCase ) if __name__ == "__main__": __SCREAMING_SNAKE_CASE : Dict = int(input("""Enter the number of nodes: """).strip() or 0) if node_count <= 0: raise ValueError("""We need some nodes to work with.""") print( F'Given {node_count} nodes, there are {binary_tree_count(node_count)} ' F'binary trees and {catalan_number(node_count)} binary search trees.' )
31
1
'''simple docstring''' import argparse from collections import defaultdict def UpperCamelCase_ ( _UpperCAmelCase : Any , _UpperCAmelCase : List[Any] , _UpperCAmelCase : Optional[int] , _UpperCAmelCase : Any , _UpperCAmelCase : Union[str, Any] ) -> Dict: """simple docstring""" _UpperCAmelCase : Optional[Any] = F"""{file}_{class_name}_{test_name}""" done_test[_id] += 1 with open(_UpperCAmelCase , "r" ) as f: _UpperCAmelCase : Union[str, Any] = f.readlines() _UpperCAmelCase : List[Any] = F"""class {class_name}(""" _UpperCAmelCase : int = F"""{4 * ' '}def {test_name}(""" _UpperCAmelCase : Union[str, Any] = F"""{8 * ' '}{correct_line.split()[0]}""" _UpperCAmelCase : Union[str, Any] = F"""{16 * ' '}{correct_line.split()[0]}""" _UpperCAmelCase : Union[str, Any] = False _UpperCAmelCase : Optional[Any] = False _UpperCAmelCase : List[str] = False _UpperCAmelCase : Optional[Any] = False _UpperCAmelCase : List[Any] = 0 _UpperCAmelCase : str = 0 _UpperCAmelCase : Tuple = [] for line in lines: if line.startswith(_UpperCAmelCase ): _UpperCAmelCase : List[Any] = True elif in_class and line.startswith(_UpperCAmelCase ): _UpperCAmelCase : List[str] = True elif in_class and in_func and (line.startswith(_UpperCAmelCase ) or line.startswith(_UpperCAmelCase )): _UpperCAmelCase : Dict = len(line.split(correct_line.split()[0] )[0] ) count += 1 if count == done_test[_id]: _UpperCAmelCase : Tuple = True if in_class and in_func and in_line: if ")" not in line: continue else: _UpperCAmelCase : str = True if in_class and in_func and in_line and insert_line: new_lines.append(F"""{spaces * ' '}{correct_line}""" ) _UpperCAmelCase : str = False else: new_lines.append(_UpperCAmelCase ) with open(_UpperCAmelCase , "w" ) as f: for line in new_lines: f.write(_UpperCAmelCase ) def UpperCamelCase_ ( _UpperCAmelCase : Optional[Any] , _UpperCAmelCase : str=None ) -> Optional[int]: """simple docstring""" if fail is not None: with open(_UpperCAmelCase , "r" ) as f: _UpperCAmelCase : Any = {l.strip() for l in f.readlines()} else: _UpperCAmelCase : int = None with open(_UpperCAmelCase , "r" ) as f: _UpperCAmelCase : Any = f.readlines() _UpperCAmelCase : Any = defaultdict(_UpperCAmelCase ) for line in correct_lines: _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase : Optional[Any] = line.split(";" ) if test_failures is None or "::".join([file, class_name, test_name] ) in test_failures: overwrite_file(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) if __name__ == "__main__": __SCREAMING_SNAKE_CASE : int = argparse.ArgumentParser() parser.add_argument("""--correct_filename""", help="""filename of tests with expected result""") parser.add_argument("""--fail_filename""", help="""filename of test failures""", type=str, default=None) __SCREAMING_SNAKE_CASE : Any = parser.parse_args() main(args.correct_filename, args.fail_filename)
31
'''simple docstring''' import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_distilbert import DistilBertTokenizer __SCREAMING_SNAKE_CASE : str = logging.get_logger(__name__) __SCREAMING_SNAKE_CASE : str = {"""vocab_file""": """vocab.txt""", """tokenizer_file""": """tokenizer.json"""} __SCREAMING_SNAKE_CASE : Dict = { """vocab_file""": { """distilbert-base-uncased""": """https://huggingface.co/distilbert-base-uncased/resolve/main/vocab.txt""", """distilbert-base-uncased-distilled-squad""": ( """https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/vocab.txt""" ), """distilbert-base-cased""": """https://huggingface.co/distilbert-base-cased/resolve/main/vocab.txt""", """distilbert-base-cased-distilled-squad""": ( """https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/vocab.txt""" ), """distilbert-base-german-cased""": """https://huggingface.co/distilbert-base-german-cased/resolve/main/vocab.txt""", """distilbert-base-multilingual-cased""": ( """https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/vocab.txt""" ), }, """tokenizer_file""": { """distilbert-base-uncased""": """https://huggingface.co/distilbert-base-uncased/resolve/main/tokenizer.json""", """distilbert-base-uncased-distilled-squad""": ( """https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/tokenizer.json""" ), """distilbert-base-cased""": """https://huggingface.co/distilbert-base-cased/resolve/main/tokenizer.json""", """distilbert-base-cased-distilled-squad""": ( """https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/tokenizer.json""" ), """distilbert-base-german-cased""": ( """https://huggingface.co/distilbert-base-german-cased/resolve/main/tokenizer.json""" ), """distilbert-base-multilingual-cased""": ( """https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/tokenizer.json""" ), }, } __SCREAMING_SNAKE_CASE : Optional[Any] = { """distilbert-base-uncased""": 512, """distilbert-base-uncased-distilled-squad""": 512, """distilbert-base-cased""": 512, """distilbert-base-cased-distilled-squad""": 512, """distilbert-base-german-cased""": 512, """distilbert-base-multilingual-cased""": 512, } __SCREAMING_SNAKE_CASE : List[Any] = { """distilbert-base-uncased""": {"""do_lower_case""": True}, """distilbert-base-uncased-distilled-squad""": {"""do_lower_case""": True}, """distilbert-base-cased""": {"""do_lower_case""": False}, """distilbert-base-cased-distilled-squad""": {"""do_lower_case""": False}, """distilbert-base-german-cased""": {"""do_lower_case""": False}, """distilbert-base-multilingual-cased""": {"""do_lower_case""": False}, } class lowerCamelCase_ (snake_case__ ): '''simple docstring''' __UpperCamelCase: Union[str, Any] = VOCAB_FILES_NAMES __UpperCamelCase: str = PRETRAINED_VOCAB_FILES_MAP __UpperCamelCase: Optional[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCamelCase: Any = PRETRAINED_INIT_CONFIGURATION __UpperCamelCase: str = ["input_ids", "attention_mask"] __UpperCamelCase: List[str] = DistilBertTokenizer def __init__( self : str , A : int=None , A : Tuple=None , A : Tuple=True , A : Dict="[UNK]" , A : List[Any]="[SEP]" , A : Optional[Any]="[PAD]" , A : Dict="[CLS]" , A : Tuple="[MASK]" , A : str=True , A : Dict=None , **A : List[Any] , ): super().__init__( A , tokenizer_file=A , do_lower_case=A , unk_token=A , sep_token=A , pad_token=A , cls_token=A , mask_token=A , tokenize_chinese_chars=A , strip_accents=A , **A , ) _UpperCAmelCase : str = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get("lowercase" , A ) != do_lower_case or normalizer_state.get("strip_accents" , A ) != strip_accents or normalizer_state.get("handle_chinese_chars" , A ) != tokenize_chinese_chars ): _UpperCAmelCase : Dict = getattr(A , normalizer_state.pop("type" ) ) _UpperCAmelCase : int = do_lower_case _UpperCAmelCase : Optional[int] = strip_accents _UpperCAmelCase : str = tokenize_chinese_chars _UpperCAmelCase : List[Any] = normalizer_class(**A ) _UpperCAmelCase : Dict = do_lower_case def _A ( self : List[Any] , A : Tuple , A : Any=None ): _UpperCAmelCase : Optional[int] = [self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def _A ( self : int , A : List[int] , A : Optional[List[int]] = None ): _UpperCAmelCase : Any = [self.sep_token_id] _UpperCAmelCase : str = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def _A ( self : Dict , A : str , A : Optional[str] = None ): _UpperCAmelCase : Any = self._tokenizer.model.save(A , name=A ) return tuple(A )
31
1
'''simple docstring''' import os import unittest from transformers import LayoutLMTokenizer, LayoutLMTokenizerFast from transformers.models.layoutlm.tokenization_layoutlm import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class lowerCamelCase_ (snake_case__ , unittest.TestCase ): '''simple docstring''' __UpperCamelCase: Optional[Any] = LayoutLMTokenizer __UpperCamelCase: Dict = LayoutLMTokenizerFast __UpperCamelCase: List[Any] = True __UpperCamelCase: Union[str, Any] = True def _A ( self : str ): super().setUp() _UpperCAmelCase : Tuple = [ "[UNK]", "[CLS]", "[SEP]", "want", "##want", "##ed", "wa", "un", "runn", "##ing", ",", "low", "lowest", ] _UpperCAmelCase : str = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["vocab_file"] ) with open(self.vocab_file , "w" , encoding="utf-8" ) as vocab_writer: vocab_writer.write("".join([x + "\n" for x in vocab_tokens] ) ) def _A ( self : str , **A : List[str] ): return LayoutLMTokenizer.from_pretrained(self.tmpdirname , **A ) def _A ( self : List[str] , A : List[Any] ): _UpperCAmelCase : int = "UNwant\u00E9d,running" _UpperCAmelCase : Optional[Any] = "unwanted, running" return input_text, output_text def _A ( self : int ): _UpperCAmelCase : List[Any] = self.tokenizer_class(self.vocab_file ) _UpperCAmelCase : List[Any] = tokenizer.tokenize("UNwant\u00E9d,running" ) self.assertListEqual(A , ["un", "##want", "##ed", ",", "runn", "##ing"] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(A ) , [7, 4, 5, 10, 8, 9] ) def _A ( self : Union[str, Any] ): pass
31
'''simple docstring''' import shutil import tempfile import unittest from unittest.mock import patch from transformers import ( DefaultFlowCallback, IntervalStrategy, PrinterCallback, ProgressCallback, Trainer, TrainerCallback, TrainingArguments, is_torch_available, ) from transformers.testing_utils import require_torch if is_torch_available(): from transformers.trainer import DEFAULT_CALLBACKS from .test_trainer import RegressionDataset, RegressionModelConfig, RegressionPreTrainedModel class lowerCamelCase_ (snake_case__ ): '''simple docstring''' def __init__( self : List[Any] ): _UpperCAmelCase : Union[str, Any] = [] def _A ( self : Any , A : Union[str, Any] , A : Optional[int] , A : List[str] , **A : Tuple ): self.events.append("on_init_end" ) def _A ( self : Any , A : str , A : List[Any] , A : List[Any] , **A : Tuple ): self.events.append("on_train_begin" ) def _A ( self : Tuple , A : List[str] , A : Tuple , A : int , **A : List[str] ): self.events.append("on_train_end" ) def _A ( self : Optional[Any] , A : Dict , A : Any , A : Optional[Any] , **A : List[Any] ): self.events.append("on_epoch_begin" ) def _A ( self : Optional[Any] , A : List[Any] , A : List[str] , A : Optional[int] , **A : Optional[int] ): self.events.append("on_epoch_end" ) def _A ( self : List[str] , A : Optional[int] , A : List[Any] , A : Union[str, Any] , **A : Any ): self.events.append("on_step_begin" ) def _A ( self : Tuple , A : Union[str, Any] , A : int , A : Optional[int] , **A : int ): self.events.append("on_step_end" ) def _A ( self : Optional[int] , A : Optional[Any] , A : Union[str, Any] , A : str , **A : Union[str, Any] ): self.events.append("on_evaluate" ) def _A ( self : Optional[Any] , A : Optional[int] , A : Dict , A : List[Any] , **A : Dict ): self.events.append("on_predict" ) def _A ( self : Dict , A : Dict , A : List[Any] , A : Dict , **A : str ): self.events.append("on_save" ) def _A ( self : Tuple , A : Optional[Any] , A : Union[str, Any] , A : Optional[int] , **A : Dict ): self.events.append("on_log" ) def _A ( self : Optional[int] , A : Optional[Any] , A : Tuple , A : Tuple , **A : List[str] ): self.events.append("on_prediction_step" ) @require_torch class lowerCamelCase_ (unittest.TestCase ): '''simple docstring''' def _A ( self : Optional[int] ): _UpperCAmelCase : Optional[Any] = tempfile.mkdtemp() def _A ( self : List[Any] ): shutil.rmtree(self.output_dir ) def _A ( self : Union[str, Any] , A : Optional[int]=0 , A : Optional[Any]=0 , A : Optional[Any]=64 , A : Dict=64 , A : Any=None , A : Tuple=False , **A : Optional[int] ): # disable_tqdm in TrainingArguments has a flaky default since it depends on the level of logging. We make sure # its set to False since the tests later on depend on its value. _UpperCAmelCase : str = RegressionDataset(length=A ) _UpperCAmelCase : Union[str, Any] = RegressionDataset(length=A ) _UpperCAmelCase : Any = RegressionModelConfig(a=A , b=A ) _UpperCAmelCase : List[Any] = RegressionPreTrainedModel(A ) _UpperCAmelCase : Dict = TrainingArguments(self.output_dir , disable_tqdm=A , report_to=[] , **A ) return Trainer( A , A , train_dataset=A , eval_dataset=A , callbacks=A , ) def _A ( self : str , A : List[str] , A : List[str] ): self.assertEqual(len(A ) , len(A ) ) # Order doesn't matter _UpperCAmelCase : Tuple = sorted(A , key=lambda A : cb.__name__ if isinstance(A , A ) else cb.__class__.__name__ ) _UpperCAmelCase : Any = sorted(A , key=lambda A : cb.__name__ if isinstance(A , A ) else cb.__class__.__name__ ) for cba, cba in zip(A , A ): if isinstance(A , A ) and isinstance(A , A ): self.assertEqual(A , A ) elif isinstance(A , A ) and not isinstance(A , A ): self.assertEqual(A , cba.__class__ ) elif not isinstance(A , A ) and isinstance(A , A ): self.assertEqual(cba.__class__ , A ) else: self.assertEqual(A , A ) def _A ( self : int , A : List[str] ): _UpperCAmelCase : List[str] = ["on_init_end", "on_train_begin"] _UpperCAmelCase : str = 0 _UpperCAmelCase : Optional[Any] = len(trainer.get_eval_dataloader() ) _UpperCAmelCase : Optional[int] = ["on_prediction_step"] * len(trainer.get_eval_dataloader() ) + ["on_log", "on_evaluate"] for _ in range(trainer.state.num_train_epochs ): expected_events.append("on_epoch_begin" ) for _ in range(A ): step += 1 expected_events += ["on_step_begin", "on_step_end"] if step % trainer.args.logging_steps == 0: expected_events.append("on_log" ) if trainer.args.evaluation_strategy == IntervalStrategy.STEPS and step % trainer.args.eval_steps == 0: expected_events += evaluation_events.copy() if step % trainer.args.save_steps == 0: expected_events.append("on_save" ) expected_events.append("on_epoch_end" ) if trainer.args.evaluation_strategy == IntervalStrategy.EPOCH: expected_events += evaluation_events.copy() expected_events += ["on_log", "on_train_end"] return expected_events def _A ( self : str ): _UpperCAmelCase : Any = self.get_trainer() _UpperCAmelCase : int = DEFAULT_CALLBACKS.copy() + [ProgressCallback] self.check_callbacks_equality(trainer.callback_handler.callbacks , A ) # Callbacks passed at init are added to the default callbacks _UpperCAmelCase : Optional[int] = self.get_trainer(callbacks=[MyTestTrainerCallback] ) expected_callbacks.append(A ) self.check_callbacks_equality(trainer.callback_handler.callbacks , A ) # TrainingArguments.disable_tqdm controls if use ProgressCallback or PrinterCallback _UpperCAmelCase : List[Any] = self.get_trainer(disable_tqdm=A ) _UpperCAmelCase : Tuple = DEFAULT_CALLBACKS.copy() + [PrinterCallback] self.check_callbacks_equality(trainer.callback_handler.callbacks , A ) def _A ( self : Optional[Any] ): _UpperCAmelCase : Dict = DEFAULT_CALLBACKS.copy() + [ProgressCallback] _UpperCAmelCase : Dict = self.get_trainer() # We can add, pop, or remove by class name trainer.remove_callback(A ) expected_callbacks.remove(A ) self.check_callbacks_equality(trainer.callback_handler.callbacks , A ) _UpperCAmelCase : Optional[Any] = self.get_trainer() _UpperCAmelCase : Any = trainer.pop_callback(A ) self.assertEqual(cb.__class__ , A ) self.check_callbacks_equality(trainer.callback_handler.callbacks , A ) trainer.add_callback(A ) expected_callbacks.insert(0 , A ) self.check_callbacks_equality(trainer.callback_handler.callbacks , A ) # We can also add, pop, or remove by instance _UpperCAmelCase : Union[str, Any] = self.get_trainer() _UpperCAmelCase : List[Any] = trainer.callback_handler.callbacks[0] trainer.remove_callback(A ) expected_callbacks.remove(A ) self.check_callbacks_equality(trainer.callback_handler.callbacks , A ) _UpperCAmelCase : List[Any] = self.get_trainer() _UpperCAmelCase : List[Any] = trainer.callback_handler.callbacks[0] _UpperCAmelCase : Union[str, Any] = trainer.pop_callback(A ) self.assertEqual(A , A ) self.check_callbacks_equality(trainer.callback_handler.callbacks , A ) trainer.add_callback(A ) expected_callbacks.insert(0 , A ) self.check_callbacks_equality(trainer.callback_handler.callbacks , A ) def _A ( self : Optional[Any] ): import warnings # XXX: for now ignore scatter_gather warnings in this test since it's not relevant to what's being tested warnings.simplefilter(action="ignore" , category=A ) _UpperCAmelCase : Optional[int] = self.get_trainer(callbacks=[MyTestTrainerCallback] ) trainer.train() _UpperCAmelCase : Union[str, Any] = trainer.callback_handler.callbacks[-2].events self.assertEqual(A , self.get_expected_events(A ) ) # Independent log/save/eval _UpperCAmelCase : Tuple = self.get_trainer(callbacks=[MyTestTrainerCallback] , logging_steps=5 ) trainer.train() _UpperCAmelCase : int = trainer.callback_handler.callbacks[-2].events self.assertEqual(A , self.get_expected_events(A ) ) _UpperCAmelCase : List[str] = self.get_trainer(callbacks=[MyTestTrainerCallback] , save_steps=5 ) trainer.train() _UpperCAmelCase : Tuple = trainer.callback_handler.callbacks[-2].events self.assertEqual(A , self.get_expected_events(A ) ) _UpperCAmelCase : int = self.get_trainer(callbacks=[MyTestTrainerCallback] , eval_steps=5 , evaluation_strategy="steps" ) trainer.train() _UpperCAmelCase : Optional[int] = trainer.callback_handler.callbacks[-2].events self.assertEqual(A , self.get_expected_events(A ) ) _UpperCAmelCase : Optional[int] = self.get_trainer(callbacks=[MyTestTrainerCallback] , evaluation_strategy="epoch" ) trainer.train() _UpperCAmelCase : int = trainer.callback_handler.callbacks[-2].events self.assertEqual(A , self.get_expected_events(A ) ) # A bit of everything _UpperCAmelCase : int = self.get_trainer( callbacks=[MyTestTrainerCallback] , logging_steps=3 , save_steps=10 , eval_steps=5 , evaluation_strategy="steps" , ) trainer.train() _UpperCAmelCase : Optional[int] = trainer.callback_handler.callbacks[-2].events self.assertEqual(A , self.get_expected_events(A ) ) # warning should be emitted for duplicated callbacks with patch("transformers.trainer_callback.logger.warning" ) as warn_mock: _UpperCAmelCase : Optional[Any] = self.get_trainer( callbacks=[MyTestTrainerCallback, MyTestTrainerCallback] , ) assert str(A ) in warn_mock.call_args[0][0]
31
1
'''simple docstring''' from typing import List from ...configuration_utils import PretrainedConfig from ...utils import logging __SCREAMING_SNAKE_CASE : Optional[Any] = logging.get_logger(__name__) __SCREAMING_SNAKE_CASE : List[str] = { """snap-research/efficientformer-l1-300""": ( """https://huggingface.co/snap-research/efficientformer-l1-300/resolve/main/config.json""" ), } class lowerCamelCase_ (snake_case__ ): '''simple docstring''' __UpperCamelCase: int = "efficientformer" def __init__( self : List[Any] , A : List[int] = [3, 2, 6, 4] , A : List[int] = [48, 96, 224, 448] , A : List[bool] = [True, True, True, True] , A : int = 448 , A : int = 32 , A : int = 4 , A : int = 7 , A : int = 5 , A : int = 8 , A : int = 4 , A : float = 0.0 , A : int = 16 , A : int = 3 , A : int = 3 , A : int = 3 , A : int = 2 , A : int = 1 , A : float = 0.0 , A : int = 1 , A : bool = True , A : bool = True , A : float = 1E-5 , A : str = "gelu" , A : float = 0.02 , A : float = 1E-12 , A : int = 224 , A : float = 1E-05 , **A : Optional[int] , ): super().__init__(**A ) _UpperCAmelCase : int = hidden_act _UpperCAmelCase : Tuple = hidden_dropout_prob _UpperCAmelCase : Optional[Any] = hidden_sizes _UpperCAmelCase : List[Any] = num_hidden_layers _UpperCAmelCase : Any = num_attention_heads _UpperCAmelCase : Any = initializer_range _UpperCAmelCase : Optional[int] = layer_norm_eps _UpperCAmelCase : int = patch_size _UpperCAmelCase : List[str] = num_channels _UpperCAmelCase : Any = depths _UpperCAmelCase : int = mlp_expansion_ratio _UpperCAmelCase : Union[str, Any] = downsamples _UpperCAmelCase : Union[str, Any] = dim _UpperCAmelCase : List[str] = key_dim _UpperCAmelCase : Tuple = attention_ratio _UpperCAmelCase : int = resolution _UpperCAmelCase : Optional[Any] = pool_size _UpperCAmelCase : str = downsample_patch_size _UpperCAmelCase : str = downsample_stride _UpperCAmelCase : Optional[int] = downsample_pad _UpperCAmelCase : int = drop_path_rate _UpperCAmelCase : Union[str, Any] = num_metaad_blocks _UpperCAmelCase : List[Any] = distillation _UpperCAmelCase : Optional[int] = use_layer_scale _UpperCAmelCase : Optional[int] = layer_scale_init_value _UpperCAmelCase : str = image_size _UpperCAmelCase : List[Any] = batch_norm_eps
31
'''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_video_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import VivitImageProcessor class lowerCamelCase_ (unittest.TestCase ): '''simple docstring''' def __init__( self : int , A : Dict , A : Optional[int]=7 , A : Tuple=3 , A : Optional[Any]=10 , A : int=18 , A : Dict=30 , A : List[str]=400 , A : int=True , A : Optional[Any]=None , A : Optional[Any]=True , A : List[Any]=[0.5, 0.5, 0.5] , A : List[str]=[0.5, 0.5, 0.5] , A : Optional[int]=None , ): _UpperCAmelCase : Dict = size if size is not None else {"shortest_edge": 18} _UpperCAmelCase : Optional[Any] = crop_size if crop_size is not None else {"height": 18, "width": 18} _UpperCAmelCase : Tuple = parent _UpperCAmelCase : Any = batch_size _UpperCAmelCase : Optional[int] = num_channels _UpperCAmelCase : Optional[Any] = num_frames _UpperCAmelCase : Any = image_size _UpperCAmelCase : Dict = min_resolution _UpperCAmelCase : Any = max_resolution _UpperCAmelCase : Optional[int] = do_resize _UpperCAmelCase : str = size _UpperCAmelCase : List[Any] = do_normalize _UpperCAmelCase : Any = image_mean _UpperCAmelCase : Tuple = image_std _UpperCAmelCase : Any = crop_size def _A ( self : List[Any] ): return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "size": self.size, "crop_size": self.crop_size, } @require_torch @require_vision class lowerCamelCase_ (snake_case__ , unittest.TestCase ): '''simple docstring''' __UpperCamelCase: Dict = VivitImageProcessor if is_vision_available() else None def _A ( self : int ): _UpperCAmelCase : Tuple = VivitImageProcessingTester(self ) @property def _A ( self : Optional[Any] ): return self.image_processor_tester.prepare_image_processor_dict() def _A ( self : Union[str, Any] ): _UpperCAmelCase : str = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(A , "image_mean" ) ) self.assertTrue(hasattr(A , "image_std" ) ) self.assertTrue(hasattr(A , "do_normalize" ) ) self.assertTrue(hasattr(A , "do_resize" ) ) self.assertTrue(hasattr(A , "do_center_crop" ) ) self.assertTrue(hasattr(A , "size" ) ) def _A ( self : List[Any] ): _UpperCAmelCase : Union[str, Any] = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"shortest_edge": 18} ) self.assertEqual(image_processor.crop_size , {"height": 18, "width": 18} ) _UpperCAmelCase : Optional[int] = self.image_processing_class.from_dict(self.image_processor_dict , size=42 , crop_size=84 ) self.assertEqual(image_processor.size , {"shortest_edge": 42} ) self.assertEqual(image_processor.crop_size , {"height": 84, "width": 84} ) def _A ( self : Tuple ): # Initialize image_processing _UpperCAmelCase : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PIL videos _UpperCAmelCase : Any = prepare_video_inputs(self.image_processor_tester , equal_resolution=A ) for video in video_inputs: self.assertIsInstance(A , A ) self.assertIsInstance(video[0] , Image.Image ) # Test not batched input _UpperCAmelCase : str = image_processing(video_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_videos.shape , ( 1, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) # Test batched _UpperCAmelCase : List[Any] = image_processing(A , return_tensors="pt" ).pixel_values self.assertEqual( encoded_videos.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) def _A ( self : List[Any] ): # Initialize image_processing _UpperCAmelCase : List[str] = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors _UpperCAmelCase : List[Any] = prepare_video_inputs(self.image_processor_tester , equal_resolution=A , numpify=A ) for video in video_inputs: self.assertIsInstance(A , A ) self.assertIsInstance(video[0] , np.ndarray ) # Test not batched input _UpperCAmelCase : Tuple = image_processing(video_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_videos.shape , ( 1, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) # Test batched _UpperCAmelCase : Optional[int] = image_processing(A , return_tensors="pt" ).pixel_values self.assertEqual( encoded_videos.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) def _A ( self : List[Any] ): # Initialize image_processing _UpperCAmelCase : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors _UpperCAmelCase : Optional[int] = prepare_video_inputs(self.image_processor_tester , equal_resolution=A , torchify=A ) for video in video_inputs: self.assertIsInstance(A , A ) self.assertIsInstance(video[0] , torch.Tensor ) # Test not batched input _UpperCAmelCase : Optional[Any] = image_processing(video_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_videos.shape , ( 1, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) # Test batched _UpperCAmelCase : List[Any] = image_processing(A , return_tensors="pt" ).pixel_values self.assertEqual( encoded_videos.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , )
31
1