code
stringlengths
81
54k
code_codestyle
int64
0
721
style_context
stringlengths
91
41.9k
style_context_codestyle
int64
0
699
label
int64
0
1
import logging import sys from dataclasses import dataclass, field from typing import Any, Dict, List, Optional, Union import librosa import torch from datasets import DatasetDict, load_dataset from packaging import version from torch import nn from transformers import ( HfArgumentParser, Trainer, TrainingArguments, WavaVecaConfig, WavaVecaFeatureExtractor, WavaVecaForPreTraining, is_apex_available, trainer_utils, ) from transformers.models.wavaveca.modeling_wavaveca import _compute_mask_indices if is_apex_available(): from apex import amp if version.parse(version.parse(torch.__version__).base_version) >= version.parse("1.6"): __UpperCAmelCase : List[str] = True from torch.cuda.amp import autocast __UpperCAmelCase : str = logging.getLogger(__name__) @dataclass class UpperCAmelCase_ : '''simple docstring''' __UpperCamelCase : str = field( metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models"}) __UpperCamelCase : Optional[str] = field( default=_a, metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co"}, ) __UpperCamelCase : Optional[bool] = field( default=_a, metadata={"help": "Whether to freeze the feature extractor layers of the model."}) __UpperCamelCase : Optional[bool] = field( default=_a, metadata={"help": "Whether to log verbose messages or not."}, ) __UpperCamelCase : Optional[float] = field( default=2.0, metadata={"help": "Maximum temperature for gumbel softmax."}) __UpperCamelCase : Optional[float] = field( default=0.5, metadata={"help": "Minimum temperature for gumbel softmax."}) __UpperCamelCase : Optional[float] = field( default=0.9_9_9_9_9_5, metadata={"help": "Decay of gumbel temperature during training."}) def a ( SCREAMING_SNAKE_CASE_ : ModelArguments , SCREAMING_SNAKE_CASE_ : TrainingArguments ): """simple docstring""" logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''' , datefmt='''%m/%d/%Y %H:%M:%S''' , handlers=[logging.StreamHandler(sys.stdout )] , ) UpperCamelCase : Optional[Any] = logging.WARNING if model_args.verbose_logging: UpperCamelCase : Dict = logging.DEBUG elif trainer_utils.is_main_process(training_args.local_rank ): UpperCamelCase : str = logging.INFO logger.setLevel(SCREAMING_SNAKE_CASE_ ) @dataclass class UpperCAmelCase_ : '''simple docstring''' __UpperCamelCase : str = field( default=_a, metadata={"help": "The name of the dataset to use (via the datasets library)."}) __UpperCamelCase : Optional[str] = field( default=_a, metadata={"help": "The configuration name of the dataset to use (via the datasets library)."}) __UpperCamelCase : Optional[str] = field( default="train", metadata={ "help": "The name of the training data set split to use (via the datasets library). Defaults to 'train'" }, ) __UpperCamelCase : Optional[str] = field( default="validation", metadata={ "help": ( "The name of the validation data set split to use (via the datasets library). Defaults to 'validation'" ) }, ) __UpperCamelCase : Optional[str] = field( default="file", metadata={"help": "Column in the dataset that contains speech file path. Defaults to 'file'"}, ) __UpperCamelCase : bool = field( default=_a, metadata={"help": "Overwrite the cached preprocessed datasets or not."}) __UpperCamelCase : Optional[int] = field( default=1, metadata={ "help": "The percentage of the train set used as validation set in case there's no validation split" }, ) __UpperCamelCase : Optional[int] = field( default=_a, metadata={"help": "The number of processes to use for the preprocessing."}, ) __UpperCamelCase : Optional[float] = field( default=2_0.0, metadata={"help": "Filter audio files that are longer than `max_duration_in_seconds` seconds"}) @dataclass class UpperCAmelCase_ : '''simple docstring''' __UpperCamelCase : WavaVecaForPreTraining __UpperCamelCase : WavaVecaFeatureExtractor __UpperCamelCase : Union[bool, str] = "longest" __UpperCamelCase : Optional[int] = None __UpperCamelCase : Optional[int] = None def __call__( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Optional[Any] = self.feature_extractor.pad( __SCREAMING_SNAKE_CASE , max_length=self.max_length , padding=self.padding , pad_to_multiple_of=self.pad_to_multiple_of , return_tensors='''pt''' , ) UpperCamelCase : Dict = self.model._get_feat_extract_output_lengths(batch['''input_values'''].shape[-1] ) UpperCamelCase : Dict = batch['''input_values'''].shape[0] # make sure that no loss is computed on padded inputs if batch["attention_mask"] is not None: # compute real output lengths according to convolution formula UpperCamelCase : Tuple = self.model._get_feat_extract_output_lengths(batch['''attention_mask'''].sum(-1 ) ).to( torch.long ) UpperCamelCase : Optional[int] = torch.zeros( (batch_size, mask_indices_seq_length) , dtype=torch.long , device=batch['''input_values'''].device ) # these two operations makes sure that all values # before the output lengths indices are attended to UpperCamelCase : str = 1 UpperCamelCase : List[Any] = attention_mask.flip([-1] ).cumsum(-1 ).flip([-1] ).bool() # sample randomly masked indices UpperCamelCase : str = _compute_mask_indices( (batch_size, mask_indices_seq_length) , self.model.config.mask_time_prob , self.model.config.mask_time_length , attention_mask=__SCREAMING_SNAKE_CASE , min_masks=2 , ) return batch class UpperCAmelCase_ ( _a): '''simple docstring''' def __init__( self , *__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=1 , __SCREAMING_SNAKE_CASE=0 , __SCREAMING_SNAKE_CASE=1.0 , **__SCREAMING_SNAKE_CASE ): """simple docstring""" super().__init__(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) UpperCamelCase : Tuple = 0 UpperCamelCase : List[str] = max_gumbel_temp UpperCamelCase : Optional[Any] = min_gumbel_temp UpperCamelCase : Optional[int] = gumbel_temp_decay def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" model.train() UpperCamelCase : List[str] = self._prepare_inputs(__SCREAMING_SNAKE_CASE ) if self.use_amp: with autocast(): UpperCamelCase : Tuple = self.compute_loss(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else: UpperCamelCase : Dict = self.compute_loss(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) if self.args.n_gpu > 1 or self.deepspeed: if model.module.config.ctc_loss_reduction == "mean": UpperCamelCase : Any = loss.mean() elif model.module.config.ctc_loss_reduction == "sum": UpperCamelCase : Dict = loss.sum() / (inputs['''mask_time_indices''']).sum() else: raise ValueError(f"""{model.config.ctc_loss_reduction} is not valid. Choose one of ['mean', 'sum']""" ) if self.args.gradient_accumulation_steps > 1: UpperCamelCase : List[Any] = loss / self.args.gradient_accumulation_steps if self.use_amp: self.scaler.scale(__SCREAMING_SNAKE_CASE ).backward() elif self.use_apex: with amp.scale_loss(__SCREAMING_SNAKE_CASE , self.optimizer ) as scaled_loss: scaled_loss.backward() elif self.deepspeed: self.deepspeed.backward(__SCREAMING_SNAKE_CASE ) else: loss.backward() self.num_update_step += 1 # make sure gumbel softmax temperature is decayed if self.args.n_gpu > 1 or self.deepspeed: model.module.set_gumbel_temperature( max(self.max_gumbel_temp * self.gumbel_temp_decay**self.num_update_step , self.min_gumbel_temp ) ) else: model.set_gumbel_temperature( max(self.max_gumbel_temp * self.gumbel_temp_decay**self.num_update_step , self.min_gumbel_temp ) ) return loss.detach() def a ( ): """simple docstring""" UpperCamelCase : List[str] = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) UpperCamelCase , UpperCamelCase , UpperCamelCase : List[str] = parser.parse_args_into_dataclasses() configure_logger(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # Downloading and loading a dataset from the hub. UpperCamelCase : Optional[Any] = load_dataset(data_args.dataset_name , data_args.dataset_config_name , cache_dir=model_args.cache_dir ) if "validation" not in datasets.keys(): # make sure only "validation" and "train" keys remain" UpperCamelCase : List[Any] = DatasetDict() UpperCamelCase : Optional[int] = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=F"""{data_args.train_split_name}[:{data_args.validation_split_percentage}%]""" , cache_dir=model_args.cache_dir , ) UpperCamelCase : Any = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=F"""{data_args.train_split_name}[{data_args.validation_split_percentage}%:]""" , cache_dir=model_args.cache_dir , ) else: # make sure only "validation" and "train" keys remain" UpperCamelCase : Optional[Any] = DatasetDict() UpperCamelCase : Dict = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split='''validation''' , cache_dir=model_args.cache_dir , ) UpperCamelCase : List[Any] = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=F"""{data_args.train_split_name}""" , cache_dir=model_args.cache_dir , ) # only normalized-inputs-training is supported UpperCamelCase : Dict = WavaVecaFeatureExtractor.from_pretrained( model_args.model_name_or_path , cache_dir=model_args.cache_dir , do_normalize=SCREAMING_SNAKE_CASE_ ) def prepare_dataset(SCREAMING_SNAKE_CASE_ : str ): # check that all files have the correct sampling rate UpperCamelCase , UpperCamelCase : Optional[Any] = librosa.load(batch[data_args.speech_file_column] , sr=feature_extractor.sampling_rate ) return batch # load audio files into numpy arrays UpperCamelCase : Union[str, Any] = datasets.map( SCREAMING_SNAKE_CASE_ , num_proc=data_args.preprocessing_num_workers , remove_columns=datasets['''train'''].column_names ) # filter audio files that are too long UpperCamelCase : Optional[Any] = vectorized_datasets.filter( lambda SCREAMING_SNAKE_CASE_ : len(data['''speech'''] ) < int(data_args.max_duration_in_seconds * feature_extractor.sampling_rate ) ) def normalize(SCREAMING_SNAKE_CASE_ : Optional[Any] ): return feature_extractor(batch['''speech'''] , sampling_rate=feature_extractor.sampling_rate ) # normalize and transform to `BatchFeatures` UpperCamelCase : int = vectorized_datasets.map( SCREAMING_SNAKE_CASE_ , batched=SCREAMING_SNAKE_CASE_ , num_proc=data_args.preprocessing_num_workers , load_from_cache_file=not data_args.overwrite_cache , remove_columns=vectorized_datasets['''train'''].column_names , ) # pretraining is only supported for "newer" stable layer norm architecture # apply_spec_augment has to be True, mask_feature_prob has to be 0.0 UpperCamelCase : Union[str, Any] = WavaVecaConfig.from_pretrained( model_args.model_name_or_path , cache_dir=model_args.cache_dir , gradient_checkpointing=training_args.gradient_checkpointing , ) if not config.do_stable_layer_norm or config.feat_extract_norm != "layer": raise ValueError( '''PreTraining is only supported for ``config.do_stable_layer_norm=True`` and''' ''' ``config.feat_extract_norm=\'layer\'''' ) UpperCamelCase : Union[str, Any] = WavaVecaForPreTraining(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Dict = DataCollatorForWavaVecaPretraining(model=SCREAMING_SNAKE_CASE_ , feature_extractor=SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Tuple = WavaVecaPreTrainer( model=SCREAMING_SNAKE_CASE_ , data_collator=SCREAMING_SNAKE_CASE_ , args=SCREAMING_SNAKE_CASE_ , train_dataset=vectorized_datasets['''train'''] , eval_dataset=vectorized_datasets['''validation'''] , tokenizer=SCREAMING_SNAKE_CASE_ , max_gumbel_temp=model_args.max_gumbel_temperature , min_gumbel_temp=model_args.min_gumbel_temperature , gumbel_temp_decay=model_args.gumbel_temperature_decay , ) trainer.train() if __name__ == "__main__": main()
643
def a ( SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int ): """simple docstring""" if a < 0 or b < 0: raise ValueError('''the value of both inputs must be positive''' ) UpperCamelCase : int = str(bin(SCREAMING_SNAKE_CASE_ ) )[2:] # remove the leading "0b" UpperCamelCase : List[str] = str(bin(SCREAMING_SNAKE_CASE_ ) )[2:] UpperCamelCase : Tuple = max(len(SCREAMING_SNAKE_CASE_ ) , len(SCREAMING_SNAKE_CASE_ ) ) return "0b" + "".join( str(int('''1''' in (char_a, char_b) ) ) for char_a, char_b in zip(a_binary.zfill(SCREAMING_SNAKE_CASE_ ) , b_binary.zfill(SCREAMING_SNAKE_CASE_ ) ) ) if __name__ == "__main__": import doctest doctest.testmod()
643
1
import io import json import fsspec import pytest from datasets import Dataset, DatasetDict, Features, NamedSplit, Value from datasets.io.json import JsonDatasetReader, JsonDatasetWriter from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases def a ( SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : Optional[Any] ): """simple docstring""" assert isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) assert dataset.num_rows == 4 assert dataset.num_columns == 3 assert dataset.column_names == ["col_1", "col_2", "col_3"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize('''keep_in_memory''' , [False, True] ) def a ( SCREAMING_SNAKE_CASE_ : Optional[Any] , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Union[str, Any] ): """simple docstring""" UpperCamelCase : List[Any] = tmp_path / '''cache''' UpperCamelCase : Optional[Any] = {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): UpperCamelCase : Optional[int] = JsonDatasetReader(SCREAMING_SNAKE_CASE_ , cache_dir=SCREAMING_SNAKE_CASE_ , keep_in_memory=SCREAMING_SNAKE_CASE_ ).read() _check_json_dataset(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) @pytest.mark.parametrize( '''features''' , [ None, {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}, {'''col_1''': '''string''', '''col_2''': '''string''', '''col_3''': '''string'''}, {'''col_1''': '''int32''', '''col_2''': '''int32''', '''col_3''': '''int32'''}, {'''col_1''': '''float32''', '''col_2''': '''float32''', '''col_3''': '''float32'''}, ] , ) def a ( SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Optional[Any] , SCREAMING_SNAKE_CASE_ : List[str] ): """simple docstring""" UpperCamelCase : int = tmp_path / '''cache''' UpperCamelCase : Tuple = {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''} UpperCamelCase : Optional[int] = features.copy() if features else default_expected_features UpperCamelCase : Optional[int] = ( Features({feature: Value(SCREAMING_SNAKE_CASE_ ) for feature, dtype in features.items()} ) if features is not None else None ) UpperCamelCase : Optional[int] = JsonDatasetReader(SCREAMING_SNAKE_CASE_ , features=SCREAMING_SNAKE_CASE_ , cache_dir=SCREAMING_SNAKE_CASE_ ).read() _check_json_dataset(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) @pytest.mark.parametrize( '''features''' , [ None, {'''col_3''': '''float64''', '''col_1''': '''string''', '''col_2''': '''int64'''}, ] , ) def a ( SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Optional[Any] ): """simple docstring""" UpperCamelCase : Tuple = tmp_path / '''cache''' UpperCamelCase : List[Any] = {'''col_3''': '''float64''', '''col_1''': '''string''', '''col_2''': '''int64'''} UpperCamelCase : Dict = features.copy() if features else default_expected_features UpperCamelCase : Optional[int] = ( Features({feature: Value(SCREAMING_SNAKE_CASE_ ) for feature, dtype in features.items()} ) if features is not None else None ) UpperCamelCase : Optional[Any] = JsonDatasetReader(SCREAMING_SNAKE_CASE_ , features=SCREAMING_SNAKE_CASE_ , cache_dir=SCREAMING_SNAKE_CASE_ ).read() assert isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) assert dataset.num_rows == 2 assert dataset.num_columns == 3 assert dataset.column_names == ["col_3", "col_1", "col_2"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype def a ( SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : List[Any] ): """simple docstring""" UpperCamelCase : Any = {'''col_2''': '''int64''', '''col_3''': '''float64''', '''col_1''': '''string'''} UpperCamelCase : Union[str, Any] = features.copy() UpperCamelCase : List[str] = ( Features({feature: Value(SCREAMING_SNAKE_CASE_ ) for feature, dtype in features.items()} ) if features is not None else None ) UpperCamelCase : Dict = tmp_path / '''cache''' UpperCamelCase : Tuple = JsonDatasetReader(SCREAMING_SNAKE_CASE_ , features=SCREAMING_SNAKE_CASE_ , cache_dir=SCREAMING_SNAKE_CASE_ ).read() assert isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) assert dataset.num_rows == 2 assert dataset.num_columns == 3 assert dataset.column_names == ["col_2", "col_3", "col_1"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize('''split''' , [None, NamedSplit('''train''' ), '''train''', '''test'''] ) def a ( SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Any ): """simple docstring""" UpperCamelCase : Tuple = tmp_path / '''cache''' UpperCamelCase : List[Any] = {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''} UpperCamelCase : int = JsonDatasetReader(SCREAMING_SNAKE_CASE_ , cache_dir=SCREAMING_SNAKE_CASE_ , split=SCREAMING_SNAKE_CASE_ ).read() _check_json_dataset(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) assert dataset.split == split if split else "train" @pytest.mark.parametrize('''path_type''' , [str, list] ) def a ( SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Union[str, Any] ): """simple docstring""" if issubclass(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): UpperCamelCase : List[Any] = jsonl_path elif issubclass(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): UpperCamelCase : Optional[Any] = [jsonl_path] UpperCamelCase : Optional[Any] = tmp_path / '''cache''' UpperCamelCase : str = {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''} UpperCamelCase : Any = JsonDatasetReader(SCREAMING_SNAKE_CASE_ , cache_dir=SCREAMING_SNAKE_CASE_ ).read() _check_json_dataset(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def a ( SCREAMING_SNAKE_CASE_ : List[str] , SCREAMING_SNAKE_CASE_ : Tuple , SCREAMING_SNAKE_CASE_ : List[Any]=("train",) ): """simple docstring""" assert isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) for split in splits: UpperCamelCase : List[Any] = dataset_dict[split] assert dataset.num_rows == 4 assert dataset.num_columns == 3 assert dataset.column_names == ["col_1", "col_2", "col_3"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize('''keep_in_memory''' , [False, True] ) def a ( SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : Dict ): """simple docstring""" UpperCamelCase : int = tmp_path / '''cache''' UpperCamelCase : Optional[int] = {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): UpperCamelCase : str = JsonDatasetReader({'''train''': jsonl_path} , cache_dir=SCREAMING_SNAKE_CASE_ , keep_in_memory=SCREAMING_SNAKE_CASE_ ).read() _check_json_datasetdict(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) @pytest.mark.parametrize( '''features''' , [ None, {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}, {'''col_1''': '''string''', '''col_2''': '''string''', '''col_3''': '''string'''}, {'''col_1''': '''int32''', '''col_2''': '''int32''', '''col_3''': '''int32'''}, {'''col_1''': '''float32''', '''col_2''': '''float32''', '''col_3''': '''float32'''}, ] , ) def a ( SCREAMING_SNAKE_CASE_ : Optional[Any] , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : Tuple ): """simple docstring""" UpperCamelCase : int = tmp_path / '''cache''' UpperCamelCase : Union[str, Any] = {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''} UpperCamelCase : List[str] = features.copy() if features else default_expected_features UpperCamelCase : Union[str, Any] = ( Features({feature: Value(SCREAMING_SNAKE_CASE_ ) for feature, dtype in features.items()} ) if features is not None else None ) UpperCamelCase : Union[str, Any] = JsonDatasetReader({'''train''': jsonl_path} , features=SCREAMING_SNAKE_CASE_ , cache_dir=SCREAMING_SNAKE_CASE_ ).read() _check_json_datasetdict(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) @pytest.mark.parametrize('''split''' , [None, NamedSplit('''train''' ), '''train''', '''test'''] ) def a ( SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : List[str] ): """simple docstring""" if split: UpperCamelCase : Optional[int] = {split: jsonl_path} else: UpperCamelCase : Tuple = '''train''' UpperCamelCase : int = {'''train''': jsonl_path, '''test''': jsonl_path} UpperCamelCase : Tuple = tmp_path / '''cache''' UpperCamelCase : int = {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''} UpperCamelCase : Optional[Any] = JsonDatasetReader(SCREAMING_SNAKE_CASE_ , cache_dir=SCREAMING_SNAKE_CASE_ ).read() _check_json_datasetdict(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , splits=list(path.keys() ) ) assert all(dataset[split].split == split for split in path.keys() ) def a ( SCREAMING_SNAKE_CASE_ : Dict ): """simple docstring""" return json.load(SCREAMING_SNAKE_CASE_ ) def a ( SCREAMING_SNAKE_CASE_ : Union[str, Any] ): """simple docstring""" return [json.loads(SCREAMING_SNAKE_CASE_ ) for line in buffer] class UpperCAmelCase_ : '''simple docstring''' @pytest.mark.parametrize('''lines, load_json_function''' , [(True, load_json_lines), (False, load_json)] ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" with io.BytesIO() as buffer: JsonDatasetWriter(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , lines=__SCREAMING_SNAKE_CASE ).write() buffer.seek(0 ) UpperCamelCase : Tuple = load_json_function(__SCREAMING_SNAKE_CASE ) assert isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) assert isinstance(exported_content[0] , __SCREAMING_SNAKE_CASE ) assert len(__SCREAMING_SNAKE_CASE ) == 10 @pytest.mark.parametrize( '''orient, container, keys, len_at''' , [ ('''records''', list, {'''tokens''', '''labels''', '''answers''', '''id'''}, None), ('''split''', dict, {'''columns''', '''data'''}, '''data'''), ('''index''', dict, set('''0123456789''' ), None), ('''columns''', dict, {'''tokens''', '''labels''', '''answers''', '''id'''}, '''tokens'''), ('''values''', list, None, None), ('''table''', dict, {'''schema''', '''data'''}, '''data'''), ] , ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" with io.BytesIO() as buffer: JsonDatasetWriter(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , lines=__SCREAMING_SNAKE_CASE , orient=__SCREAMING_SNAKE_CASE ).write() buffer.seek(0 ) UpperCamelCase : Optional[int] = load_json(__SCREAMING_SNAKE_CASE ) assert isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) if keys: if container is dict: assert exported_content.keys() == keys else: assert exported_content[0].keys() == keys else: assert not hasattr(__SCREAMING_SNAKE_CASE , '''keys''' ) and not hasattr(exported_content[0] , '''keys''' ) if len_at: assert len(exported_content[len_at] ) == 10 else: assert len(__SCREAMING_SNAKE_CASE ) == 10 @pytest.mark.parametrize('''lines, load_json_function''' , [(True, load_json_lines), (False, load_json)] ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" with io.BytesIO() as buffer: JsonDatasetWriter(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , lines=__SCREAMING_SNAKE_CASE , num_proc=2 ).write() buffer.seek(0 ) UpperCamelCase : str = load_json_function(__SCREAMING_SNAKE_CASE ) assert isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) assert isinstance(exported_content[0] , __SCREAMING_SNAKE_CASE ) assert len(__SCREAMING_SNAKE_CASE ) == 10 @pytest.mark.parametrize( '''orient, container, keys, len_at''' , [ ('''records''', list, {'''tokens''', '''labels''', '''answers''', '''id'''}, None), ('''split''', dict, {'''columns''', '''data'''}, '''data'''), ('''index''', dict, set('''0123456789''' ), None), ('''columns''', dict, {'''tokens''', '''labels''', '''answers''', '''id'''}, '''tokens'''), ('''values''', list, None, None), ('''table''', dict, {'''schema''', '''data'''}, '''data'''), ] , ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" with io.BytesIO() as buffer: JsonDatasetWriter(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , lines=__SCREAMING_SNAKE_CASE , orient=__SCREAMING_SNAKE_CASE , num_proc=2 ).write() buffer.seek(0 ) UpperCamelCase : List[str] = load_json(__SCREAMING_SNAKE_CASE ) assert isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) if keys: if container is dict: assert exported_content.keys() == keys else: assert exported_content[0].keys() == keys else: assert not hasattr(__SCREAMING_SNAKE_CASE , '''keys''' ) and not hasattr(exported_content[0] , '''keys''' ) if len_at: assert len(exported_content[len_at] ) == 10 else: assert len(__SCREAMING_SNAKE_CASE ) == 10 def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" with pytest.raises(__SCREAMING_SNAKE_CASE ): with io.BytesIO() as buffer: JsonDatasetWriter(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , num_proc=0 ) @pytest.mark.parametrize('''compression, extension''' , [('''gzip''', '''gz'''), ('''bz2''', '''bz2'''), ('''xz''', '''xz''')] ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Optional[int] = tmp_path_factory.mktemp('''data''' ) / f"""test.json.{extension}""" UpperCamelCase : Any = str(shared_datadir / f"""test_file.json.{extension}""" ) JsonDatasetWriter(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , compression=__SCREAMING_SNAKE_CASE ).write() with fsspec.open(__SCREAMING_SNAKE_CASE , '''rb''' , compression='''infer''' ) as f: UpperCamelCase : Any = f.read() with fsspec.open(__SCREAMING_SNAKE_CASE , '''rb''' , compression='''infer''' ) as f: UpperCamelCase : Dict = f.read() assert exported_content == original_content
643
from itertools import product from cva import COLOR_BGR2GRAY, cvtColor, imread, imshow, waitKey from numpy import dot, exp, mgrid, pi, ravel, square, uinta, zeros def a ( SCREAMING_SNAKE_CASE_ : List[Any] , SCREAMING_SNAKE_CASE_ : List[str] ): """simple docstring""" UpperCamelCase : List[str] = k_size // 2 UpperCamelCase , UpperCamelCase : Optional[int] = mgrid[0 - center : k_size - center, 0 - center : k_size - center] UpperCamelCase : Dict = 1 / (2 * pi * sigma) * exp(-(square(SCREAMING_SNAKE_CASE_ ) + square(SCREAMING_SNAKE_CASE_ )) / (2 * square(SCREAMING_SNAKE_CASE_ )) ) return g def a ( SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : int ): """simple docstring""" UpperCamelCase , UpperCamelCase : Tuple = image.shape[0], image.shape[1] # dst image height and width UpperCamelCase : str = height - k_size + 1 UpperCamelCase : Optional[int] = width - k_size + 1 # im2col, turn the k_size*k_size pixels into a row and np.vstack all rows UpperCamelCase : List[Any] = zeros((dst_height * dst_width, k_size * k_size) ) UpperCamelCase : Tuple = 0 for i, j in product(range(SCREAMING_SNAKE_CASE_ ) , range(SCREAMING_SNAKE_CASE_ ) ): UpperCamelCase : Dict = ravel(image[i : i + k_size, j : j + k_size] ) UpperCamelCase : Dict = window row += 1 # turn the kernel into shape(k*k, 1) UpperCamelCase : Optional[int] = gen_gaussian_kernel(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) UpperCamelCase : List[Any] = ravel(SCREAMING_SNAKE_CASE_ ) # reshape and get the dst image UpperCamelCase : Optional[int] = dot(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ).reshape(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ).astype(SCREAMING_SNAKE_CASE_ ) return dst if __name__ == "__main__": # read original image __UpperCAmelCase : Union[str, Any] = imread(r"../image_data/lena.jpg") # turn image in gray scale value __UpperCAmelCase : Optional[Any] = cvtColor(img, COLOR_BGR2GRAY) # get values with two different mask size __UpperCAmelCase : Optional[int] = gaussian_filter(gray, 3, sigma=1) __UpperCAmelCase : List[Any] = gaussian_filter(gray, 5, sigma=0.8) # show result images imshow("gaussian filter with 3x3 mask", gaussianaxa) imshow("gaussian filter with 5x5 mask", gaussianaxa) waitKey()
643
1
import itertools from dataclasses import dataclass from typing import List, Optional import pyarrow as pa import pyarrow.parquet as pq import datasets from datasets.table import table_cast __UpperCAmelCase : Any = datasets.utils.logging.get_logger(__name__) @dataclass class UpperCAmelCase_ ( datasets.BuilderConfig): '''simple docstring''' __UpperCamelCase : int = 10000 __UpperCamelCase : Optional[List[str]] = None __UpperCamelCase : Optional[datasets.Features] = None class UpperCAmelCase_ ( datasets.ArrowBasedBuilder): '''simple docstring''' __UpperCamelCase : str = ParquetConfig def _lowercase ( self ): """simple docstring""" return datasets.DatasetInfo(features=self.config.features ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if not self.config.data_files: raise ValueError(f"""At least one data file must be specified, but got data_files={self.config.data_files}""" ) UpperCamelCase : Optional[Any] = dl_manager.download_and_extract(self.config.data_files ) if isinstance(__SCREAMING_SNAKE_CASE , (str, list, tuple) ): UpperCamelCase : Tuple = data_files if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): UpperCamelCase : Optional[int] = [files] # Use `dl_manager.iter_files` to skip hidden files in an extracted archive UpperCamelCase : Union[str, Any] = [dl_manager.iter_files(__SCREAMING_SNAKE_CASE ) for file in files] return [datasets.SplitGenerator(name=datasets.Split.TRAIN , gen_kwargs={'''files''': files} )] UpperCamelCase : int = [] for split_name, files in data_files.items(): if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): UpperCamelCase : Any = [files] # Use `dl_manager.iter_files` to skip hidden files in an extracted archive UpperCamelCase : List[str] = [dl_manager.iter_files(__SCREAMING_SNAKE_CASE ) for file in files] # Infer features is they are stoed in the arrow schema if self.info.features is None: for file in itertools.chain.from_iterable(__SCREAMING_SNAKE_CASE ): with open(__SCREAMING_SNAKE_CASE , '''rb''' ) as f: UpperCamelCase : Optional[Any] = datasets.Features.from_arrow_schema(pq.read_schema(__SCREAMING_SNAKE_CASE ) ) break splits.append(datasets.SplitGenerator(name=__SCREAMING_SNAKE_CASE , gen_kwargs={'''files''': files} ) ) return splits def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if self.info.features is not None: # more expensive cast to support nested features with keys in a different order # allows str <-> int/float or str to Audio for example UpperCamelCase : List[Any] = table_cast(__SCREAMING_SNAKE_CASE , self.info.features.arrow_schema ) return pa_table def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : List[Any] = self.info.features.arrow_schema if self.info.features is not None else None if self.info.features is not None and self.config.columns is not None: if sorted(field.name for field in schema ) != sorted(self.config.columns ): raise ValueError( f"""Tried to load parquet data with columns '{self.config.columns}' with mismatching features '{self.info.features}'""" ) for file_idx, file in enumerate(itertools.chain.from_iterable(__SCREAMING_SNAKE_CASE ) ): with open(__SCREAMING_SNAKE_CASE , '''rb''' ) as f: UpperCamelCase : List[Any] = pq.ParquetFile(__SCREAMING_SNAKE_CASE ) try: for batch_idx, record_batch in enumerate( parquet_file.iter_batches(batch_size=self.config.batch_size , columns=self.config.columns ) ): UpperCamelCase : int = pa.Table.from_batches([record_batch] ) # Uncomment for debugging (will print the Arrow table size and elements) # logger.warning(f"pa_table: {pa_table} num rows: {pa_table.num_rows}") # logger.warning('\n'.join(str(pa_table.slice(i, 1).to_pydict()) for i in range(pa_table.num_rows))) yield f"""{file_idx}_{batch_idx}""", self._cast_table(__SCREAMING_SNAKE_CASE ) except ValueError as e: logger.error(f"""Failed to read file '{file}' with error {type(__SCREAMING_SNAKE_CASE )}: {e}""" ) raise
643
from .imports import is_tqdm_available if is_tqdm_available(): from tqdm.auto import tqdm as _tqdm from ..state import PartialState def a ( SCREAMING_SNAKE_CASE_ : bool = True , *SCREAMING_SNAKE_CASE_ : List[str] , **SCREAMING_SNAKE_CASE_ : Tuple ): """simple docstring""" if not is_tqdm_available(): raise ImportError('''Accelerate\'s `tqdm` module requires `tqdm` to be installed. Please run `pip install tqdm`.''' ) UpperCamelCase : int = False if main_process_only: UpperCamelCase : int = PartialState().local_process_index == 0 return _tqdm(*SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , disable=SCREAMING_SNAKE_CASE_ )
643
1
from packaging import version from .import_utils import is_accelerate_available if is_accelerate_available(): import accelerate def a ( SCREAMING_SNAKE_CASE_ : int ): """simple docstring""" if not is_accelerate_available(): return method UpperCamelCase : Dict = version.parse(accelerate.__version__ ).base_version if version.parse(SCREAMING_SNAKE_CASE_ ) < version.parse('''0.17.0''' ): return method def wrapper(self : str , *SCREAMING_SNAKE_CASE_ : List[Any] , **SCREAMING_SNAKE_CASE_ : List[Any] ): if hasattr(self , '''_hf_hook''' ) and hasattr(self._hf_hook , '''pre_forward''' ): self._hf_hook.pre_forward(self ) return method(self , *SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) return wrapper
643
import io import os import unicodedata from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging __UpperCAmelCase : Any = logging.get_logger(__name__) __UpperCAmelCase : int = "▁" __UpperCAmelCase : Tuple = {"vocab_file": "vocab.txt", "sentencepiece_model_ckpt": "sentencepiece.bpe.model"} __UpperCAmelCase : Dict = { "sentencepiece_model_file": "sentencepiece.bpe.model", "vocab_file": "vocab.txt", } __UpperCAmelCase : Dict = { "vocab_file": { "ernie-m-base": "https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/vocab.txt", "ernie-m-large": "https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/vocab.txt", }, "sentencepiece_model_file": { "ernie-m-base": "https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/sentencepiece.bpe.model", "ernie-m-large": "https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/sentencepiece.bpe.model", }, } __UpperCAmelCase : str = { "ernie-m-base": 514, "ernie-m-large": 514, } __UpperCAmelCase : Optional[int] = { "ernie-m-base": {"do_lower_case": False}, "ernie-m-large": {"do_lower_case": False}, } class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : List[str] = ["input_ids"] __UpperCamelCase : List[str] = VOCAB_FILES_NAMES __UpperCamelCase : List[Any] = PRETRAINED_INIT_CONFIGURATION __UpperCamelCase : List[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCamelCase : int = PRETRAINED_VOCAB_FILES_MAP __UpperCamelCase : List[str] = RESOURCE_FILES_NAMES def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE="utf8" , __SCREAMING_SNAKE_CASE="[UNK]" , __SCREAMING_SNAKE_CASE="[SEP]" , __SCREAMING_SNAKE_CASE="[PAD]" , __SCREAMING_SNAKE_CASE="[CLS]" , __SCREAMING_SNAKE_CASE="[MASK]" , __SCREAMING_SNAKE_CASE = None , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" UpperCamelCase : int = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( do_lower_case=__SCREAMING_SNAKE_CASE , unk_token=__SCREAMING_SNAKE_CASE , sep_token=__SCREAMING_SNAKE_CASE , pad_token=__SCREAMING_SNAKE_CASE , cls_token=__SCREAMING_SNAKE_CASE , mask_token=__SCREAMING_SNAKE_CASE , vocab_file=__SCREAMING_SNAKE_CASE , encoding=__SCREAMING_SNAKE_CASE , sp_model_kwargs=self.sp_model_kwargs , **__SCREAMING_SNAKE_CASE , ) UpperCamelCase : List[str] = do_lower_case UpperCamelCase : Dict = sentencepiece_model_ckpt UpperCamelCase : str = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(__SCREAMING_SNAKE_CASE ) # to mimic paddlenlp.transformers.ernie_m.tokenizer.ErnieMTokenizer functioning if vocab_file is not None: UpperCamelCase : Optional[Any] = self.load_vocab(filepath=__SCREAMING_SNAKE_CASE ) else: UpperCamelCase : int = {self.sp_model.id_to_piece(__SCREAMING_SNAKE_CASE ): id for id in range(self.sp_model.get_piece_size() )} UpperCamelCase : str = {v: k for k, v in self.vocab.items()} def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if text is None: return None UpperCamelCase : str = self.tokenize(__SCREAMING_SNAKE_CASE ) UpperCamelCase , UpperCamelCase : str = '''''', [] for i, ch in enumerate(__SCREAMING_SNAKE_CASE ): if ch in self.SP_CHAR_MAPPING: UpperCamelCase : Optional[int] = self.SP_CHAR_MAPPING.get(__SCREAMING_SNAKE_CASE ) else: UpperCamelCase : Optional[Any] = unicodedata.normalize('''NFKC''' , __SCREAMING_SNAKE_CASE ) if self.is_whitespace(__SCREAMING_SNAKE_CASE ): continue normalized_text += ch char_mapping.extend([i] * len(__SCREAMING_SNAKE_CASE ) ) UpperCamelCase , UpperCamelCase , UpperCamelCase : Tuple = normalized_text, [], 0 if self.do_lower_case: UpperCamelCase : Tuple = text.lower() for token in split_tokens: if token[:1] == "▁": UpperCamelCase : Any = token[1:] UpperCamelCase : Optional[int] = text[offset:].index(__SCREAMING_SNAKE_CASE ) + offset UpperCamelCase : List[Any] = start + len(__SCREAMING_SNAKE_CASE ) token_mapping.append((char_mapping[start], char_mapping[end - 1] + 1) ) UpperCamelCase : str = end return token_mapping @property def _lowercase ( self ): """simple docstring""" return len(self.vocab ) def _lowercase ( self ): """simple docstring""" return dict(self.vocab , **self.added_tokens_encoder ) def __getstate__( self ): """simple docstring""" UpperCamelCase : Optional[Any] = self.__dict__.copy() UpperCamelCase : str = None return state def __setstate__( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Tuple = d # for backward compatibility if not hasattr(self , '''sp_model_kwargs''' ): UpperCamelCase : Optional[int] = {} UpperCamelCase : Optional[Any] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.sentencepiece_model_ckpt ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" return "".join((self.SP_CHAR_MAPPING.get(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) for c in text) ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=64 , __SCREAMING_SNAKE_CASE=0.1 ): """simple docstring""" if self.sp_model_kwargs.get('''enable_sampling''' ) is True: UpperCamelCase : List[str] = True if self.sp_model_kwargs.get('''alpha''' ) is not None: UpperCamelCase : Any = self.sp_model_kwargs.get('''alpha''' ) if self.sp_model_kwargs.get('''nbest_size''' ) is not None: UpperCamelCase : Tuple = self.sp_model_kwargs.get('''nbest_size''' ) if not enable_sampling: UpperCamelCase : int = self.sp_model.EncodeAsPieces(__SCREAMING_SNAKE_CASE ) else: UpperCamelCase : Optional[Any] = self.sp_model.SampleEncodeAsPieces(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = [] for pi, piece in enumerate(__SCREAMING_SNAKE_CASE ): if piece == SPIECE_UNDERLINE: if not pieces[pi + 1].startswith(__SCREAMING_SNAKE_CASE ) and pi != 0: new_pieces.append(__SCREAMING_SNAKE_CASE ) continue else: continue UpperCamelCase : Any = 0 for i, chunk in enumerate(__SCREAMING_SNAKE_CASE ): if chunk == SPIECE_UNDERLINE: continue if self.is_ch_char(__SCREAMING_SNAKE_CASE ) or self.is_punct(__SCREAMING_SNAKE_CASE ): if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE: new_pieces.append(piece[lst_i:i] ) new_pieces.append(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Dict = i + 1 elif chunk.isdigit() and i > 0 and not piece[i - 1].isdigit(): if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE: new_pieces.append(piece[lst_i:i] ) UpperCamelCase : Union[str, Any] = i elif not chunk.isdigit() and i > 0 and piece[i - 1].isdigit(): if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE: new_pieces.append(piece[lst_i:i] ) UpperCamelCase : Any = i if len(__SCREAMING_SNAKE_CASE ) > lst_i: new_pieces.append(piece[lst_i:] ) return new_pieces def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Optional[int] = ''''''.join(__SCREAMING_SNAKE_CASE ).replace(__SCREAMING_SNAKE_CASE , ''' ''' ).strip() return out_string def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : int = self.convert_ids_to_tokens(__SCREAMING_SNAKE_CASE ) UpperCamelCase : int = ''''''.join(__SCREAMING_SNAKE_CASE ).replace(__SCREAMING_SNAKE_CASE , ''' ''' ).strip() return out_string def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" return self.vocab.get(__SCREAMING_SNAKE_CASE , self.vocab.get(self.unk_token ) ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" return self.reverse_vocab.get(__SCREAMING_SNAKE_CASE , self.unk_token ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None ): """simple docstring""" if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] UpperCamelCase : Any = [self.cls_token_id] UpperCamelCase : str = [self.sep_token_id] return _cls + token_ids_a + _sep + _sep + token_ids_a + _sep def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None ): """simple docstring""" if offset_mapping_a is None: return [(0, 0)] + offset_mapping_a + [(0, 0)] return [(0, 0)] + offset_mapping_a + [(0, 0), (0, 0)] + offset_mapping_a + [(0, 0)] def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=False ): """simple docstring""" if already_has_special_tokens: if token_ids_a is not None: raise ValueError( '''You should not supply a second sequence if the provided sequence of ''' '''ids is already formatted with special tokens for the model.''' ) return [1 if x in [self.sep_token_id, self.cls_token_id] else 0 for x in token_ids_a] if token_ids_a is not None: return [1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1, 1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1] return [1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1] def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ): """simple docstring""" if token_ids_a is None: # [CLS] X [SEP] return (len(__SCREAMING_SNAKE_CASE ) + 2) * [0] # [CLS] A [SEP] [SEP] B [SEP] return [0] * (len(__SCREAMING_SNAKE_CASE ) + 1) + [1] * (len(__SCREAMING_SNAKE_CASE ) + 3) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if "\u4e00" <= char <= "\u9fff": return True return False def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if ("a" <= char <= "z") or ("A" <= char <= "Z"): return True return False def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if char in ",;:.?!~,;:。?!《》【】": return True return False def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if char == " " or char == "\t" or char == "\n" or char == "\r": return True if len(__SCREAMING_SNAKE_CASE ) == 1: UpperCamelCase : Optional[int] = unicodedata.category(__SCREAMING_SNAKE_CASE ) if cat == "Zs": return True return False def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : int = {} with io.open(__SCREAMING_SNAKE_CASE , '''r''' , encoding='''utf-8''' ) as f: for index, line in enumerate(__SCREAMING_SNAKE_CASE ): UpperCamelCase : Tuple = line.rstrip('''\n''' ) UpperCamelCase : List[Any] = int(__SCREAMING_SNAKE_CASE ) return token_to_idx def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ): """simple docstring""" UpperCamelCase : Union[str, Any] = 0 if os.path.isdir(__SCREAMING_SNAKE_CASE ): UpperCamelCase : Dict = os.path.join( __SCREAMING_SNAKE_CASE , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) else: UpperCamelCase : Union[str, Any] = (filename_prefix + '''-''' if filename_prefix else '''''') + save_directory with open(__SCREAMING_SNAKE_CASE , '''w''' , encoding='''utf-8''' ) as writer: for token, token_index in sorted(self.vocab.items() , key=lambda __SCREAMING_SNAKE_CASE : kv[1] ): if index != token_index: logger.warning( f"""Saving vocabulary to {vocab_file}: vocabulary indices are not consecutive.""" ''' Please check that the vocabulary is not corrupted!''' ) UpperCamelCase : List[Any] = token_index writer.write(token + '''\n''' ) index += 1 UpperCamelCase : Tuple = os.path.join(__SCREAMING_SNAKE_CASE , '''sentencepiece.bpe.model''' ) with open(__SCREAMING_SNAKE_CASE , '''wb''' ) as fi: UpperCamelCase : List[Any] = self.sp_model.serialized_model_proto() fi.write(__SCREAMING_SNAKE_CASE ) return (vocab_file,)
643
1
import numpy as np import skfuzzy as fuzz if __name__ == "__main__": # Create universe of discourse in Python using linspace () __UpperCAmelCase : Tuple = np.linspace(start=0, stop=75, num=75, endpoint=True, retstep=False) # Create two fuzzy sets by defining any membership function # (trapmf(), gbellmf(), gaussmf(), etc). __UpperCAmelCase : Dict = [0, 25, 50] __UpperCAmelCase : Union[str, Any] = [25, 50, 75] __UpperCAmelCase : Optional[int] = fuzz.membership.trimf(X, abca) __UpperCAmelCase : str = fuzz.membership.trimf(X, abca) # Compute the different operations using inbuilt functions. __UpperCAmelCase : int = np.ones(75) __UpperCAmelCase : Union[str, Any] = np.zeros((75,)) # 1. Union = max(µA(x), µB(x)) __UpperCAmelCase : Optional[Any] = fuzz.fuzzy_or(X, young, X, middle_aged)[1] # 2. Intersection = min(µA(x), µB(x)) __UpperCAmelCase : Dict = fuzz.fuzzy_and(X, young, X, middle_aged)[1] # 3. Complement (A) = (1- min(µA(x)) __UpperCAmelCase : Any = fuzz.fuzzy_not(young) # 4. Difference (A/B) = min(µA(x),(1- µB(x))) __UpperCAmelCase : Optional[Any] = fuzz.fuzzy_and(X, young, X, fuzz.fuzzy_not(middle_aged)[1])[1] # 5. Algebraic Sum = [µA(x) + µB(x) – (µA(x) * µB(x))] __UpperCAmelCase : Any = young + middle_aged - (young * middle_aged) # 6. Algebraic Product = (µA(x) * µB(x)) __UpperCAmelCase : int = young * middle_aged # 7. Bounded Sum = min[1,(µA(x), µB(x))] __UpperCAmelCase : Dict = fuzz.fuzzy_and(X, one, X, young + middle_aged)[1] # 8. Bounded difference = min[0,(µA(x), µB(x))] __UpperCAmelCase : Union[str, Any] = fuzz.fuzzy_or(X, zero, X, young - middle_aged)[1] # max-min composition # max-product composition # Plot each set A, set B and each operation result using plot() and subplot(). from matplotlib import pyplot as plt plt.figure() plt.subplot(4, 3, 1) plt.plot(X, young) plt.title("Young") plt.grid(True) plt.subplot(4, 3, 2) plt.plot(X, middle_aged) plt.title("Middle aged") plt.grid(True) plt.subplot(4, 3, 3) plt.plot(X, union) plt.title("union") plt.grid(True) plt.subplot(4, 3, 4) plt.plot(X, intersection) plt.title("intersection") plt.grid(True) plt.subplot(4, 3, 5) plt.plot(X, complement_a) plt.title("complement_a") plt.grid(True) plt.subplot(4, 3, 6) plt.plot(X, difference) plt.title("difference a/b") plt.grid(True) plt.subplot(4, 3, 7) plt.plot(X, alg_sum) plt.title("alg_sum") plt.grid(True) plt.subplot(4, 3, 8) plt.plot(X, alg_product) plt.title("alg_product") plt.grid(True) plt.subplot(4, 3, 9) plt.plot(X, bdd_sum) plt.title("bdd_sum") plt.grid(True) plt.subplot(4, 3, 10) plt.plot(X, bdd_difference) plt.title("bdd_difference") plt.grid(True) plt.subplots_adjust(hspace=0.5) plt.show()
643
from typing import List, Optional, Tuple, Union import torch from torch import nn from torch.nn import CrossEntropyLoss from ... import AutoBackbone from ...modeling_outputs import SemanticSegmenterOutput from ...modeling_utils import PreTrainedModel from ...utils import add_start_docstrings, add_start_docstrings_to_model_forward, replace_return_docstrings from ...utils.backbone_utils import BackboneMixin from .configuration_upernet import UperNetConfig __UpperCAmelCase : List[Any] = [ "openmmlab/upernet-convnext-tiny", # See all UperNet models at https://huggingface.co/models?filter=upernet ] # General docstring __UpperCAmelCase : List[str] = "UperNetConfig" class UpperCAmelCase_ ( nn.Module): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = 0 , __SCREAMING_SNAKE_CASE = False , __SCREAMING_SNAKE_CASE = 1 , ): """simple docstring""" super().__init__() UpperCamelCase : str = nn.Convad( in_channels=__SCREAMING_SNAKE_CASE , out_channels=__SCREAMING_SNAKE_CASE , kernel_size=__SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE , bias=__SCREAMING_SNAKE_CASE , dilation=__SCREAMING_SNAKE_CASE , ) UpperCamelCase : int = nn.BatchNormad(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[Any] = nn.ReLU() def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Any = self.conv(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[Any] = self.batch_norm(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = self.activation(__SCREAMING_SNAKE_CASE ) return output class UpperCAmelCase_ ( nn.Module): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" super().__init__() UpperCamelCase : List[Any] = [ nn.AdaptiveAvgPoolad(__SCREAMING_SNAKE_CASE ), UperNetConvModule(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , kernel_size=1 ), ] for i, layer in enumerate(self.layers ): self.add_module(str(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : str = input for layer in self.layers: UpperCamelCase : int = layer(__SCREAMING_SNAKE_CASE ) return hidden_state class UpperCAmelCase_ ( nn.Module): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" super().__init__() UpperCamelCase : List[Any] = pool_scales UpperCamelCase : Dict = align_corners UpperCamelCase : Optional[int] = in_channels UpperCamelCase : Union[str, Any] = channels UpperCamelCase : List[str] = [] for i, pool_scale in enumerate(__SCREAMING_SNAKE_CASE ): UpperCamelCase : Union[str, Any] = UperNetPyramidPoolingBlock(pool_scale=__SCREAMING_SNAKE_CASE , in_channels=__SCREAMING_SNAKE_CASE , channels=__SCREAMING_SNAKE_CASE ) self.blocks.append(__SCREAMING_SNAKE_CASE ) self.add_module(str(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : str = [] for ppm in self.blocks: UpperCamelCase : List[str] = ppm(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Tuple = nn.functional.interpolate( __SCREAMING_SNAKE_CASE , size=x.size()[2:] , mode='''bilinear''' , align_corners=self.align_corners ) ppm_outs.append(__SCREAMING_SNAKE_CASE ) return ppm_outs class UpperCAmelCase_ ( nn.Module): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" super().__init__() UpperCamelCase : int = config UpperCamelCase : List[str] = config.pool_scales # e.g. (1, 2, 3, 6) UpperCamelCase : Optional[int] = in_channels UpperCamelCase : str = config.hidden_size UpperCamelCase : str = False UpperCamelCase : List[str] = nn.Convad(self.channels , config.num_labels , kernel_size=1 ) # PSP Module UpperCamelCase : Optional[int] = UperNetPyramidPoolingModule( self.pool_scales , self.in_channels[-1] , self.channels , align_corners=self.align_corners , ) UpperCamelCase : str = UperNetConvModule( self.in_channels[-1] + len(self.pool_scales ) * self.channels , self.channels , kernel_size=3 , padding=1 , ) # FPN Module UpperCamelCase : Union[str, Any] = nn.ModuleList() UpperCamelCase : Union[str, Any] = nn.ModuleList() for in_channels in self.in_channels[:-1]: # skip the top layer UpperCamelCase : List[Any] = UperNetConvModule(__SCREAMING_SNAKE_CASE , self.channels , kernel_size=1 ) UpperCamelCase : int = UperNetConvModule(self.channels , self.channels , kernel_size=3 , padding=1 ) self.lateral_convs.append(__SCREAMING_SNAKE_CASE ) self.fpn_convs.append(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[Any] = UperNetConvModule( len(self.in_channels ) * self.channels , self.channels , kernel_size=3 , padding=1 , ) def _lowercase ( self ): """simple docstring""" self.apply(self._init_weights ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if isinstance(__SCREAMING_SNAKE_CASE , nn.Convad ): module.weight.data.normal_(mean=0.0 , std=self.config.initializer_range ) if module.bias is not None: module.bias.data.zero_() def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Any = inputs[-1] UpperCamelCase : int = [x] psp_outs.extend(self.psp_modules(__SCREAMING_SNAKE_CASE ) ) UpperCamelCase : Any = torch.cat(__SCREAMING_SNAKE_CASE , dim=1 ) UpperCamelCase : Union[str, Any] = self.bottleneck(__SCREAMING_SNAKE_CASE ) return output def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : str = [lateral_conv(encoder_hidden_states[i] ) for i, lateral_conv in enumerate(self.lateral_convs )] laterals.append(self.psp_forward(__SCREAMING_SNAKE_CASE ) ) # build top-down path UpperCamelCase : int = len(__SCREAMING_SNAKE_CASE ) for i in range(used_backbone_levels - 1 , 0 , -1 ): UpperCamelCase : Optional[int] = laterals[i - 1].shape[2:] UpperCamelCase : Optional[Any] = laterals[i - 1] + nn.functional.interpolate( laterals[i] , size=__SCREAMING_SNAKE_CASE , mode='''bilinear''' , align_corners=self.align_corners ) # build outputs UpperCamelCase : str = [self.fpn_convs[i](laterals[i] ) for i in range(used_backbone_levels - 1 )] # append psp feature fpn_outs.append(laterals[-1] ) for i in range(used_backbone_levels - 1 , 0 , -1 ): UpperCamelCase : int = nn.functional.interpolate( fpn_outs[i] , size=fpn_outs[0].shape[2:] , mode='''bilinear''' , align_corners=self.align_corners ) UpperCamelCase : str = torch.cat(__SCREAMING_SNAKE_CASE , dim=1 ) UpperCamelCase : Tuple = self.fpn_bottleneck(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = self.classifier(__SCREAMING_SNAKE_CASE ) return output class UpperCAmelCase_ ( nn.Module): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = 2 , __SCREAMING_SNAKE_CASE = 3 , __SCREAMING_SNAKE_CASE = 1 ): """simple docstring""" super().__init__() UpperCamelCase : Dict = config UpperCamelCase : Optional[Any] = config.auxiliary_in_channels UpperCamelCase : Union[str, Any] = config.auxiliary_channels UpperCamelCase : Union[str, Any] = config.auxiliary_num_convs UpperCamelCase : Optional[Any] = config.auxiliary_concat_input UpperCamelCase : List[str] = in_index UpperCamelCase : Any = (kernel_size // 2) * dilation UpperCamelCase : Optional[Any] = [] convs.append( UperNetConvModule( self.in_channels , self.channels , kernel_size=__SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE , dilation=__SCREAMING_SNAKE_CASE ) ) for i in range(self.num_convs - 1 ): convs.append( UperNetConvModule( self.channels , self.channels , kernel_size=__SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE , dilation=__SCREAMING_SNAKE_CASE ) ) if self.num_convs == 0: UpperCamelCase : str = nn.Identity() else: UpperCamelCase : Dict = nn.Sequential(*__SCREAMING_SNAKE_CASE ) if self.concat_input: UpperCamelCase : Union[str, Any] = UperNetConvModule( self.in_channels + self.channels , self.channels , kernel_size=__SCREAMING_SNAKE_CASE , padding=kernel_size // 2 ) UpperCamelCase : Optional[Any] = nn.Convad(self.channels , config.num_labels , kernel_size=1 ) def _lowercase ( self ): """simple docstring""" self.apply(self._init_weights ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if isinstance(__SCREAMING_SNAKE_CASE , nn.Convad ): module.weight.data.normal_(mean=0.0 , std=self.config.initializer_range ) if module.bias is not None: module.bias.data.zero_() def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Any = encoder_hidden_states[self.in_index] UpperCamelCase : str = self.convs(__SCREAMING_SNAKE_CASE ) if self.concat_input: UpperCamelCase : int = self.conv_cat(torch.cat([hidden_states, output] , dim=1 ) ) UpperCamelCase : Union[str, Any] = self.classifier(__SCREAMING_SNAKE_CASE ) return output class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : Optional[Any] = UperNetConfig __UpperCamelCase : Optional[int] = "pixel_values" __UpperCamelCase : Dict = True def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): module.backbone.init_weights() module.decode_head.init_weights() module.auxiliary_head.init_weights() def _lowercase ( self ): """simple docstring""" self.backbone.init_weights() self.decode_head.init_weights() self.auxiliary_head.init_weights() def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=False ): """simple docstring""" if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): UpperCamelCase : str = value __UpperCAmelCase : List[Any] = r"\n Parameters:\n This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) sub-class. Use\n it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and\n behavior.\n config ([`UperNetConfig`]): 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" __UpperCAmelCase : Union[str, Any] = r"\n Args:\n pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n Pixel values. Padding will be ignored by default should you provide it. Pixel values can be obtained using\n [`AutoImageProcessor`]. See [`SegformerImageProcessor.__call__`] for details.\n output_attentions (`bool`, *optional*):\n Whether or not to return the attentions tensors of all attention layers in case the backbone has them. See\n `attentions` under returned tensors for more detail.\n output_hidden_states (`bool`, *optional*):\n Whether or not to return the hidden states of all layers of the backbone. See `hidden_states` under\n returned tensors for 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( "UperNet framework leveraging any vision backbone e.g. for ADE20k, CityScapes.", _a, ) class UpperCAmelCase_ ( _a): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" super().__init__(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Dict = AutoBackbone.from_config(config.backbone_config ) # Semantic segmentation head(s) UpperCamelCase : int = UperNetHead(__SCREAMING_SNAKE_CASE , in_channels=self.backbone.channels ) UpperCamelCase : int = UperNetFCNHead(__SCREAMING_SNAKE_CASE ) if config.use_auxiliary_head else None # Initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(UPERNET_INPUTS_DOCSTRING.format('''batch_size, sequence_length''' ) ) @replace_return_docstrings(output_type=__SCREAMING_SNAKE_CASE , config_class=_CONFIG_FOR_DOC ) def _lowercase ( self , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , ): """simple docstring""" UpperCamelCase : List[str] = return_dict if return_dict is not None else self.config.use_return_dict UpperCamelCase : str = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) UpperCamelCase : Tuple = output_attentions if output_attentions is not None else self.config.output_attentions UpperCamelCase : Tuple = self.backbone.forward_with_filtered_kwargs( __SCREAMING_SNAKE_CASE , output_hidden_states=__SCREAMING_SNAKE_CASE , output_attentions=__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = outputs.feature_maps UpperCamelCase : Union[str, Any] = self.decode_head(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = nn.functional.interpolate(__SCREAMING_SNAKE_CASE , size=pixel_values.shape[2:] , mode='''bilinear''' , align_corners=__SCREAMING_SNAKE_CASE ) UpperCamelCase : int = None if self.auxiliary_head is not None: UpperCamelCase : int = self.auxiliary_head(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Tuple = nn.functional.interpolate( __SCREAMING_SNAKE_CASE , size=pixel_values.shape[2:] , mode='''bilinear''' , align_corners=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = None if labels is not None: if self.config.num_labels == 1: raise ValueError('''The number of labels should be greater than one''' ) else: # compute weighted loss UpperCamelCase : Optional[int] = CrossEntropyLoss(ignore_index=self.config.loss_ignore_index ) UpperCamelCase : Tuple = loss_fct(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[Any] = loss_fct(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = main_loss + self.config.auxiliary_loss_weight * auxiliary_loss if not return_dict: if output_hidden_states: UpperCamelCase : Optional[Any] = (logits,) + outputs[1:] else: UpperCamelCase : int = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return SemanticSegmenterOutput( loss=__SCREAMING_SNAKE_CASE , logits=__SCREAMING_SNAKE_CASE , hidden_states=outputs.hidden_states , attentions=outputs.attentions , )
643
1
# tests directory-specific settings - this file is run automatically # by pytest before any tests are run import sys import warnings from os.path import abspath, dirname, join # allow having multiple repository checkouts and not needing to remember to rerun # 'pip install -e .[dev]' when switching between checkouts and running tests. __UpperCAmelCase : Union[str, Any] = abspath(join(dirname(dirname(__file__)), "src")) sys.path.insert(1, git_repo_path) # silence FutureWarning warnings in tests since often we can't act on them until # they become normal warnings - i.e. the tests still need to test the current functionality warnings.simplefilter(action="ignore", category=FutureWarning) def a ( SCREAMING_SNAKE_CASE_ : Tuple ): """simple docstring""" from diffusers.utils.testing_utils import pytest_addoption_shared pytest_addoption_shared(SCREAMING_SNAKE_CASE_ ) def a ( SCREAMING_SNAKE_CASE_ : Union[str, Any] ): """simple docstring""" from diffusers.utils.testing_utils import pytest_terminal_summary_main UpperCamelCase : Union[str, Any] = terminalreporter.config.getoption('''--make-reports''' ) if make_reports: pytest_terminal_summary_main(SCREAMING_SNAKE_CASE_ , id=SCREAMING_SNAKE_CASE_ )
643
import json import os import tempfile import transformers import datasets from utils import generate_example_dataset, get_duration __UpperCAmelCase : Optional[int] = 500000 __UpperCAmelCase , __UpperCAmelCase : Any = os.path.split(__file__) __UpperCAmelCase : int = os.path.join(RESULTS_BASEPATH, "results", RESULTS_FILENAME.replace(".py", ".json")) @get_duration def a ( SCREAMING_SNAKE_CASE_ : datasets.Dataset , **SCREAMING_SNAKE_CASE_ : Union[str, Any] ): """simple docstring""" UpperCamelCase : Tuple = dataset.map(**SCREAMING_SNAKE_CASE_ ) @get_duration def a ( SCREAMING_SNAKE_CASE_ : datasets.Dataset , **SCREAMING_SNAKE_CASE_ : Any ): """simple docstring""" UpperCamelCase : int = dataset.filter(**SCREAMING_SNAKE_CASE_ ) def a ( ): """simple docstring""" UpperCamelCase : Optional[int] = {'''num examples''': SPEED_TEST_N_EXAMPLES} with tempfile.TemporaryDirectory() as tmp_dir: UpperCamelCase : Dict = datasets.Features({'''text''': datasets.Value('''string''' ), '''numbers''': datasets.Value('''float32''' )} ) UpperCamelCase : List[str] = generate_example_dataset( os.path.join(SCREAMING_SNAKE_CASE_ , '''dataset.arrow''' ) , SCREAMING_SNAKE_CASE_ , num_examples=SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Union[str, Any] = transformers.AutoTokenizer.from_pretrained('''bert-base-cased''' , use_fast=SCREAMING_SNAKE_CASE_ ) def tokenize(SCREAMING_SNAKE_CASE_ : Dict ): return tokenizer(examples['''text'''] ) UpperCamelCase : List[Any] = map(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : List[str] = map(SCREAMING_SNAKE_CASE_ , batched=SCREAMING_SNAKE_CASE_ ) UpperCamelCase : List[Any] = map(SCREAMING_SNAKE_CASE_ , function=lambda SCREAMING_SNAKE_CASE_ : None , batched=SCREAMING_SNAKE_CASE_ ) with dataset.formatted_as(type='''numpy''' ): UpperCamelCase : Tuple = map(SCREAMING_SNAKE_CASE_ , function=lambda SCREAMING_SNAKE_CASE_ : None , batched=SCREAMING_SNAKE_CASE_ ) with dataset.formatted_as(type='''pandas''' ): UpperCamelCase : int = map(SCREAMING_SNAKE_CASE_ , function=lambda SCREAMING_SNAKE_CASE_ : None , batched=SCREAMING_SNAKE_CASE_ ) with dataset.formatted_as(type='''torch''' , columns='''numbers''' ): UpperCamelCase : Dict = map(SCREAMING_SNAKE_CASE_ , function=lambda SCREAMING_SNAKE_CASE_ : None , batched=SCREAMING_SNAKE_CASE_ ) with dataset.formatted_as(type='''tensorflow''' , columns='''numbers''' ): UpperCamelCase : Tuple = map(SCREAMING_SNAKE_CASE_ , function=lambda SCREAMING_SNAKE_CASE_ : None , batched=SCREAMING_SNAKE_CASE_ ) UpperCamelCase : List[str] = map(SCREAMING_SNAKE_CASE_ , function=SCREAMING_SNAKE_CASE_ , batched=SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Dict = filter(SCREAMING_SNAKE_CASE_ ) # Activate later when tokenizer support batched inputs # with dataset.formatted_as(type='numpy'): # times[func.__name__ + " fast-tokenizer batched numpy"] = func(dataset, function=tokenize, batched=True) with open(SCREAMING_SNAKE_CASE_ , '''wb''' ) as f: f.write(json.dumps(SCREAMING_SNAKE_CASE_ ).encode('''utf-8''' ) ) if __name__ == "__main__": # useful to run the profiler benchmark_map_filter()
643
1
import random def a ( SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : float , SCREAMING_SNAKE_CASE_ : bool = False ): """simple docstring""" UpperCamelCase : dict = {i: [] for i in range(SCREAMING_SNAKE_CASE_ )} # if probability is greater or equal than 1, then generate a complete graph if probability >= 1: return complete_graph(SCREAMING_SNAKE_CASE_ ) # if probability is lower or equal than 0, then return a graph without edges if probability <= 0: return graph # for each couple of nodes, add an edge from u to v # if the number randomly generated is greater than probability probability for i in range(SCREAMING_SNAKE_CASE_ ): for j in range(i + 1 , SCREAMING_SNAKE_CASE_ ): if random.random() < probability: graph[i].append(SCREAMING_SNAKE_CASE_ ) if not directed: # if the graph is undirected, add an edge in from j to i, either graph[j].append(SCREAMING_SNAKE_CASE_ ) return graph def a ( SCREAMING_SNAKE_CASE_ : int ): """simple docstring""" return { i: [j for j in range(SCREAMING_SNAKE_CASE_ ) if i != j] for i in range(SCREAMING_SNAKE_CASE_ ) } if __name__ == "__main__": import doctest doctest.testmod()
643
from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import ShapEPipeline else: from .camera import create_pan_cameras from .pipeline_shap_e import ShapEPipeline from .pipeline_shap_e_img2img import ShapEImgaImgPipeline from .renderer import ( BoundingBoxVolume, ImportanceRaySampler, MLPNeRFModelOutput, MLPNeRSTFModel, ShapEParamsProjModel, ShapERenderer, StratifiedRaySampler, VoidNeRFModel, )
643
1
import warnings from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : Tuple = ["image_processor", "tokenizer"] __UpperCamelCase : Optional[Any] = "CLIPImageProcessor" __UpperCamelCase : Tuple = ("CLIPTokenizer", "CLIPTokenizerFast") def __init__( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : List[Any] = None if "feature_extractor" in kwargs: warnings.warn( '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`''' ''' instead.''' , __SCREAMING_SNAKE_CASE , ) UpperCamelCase : int = kwargs.pop('''feature_extractor''' ) UpperCamelCase : List[Any] = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError('''You need to specify an `image_processor`.''' ) if tokenizer is None: raise ValueError('''You need to specify a `tokenizer`.''' ) super().__init__(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def __call__( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE ): """simple docstring""" if text is None and images is None: raise ValueError('''You have to specify either text or images. Both cannot be none.''' ) if text is not None: UpperCamelCase : int = self.tokenizer(__SCREAMING_SNAKE_CASE , return_tensors=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) if images is not None: UpperCamelCase : List[Any] = self.image_processor(__SCREAMING_SNAKE_CASE , return_tensors=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) if text is not None and images is not None: UpperCamelCase : int = image_features.pixel_values return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**__SCREAMING_SNAKE_CASE ) , tensor_type=__SCREAMING_SNAKE_CASE ) def _lowercase ( self , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): """simple docstring""" return self.tokenizer.batch_decode(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def _lowercase ( self , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): """simple docstring""" return self.tokenizer.decode(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) @property def _lowercase ( self ): """simple docstring""" UpperCamelCase : Optional[Any] = self.tokenizer.model_input_names UpperCamelCase : Tuple = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) ) @property def _lowercase ( self ): """simple docstring""" warnings.warn( '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.''' , __SCREAMING_SNAKE_CASE , ) return self.image_processor_class @property def _lowercase ( self ): """simple docstring""" warnings.warn( '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.''' , __SCREAMING_SNAKE_CASE , ) return self.image_processor
643
import torch from transformers import AutoModel class UpperCAmelCase_ ( torch.nn.Module): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE="sayef/fsner-bert-base-uncased" ): """simple docstring""" super(__SCREAMING_SNAKE_CASE , self ).__init__() UpperCamelCase : List[str] = AutoModel.from_pretrained(__SCREAMING_SNAKE_CASE , return_dict=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = torch.nn.CosineSimilarity(3 , 1e-08 ) UpperCamelCase : List[Any] = torch.nn.Softmax(dim=1 ) def _lowercase ( self , **__SCREAMING_SNAKE_CASE ): """simple docstring""" return self.bert(**__SCREAMING_SNAKE_CASE ).last_hidden_state def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" return token_embeddings.sum(2 , keepdim=__SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=1 ): """simple docstring""" return self.softmax(T * self.cos(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Any = W_supports['''sizes'''].tolist() UpperCamelCase : Optional[int] = W_supports['''start_token_id'''].item() UpperCamelCase : Any = W_supports['''end_token_id'''].item() del W_supports["sizes"] del W_supports["start_token_id"] del W_supports["end_token_id"] UpperCamelCase : Union[str, Any] = self.BERT(**__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = self.BERT(**__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[Any] = None UpperCamelCase : Any = None UpperCamelCase : Optional[Any] = W_supports['''input_ids'''] == start_token_id UpperCamelCase : Any = W_supports['''input_ids'''] == end_token_id for i, size in enumerate(__SCREAMING_SNAKE_CASE ): if i == 0: UpperCamelCase : Optional[int] = 0 else: UpperCamelCase : Tuple = support_sizes[i - 1] UpperCamelCase : Tuple = S[s : s + size][start_token_masks[s : s + size]] UpperCamelCase : List[str] = S[s : s + size][end_token_masks[s : s + size]] UpperCamelCase : Dict = torch.matmul(q[i] , s_start.T ).sum(1 ).softmax(0 ) UpperCamelCase : Tuple = torch.matmul(q[i] , s_end.T ).sum(1 ).softmax(0 ) if p_starts is not None: UpperCamelCase : List[str] = torch.vstack((p_starts, p_start) ) UpperCamelCase : Union[str, Any] = torch.vstack((p_ends, p_end) ) else: UpperCamelCase : str = p_start UpperCamelCase : Optional[int] = p_end return p_starts, p_ends
643
1
import sys import webbrowser import requests from bsa import BeautifulSoup from fake_useragent import UserAgent if __name__ == "__main__": print("Googling.....") __UpperCAmelCase : int = "https://www.google.com/search?q=" + " ".join(sys.argv[1:]) __UpperCAmelCase : Optional[Any] = requests.get(url, headers={"UserAgent": UserAgent().random}) # res.raise_for_status() with open("project1a.html", "wb") as out_file: # only for knowing the class for data in res.iter_content(10000): out_file.write(data) __UpperCAmelCase : Union[str, Any] = BeautifulSoup(res.text, "html.parser") __UpperCAmelCase : str = list(soup.select(".eZt8xd"))[:5] print(len(links)) for link in links: if link.text == "Maps": webbrowser.open(link.get("href")) else: webbrowser.open(f'''https://google.com{link.get('href')}''')
643
import json import os import unittest from transformers import DebertaTokenizer, DebertaTokenizerFast from transformers.models.deberta.tokenization_deberta import VOCAB_FILES_NAMES from transformers.testing_utils import slow from ...test_tokenization_common import TokenizerTesterMixin class UpperCAmelCase_ ( _a, unittest.TestCase): '''simple docstring''' __UpperCamelCase : str = DebertaTokenizer __UpperCamelCase : Optional[int] = True __UpperCamelCase : Optional[int] = DebertaTokenizerFast def _lowercase ( self ): """simple docstring""" super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt UpperCamelCase : Optional[int] = [ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''\u0120''', '''\u0120l''', '''\u0120n''', '''\u0120lo''', '''\u0120low''', '''er''', '''\u0120lowest''', '''\u0120newer''', '''\u0120wider''', '''[UNK]''', ] UpperCamelCase : Tuple = dict(zip(__SCREAMING_SNAKE_CASE , range(len(__SCREAMING_SNAKE_CASE ) ) ) ) UpperCamelCase : Any = ['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', ''''''] UpperCamelCase : List[Any] = {'''unk_token''': '''[UNK]'''} UpperCamelCase : Union[str, Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) UpperCamelCase : Optional[int] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''merges_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(__SCREAMING_SNAKE_CASE ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(__SCREAMING_SNAKE_CASE ) ) def _lowercase ( self , **__SCREAMING_SNAKE_CASE ): """simple docstring""" kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname , **__SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : int = '''lower newer''' UpperCamelCase : Union[str, Any] = '''lower newer''' return input_text, output_text def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[str] = self.get_tokenizer() UpperCamelCase : int = '''lower newer''' UpperCamelCase : Union[str, Any] = ['''l''', '''o''', '''w''', '''er''', '''\u0120''', '''n''', '''e''', '''w''', '''er'''] UpperCamelCase : Tuple = tokenizer.tokenize(__SCREAMING_SNAKE_CASE ) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) UpperCamelCase : List[Any] = tokens + [tokenizer.unk_token] UpperCamelCase : Optional[int] = [0, 1, 2, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(tokenizer.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : int = self.get_tokenizer() UpperCamelCase : Optional[Any] = tokenizer('''Hello''' , '''World''' ) UpperCamelCase : List[str] = [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1] self.assertListEqual(tokd['''token_type_ids'''] , __SCREAMING_SNAKE_CASE ) @slow def _lowercase ( self ): """simple docstring""" UpperCamelCase : Union[str, Any] = self.tokenizer_class.from_pretrained('''microsoft/deberta-base''' ) UpperCamelCase : Optional[Any] = tokenizer.encode('''sequence builders''' , add_special_tokens=__SCREAMING_SNAKE_CASE ) UpperCamelCase : int = tokenizer.encode('''multi-sequence build''' , add_special_tokens=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Dict = tokenizer.encode( '''sequence builders''' , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = tokenizer.encode( '''sequence builders''' , '''multi-sequence build''' , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = tokenizer.build_inputs_with_special_tokens(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = tokenizer.build_inputs_with_special_tokens(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) assert encoded_sentence == encoded_text_from_decode assert encoded_pair == encoded_pair_from_decode @slow def _lowercase ( self ): """simple docstring""" UpperCamelCase : Optional[Any] = [self.tokenizer_class] if self.test_rust_tokenizer: tokenizer_classes.append(self.rust_tokenizer_class ) for tokenizer_class in tokenizer_classes: UpperCamelCase : Optional[int] = tokenizer_class.from_pretrained('''microsoft/deberta-base''' ) UpperCamelCase : str = [ '''ALBERT: A Lite BERT for Self-supervised Learning of Language Representations''', '''ALBERT incorporates two parameter reduction techniques''', '''The first one is a factorized embedding parameterization. By decomposing the large vocabulary''' ''' embedding matrix into two small matrices, we separate the size of the hidden layers from the size of''' ''' vocabulary embedding.''', ] UpperCamelCase : Union[str, Any] = tokenizer(__SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = [tokenizer.decode(__SCREAMING_SNAKE_CASE , skip_special_tokens=__SCREAMING_SNAKE_CASE ) for seq in encoding['''input_ids''']] # fmt: off UpperCamelCase : int = { '''input_ids''': [ [1, 2_118, 11_126, 565, 35, 83, 25_191, 163, 18_854, 13, 12_156, 12, 16_101, 25_376, 13_807, 9, 22_205, 27_893, 1_635, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 2_118, 11_126, 565, 24_536, 80, 43_797, 4_878, 7_373, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 133, 78, 65, 16, 10, 3_724, 1_538, 33_183, 11_303, 43_797, 1_938, 4, 870, 24_165, 29_105, 5, 739, 32_644, 33_183, 11_303, 36_173, 88, 80, 650, 7_821, 45_940, 6, 52, 2_559, 5, 1_836, 9, 5, 7_397, 13_171, 31, 5, 1_836, 9, 32_644, 33_183, 11_303, 4, 2] ], '''token_type_ids''': [ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ], '''attention_mask''': [ [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] ] } # fmt: on UpperCamelCase : List[str] = [ '''ALBERT: A Lite BERT for Self-supervised Learning of Language Representations''', '''ALBERT incorporates two parameter reduction techniques''', '''The first one is a factorized embedding parameterization. By decomposing the large vocabulary''' ''' embedding matrix into two small matrices, we separate the size of the hidden layers from the size of''' ''' vocabulary embedding.''', ] self.assertDictEqual(encoding.data , __SCREAMING_SNAKE_CASE ) for expected, decoded in zip(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE )
643
1
import logging from pathlib import Path import numpy as np import pytorch_lightning as pl import torch from pytorch_lightning.callbacks import EarlyStopping, ModelCheckpoint from pytorch_lightning.utilities import rank_zero_only from utils_rag import save_json def a ( SCREAMING_SNAKE_CASE_ : str ): """simple docstring""" UpperCamelCase : Dict = filter(lambda SCREAMING_SNAKE_CASE_ : p.requires_grad , model.parameters() ) UpperCamelCase : int = sum([np.prod(p.size() ) for p in model_parameters] ) return params __UpperCAmelCase : List[str] = logging.getLogger(__name__) def a ( SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : Dict ): """simple docstring""" if metric == "rouge2": UpperCamelCase : str = '''{val_avg_rouge2:.4f}-{step_count}''' elif metric == "bleu": UpperCamelCase : Optional[Any] = '''{val_avg_bleu:.4f}-{step_count}''' elif metric == "em": UpperCamelCase : int = '''{val_avg_em:.4f}-{step_count}''' else: raise NotImplementedError( F"""seq2seq callbacks only support rouge2 and bleu, got {metric}, You can make your own by adding to this""" ''' function.''' ) UpperCamelCase : Tuple = ModelCheckpoint( dirpath=SCREAMING_SNAKE_CASE_ , filename=SCREAMING_SNAKE_CASE_ , monitor=F"""val_{metric}""" , mode='''max''' , save_top_k=3 , every_n_epochs=1 , ) return checkpoint_callback def a ( SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : List[Any] ): """simple docstring""" return EarlyStopping( monitor=F"""val_{metric}""" , mode='''min''' if '''loss''' in metric else '''max''' , patience=SCREAMING_SNAKE_CASE_ , verbose=SCREAMING_SNAKE_CASE_ , ) class UpperCAmelCase_ ( pl.Callback): '''simple docstring''' def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Union[str, Any] = {f"""lr_group_{i}""": param['''lr'''] for i, param in enumerate(pl_module.trainer.optimizers[0].param_groups )} pl_module.logger.log_metrics(__SCREAMING_SNAKE_CASE ) @rank_zero_only def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=True ): """simple docstring""" logger.info(f"""***** {type_path} results at step {trainer.global_step:05d} *****""" ) UpperCamelCase : Union[str, Any] = trainer.callback_metrics trainer.logger.log_metrics({k: v for k, v in metrics.items() if k not in ['''log''', '''progress_bar''', '''preds''']} ) # Log results UpperCamelCase : List[str] = Path(pl_module.hparams.output_dir ) if type_path == "test": UpperCamelCase : Union[str, Any] = od / '''test_results.txt''' UpperCamelCase : Any = od / '''test_generations.txt''' else: # this never gets hit. I prefer not to save intermediate generations, and results are in metrics.json # If people want this it will be easy enough to add back. UpperCamelCase : Optional[int] = od / f"""{type_path}_results/{trainer.global_step:05d}.txt""" UpperCamelCase : Tuple = od / f"""{type_path}_generations/{trainer.global_step:05d}.txt""" results_file.parent.mkdir(exist_ok=__SCREAMING_SNAKE_CASE ) generations_file.parent.mkdir(exist_ok=__SCREAMING_SNAKE_CASE ) with open(__SCREAMING_SNAKE_CASE , '''a+''' ) as writer: for key in sorted(__SCREAMING_SNAKE_CASE ): if key in ["log", "progress_bar", "preds"]: continue UpperCamelCase : Any = metrics[key] if isinstance(__SCREAMING_SNAKE_CASE , torch.Tensor ): UpperCamelCase : Any = val.item() UpperCamelCase : Dict = f"""{key}: {val:.6f}\n""" writer.write(__SCREAMING_SNAKE_CASE ) if not save_generations: return if "preds" in metrics: UpperCamelCase : int = '''\n'''.join(metrics['''preds'''] ) generations_file.open('''w+''' ).write(__SCREAMING_SNAKE_CASE ) @rank_zero_only def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" try: UpperCamelCase : Optional[int] = pl_module.model.model.num_parameters() except AttributeError: UpperCamelCase : Dict = pl_module.model.num_parameters() UpperCamelCase : int = count_trainable_parameters(__SCREAMING_SNAKE_CASE ) # mp stands for million parameters trainer.logger.log_metrics({'''n_params''': npars, '''mp''': npars / 1e6, '''grad_mp''': n_trainable_pars / 1e6} ) @rank_zero_only def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" save_json(pl_module.metrics , pl_module.metrics_save_path ) return self._write_logs(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , '''test''' ) @rank_zero_only def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" save_json(pl_module.metrics , pl_module.metrics_save_path ) # Uncommenting this will save val generations # return self._write_logs(trainer, pl_module, "valid")
643
import re from pathlib import Path from unittest import TestCase import pytest @pytest.mark.integration class UpperCAmelCase_ ( _a): '''simple docstring''' def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" with open(__SCREAMING_SNAKE_CASE , encoding='''utf-8''' ) as input_file: UpperCamelCase : str = re.compile(R'''(?!.*\b(?:encoding|rb|w|wb|w+|wb+|ab|ab+)\b)(?<=\s)(open)\((.*)\)''' ) UpperCamelCase : Optional[int] = input_file.read() UpperCamelCase : Union[str, Any] = regexp.search(__SCREAMING_SNAKE_CASE ) return match def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" with open(__SCREAMING_SNAKE_CASE , encoding='''utf-8''' ) as input_file: UpperCamelCase : Optional[int] = re.compile(R'''#[^\r\n]*print\(|\"[^\r\n]*print\(|\"\"\".*?print\(.*?\"\"\"|(print\()''' , re.DOTALL ) UpperCamelCase : Tuple = input_file.read() # use `re.finditer` to handle the case where the ignored groups would be matched first by `re.search` UpperCamelCase : Dict = regexp.finditer(__SCREAMING_SNAKE_CASE ) UpperCamelCase : int = [match for match in matches if match is not None and match.group(1 ) is not None] return matches[0] if matches else None def _lowercase ( self ): """simple docstring""" UpperCamelCase : int = Path('''./datasets''' ) UpperCamelCase : Tuple = list(dataset_paths.absolute().glob('''**/*.py''' ) ) for dataset in dataset_files: if self._no_encoding_on_file_open(str(__SCREAMING_SNAKE_CASE ) ): raise AssertionError(f"""open(...) must use utf-8 encoding in {dataset}""" ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Optional[int] = Path('''./datasets''' ) UpperCamelCase : Tuple = list(dataset_paths.absolute().glob('''**/*.py''' ) ) for dataset in dataset_files: if self._no_print_statements(str(__SCREAMING_SNAKE_CASE ) ): raise AssertionError(f"""print statement found in {dataset}. Use datasets.logger/logging instead.""" )
643
1
from typing import List, Optional, Union import numpy as np from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import PaddingStrategy, TensorType, logging __UpperCAmelCase : Union[str, Any] = logging.get_logger(__name__) class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : Any = ["input_values", "padding_mask"] def __init__( self , __SCREAMING_SNAKE_CASE = 1 , __SCREAMING_SNAKE_CASE = 24_000 , __SCREAMING_SNAKE_CASE = 0.0 , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" super().__init__(feature_size=__SCREAMING_SNAKE_CASE , sampling_rate=__SCREAMING_SNAKE_CASE , padding_value=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) UpperCamelCase : str = chunk_length_s UpperCamelCase : str = overlap @property def _lowercase ( self ): """simple docstring""" if self.chunk_length_s is None: return None else: return int(self.chunk_length_s * self.sampling_rate ) @property def _lowercase ( self ): """simple docstring""" if self.chunk_length_s is None or self.overlap is None: return None else: return max(1 , int((1.0 - self.overlap) * self.chunk_length ) ) def __call__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = False , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , ): """simple docstring""" if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( f"""The model corresponding to this feature extractor: {self} was trained using a sampling rate of""" f""" {self.sampling_rate}. Please make sure that the provided audio input was sampled with""" f""" {self.sampling_rate} and not {sampling_rate}.""" ) else: logger.warning( '''It is strongly recommended to pass the `sampling_rate` argument to this function. ''' '''Failing to do so can result in silent errors that might be hard to debug.''' ) if padding and truncation: raise ValueError('''Both padding and truncation were set. Make sure you only set one.''' ) elif padding is None: # by default let's pad the inputs UpperCamelCase : int = True UpperCamelCase : Any = bool( isinstance(__SCREAMING_SNAKE_CASE , (list, tuple) ) and (isinstance(raw_audio[0] , (np.ndarray, tuple, list) )) ) if is_batched: UpperCamelCase : Any = [np.asarray(__SCREAMING_SNAKE_CASE , dtype=np.floataa ).T for audio in raw_audio] elif not is_batched and not isinstance(__SCREAMING_SNAKE_CASE , np.ndarray ): UpperCamelCase : List[str] = np.asarray(__SCREAMING_SNAKE_CASE , dtype=np.floataa ) elif isinstance(__SCREAMING_SNAKE_CASE , np.ndarray ) and raw_audio.dtype is np.dtype(np.floataa ): UpperCamelCase : Optional[int] = raw_audio.astype(np.floataa ) # always return batch if not is_batched: UpperCamelCase : Tuple = [np.asarray(__SCREAMING_SNAKE_CASE ).T] # verify inputs are valid for idx, example in enumerate(__SCREAMING_SNAKE_CASE ): if example.ndim > 2: raise ValueError(f"""Expected input shape (channels, length) but got shape {example.shape}""" ) if self.feature_size == 1 and example.ndim != 1: raise ValueError(f"""Expected mono audio but example has {example.shape[-1]} channels""" ) if self.feature_size == 2 and example.shape[-1] != 2: raise ValueError(f"""Expected stereo audio but example has {example.shape[-1]} channels""" ) UpperCamelCase : List[Any] = None UpperCamelCase : Optional[int] = BatchFeature({'''input_values''': raw_audio} ) if self.chunk_stride is not None and self.chunk_length is not None and max_length is None: if truncation: UpperCamelCase : Optional[Any] = min(array.shape[0] for array in raw_audio ) UpperCamelCase : Tuple = int(np.floor(max_length / self.chunk_stride ) ) UpperCamelCase : str = (nb_step - 1) * self.chunk_stride + self.chunk_length elif padding: UpperCamelCase : List[str] = max(array.shape[0] for array in raw_audio ) UpperCamelCase : Tuple = int(np.ceil(max_length / self.chunk_stride ) ) UpperCamelCase : Any = (nb_step - 1) * self.chunk_stride + self.chunk_length UpperCamelCase : Dict = '''max_length''' else: UpperCamelCase : List[str] = input_values # normal padding on batch if padded_inputs is None: UpperCamelCase : List[str] = self.pad( __SCREAMING_SNAKE_CASE , max_length=__SCREAMING_SNAKE_CASE , truncation=__SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE , return_attention_mask=__SCREAMING_SNAKE_CASE , ) if padding: UpperCamelCase : str = padded_inputs.pop('''attention_mask''' ) UpperCamelCase : Any = [] for example in padded_inputs.pop('''input_values''' ): if self.feature_size == 1: UpperCamelCase : Optional[int] = example[..., None] input_values.append(example.T ) UpperCamelCase : Optional[Any] = input_values if return_tensors is not None: UpperCamelCase : List[str] = padded_inputs.convert_to_tensors(__SCREAMING_SNAKE_CASE ) return padded_inputs
643
from __future__ import annotations import unittest from transformers import XGLMConfig, XGLMTokenizer, 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 tensorflow as tf from transformers.models.xglm.modeling_tf_xglm import ( TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFXGLMForCausalLM, TFXGLMModel, ) @require_tf class UpperCAmelCase_ : '''simple docstring''' __UpperCamelCase : Any = XGLMConfig __UpperCamelCase : Dict = {} __UpperCamelCase : List[str] = "gelu" def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=14 , __SCREAMING_SNAKE_CASE=7 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=99 , __SCREAMING_SNAKE_CASE=32 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=4 , __SCREAMING_SNAKE_CASE=37 , __SCREAMING_SNAKE_CASE="gelu" , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=512 , __SCREAMING_SNAKE_CASE=0.02 , ): """simple docstring""" UpperCamelCase : Any = parent UpperCamelCase : Optional[int] = batch_size UpperCamelCase : str = seq_length UpperCamelCase : List[str] = is_training UpperCamelCase : Tuple = use_input_mask UpperCamelCase : Union[str, Any] = use_labels UpperCamelCase : int = vocab_size UpperCamelCase : Optional[int] = d_model UpperCamelCase : Any = num_hidden_layers UpperCamelCase : List[str] = num_attention_heads UpperCamelCase : Optional[Any] = ffn_dim UpperCamelCase : Optional[int] = activation_function UpperCamelCase : List[str] = activation_dropout UpperCamelCase : Any = attention_dropout UpperCamelCase : str = max_position_embeddings UpperCamelCase : Union[str, Any] = initializer_range UpperCamelCase : int = None UpperCamelCase : Dict = 0 UpperCamelCase : int = 2 UpperCamelCase : Any = 1 def _lowercase ( self ): """simple docstring""" return XGLMConfig.from_pretrained('''facebook/xglm-564M''' ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Optional[int] = tf.clip_by_value( ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) , clip_value_min=0 , clip_value_max=3 ) UpperCamelCase : int = None if self.use_input_mask: UpperCamelCase : Any = random_attention_mask([self.batch_size, self.seq_length] ) UpperCamelCase : Tuple = self.get_config() UpperCamelCase : str = floats_tensor([self.num_hidden_layers, self.num_attention_heads] , 2 ) return ( config, input_ids, input_mask, head_mask, ) def _lowercase ( self ): """simple docstring""" return XGLMConfig( vocab_size=self.vocab_size , d_model=self.hidden_size , num_layers=self.num_hidden_layers , attention_heads=self.num_attention_heads , ffn_dim=self.ffn_dim , activation_function=self.activation_function , activation_dropout=self.activation_dropout , attention_dropout=self.attention_dropout , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , use_cache=__SCREAMING_SNAKE_CASE , bos_token_id=self.bos_token_id , eos_token_id=self.eos_token_id , pad_token_id=self.pad_token_id , return_dict=__SCREAMING_SNAKE_CASE , ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[str] = self.prepare_config_and_inputs() ( ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ) : Dict = config_and_inputs UpperCamelCase : List[str] = { '''input_ids''': input_ids, '''head_mask''': head_mask, } return config, inputs_dict @require_tf class UpperCAmelCase_ ( _a, _a, unittest.TestCase): '''simple docstring''' __UpperCamelCase : Optional[Any] = (TFXGLMModel, TFXGLMForCausalLM) if is_tf_available() else () __UpperCamelCase : Union[str, Any] = (TFXGLMForCausalLM,) if is_tf_available() else () __UpperCamelCase : Any = ( {"feature-extraction": TFXGLMModel, "text-generation": TFXGLMForCausalLM} if is_tf_available() else {} ) __UpperCamelCase : Optional[int] = False __UpperCamelCase : List[Any] = False __UpperCamelCase : List[Any] = False def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[Any] = TFXGLMModelTester(self ) UpperCamelCase : Any = ConfigTester(self , config_class=__SCREAMING_SNAKE_CASE , n_embd=37 ) def _lowercase ( self ): """simple docstring""" self.config_tester.run_common_tests() @slow def _lowercase ( self ): """simple docstring""" for model_name in TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCamelCase : List[Any] = TFXGLMModel.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsNotNone(__SCREAMING_SNAKE_CASE ) @unittest.skip(reason='''Currently, model embeddings are going to undergo a major refactor.''' ) def _lowercase ( self ): """simple docstring""" super().test_resize_token_embeddings() @require_tf class UpperCAmelCase_ ( unittest.TestCase): '''simple docstring''' @slow def _lowercase ( self , __SCREAMING_SNAKE_CASE=True ): """simple docstring""" UpperCamelCase : List[str] = TFXGLMForCausalLM.from_pretrained('''facebook/xglm-564M''' ) UpperCamelCase : List[Any] = tf.convert_to_tensor([[2, 268, 9_865]] , dtype=tf.intaa ) # The dog # </s> The dog is a very friendly dog. He is very affectionate and loves to play with other # fmt: off UpperCamelCase : str = [2, 268, 9_865, 67, 11, 1_988, 57_252, 9_865, 5, 984, 67, 1_988, 213_838, 1_658, 53, 70_446, 33, 6_657, 278, 1_581] # fmt: on UpperCamelCase : Union[str, Any] = model.generate(__SCREAMING_SNAKE_CASE , do_sample=__SCREAMING_SNAKE_CASE , num_beams=1 ) if verify_outputs: self.assertListEqual(output_ids[0].numpy().tolist() , __SCREAMING_SNAKE_CASE ) @slow def _lowercase ( self ): """simple docstring""" UpperCamelCase : str = XGLMTokenizer.from_pretrained('''facebook/xglm-564M''' ) UpperCamelCase : List[str] = TFXGLMForCausalLM.from_pretrained('''facebook/xglm-564M''' ) tf.random.set_seed(0 ) UpperCamelCase : Tuple = tokenizer('''Today is a nice day and''' , return_tensors='''tf''' ) UpperCamelCase : int = tokenized.input_ids # forces the generation to happen on CPU, to avoid GPU-related quirks (and assure same output regardless of the available devices) with tf.device(''':/CPU:0''' ): UpperCamelCase : str = model.generate(__SCREAMING_SNAKE_CASE , do_sample=__SCREAMING_SNAKE_CASE , seed=[7, 0] ) UpperCamelCase : Dict = tokenizer.decode(output_ids[0] , skip_special_tokens=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = ( '''Today is a nice day and warm evening here over Southern Alberta!! Today when they closed schools due''' ) self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) @slow def _lowercase ( self ): """simple docstring""" UpperCamelCase : Dict = TFXGLMForCausalLM.from_pretrained('''facebook/xglm-564M''' ) UpperCamelCase : Tuple = XGLMTokenizer.from_pretrained('''facebook/xglm-564M''' ) UpperCamelCase : Tuple = '''left''' # use different length sentences to test batching UpperCamelCase : Any = [ '''This is an extremelly long sentence that only exists to test the ability of the model to cope with ''' '''left-padding, such as in batched generation. The output for the sequence below should be the same ''' '''regardless of whether left padding is applied or not. When''', '''Hello, my dog is a little''', ] UpperCamelCase : List[Any] = tokenizer(__SCREAMING_SNAKE_CASE , return_tensors='''tf''' , padding=__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = inputs['''input_ids'''] UpperCamelCase : Optional[int] = model.generate(input_ids=__SCREAMING_SNAKE_CASE , attention_mask=inputs['''attention_mask'''] , max_new_tokens=12 ) UpperCamelCase : Optional[int] = tokenizer(sentences[0] , return_tensors='''tf''' ).input_ids UpperCamelCase : Optional[Any] = model.generate(input_ids=__SCREAMING_SNAKE_CASE , max_new_tokens=12 ) UpperCamelCase : str = tokenizer(sentences[1] , return_tensors='''tf''' ).input_ids UpperCamelCase : List[Any] = model.generate(input_ids=__SCREAMING_SNAKE_CASE , max_new_tokens=12 ) UpperCamelCase : Any = tokenizer.batch_decode(__SCREAMING_SNAKE_CASE , skip_special_tokens=__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = tokenizer.decode(output_non_padded[0] , skip_special_tokens=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Tuple = tokenizer.decode(output_padded[0] , skip_special_tokens=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = [ '''This is an extremelly long sentence that only exists to test the ability of the model to cope with ''' '''left-padding, such as in batched generation. The output for the sequence below should be the same ''' '''regardless of whether left padding is applied or not. When left padding is applied, the sequence will be ''' '''a single''', '''Hello, my dog is a little bit of a shy one, but he is very friendly''', ] self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) self.assertListEqual(__SCREAMING_SNAKE_CASE , [non_padded_sentence, padded_sentence] )
643
1
import json from typing import List, Optional, Tuple from tokenizers import normalizers from tokenizers.pre_tokenizers import BertPreTokenizer, PreTokenizer from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_roformer import RoFormerTokenizer from .tokenization_utils import JiebaPreTokenizer __UpperCAmelCase : Dict = logging.get_logger(__name__) __UpperCAmelCase : Optional[Any] = {"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"} __UpperCAmelCase : Dict = { "vocab_file": { "junnyu/roformer_chinese_small": "https://huggingface.co/junnyu/roformer_chinese_small/resolve/main/vocab.txt", "junnyu/roformer_chinese_base": "https://huggingface.co/junnyu/roformer_chinese_base/resolve/main/vocab.txt", "junnyu/roformer_chinese_char_small": ( "https://huggingface.co/junnyu/roformer_chinese_char_small/resolve/main/vocab.txt" ), "junnyu/roformer_chinese_char_base": ( "https://huggingface.co/junnyu/roformer_chinese_char_base/resolve/main/vocab.txt" ), "junnyu/roformer_small_discriminator": ( "https://huggingface.co/junnyu/roformer_small_discriminator/resolve/main/vocab.txt" ), "junnyu/roformer_small_generator": ( "https://huggingface.co/junnyu/roformer_small_generator/resolve/main/vocab.txt" ), } } __UpperCAmelCase : Tuple = { "junnyu/roformer_chinese_small": 1536, "junnyu/roformer_chinese_base": 1536, "junnyu/roformer_chinese_char_small": 512, "junnyu/roformer_chinese_char_base": 512, "junnyu/roformer_small_discriminator": 128, "junnyu/roformer_small_generator": 128, } __UpperCAmelCase : Any = { "junnyu/roformer_chinese_small": {"do_lower_case": True}, "junnyu/roformer_chinese_base": {"do_lower_case": True}, "junnyu/roformer_chinese_char_small": {"do_lower_case": True}, "junnyu/roformer_chinese_char_base": {"do_lower_case": True}, "junnyu/roformer_small_discriminator": {"do_lower_case": True}, "junnyu/roformer_small_generator": {"do_lower_case": True}, } class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : str = VOCAB_FILES_NAMES __UpperCamelCase : Optional[int] = PRETRAINED_VOCAB_FILES_MAP __UpperCamelCase : Union[str, Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCamelCase : int = PRETRAINED_INIT_CONFIGURATION __UpperCamelCase : Any = RoFormerTokenizer def __init__( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE="[UNK]" , __SCREAMING_SNAKE_CASE="[SEP]" , __SCREAMING_SNAKE_CASE="[PAD]" , __SCREAMING_SNAKE_CASE="[CLS]" , __SCREAMING_SNAKE_CASE="[MASK]" , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" super().__init__( __SCREAMING_SNAKE_CASE , tokenizer_file=__SCREAMING_SNAKE_CASE , do_lower_case=__SCREAMING_SNAKE_CASE , unk_token=__SCREAMING_SNAKE_CASE , sep_token=__SCREAMING_SNAKE_CASE , pad_token=__SCREAMING_SNAKE_CASE , cls_token=__SCREAMING_SNAKE_CASE , mask_token=__SCREAMING_SNAKE_CASE , tokenize_chinese_chars=__SCREAMING_SNAKE_CASE , strip_accents=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) UpperCamelCase : List[Any] = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( pre_tok_state.get('''lowercase''' , __SCREAMING_SNAKE_CASE ) != do_lower_case or pre_tok_state.get('''strip_accents''' , __SCREAMING_SNAKE_CASE ) != strip_accents ): UpperCamelCase : List[Any] = getattr(__SCREAMING_SNAKE_CASE , pre_tok_state.pop('''type''' ) ) UpperCamelCase : Optional[int] = do_lower_case UpperCamelCase : Optional[Any] = strip_accents UpperCamelCase : List[Any] = pre_tok_class(**__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[Any] = do_lower_case def __getstate__( self ): """simple docstring""" UpperCamelCase : Optional[int] = self.__dict__.copy() UpperCamelCase : Any = BertPreTokenizer() return state def __setstate__( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Any = d UpperCamelCase : List[str] = self.__dict__['''_tokenizer'''].get_vocab() UpperCamelCase : Any = PreTokenizer.custom(JiebaPreTokenizer(__SCREAMING_SNAKE_CASE ) ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None ): """simple docstring""" UpperCamelCase : Any = [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 _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ): """simple docstring""" UpperCamelCase : Dict = [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 ) * [0] + len(token_ids_a + sep ) * [1] def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ): """simple docstring""" UpperCamelCase : List[Any] = self._tokenizer.model.save(__SCREAMING_SNAKE_CASE , name=__SCREAMING_SNAKE_CASE ) return tuple(__SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=False , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" UpperCamelCase : Any = BertPreTokenizer() return super().save_pretrained(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE )
643
import os from shutil import copyfile from typing import List, Optional, Tuple from tokenizers import processors from ...tokenization_utils import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_mbart import MBartTokenizer else: __UpperCAmelCase : Optional[int] = None __UpperCAmelCase : int = logging.get_logger(__name__) __UpperCAmelCase : List[Any] = {"vocab_file": "sentencepiece.bpe.model", "tokenizer_file": "tokenizer.json"} __UpperCAmelCase : str = { "vocab_file": { "facebook/mbart-large-en-ro": ( "https://huggingface.co/facebook/mbart-large-en-ro/resolve/main/sentencepiece.bpe.model" ), "facebook/mbart-large-cc25": ( "https://huggingface.co/facebook/mbart-large-cc25/resolve/main/sentencepiece.bpe.model" ), }, "tokenizer_file": { "facebook/mbart-large-en-ro": "https://huggingface.co/facebook/mbart-large-en-ro/resolve/main/tokenizer.json", "facebook/mbart-large-cc25": "https://huggingface.co/facebook/mbart-large-cc25/resolve/main/tokenizer.json", }, } __UpperCAmelCase : Union[str, Any] = { "facebook/mbart-large-en-ro": 1024, "facebook/mbart-large-cc25": 1024, } # fmt: off __UpperCAmelCase : Any = ["ar_AR", "cs_CZ", "de_DE", "en_XX", "es_XX", "et_EE", "fi_FI", "fr_XX", "gu_IN", "hi_IN", "it_IT", "ja_XX", "kk_KZ", "ko_KR", "lt_LT", "lv_LV", "my_MM", "ne_NP", "nl_XX", "ro_RO", "ru_RU", "si_LK", "tr_TR", "vi_VN", "zh_CN"] class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : List[str] = VOCAB_FILES_NAMES __UpperCamelCase : str = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCamelCase : Tuple = PRETRAINED_VOCAB_FILES_MAP __UpperCamelCase : Union[str, Any] = ["input_ids", "attention_mask"] __UpperCamelCase : Any = MBartTokenizer __UpperCamelCase : List[int] = [] __UpperCamelCase : List[int] = [] def __init__( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE="<s>" , __SCREAMING_SNAKE_CASE="</s>" , __SCREAMING_SNAKE_CASE="</s>" , __SCREAMING_SNAKE_CASE="<s>" , __SCREAMING_SNAKE_CASE="<unk>" , __SCREAMING_SNAKE_CASE="<pad>" , __SCREAMING_SNAKE_CASE="<mask>" , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" UpperCamelCase : Union[str, Any] = AddedToken(__SCREAMING_SNAKE_CASE , lstrip=__SCREAMING_SNAKE_CASE , rstrip=__SCREAMING_SNAKE_CASE ) if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else mask_token super().__init__( vocab_file=__SCREAMING_SNAKE_CASE , tokenizer_file=__SCREAMING_SNAKE_CASE , bos_token=__SCREAMING_SNAKE_CASE , eos_token=__SCREAMING_SNAKE_CASE , sep_token=__SCREAMING_SNAKE_CASE , cls_token=__SCREAMING_SNAKE_CASE , unk_token=__SCREAMING_SNAKE_CASE , pad_token=__SCREAMING_SNAKE_CASE , mask_token=__SCREAMING_SNAKE_CASE , src_lang=__SCREAMING_SNAKE_CASE , tgt_lang=__SCREAMING_SNAKE_CASE , additional_special_tokens=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) UpperCamelCase : Dict = vocab_file UpperCamelCase : List[str] = False if not self.vocab_file else True UpperCamelCase : List[Any] = FAIRSEQ_LANGUAGE_CODES.copy() if additional_special_tokens is not None: # Only add those special tokens if they are not already there. _additional_special_tokens.extend( [t for t in additional_special_tokens if t not in _additional_special_tokens] ) self.add_special_tokens({'''additional_special_tokens''': _additional_special_tokens} ) UpperCamelCase : List[Any] = { lang_code: self.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) for lang_code in FAIRSEQ_LANGUAGE_CODES } UpperCamelCase : Dict = src_lang if src_lang is not None else '''en_XX''' UpperCamelCase : List[Any] = self.convert_tokens_to_ids(self._src_lang ) UpperCamelCase : str = tgt_lang self.set_src_lang_special_tokens(self._src_lang ) @property def _lowercase ( self ): """simple docstring""" return self._src_lang @src_lang.setter def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Union[str, Any] = new_src_lang self.set_src_lang_special_tokens(self._src_lang ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ): """simple docstring""" if token_ids_a is None: return self.prefix_tokens + token_ids_a + self.suffix_tokens # We don't expect to process pairs, but leave the pair logic for API consistency return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ): """simple docstring""" UpperCamelCase : str = [self.sep_token_id] UpperCamelCase : Union[str, Any] = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): """simple docstring""" if src_lang is None or tgt_lang is None: raise ValueError('''Translation requires a `src_lang` and a `tgt_lang` for this model''' ) UpperCamelCase : List[str] = src_lang UpperCamelCase : Dict = self(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , return_tensors=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[Any] = self.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Any = tgt_lang_id return inputs def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = "en_XX" , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = "ro_RO" , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" UpperCamelCase : Optional[Any] = src_lang UpperCamelCase : Optional[int] = tgt_lang return super().prepare_seqaseq_batch(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" return self.set_src_lang_special_tokens(self.src_lang ) def _lowercase ( self ): """simple docstring""" return self.set_tgt_lang_special_tokens(self.tgt_lang ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Optional[int] = self.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Any = [] UpperCamelCase : Dict = [self.eos_token_id, self.cur_lang_code] UpperCamelCase : Union[str, Any] = self.convert_ids_to_tokens(self.prefix_tokens ) UpperCamelCase : int = self.convert_ids_to_tokens(self.suffix_tokens ) UpperCamelCase : Tuple = processors.TemplateProcessing( single=prefix_tokens_str + ['''$A'''] + suffix_tokens_str , pair=prefix_tokens_str + ['''$A''', '''$B'''] + suffix_tokens_str , special_tokens=list(zip(prefix_tokens_str + suffix_tokens_str , self.prefix_tokens + self.suffix_tokens ) ) , ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Optional[int] = self.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) UpperCamelCase : int = [] UpperCamelCase : Optional[Any] = [self.eos_token_id, self.cur_lang_code] UpperCamelCase : Optional[Any] = self.convert_ids_to_tokens(self.prefix_tokens ) UpperCamelCase : List[str] = self.convert_ids_to_tokens(self.suffix_tokens ) UpperCamelCase : Optional[int] = processors.TemplateProcessing( single=prefix_tokens_str + ['''$A'''] + suffix_tokens_str , pair=prefix_tokens_str + ['''$A''', '''$B'''] + suffix_tokens_str , special_tokens=list(zip(prefix_tokens_str + suffix_tokens_str , self.prefix_tokens + self.suffix_tokens ) ) , ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ): """simple docstring""" if not self.can_save_slow_tokenizer: raise ValueError( '''Your fast tokenizer does not have the necessary information to save the vocabulary for a slow ''' '''tokenizer.''' ) if not os.path.isdir(__SCREAMING_SNAKE_CASE ): logger.error(f"""Vocabulary path ({save_directory}) should be a directory.""" ) return UpperCamelCase : Optional[int] = os.path.join( __SCREAMING_SNAKE_CASE , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(__SCREAMING_SNAKE_CASE ): copyfile(self.vocab_file , __SCREAMING_SNAKE_CASE ) return (out_vocab_file,)
643
1
import json import os import shutil import warnings from argparse import ArgumentParser, Namespace from pathlib import Path from typing import List from ..utils import logging from . import BaseTransformersCLICommand try: from cookiecutter.main import cookiecutter __UpperCAmelCase : List[Any] = True except ImportError: __UpperCAmelCase : List[str] = False __UpperCAmelCase : Any = logging.get_logger(__name__) # pylint: disable=invalid-name def a ( SCREAMING_SNAKE_CASE_ : Namespace ): """simple docstring""" return AddNewModelCommand(args.testing , args.testing_file , path=args.path ) class UpperCAmelCase_ ( _a): '''simple docstring''' @staticmethod def _lowercase ( __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : List[Any] = parser.add_parser('''add-new-model''' ) add_new_model_parser.add_argument('''--testing''' , action='''store_true''' , help='''If in testing mode.''' ) add_new_model_parser.add_argument('''--testing_file''' , type=__SCREAMING_SNAKE_CASE , help='''Configuration file on which to run.''' ) add_new_model_parser.add_argument( '''--path''' , type=__SCREAMING_SNAKE_CASE , help='''Path to cookiecutter. Should only be used for testing purposes.''' ) add_new_model_parser.set_defaults(func=__SCREAMING_SNAKE_CASE ) def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , *__SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Tuple = testing UpperCamelCase : Any = testing_file UpperCamelCase : Dict = path def _lowercase ( self ): """simple docstring""" warnings.warn( '''The command `transformers-cli add-new-model` is deprecated and will be removed in v5 of Transformers. ''' '''It is not actively maintained anymore, so might give a result that won\'t pass all tests and quality ''' '''checks, you should use `transformers-cli add-new-model-like` instead.''' ) if not _has_cookiecutter: raise ImportError( '''Model creation dependencies are required to use the `add_new_model` command. Install them by running ''' '''the following at the root of your `transformers` clone:\n\n\t$ pip install -e .[modelcreation]\n''' ) # Ensure that there is no other `cookiecutter-template-xxx` directory in the current working directory UpperCamelCase : List[str] = [directory for directory in os.listdir() if '''cookiecutter-template-''' == directory[:22]] if len(__SCREAMING_SNAKE_CASE ) > 0: raise ValueError( '''Several directories starting with `cookiecutter-template-` in current working directory. ''' '''Please clean your directory by removing all folders starting with `cookiecutter-template-` or ''' '''change your working directory.''' ) UpperCamelCase : Dict = ( Path(__SCREAMING_SNAKE_CASE ).parent.parent.parent.parent if self._path is None else Path(self._path ).parent.parent ) UpperCamelCase : List[Any] = path_to_transformer_root / '''templates''' / '''adding_a_new_model''' # Execute cookiecutter if not self._testing: cookiecutter(str(__SCREAMING_SNAKE_CASE ) ) else: with open(self._testing_file , '''r''' ) as configuration_file: UpperCamelCase : Tuple = json.load(__SCREAMING_SNAKE_CASE ) cookiecutter( str(path_to_cookiecutter if self._path is None else self._path ) , no_input=__SCREAMING_SNAKE_CASE , extra_context=__SCREAMING_SNAKE_CASE , ) UpperCamelCase : Dict = [directory for directory in os.listdir() if '''cookiecutter-template-''' in directory[:22]][0] # Retrieve configuration with open(directory + '''/configuration.json''' , '''r''' ) as configuration_file: UpperCamelCase : Tuple = json.load(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Dict = configuration['''lowercase_modelname'''] UpperCamelCase : int = configuration['''generate_tensorflow_pytorch_and_flax'''] os.remove(f"""{directory}/configuration.json""" ) UpperCamelCase : str = '''PyTorch''' in generate_tensorflow_pytorch_and_flax UpperCamelCase : Any = '''TensorFlow''' in generate_tensorflow_pytorch_and_flax UpperCamelCase : Union[str, Any] = '''Flax''' in generate_tensorflow_pytorch_and_flax UpperCamelCase : Optional[Any] = f"""{path_to_transformer_root}/src/transformers/models/{lowercase_model_name}""" os.makedirs(__SCREAMING_SNAKE_CASE , exist_ok=__SCREAMING_SNAKE_CASE ) os.makedirs(f"""{path_to_transformer_root}/tests/models/{lowercase_model_name}""" , exist_ok=__SCREAMING_SNAKE_CASE ) # Tests require submodules as they have parent imports with open(f"""{path_to_transformer_root}/tests/models/{lowercase_model_name}/__init__.py""" , '''w''' ): pass shutil.move( f"""{directory}/__init__.py""" , f"""{model_dir}/__init__.py""" , ) shutil.move( f"""{directory}/configuration_{lowercase_model_name}.py""" , f"""{model_dir}/configuration_{lowercase_model_name}.py""" , ) def remove_copy_lines(__SCREAMING_SNAKE_CASE ): with open(__SCREAMING_SNAKE_CASE , '''r''' ) as f: UpperCamelCase : Any = f.readlines() with open(__SCREAMING_SNAKE_CASE , '''w''' ) as f: for line in lines: if "# Copied from transformers." not in line: f.write(__SCREAMING_SNAKE_CASE ) if output_pytorch: if not self._testing: remove_copy_lines(f"""{directory}/modeling_{lowercase_model_name}.py""" ) shutil.move( f"""{directory}/modeling_{lowercase_model_name}.py""" , f"""{model_dir}/modeling_{lowercase_model_name}.py""" , ) shutil.move( f"""{directory}/test_modeling_{lowercase_model_name}.py""" , f"""{path_to_transformer_root}/tests/models/{lowercase_model_name}/test_modeling_{lowercase_model_name}.py""" , ) else: os.remove(f"""{directory}/modeling_{lowercase_model_name}.py""" ) os.remove(f"""{directory}/test_modeling_{lowercase_model_name}.py""" ) if output_tensorflow: if not self._testing: remove_copy_lines(f"""{directory}/modeling_tf_{lowercase_model_name}.py""" ) shutil.move( f"""{directory}/modeling_tf_{lowercase_model_name}.py""" , f"""{model_dir}/modeling_tf_{lowercase_model_name}.py""" , ) shutil.move( f"""{directory}/test_modeling_tf_{lowercase_model_name}.py""" , f"""{path_to_transformer_root}/tests/models/{lowercase_model_name}/test_modeling_tf_{lowercase_model_name}.py""" , ) else: os.remove(f"""{directory}/modeling_tf_{lowercase_model_name}.py""" ) os.remove(f"""{directory}/test_modeling_tf_{lowercase_model_name}.py""" ) if output_flax: if not self._testing: remove_copy_lines(f"""{directory}/modeling_flax_{lowercase_model_name}.py""" ) shutil.move( f"""{directory}/modeling_flax_{lowercase_model_name}.py""" , f"""{model_dir}/modeling_flax_{lowercase_model_name}.py""" , ) shutil.move( f"""{directory}/test_modeling_flax_{lowercase_model_name}.py""" , f"""{path_to_transformer_root}/tests/models/{lowercase_model_name}/test_modeling_flax_{lowercase_model_name}.py""" , ) else: os.remove(f"""{directory}/modeling_flax_{lowercase_model_name}.py""" ) os.remove(f"""{directory}/test_modeling_flax_{lowercase_model_name}.py""" ) shutil.move( f"""{directory}/{lowercase_model_name}.md""" , f"""{path_to_transformer_root}/docs/source/en/model_doc/{lowercase_model_name}.md""" , ) shutil.move( f"""{directory}/tokenization_{lowercase_model_name}.py""" , f"""{model_dir}/tokenization_{lowercase_model_name}.py""" , ) shutil.move( f"""{directory}/tokenization_fast_{lowercase_model_name}.py""" , f"""{model_dir}/tokenization_{lowercase_model_name}_fast.py""" , ) from os import fdopen, remove from shutil import copymode, move from tempfile import mkstemp def replace(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): # Create temp file UpperCamelCase , UpperCamelCase : Optional[Any] = mkstemp() UpperCamelCase : Tuple = False with fdopen(__SCREAMING_SNAKE_CASE , '''w''' ) as new_file: with open(__SCREAMING_SNAKE_CASE ) as old_file: for line in old_file: new_file.write(__SCREAMING_SNAKE_CASE ) if line_to_copy_below in line: UpperCamelCase : Optional[int] = True for line_to_copy in lines_to_copy: new_file.write(__SCREAMING_SNAKE_CASE ) if not line_found: raise ValueError(f"""Line {line_to_copy_below} was not found in file.""" ) # Copy the file permissions from the old file to the new file copymode(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # Remove original file remove(__SCREAMING_SNAKE_CASE ) # Move new file move(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def skip_units(__SCREAMING_SNAKE_CASE ): return ( ("generating PyTorch" in line and not output_pytorch) or ("generating TensorFlow" in line and not output_tensorflow) or ("generating Flax" in line and not output_flax) ) def replace_in_files(__SCREAMING_SNAKE_CASE ): with open(__SCREAMING_SNAKE_CASE ) as datafile: UpperCamelCase : int = [] UpperCamelCase : Dict = False UpperCamelCase : List[Any] = False for line in datafile: if "# To replace in: " in line and "##" not in line: UpperCamelCase : Dict = line.split('''"''' )[1] UpperCamelCase : int = skip_units(__SCREAMING_SNAKE_CASE ) elif "# Below: " in line and "##" not in line: UpperCamelCase : Dict = line.split('''"''' )[1] UpperCamelCase : List[str] = skip_units(__SCREAMING_SNAKE_CASE ) elif "# End." in line and "##" not in line: if not skip_file and not skip_snippet: replace(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) UpperCamelCase : Dict = [] elif "# Replace with" in line and "##" not in line: UpperCamelCase : Tuple = [] elif "##" not in line: lines_to_copy.append(__SCREAMING_SNAKE_CASE ) remove(__SCREAMING_SNAKE_CASE ) replace_in_files(f"""{directory}/to_replace_{lowercase_model_name}.py""" ) os.rmdir(__SCREAMING_SNAKE_CASE )
643
import unittest import numpy as np import torch from diffusers import VersatileDiffusionImageVariationPipeline from diffusers.utils.testing_utils import load_image, require_torch_gpu, slow, torch_device __UpperCAmelCase : Dict = False class UpperCAmelCase_ ( unittest.TestCase): '''simple docstring''' pass @slow @require_torch_gpu class UpperCAmelCase_ ( unittest.TestCase): '''simple docstring''' def _lowercase ( self ): """simple docstring""" UpperCamelCase : Dict = VersatileDiffusionImageVariationPipeline.from_pretrained('''shi-labs/versatile-diffusion''' ) pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg''' ) UpperCamelCase : str = torch.manual_seed(0 ) UpperCamelCase : Union[str, Any] = pipe( image=__SCREAMING_SNAKE_CASE , generator=__SCREAMING_SNAKE_CASE , guidance_scale=7.5 , num_inference_steps=50 , output_type='''numpy''' , ).images UpperCamelCase : List[Any] = image[0, 253:256, 253:256, -1] assert image.shape == (1, 512, 512, 3) UpperCamelCase : Dict = np.array([0.0_441, 0.0_469, 0.0_507, 0.0_575, 0.0_632, 0.0_650, 0.0_865, 0.0_909, 0.0_945] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
643
1
import os # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_doctest_list.py __UpperCAmelCase : List[str] = "." if __name__ == "__main__": __UpperCAmelCase : Optional[int] = os.path.join(REPO_PATH, "utils/documentation_tests.txt") __UpperCAmelCase : str = [] __UpperCAmelCase : int = [] with open(doctest_file_path) as fp: for line in fp: __UpperCAmelCase : Optional[Any] = line.strip() __UpperCAmelCase : List[str] = os.path.join(REPO_PATH, line) if not (os.path.isfile(path) or os.path.isdir(path)): non_existent_paths.append(line) all_paths.append(path) if len(non_existent_paths) > 0: __UpperCAmelCase : List[Any] = "\n".join(non_existent_paths) raise ValueError(f'''`utils/documentation_tests.txt` contains non-existent paths:\n{non_existent_paths}''') if all_paths != sorted(all_paths): raise ValueError("Files in `utils/documentation_tests.txt` are not in alphabetical order.")
643
import copy from typing import Any, Dict, List, Optional, Union import numpy as np 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 __UpperCAmelCase : Dict = logging.get_logger(__name__) class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : Any = ["input_features"] def __init__( self , __SCREAMING_SNAKE_CASE=80 , __SCREAMING_SNAKE_CASE=16_000 , __SCREAMING_SNAKE_CASE=160 , __SCREAMING_SNAKE_CASE=30 , __SCREAMING_SNAKE_CASE=400 , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=False , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" super().__init__( feature_size=__SCREAMING_SNAKE_CASE , sampling_rate=__SCREAMING_SNAKE_CASE , padding_value=__SCREAMING_SNAKE_CASE , return_attention_mask=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) UpperCamelCase : List[str] = n_fft UpperCamelCase : Dict = hop_length UpperCamelCase : Dict = chunk_length UpperCamelCase : List[str] = chunk_length * sampling_rate UpperCamelCase : Dict = self.n_samples // hop_length UpperCamelCase : str = sampling_rate UpperCamelCase : Union[str, Any] = mel_filter_bank( num_frequency_bins=1 + n_fft // 2 , num_mel_filters=__SCREAMING_SNAKE_CASE , min_frequency=0.0 , max_frequency=8_000.0 , sampling_rate=__SCREAMING_SNAKE_CASE , norm='''slaney''' , mel_scale='''slaney''' , ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : List[str] = spectrogram( __SCREAMING_SNAKE_CASE , window_function(self.n_fft , '''hann''' ) , frame_length=self.n_fft , hop_length=self.hop_length , power=2.0 , mel_filters=self.mel_filters , log_mel='''log10''' , ) UpperCamelCase : int = log_spec[:, :-1] UpperCamelCase : int = np.maximum(__SCREAMING_SNAKE_CASE , log_spec.max() - 8.0 ) UpperCamelCase : Any = (log_spec + 4.0) / 4.0 return log_spec @staticmethod # Copied from transformers.models.wav2vec2.feature_extraction_wav2vec2.Wav2Vec2FeatureExtractor.zero_mean_unit_var_norm def _lowercase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = 0.0 ): """simple docstring""" if attention_mask is not None: UpperCamelCase : List[Any] = np.array(__SCREAMING_SNAKE_CASE , np.intaa ) UpperCamelCase : Optional[Any] = [] for vector, length in zip(__SCREAMING_SNAKE_CASE , attention_mask.sum(-1 ) ): UpperCamelCase : Optional[Any] = (vector - vector[:length].mean()) / np.sqrt(vector[:length].var() + 1e-7 ) if length < normed_slice.shape[0]: UpperCamelCase : Optional[int] = padding_value normed_input_values.append(__SCREAMING_SNAKE_CASE ) else: UpperCamelCase : Union[str, Any] = [(x - x.mean()) / np.sqrt(x.var() + 1e-7 ) for x in input_values] return normed_input_values def __call__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = True , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = "max_length" , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( f"""The model corresponding to this feature extractor: {self.__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 : Tuple = isinstance(__SCREAMING_SNAKE_CASE , np.ndarray ) and len(raw_speech.shape ) > 1 if is_batched_numpy and len(raw_speech.shape ) > 2: raise ValueError(f"""Only mono-channel audio is supported for input to {self}""" ) UpperCamelCase : Union[str, Any] = is_batched_numpy or ( isinstance(__SCREAMING_SNAKE_CASE , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: UpperCamelCase : List[Any] = [np.asarray([speech] , dtype=np.floataa ).T for speech in raw_speech] elif not is_batched and not isinstance(__SCREAMING_SNAKE_CASE , np.ndarray ): UpperCamelCase : int = np.asarray(__SCREAMING_SNAKE_CASE , dtype=np.floataa ) elif isinstance(__SCREAMING_SNAKE_CASE , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): UpperCamelCase : Union[str, Any] = raw_speech.astype(np.floataa ) # always return batch if not is_batched: UpperCamelCase : Optional[int] = [np.asarray([raw_speech] ).T] UpperCamelCase : Optional[int] = BatchFeature({'''input_features''': raw_speech} ) # convert into correct format for padding UpperCamelCase : Optional[Any] = self.pad( __SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE , max_length=max_length if max_length else self.n_samples , truncation=__SCREAMING_SNAKE_CASE , pad_to_multiple_of=__SCREAMING_SNAKE_CASE , return_attention_mask=return_attention_mask or do_normalize , ) # zero-mean and unit-variance normalization if do_normalize: UpperCamelCase : Optional[Any] = self.zero_mean_unit_var_norm( padded_inputs['''input_features'''] , attention_mask=padded_inputs['''attention_mask'''] , padding_value=self.padding_value , ) UpperCamelCase : List[str] = np.stack(padded_inputs['''input_features'''] , axis=0 ) # make sure list is in array format UpperCamelCase : Dict = padded_inputs.get('''input_features''' ).transpose(2 , 0 , 1 ) UpperCamelCase : Tuple = [self._np_extract_fbank_features(__SCREAMING_SNAKE_CASE ) for waveform in input_features[0]] if isinstance(input_features[0] , __SCREAMING_SNAKE_CASE ): UpperCamelCase : Optional[int] = [np.asarray(__SCREAMING_SNAKE_CASE , dtype=np.floataa ) for feature in input_features] else: UpperCamelCase : Dict = input_features if return_attention_mask: # rescale from sample (48000) to feature (3000) UpperCamelCase : Union[str, Any] = padded_inputs['''attention_mask'''][:, :: self.hop_length] if return_tensors is not None: UpperCamelCase : Dict = padded_inputs.convert_to_tensors(__SCREAMING_SNAKE_CASE ) return padded_inputs def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[Any] = copy.deepcopy(self.__dict__ ) UpperCamelCase : List[str] = self.__class__.__name__ if "mel_filters" in output: del output["mel_filters"] return output
643
1
from unittest.mock import Mock, patch from file_transfer.send_file import send_file @patch('''socket.socket''' ) @patch('''builtins.open''' ) def a ( SCREAMING_SNAKE_CASE_ : Tuple , SCREAMING_SNAKE_CASE_ : List[str] ): """simple docstring""" UpperCamelCase : int = Mock() UpperCamelCase : Any = conn, Mock() UpperCamelCase : List[Any] = iter([1, None] ) UpperCamelCase : Optional[Any] = lambda SCREAMING_SNAKE_CASE_ : next(SCREAMING_SNAKE_CASE_ ) # ===== invoke ===== send_file(filename='''mytext.txt''' , testing=SCREAMING_SNAKE_CASE_ ) # ===== ensurance ===== sock.assert_called_once() sock.return_value.bind.assert_called_once() sock.return_value.listen.assert_called_once() sock.return_value.accept.assert_called_once() conn.recv.assert_called_once() file.return_value.__enter__.assert_called_once() file.return_value.__enter__.return_value.read.assert_called() conn.send.assert_called_once() conn.close.assert_called_once() sock.return_value.shutdown.assert_called_once() sock.return_value.close.assert_called_once()
643
import json from typing import List, Optional, Tuple from tokenizers import normalizers from tokenizers.pre_tokenizers import BertPreTokenizer, PreTokenizer from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_roformer import RoFormerTokenizer from .tokenization_utils import JiebaPreTokenizer __UpperCAmelCase : Dict = logging.get_logger(__name__) __UpperCAmelCase : Optional[Any] = {"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"} __UpperCAmelCase : Dict = { "vocab_file": { "junnyu/roformer_chinese_small": "https://huggingface.co/junnyu/roformer_chinese_small/resolve/main/vocab.txt", "junnyu/roformer_chinese_base": "https://huggingface.co/junnyu/roformer_chinese_base/resolve/main/vocab.txt", "junnyu/roformer_chinese_char_small": ( "https://huggingface.co/junnyu/roformer_chinese_char_small/resolve/main/vocab.txt" ), "junnyu/roformer_chinese_char_base": ( "https://huggingface.co/junnyu/roformer_chinese_char_base/resolve/main/vocab.txt" ), "junnyu/roformer_small_discriminator": ( "https://huggingface.co/junnyu/roformer_small_discriminator/resolve/main/vocab.txt" ), "junnyu/roformer_small_generator": ( "https://huggingface.co/junnyu/roformer_small_generator/resolve/main/vocab.txt" ), } } __UpperCAmelCase : Tuple = { "junnyu/roformer_chinese_small": 1536, "junnyu/roformer_chinese_base": 1536, "junnyu/roformer_chinese_char_small": 512, "junnyu/roformer_chinese_char_base": 512, "junnyu/roformer_small_discriminator": 128, "junnyu/roformer_small_generator": 128, } __UpperCAmelCase : Any = { "junnyu/roformer_chinese_small": {"do_lower_case": True}, "junnyu/roformer_chinese_base": {"do_lower_case": True}, "junnyu/roformer_chinese_char_small": {"do_lower_case": True}, "junnyu/roformer_chinese_char_base": {"do_lower_case": True}, "junnyu/roformer_small_discriminator": {"do_lower_case": True}, "junnyu/roformer_small_generator": {"do_lower_case": True}, } class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : str = VOCAB_FILES_NAMES __UpperCamelCase : Optional[int] = PRETRAINED_VOCAB_FILES_MAP __UpperCamelCase : Union[str, Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCamelCase : int = PRETRAINED_INIT_CONFIGURATION __UpperCamelCase : Any = RoFormerTokenizer def __init__( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE="[UNK]" , __SCREAMING_SNAKE_CASE="[SEP]" , __SCREAMING_SNAKE_CASE="[PAD]" , __SCREAMING_SNAKE_CASE="[CLS]" , __SCREAMING_SNAKE_CASE="[MASK]" , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" super().__init__( __SCREAMING_SNAKE_CASE , tokenizer_file=__SCREAMING_SNAKE_CASE , do_lower_case=__SCREAMING_SNAKE_CASE , unk_token=__SCREAMING_SNAKE_CASE , sep_token=__SCREAMING_SNAKE_CASE , pad_token=__SCREAMING_SNAKE_CASE , cls_token=__SCREAMING_SNAKE_CASE , mask_token=__SCREAMING_SNAKE_CASE , tokenize_chinese_chars=__SCREAMING_SNAKE_CASE , strip_accents=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) UpperCamelCase : List[Any] = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( pre_tok_state.get('''lowercase''' , __SCREAMING_SNAKE_CASE ) != do_lower_case or pre_tok_state.get('''strip_accents''' , __SCREAMING_SNAKE_CASE ) != strip_accents ): UpperCamelCase : List[Any] = getattr(__SCREAMING_SNAKE_CASE , pre_tok_state.pop('''type''' ) ) UpperCamelCase : Optional[int] = do_lower_case UpperCamelCase : Optional[Any] = strip_accents UpperCamelCase : List[Any] = pre_tok_class(**__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[Any] = do_lower_case def __getstate__( self ): """simple docstring""" UpperCamelCase : Optional[int] = self.__dict__.copy() UpperCamelCase : Any = BertPreTokenizer() return state def __setstate__( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Any = d UpperCamelCase : List[str] = self.__dict__['''_tokenizer'''].get_vocab() UpperCamelCase : Any = PreTokenizer.custom(JiebaPreTokenizer(__SCREAMING_SNAKE_CASE ) ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None ): """simple docstring""" UpperCamelCase : Any = [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 _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ): """simple docstring""" UpperCamelCase : Dict = [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 ) * [0] + len(token_ids_a + sep ) * [1] def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ): """simple docstring""" UpperCamelCase : List[Any] = self._tokenizer.model.save(__SCREAMING_SNAKE_CASE , name=__SCREAMING_SNAKE_CASE ) return tuple(__SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=False , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" UpperCamelCase : Any = BertPreTokenizer() return super().save_pretrained(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE )
643
1
def a ( SCREAMING_SNAKE_CASE_ : float ): """simple docstring""" return 1_0 - x * x def a ( SCREAMING_SNAKE_CASE_ : float , SCREAMING_SNAKE_CASE_ : float ): """simple docstring""" if equation(SCREAMING_SNAKE_CASE_ ) * equation(SCREAMING_SNAKE_CASE_ ) >= 0: raise ValueError('''Wrong space!''' ) UpperCamelCase : List[Any] = a while (b - a) >= 0.01: # Find middle point UpperCamelCase : Any = (a + b) / 2 # Check if middle point is root if equation(SCREAMING_SNAKE_CASE_ ) == 0.0: break # Decide the side to repeat the steps if equation(SCREAMING_SNAKE_CASE_ ) * equation(SCREAMING_SNAKE_CASE_ ) < 0: UpperCamelCase : Optional[Any] = c else: UpperCamelCase : Tuple = c return c if __name__ == "__main__": import doctest doctest.testmod() print(bisection(-2, 5)) print(bisection(0, 6))
643
from __future__ import annotations def a ( SCREAMING_SNAKE_CASE_ : list[int] ): """simple docstring""" if len(SCREAMING_SNAKE_CASE_ ) == 0: return array UpperCamelCase , UpperCamelCase : Union[str, Any] = min(SCREAMING_SNAKE_CASE_ ), max(SCREAMING_SNAKE_CASE_ ) # Compute the variables UpperCamelCase : Union[str, Any] = _max - _min + 1 UpperCamelCase , UpperCamelCase : Optional[Any] = [0] * holes_range, [0] * holes_range # Make the sorting. for i in array: UpperCamelCase : Optional[int] = i - _min UpperCamelCase : Any = i holes_repeat[index] += 1 # Makes the array back by replacing the numbers. UpperCamelCase : str = 0 for i in range(SCREAMING_SNAKE_CASE_ ): while holes_repeat[i] > 0: UpperCamelCase : List[Any] = holes[i] index += 1 holes_repeat[i] -= 1 # Returns the sorted array. return array if __name__ == "__main__": import doctest doctest.testmod() __UpperCAmelCase : Any = input("Enter numbers separated by comma:\n") __UpperCAmelCase : int = [int(x) for x in user_input.split(",")] print(pigeon_sort(unsorted))
643
1
import json from typing import Dict, List, Optional, Tuple, Union from tokenizers import pre_tokenizers, processors from ...tokenization_utils_base import AddedToken, BatchEncoding, EncodedInput from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import PaddingStrategy, logging from .tokenization_led import LEDTokenizer __UpperCAmelCase : List[str] = logging.get_logger(__name__) __UpperCAmelCase : Optional[int] = {"vocab_file": "vocab.json", "merges_file": "merges.txt", "tokenizer_file": "tokenizer.json"} __UpperCAmelCase : Union[str, Any] = { "vocab_file": { "allenai/led-base-16384": "https://huggingface.co/allenai/led-base-16384/resolve/main/vocab.json", }, "merges_file": { "allenai/led-base-16384": "https://huggingface.co/allenai/led-base-16384/resolve/main/merges.txt", }, "tokenizer_file": { "allenai/led-base-16384": "https://huggingface.co/allenai/led-base-16384/resolve/main/tokenizer.json", }, } __UpperCAmelCase : List[Any] = { "allenai/led-base-16384": 16384, } class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : Optional[int] = VOCAB_FILES_NAMES __UpperCamelCase : str = PRETRAINED_VOCAB_FILES_MAP __UpperCamelCase : Optional[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCamelCase : Optional[int] = LEDTokenizer __UpperCamelCase : Optional[Any] = ["input_ids", "attention_mask"] def __init__( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE="replace" , __SCREAMING_SNAKE_CASE="<s>" , __SCREAMING_SNAKE_CASE="</s>" , __SCREAMING_SNAKE_CASE="</s>" , __SCREAMING_SNAKE_CASE="<s>" , __SCREAMING_SNAKE_CASE="<unk>" , __SCREAMING_SNAKE_CASE="<pad>" , __SCREAMING_SNAKE_CASE="<mask>" , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=True , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" super().__init__( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , tokenizer_file=__SCREAMING_SNAKE_CASE , errors=__SCREAMING_SNAKE_CASE , bos_token=__SCREAMING_SNAKE_CASE , eos_token=__SCREAMING_SNAKE_CASE , sep_token=__SCREAMING_SNAKE_CASE , cls_token=__SCREAMING_SNAKE_CASE , unk_token=__SCREAMING_SNAKE_CASE , pad_token=__SCREAMING_SNAKE_CASE , mask_token=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) UpperCamelCase : List[Any] = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get('''add_prefix_space''' , __SCREAMING_SNAKE_CASE ) != add_prefix_space: UpperCamelCase : Union[str, Any] = getattr(__SCREAMING_SNAKE_CASE , pre_tok_state.pop('''type''' ) ) UpperCamelCase : Dict = add_prefix_space UpperCamelCase : Dict = pre_tok_class(**__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = add_prefix_space # the pre_tokenizer is already updated in the GPT2TokenizerFast `__init__` UpperCamelCase : Union[str, Any] = '''post_processor''' UpperCamelCase : int = getattr(self.backend_tokenizer , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) if tokenizer_component_instance: UpperCamelCase : Any = json.loads(tokenizer_component_instance.__getstate__() ) # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class` if "sep" in state: UpperCamelCase : int = tuple(state['''sep'''] ) if "cls" in state: UpperCamelCase : int = tuple(state['''cls'''] ) UpperCamelCase : Union[str, Any] = False if state.get('''add_prefix_space''' , __SCREAMING_SNAKE_CASE ) != add_prefix_space: UpperCamelCase : List[Any] = add_prefix_space UpperCamelCase : List[Any] = True if state.get('''trim_offsets''' , __SCREAMING_SNAKE_CASE ) != trim_offsets: UpperCamelCase : Dict = trim_offsets UpperCamelCase : Any = True if changes_to_apply: UpperCamelCase : Optional[Any] = getattr(__SCREAMING_SNAKE_CASE , state.pop('''type''' ) ) UpperCamelCase : Any = component_class(**__SCREAMING_SNAKE_CASE ) setattr(self.backend_tokenizer , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) @property # Copied from transformers.models.bart.tokenization_bart_fast.BartTokenizerFast.mask_token with BART->LED def _lowercase ( self ): """simple docstring""" if self._mask_token is None: if self.verbose: logger.error('''Using mask_token, but it is not set yet.''' ) return None return str(self._mask_token ) @mask_token.setter def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Optional[int] = AddedToken(__SCREAMING_SNAKE_CASE , lstrip=__SCREAMING_SNAKE_CASE , rstrip=__SCREAMING_SNAKE_CASE ) if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else value UpperCamelCase : Dict = value def _lowercase ( self , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : int = kwargs.get('''is_split_into_words''' , __SCREAMING_SNAKE_CASE ) if is_split_into_words and not self.add_prefix_space: raise ValueError( f"""You need to instantiate {self.__class__.__name__} with add_prefix_space=True """ '''to use it with pretokenized inputs.''' ) return super()._batch_encode_plus(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def _lowercase ( self , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : List[str] = kwargs.get('''is_split_into_words''' , __SCREAMING_SNAKE_CASE ) if is_split_into_words and not self.add_prefix_space: raise ValueError( f"""You need to instantiate {self.__class__.__name__} with add_prefix_space=True """ '''to use it with pretokenized inputs.''' ) return super()._encode_plus(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ): """simple docstring""" UpperCamelCase : Union[str, Any] = self._tokenizer.model.save(__SCREAMING_SNAKE_CASE , name=__SCREAMING_SNAKE_CASE ) return tuple(__SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None ): """simple docstring""" UpperCamelCase : Optional[int] = [self.bos_token_id] + token_ids_a + [self.eos_token_id] if token_ids_a is None: return output return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id] def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ): """simple docstring""" UpperCamelCase : Tuple = [self.sep_token_id] UpperCamelCase : List[Any] = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = PaddingStrategy.DO_NOT_PAD , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , ): """simple docstring""" UpperCamelCase : str = super()._pad( encoded_inputs=__SCREAMING_SNAKE_CASE , max_length=__SCREAMING_SNAKE_CASE , padding_strategy=__SCREAMING_SNAKE_CASE , pad_to_multiple_of=__SCREAMING_SNAKE_CASE , return_attention_mask=__SCREAMING_SNAKE_CASE , ) # Load from model defaults if return_attention_mask is None: UpperCamelCase : Any = '''attention_mask''' in self.model_input_names if return_attention_mask and "global_attention_mask" in encoded_inputs: UpperCamelCase : int = encoded_inputs[self.model_input_names[0]] # `global_attention_mask` need to have the same length as other (sequential) inputs. UpperCamelCase : Dict = len(encoded_inputs['''global_attention_mask'''] ) != len(__SCREAMING_SNAKE_CASE ) if needs_to_be_padded: UpperCamelCase : Dict = len(__SCREAMING_SNAKE_CASE ) - len(encoded_inputs['''global_attention_mask'''] ) if self.padding_side == "right": # Use `-1` since `0` in `global_attention_mask` means `local attention` instead of `not to attend` UpperCamelCase : str = ( encoded_inputs['''global_attention_mask'''] + [-1] * difference ) elif self.padding_side == "left": UpperCamelCase : str = [-1] * difference + encoded_inputs[ '''global_attention_mask''' ] else: raise ValueError('''Invalid padding strategy:''' + str(self.padding_side ) ) return encoded_inputs
643
import json import os import shutil import warnings from argparse import ArgumentParser, Namespace from pathlib import Path from typing import List from ..utils import logging from . import BaseTransformersCLICommand try: from cookiecutter.main import cookiecutter __UpperCAmelCase : List[Any] = True except ImportError: __UpperCAmelCase : List[str] = False __UpperCAmelCase : Any = logging.get_logger(__name__) # pylint: disable=invalid-name def a ( SCREAMING_SNAKE_CASE_ : Namespace ): """simple docstring""" return AddNewModelCommand(args.testing , args.testing_file , path=args.path ) class UpperCAmelCase_ ( _a): '''simple docstring''' @staticmethod def _lowercase ( __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : List[Any] = parser.add_parser('''add-new-model''' ) add_new_model_parser.add_argument('''--testing''' , action='''store_true''' , help='''If in testing mode.''' ) add_new_model_parser.add_argument('''--testing_file''' , type=__SCREAMING_SNAKE_CASE , help='''Configuration file on which to run.''' ) add_new_model_parser.add_argument( '''--path''' , type=__SCREAMING_SNAKE_CASE , help='''Path to cookiecutter. Should only be used for testing purposes.''' ) add_new_model_parser.set_defaults(func=__SCREAMING_SNAKE_CASE ) def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , *__SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Tuple = testing UpperCamelCase : Any = testing_file UpperCamelCase : Dict = path def _lowercase ( self ): """simple docstring""" warnings.warn( '''The command `transformers-cli add-new-model` is deprecated and will be removed in v5 of Transformers. ''' '''It is not actively maintained anymore, so might give a result that won\'t pass all tests and quality ''' '''checks, you should use `transformers-cli add-new-model-like` instead.''' ) if not _has_cookiecutter: raise ImportError( '''Model creation dependencies are required to use the `add_new_model` command. Install them by running ''' '''the following at the root of your `transformers` clone:\n\n\t$ pip install -e .[modelcreation]\n''' ) # Ensure that there is no other `cookiecutter-template-xxx` directory in the current working directory UpperCamelCase : List[str] = [directory for directory in os.listdir() if '''cookiecutter-template-''' == directory[:22]] if len(__SCREAMING_SNAKE_CASE ) > 0: raise ValueError( '''Several directories starting with `cookiecutter-template-` in current working directory. ''' '''Please clean your directory by removing all folders starting with `cookiecutter-template-` or ''' '''change your working directory.''' ) UpperCamelCase : Dict = ( Path(__SCREAMING_SNAKE_CASE ).parent.parent.parent.parent if self._path is None else Path(self._path ).parent.parent ) UpperCamelCase : List[Any] = path_to_transformer_root / '''templates''' / '''adding_a_new_model''' # Execute cookiecutter if not self._testing: cookiecutter(str(__SCREAMING_SNAKE_CASE ) ) else: with open(self._testing_file , '''r''' ) as configuration_file: UpperCamelCase : Tuple = json.load(__SCREAMING_SNAKE_CASE ) cookiecutter( str(path_to_cookiecutter if self._path is None else self._path ) , no_input=__SCREAMING_SNAKE_CASE , extra_context=__SCREAMING_SNAKE_CASE , ) UpperCamelCase : Dict = [directory for directory in os.listdir() if '''cookiecutter-template-''' in directory[:22]][0] # Retrieve configuration with open(directory + '''/configuration.json''' , '''r''' ) as configuration_file: UpperCamelCase : Tuple = json.load(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Dict = configuration['''lowercase_modelname'''] UpperCamelCase : int = configuration['''generate_tensorflow_pytorch_and_flax'''] os.remove(f"""{directory}/configuration.json""" ) UpperCamelCase : str = '''PyTorch''' in generate_tensorflow_pytorch_and_flax UpperCamelCase : Any = '''TensorFlow''' in generate_tensorflow_pytorch_and_flax UpperCamelCase : Union[str, Any] = '''Flax''' in generate_tensorflow_pytorch_and_flax UpperCamelCase : Optional[Any] = f"""{path_to_transformer_root}/src/transformers/models/{lowercase_model_name}""" os.makedirs(__SCREAMING_SNAKE_CASE , exist_ok=__SCREAMING_SNAKE_CASE ) os.makedirs(f"""{path_to_transformer_root}/tests/models/{lowercase_model_name}""" , exist_ok=__SCREAMING_SNAKE_CASE ) # Tests require submodules as they have parent imports with open(f"""{path_to_transformer_root}/tests/models/{lowercase_model_name}/__init__.py""" , '''w''' ): pass shutil.move( f"""{directory}/__init__.py""" , f"""{model_dir}/__init__.py""" , ) shutil.move( f"""{directory}/configuration_{lowercase_model_name}.py""" , f"""{model_dir}/configuration_{lowercase_model_name}.py""" , ) def remove_copy_lines(__SCREAMING_SNAKE_CASE ): with open(__SCREAMING_SNAKE_CASE , '''r''' ) as f: UpperCamelCase : Any = f.readlines() with open(__SCREAMING_SNAKE_CASE , '''w''' ) as f: for line in lines: if "# Copied from transformers." not in line: f.write(__SCREAMING_SNAKE_CASE ) if output_pytorch: if not self._testing: remove_copy_lines(f"""{directory}/modeling_{lowercase_model_name}.py""" ) shutil.move( f"""{directory}/modeling_{lowercase_model_name}.py""" , f"""{model_dir}/modeling_{lowercase_model_name}.py""" , ) shutil.move( f"""{directory}/test_modeling_{lowercase_model_name}.py""" , f"""{path_to_transformer_root}/tests/models/{lowercase_model_name}/test_modeling_{lowercase_model_name}.py""" , ) else: os.remove(f"""{directory}/modeling_{lowercase_model_name}.py""" ) os.remove(f"""{directory}/test_modeling_{lowercase_model_name}.py""" ) if output_tensorflow: if not self._testing: remove_copy_lines(f"""{directory}/modeling_tf_{lowercase_model_name}.py""" ) shutil.move( f"""{directory}/modeling_tf_{lowercase_model_name}.py""" , f"""{model_dir}/modeling_tf_{lowercase_model_name}.py""" , ) shutil.move( f"""{directory}/test_modeling_tf_{lowercase_model_name}.py""" , f"""{path_to_transformer_root}/tests/models/{lowercase_model_name}/test_modeling_tf_{lowercase_model_name}.py""" , ) else: os.remove(f"""{directory}/modeling_tf_{lowercase_model_name}.py""" ) os.remove(f"""{directory}/test_modeling_tf_{lowercase_model_name}.py""" ) if output_flax: if not self._testing: remove_copy_lines(f"""{directory}/modeling_flax_{lowercase_model_name}.py""" ) shutil.move( f"""{directory}/modeling_flax_{lowercase_model_name}.py""" , f"""{model_dir}/modeling_flax_{lowercase_model_name}.py""" , ) shutil.move( f"""{directory}/test_modeling_flax_{lowercase_model_name}.py""" , f"""{path_to_transformer_root}/tests/models/{lowercase_model_name}/test_modeling_flax_{lowercase_model_name}.py""" , ) else: os.remove(f"""{directory}/modeling_flax_{lowercase_model_name}.py""" ) os.remove(f"""{directory}/test_modeling_flax_{lowercase_model_name}.py""" ) shutil.move( f"""{directory}/{lowercase_model_name}.md""" , f"""{path_to_transformer_root}/docs/source/en/model_doc/{lowercase_model_name}.md""" , ) shutil.move( f"""{directory}/tokenization_{lowercase_model_name}.py""" , f"""{model_dir}/tokenization_{lowercase_model_name}.py""" , ) shutil.move( f"""{directory}/tokenization_fast_{lowercase_model_name}.py""" , f"""{model_dir}/tokenization_{lowercase_model_name}_fast.py""" , ) from os import fdopen, remove from shutil import copymode, move from tempfile import mkstemp def replace(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): # Create temp file UpperCamelCase , UpperCamelCase : Optional[Any] = mkstemp() UpperCamelCase : Tuple = False with fdopen(__SCREAMING_SNAKE_CASE , '''w''' ) as new_file: with open(__SCREAMING_SNAKE_CASE ) as old_file: for line in old_file: new_file.write(__SCREAMING_SNAKE_CASE ) if line_to_copy_below in line: UpperCamelCase : Optional[int] = True for line_to_copy in lines_to_copy: new_file.write(__SCREAMING_SNAKE_CASE ) if not line_found: raise ValueError(f"""Line {line_to_copy_below} was not found in file.""" ) # Copy the file permissions from the old file to the new file copymode(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # Remove original file remove(__SCREAMING_SNAKE_CASE ) # Move new file move(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def skip_units(__SCREAMING_SNAKE_CASE ): return ( ("generating PyTorch" in line and not output_pytorch) or ("generating TensorFlow" in line and not output_tensorflow) or ("generating Flax" in line and not output_flax) ) def replace_in_files(__SCREAMING_SNAKE_CASE ): with open(__SCREAMING_SNAKE_CASE ) as datafile: UpperCamelCase : int = [] UpperCamelCase : Dict = False UpperCamelCase : List[Any] = False for line in datafile: if "# To replace in: " in line and "##" not in line: UpperCamelCase : Dict = line.split('''"''' )[1] UpperCamelCase : int = skip_units(__SCREAMING_SNAKE_CASE ) elif "# Below: " in line and "##" not in line: UpperCamelCase : Dict = line.split('''"''' )[1] UpperCamelCase : List[str] = skip_units(__SCREAMING_SNAKE_CASE ) elif "# End." in line and "##" not in line: if not skip_file and not skip_snippet: replace(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) UpperCamelCase : Dict = [] elif "# Replace with" in line and "##" not in line: UpperCamelCase : Tuple = [] elif "##" not in line: lines_to_copy.append(__SCREAMING_SNAKE_CASE ) remove(__SCREAMING_SNAKE_CASE ) replace_in_files(f"""{directory}/to_replace_{lowercase_model_name}.py""" ) os.rmdir(__SCREAMING_SNAKE_CASE )
643
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __UpperCAmelCase : Optional[int] = {"configuration_sew": ["SEW_PRETRAINED_CONFIG_ARCHIVE_MAP", "SEWConfig"]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase : Optional[int] = [ "SEW_PRETRAINED_MODEL_ARCHIVE_LIST", "SEWForCTC", "SEWForSequenceClassification", "SEWModel", "SEWPreTrainedModel", ] if TYPE_CHECKING: from .configuration_sew import SEW_PRETRAINED_CONFIG_ARCHIVE_MAP, SEWConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_sew import ( SEW_PRETRAINED_MODEL_ARCHIVE_LIST, SEWForCTC, SEWForSequenceClassification, SEWModel, SEWPreTrainedModel, ) else: import sys __UpperCAmelCase : Union[str, Any] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
643
from pathlib import Path import cva import numpy as np from matplotlib import pyplot as plt def a ( SCREAMING_SNAKE_CASE_ : np.ndarray , SCREAMING_SNAKE_CASE_ : np.ndarray , SCREAMING_SNAKE_CASE_ : np.ndarray , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int ): """simple docstring""" UpperCamelCase : str = cva.getAffineTransform(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) return cva.warpAffine(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , (rows, cols) ) if __name__ == "__main__": # read original image __UpperCAmelCase : Tuple = cva.imread( str(Path(__file__).resolve().parent.parent / "image_data" / "lena.jpg") ) # turn image in gray scale value __UpperCAmelCase : int = cva.cvtColor(image, cva.COLOR_BGR2GRAY) # get image shape __UpperCAmelCase , __UpperCAmelCase : Tuple = gray_img.shape # set different points to rotate image __UpperCAmelCase : Optional[int] = np.array([[50, 50], [200, 50], [50, 200]], np.floataa) __UpperCAmelCase : Optional[int] = np.array([[10, 100], [200, 50], [100, 250]], np.floataa) __UpperCAmelCase : Any = np.array([[50, 50], [150, 50], [120, 200]], np.floataa) __UpperCAmelCase : int = np.array([[10, 100], [80, 50], [180, 250]], np.floataa) # add all rotated images in a list __UpperCAmelCase : Union[str, Any] = [ gray_img, get_rotation(gray_img, ptsa, ptsa, img_rows, img_cols), get_rotation(gray_img, ptsa, ptsa, img_rows, img_cols), get_rotation(gray_img, ptsa, ptsa, img_rows, img_cols), ] # plot different image rotations __UpperCAmelCase : List[str] = plt.figure(1) __UpperCAmelCase : Dict = ["Original", "Rotation 1", "Rotation 2", "Rotation 3"] for i, image in enumerate(images): plt.subplot(2, 2, i + 1), plt.imshow(image, "gray") plt.title(titles[i]) plt.axis("off") plt.subplots_adjust(left=0.0, bottom=0.05, right=1.0, top=0.95) plt.show()
643
1
from sympy import diff, lambdify, symbols from sympy.functions import * # noqa: F403 def a ( SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : complex , SCREAMING_SNAKE_CASE_ : str = "x" , SCREAMING_SNAKE_CASE_ : float = 1_0**-1_0 , SCREAMING_SNAKE_CASE_ : int = 1 , ): """simple docstring""" UpperCamelCase : Any = symbols(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Any = lambdify(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) UpperCamelCase : str = lambdify(SCREAMING_SNAKE_CASE_ , diff(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) UpperCamelCase : Dict = starting_point while True: if diff_function(SCREAMING_SNAKE_CASE_ ) != 0: UpperCamelCase : Optional[int] = prev_guess - multiplicity * func(SCREAMING_SNAKE_CASE_ ) / diff_function( SCREAMING_SNAKE_CASE_ ) else: raise ZeroDivisionError('''Could not find root''' ) from None # Precision is checked by comparing the difference of consecutive guesses if abs(next_guess - prev_guess ) < precision: return next_guess UpperCamelCase : Optional[Any] = next_guess # 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 # Find fourth Root of 5 print(f'''The root of x**4 - 5 = 0 is {newton_raphson('x**4 -5', 0.4 +5j)}''') # Find value of e print( "The root of log(y) - 1 = 0 is ", f'''{newton_raphson('log(y) - 1', 2, variable='y')}''', ) # Exponential Roots print( "The root of exp(x) - 1 = 0 is", f'''{newton_raphson('exp(x) - 1', 10, precision=0.005)}''', ) # Find root of cos(x) print(f'''The root of cos(x) = 0 is {newton_raphson('cos(x)', 0)}''')
643
import copy from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ..auto import CONFIG_MAPPING __UpperCAmelCase : Optional[Any] = logging.get_logger(__name__) __UpperCAmelCase : List[str] = { "microsoft/conditional-detr-resnet-50": ( "https://huggingface.co/microsoft/conditional-detr-resnet-50/resolve/main/config.json" ), } class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : Optional[int] = "conditional_detr" __UpperCamelCase : Optional[Any] = ["past_key_values"] __UpperCamelCase : Union[str, Any] = { "hidden_size": "d_model", "num_attention_heads": "encoder_attention_heads", } def __init__( self , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=300 , __SCREAMING_SNAKE_CASE=6 , __SCREAMING_SNAKE_CASE=2_048 , __SCREAMING_SNAKE_CASE=8 , __SCREAMING_SNAKE_CASE=6 , __SCREAMING_SNAKE_CASE=2_048 , __SCREAMING_SNAKE_CASE=8 , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE="relu" , __SCREAMING_SNAKE_CASE=256 , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=0.02 , __SCREAMING_SNAKE_CASE=1.0 , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE="sine" , __SCREAMING_SNAKE_CASE="resnet50" , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=5 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=1 , __SCREAMING_SNAKE_CASE=1 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=5 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=0.25 , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" if backbone_config is not None and use_timm_backbone: raise ValueError('''You can\'t specify both `backbone_config` and `use_timm_backbone`.''' ) if not use_timm_backbone: if backbone_config is None: logger.info('''`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.''' ) UpperCamelCase : str = CONFIG_MAPPING['''resnet'''](out_features=['''stage4'''] ) elif isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): UpperCamelCase : Tuple = backbone_config.get('''model_type''' ) UpperCamelCase : Optional[Any] = CONFIG_MAPPING[backbone_model_type] UpperCamelCase : Any = config_class.from_dict(__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = use_timm_backbone UpperCamelCase : int = backbone_config UpperCamelCase : Any = num_channels UpperCamelCase : Optional[Any] = num_queries UpperCamelCase : Tuple = d_model UpperCamelCase : Optional[Any] = encoder_ffn_dim UpperCamelCase : Optional[int] = encoder_layers UpperCamelCase : Union[str, Any] = encoder_attention_heads UpperCamelCase : Optional[Any] = decoder_ffn_dim UpperCamelCase : Optional[int] = decoder_layers UpperCamelCase : Optional[Any] = decoder_attention_heads UpperCamelCase : Any = dropout UpperCamelCase : List[Any] = attention_dropout UpperCamelCase : List[Any] = activation_dropout UpperCamelCase : List[str] = activation_function UpperCamelCase : Optional[int] = init_std UpperCamelCase : Optional[Any] = init_xavier_std UpperCamelCase : Union[str, Any] = encoder_layerdrop UpperCamelCase : Optional[Any] = decoder_layerdrop UpperCamelCase : Tuple = encoder_layers UpperCamelCase : Optional[Any] = auxiliary_loss UpperCamelCase : Union[str, Any] = position_embedding_type UpperCamelCase : Optional[int] = backbone UpperCamelCase : Dict = use_pretrained_backbone UpperCamelCase : Tuple = dilation # Hungarian matcher UpperCamelCase : Union[str, Any] = class_cost UpperCamelCase : List[Any] = bbox_cost UpperCamelCase : Optional[Any] = giou_cost # Loss coefficients UpperCamelCase : Optional[Any] = mask_loss_coefficient UpperCamelCase : Optional[int] = dice_loss_coefficient UpperCamelCase : Optional[Any] = cls_loss_coefficient UpperCamelCase : Optional[int] = bbox_loss_coefficient UpperCamelCase : Optional[int] = giou_loss_coefficient UpperCamelCase : Optional[int] = focal_alpha super().__init__(is_encoder_decoder=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) @property def _lowercase ( self ): """simple docstring""" return self.encoder_attention_heads @property def _lowercase ( self ): """simple docstring""" return self.d_model def _lowercase ( self ): """simple docstring""" UpperCamelCase : Union[str, Any] = copy.deepcopy(self.__dict__ ) if self.backbone_config is not None: UpperCamelCase : List[Any] = self.backbone_config.to_dict() UpperCamelCase : List[Any] = self.__class__.model_type return output class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : Dict = version.parse("1.11") @property def _lowercase ( self ): """simple docstring""" return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ('''pixel_mask''', {0: '''batch'''}), ] ) @property def _lowercase ( self ): """simple docstring""" return 1e-5 @property def _lowercase ( self ): """simple docstring""" return 12
643
1
from ...configuration_utils import PretrainedConfig from ...utils import logging __UpperCAmelCase : Union[str, Any] = logging.get_logger(__name__) __UpperCAmelCase : Tuple = { # See all MEGATRON_BERT models at https://huggingface.co/models?filter=bert } class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : List[str] = "megatron-bert" def __init__( self , __SCREAMING_SNAKE_CASE=29_056 , __SCREAMING_SNAKE_CASE=1_024 , __SCREAMING_SNAKE_CASE=24 , __SCREAMING_SNAKE_CASE=16 , __SCREAMING_SNAKE_CASE=4_096 , __SCREAMING_SNAKE_CASE="gelu" , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=512 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=0.02 , __SCREAMING_SNAKE_CASE=1e-12 , __SCREAMING_SNAKE_CASE=0 , __SCREAMING_SNAKE_CASE="absolute" , __SCREAMING_SNAKE_CASE=True , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" super().__init__(pad_token_id=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = vocab_size UpperCamelCase : Tuple = hidden_size UpperCamelCase : Optional[int] = num_hidden_layers UpperCamelCase : Tuple = num_attention_heads UpperCamelCase : Any = hidden_act UpperCamelCase : Tuple = intermediate_size UpperCamelCase : Tuple = hidden_dropout_prob UpperCamelCase : Union[str, Any] = attention_probs_dropout_prob UpperCamelCase : Optional[Any] = max_position_embeddings UpperCamelCase : List[Any] = type_vocab_size UpperCamelCase : List[Any] = initializer_range UpperCamelCase : Tuple = layer_norm_eps UpperCamelCase : Optional[Any] = position_embedding_type UpperCamelCase : Tuple = use_cache
643
import requests from bsa import BeautifulSoup def a ( SCREAMING_SNAKE_CASE_ : str = "AAPL" ): """simple docstring""" UpperCamelCase : Dict = F"""https://in.finance.yahoo.com/quote/{symbol}?s={symbol}""" UpperCamelCase : Any = BeautifulSoup(requests.get(SCREAMING_SNAKE_CASE_ ).text , '''html.parser''' ) UpperCamelCase : Dict = '''My(6px) Pos(r) smartphone_Mt(6px)''' return soup.find('''div''' , class_=class_ ).find('''span''' ).text if __name__ == "__main__": for symbol in "AAPL AMZN IBM GOOG MSFT ORCL".split(): print(f'''Current {symbol:<4} stock price is {stock_price(symbol):>8}''')
643
1
import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging __UpperCAmelCase : List[Any] = logging.get_logger(__name__) __UpperCAmelCase : Any = {"vocab_file": "spiece.model"} __UpperCAmelCase : str = { "vocab_file": { "bert_for_seq_generation": ( "https://huggingface.co/google/bert_for_seq_generation_L-24_bbc_encoder/resolve/main/spiece.model" ), } } __UpperCAmelCase : Optional[Any] = {"bert_for_seq_generation": 512} class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : int = VOCAB_FILES_NAMES __UpperCamelCase : Union[str, Any] = PRETRAINED_VOCAB_FILES_MAP __UpperCamelCase : int = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCamelCase : List[int] = [] __UpperCamelCase : Any = ["input_ids", "attention_mask"] def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE="<s>" , __SCREAMING_SNAKE_CASE="</s>" , __SCREAMING_SNAKE_CASE="<unk>" , __SCREAMING_SNAKE_CASE="<pad>" , __SCREAMING_SNAKE_CASE="<::::>" , __SCREAMING_SNAKE_CASE = None , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" UpperCamelCase : Any = {} if sp_model_kwargs is None else sp_model_kwargs # Add extra_ids to the special token list super().__init__( bos_token=__SCREAMING_SNAKE_CASE , eos_token=__SCREAMING_SNAKE_CASE , unk_token=__SCREAMING_SNAKE_CASE , pad_token=__SCREAMING_SNAKE_CASE , sep_token=__SCREAMING_SNAKE_CASE , sp_model_kwargs=self.sp_model_kwargs , **__SCREAMING_SNAKE_CASE , ) UpperCamelCase : str = vocab_file UpperCamelCase : List[Any] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(__SCREAMING_SNAKE_CASE ) @property def _lowercase ( self ): """simple docstring""" return self.sp_model.get_piece_size() def _lowercase ( self ): """simple docstring""" UpperCamelCase : Dict = {self.convert_ids_to_tokens(__SCREAMING_SNAKE_CASE ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self ): """simple docstring""" UpperCamelCase : int = self.__dict__.copy() UpperCamelCase : Optional[Any] = None return state def __setstate__( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : List[Any] = d # for backward compatibility if not hasattr(self , '''sp_model_kwargs''' ): UpperCamelCase : Union[str, Any] = {} UpperCamelCase : List[str] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" return self.sp_model.encode(__SCREAMING_SNAKE_CASE , out_type=__SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" return self.sp_model.piece_to_id(__SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Union[str, Any] = self.sp_model.IdToPiece(__SCREAMING_SNAKE_CASE ) return token def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Union[str, Any] = [] UpperCamelCase : Any = '''''' for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: out_string += self.sp_model.decode(__SCREAMING_SNAKE_CASE ) + token UpperCamelCase : List[str] = [] else: current_sub_tokens.append(__SCREAMING_SNAKE_CASE ) out_string += self.sp_model.decode(__SCREAMING_SNAKE_CASE ) return out_string.strip() def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ): """simple docstring""" if not os.path.isdir(__SCREAMING_SNAKE_CASE ): logger.error(f"""Vocabulary path ({save_directory}) should be a directory""" ) return UpperCamelCase : int = os.path.join( __SCREAMING_SNAKE_CASE , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(__SCREAMING_SNAKE_CASE ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , __SCREAMING_SNAKE_CASE ) elif not os.path.isfile(self.vocab_file ): with open(__SCREAMING_SNAKE_CASE , '''wb''' ) as fi: UpperCamelCase : Optional[int] = self.sp_model.serialized_model_proto() fi.write(__SCREAMING_SNAKE_CASE ) return (out_vocab_file,)
643
def a ( SCREAMING_SNAKE_CASE_ : int ): """simple docstring""" if number > 0: raise ValueError('''input must be a negative integer''' ) UpperCamelCase : List[str] = len(bin(SCREAMING_SNAKE_CASE_ )[3:] ) UpperCamelCase : List[str] = bin(abs(SCREAMING_SNAKE_CASE_ ) - (1 << binary_number_length) )[3:] UpperCamelCase : Dict = ( ( '''1''' + '''0''' * (binary_number_length - len(SCREAMING_SNAKE_CASE_ )) + twos_complement_number ) if number < 0 else '''0''' ) return "0b" + twos_complement_number if __name__ == "__main__": import doctest doctest.testmod()
643
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tokenizers_available, is_torch_available, ) __UpperCAmelCase : List[Any] = {} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase : Union[str, Any] = ["NllbTokenizer"] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase : Union[str, Any] = ["NllbTokenizerFast"] if TYPE_CHECKING: try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_nllb import NllbTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_nllb_fast import NllbTokenizerFast else: import sys __UpperCAmelCase : List[str] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
643
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging __UpperCAmelCase : str = logging.get_logger(__name__) __UpperCAmelCase : Dict = { "hustvl/yolos-small": "https://huggingface.co/hustvl/yolos-small/resolve/main/config.json", # See all YOLOS models at https://huggingface.co/models?filter=yolos } class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : Optional[int] = "yolos" def __init__( self , __SCREAMING_SNAKE_CASE=768 , __SCREAMING_SNAKE_CASE=12 , __SCREAMING_SNAKE_CASE=12 , __SCREAMING_SNAKE_CASE=3_072 , __SCREAMING_SNAKE_CASE="gelu" , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=0.02 , __SCREAMING_SNAKE_CASE=1e-12 , __SCREAMING_SNAKE_CASE=[512, 864] , __SCREAMING_SNAKE_CASE=16 , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=100 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=1 , __SCREAMING_SNAKE_CASE=5 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=5 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=0.1 , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" super().__init__(**__SCREAMING_SNAKE_CASE ) UpperCamelCase : Tuple = hidden_size UpperCamelCase : List[Any] = num_hidden_layers UpperCamelCase : int = num_attention_heads UpperCamelCase : Dict = intermediate_size UpperCamelCase : Dict = hidden_act UpperCamelCase : int = hidden_dropout_prob UpperCamelCase : Any = attention_probs_dropout_prob UpperCamelCase : Optional[Any] = initializer_range UpperCamelCase : List[Any] = layer_norm_eps UpperCamelCase : int = image_size UpperCamelCase : Any = patch_size UpperCamelCase : str = num_channels UpperCamelCase : str = qkv_bias UpperCamelCase : Tuple = num_detection_tokens UpperCamelCase : List[Any] = use_mid_position_embeddings UpperCamelCase : Dict = auxiliary_loss # Hungarian matcher UpperCamelCase : Optional[Any] = class_cost UpperCamelCase : Union[str, Any] = bbox_cost UpperCamelCase : Any = giou_cost # Loss coefficients UpperCamelCase : List[Any] = bbox_loss_coefficient UpperCamelCase : Union[str, Any] = giou_loss_coefficient UpperCamelCase : Dict = eos_coefficient class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : List[str] = version.parse("1.11") @property def _lowercase ( self ): """simple docstring""" return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ] ) @property def _lowercase ( self ): """simple docstring""" return 1e-4 @property def _lowercase ( self ): """simple docstring""" return 12
643
1
import argparse import torch from huggingface_hub import hf_hub_download from transformers import AutoTokenizer, RobertaPreLayerNormConfig, RobertaPreLayerNormForMaskedLM from transformers.utils import logging logging.set_verbosity_info() __UpperCAmelCase : Dict = logging.get_logger(__name__) def a ( SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : str ): """simple docstring""" UpperCamelCase : Union[str, Any] = RobertaPreLayerNormConfig.from_pretrained( SCREAMING_SNAKE_CASE_ , architectures=['''RobertaPreLayerNormForMaskedLM'''] ) # convert state_dict UpperCamelCase : int = torch.load(hf_hub_download(repo_id=SCREAMING_SNAKE_CASE_ , filename='''pytorch_model.bin''' ) ) UpperCamelCase : Union[str, Any] = {} for tensor_key, tensor_value in original_state_dict.items(): # The transformer implementation gives the model a unique name, rather than overwiriting 'roberta' if tensor_key.startswith('''roberta.''' ): UpperCamelCase : Tuple = '''roberta_prelayernorm.''' + tensor_key[len('''roberta.''' ) :] # The original implementation contains weights which are not used, remove them from the state_dict if tensor_key.endswith('''.self.LayerNorm.weight''' ) or tensor_key.endswith('''.self.LayerNorm.bias''' ): continue UpperCamelCase : Union[str, Any] = tensor_value UpperCamelCase : Tuple = RobertaPreLayerNormForMaskedLM.from_pretrained( pretrained_model_name_or_path=SCREAMING_SNAKE_CASE_ , config=SCREAMING_SNAKE_CASE_ , state_dict=SCREAMING_SNAKE_CASE_ ) model.save_pretrained(SCREAMING_SNAKE_CASE_ ) # convert tokenizer UpperCamelCase : Optional[Any] = AutoTokenizer.from_pretrained(SCREAMING_SNAKE_CASE_ ) tokenizer.save_pretrained(SCREAMING_SNAKE_CASE_ ) if __name__ == "__main__": __UpperCAmelCase : str = argparse.ArgumentParser() # Required parameters parser.add_argument( "--checkpoint-repo", default=None, type=str, required=True, help="Path the official PyTorch dump, e.g. 'andreasmadsen/efficient_mlm_m0.40'.", ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, required=True, help="Path to the output PyTorch model." ) __UpperCAmelCase : Union[str, Any] = parser.parse_args() convert_roberta_prelayernorm_checkpoint_to_pytorch(args.checkpoint_repo, args.pytorch_dump_folder_path)
643
def a ( SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int ): """simple docstring""" if a < 0 or b < 0: raise ValueError('''the value of both inputs must be positive''' ) UpperCamelCase : int = str(bin(SCREAMING_SNAKE_CASE_ ) )[2:] # remove the leading "0b" UpperCamelCase : List[str] = str(bin(SCREAMING_SNAKE_CASE_ ) )[2:] UpperCamelCase : Tuple = max(len(SCREAMING_SNAKE_CASE_ ) , len(SCREAMING_SNAKE_CASE_ ) ) return "0b" + "".join( str(int('''1''' in (char_a, char_b) ) ) for char_a, char_b in zip(a_binary.zfill(SCREAMING_SNAKE_CASE_ ) , b_binary.zfill(SCREAMING_SNAKE_CASE_ ) ) ) if __name__ == "__main__": import doctest doctest.testmod()
643
1
import unittest from transformers import AutoConfig, AutoTokenizer, BertConfig, TensorType, is_flax_available from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, require_flax, slow if is_flax_available(): import jax from transformers.models.auto.modeling_flax_auto import FlaxAutoModel from transformers.models.bert.modeling_flax_bert import FlaxBertModel from transformers.models.roberta.modeling_flax_roberta import FlaxRobertaModel @require_flax class UpperCAmelCase_ ( unittest.TestCase): '''simple docstring''' @slow def _lowercase ( self ): """simple docstring""" for model_name in ["bert-base-cased", "bert-large-uncased"]: with self.subTest(__SCREAMING_SNAKE_CASE ): UpperCamelCase : Tuple = AutoConfig.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsNotNone(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = FlaxAutoModel.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsNotNone(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) @slow def _lowercase ( self ): """simple docstring""" for model_name in ["roberta-base", "roberta-large"]: with self.subTest(__SCREAMING_SNAKE_CASE ): UpperCamelCase : Any = AutoConfig.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsNotNone(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = FlaxAutoModel.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsNotNone(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) @slow def _lowercase ( self ): """simple docstring""" for model_name in ["bert-base-cased", "bert-large-uncased"]: UpperCamelCase : Union[str, Any] = AutoTokenizer.from_pretrained(__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[Any] = FlaxBertModel.from_pretrained(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = tokenizer('''Do you support jax jitted function?''' , return_tensors=TensorType.JAX ) @jax.jit def eval(**__SCREAMING_SNAKE_CASE ): return model(**__SCREAMING_SNAKE_CASE ) eval(**__SCREAMING_SNAKE_CASE ).block_until_ready() @slow def _lowercase ( self ): """simple docstring""" for model_name in ["roberta-base", "roberta-large"]: UpperCamelCase : Any = AutoTokenizer.from_pretrained(__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = FlaxRobertaModel.from_pretrained(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = tokenizer('''Do you support jax jitted function?''' , return_tensors=TensorType.JAX ) @jax.jit def eval(**__SCREAMING_SNAKE_CASE ): return model(**__SCREAMING_SNAKE_CASE ) eval(**__SCREAMING_SNAKE_CASE ).block_until_ready() def _lowercase ( self ): """simple docstring""" with self.assertRaisesRegex( __SCREAMING_SNAKE_CASE , '''bert-base is not a local folder and is not a valid model identifier''' ): UpperCamelCase : str = FlaxAutoModel.from_pretrained('''bert-base''' ) def _lowercase ( self ): """simple docstring""" with self.assertRaisesRegex( __SCREAMING_SNAKE_CASE , R'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)''' ): UpperCamelCase : Any = FlaxAutoModel.from_pretrained(__SCREAMING_SNAKE_CASE , revision='''aaaaaa''' ) def _lowercase ( self ): """simple docstring""" with self.assertRaisesRegex( __SCREAMING_SNAKE_CASE , '''hf-internal-testing/config-no-model does not appear to have a file named flax_model.msgpack''' , ): UpperCamelCase : Optional[int] = FlaxAutoModel.from_pretrained('''hf-internal-testing/config-no-model''' ) def _lowercase ( self ): """simple docstring""" with self.assertRaisesRegex(__SCREAMING_SNAKE_CASE , '''Use `from_pt=True` to load this model''' ): UpperCamelCase : str = FlaxAutoModel.from_pretrained('''hf-internal-testing/tiny-bert-pt-only''' )
643
from itertools import product from cva import COLOR_BGR2GRAY, cvtColor, imread, imshow, waitKey from numpy import dot, exp, mgrid, pi, ravel, square, uinta, zeros def a ( SCREAMING_SNAKE_CASE_ : List[Any] , SCREAMING_SNAKE_CASE_ : List[str] ): """simple docstring""" UpperCamelCase : List[str] = k_size // 2 UpperCamelCase , UpperCamelCase : Optional[int] = mgrid[0 - center : k_size - center, 0 - center : k_size - center] UpperCamelCase : Dict = 1 / (2 * pi * sigma) * exp(-(square(SCREAMING_SNAKE_CASE_ ) + square(SCREAMING_SNAKE_CASE_ )) / (2 * square(SCREAMING_SNAKE_CASE_ )) ) return g def a ( SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : int ): """simple docstring""" UpperCamelCase , UpperCamelCase : Tuple = image.shape[0], image.shape[1] # dst image height and width UpperCamelCase : str = height - k_size + 1 UpperCamelCase : Optional[int] = width - k_size + 1 # im2col, turn the k_size*k_size pixels into a row and np.vstack all rows UpperCamelCase : List[Any] = zeros((dst_height * dst_width, k_size * k_size) ) UpperCamelCase : Tuple = 0 for i, j in product(range(SCREAMING_SNAKE_CASE_ ) , range(SCREAMING_SNAKE_CASE_ ) ): UpperCamelCase : Dict = ravel(image[i : i + k_size, j : j + k_size] ) UpperCamelCase : Dict = window row += 1 # turn the kernel into shape(k*k, 1) UpperCamelCase : Optional[int] = gen_gaussian_kernel(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) UpperCamelCase : List[Any] = ravel(SCREAMING_SNAKE_CASE_ ) # reshape and get the dst image UpperCamelCase : Optional[int] = dot(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ).reshape(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ).astype(SCREAMING_SNAKE_CASE_ ) return dst if __name__ == "__main__": # read original image __UpperCAmelCase : Union[str, Any] = imread(r"../image_data/lena.jpg") # turn image in gray scale value __UpperCAmelCase : Optional[Any] = cvtColor(img, COLOR_BGR2GRAY) # get values with two different mask size __UpperCAmelCase : Optional[int] = gaussian_filter(gray, 3, sigma=1) __UpperCAmelCase : List[Any] = gaussian_filter(gray, 5, sigma=0.8) # show result images imshow("gaussian filter with 3x3 mask", gaussianaxa) imshow("gaussian filter with 5x5 mask", gaussianaxa) waitKey()
643
1
import darl # noqa import gym import tqdm from diffusers.experimental import ValueGuidedRLPipeline __UpperCAmelCase : str = { "n_samples": 64, "horizon": 32, "num_inference_steps": 20, "n_guide_steps": 2, # can set to 0 for faster sampling, does not use value network "scale_grad_by_std": True, "scale": 0.1, "eta": 0.0, "t_grad_cutoff": 2, "device": "cpu", } if __name__ == "__main__": __UpperCAmelCase : Optional[int] = "hopper-medium-v2" __UpperCAmelCase : Optional[int] = gym.make(env_name) __UpperCAmelCase : Optional[Any] = ValueGuidedRLPipeline.from_pretrained( "bglick13/hopper-medium-v2-value-function-hor32", env=env, ) env.seed(0) __UpperCAmelCase : Tuple = env.reset() __UpperCAmelCase : Optional[int] = 0 __UpperCAmelCase : List[str] = 0 __UpperCAmelCase : List[str] = 1000 __UpperCAmelCase : Any = [obs.copy()] try: for t in tqdm.tqdm(range(T)): # call the policy __UpperCAmelCase : Optional[int] = pipeline(obs, planning_horizon=32) # execute action in environment __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase : List[str] = env.step(denorm_actions) __UpperCAmelCase : Dict = env.get_normalized_score(total_reward) # update return total_reward += reward total_score += score print( f'''Step: {t}, Reward: {reward}, Total Reward: {total_reward}, Score: {score}, Total Score:''' f''' {total_score}''' ) # save observations for rendering rollout.append(next_observation.copy()) __UpperCAmelCase : str = next_observation except KeyboardInterrupt: pass print(f'''Total reward: {total_reward}''')
643
from .imports import is_tqdm_available if is_tqdm_available(): from tqdm.auto import tqdm as _tqdm from ..state import PartialState def a ( SCREAMING_SNAKE_CASE_ : bool = True , *SCREAMING_SNAKE_CASE_ : List[str] , **SCREAMING_SNAKE_CASE_ : Tuple ): """simple docstring""" if not is_tqdm_available(): raise ImportError('''Accelerate\'s `tqdm` module requires `tqdm` to be installed. Please run `pip install tqdm`.''' ) UpperCamelCase : int = False if main_process_only: UpperCamelCase : int = PartialState().local_process_index == 0 return _tqdm(*SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , disable=SCREAMING_SNAKE_CASE_ )
643
1
import copy import inspect import unittest from transformers import AutoBackbone from transformers.configuration_utils import PretrainedConfig from transformers.testing_utils import require_timm, require_torch, torch_device from transformers.utils.import_utils import is_torch_available from ...test_backbone_common import BackboneTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor if is_torch_available(): import torch from transformers import TimmBackbone, TimmBackboneConfig from ...test_pipeline_mixin import PipelineTesterMixin class UpperCAmelCase_ : '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE="resnet50" , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=32 , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=True , ): """simple docstring""" UpperCamelCase : Optional[Any] = parent UpperCamelCase : Dict = out_indices if out_indices is not None else [4] UpperCamelCase : List[str] = stage_names UpperCamelCase : Any = out_features UpperCamelCase : List[Any] = backbone UpperCamelCase : Tuple = batch_size UpperCamelCase : int = image_size UpperCamelCase : Optional[int] = num_channels UpperCamelCase : Optional[int] = use_pretrained_backbone UpperCamelCase : Optional[int] = is_training def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[Any] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) UpperCamelCase : List[Any] = self.get_config() return config, pixel_values def _lowercase ( self ): """simple docstring""" return TimmBackboneConfig( image_size=self.image_size , num_channels=self.num_channels , out_features=self.out_features , out_indices=self.out_indices , stage_names=self.stage_names , use_pretrained_backbone=self.use_pretrained_backbone , backbone=self.backbone , ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : int = TimmBackbone(config=__SCREAMING_SNAKE_CASE ) model.to(__SCREAMING_SNAKE_CASE ) model.eval() with torch.no_grad(): UpperCamelCase : int = model(__SCREAMING_SNAKE_CASE ) self.parent.assertEqual( result.feature_map[-1].shape , (self.batch_size, model.channels[-1], 14, 14) , ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Tuple = self.prepare_config_and_inputs() UpperCamelCase , UpperCamelCase : List[str] = config_and_inputs UpperCamelCase : Dict = {'''pixel_values''': pixel_values} return config, inputs_dict @require_torch @require_timm class UpperCAmelCase_ ( _a, _a, _a, unittest.TestCase): '''simple docstring''' __UpperCamelCase : str = (TimmBackbone,) if is_torch_available() else () __UpperCamelCase : List[str] = {"feature-extraction": TimmBackbone} if is_torch_available() else {} __UpperCamelCase : str = False __UpperCamelCase : Optional[Any] = False __UpperCamelCase : Union[str, Any] = False __UpperCamelCase : Any = False def _lowercase ( self ): """simple docstring""" UpperCamelCase : Any = TimmBackboneModelTester(self ) UpperCamelCase : Tuple = ConfigTester(self , config_class=__SCREAMING_SNAKE_CASE , has_text_modality=__SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[Any] = '''resnet18''' UpperCamelCase : List[Any] = '''microsoft/resnet-18''' UpperCamelCase : Optional[Any] = AutoBackbone.from_pretrained(__SCREAMING_SNAKE_CASE , use_timm_backbone=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Any = AutoBackbone.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertEqual(len(timm_model.out_features ) , len(transformers_model.out_features ) ) self.assertEqual(len(timm_model.stage_names ) , len(transformers_model.stage_names ) ) self.assertEqual(timm_model.channels , transformers_model.channels ) # Out indices are set to the last layer by default. For timm models, we don't know # the number of layers in advance, so we set it to (-1,), whereas for transformers # models, we set it to [len(stage_names) - 1] (kept for backward compatibility). self.assertEqual(timm_model.out_indices , (-1,) ) self.assertEqual(transformers_model.out_indices , [len(timm_model.stage_names ) - 1] ) UpperCamelCase : Optional[Any] = AutoBackbone.from_pretrained(__SCREAMING_SNAKE_CASE , use_timm_backbone=__SCREAMING_SNAKE_CASE , out_indices=[1, 2, 3] ) UpperCamelCase : str = AutoBackbone.from_pretrained(__SCREAMING_SNAKE_CASE , out_indices=[1, 2, 3] ) self.assertEqual(timm_model.out_indices , transformers_model.out_indices ) self.assertEqual(len(timm_model.out_features ) , len(transformers_model.out_features ) ) self.assertEqual(timm_model.channels , transformers_model.channels ) @unittest.skip('''TimmBackbone doesn\'t support feed forward chunking''' ) def _lowercase ( self ): """simple docstring""" pass @unittest.skip('''TimmBackbone doesn\'t have num_hidden_layers attribute''' ) def _lowercase ( self ): """simple docstring""" pass @unittest.skip('''TimmBackbone initialization is managed on the timm side''' ) def _lowercase ( self ): """simple docstring""" pass @unittest.skip('''TimmBackbone models doesn\'t have inputs_embeds''' ) def _lowercase ( self ): """simple docstring""" pass @unittest.skip('''TimmBackbone models doesn\'t have inputs_embeds''' ) def _lowercase ( self ): """simple docstring""" pass @unittest.skip('''TimmBackbone model cannot be created without specifying a backbone checkpoint''' ) def _lowercase ( self ): """simple docstring""" pass @unittest.skip('''Only checkpoints on timm can be loaded into TimmBackbone''' ) def _lowercase ( self ): """simple docstring""" pass @unittest.skip('''model weights aren\'t tied in TimmBackbone.''' ) def _lowercase ( self ): """simple docstring""" pass @unittest.skip('''model weights aren\'t tied in TimmBackbone.''' ) def _lowercase ( self ): """simple docstring""" pass @unittest.skip('''Only checkpoints on timm can be loaded into TimmBackbone''' ) def _lowercase ( self ): """simple docstring""" pass @unittest.skip('''Only checkpoints on timm can be loaded into TimmBackbone''' ) def _lowercase ( self ): """simple docstring""" pass @unittest.skip('''TimmBackbone doesn\'t have hidden size info in its configuration.''' ) def _lowercase ( self ): """simple docstring""" pass @unittest.skip('''TimmBackbone doesn\'t support output_attentions.''' ) def _lowercase ( self ): """simple docstring""" pass @unittest.skip('''Safetensors is not supported by timm.''' ) def _lowercase ( self ): """simple docstring""" pass @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.''' ) def _lowercase ( self ): """simple docstring""" pass def _lowercase ( self ): """simple docstring""" UpperCamelCase , UpperCamelCase : Any = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCamelCase : Dict = model_class(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[Any] = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic UpperCamelCase : Tuple = [*signature.parameters.keys()] UpperCamelCase : List[str] = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , __SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" UpperCamelCase , UpperCamelCase : Dict = self.model_tester.prepare_config_and_inputs_for_common() UpperCamelCase : Dict = True UpperCamelCase : Optional[int] = self.has_attentions # no need to test all models as different heads yield the same functionality UpperCamelCase : Optional[Any] = self.all_model_classes[0] UpperCamelCase : Tuple = model_class(__SCREAMING_SNAKE_CASE ) model.to(__SCREAMING_SNAKE_CASE ) UpperCamelCase : str = self._prepare_for_class(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) UpperCamelCase : int = model(**__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = outputs[0][-1] # Encoder-/Decoder-only models UpperCamelCase : List[Any] = outputs.hidden_states[0] hidden_states.retain_grad() if self.has_attentions: UpperCamelCase : Union[str, Any] = outputs.attentions[0] attentions.retain_grad() output.flatten()[0].backward(retain_graph=__SCREAMING_SNAKE_CASE ) self.assertIsNotNone(hidden_states.grad ) if self.has_attentions: self.assertIsNotNone(attentions.grad ) def _lowercase ( self ): """simple docstring""" UpperCamelCase , UpperCamelCase : Any = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCamelCase : Dict = model_class(__SCREAMING_SNAKE_CASE ) model.to(__SCREAMING_SNAKE_CASE ) model.eval() UpperCamelCase : List[Any] = model(**__SCREAMING_SNAKE_CASE ) self.assertEqual(len(result.feature_maps ) , len(config.out_indices ) ) self.assertEqual(len(model.channels ) , len(config.out_indices ) ) # Check output of last stage is taken if out_features=None, out_indices=None UpperCamelCase : Optional[int] = copy.deepcopy(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Dict = None UpperCamelCase : Dict = model_class(__SCREAMING_SNAKE_CASE ) model.to(__SCREAMING_SNAKE_CASE ) model.eval() UpperCamelCase : Optional[int] = model(**__SCREAMING_SNAKE_CASE ) self.assertEqual(len(result.feature_maps ) , 1 ) self.assertEqual(len(model.channels ) , 1 ) # Check backbone can be initialized with fresh weights UpperCamelCase : int = copy.deepcopy(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Any = False UpperCamelCase : Any = model_class(__SCREAMING_SNAKE_CASE ) model.to(__SCREAMING_SNAKE_CASE ) model.eval() UpperCamelCase : int = model(**__SCREAMING_SNAKE_CASE )
643
import io import os import unicodedata from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging __UpperCAmelCase : Any = logging.get_logger(__name__) __UpperCAmelCase : int = "▁" __UpperCAmelCase : Tuple = {"vocab_file": "vocab.txt", "sentencepiece_model_ckpt": "sentencepiece.bpe.model"} __UpperCAmelCase : Dict = { "sentencepiece_model_file": "sentencepiece.bpe.model", "vocab_file": "vocab.txt", } __UpperCAmelCase : Dict = { "vocab_file": { "ernie-m-base": "https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/vocab.txt", "ernie-m-large": "https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/vocab.txt", }, "sentencepiece_model_file": { "ernie-m-base": "https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/sentencepiece.bpe.model", "ernie-m-large": "https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/sentencepiece.bpe.model", }, } __UpperCAmelCase : str = { "ernie-m-base": 514, "ernie-m-large": 514, } __UpperCAmelCase : Optional[int] = { "ernie-m-base": {"do_lower_case": False}, "ernie-m-large": {"do_lower_case": False}, } class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : List[str] = ["input_ids"] __UpperCamelCase : List[str] = VOCAB_FILES_NAMES __UpperCamelCase : List[Any] = PRETRAINED_INIT_CONFIGURATION __UpperCamelCase : List[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCamelCase : int = PRETRAINED_VOCAB_FILES_MAP __UpperCamelCase : List[str] = RESOURCE_FILES_NAMES def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE="utf8" , __SCREAMING_SNAKE_CASE="[UNK]" , __SCREAMING_SNAKE_CASE="[SEP]" , __SCREAMING_SNAKE_CASE="[PAD]" , __SCREAMING_SNAKE_CASE="[CLS]" , __SCREAMING_SNAKE_CASE="[MASK]" , __SCREAMING_SNAKE_CASE = None , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" UpperCamelCase : int = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( do_lower_case=__SCREAMING_SNAKE_CASE , unk_token=__SCREAMING_SNAKE_CASE , sep_token=__SCREAMING_SNAKE_CASE , pad_token=__SCREAMING_SNAKE_CASE , cls_token=__SCREAMING_SNAKE_CASE , mask_token=__SCREAMING_SNAKE_CASE , vocab_file=__SCREAMING_SNAKE_CASE , encoding=__SCREAMING_SNAKE_CASE , sp_model_kwargs=self.sp_model_kwargs , **__SCREAMING_SNAKE_CASE , ) UpperCamelCase : List[str] = do_lower_case UpperCamelCase : Dict = sentencepiece_model_ckpt UpperCamelCase : str = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(__SCREAMING_SNAKE_CASE ) # to mimic paddlenlp.transformers.ernie_m.tokenizer.ErnieMTokenizer functioning if vocab_file is not None: UpperCamelCase : Optional[Any] = self.load_vocab(filepath=__SCREAMING_SNAKE_CASE ) else: UpperCamelCase : int = {self.sp_model.id_to_piece(__SCREAMING_SNAKE_CASE ): id for id in range(self.sp_model.get_piece_size() )} UpperCamelCase : str = {v: k for k, v in self.vocab.items()} def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if text is None: return None UpperCamelCase : str = self.tokenize(__SCREAMING_SNAKE_CASE ) UpperCamelCase , UpperCamelCase : str = '''''', [] for i, ch in enumerate(__SCREAMING_SNAKE_CASE ): if ch in self.SP_CHAR_MAPPING: UpperCamelCase : Optional[int] = self.SP_CHAR_MAPPING.get(__SCREAMING_SNAKE_CASE ) else: UpperCamelCase : Optional[Any] = unicodedata.normalize('''NFKC''' , __SCREAMING_SNAKE_CASE ) if self.is_whitespace(__SCREAMING_SNAKE_CASE ): continue normalized_text += ch char_mapping.extend([i] * len(__SCREAMING_SNAKE_CASE ) ) UpperCamelCase , UpperCamelCase , UpperCamelCase : Tuple = normalized_text, [], 0 if self.do_lower_case: UpperCamelCase : Tuple = text.lower() for token in split_tokens: if token[:1] == "▁": UpperCamelCase : Any = token[1:] UpperCamelCase : Optional[int] = text[offset:].index(__SCREAMING_SNAKE_CASE ) + offset UpperCamelCase : List[Any] = start + len(__SCREAMING_SNAKE_CASE ) token_mapping.append((char_mapping[start], char_mapping[end - 1] + 1) ) UpperCamelCase : str = end return token_mapping @property def _lowercase ( self ): """simple docstring""" return len(self.vocab ) def _lowercase ( self ): """simple docstring""" return dict(self.vocab , **self.added_tokens_encoder ) def __getstate__( self ): """simple docstring""" UpperCamelCase : Optional[Any] = self.__dict__.copy() UpperCamelCase : str = None return state def __setstate__( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Tuple = d # for backward compatibility if not hasattr(self , '''sp_model_kwargs''' ): UpperCamelCase : Optional[int] = {} UpperCamelCase : Optional[Any] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.sentencepiece_model_ckpt ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" return "".join((self.SP_CHAR_MAPPING.get(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) for c in text) ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=64 , __SCREAMING_SNAKE_CASE=0.1 ): """simple docstring""" if self.sp_model_kwargs.get('''enable_sampling''' ) is True: UpperCamelCase : List[str] = True if self.sp_model_kwargs.get('''alpha''' ) is not None: UpperCamelCase : Any = self.sp_model_kwargs.get('''alpha''' ) if self.sp_model_kwargs.get('''nbest_size''' ) is not None: UpperCamelCase : Tuple = self.sp_model_kwargs.get('''nbest_size''' ) if not enable_sampling: UpperCamelCase : int = self.sp_model.EncodeAsPieces(__SCREAMING_SNAKE_CASE ) else: UpperCamelCase : Optional[Any] = self.sp_model.SampleEncodeAsPieces(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = [] for pi, piece in enumerate(__SCREAMING_SNAKE_CASE ): if piece == SPIECE_UNDERLINE: if not pieces[pi + 1].startswith(__SCREAMING_SNAKE_CASE ) and pi != 0: new_pieces.append(__SCREAMING_SNAKE_CASE ) continue else: continue UpperCamelCase : Any = 0 for i, chunk in enumerate(__SCREAMING_SNAKE_CASE ): if chunk == SPIECE_UNDERLINE: continue if self.is_ch_char(__SCREAMING_SNAKE_CASE ) or self.is_punct(__SCREAMING_SNAKE_CASE ): if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE: new_pieces.append(piece[lst_i:i] ) new_pieces.append(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Dict = i + 1 elif chunk.isdigit() and i > 0 and not piece[i - 1].isdigit(): if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE: new_pieces.append(piece[lst_i:i] ) UpperCamelCase : Union[str, Any] = i elif not chunk.isdigit() and i > 0 and piece[i - 1].isdigit(): if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE: new_pieces.append(piece[lst_i:i] ) UpperCamelCase : Any = i if len(__SCREAMING_SNAKE_CASE ) > lst_i: new_pieces.append(piece[lst_i:] ) return new_pieces def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Optional[int] = ''''''.join(__SCREAMING_SNAKE_CASE ).replace(__SCREAMING_SNAKE_CASE , ''' ''' ).strip() return out_string def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : int = self.convert_ids_to_tokens(__SCREAMING_SNAKE_CASE ) UpperCamelCase : int = ''''''.join(__SCREAMING_SNAKE_CASE ).replace(__SCREAMING_SNAKE_CASE , ''' ''' ).strip() return out_string def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" return self.vocab.get(__SCREAMING_SNAKE_CASE , self.vocab.get(self.unk_token ) ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" return self.reverse_vocab.get(__SCREAMING_SNAKE_CASE , self.unk_token ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None ): """simple docstring""" if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] UpperCamelCase : Any = [self.cls_token_id] UpperCamelCase : str = [self.sep_token_id] return _cls + token_ids_a + _sep + _sep + token_ids_a + _sep def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None ): """simple docstring""" if offset_mapping_a is None: return [(0, 0)] + offset_mapping_a + [(0, 0)] return [(0, 0)] + offset_mapping_a + [(0, 0), (0, 0)] + offset_mapping_a + [(0, 0)] def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=False ): """simple docstring""" if already_has_special_tokens: if token_ids_a is not None: raise ValueError( '''You should not supply a second sequence if the provided sequence of ''' '''ids is already formatted with special tokens for the model.''' ) return [1 if x in [self.sep_token_id, self.cls_token_id] else 0 for x in token_ids_a] if token_ids_a is not None: return [1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1, 1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1] return [1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1] def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ): """simple docstring""" if token_ids_a is None: # [CLS] X [SEP] return (len(__SCREAMING_SNAKE_CASE ) + 2) * [0] # [CLS] A [SEP] [SEP] B [SEP] return [0] * (len(__SCREAMING_SNAKE_CASE ) + 1) + [1] * (len(__SCREAMING_SNAKE_CASE ) + 3) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if "\u4e00" <= char <= "\u9fff": return True return False def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if ("a" <= char <= "z") or ("A" <= char <= "Z"): return True return False def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if char in ",;:.?!~,;:。?!《》【】": return True return False def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if char == " " or char == "\t" or char == "\n" or char == "\r": return True if len(__SCREAMING_SNAKE_CASE ) == 1: UpperCamelCase : Optional[int] = unicodedata.category(__SCREAMING_SNAKE_CASE ) if cat == "Zs": return True return False def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : int = {} with io.open(__SCREAMING_SNAKE_CASE , '''r''' , encoding='''utf-8''' ) as f: for index, line in enumerate(__SCREAMING_SNAKE_CASE ): UpperCamelCase : Tuple = line.rstrip('''\n''' ) UpperCamelCase : List[Any] = int(__SCREAMING_SNAKE_CASE ) return token_to_idx def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ): """simple docstring""" UpperCamelCase : Union[str, Any] = 0 if os.path.isdir(__SCREAMING_SNAKE_CASE ): UpperCamelCase : Dict = os.path.join( __SCREAMING_SNAKE_CASE , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) else: UpperCamelCase : Union[str, Any] = (filename_prefix + '''-''' if filename_prefix else '''''') + save_directory with open(__SCREAMING_SNAKE_CASE , '''w''' , encoding='''utf-8''' ) as writer: for token, token_index in sorted(self.vocab.items() , key=lambda __SCREAMING_SNAKE_CASE : kv[1] ): if index != token_index: logger.warning( f"""Saving vocabulary to {vocab_file}: vocabulary indices are not consecutive.""" ''' Please check that the vocabulary is not corrupted!''' ) UpperCamelCase : List[Any] = token_index writer.write(token + '''\n''' ) index += 1 UpperCamelCase : Tuple = os.path.join(__SCREAMING_SNAKE_CASE , '''sentencepiece.bpe.model''' ) with open(__SCREAMING_SNAKE_CASE , '''wb''' ) as fi: UpperCamelCase : List[Any] = self.sp_model.serialized_model_proto() fi.write(__SCREAMING_SNAKE_CASE ) return (vocab_file,)
643
1
import colorsys from PIL import Image # type: ignore def a ( SCREAMING_SNAKE_CASE_ : float , SCREAMING_SNAKE_CASE_ : float , SCREAMING_SNAKE_CASE_ : int ): """simple docstring""" UpperCamelCase : Optional[int] = x UpperCamelCase : Any = y for step in range(SCREAMING_SNAKE_CASE_ ): # noqa: B007 UpperCamelCase : Tuple = a * a - b * b + x UpperCamelCase : int = 2 * a * b + y UpperCamelCase : Dict = a_new # divergence happens for all complex number with an absolute value # greater than 4 if a * a + b * b > 4: break return step / (max_step - 1) def a ( SCREAMING_SNAKE_CASE_ : float ): """simple docstring""" if distance == 1: return (0, 0, 0) else: return (2_5_5, 2_5_5, 2_5_5) def a ( SCREAMING_SNAKE_CASE_ : float ): """simple docstring""" if distance == 1: return (0, 0, 0) else: return tuple(round(i * 2_5_5 ) for i in colorsys.hsv_to_rgb(SCREAMING_SNAKE_CASE_ , 1 , 1 ) ) def a ( SCREAMING_SNAKE_CASE_ : int = 8_0_0 , SCREAMING_SNAKE_CASE_ : int = 6_0_0 , SCREAMING_SNAKE_CASE_ : float = -0.6 , SCREAMING_SNAKE_CASE_ : float = 0 , SCREAMING_SNAKE_CASE_ : float = 3.2 , SCREAMING_SNAKE_CASE_ : int = 5_0 , SCREAMING_SNAKE_CASE_ : bool = True , ): """simple docstring""" UpperCamelCase : str = Image.new('''RGB''' , (image_width, image_height) ) UpperCamelCase : List[str] = img.load() # loop through the image-coordinates for image_x in range(SCREAMING_SNAKE_CASE_ ): for image_y in range(SCREAMING_SNAKE_CASE_ ): # determine the figure-coordinates based on the image-coordinates UpperCamelCase : Optional[Any] = figure_width / image_width * image_height UpperCamelCase : Dict = figure_center_x + (image_x / image_width - 0.5) * figure_width UpperCamelCase : int = figure_center_y + (image_y / image_height - 0.5) * figure_height UpperCamelCase : List[Any] = get_distance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # color the corresponding pixel based on the selected coloring-function if use_distance_color_coding: UpperCamelCase : str = get_color_coded_rgb(SCREAMING_SNAKE_CASE_ ) else: UpperCamelCase : str = get_black_and_white_rgb(SCREAMING_SNAKE_CASE_ ) return img if __name__ == "__main__": import doctest doctest.testmod() # colored version, full figure __UpperCAmelCase : Dict = get_image() # uncomment for colored version, different section, zoomed in # img = get_image(figure_center_x = -0.6, figure_center_y = -0.4, # figure_width = 0.8) # uncomment for black and white version, full figure # img = get_image(use_distance_color_coding = False) # uncomment to save the image # img.save("mandelbrot.png") img.show()
643
from typing import List, Optional, Tuple, Union import torch from torch import nn from torch.nn import CrossEntropyLoss from ... import AutoBackbone from ...modeling_outputs import SemanticSegmenterOutput from ...modeling_utils import PreTrainedModel from ...utils import add_start_docstrings, add_start_docstrings_to_model_forward, replace_return_docstrings from ...utils.backbone_utils import BackboneMixin from .configuration_upernet import UperNetConfig __UpperCAmelCase : List[Any] = [ "openmmlab/upernet-convnext-tiny", # See all UperNet models at https://huggingface.co/models?filter=upernet ] # General docstring __UpperCAmelCase : List[str] = "UperNetConfig" class UpperCAmelCase_ ( nn.Module): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = 0 , __SCREAMING_SNAKE_CASE = False , __SCREAMING_SNAKE_CASE = 1 , ): """simple docstring""" super().__init__() UpperCamelCase : str = nn.Convad( in_channels=__SCREAMING_SNAKE_CASE , out_channels=__SCREAMING_SNAKE_CASE , kernel_size=__SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE , bias=__SCREAMING_SNAKE_CASE , dilation=__SCREAMING_SNAKE_CASE , ) UpperCamelCase : int = nn.BatchNormad(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[Any] = nn.ReLU() def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Any = self.conv(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[Any] = self.batch_norm(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = self.activation(__SCREAMING_SNAKE_CASE ) return output class UpperCAmelCase_ ( nn.Module): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" super().__init__() UpperCamelCase : List[Any] = [ nn.AdaptiveAvgPoolad(__SCREAMING_SNAKE_CASE ), UperNetConvModule(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , kernel_size=1 ), ] for i, layer in enumerate(self.layers ): self.add_module(str(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : str = input for layer in self.layers: UpperCamelCase : int = layer(__SCREAMING_SNAKE_CASE ) return hidden_state class UpperCAmelCase_ ( nn.Module): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" super().__init__() UpperCamelCase : List[Any] = pool_scales UpperCamelCase : Dict = align_corners UpperCamelCase : Optional[int] = in_channels UpperCamelCase : Union[str, Any] = channels UpperCamelCase : List[str] = [] for i, pool_scale in enumerate(__SCREAMING_SNAKE_CASE ): UpperCamelCase : Union[str, Any] = UperNetPyramidPoolingBlock(pool_scale=__SCREAMING_SNAKE_CASE , in_channels=__SCREAMING_SNAKE_CASE , channels=__SCREAMING_SNAKE_CASE ) self.blocks.append(__SCREAMING_SNAKE_CASE ) self.add_module(str(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : str = [] for ppm in self.blocks: UpperCamelCase : List[str] = ppm(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Tuple = nn.functional.interpolate( __SCREAMING_SNAKE_CASE , size=x.size()[2:] , mode='''bilinear''' , align_corners=self.align_corners ) ppm_outs.append(__SCREAMING_SNAKE_CASE ) return ppm_outs class UpperCAmelCase_ ( nn.Module): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" super().__init__() UpperCamelCase : int = config UpperCamelCase : List[str] = config.pool_scales # e.g. (1, 2, 3, 6) UpperCamelCase : Optional[int] = in_channels UpperCamelCase : str = config.hidden_size UpperCamelCase : str = False UpperCamelCase : List[str] = nn.Convad(self.channels , config.num_labels , kernel_size=1 ) # PSP Module UpperCamelCase : Optional[int] = UperNetPyramidPoolingModule( self.pool_scales , self.in_channels[-1] , self.channels , align_corners=self.align_corners , ) UpperCamelCase : str = UperNetConvModule( self.in_channels[-1] + len(self.pool_scales ) * self.channels , self.channels , kernel_size=3 , padding=1 , ) # FPN Module UpperCamelCase : Union[str, Any] = nn.ModuleList() UpperCamelCase : Union[str, Any] = nn.ModuleList() for in_channels in self.in_channels[:-1]: # skip the top layer UpperCamelCase : List[Any] = UperNetConvModule(__SCREAMING_SNAKE_CASE , self.channels , kernel_size=1 ) UpperCamelCase : int = UperNetConvModule(self.channels , self.channels , kernel_size=3 , padding=1 ) self.lateral_convs.append(__SCREAMING_SNAKE_CASE ) self.fpn_convs.append(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[Any] = UperNetConvModule( len(self.in_channels ) * self.channels , self.channels , kernel_size=3 , padding=1 , ) def _lowercase ( self ): """simple docstring""" self.apply(self._init_weights ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if isinstance(__SCREAMING_SNAKE_CASE , nn.Convad ): module.weight.data.normal_(mean=0.0 , std=self.config.initializer_range ) if module.bias is not None: module.bias.data.zero_() def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Any = inputs[-1] UpperCamelCase : int = [x] psp_outs.extend(self.psp_modules(__SCREAMING_SNAKE_CASE ) ) UpperCamelCase : Any = torch.cat(__SCREAMING_SNAKE_CASE , dim=1 ) UpperCamelCase : Union[str, Any] = self.bottleneck(__SCREAMING_SNAKE_CASE ) return output def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : str = [lateral_conv(encoder_hidden_states[i] ) for i, lateral_conv in enumerate(self.lateral_convs )] laterals.append(self.psp_forward(__SCREAMING_SNAKE_CASE ) ) # build top-down path UpperCamelCase : int = len(__SCREAMING_SNAKE_CASE ) for i in range(used_backbone_levels - 1 , 0 , -1 ): UpperCamelCase : Optional[int] = laterals[i - 1].shape[2:] UpperCamelCase : Optional[Any] = laterals[i - 1] + nn.functional.interpolate( laterals[i] , size=__SCREAMING_SNAKE_CASE , mode='''bilinear''' , align_corners=self.align_corners ) # build outputs UpperCamelCase : str = [self.fpn_convs[i](laterals[i] ) for i in range(used_backbone_levels - 1 )] # append psp feature fpn_outs.append(laterals[-1] ) for i in range(used_backbone_levels - 1 , 0 , -1 ): UpperCamelCase : int = nn.functional.interpolate( fpn_outs[i] , size=fpn_outs[0].shape[2:] , mode='''bilinear''' , align_corners=self.align_corners ) UpperCamelCase : str = torch.cat(__SCREAMING_SNAKE_CASE , dim=1 ) UpperCamelCase : Tuple = self.fpn_bottleneck(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = self.classifier(__SCREAMING_SNAKE_CASE ) return output class UpperCAmelCase_ ( nn.Module): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = 2 , __SCREAMING_SNAKE_CASE = 3 , __SCREAMING_SNAKE_CASE = 1 ): """simple docstring""" super().__init__() UpperCamelCase : Dict = config UpperCamelCase : Optional[Any] = config.auxiliary_in_channels UpperCamelCase : Union[str, Any] = config.auxiliary_channels UpperCamelCase : Union[str, Any] = config.auxiliary_num_convs UpperCamelCase : Optional[Any] = config.auxiliary_concat_input UpperCamelCase : List[str] = in_index UpperCamelCase : Any = (kernel_size // 2) * dilation UpperCamelCase : Optional[Any] = [] convs.append( UperNetConvModule( self.in_channels , self.channels , kernel_size=__SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE , dilation=__SCREAMING_SNAKE_CASE ) ) for i in range(self.num_convs - 1 ): convs.append( UperNetConvModule( self.channels , self.channels , kernel_size=__SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE , dilation=__SCREAMING_SNAKE_CASE ) ) if self.num_convs == 0: UpperCamelCase : str = nn.Identity() else: UpperCamelCase : Dict = nn.Sequential(*__SCREAMING_SNAKE_CASE ) if self.concat_input: UpperCamelCase : Union[str, Any] = UperNetConvModule( self.in_channels + self.channels , self.channels , kernel_size=__SCREAMING_SNAKE_CASE , padding=kernel_size // 2 ) UpperCamelCase : Optional[Any] = nn.Convad(self.channels , config.num_labels , kernel_size=1 ) def _lowercase ( self ): """simple docstring""" self.apply(self._init_weights ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if isinstance(__SCREAMING_SNAKE_CASE , nn.Convad ): module.weight.data.normal_(mean=0.0 , std=self.config.initializer_range ) if module.bias is not None: module.bias.data.zero_() def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Any = encoder_hidden_states[self.in_index] UpperCamelCase : str = self.convs(__SCREAMING_SNAKE_CASE ) if self.concat_input: UpperCamelCase : int = self.conv_cat(torch.cat([hidden_states, output] , dim=1 ) ) UpperCamelCase : Union[str, Any] = self.classifier(__SCREAMING_SNAKE_CASE ) return output class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : Optional[Any] = UperNetConfig __UpperCamelCase : Optional[int] = "pixel_values" __UpperCamelCase : Dict = True def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): module.backbone.init_weights() module.decode_head.init_weights() module.auxiliary_head.init_weights() def _lowercase ( self ): """simple docstring""" self.backbone.init_weights() self.decode_head.init_weights() self.auxiliary_head.init_weights() def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=False ): """simple docstring""" if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): UpperCamelCase : str = value __UpperCAmelCase : List[Any] = r"\n Parameters:\n This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) sub-class. Use\n it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and\n behavior.\n config ([`UperNetConfig`]): 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" __UpperCAmelCase : Union[str, Any] = r"\n Args:\n pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n Pixel values. Padding will be ignored by default should you provide it. Pixel values can be obtained using\n [`AutoImageProcessor`]. See [`SegformerImageProcessor.__call__`] for details.\n output_attentions (`bool`, *optional*):\n Whether or not to return the attentions tensors of all attention layers in case the backbone has them. See\n `attentions` under returned tensors for more detail.\n output_hidden_states (`bool`, *optional*):\n Whether or not to return the hidden states of all layers of the backbone. See `hidden_states` under\n returned tensors for 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( "UperNet framework leveraging any vision backbone e.g. for ADE20k, CityScapes.", _a, ) class UpperCAmelCase_ ( _a): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" super().__init__(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Dict = AutoBackbone.from_config(config.backbone_config ) # Semantic segmentation head(s) UpperCamelCase : int = UperNetHead(__SCREAMING_SNAKE_CASE , in_channels=self.backbone.channels ) UpperCamelCase : int = UperNetFCNHead(__SCREAMING_SNAKE_CASE ) if config.use_auxiliary_head else None # Initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(UPERNET_INPUTS_DOCSTRING.format('''batch_size, sequence_length''' ) ) @replace_return_docstrings(output_type=__SCREAMING_SNAKE_CASE , config_class=_CONFIG_FOR_DOC ) def _lowercase ( self , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , ): """simple docstring""" UpperCamelCase : List[str] = return_dict if return_dict is not None else self.config.use_return_dict UpperCamelCase : str = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) UpperCamelCase : Tuple = output_attentions if output_attentions is not None else self.config.output_attentions UpperCamelCase : Tuple = self.backbone.forward_with_filtered_kwargs( __SCREAMING_SNAKE_CASE , output_hidden_states=__SCREAMING_SNAKE_CASE , output_attentions=__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = outputs.feature_maps UpperCamelCase : Union[str, Any] = self.decode_head(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = nn.functional.interpolate(__SCREAMING_SNAKE_CASE , size=pixel_values.shape[2:] , mode='''bilinear''' , align_corners=__SCREAMING_SNAKE_CASE ) UpperCamelCase : int = None if self.auxiliary_head is not None: UpperCamelCase : int = self.auxiliary_head(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Tuple = nn.functional.interpolate( __SCREAMING_SNAKE_CASE , size=pixel_values.shape[2:] , mode='''bilinear''' , align_corners=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = None if labels is not None: if self.config.num_labels == 1: raise ValueError('''The number of labels should be greater than one''' ) else: # compute weighted loss UpperCamelCase : Optional[int] = CrossEntropyLoss(ignore_index=self.config.loss_ignore_index ) UpperCamelCase : Tuple = loss_fct(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[Any] = loss_fct(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = main_loss + self.config.auxiliary_loss_weight * auxiliary_loss if not return_dict: if output_hidden_states: UpperCamelCase : Optional[Any] = (logits,) + outputs[1:] else: UpperCamelCase : int = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return SemanticSegmenterOutput( loss=__SCREAMING_SNAKE_CASE , logits=__SCREAMING_SNAKE_CASE , hidden_states=outputs.hidden_states , attentions=outputs.attentions , )
643
1
def a ( SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int ): """simple docstring""" if not isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): raise ValueError('''iterations must be defined as integers''' ) if not isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) or not number >= 1: raise ValueError( '''starting number must be and integer and be more than 0''' ) if not iterations >= 1: raise ValueError('''Iterations must be done more than 0 times to play FizzBuzz''' ) UpperCamelCase : int = '''''' while number <= iterations: if number % 3 == 0: out += "Fizz" if number % 5 == 0: out += "Buzz" if 0 not in (number % 3, number % 5): out += str(SCREAMING_SNAKE_CASE_ ) # print(out) number += 1 out += " " return out if __name__ == "__main__": import doctest doctest.testmod()
643
import json import os import tempfile import transformers import datasets from utils import generate_example_dataset, get_duration __UpperCAmelCase : Optional[int] = 500000 __UpperCAmelCase , __UpperCAmelCase : Any = os.path.split(__file__) __UpperCAmelCase : int = os.path.join(RESULTS_BASEPATH, "results", RESULTS_FILENAME.replace(".py", ".json")) @get_duration def a ( SCREAMING_SNAKE_CASE_ : datasets.Dataset , **SCREAMING_SNAKE_CASE_ : Union[str, Any] ): """simple docstring""" UpperCamelCase : Tuple = dataset.map(**SCREAMING_SNAKE_CASE_ ) @get_duration def a ( SCREAMING_SNAKE_CASE_ : datasets.Dataset , **SCREAMING_SNAKE_CASE_ : Any ): """simple docstring""" UpperCamelCase : int = dataset.filter(**SCREAMING_SNAKE_CASE_ ) def a ( ): """simple docstring""" UpperCamelCase : Optional[int] = {'''num examples''': SPEED_TEST_N_EXAMPLES} with tempfile.TemporaryDirectory() as tmp_dir: UpperCamelCase : Dict = datasets.Features({'''text''': datasets.Value('''string''' ), '''numbers''': datasets.Value('''float32''' )} ) UpperCamelCase : List[str] = generate_example_dataset( os.path.join(SCREAMING_SNAKE_CASE_ , '''dataset.arrow''' ) , SCREAMING_SNAKE_CASE_ , num_examples=SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Union[str, Any] = transformers.AutoTokenizer.from_pretrained('''bert-base-cased''' , use_fast=SCREAMING_SNAKE_CASE_ ) def tokenize(SCREAMING_SNAKE_CASE_ : Dict ): return tokenizer(examples['''text'''] ) UpperCamelCase : List[Any] = map(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : List[str] = map(SCREAMING_SNAKE_CASE_ , batched=SCREAMING_SNAKE_CASE_ ) UpperCamelCase : List[Any] = map(SCREAMING_SNAKE_CASE_ , function=lambda SCREAMING_SNAKE_CASE_ : None , batched=SCREAMING_SNAKE_CASE_ ) with dataset.formatted_as(type='''numpy''' ): UpperCamelCase : Tuple = map(SCREAMING_SNAKE_CASE_ , function=lambda SCREAMING_SNAKE_CASE_ : None , batched=SCREAMING_SNAKE_CASE_ ) with dataset.formatted_as(type='''pandas''' ): UpperCamelCase : int = map(SCREAMING_SNAKE_CASE_ , function=lambda SCREAMING_SNAKE_CASE_ : None , batched=SCREAMING_SNAKE_CASE_ ) with dataset.formatted_as(type='''torch''' , columns='''numbers''' ): UpperCamelCase : Dict = map(SCREAMING_SNAKE_CASE_ , function=lambda SCREAMING_SNAKE_CASE_ : None , batched=SCREAMING_SNAKE_CASE_ ) with dataset.formatted_as(type='''tensorflow''' , columns='''numbers''' ): UpperCamelCase : Tuple = map(SCREAMING_SNAKE_CASE_ , function=lambda SCREAMING_SNAKE_CASE_ : None , batched=SCREAMING_SNAKE_CASE_ ) UpperCamelCase : List[str] = map(SCREAMING_SNAKE_CASE_ , function=SCREAMING_SNAKE_CASE_ , batched=SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Dict = filter(SCREAMING_SNAKE_CASE_ ) # Activate later when tokenizer support batched inputs # with dataset.formatted_as(type='numpy'): # times[func.__name__ + " fast-tokenizer batched numpy"] = func(dataset, function=tokenize, batched=True) with open(SCREAMING_SNAKE_CASE_ , '''wb''' ) as f: f.write(json.dumps(SCREAMING_SNAKE_CASE_ ).encode('''utf-8''' ) ) if __name__ == "__main__": # useful to run the profiler benchmark_map_filter()
643
1
from datetime import datetime as dt import os from github import Github __UpperCAmelCase : Optional[int] = [ "good first issue", "good second issue", "good difficult issue", "feature request", "new model", "wip", ] def a ( ): """simple docstring""" UpperCamelCase : List[str] = Github(os.environ['''GITHUB_TOKEN'''] ) UpperCamelCase : Any = g.get_repo('''huggingface/transformers''' ) UpperCamelCase : str = repo.get_issues(state='''open''' ) for issue in open_issues: UpperCamelCase : Dict = sorted([comment for comment in issue.get_comments()] , key=lambda SCREAMING_SNAKE_CASE_ : i.created_at , reverse=SCREAMING_SNAKE_CASE_ ) UpperCamelCase : List[Any] = comments[0] if len(SCREAMING_SNAKE_CASE_ ) > 0 else None if ( last_comment is not None and last_comment.user.login == "github-actions[bot]" and (dt.utcnow() - issue.updated_at).days > 7 and (dt.utcnow() - issue.created_at).days >= 3_0 and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels() ) ): # print(f"Would close issue {issue.number} since it has been 7 days of inactivity since bot mention.") issue.edit(state='''closed''' ) elif ( (dt.utcnow() - issue.updated_at).days > 2_3 and (dt.utcnow() - issue.created_at).days >= 3_0 and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels() ) ): # print(f"Would add stale comment to {issue.number}") issue.create_comment( '''This issue has been automatically marked as stale because it has not had ''' '''recent activity. If you think this still needs to be addressed ''' '''please comment on this thread.\n\nPlease note that issues that do not follow the ''' '''[contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) ''' '''are likely to be ignored.''' ) if __name__ == "__main__": main()
643
from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import ShapEPipeline else: from .camera import create_pan_cameras from .pipeline_shap_e import ShapEPipeline from .pipeline_shap_e_img2img import ShapEImgaImgPipeline from .renderer import ( BoundingBoxVolume, ImportanceRaySampler, MLPNeRFModelOutput, MLPNeRSTFModel, ShapEParamsProjModel, ShapERenderer, StratifiedRaySampler, VoidNeRFModel, )
643
1
import copy from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import ClassLabel, Features, Image from .base import TaskTemplate @dataclass(frozen=_a) class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : str = field(default="image-classification", metadata={"include_in_asdict_even_if_is_default": True}) __UpperCamelCase : ClassVar[Features] = Features({"image": Image()}) __UpperCamelCase : ClassVar[Features] = Features({"labels": ClassLabel}) __UpperCamelCase : str = "image" __UpperCamelCase : str = "labels" def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if self.label_column not in features: raise ValueError(f"""Column {self.label_column} is not present in features.""" ) if not isinstance(features[self.label_column] , __SCREAMING_SNAKE_CASE ): raise ValueError(f"""Column {self.label_column} is not a ClassLabel.""" ) UpperCamelCase : str = copy.deepcopy(self ) UpperCamelCase : Optional[Any] = self.label_schema.copy() UpperCamelCase : Union[str, Any] = features[self.label_column] UpperCamelCase : Union[str, Any] = label_schema return task_template @property def _lowercase ( self ): """simple docstring""" return { self.image_column: "image", self.label_column: "labels", }
643
import torch from transformers import AutoModel class UpperCAmelCase_ ( torch.nn.Module): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE="sayef/fsner-bert-base-uncased" ): """simple docstring""" super(__SCREAMING_SNAKE_CASE , self ).__init__() UpperCamelCase : List[str] = AutoModel.from_pretrained(__SCREAMING_SNAKE_CASE , return_dict=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = torch.nn.CosineSimilarity(3 , 1e-08 ) UpperCamelCase : List[Any] = torch.nn.Softmax(dim=1 ) def _lowercase ( self , **__SCREAMING_SNAKE_CASE ): """simple docstring""" return self.bert(**__SCREAMING_SNAKE_CASE ).last_hidden_state def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" return token_embeddings.sum(2 , keepdim=__SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=1 ): """simple docstring""" return self.softmax(T * self.cos(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Any = W_supports['''sizes'''].tolist() UpperCamelCase : Optional[int] = W_supports['''start_token_id'''].item() UpperCamelCase : Any = W_supports['''end_token_id'''].item() del W_supports["sizes"] del W_supports["start_token_id"] del W_supports["end_token_id"] UpperCamelCase : Union[str, Any] = self.BERT(**__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = self.BERT(**__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[Any] = None UpperCamelCase : Any = None UpperCamelCase : Optional[Any] = W_supports['''input_ids'''] == start_token_id UpperCamelCase : Any = W_supports['''input_ids'''] == end_token_id for i, size in enumerate(__SCREAMING_SNAKE_CASE ): if i == 0: UpperCamelCase : Optional[int] = 0 else: UpperCamelCase : Tuple = support_sizes[i - 1] UpperCamelCase : Tuple = S[s : s + size][start_token_masks[s : s + size]] UpperCamelCase : List[str] = S[s : s + size][end_token_masks[s : s + size]] UpperCamelCase : Dict = torch.matmul(q[i] , s_start.T ).sum(1 ).softmax(0 ) UpperCamelCase : Tuple = torch.matmul(q[i] , s_end.T ).sum(1 ).softmax(0 ) if p_starts is not None: UpperCamelCase : List[str] = torch.vstack((p_starts, p_start) ) UpperCamelCase : Union[str, Any] = torch.vstack((p_ends, p_end) ) else: UpperCamelCase : str = p_start UpperCamelCase : Optional[int] = p_end return p_starts, p_ends
643
1
from ...configuration_utils import PretrainedConfig from ...utils import logging __UpperCAmelCase : Dict = logging.get_logger(__name__) __UpperCAmelCase : Optional[int] = { "microsoft/biogpt": "https://huggingface.co/microsoft/biogpt/resolve/main/config.json", # See all BioGPT models at https://huggingface.co/models?filter=biogpt } class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : str = "biogpt" def __init__( self , __SCREAMING_SNAKE_CASE=42_384 , __SCREAMING_SNAKE_CASE=1_024 , __SCREAMING_SNAKE_CASE=24 , __SCREAMING_SNAKE_CASE=16 , __SCREAMING_SNAKE_CASE=4_096 , __SCREAMING_SNAKE_CASE="gelu" , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=1_024 , __SCREAMING_SNAKE_CASE=0.02 , __SCREAMING_SNAKE_CASE=1e-12 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=1 , __SCREAMING_SNAKE_CASE=0 , __SCREAMING_SNAKE_CASE=2 , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" UpperCamelCase : Tuple = vocab_size UpperCamelCase : str = max_position_embeddings UpperCamelCase : Tuple = hidden_size UpperCamelCase : int = num_hidden_layers UpperCamelCase : int = num_attention_heads UpperCamelCase : Union[str, Any] = intermediate_size UpperCamelCase : int = hidden_act UpperCamelCase : List[str] = hidden_dropout_prob UpperCamelCase : Any = attention_probs_dropout_prob UpperCamelCase : Tuple = initializer_range UpperCamelCase : str = layer_norm_eps UpperCamelCase : str = scale_embedding UpperCamelCase : str = use_cache UpperCamelCase : Optional[Any] = layerdrop UpperCamelCase : Any = activation_dropout super().__init__(pad_token_id=__SCREAMING_SNAKE_CASE , bos_token_id=__SCREAMING_SNAKE_CASE , eos_token_id=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE )
643
import json import os import unittest from transformers import DebertaTokenizer, DebertaTokenizerFast from transformers.models.deberta.tokenization_deberta import VOCAB_FILES_NAMES from transformers.testing_utils import slow from ...test_tokenization_common import TokenizerTesterMixin class UpperCAmelCase_ ( _a, unittest.TestCase): '''simple docstring''' __UpperCamelCase : str = DebertaTokenizer __UpperCamelCase : Optional[int] = True __UpperCamelCase : Optional[int] = DebertaTokenizerFast def _lowercase ( self ): """simple docstring""" super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt UpperCamelCase : Optional[int] = [ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''\u0120''', '''\u0120l''', '''\u0120n''', '''\u0120lo''', '''\u0120low''', '''er''', '''\u0120lowest''', '''\u0120newer''', '''\u0120wider''', '''[UNK]''', ] UpperCamelCase : Tuple = dict(zip(__SCREAMING_SNAKE_CASE , range(len(__SCREAMING_SNAKE_CASE ) ) ) ) UpperCamelCase : Any = ['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', ''''''] UpperCamelCase : List[Any] = {'''unk_token''': '''[UNK]'''} UpperCamelCase : Union[str, Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) UpperCamelCase : Optional[int] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''merges_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(__SCREAMING_SNAKE_CASE ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(__SCREAMING_SNAKE_CASE ) ) def _lowercase ( self , **__SCREAMING_SNAKE_CASE ): """simple docstring""" kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname , **__SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : int = '''lower newer''' UpperCamelCase : Union[str, Any] = '''lower newer''' return input_text, output_text def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[str] = self.get_tokenizer() UpperCamelCase : int = '''lower newer''' UpperCamelCase : Union[str, Any] = ['''l''', '''o''', '''w''', '''er''', '''\u0120''', '''n''', '''e''', '''w''', '''er'''] UpperCamelCase : Tuple = tokenizer.tokenize(__SCREAMING_SNAKE_CASE ) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) UpperCamelCase : List[Any] = tokens + [tokenizer.unk_token] UpperCamelCase : Optional[int] = [0, 1, 2, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(tokenizer.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : int = self.get_tokenizer() UpperCamelCase : Optional[Any] = tokenizer('''Hello''' , '''World''' ) UpperCamelCase : List[str] = [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1] self.assertListEqual(tokd['''token_type_ids'''] , __SCREAMING_SNAKE_CASE ) @slow def _lowercase ( self ): """simple docstring""" UpperCamelCase : Union[str, Any] = self.tokenizer_class.from_pretrained('''microsoft/deberta-base''' ) UpperCamelCase : Optional[Any] = tokenizer.encode('''sequence builders''' , add_special_tokens=__SCREAMING_SNAKE_CASE ) UpperCamelCase : int = tokenizer.encode('''multi-sequence build''' , add_special_tokens=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Dict = tokenizer.encode( '''sequence builders''' , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = tokenizer.encode( '''sequence builders''' , '''multi-sequence build''' , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = tokenizer.build_inputs_with_special_tokens(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = tokenizer.build_inputs_with_special_tokens(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) assert encoded_sentence == encoded_text_from_decode assert encoded_pair == encoded_pair_from_decode @slow def _lowercase ( self ): """simple docstring""" UpperCamelCase : Optional[Any] = [self.tokenizer_class] if self.test_rust_tokenizer: tokenizer_classes.append(self.rust_tokenizer_class ) for tokenizer_class in tokenizer_classes: UpperCamelCase : Optional[int] = tokenizer_class.from_pretrained('''microsoft/deberta-base''' ) UpperCamelCase : str = [ '''ALBERT: A Lite BERT for Self-supervised Learning of Language Representations''', '''ALBERT incorporates two parameter reduction techniques''', '''The first one is a factorized embedding parameterization. By decomposing the large vocabulary''' ''' embedding matrix into two small matrices, we separate the size of the hidden layers from the size of''' ''' vocabulary embedding.''', ] UpperCamelCase : Union[str, Any] = tokenizer(__SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = [tokenizer.decode(__SCREAMING_SNAKE_CASE , skip_special_tokens=__SCREAMING_SNAKE_CASE ) for seq in encoding['''input_ids''']] # fmt: off UpperCamelCase : int = { '''input_ids''': [ [1, 2_118, 11_126, 565, 35, 83, 25_191, 163, 18_854, 13, 12_156, 12, 16_101, 25_376, 13_807, 9, 22_205, 27_893, 1_635, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 2_118, 11_126, 565, 24_536, 80, 43_797, 4_878, 7_373, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 133, 78, 65, 16, 10, 3_724, 1_538, 33_183, 11_303, 43_797, 1_938, 4, 870, 24_165, 29_105, 5, 739, 32_644, 33_183, 11_303, 36_173, 88, 80, 650, 7_821, 45_940, 6, 52, 2_559, 5, 1_836, 9, 5, 7_397, 13_171, 31, 5, 1_836, 9, 32_644, 33_183, 11_303, 4, 2] ], '''token_type_ids''': [ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ], '''attention_mask''': [ [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] ] } # fmt: on UpperCamelCase : List[str] = [ '''ALBERT: A Lite BERT for Self-supervised Learning of Language Representations''', '''ALBERT incorporates two parameter reduction techniques''', '''The first one is a factorized embedding parameterization. By decomposing the large vocabulary''' ''' embedding matrix into two small matrices, we separate the size of the hidden layers from the size of''' ''' vocabulary embedding.''', ] self.assertDictEqual(encoding.data , __SCREAMING_SNAKE_CASE ) for expected, decoded in zip(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE )
643
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_tokenizers_available, is_torch_available, is_vision_available, ) __UpperCAmelCase : Any = { "configuration_layoutlmv3": [ "LAYOUTLMV3_PRETRAINED_CONFIG_ARCHIVE_MAP", "LayoutLMv3Config", "LayoutLMv3OnnxConfig", ], "processing_layoutlmv3": ["LayoutLMv3Processor"], "tokenization_layoutlmv3": ["LayoutLMv3Tokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase : Optional[Any] = ["LayoutLMv3TokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase : Optional[int] = [ "LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST", "LayoutLMv3ForQuestionAnswering", "LayoutLMv3ForSequenceClassification", "LayoutLMv3ForTokenClassification", "LayoutLMv3Model", "LayoutLMv3PreTrainedModel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase : Any = [ "TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST", "TFLayoutLMv3ForQuestionAnswering", "TFLayoutLMv3ForSequenceClassification", "TFLayoutLMv3ForTokenClassification", "TFLayoutLMv3Model", "TFLayoutLMv3PreTrainedModel", ] try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase : Union[str, Any] = ["LayoutLMv3FeatureExtractor"] __UpperCAmelCase : Any = ["LayoutLMv3ImageProcessor"] if TYPE_CHECKING: from .configuration_layoutlmva import ( LAYOUTLMV3_PRETRAINED_CONFIG_ARCHIVE_MAP, LayoutLMvaConfig, LayoutLMvaOnnxConfig, ) from .processing_layoutlmva import LayoutLMvaProcessor from .tokenization_layoutlmva import LayoutLMvaTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_layoutlmva_fast import LayoutLMvaTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_layoutlmva import ( LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST, LayoutLMvaForQuestionAnswering, LayoutLMvaForSequenceClassification, LayoutLMvaForTokenClassification, LayoutLMvaModel, LayoutLMvaPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_layoutlmva import ( TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST, TFLayoutLMvaForQuestionAnswering, TFLayoutLMvaForSequenceClassification, TFLayoutLMvaForTokenClassification, TFLayoutLMvaModel, TFLayoutLMvaPreTrainedModel, ) try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_layoutlmva import LayoutLMvaFeatureExtractor from .image_processing_layoutlmva import LayoutLMvaImageProcessor else: import sys __UpperCAmelCase : Optional[int] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
643
import re from pathlib import Path from unittest import TestCase import pytest @pytest.mark.integration class UpperCAmelCase_ ( _a): '''simple docstring''' def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" with open(__SCREAMING_SNAKE_CASE , encoding='''utf-8''' ) as input_file: UpperCamelCase : str = re.compile(R'''(?!.*\b(?:encoding|rb|w|wb|w+|wb+|ab|ab+)\b)(?<=\s)(open)\((.*)\)''' ) UpperCamelCase : Optional[int] = input_file.read() UpperCamelCase : Union[str, Any] = regexp.search(__SCREAMING_SNAKE_CASE ) return match def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" with open(__SCREAMING_SNAKE_CASE , encoding='''utf-8''' ) as input_file: UpperCamelCase : Optional[int] = re.compile(R'''#[^\r\n]*print\(|\"[^\r\n]*print\(|\"\"\".*?print\(.*?\"\"\"|(print\()''' , re.DOTALL ) UpperCamelCase : Tuple = input_file.read() # use `re.finditer` to handle the case where the ignored groups would be matched first by `re.search` UpperCamelCase : Dict = regexp.finditer(__SCREAMING_SNAKE_CASE ) UpperCamelCase : int = [match for match in matches if match is not None and match.group(1 ) is not None] return matches[0] if matches else None def _lowercase ( self ): """simple docstring""" UpperCamelCase : int = Path('''./datasets''' ) UpperCamelCase : Tuple = list(dataset_paths.absolute().glob('''**/*.py''' ) ) for dataset in dataset_files: if self._no_encoding_on_file_open(str(__SCREAMING_SNAKE_CASE ) ): raise AssertionError(f"""open(...) must use utf-8 encoding in {dataset}""" ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Optional[int] = Path('''./datasets''' ) UpperCamelCase : Tuple = list(dataset_paths.absolute().glob('''**/*.py''' ) ) for dataset in dataset_files: if self._no_print_statements(str(__SCREAMING_SNAKE_CASE ) ): raise AssertionError(f"""print statement found in {dataset}. Use datasets.logger/logging instead.""" )
643
1
import inspect import os import sys import unittest import accelerate from accelerate.test_utils import execute_subprocess_async, require_tpu class UpperCAmelCase_ ( unittest.TestCase): '''simple docstring''' def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[str] = inspect.getfile(accelerate.test_utils ) UpperCamelCase : Tuple = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ['''scripts''', '''test_script.py'''] ) UpperCamelCase : Union[str, Any] = os.path.sep.join(inspect.getfile(self.__class__ ).split(os.path.sep )[:-1] ) @require_tpu def _lowercase ( self ): """simple docstring""" UpperCamelCase : Dict = f""" {self.test_dir}/xla_spawn.py --num_cores 8 {self.test_file_path} """.split() UpperCamelCase : Dict = [sys.executable] + distributed_args execute_subprocess_async(__SCREAMING_SNAKE_CASE , env=os.environ.copy() )
643
from __future__ import annotations import unittest from transformers import XGLMConfig, XGLMTokenizer, 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 tensorflow as tf from transformers.models.xglm.modeling_tf_xglm import ( TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFXGLMForCausalLM, TFXGLMModel, ) @require_tf class UpperCAmelCase_ : '''simple docstring''' __UpperCamelCase : Any = XGLMConfig __UpperCamelCase : Dict = {} __UpperCamelCase : List[str] = "gelu" def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=14 , __SCREAMING_SNAKE_CASE=7 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=99 , __SCREAMING_SNAKE_CASE=32 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=4 , __SCREAMING_SNAKE_CASE=37 , __SCREAMING_SNAKE_CASE="gelu" , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=512 , __SCREAMING_SNAKE_CASE=0.02 , ): """simple docstring""" UpperCamelCase : Any = parent UpperCamelCase : Optional[int] = batch_size UpperCamelCase : str = seq_length UpperCamelCase : List[str] = is_training UpperCamelCase : Tuple = use_input_mask UpperCamelCase : Union[str, Any] = use_labels UpperCamelCase : int = vocab_size UpperCamelCase : Optional[int] = d_model UpperCamelCase : Any = num_hidden_layers UpperCamelCase : List[str] = num_attention_heads UpperCamelCase : Optional[Any] = ffn_dim UpperCamelCase : Optional[int] = activation_function UpperCamelCase : List[str] = activation_dropout UpperCamelCase : Any = attention_dropout UpperCamelCase : str = max_position_embeddings UpperCamelCase : Union[str, Any] = initializer_range UpperCamelCase : int = None UpperCamelCase : Dict = 0 UpperCamelCase : int = 2 UpperCamelCase : Any = 1 def _lowercase ( self ): """simple docstring""" return XGLMConfig.from_pretrained('''facebook/xglm-564M''' ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Optional[int] = tf.clip_by_value( ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) , clip_value_min=0 , clip_value_max=3 ) UpperCamelCase : int = None if self.use_input_mask: UpperCamelCase : Any = random_attention_mask([self.batch_size, self.seq_length] ) UpperCamelCase : Tuple = self.get_config() UpperCamelCase : str = floats_tensor([self.num_hidden_layers, self.num_attention_heads] , 2 ) return ( config, input_ids, input_mask, head_mask, ) def _lowercase ( self ): """simple docstring""" return XGLMConfig( vocab_size=self.vocab_size , d_model=self.hidden_size , num_layers=self.num_hidden_layers , attention_heads=self.num_attention_heads , ffn_dim=self.ffn_dim , activation_function=self.activation_function , activation_dropout=self.activation_dropout , attention_dropout=self.attention_dropout , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , use_cache=__SCREAMING_SNAKE_CASE , bos_token_id=self.bos_token_id , eos_token_id=self.eos_token_id , pad_token_id=self.pad_token_id , return_dict=__SCREAMING_SNAKE_CASE , ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[str] = self.prepare_config_and_inputs() ( ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ) : Dict = config_and_inputs UpperCamelCase : List[str] = { '''input_ids''': input_ids, '''head_mask''': head_mask, } return config, inputs_dict @require_tf class UpperCAmelCase_ ( _a, _a, unittest.TestCase): '''simple docstring''' __UpperCamelCase : Optional[Any] = (TFXGLMModel, TFXGLMForCausalLM) if is_tf_available() else () __UpperCamelCase : Union[str, Any] = (TFXGLMForCausalLM,) if is_tf_available() else () __UpperCamelCase : Any = ( {"feature-extraction": TFXGLMModel, "text-generation": TFXGLMForCausalLM} if is_tf_available() else {} ) __UpperCamelCase : Optional[int] = False __UpperCamelCase : List[Any] = False __UpperCamelCase : List[Any] = False def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[Any] = TFXGLMModelTester(self ) UpperCamelCase : Any = ConfigTester(self , config_class=__SCREAMING_SNAKE_CASE , n_embd=37 ) def _lowercase ( self ): """simple docstring""" self.config_tester.run_common_tests() @slow def _lowercase ( self ): """simple docstring""" for model_name in TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCamelCase : List[Any] = TFXGLMModel.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsNotNone(__SCREAMING_SNAKE_CASE ) @unittest.skip(reason='''Currently, model embeddings are going to undergo a major refactor.''' ) def _lowercase ( self ): """simple docstring""" super().test_resize_token_embeddings() @require_tf class UpperCAmelCase_ ( unittest.TestCase): '''simple docstring''' @slow def _lowercase ( self , __SCREAMING_SNAKE_CASE=True ): """simple docstring""" UpperCamelCase : List[str] = TFXGLMForCausalLM.from_pretrained('''facebook/xglm-564M''' ) UpperCamelCase : List[Any] = tf.convert_to_tensor([[2, 268, 9_865]] , dtype=tf.intaa ) # The dog # </s> The dog is a very friendly dog. He is very affectionate and loves to play with other # fmt: off UpperCamelCase : str = [2, 268, 9_865, 67, 11, 1_988, 57_252, 9_865, 5, 984, 67, 1_988, 213_838, 1_658, 53, 70_446, 33, 6_657, 278, 1_581] # fmt: on UpperCamelCase : Union[str, Any] = model.generate(__SCREAMING_SNAKE_CASE , do_sample=__SCREAMING_SNAKE_CASE , num_beams=1 ) if verify_outputs: self.assertListEqual(output_ids[0].numpy().tolist() , __SCREAMING_SNAKE_CASE ) @slow def _lowercase ( self ): """simple docstring""" UpperCamelCase : str = XGLMTokenizer.from_pretrained('''facebook/xglm-564M''' ) UpperCamelCase : List[str] = TFXGLMForCausalLM.from_pretrained('''facebook/xglm-564M''' ) tf.random.set_seed(0 ) UpperCamelCase : Tuple = tokenizer('''Today is a nice day and''' , return_tensors='''tf''' ) UpperCamelCase : int = tokenized.input_ids # forces the generation to happen on CPU, to avoid GPU-related quirks (and assure same output regardless of the available devices) with tf.device(''':/CPU:0''' ): UpperCamelCase : str = model.generate(__SCREAMING_SNAKE_CASE , do_sample=__SCREAMING_SNAKE_CASE , seed=[7, 0] ) UpperCamelCase : Dict = tokenizer.decode(output_ids[0] , skip_special_tokens=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = ( '''Today is a nice day and warm evening here over Southern Alberta!! Today when they closed schools due''' ) self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) @slow def _lowercase ( self ): """simple docstring""" UpperCamelCase : Dict = TFXGLMForCausalLM.from_pretrained('''facebook/xglm-564M''' ) UpperCamelCase : Tuple = XGLMTokenizer.from_pretrained('''facebook/xglm-564M''' ) UpperCamelCase : Tuple = '''left''' # use different length sentences to test batching UpperCamelCase : Any = [ '''This is an extremelly long sentence that only exists to test the ability of the model to cope with ''' '''left-padding, such as in batched generation. The output for the sequence below should be the same ''' '''regardless of whether left padding is applied or not. When''', '''Hello, my dog is a little''', ] UpperCamelCase : List[Any] = tokenizer(__SCREAMING_SNAKE_CASE , return_tensors='''tf''' , padding=__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = inputs['''input_ids'''] UpperCamelCase : Optional[int] = model.generate(input_ids=__SCREAMING_SNAKE_CASE , attention_mask=inputs['''attention_mask'''] , max_new_tokens=12 ) UpperCamelCase : Optional[int] = tokenizer(sentences[0] , return_tensors='''tf''' ).input_ids UpperCamelCase : Optional[Any] = model.generate(input_ids=__SCREAMING_SNAKE_CASE , max_new_tokens=12 ) UpperCamelCase : str = tokenizer(sentences[1] , return_tensors='''tf''' ).input_ids UpperCamelCase : List[Any] = model.generate(input_ids=__SCREAMING_SNAKE_CASE , max_new_tokens=12 ) UpperCamelCase : Any = tokenizer.batch_decode(__SCREAMING_SNAKE_CASE , skip_special_tokens=__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = tokenizer.decode(output_non_padded[0] , skip_special_tokens=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Tuple = tokenizer.decode(output_padded[0] , skip_special_tokens=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = [ '''This is an extremelly long sentence that only exists to test the ability of the model to cope with ''' '''left-padding, such as in batched generation. The output for the sequence below should be the same ''' '''regardless of whether left padding is applied or not. When left padding is applied, the sequence will be ''' '''a single''', '''Hello, my dog is a little bit of a shy one, but he is very friendly''', ] self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) self.assertListEqual(__SCREAMING_SNAKE_CASE , [non_padded_sentence, padded_sentence] )
643
1
def a ( SCREAMING_SNAKE_CASE_ : float , SCREAMING_SNAKE_CASE_ : float ): """simple docstring""" return price * (1 + tax_rate) if __name__ == "__main__": print(f'''{price_plus_tax(100, 0.25) = }''') print(f'''{price_plus_tax(125.50, 0.05) = }''')
643
import os from shutil import copyfile from typing import List, Optional, Tuple from tokenizers import processors from ...tokenization_utils import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_mbart import MBartTokenizer else: __UpperCAmelCase : Optional[int] = None __UpperCAmelCase : int = logging.get_logger(__name__) __UpperCAmelCase : List[Any] = {"vocab_file": "sentencepiece.bpe.model", "tokenizer_file": "tokenizer.json"} __UpperCAmelCase : str = { "vocab_file": { "facebook/mbart-large-en-ro": ( "https://huggingface.co/facebook/mbart-large-en-ro/resolve/main/sentencepiece.bpe.model" ), "facebook/mbart-large-cc25": ( "https://huggingface.co/facebook/mbart-large-cc25/resolve/main/sentencepiece.bpe.model" ), }, "tokenizer_file": { "facebook/mbart-large-en-ro": "https://huggingface.co/facebook/mbart-large-en-ro/resolve/main/tokenizer.json", "facebook/mbart-large-cc25": "https://huggingface.co/facebook/mbart-large-cc25/resolve/main/tokenizer.json", }, } __UpperCAmelCase : Union[str, Any] = { "facebook/mbart-large-en-ro": 1024, "facebook/mbart-large-cc25": 1024, } # fmt: off __UpperCAmelCase : Any = ["ar_AR", "cs_CZ", "de_DE", "en_XX", "es_XX", "et_EE", "fi_FI", "fr_XX", "gu_IN", "hi_IN", "it_IT", "ja_XX", "kk_KZ", "ko_KR", "lt_LT", "lv_LV", "my_MM", "ne_NP", "nl_XX", "ro_RO", "ru_RU", "si_LK", "tr_TR", "vi_VN", "zh_CN"] class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : List[str] = VOCAB_FILES_NAMES __UpperCamelCase : str = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCamelCase : Tuple = PRETRAINED_VOCAB_FILES_MAP __UpperCamelCase : Union[str, Any] = ["input_ids", "attention_mask"] __UpperCamelCase : Any = MBartTokenizer __UpperCamelCase : List[int] = [] __UpperCamelCase : List[int] = [] def __init__( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE="<s>" , __SCREAMING_SNAKE_CASE="</s>" , __SCREAMING_SNAKE_CASE="</s>" , __SCREAMING_SNAKE_CASE="<s>" , __SCREAMING_SNAKE_CASE="<unk>" , __SCREAMING_SNAKE_CASE="<pad>" , __SCREAMING_SNAKE_CASE="<mask>" , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" UpperCamelCase : Union[str, Any] = AddedToken(__SCREAMING_SNAKE_CASE , lstrip=__SCREAMING_SNAKE_CASE , rstrip=__SCREAMING_SNAKE_CASE ) if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else mask_token super().__init__( vocab_file=__SCREAMING_SNAKE_CASE , tokenizer_file=__SCREAMING_SNAKE_CASE , bos_token=__SCREAMING_SNAKE_CASE , eos_token=__SCREAMING_SNAKE_CASE , sep_token=__SCREAMING_SNAKE_CASE , cls_token=__SCREAMING_SNAKE_CASE , unk_token=__SCREAMING_SNAKE_CASE , pad_token=__SCREAMING_SNAKE_CASE , mask_token=__SCREAMING_SNAKE_CASE , src_lang=__SCREAMING_SNAKE_CASE , tgt_lang=__SCREAMING_SNAKE_CASE , additional_special_tokens=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) UpperCamelCase : Dict = vocab_file UpperCamelCase : List[str] = False if not self.vocab_file else True UpperCamelCase : List[Any] = FAIRSEQ_LANGUAGE_CODES.copy() if additional_special_tokens is not None: # Only add those special tokens if they are not already there. _additional_special_tokens.extend( [t for t in additional_special_tokens if t not in _additional_special_tokens] ) self.add_special_tokens({'''additional_special_tokens''': _additional_special_tokens} ) UpperCamelCase : List[Any] = { lang_code: self.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) for lang_code in FAIRSEQ_LANGUAGE_CODES } UpperCamelCase : Dict = src_lang if src_lang is not None else '''en_XX''' UpperCamelCase : List[Any] = self.convert_tokens_to_ids(self._src_lang ) UpperCamelCase : str = tgt_lang self.set_src_lang_special_tokens(self._src_lang ) @property def _lowercase ( self ): """simple docstring""" return self._src_lang @src_lang.setter def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Union[str, Any] = new_src_lang self.set_src_lang_special_tokens(self._src_lang ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ): """simple docstring""" if token_ids_a is None: return self.prefix_tokens + token_ids_a + self.suffix_tokens # We don't expect to process pairs, but leave the pair logic for API consistency return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ): """simple docstring""" UpperCamelCase : str = [self.sep_token_id] UpperCamelCase : Union[str, Any] = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): """simple docstring""" if src_lang is None or tgt_lang is None: raise ValueError('''Translation requires a `src_lang` and a `tgt_lang` for this model''' ) UpperCamelCase : List[str] = src_lang UpperCamelCase : Dict = self(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , return_tensors=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[Any] = self.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Any = tgt_lang_id return inputs def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = "en_XX" , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = "ro_RO" , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" UpperCamelCase : Optional[Any] = src_lang UpperCamelCase : Optional[int] = tgt_lang return super().prepare_seqaseq_batch(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" return self.set_src_lang_special_tokens(self.src_lang ) def _lowercase ( self ): """simple docstring""" return self.set_tgt_lang_special_tokens(self.tgt_lang ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Optional[int] = self.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Any = [] UpperCamelCase : Dict = [self.eos_token_id, self.cur_lang_code] UpperCamelCase : Union[str, Any] = self.convert_ids_to_tokens(self.prefix_tokens ) UpperCamelCase : int = self.convert_ids_to_tokens(self.suffix_tokens ) UpperCamelCase : Tuple = processors.TemplateProcessing( single=prefix_tokens_str + ['''$A'''] + suffix_tokens_str , pair=prefix_tokens_str + ['''$A''', '''$B'''] + suffix_tokens_str , special_tokens=list(zip(prefix_tokens_str + suffix_tokens_str , self.prefix_tokens + self.suffix_tokens ) ) , ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Optional[int] = self.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) UpperCamelCase : int = [] UpperCamelCase : Optional[Any] = [self.eos_token_id, self.cur_lang_code] UpperCamelCase : Optional[Any] = self.convert_ids_to_tokens(self.prefix_tokens ) UpperCamelCase : List[str] = self.convert_ids_to_tokens(self.suffix_tokens ) UpperCamelCase : Optional[int] = processors.TemplateProcessing( single=prefix_tokens_str + ['''$A'''] + suffix_tokens_str , pair=prefix_tokens_str + ['''$A''', '''$B'''] + suffix_tokens_str , special_tokens=list(zip(prefix_tokens_str + suffix_tokens_str , self.prefix_tokens + self.suffix_tokens ) ) , ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ): """simple docstring""" if not self.can_save_slow_tokenizer: raise ValueError( '''Your fast tokenizer does not have the necessary information to save the vocabulary for a slow ''' '''tokenizer.''' ) if not os.path.isdir(__SCREAMING_SNAKE_CASE ): logger.error(f"""Vocabulary path ({save_directory}) should be a directory.""" ) return UpperCamelCase : Optional[int] = os.path.join( __SCREAMING_SNAKE_CASE , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(__SCREAMING_SNAKE_CASE ): copyfile(self.vocab_file , __SCREAMING_SNAKE_CASE ) return (out_vocab_file,)
643
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available __UpperCAmelCase : Dict = { "configuration_squeezebert": [ "SQUEEZEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP", "SqueezeBertConfig", "SqueezeBertOnnxConfig", ], "tokenization_squeezebert": ["SqueezeBertTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase : Union[str, Any] = ["SqueezeBertTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase : Union[str, Any] = [ "SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST", "SqueezeBertForMaskedLM", "SqueezeBertForMultipleChoice", "SqueezeBertForQuestionAnswering", "SqueezeBertForSequenceClassification", "SqueezeBertForTokenClassification", "SqueezeBertModel", "SqueezeBertModule", "SqueezeBertPreTrainedModel", ] if TYPE_CHECKING: from .configuration_squeezebert import ( SQUEEZEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, SqueezeBertConfig, SqueezeBertOnnxConfig, ) from .tokenization_squeezebert import SqueezeBertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_squeezebert_fast import SqueezeBertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_squeezebert import ( SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST, SqueezeBertForMaskedLM, SqueezeBertForMultipleChoice, SqueezeBertForQuestionAnswering, SqueezeBertForSequenceClassification, SqueezeBertForTokenClassification, SqueezeBertModel, SqueezeBertModule, SqueezeBertPreTrainedModel, ) else: import sys __UpperCAmelCase : Union[str, Any] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
643
import unittest import numpy as np import torch from diffusers import VersatileDiffusionImageVariationPipeline from diffusers.utils.testing_utils import load_image, require_torch_gpu, slow, torch_device __UpperCAmelCase : Dict = False class UpperCAmelCase_ ( unittest.TestCase): '''simple docstring''' pass @slow @require_torch_gpu class UpperCAmelCase_ ( unittest.TestCase): '''simple docstring''' def _lowercase ( self ): """simple docstring""" UpperCamelCase : Dict = VersatileDiffusionImageVariationPipeline.from_pretrained('''shi-labs/versatile-diffusion''' ) pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg''' ) UpperCamelCase : str = torch.manual_seed(0 ) UpperCamelCase : Union[str, Any] = pipe( image=__SCREAMING_SNAKE_CASE , generator=__SCREAMING_SNAKE_CASE , guidance_scale=7.5 , num_inference_steps=50 , output_type='''numpy''' , ).images UpperCamelCase : List[Any] = image[0, 253:256, 253:256, -1] assert image.shape == (1, 512, 512, 3) UpperCamelCase : Dict = np.array([0.0_441, 0.0_469, 0.0_507, 0.0_575, 0.0_632, 0.0_650, 0.0_865, 0.0_909, 0.0_945] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
643
1
import argparse import os from pathlib import Path from typing import Dict import tensorflow as tf import torch from tqdm import tqdm from transformers import PegasusConfig, PegasusForConditionalGeneration, PegasusTokenizer from transformers.models.pegasus.configuration_pegasus import DEFAULTS, task_specific_params __UpperCAmelCase : Dict = [ # replace left string with right string to get the relevant state_dict key (identical state dict to bart) ["memory_attention", "encoder_attn"], ["attention", "attn"], ["/", "."], [".LayerNorm.gamma", "_layer_norm.weight"], [".LayerNorm.beta", "_layer_norm.bias"], ["r.layer_", "r.layers."], ["output_proj", "out_proj"], ["ffn.dense_1.", "fc2."], ["ffn.dense.", "fc1."], ["ffn_layer_norm", "final_layer_norm"], ["kernel", "weight"], ["encoder_layer_norm.", "encoder.layer_norm."], ["decoder_layer_norm.", "decoder.layer_norm."], ["embeddings.weights", "shared.weight"], ] def a ( SCREAMING_SNAKE_CASE_ : Dict ): """simple docstring""" for pegasus_name, hf_name in PATTERNS: UpperCamelCase : str = k.replace(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) return k def a ( SCREAMING_SNAKE_CASE_ : dict , SCREAMING_SNAKE_CASE_ : dict ): """simple docstring""" UpperCamelCase : Optional[int] = DEFAULTS.copy() cfg_kwargs.update(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : int = PegasusConfig(**SCREAMING_SNAKE_CASE_ ) UpperCamelCase : int = PegasusForConditionalGeneration(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : str = torch_model.model.state_dict() UpperCamelCase : Optional[Any] = {} for k, v in tf_weights.items(): UpperCamelCase : str = rename_state_dict_key(SCREAMING_SNAKE_CASE_ ) if new_k not in sd: raise ValueError(F"""could not find new key {new_k} in state dict. (converted from {k})""" ) if "dense" in k or "proj" in new_k: UpperCamelCase : List[Any] = v.T UpperCamelCase : Any = torch.tensor(SCREAMING_SNAKE_CASE_ , dtype=sd[new_k].dtype ) assert v.shape == sd[new_k].shape, F"""{new_k}, {k}, {v.shape}, {sd[new_k].shape}""" # make sure embedding.padding_idx is respected UpperCamelCase : Union[str, Any] = torch.zeros_like(mapping['''shared.weight'''][cfg.pad_token_id + 1] ) UpperCamelCase : Union[str, Any] = mapping['''shared.weight'''] UpperCamelCase : Union[str, Any] = mapping['''shared.weight'''] UpperCamelCase : List[str] = {k: torch.zeros_like(SCREAMING_SNAKE_CASE_ ) for k, v in sd.items() if k.endswith('''bias''' ) and k not in mapping} mapping.update(**SCREAMING_SNAKE_CASE_ ) UpperCamelCase , UpperCamelCase : str = torch_model.model.load_state_dict(SCREAMING_SNAKE_CASE_ , strict=SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Union[str, Any] = [ k for k in missing if k not in ['''encoder.embed_positions.weight''', '''decoder.embed_positions.weight'''] ] assert unexpected_missing == [], F"""no matches found for the following torch keys {unexpected_missing}""" assert extra == [], F"""no matches found for the following tf keys {extra}""" return torch_model def a ( SCREAMING_SNAKE_CASE_ : Any="./ckpt/aeslc/model.ckpt-32000" ): """simple docstring""" UpperCamelCase : Optional[Any] = tf.train.list_variables(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : List[Any] = {} UpperCamelCase : Union[str, Any] = ['''Adafactor''', '''global_step'''] for name, shape in tqdm(SCREAMING_SNAKE_CASE_ , desc='''converting tf checkpoint to dict''' ): UpperCamelCase : Dict = any(pat in name for pat in ignore_name ) if skip_key: continue UpperCamelCase : Union[str, Any] = tf.train.load_variable(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Any = array return tf_weights def a ( SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : str ): """simple docstring""" UpperCamelCase : Any = Path(SCREAMING_SNAKE_CASE_ ).parent.name UpperCamelCase : Tuple = task_specific_params[F"""summarization_{dataset}"""]['''max_position_embeddings'''] UpperCamelCase : str = PegasusTokenizer.from_pretrained('''sshleifer/pegasus''' , model_max_length=SCREAMING_SNAKE_CASE_ ) assert tok.model_max_length == desired_max_model_length tok.save_pretrained(SCREAMING_SNAKE_CASE_ ) # convert model UpperCamelCase : Any = get_tf_weights_as_numpy(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : int = task_specific_params[F"""summarization_{dataset}"""] if dataset == "large": UpperCamelCase : Any = task_specific_params UpperCamelCase : Optional[int] = convert_pegasus(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) torch_model.save_pretrained(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Tuple = torch_model.state_dict() sd.pop('''model.decoder.embed_positions.weight''' ) sd.pop('''model.encoder.embed_positions.weight''' ) torch.save(SCREAMING_SNAKE_CASE_ , Path(SCREAMING_SNAKE_CASE_ ) / '''pytorch_model.bin''' ) if __name__ == "__main__": __UpperCAmelCase : Tuple = argparse.ArgumentParser() # Required parameters parser.add_argument("tf_ckpt_path", type=str, help="passed to tf.train.list_variables") parser.add_argument("save_dir", default=None, type=str, help="Path to the output PyTorch model.") __UpperCAmelCase : List[Any] = parser.parse_args() if args.save_dir is None: __UpperCAmelCase : Optional[int] = Path(args.tf_ckpt_path).parent.name __UpperCAmelCase : Optional[Any] = os.path.join("pegasus", dataset) convert_pegasus_ckpt_to_pytorch(args.tf_ckpt_path, args.save_dir)
643
import copy from typing import Any, Dict, List, Optional, Union import numpy as np 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 __UpperCAmelCase : Dict = logging.get_logger(__name__) class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : Any = ["input_features"] def __init__( self , __SCREAMING_SNAKE_CASE=80 , __SCREAMING_SNAKE_CASE=16_000 , __SCREAMING_SNAKE_CASE=160 , __SCREAMING_SNAKE_CASE=30 , __SCREAMING_SNAKE_CASE=400 , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=False , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" super().__init__( feature_size=__SCREAMING_SNAKE_CASE , sampling_rate=__SCREAMING_SNAKE_CASE , padding_value=__SCREAMING_SNAKE_CASE , return_attention_mask=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) UpperCamelCase : List[str] = n_fft UpperCamelCase : Dict = hop_length UpperCamelCase : Dict = chunk_length UpperCamelCase : List[str] = chunk_length * sampling_rate UpperCamelCase : Dict = self.n_samples // hop_length UpperCamelCase : str = sampling_rate UpperCamelCase : Union[str, Any] = mel_filter_bank( num_frequency_bins=1 + n_fft // 2 , num_mel_filters=__SCREAMING_SNAKE_CASE , min_frequency=0.0 , max_frequency=8_000.0 , sampling_rate=__SCREAMING_SNAKE_CASE , norm='''slaney''' , mel_scale='''slaney''' , ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : List[str] = spectrogram( __SCREAMING_SNAKE_CASE , window_function(self.n_fft , '''hann''' ) , frame_length=self.n_fft , hop_length=self.hop_length , power=2.0 , mel_filters=self.mel_filters , log_mel='''log10''' , ) UpperCamelCase : int = log_spec[:, :-1] UpperCamelCase : int = np.maximum(__SCREAMING_SNAKE_CASE , log_spec.max() - 8.0 ) UpperCamelCase : Any = (log_spec + 4.0) / 4.0 return log_spec @staticmethod # Copied from transformers.models.wav2vec2.feature_extraction_wav2vec2.Wav2Vec2FeatureExtractor.zero_mean_unit_var_norm def _lowercase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = 0.0 ): """simple docstring""" if attention_mask is not None: UpperCamelCase : List[Any] = np.array(__SCREAMING_SNAKE_CASE , np.intaa ) UpperCamelCase : Optional[Any] = [] for vector, length in zip(__SCREAMING_SNAKE_CASE , attention_mask.sum(-1 ) ): UpperCamelCase : Optional[Any] = (vector - vector[:length].mean()) / np.sqrt(vector[:length].var() + 1e-7 ) if length < normed_slice.shape[0]: UpperCamelCase : Optional[int] = padding_value normed_input_values.append(__SCREAMING_SNAKE_CASE ) else: UpperCamelCase : Union[str, Any] = [(x - x.mean()) / np.sqrt(x.var() + 1e-7 ) for x in input_values] return normed_input_values def __call__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = True , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = "max_length" , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( f"""The model corresponding to this feature extractor: {self.__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 : Tuple = isinstance(__SCREAMING_SNAKE_CASE , np.ndarray ) and len(raw_speech.shape ) > 1 if is_batched_numpy and len(raw_speech.shape ) > 2: raise ValueError(f"""Only mono-channel audio is supported for input to {self}""" ) UpperCamelCase : Union[str, Any] = is_batched_numpy or ( isinstance(__SCREAMING_SNAKE_CASE , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: UpperCamelCase : List[Any] = [np.asarray([speech] , dtype=np.floataa ).T for speech in raw_speech] elif not is_batched and not isinstance(__SCREAMING_SNAKE_CASE , np.ndarray ): UpperCamelCase : int = np.asarray(__SCREAMING_SNAKE_CASE , dtype=np.floataa ) elif isinstance(__SCREAMING_SNAKE_CASE , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): UpperCamelCase : Union[str, Any] = raw_speech.astype(np.floataa ) # always return batch if not is_batched: UpperCamelCase : Optional[int] = [np.asarray([raw_speech] ).T] UpperCamelCase : Optional[int] = BatchFeature({'''input_features''': raw_speech} ) # convert into correct format for padding UpperCamelCase : Optional[Any] = self.pad( __SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE , max_length=max_length if max_length else self.n_samples , truncation=__SCREAMING_SNAKE_CASE , pad_to_multiple_of=__SCREAMING_SNAKE_CASE , return_attention_mask=return_attention_mask or do_normalize , ) # zero-mean and unit-variance normalization if do_normalize: UpperCamelCase : Optional[Any] = self.zero_mean_unit_var_norm( padded_inputs['''input_features'''] , attention_mask=padded_inputs['''attention_mask'''] , padding_value=self.padding_value , ) UpperCamelCase : List[str] = np.stack(padded_inputs['''input_features'''] , axis=0 ) # make sure list is in array format UpperCamelCase : Dict = padded_inputs.get('''input_features''' ).transpose(2 , 0 , 1 ) UpperCamelCase : Tuple = [self._np_extract_fbank_features(__SCREAMING_SNAKE_CASE ) for waveform in input_features[0]] if isinstance(input_features[0] , __SCREAMING_SNAKE_CASE ): UpperCamelCase : Optional[int] = [np.asarray(__SCREAMING_SNAKE_CASE , dtype=np.floataa ) for feature in input_features] else: UpperCamelCase : Dict = input_features if return_attention_mask: # rescale from sample (48000) to feature (3000) UpperCamelCase : Union[str, Any] = padded_inputs['''attention_mask'''][:, :: self.hop_length] if return_tensors is not None: UpperCamelCase : Dict = padded_inputs.convert_to_tensors(__SCREAMING_SNAKE_CASE ) return padded_inputs def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[Any] = copy.deepcopy(self.__dict__ ) UpperCamelCase : List[str] = self.__class__.__name__ if "mel_filters" in output: del output["mel_filters"] return output
643
1
def a ( SCREAMING_SNAKE_CASE_ : int = 5_0 ): """simple docstring""" UpperCamelCase : Dict = [1] * (length + 1) for row_length in range(length + 1 ): for tile_length in range(2 , 5 ): for tile_start in range(row_length - tile_length + 1 ): ways_number[row_length] += ways_number[ row_length - tile_start - tile_length ] return ways_number[length] if __name__ == "__main__": print(f'''{solution() = }''')
643
import json from typing import List, Optional, Tuple from tokenizers import normalizers from tokenizers.pre_tokenizers import BertPreTokenizer, PreTokenizer from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_roformer import RoFormerTokenizer from .tokenization_utils import JiebaPreTokenizer __UpperCAmelCase : Dict = logging.get_logger(__name__) __UpperCAmelCase : Optional[Any] = {"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"} __UpperCAmelCase : Dict = { "vocab_file": { "junnyu/roformer_chinese_small": "https://huggingface.co/junnyu/roformer_chinese_small/resolve/main/vocab.txt", "junnyu/roformer_chinese_base": "https://huggingface.co/junnyu/roformer_chinese_base/resolve/main/vocab.txt", "junnyu/roformer_chinese_char_small": ( "https://huggingface.co/junnyu/roformer_chinese_char_small/resolve/main/vocab.txt" ), "junnyu/roformer_chinese_char_base": ( "https://huggingface.co/junnyu/roformer_chinese_char_base/resolve/main/vocab.txt" ), "junnyu/roformer_small_discriminator": ( "https://huggingface.co/junnyu/roformer_small_discriminator/resolve/main/vocab.txt" ), "junnyu/roformer_small_generator": ( "https://huggingface.co/junnyu/roformer_small_generator/resolve/main/vocab.txt" ), } } __UpperCAmelCase : Tuple = { "junnyu/roformer_chinese_small": 1536, "junnyu/roformer_chinese_base": 1536, "junnyu/roformer_chinese_char_small": 512, "junnyu/roformer_chinese_char_base": 512, "junnyu/roformer_small_discriminator": 128, "junnyu/roformer_small_generator": 128, } __UpperCAmelCase : Any = { "junnyu/roformer_chinese_small": {"do_lower_case": True}, "junnyu/roformer_chinese_base": {"do_lower_case": True}, "junnyu/roformer_chinese_char_small": {"do_lower_case": True}, "junnyu/roformer_chinese_char_base": {"do_lower_case": True}, "junnyu/roformer_small_discriminator": {"do_lower_case": True}, "junnyu/roformer_small_generator": {"do_lower_case": True}, } class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : str = VOCAB_FILES_NAMES __UpperCamelCase : Optional[int] = PRETRAINED_VOCAB_FILES_MAP __UpperCamelCase : Union[str, Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCamelCase : int = PRETRAINED_INIT_CONFIGURATION __UpperCamelCase : Any = RoFormerTokenizer def __init__( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE="[UNK]" , __SCREAMING_SNAKE_CASE="[SEP]" , __SCREAMING_SNAKE_CASE="[PAD]" , __SCREAMING_SNAKE_CASE="[CLS]" , __SCREAMING_SNAKE_CASE="[MASK]" , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" super().__init__( __SCREAMING_SNAKE_CASE , tokenizer_file=__SCREAMING_SNAKE_CASE , do_lower_case=__SCREAMING_SNAKE_CASE , unk_token=__SCREAMING_SNAKE_CASE , sep_token=__SCREAMING_SNAKE_CASE , pad_token=__SCREAMING_SNAKE_CASE , cls_token=__SCREAMING_SNAKE_CASE , mask_token=__SCREAMING_SNAKE_CASE , tokenize_chinese_chars=__SCREAMING_SNAKE_CASE , strip_accents=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) UpperCamelCase : List[Any] = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( pre_tok_state.get('''lowercase''' , __SCREAMING_SNAKE_CASE ) != do_lower_case or pre_tok_state.get('''strip_accents''' , __SCREAMING_SNAKE_CASE ) != strip_accents ): UpperCamelCase : List[Any] = getattr(__SCREAMING_SNAKE_CASE , pre_tok_state.pop('''type''' ) ) UpperCamelCase : Optional[int] = do_lower_case UpperCamelCase : Optional[Any] = strip_accents UpperCamelCase : List[Any] = pre_tok_class(**__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[Any] = do_lower_case def __getstate__( self ): """simple docstring""" UpperCamelCase : Optional[int] = self.__dict__.copy() UpperCamelCase : Any = BertPreTokenizer() return state def __setstate__( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Any = d UpperCamelCase : List[str] = self.__dict__['''_tokenizer'''].get_vocab() UpperCamelCase : Any = PreTokenizer.custom(JiebaPreTokenizer(__SCREAMING_SNAKE_CASE ) ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None ): """simple docstring""" UpperCamelCase : Any = [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 _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ): """simple docstring""" UpperCamelCase : Dict = [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 ) * [0] + len(token_ids_a + sep ) * [1] def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ): """simple docstring""" UpperCamelCase : List[Any] = self._tokenizer.model.save(__SCREAMING_SNAKE_CASE , name=__SCREAMING_SNAKE_CASE ) return tuple(__SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=False , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" UpperCamelCase : Any = BertPreTokenizer() return super().save_pretrained(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE )
643
1
import random import torch from huggingface_hub import HfApi from diffusers import UNetaDModel __UpperCAmelCase : Optional[Any] = HfApi() __UpperCAmelCase : int = {} # fmt: off __UpperCAmelCase : List[Any] = torch.tensor([ -0.7_515, -1.6_883, 0.2_420, 0.0_300, 0.6_347, 1.3_433, -1.1_743, -3.7_467, 1.2_342, -2.2_485, 0.4_636, 0.8_076, -0.7_991, 0.3_969, 0.8_498, 0.9_189, -1.8_887, -3.3_522, 0.7_639, 0.2_040, 0.6_271, -2.7_148, -1.6_316, 3.0_839, 0.3_186, 0.2_721, -0.9_759, -1.2_461, 2.6_257, 1.3_557 ]) __UpperCAmelCase : Optional[int] = torch.tensor([ -2.3_639, -2.5_344, 0.0_054, -0.6_674, 1.5_990, 1.0_158, 0.3_124, -2.1_436, 1.8_795, -2.5_429, -0.1_566, -0.3_973, 1.2_490, 2.6_447, 1.2_283, -0.5_208, -2.8_154, -3.5_119, 2.3_838, 1.2_033, 1.7_201, -2.1_256, -1.4_576, 2.7_948, 2.4_204, -0.9_752, -1.2_546, 0.8_027, 3.2_758, 3.1_365 ]) __UpperCAmelCase : str = torch.tensor([ -0.6_531, -0.6_891, -0.3_172, -0.5_375, -0.9_140, -0.5_367, -0.1_175, -0.7_869, -0.3_808, -0.4_513, -0.2_098, -0.0_083, 0.3_183, 0.5_140, 0.2_247, -0.1_304, -0.1_302, -0.2_802, -0.2_084, -0.2_025, -0.4_967, -0.4_873, -0.0_861, 0.6_925, 0.0_250, 0.1_290, -0.1_543, 0.6_316, 1.0_460, 1.4_943 ]) __UpperCAmelCase : Optional[Any] = torch.tensor([ 0.0_911, 0.1_107, 0.0_182, 0.0_435, -0.0_805, -0.0_608, 0.0_381, 0.2_172, -0.0_280, 0.1_327, -0.0_299, -0.0_255, -0.0_050, -0.1_170, -0.1_046, 0.0_309, 0.1_367, 0.1_728, -0.0_533, -0.0_748, -0.0_534, 0.1_624, 0.0_384, -0.1_805, -0.0_707, 0.0_642, 0.0_220, -0.0_134, -0.1_333, -0.1_505 ]) __UpperCAmelCase : Dict = torch.tensor([ 0.1_321, 0.1_337, 0.0_440, 0.0_622, -0.0_591, -0.0_370, 0.0_503, 0.2_133, -0.0_177, 0.1_415, -0.0_116, -0.0_112, 0.0_044, -0.0_980, -0.0_789, 0.0_395, 0.1_502, 0.1_785, -0.0_488, -0.0_514, -0.0_404, 0.1_539, 0.0_454, -0.1_559, -0.0_665, 0.0_659, 0.0_383, -0.0_005, -0.1_266, -0.1_386 ]) __UpperCAmelCase : List[str] = torch.tensor([ 0.1_154, 0.1_218, 0.0_307, 0.0_526, -0.0_711, -0.0_541, 0.0_366, 0.2_078, -0.0_267, 0.1_317, -0.0_226, -0.0_193, -0.0_014, -0.1_055, -0.0_902, 0.0_330, 0.1_391, 0.1_709, -0.0_562, -0.0_693, -0.0_560, 0.1_482, 0.0_381, -0.1_683, -0.0_681, 0.0_661, 0.0_331, -0.0_046, -0.1_268, -0.1_431 ]) __UpperCAmelCase : int = torch.tensor([ 0.1_192, 0.1_240, 0.0_414, 0.0_606, -0.0_557, -0.0_412, 0.0_430, 0.2_042, -0.0_200, 0.1_385, -0.0_115, -0.0_132, 0.0_017, -0.0_965, -0.0_802, 0.0_398, 0.1_433, 0.1_747, -0.0_458, -0.0_533, -0.0_407, 0.1_545, 0.0_419, -0.1_574, -0.0_645, 0.0_626, 0.0_341, -0.0_010, -0.1_199, -0.1_390 ]) __UpperCAmelCase : Union[str, Any] = torch.tensor([ 0.1_075, 0.1_074, 0.0_205, 0.0_431, -0.0_774, -0.0_607, 0.0_298, 0.2_042, -0.0_320, 0.1_267, -0.0_281, -0.0_250, -0.0_064, -0.1_091, -0.0_946, 0.0_290, 0.1_328, 0.1_650, -0.0_580, -0.0_738, -0.0_586, 0.1_440, 0.0_337, -0.1_746, -0.0_712, 0.0_605, 0.0_250, -0.0_099, -0.1_316, -0.1_473 ]) __UpperCAmelCase : Tuple = torch.tensor([ -1.4_572, -2.0_481, -0.0_414, -0.6_005, 1.4_136, 0.5_848, 0.4_028, -2.7_330, 1.2_212, -2.1_228, 0.2_155, 0.4_039, 0.7_662, 2.0_535, 0.7_477, -0.3_243, -2.1_758, -2.7_648, 1.6_947, 0.7_026, 1.2_338, -1.6_078, -0.8_682, 2.2_810, 1.8_574, -0.5_718, -0.5_586, -0.0_186, 2.3_415, 2.1_251]) __UpperCAmelCase : Dict = torch.tensor([ -1.3_690, -1.9_720, -0.4_090, -0.6_966, 1.4_660, 0.9_938, -0.1_385, -2.7_324, 0.7_736, -1.8_917, 0.2_923, 0.4_293, 0.1_693, 1.4_112, 1.1_887, -0.3_181, -2.2_160, -2.6_381, 1.3_170, 0.8_163, 0.9_240, -1.6_544, -0.6_099, 2.5_259, 1.6_430, -0.9_090, -0.9_392, -0.0_126, 2.4_268, 2.3_266 ]) __UpperCAmelCase : List[str] = torch.tensor([ -1.3_525, -1.9_628, -0.3_956, -0.6_860, 1.4_664, 1.0_014, -0.1_259, -2.7_212, 0.7_772, -1.8_811, 0.2_996, 0.4_388, 0.1_704, 1.4_029, 1.1_701, -0.3_027, -2.2_053, -2.6_287, 1.3_350, 0.8_131, 0.9_274, -1.6_292, -0.6_098, 2.5_131, 1.6_505, -0.8_958, -0.9_298, -0.0_151, 2.4_257, 2.3_355 ]) __UpperCAmelCase : List[Any] = torch.tensor([ -2.0_585, -2.7_897, -0.2_850, -0.8_940, 1.9_052, 0.5_702, 0.6_345, -3.8_959, 1.5_932, -3.2_319, 0.1_974, 0.0_287, 1.7_566, 2.6_543, 0.8_387, -0.5_351, -3.2_736, -4.3_375, 2.9_029, 1.6_390, 1.4_640, -2.1_701, -1.9_013, 2.9_341, 3.4_981, -0.6_255, -1.1_644, -0.1_591, 3.7_097, 3.2_066 ]) __UpperCAmelCase : Any = torch.tensor([ -2.3_139, -2.5_594, -0.0_197, -0.6_785, 1.7_001, 1.1_606, 0.3_075, -2.1_740, 1.8_071, -2.5_630, -0.0_926, -0.3_811, 1.2_116, 2.6_246, 1.2_731, -0.5_398, -2.8_153, -3.6_140, 2.3_893, 1.3_262, 1.6_258, -2.1_856, -1.3_267, 2.8_395, 2.3_779, -1.0_623, -1.2_468, 0.8_959, 3.3_367, 3.2_243 ]) __UpperCAmelCase : Dict = torch.tensor([ -2.0_628, -2.7_667, -0.2_089, -0.8_263, 2.0_539, 0.5_992, 0.6_495, -3.8_336, 1.6_025, -3.2_817, 0.1_721, -0.0_633, 1.7_516, 2.7_039, 0.8_100, -0.5_908, -3.2_113, -4.4_343, 2.9_257, 1.3_632, 1.5_562, -2.1_489, -1.9_894, 3.0_560, 3.3_396, -0.7_328, -1.0_417, 0.0_383, 3.7_093, 3.2_343 ]) __UpperCAmelCase : Union[str, Any] = torch.tensor([ -1.4_574, -2.0_569, -0.0_473, -0.6_117, 1.4_018, 0.5_769, 0.4_129, -2.7_344, 1.2_241, -2.1_397, 0.2_000, 0.3_937, 0.7_616, 2.0_453, 0.7_324, -0.3_391, -2.1_746, -2.7_744, 1.6_963, 0.6_921, 1.2_187, -1.6_172, -0.8_877, 2.2_439, 1.8_471, -0.5_839, -0.5_605, -0.0_464, 2.3_250, 2.1_219 ]) # fmt: on __UpperCAmelCase : int = api.list_models(filter="diffusers") for mod in models: if "google" in mod.author or mod.modelId == "CompVis/ldm-celebahq-256": __UpperCAmelCase : int = "/home/patrick/google_checkpoints/" + mod.modelId.split("/")[-1] print(f'''Started running {mod.modelId}!!!''') if mod.modelId.startswith("CompVis"): __UpperCAmelCase : Optional[Any] = UNetaDModel.from_pretrained(local_checkpoint, subfolder="unet") else: __UpperCAmelCase : Optional[Any] = UNetaDModel.from_pretrained(local_checkpoint) torch.manual_seed(0) random.seed(0) __UpperCAmelCase : Any = torch.randn(1, model.config.in_channels, model.config.sample_size, model.config.sample_size) __UpperCAmelCase : int = torch.tensor([10] * noise.shape[0]) with torch.no_grad(): __UpperCAmelCase : Tuple = model(noise, time_step).sample assert torch.allclose( logits[0, 0, 0, :30], results["_".join("_".join(mod.modelId.split("/")).split("-"))], atol=1E-3 ) print(f'''{mod.modelId} has passed successfully!!!''')
643
from __future__ import annotations def a ( SCREAMING_SNAKE_CASE_ : list[int] ): """simple docstring""" if len(SCREAMING_SNAKE_CASE_ ) == 0: return array UpperCamelCase , UpperCamelCase : Union[str, Any] = min(SCREAMING_SNAKE_CASE_ ), max(SCREAMING_SNAKE_CASE_ ) # Compute the variables UpperCamelCase : Union[str, Any] = _max - _min + 1 UpperCamelCase , UpperCamelCase : Optional[Any] = [0] * holes_range, [0] * holes_range # Make the sorting. for i in array: UpperCamelCase : Optional[int] = i - _min UpperCamelCase : Any = i holes_repeat[index] += 1 # Makes the array back by replacing the numbers. UpperCamelCase : str = 0 for i in range(SCREAMING_SNAKE_CASE_ ): while holes_repeat[i] > 0: UpperCamelCase : List[Any] = holes[i] index += 1 holes_repeat[i] -= 1 # Returns the sorted array. return array if __name__ == "__main__": import doctest doctest.testmod() __UpperCAmelCase : Any = input("Enter numbers separated by comma:\n") __UpperCAmelCase : int = [int(x) for x in user_input.split(",")] print(pigeon_sort(unsorted))
643
1
from __future__ import annotations from itertools import permutations from random import randint from timeit import repeat def a ( ): """simple docstring""" UpperCamelCase : int = [randint(-1_0_0_0 , 1_0_0_0 ) for i in range(1_0 )] UpperCamelCase : Optional[int] = randint(-5_0_0_0 , 5_0_0_0 ) return (arr, r) __UpperCAmelCase : Optional[int] = make_dataset() def a ( SCREAMING_SNAKE_CASE_ : list[int] , SCREAMING_SNAKE_CASE_ : int ): """simple docstring""" for triplet in permutations(SCREAMING_SNAKE_CASE_ , 3 ): if sum(SCREAMING_SNAKE_CASE_ ) == target: return tuple(sorted(SCREAMING_SNAKE_CASE_ ) ) return (0, 0, 0) def a ( SCREAMING_SNAKE_CASE_ : list[int] , SCREAMING_SNAKE_CASE_ : int ): """simple docstring""" arr.sort() UpperCamelCase : str = len(SCREAMING_SNAKE_CASE_ ) for i in range(n - 1 ): UpperCamelCase , UpperCamelCase : Any = i + 1, n - 1 while left < right: if arr[i] + arr[left] + arr[right] == target: return (arr[i], arr[left], arr[right]) elif arr[i] + arr[left] + arr[right] < target: left += 1 elif arr[i] + arr[left] + arr[right] > target: right -= 1 return (0, 0, 0) def a ( ): """simple docstring""" UpperCamelCase : Optional[int] = ''' from __main__ import dataset, triplet_sum1, triplet_sum2 ''' UpperCamelCase : Any = ''' triplet_sum1(*dataset) ''' UpperCamelCase : str = ''' triplet_sum2(*dataset) ''' UpperCamelCase : List[Any] = repeat(setup=SCREAMING_SNAKE_CASE_ , stmt=SCREAMING_SNAKE_CASE_ , repeat=5 , number=1_0_0_0_0 ) UpperCamelCase : List[str] = repeat(setup=SCREAMING_SNAKE_CASE_ , stmt=SCREAMING_SNAKE_CASE_ , repeat=5 , number=1_0_0_0_0 ) return (min(SCREAMING_SNAKE_CASE_ ), min(SCREAMING_SNAKE_CASE_ )) if __name__ == "__main__": from doctest import testmod testmod() __UpperCAmelCase : str = solution_times() print(f'''The time for naive implementation is {times[0]}.''') print(f'''The time for optimized implementation is {times[1]}.''')
643
import json import os import shutil import warnings from argparse import ArgumentParser, Namespace from pathlib import Path from typing import List from ..utils import logging from . import BaseTransformersCLICommand try: from cookiecutter.main import cookiecutter __UpperCAmelCase : List[Any] = True except ImportError: __UpperCAmelCase : List[str] = False __UpperCAmelCase : Any = logging.get_logger(__name__) # pylint: disable=invalid-name def a ( SCREAMING_SNAKE_CASE_ : Namespace ): """simple docstring""" return AddNewModelCommand(args.testing , args.testing_file , path=args.path ) class UpperCAmelCase_ ( _a): '''simple docstring''' @staticmethod def _lowercase ( __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : List[Any] = parser.add_parser('''add-new-model''' ) add_new_model_parser.add_argument('''--testing''' , action='''store_true''' , help='''If in testing mode.''' ) add_new_model_parser.add_argument('''--testing_file''' , type=__SCREAMING_SNAKE_CASE , help='''Configuration file on which to run.''' ) add_new_model_parser.add_argument( '''--path''' , type=__SCREAMING_SNAKE_CASE , help='''Path to cookiecutter. Should only be used for testing purposes.''' ) add_new_model_parser.set_defaults(func=__SCREAMING_SNAKE_CASE ) def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , *__SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Tuple = testing UpperCamelCase : Any = testing_file UpperCamelCase : Dict = path def _lowercase ( self ): """simple docstring""" warnings.warn( '''The command `transformers-cli add-new-model` is deprecated and will be removed in v5 of Transformers. ''' '''It is not actively maintained anymore, so might give a result that won\'t pass all tests and quality ''' '''checks, you should use `transformers-cli add-new-model-like` instead.''' ) if not _has_cookiecutter: raise ImportError( '''Model creation dependencies are required to use the `add_new_model` command. Install them by running ''' '''the following at the root of your `transformers` clone:\n\n\t$ pip install -e .[modelcreation]\n''' ) # Ensure that there is no other `cookiecutter-template-xxx` directory in the current working directory UpperCamelCase : List[str] = [directory for directory in os.listdir() if '''cookiecutter-template-''' == directory[:22]] if len(__SCREAMING_SNAKE_CASE ) > 0: raise ValueError( '''Several directories starting with `cookiecutter-template-` in current working directory. ''' '''Please clean your directory by removing all folders starting with `cookiecutter-template-` or ''' '''change your working directory.''' ) UpperCamelCase : Dict = ( Path(__SCREAMING_SNAKE_CASE ).parent.parent.parent.parent if self._path is None else Path(self._path ).parent.parent ) UpperCamelCase : List[Any] = path_to_transformer_root / '''templates''' / '''adding_a_new_model''' # Execute cookiecutter if not self._testing: cookiecutter(str(__SCREAMING_SNAKE_CASE ) ) else: with open(self._testing_file , '''r''' ) as configuration_file: UpperCamelCase : Tuple = json.load(__SCREAMING_SNAKE_CASE ) cookiecutter( str(path_to_cookiecutter if self._path is None else self._path ) , no_input=__SCREAMING_SNAKE_CASE , extra_context=__SCREAMING_SNAKE_CASE , ) UpperCamelCase : Dict = [directory for directory in os.listdir() if '''cookiecutter-template-''' in directory[:22]][0] # Retrieve configuration with open(directory + '''/configuration.json''' , '''r''' ) as configuration_file: UpperCamelCase : Tuple = json.load(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Dict = configuration['''lowercase_modelname'''] UpperCamelCase : int = configuration['''generate_tensorflow_pytorch_and_flax'''] os.remove(f"""{directory}/configuration.json""" ) UpperCamelCase : str = '''PyTorch''' in generate_tensorflow_pytorch_and_flax UpperCamelCase : Any = '''TensorFlow''' in generate_tensorflow_pytorch_and_flax UpperCamelCase : Union[str, Any] = '''Flax''' in generate_tensorflow_pytorch_and_flax UpperCamelCase : Optional[Any] = f"""{path_to_transformer_root}/src/transformers/models/{lowercase_model_name}""" os.makedirs(__SCREAMING_SNAKE_CASE , exist_ok=__SCREAMING_SNAKE_CASE ) os.makedirs(f"""{path_to_transformer_root}/tests/models/{lowercase_model_name}""" , exist_ok=__SCREAMING_SNAKE_CASE ) # Tests require submodules as they have parent imports with open(f"""{path_to_transformer_root}/tests/models/{lowercase_model_name}/__init__.py""" , '''w''' ): pass shutil.move( f"""{directory}/__init__.py""" , f"""{model_dir}/__init__.py""" , ) shutil.move( f"""{directory}/configuration_{lowercase_model_name}.py""" , f"""{model_dir}/configuration_{lowercase_model_name}.py""" , ) def remove_copy_lines(__SCREAMING_SNAKE_CASE ): with open(__SCREAMING_SNAKE_CASE , '''r''' ) as f: UpperCamelCase : Any = f.readlines() with open(__SCREAMING_SNAKE_CASE , '''w''' ) as f: for line in lines: if "# Copied from transformers." not in line: f.write(__SCREAMING_SNAKE_CASE ) if output_pytorch: if not self._testing: remove_copy_lines(f"""{directory}/modeling_{lowercase_model_name}.py""" ) shutil.move( f"""{directory}/modeling_{lowercase_model_name}.py""" , f"""{model_dir}/modeling_{lowercase_model_name}.py""" , ) shutil.move( f"""{directory}/test_modeling_{lowercase_model_name}.py""" , f"""{path_to_transformer_root}/tests/models/{lowercase_model_name}/test_modeling_{lowercase_model_name}.py""" , ) else: os.remove(f"""{directory}/modeling_{lowercase_model_name}.py""" ) os.remove(f"""{directory}/test_modeling_{lowercase_model_name}.py""" ) if output_tensorflow: if not self._testing: remove_copy_lines(f"""{directory}/modeling_tf_{lowercase_model_name}.py""" ) shutil.move( f"""{directory}/modeling_tf_{lowercase_model_name}.py""" , f"""{model_dir}/modeling_tf_{lowercase_model_name}.py""" , ) shutil.move( f"""{directory}/test_modeling_tf_{lowercase_model_name}.py""" , f"""{path_to_transformer_root}/tests/models/{lowercase_model_name}/test_modeling_tf_{lowercase_model_name}.py""" , ) else: os.remove(f"""{directory}/modeling_tf_{lowercase_model_name}.py""" ) os.remove(f"""{directory}/test_modeling_tf_{lowercase_model_name}.py""" ) if output_flax: if not self._testing: remove_copy_lines(f"""{directory}/modeling_flax_{lowercase_model_name}.py""" ) shutil.move( f"""{directory}/modeling_flax_{lowercase_model_name}.py""" , f"""{model_dir}/modeling_flax_{lowercase_model_name}.py""" , ) shutil.move( f"""{directory}/test_modeling_flax_{lowercase_model_name}.py""" , f"""{path_to_transformer_root}/tests/models/{lowercase_model_name}/test_modeling_flax_{lowercase_model_name}.py""" , ) else: os.remove(f"""{directory}/modeling_flax_{lowercase_model_name}.py""" ) os.remove(f"""{directory}/test_modeling_flax_{lowercase_model_name}.py""" ) shutil.move( f"""{directory}/{lowercase_model_name}.md""" , f"""{path_to_transformer_root}/docs/source/en/model_doc/{lowercase_model_name}.md""" , ) shutil.move( f"""{directory}/tokenization_{lowercase_model_name}.py""" , f"""{model_dir}/tokenization_{lowercase_model_name}.py""" , ) shutil.move( f"""{directory}/tokenization_fast_{lowercase_model_name}.py""" , f"""{model_dir}/tokenization_{lowercase_model_name}_fast.py""" , ) from os import fdopen, remove from shutil import copymode, move from tempfile import mkstemp def replace(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): # Create temp file UpperCamelCase , UpperCamelCase : Optional[Any] = mkstemp() UpperCamelCase : Tuple = False with fdopen(__SCREAMING_SNAKE_CASE , '''w''' ) as new_file: with open(__SCREAMING_SNAKE_CASE ) as old_file: for line in old_file: new_file.write(__SCREAMING_SNAKE_CASE ) if line_to_copy_below in line: UpperCamelCase : Optional[int] = True for line_to_copy in lines_to_copy: new_file.write(__SCREAMING_SNAKE_CASE ) if not line_found: raise ValueError(f"""Line {line_to_copy_below} was not found in file.""" ) # Copy the file permissions from the old file to the new file copymode(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # Remove original file remove(__SCREAMING_SNAKE_CASE ) # Move new file move(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def skip_units(__SCREAMING_SNAKE_CASE ): return ( ("generating PyTorch" in line and not output_pytorch) or ("generating TensorFlow" in line and not output_tensorflow) or ("generating Flax" in line and not output_flax) ) def replace_in_files(__SCREAMING_SNAKE_CASE ): with open(__SCREAMING_SNAKE_CASE ) as datafile: UpperCamelCase : int = [] UpperCamelCase : Dict = False UpperCamelCase : List[Any] = False for line in datafile: if "# To replace in: " in line and "##" not in line: UpperCamelCase : Dict = line.split('''"''' )[1] UpperCamelCase : int = skip_units(__SCREAMING_SNAKE_CASE ) elif "# Below: " in line and "##" not in line: UpperCamelCase : Dict = line.split('''"''' )[1] UpperCamelCase : List[str] = skip_units(__SCREAMING_SNAKE_CASE ) elif "# End." in line and "##" not in line: if not skip_file and not skip_snippet: replace(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) UpperCamelCase : Dict = [] elif "# Replace with" in line and "##" not in line: UpperCamelCase : Tuple = [] elif "##" not in line: lines_to_copy.append(__SCREAMING_SNAKE_CASE ) remove(__SCREAMING_SNAKE_CASE ) replace_in_files(f"""{directory}/to_replace_{lowercase_model_name}.py""" ) os.rmdir(__SCREAMING_SNAKE_CASE )
643
1
from __future__ import annotations from typing import Any def a ( SCREAMING_SNAKE_CASE_ : list[Any] ): """simple docstring""" create_state_space_tree(SCREAMING_SNAKE_CASE_ , [] , 0 ) def a ( SCREAMING_SNAKE_CASE_ : list[Any] , SCREAMING_SNAKE_CASE_ : list[Any] , SCREAMING_SNAKE_CASE_ : int ): """simple docstring""" if index == len(SCREAMING_SNAKE_CASE_ ): print(SCREAMING_SNAKE_CASE_ ) return create_state_space_tree(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , index + 1 ) current_subsequence.append(sequence[index] ) create_state_space_tree(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , index + 1 ) current_subsequence.pop() if __name__ == "__main__": __UpperCAmelCase : list[Any] = [3, 1, 2, 4] generate_all_subsequences(seq) seq.clear() seq.extend(["A", "B", "C"]) generate_all_subsequences(seq)
643
from pathlib import Path import cva import numpy as np from matplotlib import pyplot as plt def a ( SCREAMING_SNAKE_CASE_ : np.ndarray , SCREAMING_SNAKE_CASE_ : np.ndarray , SCREAMING_SNAKE_CASE_ : np.ndarray , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int ): """simple docstring""" UpperCamelCase : str = cva.getAffineTransform(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) return cva.warpAffine(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , (rows, cols) ) if __name__ == "__main__": # read original image __UpperCAmelCase : Tuple = cva.imread( str(Path(__file__).resolve().parent.parent / "image_data" / "lena.jpg") ) # turn image in gray scale value __UpperCAmelCase : int = cva.cvtColor(image, cva.COLOR_BGR2GRAY) # get image shape __UpperCAmelCase , __UpperCAmelCase : Tuple = gray_img.shape # set different points to rotate image __UpperCAmelCase : Optional[int] = np.array([[50, 50], [200, 50], [50, 200]], np.floataa) __UpperCAmelCase : Optional[int] = np.array([[10, 100], [200, 50], [100, 250]], np.floataa) __UpperCAmelCase : Any = np.array([[50, 50], [150, 50], [120, 200]], np.floataa) __UpperCAmelCase : int = np.array([[10, 100], [80, 50], [180, 250]], np.floataa) # add all rotated images in a list __UpperCAmelCase : Union[str, Any] = [ gray_img, get_rotation(gray_img, ptsa, ptsa, img_rows, img_cols), get_rotation(gray_img, ptsa, ptsa, img_rows, img_cols), get_rotation(gray_img, ptsa, ptsa, img_rows, img_cols), ] # plot different image rotations __UpperCAmelCase : List[str] = plt.figure(1) __UpperCAmelCase : Dict = ["Original", "Rotation 1", "Rotation 2", "Rotation 3"] for i, image in enumerate(images): plt.subplot(2, 2, i + 1), plt.imshow(image, "gray") plt.title(titles[i]) plt.axis("off") plt.subplots_adjust(left=0.0, bottom=0.05, right=1.0, top=0.95) plt.show()
643
1
import json import os import tempfile import transformers import datasets from utils import generate_example_dataset, get_duration __UpperCAmelCase : Optional[int] = 500000 __UpperCAmelCase , __UpperCAmelCase : Any = os.path.split(__file__) __UpperCAmelCase : int = os.path.join(RESULTS_BASEPATH, "results", RESULTS_FILENAME.replace(".py", ".json")) @get_duration def a ( SCREAMING_SNAKE_CASE_ : datasets.Dataset , **SCREAMING_SNAKE_CASE_ : Union[str, Any] ): """simple docstring""" UpperCamelCase : Tuple = dataset.map(**SCREAMING_SNAKE_CASE_ ) @get_duration def a ( SCREAMING_SNAKE_CASE_ : datasets.Dataset , **SCREAMING_SNAKE_CASE_ : Any ): """simple docstring""" UpperCamelCase : int = dataset.filter(**SCREAMING_SNAKE_CASE_ ) def a ( ): """simple docstring""" UpperCamelCase : Optional[int] = {'''num examples''': SPEED_TEST_N_EXAMPLES} with tempfile.TemporaryDirectory() as tmp_dir: UpperCamelCase : Dict = datasets.Features({'''text''': datasets.Value('''string''' ), '''numbers''': datasets.Value('''float32''' )} ) UpperCamelCase : List[str] = generate_example_dataset( os.path.join(SCREAMING_SNAKE_CASE_ , '''dataset.arrow''' ) , SCREAMING_SNAKE_CASE_ , num_examples=SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Union[str, Any] = transformers.AutoTokenizer.from_pretrained('''bert-base-cased''' , use_fast=SCREAMING_SNAKE_CASE_ ) def tokenize(SCREAMING_SNAKE_CASE_ : Dict ): return tokenizer(examples['''text'''] ) UpperCamelCase : List[Any] = map(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : List[str] = map(SCREAMING_SNAKE_CASE_ , batched=SCREAMING_SNAKE_CASE_ ) UpperCamelCase : List[Any] = map(SCREAMING_SNAKE_CASE_ , function=lambda SCREAMING_SNAKE_CASE_ : None , batched=SCREAMING_SNAKE_CASE_ ) with dataset.formatted_as(type='''numpy''' ): UpperCamelCase : Tuple = map(SCREAMING_SNAKE_CASE_ , function=lambda SCREAMING_SNAKE_CASE_ : None , batched=SCREAMING_SNAKE_CASE_ ) with dataset.formatted_as(type='''pandas''' ): UpperCamelCase : int = map(SCREAMING_SNAKE_CASE_ , function=lambda SCREAMING_SNAKE_CASE_ : None , batched=SCREAMING_SNAKE_CASE_ ) with dataset.formatted_as(type='''torch''' , columns='''numbers''' ): UpperCamelCase : Dict = map(SCREAMING_SNAKE_CASE_ , function=lambda SCREAMING_SNAKE_CASE_ : None , batched=SCREAMING_SNAKE_CASE_ ) with dataset.formatted_as(type='''tensorflow''' , columns='''numbers''' ): UpperCamelCase : Tuple = map(SCREAMING_SNAKE_CASE_ , function=lambda SCREAMING_SNAKE_CASE_ : None , batched=SCREAMING_SNAKE_CASE_ ) UpperCamelCase : List[str] = map(SCREAMING_SNAKE_CASE_ , function=SCREAMING_SNAKE_CASE_ , batched=SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Dict = filter(SCREAMING_SNAKE_CASE_ ) # Activate later when tokenizer support batched inputs # with dataset.formatted_as(type='numpy'): # times[func.__name__ + " fast-tokenizer batched numpy"] = func(dataset, function=tokenize, batched=True) with open(SCREAMING_SNAKE_CASE_ , '''wb''' ) as f: f.write(json.dumps(SCREAMING_SNAKE_CASE_ ).encode('''utf-8''' ) ) if __name__ == "__main__": # useful to run the profiler benchmark_map_filter()
643
import copy from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ..auto import CONFIG_MAPPING __UpperCAmelCase : Optional[Any] = logging.get_logger(__name__) __UpperCAmelCase : List[str] = { "microsoft/conditional-detr-resnet-50": ( "https://huggingface.co/microsoft/conditional-detr-resnet-50/resolve/main/config.json" ), } class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : Optional[int] = "conditional_detr" __UpperCamelCase : Optional[Any] = ["past_key_values"] __UpperCamelCase : Union[str, Any] = { "hidden_size": "d_model", "num_attention_heads": "encoder_attention_heads", } def __init__( self , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=300 , __SCREAMING_SNAKE_CASE=6 , __SCREAMING_SNAKE_CASE=2_048 , __SCREAMING_SNAKE_CASE=8 , __SCREAMING_SNAKE_CASE=6 , __SCREAMING_SNAKE_CASE=2_048 , __SCREAMING_SNAKE_CASE=8 , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE="relu" , __SCREAMING_SNAKE_CASE=256 , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=0.02 , __SCREAMING_SNAKE_CASE=1.0 , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE="sine" , __SCREAMING_SNAKE_CASE="resnet50" , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=5 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=1 , __SCREAMING_SNAKE_CASE=1 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=5 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=0.25 , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" if backbone_config is not None and use_timm_backbone: raise ValueError('''You can\'t specify both `backbone_config` and `use_timm_backbone`.''' ) if not use_timm_backbone: if backbone_config is None: logger.info('''`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.''' ) UpperCamelCase : str = CONFIG_MAPPING['''resnet'''](out_features=['''stage4'''] ) elif isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): UpperCamelCase : Tuple = backbone_config.get('''model_type''' ) UpperCamelCase : Optional[Any] = CONFIG_MAPPING[backbone_model_type] UpperCamelCase : Any = config_class.from_dict(__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = use_timm_backbone UpperCamelCase : int = backbone_config UpperCamelCase : Any = num_channels UpperCamelCase : Optional[Any] = num_queries UpperCamelCase : Tuple = d_model UpperCamelCase : Optional[Any] = encoder_ffn_dim UpperCamelCase : Optional[int] = encoder_layers UpperCamelCase : Union[str, Any] = encoder_attention_heads UpperCamelCase : Optional[Any] = decoder_ffn_dim UpperCamelCase : Optional[int] = decoder_layers UpperCamelCase : Optional[Any] = decoder_attention_heads UpperCamelCase : Any = dropout UpperCamelCase : List[Any] = attention_dropout UpperCamelCase : List[Any] = activation_dropout UpperCamelCase : List[str] = activation_function UpperCamelCase : Optional[int] = init_std UpperCamelCase : Optional[Any] = init_xavier_std UpperCamelCase : Union[str, Any] = encoder_layerdrop UpperCamelCase : Optional[Any] = decoder_layerdrop UpperCamelCase : Tuple = encoder_layers UpperCamelCase : Optional[Any] = auxiliary_loss UpperCamelCase : Union[str, Any] = position_embedding_type UpperCamelCase : Optional[int] = backbone UpperCamelCase : Dict = use_pretrained_backbone UpperCamelCase : Tuple = dilation # Hungarian matcher UpperCamelCase : Union[str, Any] = class_cost UpperCamelCase : List[Any] = bbox_cost UpperCamelCase : Optional[Any] = giou_cost # Loss coefficients UpperCamelCase : Optional[Any] = mask_loss_coefficient UpperCamelCase : Optional[int] = dice_loss_coefficient UpperCamelCase : Optional[Any] = cls_loss_coefficient UpperCamelCase : Optional[int] = bbox_loss_coefficient UpperCamelCase : Optional[int] = giou_loss_coefficient UpperCamelCase : Optional[int] = focal_alpha super().__init__(is_encoder_decoder=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) @property def _lowercase ( self ): """simple docstring""" return self.encoder_attention_heads @property def _lowercase ( self ): """simple docstring""" return self.d_model def _lowercase ( self ): """simple docstring""" UpperCamelCase : Union[str, Any] = copy.deepcopy(self.__dict__ ) if self.backbone_config is not None: UpperCamelCase : List[Any] = self.backbone_config.to_dict() UpperCamelCase : List[Any] = self.__class__.model_type return output class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : Dict = version.parse("1.11") @property def _lowercase ( self ): """simple docstring""" return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ('''pixel_mask''', {0: '''batch'''}), ] ) @property def _lowercase ( self ): """simple docstring""" return 1e-5 @property def _lowercase ( self ): """simple docstring""" return 12
643
1
from __future__ import annotations import copy import inspect import json import math import os import tempfile import unittest from importlib import import_module import numpy as np from transformers import ViTMAEConfig from transformers.file_utils import cached_property, is_tf_available, is_vision_available from transformers.testing_utils import require_tf, require_vision, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFViTMAEForPreTraining, TFViTMAEModel if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class UpperCAmelCase_ : '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=13 , __SCREAMING_SNAKE_CASE=30 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=32 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=4 , __SCREAMING_SNAKE_CASE=37 , __SCREAMING_SNAKE_CASE="gelu" , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=10 , __SCREAMING_SNAKE_CASE=0.02 , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=0.6 , __SCREAMING_SNAKE_CASE=None , ): """simple docstring""" UpperCamelCase : Tuple = parent UpperCamelCase : Any = batch_size UpperCamelCase : Optional[Any] = image_size UpperCamelCase : Optional[int] = patch_size UpperCamelCase : Union[str, Any] = num_channels UpperCamelCase : List[Any] = is_training UpperCamelCase : Tuple = use_labels UpperCamelCase : Union[str, Any] = hidden_size UpperCamelCase : List[Any] = num_hidden_layers UpperCamelCase : Optional[Any] = num_attention_heads UpperCamelCase : Dict = intermediate_size UpperCamelCase : Optional[Any] = hidden_act UpperCamelCase : List[str] = hidden_dropout_prob UpperCamelCase : Any = attention_probs_dropout_prob UpperCamelCase : int = type_sequence_label_size UpperCamelCase : str = initializer_range UpperCamelCase : Tuple = mask_ratio UpperCamelCase : Dict = scope # in ViTMAE, the expected sequence length = (num_patches + 1) * (1 - config.mask_ratio), rounded above # (we add 1 for the [CLS] token) UpperCamelCase : str = (image_size // patch_size) ** 2 UpperCamelCase : int = int(math.ceil((1 - mask_ratio) * (num_patches + 1) ) ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : str = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) UpperCamelCase : Optional[int] = None if self.use_labels: UpperCamelCase : Dict = ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCamelCase : Any = self.get_config() return config, pixel_values, labels def _lowercase ( self ): """simple docstring""" return ViTMAEConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , decoder_hidden_size=self.hidden_size , decoder_num_hidden_layers=self.num_hidden_layers , decoder_num_attention_heads=self.num_attention_heads , decoder_intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=__SCREAMING_SNAKE_CASE , initializer_range=self.initializer_range , mask_ratio=self.mask_ratio , ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Dict = TFViTMAEModel(config=__SCREAMING_SNAKE_CASE ) UpperCamelCase : str = model(__SCREAMING_SNAKE_CASE , training=__SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Any = TFViTMAEForPreTraining(__SCREAMING_SNAKE_CASE ) UpperCamelCase : str = model(__SCREAMING_SNAKE_CASE , training=__SCREAMING_SNAKE_CASE ) # expected sequence length = num_patches UpperCamelCase : Tuple = (self.image_size // self.patch_size) ** 2 UpperCamelCase : str = self.patch_size**2 * self.num_channels self.parent.assertEqual(result.logits.shape , (self.batch_size, num_patches, expected_num_channels) ) # test greyscale images UpperCamelCase : str = 1 UpperCamelCase : List[str] = TFViTMAEForPreTraining(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Any = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) UpperCamelCase : List[Any] = model(__SCREAMING_SNAKE_CASE , training=__SCREAMING_SNAKE_CASE ) UpperCamelCase : str = self.patch_size**2 self.parent.assertEqual(result.logits.shape , (self.batch_size, num_patches, expected_num_channels) ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Tuple = self.prepare_config_and_inputs() ((UpperCamelCase) , (UpperCamelCase) , (UpperCamelCase)) : Tuple = config_and_inputs UpperCamelCase : Dict = {'''pixel_values''': pixel_values} return config, inputs_dict @require_tf class UpperCAmelCase_ ( _a, _a, unittest.TestCase): '''simple docstring''' __UpperCamelCase : Optional[Any] = (TFViTMAEModel, TFViTMAEForPreTraining) if is_tf_available() else () __UpperCamelCase : Tuple = {"feature-extraction": TFViTMAEModel} if is_tf_available() else {} __UpperCamelCase : Optional[Any] = False __UpperCamelCase : Tuple = False __UpperCamelCase : Any = False __UpperCamelCase : Union[str, Any] = False def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[str] = TFViTMAEModelTester(self ) UpperCamelCase : Union[str, Any] = ConfigTester(self , config_class=__SCREAMING_SNAKE_CASE , has_text_modality=__SCREAMING_SNAKE_CASE , hidden_size=37 ) def _lowercase ( self ): """simple docstring""" self.config_tester.run_common_tests() @unittest.skip(reason='''ViTMAE does not use inputs_embeds''' ) def _lowercase ( self ): """simple docstring""" pass def _lowercase ( self ): """simple docstring""" UpperCamelCase , UpperCamelCase : Tuple = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCamelCase : Tuple = model_class(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(model.get_input_embeddings() , (tf.keras.layers.Layer) ) UpperCamelCase : Any = model.get_output_embeddings() self.assertTrue(x is None or isinstance(__SCREAMING_SNAKE_CASE , tf.keras.layers.Layer ) ) def _lowercase ( self ): """simple docstring""" UpperCamelCase , UpperCamelCase : List[str] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCamelCase : Union[str, Any] = model_class(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Any = inspect.signature(model.call ) # signature.parameters is an OrderedDict => so arg_names order is deterministic UpperCamelCase : Dict = [*signature.parameters.keys()] UpperCamelCase : Any = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , __SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_pretraining(*__SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" np.random.seed(2 ) UpperCamelCase , UpperCamelCase : Dict = self.model_tester.prepare_config_and_inputs_for_common() UpperCamelCase : str = int((config.image_size // config.patch_size) ** 2 ) UpperCamelCase : Optional[int] = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) for model_class in self.all_model_classes: UpperCamelCase : int = model_class(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Any = self._prepare_for_class(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = model(__SCREAMING_SNAKE_CASE , noise=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = copy.deepcopy(self._prepare_for_class(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ) UpperCamelCase : Optional[Any] = model(**__SCREAMING_SNAKE_CASE , noise=__SCREAMING_SNAKE_CASE ) UpperCamelCase : int = outputs_dict[0].numpy() UpperCamelCase : Any = outputs_keywords[0].numpy() self.assertLess(np.sum(np.abs(output_dict - output_keywords ) ) , 1e-6 ) def _lowercase ( self ): """simple docstring""" np.random.seed(2 ) UpperCamelCase , UpperCamelCase : str = self.model_tester.prepare_config_and_inputs_for_common() UpperCamelCase : Tuple = int((config.image_size // config.patch_size) ** 2 ) UpperCamelCase : List[Any] = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) def prepare_numpy_arrays(__SCREAMING_SNAKE_CASE ): UpperCamelCase : Union[str, Any] = {} for k, v in inputs_dict.items(): if tf.is_tensor(__SCREAMING_SNAKE_CASE ): UpperCamelCase : Optional[int] = v.numpy() else: UpperCamelCase : str = np.array(__SCREAMING_SNAKE_CASE ) return inputs_np_dict for model_class in self.all_model_classes: UpperCamelCase : Tuple = model_class(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = self._prepare_for_class(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[Any] = prepare_numpy_arrays(__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[Any] = model(__SCREAMING_SNAKE_CASE , noise=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = model(**__SCREAMING_SNAKE_CASE , noise=__SCREAMING_SNAKE_CASE ) self.assert_outputs_same(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" np.random.seed(2 ) UpperCamelCase : List[str] = int((tf_model.config.image_size // tf_model.config.patch_size) ** 2 ) UpperCamelCase : Tuple = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) UpperCamelCase : Union[str, Any] = tf.constant(__SCREAMING_SNAKE_CASE ) # Add `noise` argument. # PT inputs will be prepared in `super().check_pt_tf_models()` with this added `noise` argument UpperCamelCase : Optional[int] = tf_noise super().check_pt_tf_models(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" np.random.seed(2 ) UpperCamelCase , UpperCamelCase : Any = self.model_tester.prepare_config_and_inputs_for_common() UpperCamelCase : List[str] = { module_member for model_class in self.all_model_classes for module in (import_module(model_class.__module__ ),) for module_member_name in dir(__SCREAMING_SNAKE_CASE ) if module_member_name.endswith('''MainLayer''' ) # This condition is required, since `modeling_tf_clip.py` has 3 classes whose names end with `MainLayer`. and module_member_name[: -len('''MainLayer''' )] == model_class.__name__[: -len('''Model''' )] for module_member in (getattr(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ),) if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) and tf.keras.layers.Layer in module_member.__bases__ and getattr(__SCREAMING_SNAKE_CASE , '''_keras_serializable''' , __SCREAMING_SNAKE_CASE ) } UpperCamelCase : Union[str, Any] = int((config.image_size // config.patch_size) ** 2 ) UpperCamelCase : List[Any] = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) UpperCamelCase : Tuple = tf.convert_to_tensor(__SCREAMING_SNAKE_CASE ) inputs_dict.update({'''noise''': noise} ) for main_layer_class in tf_main_layer_classes: UpperCamelCase : Tuple = main_layer_class(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[Any] = { name: tf.keras.Input(tensor.shape[1:] , dtype=tensor.dtype ) for name, tensor in inputs_dict.items() } UpperCamelCase : Tuple = tf.keras.Model(__SCREAMING_SNAKE_CASE , outputs=main_layer(__SCREAMING_SNAKE_CASE ) ) UpperCamelCase : Optional[int] = model(__SCREAMING_SNAKE_CASE ) with tempfile.TemporaryDirectory() as tmpdirname: UpperCamelCase : Any = os.path.join(__SCREAMING_SNAKE_CASE , '''keras_model.h5''' ) model.save(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Dict = tf.keras.models.load_model( __SCREAMING_SNAKE_CASE , custom_objects={main_layer_class.__name__: main_layer_class} ) assert isinstance(__SCREAMING_SNAKE_CASE , tf.keras.Model ) UpperCamelCase : Optional[Any] = model(__SCREAMING_SNAKE_CASE ) self.assert_outputs_same(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) @slow def _lowercase ( self ): """simple docstring""" np.random.seed(2 ) UpperCamelCase , UpperCamelCase : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() UpperCamelCase : int = int((config.image_size // config.patch_size) ** 2 ) UpperCamelCase : int = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) for model_class in self.all_model_classes: UpperCamelCase : int = model_class(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = self._prepare_for_class(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) UpperCamelCase : Any = model(__SCREAMING_SNAKE_CASE , noise=__SCREAMING_SNAKE_CASE ) if model_class.__name__ == "TFViTMAEModel": UpperCamelCase : Optional[Any] = outputs.last_hidden_state.numpy() UpperCamelCase : Optional[int] = 0 else: UpperCamelCase : str = outputs.logits.numpy() UpperCamelCase : Optional[int] = 0 with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(__SCREAMING_SNAKE_CASE , saved_model=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Dict = model_class.from_pretrained(__SCREAMING_SNAKE_CASE ) UpperCamelCase : str = model(__SCREAMING_SNAKE_CASE , noise=__SCREAMING_SNAKE_CASE ) if model_class.__name__ == "TFViTMAEModel": UpperCamelCase : List[str] = after_outputs['''last_hidden_state'''].numpy() UpperCamelCase : Tuple = 0 else: UpperCamelCase : int = after_outputs['''logits'''].numpy() UpperCamelCase : Union[str, Any] = 0 UpperCamelCase : Optional[Any] = np.amax(np.abs(out_a - out_a ) ) self.assertLessEqual(__SCREAMING_SNAKE_CASE , 1e-5 ) def _lowercase ( self ): """simple docstring""" np.random.seed(2 ) UpperCamelCase , UpperCamelCase : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() UpperCamelCase : Union[str, Any] = int((config.image_size // config.patch_size) ** 2 ) UpperCamelCase : Any = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) for model_class in self.all_model_classes: UpperCamelCase : Optional[int] = model_class(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = self._prepare_for_class(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) UpperCamelCase : Any = model(__SCREAMING_SNAKE_CASE , noise=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Dict = model.get_config() # make sure that returned config is jsonifiable, which is required by keras json.dumps(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Dict = model_class.from_config(model.get_config() ) # make sure it also accepts a normal config UpperCamelCase : str = model_class.from_config(model.config ) UpperCamelCase : Tuple = new_model(__SCREAMING_SNAKE_CASE ) # Build model new_model.set_weights(model.get_weights() ) UpperCamelCase : List[Any] = new_model(__SCREAMING_SNAKE_CASE , noise=__SCREAMING_SNAKE_CASE ) self.assert_outputs_same(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) @unittest.skip( reason='''ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load to get deterministic results.''' ) def _lowercase ( self ): """simple docstring""" pass @unittest.skip(reason='''ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load''' ) def _lowercase ( self ): """simple docstring""" pass @slow def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[str] = TFViTMAEModel.from_pretrained('''google/vit-base-patch16-224''' ) self.assertIsNotNone(__SCREAMING_SNAKE_CASE ) def a ( ): """simple docstring""" UpperCamelCase : List[Any] = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_tf @require_vision class UpperCAmelCase_ ( unittest.TestCase): '''simple docstring''' @cached_property def _lowercase ( self ): """simple docstring""" return ViTImageProcessor.from_pretrained('''facebook/vit-mae-base''' ) if is_vision_available() else None @slow def _lowercase ( self ): """simple docstring""" np.random.seed(2 ) UpperCamelCase : List[Any] = TFViTMAEForPreTraining.from_pretrained('''facebook/vit-mae-base''' ) UpperCamelCase : int = self.default_image_processor UpperCamelCase : str = prepare_img() UpperCamelCase : Dict = image_processor(images=__SCREAMING_SNAKE_CASE , return_tensors='''tf''' ) # prepare a noise vector that will be also used for testing the TF model # (this way we can ensure that the PT and TF models operate on the same inputs) UpperCamelCase : Dict = ViTMAEConfig() UpperCamelCase : str = int((vit_mae_config.image_size // vit_mae_config.patch_size) ** 2 ) UpperCamelCase : Dict = np.random.uniform(size=(1, num_patches) ) # forward pass UpperCamelCase : Union[str, Any] = model(**__SCREAMING_SNAKE_CASE , noise=__SCREAMING_SNAKE_CASE ) # verify the logits UpperCamelCase : str = tf.convert_to_tensor([1, 196, 768] ) self.assertEqual(outputs.logits.shape , __SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = tf.convert_to_tensor( [[-0.0_548, -1.7_023, -0.9_325], [0.3_721, -0.5_670, -0.2_233], [0.8_235, -1.3_878, -0.3_524]] ) tf.debugging.assert_near(outputs.logits[0, :3, :3] , __SCREAMING_SNAKE_CASE , atol=1e-4 )
643
import requests from bsa import BeautifulSoup def a ( SCREAMING_SNAKE_CASE_ : str = "AAPL" ): """simple docstring""" UpperCamelCase : Dict = F"""https://in.finance.yahoo.com/quote/{symbol}?s={symbol}""" UpperCamelCase : Any = BeautifulSoup(requests.get(SCREAMING_SNAKE_CASE_ ).text , '''html.parser''' ) UpperCamelCase : Dict = '''My(6px) Pos(r) smartphone_Mt(6px)''' return soup.find('''div''' , class_=class_ ).find('''span''' ).text if __name__ == "__main__": for symbol in "AAPL AMZN IBM GOOG MSFT ORCL".split(): print(f'''Current {symbol:<4} stock price is {stock_price(symbol):>8}''')
643
1
import collections import importlib.util import os import re from pathlib import Path __UpperCAmelCase : Any = "src/transformers" # Matches is_xxx_available() __UpperCAmelCase : List[Any] = re.compile(r"is\_([a-z_]*)_available()") # Catches a one-line _import_struct = {xxx} __UpperCAmelCase : List[str] = re.compile(r"^_import_structure\s+=\s+\{([^\}]+)\}") # Catches a line with a key-values pattern: "bla": ["foo", "bar"] __UpperCAmelCase : Any = re.compile(r"\s+\"\S*\":\s+\[([^\]]*)\]") # Catches a line if not is_foo_available __UpperCAmelCase : List[str] = re.compile(r"^\s*if\s+not\s+is\_[a-z_]*\_available\(\)") # Catches a line _import_struct["bla"].append("foo") __UpperCAmelCase : Tuple = re.compile(r"^\s*_import_structure\[\"\S*\"\]\.append\(\"(\S*)\"\)") # Catches a line _import_struct["bla"].extend(["foo", "bar"]) or _import_struct["bla"] = ["foo", "bar"] __UpperCAmelCase : Optional[int] = re.compile(r"^\s*_import_structure\[\S*\](?:\.extend\(|\s*=\s+)\[([^\]]*)\]") # Catches a line with an object between quotes and a comma: "MyModel", __UpperCAmelCase : str = re.compile("^\s+\"([^\"]+)\",") # Catches a line with objects between brackets only: ["foo", "bar"], __UpperCAmelCase : int = re.compile("^\s+\[([^\]]+)\]") # Catches a line with from foo import bar, bla, boo __UpperCAmelCase : Any = re.compile(r"\s+from\s+\S*\s+import\s+([^\(\s].*)\n") # Catches a line with try: __UpperCAmelCase : List[str] = re.compile(r"^\s*try:") # Catches a line with else: __UpperCAmelCase : Any = re.compile(r"^\s*else:") def a ( SCREAMING_SNAKE_CASE_ : List[str] ): """simple docstring""" if _re_test_backend.search(SCREAMING_SNAKE_CASE_ ) is None: return None UpperCamelCase : Tuple = [b[0] for b in _re_backend.findall(SCREAMING_SNAKE_CASE_ )] backends.sort() return "_and_".join(SCREAMING_SNAKE_CASE_ ) def a ( SCREAMING_SNAKE_CASE_ : Dict ): """simple docstring""" with open(SCREAMING_SNAKE_CASE_ , '''r''' , encoding='''utf-8''' , newline='''\n''' ) as f: UpperCamelCase : Any = f.readlines() UpperCamelCase : Dict = 0 while line_index < len(SCREAMING_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(SCREAMING_SNAKE_CASE_ ): return None # First grab the objects without a specific backend in _import_structure UpperCamelCase : List[Any] = [] while not lines[line_index].startswith('''if TYPE_CHECKING''' ) and find_backend(lines[line_index] ) is None: UpperCamelCase : Any = lines[line_index] # If we have everything on a single line, let's deal with it. if _re_one_line_import_struct.search(SCREAMING_SNAKE_CASE_ ): UpperCamelCase : int = _re_one_line_import_struct.search(SCREAMING_SNAKE_CASE_ ).groups()[0] UpperCamelCase : int = re.findall('''\[([^\]]+)\]''' , SCREAMING_SNAKE_CASE_ ) for imp in imports: objects.extend([obj[1:-1] for obj in imp.split(''', ''' )] ) line_index += 1 continue UpperCamelCase : Union[str, Any] = _re_import_struct_key_value.search(SCREAMING_SNAKE_CASE_ ) if single_line_import_search is not None: UpperCamelCase : str = [obj[1:-1] for obj in single_line_import_search.groups()[0].split(''', ''' ) if len(SCREAMING_SNAKE_CASE_ ) > 0] objects.extend(SCREAMING_SNAKE_CASE_ ) elif line.startswith(''' ''' * 8 + '''"''' ): objects.append(line[9:-3] ) line_index += 1 UpperCamelCase : int = {'''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. UpperCamelCase : int = 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: UpperCamelCase : List[str] = 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 UpperCamelCase : List[str] = [] # Until we unindent, add backend objects to the list while len(lines[line_index] ) <= 1 or lines[line_index].startswith(''' ''' * 4 ): UpperCamelCase : Dict = lines[line_index] if _re_import_struct_add_one.search(SCREAMING_SNAKE_CASE_ ) is not None: objects.append(_re_import_struct_add_one.search(SCREAMING_SNAKE_CASE_ ).groups()[0] ) elif _re_import_struct_add_many.search(SCREAMING_SNAKE_CASE_ ) is not None: UpperCamelCase : List[Any] = _re_import_struct_add_many.search(SCREAMING_SNAKE_CASE_ ).groups()[0].split(''', ''' ) UpperCamelCase : Union[str, Any] = [obj[1:-1] for obj in imports if len(SCREAMING_SNAKE_CASE_ ) > 0] objects.extend(SCREAMING_SNAKE_CASE_ ) elif _re_between_brackets.search(SCREAMING_SNAKE_CASE_ ) is not None: UpperCamelCase : Optional[Any] = _re_between_brackets.search(SCREAMING_SNAKE_CASE_ ).groups()[0].split(''', ''' ) UpperCamelCase : List[Any] = [obj[1:-1] for obj in imports if len(SCREAMING_SNAKE_CASE_ ) > 0] objects.extend(SCREAMING_SNAKE_CASE_ ) elif _re_quote_object.search(SCREAMING_SNAKE_CASE_ ) is not None: objects.append(_re_quote_object.search(SCREAMING_SNAKE_CASE_ ).groups()[0] ) elif line.startswith(''' ''' * 8 + '''"''' ): objects.append(line[9:-3] ) elif line.startswith(''' ''' * 1_2 + '''"''' ): objects.append(line[1_3:-3] ) line_index += 1 UpperCamelCase : Any = objects else: line_index += 1 # At this stage we are in the TYPE_CHECKING part, first grab the objects without a specific backend UpperCamelCase : int = [] while ( line_index < len(SCREAMING_SNAKE_CASE_ ) and find_backend(lines[line_index] ) is None and not lines[line_index].startswith('''else''' ) ): UpperCamelCase : Union[str, Any] = lines[line_index] UpperCamelCase : Union[str, Any] = _re_import.search(SCREAMING_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 UpperCamelCase : Optional[Any] = {'''none''': objects} # Let's continue with backend-specific objects while line_index < len(SCREAMING_SNAKE_CASE_ ): # If the line is an if is_backend_available, we grab all objects associated. UpperCamelCase : Dict = 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: UpperCamelCase : str = 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 UpperCamelCase : Union[str, Any] = [] # Until we unindent, add backend objects to the list while len(lines[line_index] ) <= 1 or lines[line_index].startswith(''' ''' * 8 ): UpperCamelCase : Union[str, Any] = lines[line_index] UpperCamelCase : Optional[Any] = _re_import.search(SCREAMING_SNAKE_CASE_ ) if single_line_import_search is not None: objects.extend(single_line_import_search.groups()[0].split(''', ''' ) ) elif line.startswith(''' ''' * 1_2 ): objects.append(line[1_2:-2] ) line_index += 1 UpperCamelCase : Any = objects else: line_index += 1 return import_dict_objects, type_hint_objects def a ( SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : Dict ): """simple docstring""" def find_duplicates(SCREAMING_SNAKE_CASE_ : List[Any] ): return [k for k, v in collections.Counter(SCREAMING_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!"] UpperCamelCase : Union[str, Any] = [] for key in import_dict_objects.keys(): UpperCamelCase : Optional[int] = find_duplicates(import_dict_objects[key] ) if duplicate_imports: errors.append(F"""Duplicate _import_structure definitions for: {duplicate_imports}""" ) UpperCamelCase : 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] ) ): UpperCamelCase : Optional[Any] = '''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 a ( ): """simple docstring""" UpperCamelCase : List[Any] = [] for root, _, files in os.walk(SCREAMING_SNAKE_CASE_ ): if "__init__.py" in files: UpperCamelCase : str = os.path.join(SCREAMING_SNAKE_CASE_ , '''__init__.py''' ) UpperCamelCase : List[str] = parse_init(SCREAMING_SNAKE_CASE_ ) if objects is not None: UpperCamelCase : Union[str, Any] = analyze_results(*SCREAMING_SNAKE_CASE_ ) if len(SCREAMING_SNAKE_CASE_ ) > 0: UpperCamelCase : Union[str, Any] = F"""Problem in {fname}, both halves do not define the same objects.\n{errors[0]}""" failures.append('''\n'''.join(SCREAMING_SNAKE_CASE_ ) ) if len(SCREAMING_SNAKE_CASE_ ) > 0: raise ValueError('''\n\n'''.join(SCREAMING_SNAKE_CASE_ ) ) def a ( ): """simple docstring""" UpperCamelCase : List[Any] = [] for path, directories, files in os.walk(SCREAMING_SNAKE_CASE_ ): for folder in directories: # Ignore private modules if folder.startswith('''_''' ): directories.remove(SCREAMING_SNAKE_CASE_ ) continue # Ignore leftovers from branches (empty folders apart from pycache) if len(list((Path(SCREAMING_SNAKE_CASE_ ) / folder).glob('''*.py''' ) ) ) == 0: continue UpperCamelCase : Tuple = str((Path(SCREAMING_SNAKE_CASE_ ) / folder).relative_to(SCREAMING_SNAKE_CASE_ ) ) UpperCamelCase : Optional[int] = short_path.replace(os.path.sep , '''.''' ) submodules.append(SCREAMING_SNAKE_CASE_ ) for fname in files: if fname == "__init__.py": continue UpperCamelCase : List[str] = str((Path(SCREAMING_SNAKE_CASE_ ) / fname).relative_to(SCREAMING_SNAKE_CASE_ ) ) UpperCamelCase : Union[str, Any] = short_path.replace('''.py''' , '''''' ).replace(os.path.sep , '''.''' ) if len(submodule.split('''.''' ) ) == 1: submodules.append(SCREAMING_SNAKE_CASE_ ) return submodules __UpperCAmelCase : Union[str, Any] = [ "convert_pytorch_checkpoint_to_tf2", "modeling_flax_pytorch_utils", ] def a ( ): """simple docstring""" UpperCamelCase : Optional[Any] = importlib.util.spec_from_file_location( '''transformers''' , os.path.join(SCREAMING_SNAKE_CASE_ , '''__init__.py''' ) , submodule_search_locations=[PATH_TO_TRANSFORMERS] , ) UpperCamelCase : Union[str, Any] = spec.loader.load_module() UpperCamelCase : Dict = [ module for module in get_transformers_submodules() if module not in IGNORE_SUBMODULES and module not in transformers._import_structure.keys() ] if len(SCREAMING_SNAKE_CASE_ ) > 0: UpperCamelCase : List[Any] = '''\n'''.join(F"""- {module}""" for module in module_not_registered ) raise ValueError( '''The following submodules are not properly registered 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()
643
def a ( SCREAMING_SNAKE_CASE_ : int ): """simple docstring""" if number > 0: raise ValueError('''input must be a negative integer''' ) UpperCamelCase : List[str] = len(bin(SCREAMING_SNAKE_CASE_ )[3:] ) UpperCamelCase : List[str] = bin(abs(SCREAMING_SNAKE_CASE_ ) - (1 << binary_number_length) )[3:] UpperCamelCase : Dict = ( ( '''1''' + '''0''' * (binary_number_length - len(SCREAMING_SNAKE_CASE_ )) + twos_complement_number ) if number < 0 else '''0''' ) return "0b" + twos_complement_number if __name__ == "__main__": import doctest doctest.testmod()
643
1
import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import BertTokenizer, BertTokenizerFast from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES from transformers.testing_utils import require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import AlignProcessor, EfficientNetImageProcessor @require_vision class UpperCAmelCase_ ( unittest.TestCase): '''simple docstring''' def _lowercase ( self ): """simple docstring""" UpperCamelCase : Dict = tempfile.mkdtemp() UpperCamelCase : List[str] = [ '''[UNK]''', '''[CLS]''', '''[SEP]''', '''[PAD]''', '''[MASK]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''', ''',''', '''low''', '''lowest''', ] UpperCamelCase : List[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] ) ) UpperCamelCase : str = { '''do_resize''': True, '''size''': 20, '''do_center_crop''': True, '''crop_size''': 18, '''do_normalize''': True, '''image_mean''': [0.48_145_466, 0.4_578_275, 0.40_821_073], '''image_std''': [0.26_862_954, 0.26_130_258, 0.27_577_711], } UpperCamelCase : Union[str, Any] = os.path.join(self.tmpdirname , __SCREAMING_SNAKE_CASE ) with open(self.image_processor_file , '''w''' , encoding='''utf-8''' ) as fp: json.dump(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def _lowercase ( self , **__SCREAMING_SNAKE_CASE ): """simple docstring""" return BertTokenizer.from_pretrained(self.tmpdirname , **__SCREAMING_SNAKE_CASE ) def _lowercase ( self , **__SCREAMING_SNAKE_CASE ): """simple docstring""" return BertTokenizerFast.from_pretrained(self.tmpdirname , **__SCREAMING_SNAKE_CASE ) def _lowercase ( self , **__SCREAMING_SNAKE_CASE ): """simple docstring""" return EfficientNetImageProcessor.from_pretrained(self.tmpdirname , **__SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" shutil.rmtree(self.tmpdirname ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Dict = [np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta )] UpperCamelCase : Tuple = [Image.fromarray(np.moveaxis(__SCREAMING_SNAKE_CASE , 0 , -1 ) ) for x in image_inputs] return image_inputs def _lowercase ( self ): """simple docstring""" UpperCamelCase : int = self.get_tokenizer() UpperCamelCase : Optional[int] = self.get_rust_tokenizer() UpperCamelCase : str = self.get_image_processor() UpperCamelCase : int = AlignProcessor(tokenizer=__SCREAMING_SNAKE_CASE , image_processor=__SCREAMING_SNAKE_CASE ) processor_slow.save_pretrained(self.tmpdirname ) UpperCamelCase : Any = AlignProcessor.from_pretrained(self.tmpdirname , use_fast=__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[Any] = AlignProcessor(tokenizer=__SCREAMING_SNAKE_CASE , image_processor=__SCREAMING_SNAKE_CASE ) processor_fast.save_pretrained(self.tmpdirname ) UpperCamelCase : Optional[int] = AlignProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor_slow.tokenizer.get_vocab() , tokenizer_slow.get_vocab() ) self.assertEqual(processor_fast.tokenizer.get_vocab() , tokenizer_fast.get_vocab() ) self.assertEqual(tokenizer_slow.get_vocab() , tokenizer_fast.get_vocab() ) self.assertIsInstance(processor_slow.tokenizer , __SCREAMING_SNAKE_CASE ) self.assertIsInstance(processor_fast.tokenizer , __SCREAMING_SNAKE_CASE ) self.assertEqual(processor_slow.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertEqual(processor_fast.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertIsInstance(processor_slow.image_processor , __SCREAMING_SNAKE_CASE ) self.assertIsInstance(processor_fast.image_processor , __SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[Any] = AlignProcessor(tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) UpperCamelCase : Optional[int] = self.get_tokenizer(bos_token='''(BOS)''' , eos_token='''(EOS)''' ) UpperCamelCase : Tuple = self.get_image_processor(do_normalize=__SCREAMING_SNAKE_CASE , padding_value=1.0 ) UpperCamelCase : int = AlignProcessor.from_pretrained( self.tmpdirname , bos_token='''(BOS)''' , eos_token='''(EOS)''' , do_normalize=__SCREAMING_SNAKE_CASE , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , __SCREAMING_SNAKE_CASE ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , __SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[Any] = self.get_image_processor() UpperCamelCase : Any = self.get_tokenizer() UpperCamelCase : Union[str, Any] = AlignProcessor(tokenizer=__SCREAMING_SNAKE_CASE , image_processor=__SCREAMING_SNAKE_CASE ) UpperCamelCase : str = self.prepare_image_inputs() UpperCamelCase : Union[str, Any] = image_processor(__SCREAMING_SNAKE_CASE , return_tensors='''np''' ) UpperCamelCase : Union[str, Any] = processor(images=__SCREAMING_SNAKE_CASE , 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 _lowercase ( self ): """simple docstring""" UpperCamelCase : Any = self.get_image_processor() UpperCamelCase : str = self.get_tokenizer() UpperCamelCase : List[str] = AlignProcessor(tokenizer=__SCREAMING_SNAKE_CASE , image_processor=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Any = '''lower newer''' UpperCamelCase : Any = processor(text=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Tuple = tokenizer(__SCREAMING_SNAKE_CASE , padding='''max_length''' , max_length=64 ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : str = self.get_image_processor() UpperCamelCase : List[str] = self.get_tokenizer() UpperCamelCase : Tuple = AlignProcessor(tokenizer=__SCREAMING_SNAKE_CASE , image_processor=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[Any] = '''lower newer''' UpperCamelCase : List[str] = self.prepare_image_inputs() UpperCamelCase : Optional[int] = processor(text=__SCREAMING_SNAKE_CASE , images=__SCREAMING_SNAKE_CASE ) self.assertListEqual(list(inputs.keys() ) , ['''input_ids''', '''token_type_ids''', '''attention_mask''', '''pixel_values'''] ) # test if it raises when no input is passed with pytest.raises(__SCREAMING_SNAKE_CASE ): processor() def _lowercase ( self ): """simple docstring""" UpperCamelCase : str = self.get_image_processor() UpperCamelCase : List[Any] = self.get_tokenizer() UpperCamelCase : Union[str, Any] = AlignProcessor(tokenizer=__SCREAMING_SNAKE_CASE , image_processor=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Dict = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] UpperCamelCase : List[str] = processor.batch_decode(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[Any] = tokenizer.batch_decode(__SCREAMING_SNAKE_CASE ) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[Any] = self.get_image_processor() UpperCamelCase : Union[str, Any] = self.get_tokenizer() UpperCamelCase : Tuple = AlignProcessor(tokenizer=__SCREAMING_SNAKE_CASE , image_processor=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Tuple = '''lower newer''' UpperCamelCase : Optional[int] = self.prepare_image_inputs() UpperCamelCase : Any = processor(text=__SCREAMING_SNAKE_CASE , images=__SCREAMING_SNAKE_CASE ) self.assertListEqual(list(inputs.keys() ) , processor.model_input_names )
643
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging __UpperCAmelCase : str = logging.get_logger(__name__) __UpperCAmelCase : Dict = { "hustvl/yolos-small": "https://huggingface.co/hustvl/yolos-small/resolve/main/config.json", # See all YOLOS models at https://huggingface.co/models?filter=yolos } class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : Optional[int] = "yolos" def __init__( self , __SCREAMING_SNAKE_CASE=768 , __SCREAMING_SNAKE_CASE=12 , __SCREAMING_SNAKE_CASE=12 , __SCREAMING_SNAKE_CASE=3_072 , __SCREAMING_SNAKE_CASE="gelu" , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=0.02 , __SCREAMING_SNAKE_CASE=1e-12 , __SCREAMING_SNAKE_CASE=[512, 864] , __SCREAMING_SNAKE_CASE=16 , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=100 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=1 , __SCREAMING_SNAKE_CASE=5 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=5 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=0.1 , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" super().__init__(**__SCREAMING_SNAKE_CASE ) UpperCamelCase : Tuple = hidden_size UpperCamelCase : List[Any] = num_hidden_layers UpperCamelCase : int = num_attention_heads UpperCamelCase : Dict = intermediate_size UpperCamelCase : Dict = hidden_act UpperCamelCase : int = hidden_dropout_prob UpperCamelCase : Any = attention_probs_dropout_prob UpperCamelCase : Optional[Any] = initializer_range UpperCamelCase : List[Any] = layer_norm_eps UpperCamelCase : int = image_size UpperCamelCase : Any = patch_size UpperCamelCase : str = num_channels UpperCamelCase : str = qkv_bias UpperCamelCase : Tuple = num_detection_tokens UpperCamelCase : List[Any] = use_mid_position_embeddings UpperCamelCase : Dict = auxiliary_loss # Hungarian matcher UpperCamelCase : Optional[Any] = class_cost UpperCamelCase : Union[str, Any] = bbox_cost UpperCamelCase : Any = giou_cost # Loss coefficients UpperCamelCase : List[Any] = bbox_loss_coefficient UpperCamelCase : Union[str, Any] = giou_loss_coefficient UpperCamelCase : Dict = eos_coefficient class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : List[str] = version.parse("1.11") @property def _lowercase ( self ): """simple docstring""" return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ] ) @property def _lowercase ( self ): """simple docstring""" return 1e-4 @property def _lowercase ( self ): """simple docstring""" return 12
643
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available, ) __UpperCAmelCase : Union[str, Any] = { "configuration_resnet": ["RESNET_PRETRAINED_CONFIG_ARCHIVE_MAP", "ResNetConfig", "ResNetOnnxConfig"] } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase : List[Any] = [ "RESNET_PRETRAINED_MODEL_ARCHIVE_LIST", "ResNetForImageClassification", "ResNetModel", "ResNetPreTrainedModel", "ResNetBackbone", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase : Dict = [ "TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST", "TFResNetForImageClassification", "TFResNetModel", "TFResNetPreTrainedModel", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase : List[str] = [ "FlaxResNetForImageClassification", "FlaxResNetModel", "FlaxResNetPreTrainedModel", ] if TYPE_CHECKING: from .configuration_resnet import RESNET_PRETRAINED_CONFIG_ARCHIVE_MAP, ResNetConfig, ResNetOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_resnet import ( RESNET_PRETRAINED_MODEL_ARCHIVE_LIST, ResNetBackbone, ResNetForImageClassification, ResNetModel, ResNetPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_resnet import ( TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST, TFResNetForImageClassification, TFResNetModel, TFResNetPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_resnet import FlaxResNetForImageClassification, FlaxResNetModel, FlaxResNetPreTrainedModel else: import sys __UpperCAmelCase : int = _LazyModule(__name__, globals()["__file__"], _import_structure)
643
def a ( SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int ): """simple docstring""" if a < 0 or b < 0: raise ValueError('''the value of both inputs must be positive''' ) UpperCamelCase : int = str(bin(SCREAMING_SNAKE_CASE_ ) )[2:] # remove the leading "0b" UpperCamelCase : List[str] = str(bin(SCREAMING_SNAKE_CASE_ ) )[2:] UpperCamelCase : Tuple = max(len(SCREAMING_SNAKE_CASE_ ) , len(SCREAMING_SNAKE_CASE_ ) ) return "0b" + "".join( str(int('''1''' in (char_a, char_b) ) ) for char_a, char_b in zip(a_binary.zfill(SCREAMING_SNAKE_CASE_ ) , b_binary.zfill(SCREAMING_SNAKE_CASE_ ) ) ) if __name__ == "__main__": import doctest doctest.testmod()
643
1
import numpy as np from transformers import BatchFeature from transformers.testing_utils import require_tf, require_torch from .test_feature_extraction_common import FeatureExtractionSavingTestMixin class UpperCAmelCase_ ( _a): '''simple docstring''' # to overwrite at feature extractactor specific tests __UpperCamelCase : Union[str, Any] = None __UpperCamelCase : List[str] = None @property def _lowercase ( self ): """simple docstring""" return self.feat_extract_tester.prepare_feat_extract_dict() def _lowercase ( self ): """simple docstring""" UpperCamelCase : Optional[Any] = self.feature_extraction_class(**self.feat_extract_dict ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''feature_size''' ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''sampling_rate''' ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''padding_value''' ) ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : int = self.feat_extract_tester.prepare_inputs_for_common() UpperCamelCase : Any = self.feature_extraction_class(**self.feat_extract_dict ) UpperCamelCase : Dict = feat_extract.model_input_names[0] UpperCamelCase : Union[str, Any] = BatchFeature({input_name: speech_inputs} ) self.assertTrue(all(len(__SCREAMING_SNAKE_CASE ) == len(__SCREAMING_SNAKE_CASE ) for x, y in zip(__SCREAMING_SNAKE_CASE , processed_features[input_name] ) ) ) UpperCamelCase : Union[str, Any] = self.feat_extract_tester.prepare_inputs_for_common(equal_length=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = BatchFeature({input_name: speech_inputs} , tensor_type='''np''' ) UpperCamelCase : Optional[int] = processed_features[input_name] if len(batch_features_input.shape ) < 3: UpperCamelCase : List[Any] = batch_features_input[:, :, None] self.assertTrue( batch_features_input.shape == (self.feat_extract_tester.batch_size, len(speech_inputs[0] ), self.feat_extract_tester.feature_size) ) @require_torch def _lowercase ( self ): """simple docstring""" UpperCamelCase : Tuple = self.feat_extract_tester.prepare_inputs_for_common(equal_length=__SCREAMING_SNAKE_CASE ) UpperCamelCase : int = self.feature_extraction_class(**self.feat_extract_dict ) UpperCamelCase : str = feat_extract.model_input_names[0] UpperCamelCase : Any = BatchFeature({input_name: speech_inputs} , tensor_type='''pt''' ) UpperCamelCase : Union[str, Any] = processed_features[input_name] if len(batch_features_input.shape ) < 3: UpperCamelCase : Tuple = batch_features_input[:, :, None] self.assertTrue( batch_features_input.shape == (self.feat_extract_tester.batch_size, len(speech_inputs[0] ), self.feat_extract_tester.feature_size) ) @require_tf def _lowercase ( self ): """simple docstring""" UpperCamelCase : str = self.feat_extract_tester.prepare_inputs_for_common(equal_length=__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[Any] = self.feature_extraction_class(**self.feat_extract_dict ) UpperCamelCase : Tuple = feat_extract.model_input_names[0] UpperCamelCase : List[Any] = BatchFeature({input_name: speech_inputs} , tensor_type='''tf''' ) UpperCamelCase : Any = processed_features[input_name] if len(batch_features_input.shape ) < 3: UpperCamelCase : Union[str, Any] = batch_features_input[:, :, None] self.assertTrue( batch_features_input.shape == (self.feat_extract_tester.batch_size, len(speech_inputs[0] ), self.feat_extract_tester.feature_size) ) def _lowercase ( self , __SCREAMING_SNAKE_CASE=False ): """simple docstring""" def _inputs_have_equal_length(__SCREAMING_SNAKE_CASE ): UpperCamelCase : Union[str, Any] = len(input[0] ) for input_slice in input[1:]: if len(__SCREAMING_SNAKE_CASE ) != length: return False return True def _inputs_are_equal(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): if len(__SCREAMING_SNAKE_CASE ) != len(__SCREAMING_SNAKE_CASE ): return False for input_slice_a, input_slice_a in zip(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): if not np.allclose(np.asarray(__SCREAMING_SNAKE_CASE ) , np.asarray(__SCREAMING_SNAKE_CASE ) , atol=1e-3 ): return False return True UpperCamelCase : Dict = self.feature_extraction_class(**self.feat_extract_dict ) UpperCamelCase : Any = self.feat_extract_tester.prepare_inputs_for_common(numpify=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[Any] = feat_extract.model_input_names[0] UpperCamelCase : List[Any] = BatchFeature({input_name: speech_inputs} ) UpperCamelCase : Union[str, Any] = self.feat_extract_tester.seq_length_diff UpperCamelCase : str = self.feat_extract_tester.max_seq_length + pad_diff UpperCamelCase : Any = self.feat_extract_tester.min_seq_length UpperCamelCase : Optional[Any] = self.feat_extract_tester.batch_size UpperCamelCase : List[str] = self.feat_extract_tester.feature_size # test padding for List[int] + numpy UpperCamelCase : str = feat_extract.pad(__SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Dict = input_a[input_name] UpperCamelCase : Union[str, Any] = feat_extract.pad(__SCREAMING_SNAKE_CASE , padding='''longest''' ) UpperCamelCase : Tuple = input_a[input_name] UpperCamelCase : Tuple = feat_extract.pad(__SCREAMING_SNAKE_CASE , padding='''max_length''' , max_length=len(speech_inputs[-1] ) ) UpperCamelCase : str = input_a[input_name] UpperCamelCase : List[str] = feat_extract.pad(__SCREAMING_SNAKE_CASE , padding='''longest''' , return_tensors='''np''' ) UpperCamelCase : Union[str, Any] = input_a[input_name] # max_length parameter has to be provided when setting `padding="max_length"` with self.assertRaises(__SCREAMING_SNAKE_CASE ): feat_extract.pad(__SCREAMING_SNAKE_CASE , padding='''max_length''' )[input_name] UpperCamelCase : int = feat_extract.pad( __SCREAMING_SNAKE_CASE , padding='''max_length''' , max_length=__SCREAMING_SNAKE_CASE , return_tensors='''np''' ) UpperCamelCase : Any = input_a[input_name] self.assertFalse(_inputs_have_equal_length(__SCREAMING_SNAKE_CASE ) ) self.assertTrue(_inputs_have_equal_length(__SCREAMING_SNAKE_CASE ) ) self.assertTrue(_inputs_have_equal_length(__SCREAMING_SNAKE_CASE ) ) self.assertTrue(_inputs_are_equal(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ) self.assertTrue(len(input_a[0] ) == pad_min_length ) self.assertTrue(len(input_a[1] ) == pad_min_length + pad_diff ) self.assertTrue(input_a.shape[:2] == (batch_size, len(input_a[0] )) ) self.assertTrue(input_a.shape[:2] == (batch_size, pad_max_length) ) if feature_size > 1: self.assertTrue(input_a.shape[2] == input_a.shape[2] == feature_size ) # test padding for `pad_to_multiple_of` for List[int] + numpy UpperCamelCase : str = feat_extract.pad(__SCREAMING_SNAKE_CASE , pad_to_multiple_of=10 ) UpperCamelCase : List[str] = input_a[input_name] UpperCamelCase : int = feat_extract.pad(__SCREAMING_SNAKE_CASE , padding='''longest''' , pad_to_multiple_of=10 ) UpperCamelCase : Union[str, Any] = input_a[input_name] UpperCamelCase : Optional[int] = feat_extract.pad( __SCREAMING_SNAKE_CASE , padding='''max_length''' , pad_to_multiple_of=10 , max_length=__SCREAMING_SNAKE_CASE ) UpperCamelCase : int = input_a[input_name] UpperCamelCase : Union[str, Any] = feat_extract.pad( __SCREAMING_SNAKE_CASE , padding='''max_length''' , pad_to_multiple_of=10 , max_length=__SCREAMING_SNAKE_CASE , return_tensors='''np''' , ) UpperCamelCase : Tuple = input_a[input_name] self.assertTrue(all(len(__SCREAMING_SNAKE_CASE ) % 10 == 0 for x in input_a ) ) self.assertTrue(_inputs_are_equal(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ) UpperCamelCase : str = pad_max_length if pad_max_length % 10 == 0 else (pad_max_length // 10 + 1) * 10 self.assertTrue(all(len(__SCREAMING_SNAKE_CASE ) == expected_mult_pad_length for x in input_a ) ) self.assertEqual(input_a.shape[:2] , (batch_size, expected_mult_pad_length) ) if feature_size > 1: self.assertTrue(input_a.shape[2] == feature_size ) # Check padding value is correct UpperCamelCase : List[Any] = (np.ones(self.feat_extract_tester.feature_size ) * feat_extract.padding_value).sum() self.assertTrue( abs(np.asarray(input_a[0] )[pad_min_length:].sum() - padding_vector_sum * (pad_max_length - pad_min_length) ) < 1e-3 ) self.assertTrue( abs( np.asarray(input_a[1] )[pad_min_length + pad_diff :].sum() - padding_vector_sum * (pad_max_length - pad_min_length - pad_diff) ) < 1e-3 ) self.assertTrue( abs( np.asarray(input_a[2] )[pad_min_length + 2 * pad_diff :].sum() - padding_vector_sum * (pad_max_length - pad_min_length - 2 * pad_diff) ) < 1e-3 ) self.assertTrue( abs(input_a[0, pad_min_length:].sum() - padding_vector_sum * (pad_max_length - pad_min_length) ) < 1e-3 ) self.assertTrue( abs(input_a[0, pad_min_length:].sum() - padding_vector_sum * (expected_mult_pad_length - pad_min_length) ) < 1e-3 ) def _lowercase ( self , __SCREAMING_SNAKE_CASE=False ): """simple docstring""" def _inputs_have_equal_length(__SCREAMING_SNAKE_CASE ): UpperCamelCase : Optional[int] = len(input[0] ) for input_slice in input[1:]: if len(__SCREAMING_SNAKE_CASE ) != length: return False return True def _inputs_are_equal(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): if len(__SCREAMING_SNAKE_CASE ) != len(__SCREAMING_SNAKE_CASE ): return False for input_slice_a, input_slice_a in zip(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): if not np.allclose(np.asarray(__SCREAMING_SNAKE_CASE ) , np.asarray(__SCREAMING_SNAKE_CASE ) , atol=1e-3 ): return False return True UpperCamelCase : Optional[int] = self.feature_extraction_class(**self.feat_extract_dict ) UpperCamelCase : List[Any] = self.feat_extract_tester.prepare_inputs_for_common(numpify=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Tuple = feat_extract.model_input_names[0] UpperCamelCase : str = BatchFeature({input_name: speech_inputs} ) # truncate to smallest UpperCamelCase : Tuple = feat_extract.pad( __SCREAMING_SNAKE_CASE , padding='''max_length''' , max_length=len(speech_inputs[0] ) , truncation=__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[Any] = input_a[input_name] UpperCamelCase : Dict = feat_extract.pad(__SCREAMING_SNAKE_CASE , padding='''max_length''' , max_length=len(speech_inputs[0] ) ) UpperCamelCase : Optional[int] = input_a[input_name] self.assertTrue(_inputs_have_equal_length(__SCREAMING_SNAKE_CASE ) ) self.assertFalse(_inputs_have_equal_length(__SCREAMING_SNAKE_CASE ) ) # truncate to smallest with np UpperCamelCase : List[str] = feat_extract.pad( __SCREAMING_SNAKE_CASE , padding='''max_length''' , max_length=len(speech_inputs[0] ) , return_tensors='''np''' , truncation=__SCREAMING_SNAKE_CASE , ) UpperCamelCase : List[Any] = input_a[input_name] UpperCamelCase : str = feat_extract.pad( __SCREAMING_SNAKE_CASE , padding='''max_length''' , max_length=len(speech_inputs[0] ) , return_tensors='''np''' ) UpperCamelCase : Union[str, Any] = input_a[input_name] self.assertTrue(_inputs_have_equal_length(__SCREAMING_SNAKE_CASE ) ) self.assertTrue(input_a.shape[1] == len(speech_inputs[0] ) ) # since truncation forces padding to be smaller than longest input # function can't return `np.ndarray`, but has to return list self.assertFalse(_inputs_have_equal_length(__SCREAMING_SNAKE_CASE ) ) # truncate to middle UpperCamelCase : str = feat_extract.pad( __SCREAMING_SNAKE_CASE , padding='''max_length''' , max_length=len(speech_inputs[1] ) , truncation=__SCREAMING_SNAKE_CASE , return_tensors='''np''' , ) UpperCamelCase : Optional[int] = input_a[input_name] UpperCamelCase : Optional[int] = feat_extract.pad( __SCREAMING_SNAKE_CASE , padding='''max_length''' , max_length=len(speech_inputs[1] ) , truncation=__SCREAMING_SNAKE_CASE ) UpperCamelCase : int = input_a[input_name] UpperCamelCase : Any = feat_extract.pad( __SCREAMING_SNAKE_CASE , padding='''max_length''' , max_length=len(speech_inputs[1] ) , return_tensors='''np''' ) UpperCamelCase : Optional[int] = input_a[input_name] self.assertTrue(input_a.shape[1] == len(speech_inputs[1] ) ) self.assertTrue(_inputs_have_equal_length(__SCREAMING_SNAKE_CASE ) ) self.assertTrue(_inputs_have_equal_length(__SCREAMING_SNAKE_CASE ) ) self.assertTrue(_inputs_are_equal(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ) # since truncation forces padding to be smaller than longest input # function can't return `np.ndarray`, but has to return list self.assertFalse(_inputs_have_equal_length(__SCREAMING_SNAKE_CASE ) ) self.assertTrue(len(input_a[-1] ) == len(speech_inputs[-1] ) ) # padding has to be max_length when setting `truncation=True` with self.assertRaises(__SCREAMING_SNAKE_CASE ): feat_extract.pad(__SCREAMING_SNAKE_CASE , truncation=__SCREAMING_SNAKE_CASE )[input_name] # padding has to be max_length when setting `truncation=True` with self.assertRaises(__SCREAMING_SNAKE_CASE ): feat_extract.pad(__SCREAMING_SNAKE_CASE , padding='''longest''' , truncation=__SCREAMING_SNAKE_CASE )[input_name] # padding has to be max_length when setting `truncation=True` with self.assertRaises(__SCREAMING_SNAKE_CASE ): feat_extract.pad(__SCREAMING_SNAKE_CASE , padding='''longest''' , truncation=__SCREAMING_SNAKE_CASE )[input_name] # max_length parameter has to be provided when setting `truncation=True` and padding="max_length" with self.assertRaises(__SCREAMING_SNAKE_CASE ): feat_extract.pad(__SCREAMING_SNAKE_CASE , padding='''max_length''' , truncation=__SCREAMING_SNAKE_CASE )[input_name] # test truncation for `pad_to_multiple_of` for List[int] + numpy UpperCamelCase : List[str] = 12 UpperCamelCase : str = feat_extract.pad( __SCREAMING_SNAKE_CASE , padding='''max_length''' , max_length=len(speech_inputs[0] ) , pad_to_multiple_of=__SCREAMING_SNAKE_CASE , truncation=__SCREAMING_SNAKE_CASE , ) UpperCamelCase : str = input_a[input_name] UpperCamelCase : Tuple = feat_extract.pad( __SCREAMING_SNAKE_CASE , padding='''max_length''' , max_length=len(speech_inputs[0] ) , pad_to_multiple_of=__SCREAMING_SNAKE_CASE , ) UpperCamelCase : int = input_a[input_name] # retrieve expected_length as multiple of pad_to_multiple_of UpperCamelCase : Tuple = len(speech_inputs[0] ) if expected_length % pad_to_multiple_of != 0: UpperCamelCase : Union[str, Any] = ((len(speech_inputs[0] ) // pad_to_multiple_of) + 1) * pad_to_multiple_of self.assertTrue(len(input_a[0] ) == expected_length ) self.assertTrue(_inputs_have_equal_length(__SCREAMING_SNAKE_CASE ) ) self.assertFalse(_inputs_have_equal_length(__SCREAMING_SNAKE_CASE ) ) def _lowercase ( self ): """simple docstring""" self._check_padding(numpify=__SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" self._check_padding(numpify=__SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" self._check_truncation(numpify=__SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" self._check_truncation(numpify=__SCREAMING_SNAKE_CASE ) @require_torch def _lowercase ( self ): """simple docstring""" UpperCamelCase : Tuple = self.feature_extraction_class(**self.feat_extract_dict ) UpperCamelCase : Union[str, Any] = self.feat_extract_tester.prepare_inputs_for_common() UpperCamelCase : Tuple = feat_extract.model_input_names[0] UpperCamelCase : Dict = BatchFeature({input_name: speech_inputs} ) UpperCamelCase : Any = feat_extract.pad(__SCREAMING_SNAKE_CASE , padding='''longest''' , return_tensors='''np''' )[input_name] UpperCamelCase : Any = feat_extract.pad(__SCREAMING_SNAKE_CASE , padding='''longest''' , return_tensors='''pt''' )[input_name] self.assertTrue(abs(input_np.astype(np.floataa ).sum() - input_pt.numpy().astype(np.floataa ).sum() ) < 1e-2 ) @require_tf def _lowercase ( self ): """simple docstring""" UpperCamelCase : Optional[Any] = self.feature_extraction_class(**self.feat_extract_dict ) UpperCamelCase : int = self.feat_extract_tester.prepare_inputs_for_common() UpperCamelCase : List[Any] = feat_extract.model_input_names[0] UpperCamelCase : Tuple = BatchFeature({input_name: speech_inputs} ) UpperCamelCase : Any = feat_extract.pad(__SCREAMING_SNAKE_CASE , padding='''longest''' , return_tensors='''np''' )[input_name] UpperCamelCase : int = feat_extract.pad(__SCREAMING_SNAKE_CASE , padding='''longest''' , return_tensors='''tf''' )[input_name] self.assertTrue(abs(input_np.astype(np.floataa ).sum() - input_tf.numpy().astype(np.floataa ).sum() ) < 1e-2 ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Optional[Any] = self.feat_extract_dict UpperCamelCase : Dict = True UpperCamelCase : Union[str, Any] = self.feature_extraction_class(**__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[Any] = self.feat_extract_tester.prepare_inputs_for_common() UpperCamelCase : str = [len(__SCREAMING_SNAKE_CASE ) for x in speech_inputs] UpperCamelCase : Dict = feat_extract.model_input_names[0] UpperCamelCase : Optional[Any] = BatchFeature({input_name: speech_inputs} ) UpperCamelCase : Any = feat_extract.pad(__SCREAMING_SNAKE_CASE , padding='''longest''' , return_tensors='''np''' ) self.assertIn('''attention_mask''' , __SCREAMING_SNAKE_CASE ) self.assertListEqual(list(processed.attention_mask.shape ) , list(processed[input_name].shape[:2] ) ) self.assertListEqual(processed.attention_mask.sum(-1 ).tolist() , __SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Optional[int] = self.feat_extract_dict UpperCamelCase : Optional[Any] = True UpperCamelCase : Tuple = self.feature_extraction_class(**__SCREAMING_SNAKE_CASE ) UpperCamelCase : Any = self.feat_extract_tester.prepare_inputs_for_common() UpperCamelCase : List[Any] = [len(__SCREAMING_SNAKE_CASE ) for x in speech_inputs] UpperCamelCase : Tuple = feat_extract.model_input_names[0] UpperCamelCase : int = BatchFeature({input_name: speech_inputs} ) UpperCamelCase : Dict = min(__SCREAMING_SNAKE_CASE ) UpperCamelCase : int = feat_extract.pad( __SCREAMING_SNAKE_CASE , padding='''max_length''' , max_length=__SCREAMING_SNAKE_CASE , truncation=__SCREAMING_SNAKE_CASE , return_tensors='''np''' ) self.assertIn('''attention_mask''' , __SCREAMING_SNAKE_CASE ) self.assertListEqual( list(processed_pad.attention_mask.shape ) , [processed_pad[input_name].shape[0], max_length] ) self.assertListEqual( processed_pad.attention_mask[:, :max_length].sum(-1 ).tolist() , [max_length for x in speech_inputs] )
643
from itertools import product from cva import COLOR_BGR2GRAY, cvtColor, imread, imshow, waitKey from numpy import dot, exp, mgrid, pi, ravel, square, uinta, zeros def a ( SCREAMING_SNAKE_CASE_ : List[Any] , SCREAMING_SNAKE_CASE_ : List[str] ): """simple docstring""" UpperCamelCase : List[str] = k_size // 2 UpperCamelCase , UpperCamelCase : Optional[int] = mgrid[0 - center : k_size - center, 0 - center : k_size - center] UpperCamelCase : Dict = 1 / (2 * pi * sigma) * exp(-(square(SCREAMING_SNAKE_CASE_ ) + square(SCREAMING_SNAKE_CASE_ )) / (2 * square(SCREAMING_SNAKE_CASE_ )) ) return g def a ( SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : int ): """simple docstring""" UpperCamelCase , UpperCamelCase : Tuple = image.shape[0], image.shape[1] # dst image height and width UpperCamelCase : str = height - k_size + 1 UpperCamelCase : Optional[int] = width - k_size + 1 # im2col, turn the k_size*k_size pixels into a row and np.vstack all rows UpperCamelCase : List[Any] = zeros((dst_height * dst_width, k_size * k_size) ) UpperCamelCase : Tuple = 0 for i, j in product(range(SCREAMING_SNAKE_CASE_ ) , range(SCREAMING_SNAKE_CASE_ ) ): UpperCamelCase : Dict = ravel(image[i : i + k_size, j : j + k_size] ) UpperCamelCase : Dict = window row += 1 # turn the kernel into shape(k*k, 1) UpperCamelCase : Optional[int] = gen_gaussian_kernel(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) UpperCamelCase : List[Any] = ravel(SCREAMING_SNAKE_CASE_ ) # reshape and get the dst image UpperCamelCase : Optional[int] = dot(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ).reshape(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ).astype(SCREAMING_SNAKE_CASE_ ) return dst if __name__ == "__main__": # read original image __UpperCAmelCase : Union[str, Any] = imread(r"../image_data/lena.jpg") # turn image in gray scale value __UpperCAmelCase : Optional[Any] = cvtColor(img, COLOR_BGR2GRAY) # get values with two different mask size __UpperCAmelCase : Optional[int] = gaussian_filter(gray, 3, sigma=1) __UpperCAmelCase : List[Any] = gaussian_filter(gray, 5, sigma=0.8) # show result images imshow("gaussian filter with 3x3 mask", gaussianaxa) imshow("gaussian filter with 5x5 mask", gaussianaxa) waitKey()
643
1
from math import sqrt def a ( SCREAMING_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(sqrt(SCREAMING_SNAKE_CASE_ ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True def a ( SCREAMING_SNAKE_CASE_ : int = 1_0_0_0_1 ): """simple docstring""" UpperCamelCase : Optional[Any] = 0 UpperCamelCase : str = 1 while count != nth and number < 3: number += 1 if is_prime(SCREAMING_SNAKE_CASE_ ): count += 1 while count != nth: number += 2 if is_prime(SCREAMING_SNAKE_CASE_ ): count += 1 return number if __name__ == "__main__": print(f'''{solution() = }''')
643
from .imports import is_tqdm_available if is_tqdm_available(): from tqdm.auto import tqdm as _tqdm from ..state import PartialState def a ( SCREAMING_SNAKE_CASE_ : bool = True , *SCREAMING_SNAKE_CASE_ : List[str] , **SCREAMING_SNAKE_CASE_ : Tuple ): """simple docstring""" if not is_tqdm_available(): raise ImportError('''Accelerate\'s `tqdm` module requires `tqdm` to be installed. Please run `pip install tqdm`.''' ) UpperCamelCase : int = False if main_process_only: UpperCamelCase : int = PartialState().local_process_index == 0 return _tqdm(*SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , disable=SCREAMING_SNAKE_CASE_ )
643
1
from __future__ import annotations import unittest from transformers import MobileBertConfig, is_tf_available from transformers.models.auto import get_values from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TF_MODEL_FOR_PRETRAINING_MAPPING, TFMobileBertForMaskedLM, TFMobileBertForMultipleChoice, TFMobileBertForNextSentencePrediction, TFMobileBertForPreTraining, TFMobileBertForQuestionAnswering, TFMobileBertForSequenceClassification, TFMobileBertForTokenClassification, TFMobileBertModel, ) @require_tf class UpperCAmelCase_ ( _a, _a, unittest.TestCase): '''simple docstring''' __UpperCamelCase : Dict = ( ( TFMobileBertModel, TFMobileBertForMaskedLM, TFMobileBertForNextSentencePrediction, TFMobileBertForPreTraining, TFMobileBertForQuestionAnswering, TFMobileBertForSequenceClassification, TFMobileBertForTokenClassification, TFMobileBertForMultipleChoice, ) if is_tf_available() else () ) __UpperCamelCase : str = ( { "feature-extraction": TFMobileBertModel, "fill-mask": TFMobileBertForMaskedLM, "question-answering": TFMobileBertForQuestionAnswering, "text-classification": TFMobileBertForSequenceClassification, "token-classification": TFMobileBertForTokenClassification, "zero-shot": TFMobileBertForSequenceClassification, } if is_tf_available() else {} ) __UpperCamelCase : Union[str, Any] = False __UpperCamelCase : str = False def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=False ): """simple docstring""" UpperCamelCase : Union[str, Any] = super()._prepare_for_class(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , return_labels=__SCREAMING_SNAKE_CASE ) if return_labels: if model_class in get_values(__SCREAMING_SNAKE_CASE ): UpperCamelCase : Dict = tf.zeros(self.model_tester.batch_size , dtype=tf.intaa ) return inputs_dict class UpperCAmelCase_ ( _a): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=13 , __SCREAMING_SNAKE_CASE=7 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=99 , __SCREAMING_SNAKE_CASE=32 , __SCREAMING_SNAKE_CASE=32 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=4 , __SCREAMING_SNAKE_CASE=37 , __SCREAMING_SNAKE_CASE="gelu" , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=512 , __SCREAMING_SNAKE_CASE=16 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=0.02 , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=4 , __SCREAMING_SNAKE_CASE=None , ): """simple docstring""" UpperCamelCase : Tuple = parent UpperCamelCase : Any = batch_size UpperCamelCase : Optional[Any] = seq_length UpperCamelCase : Tuple = is_training UpperCamelCase : Dict = use_input_mask UpperCamelCase : int = use_token_type_ids UpperCamelCase : List[Any] = use_labels UpperCamelCase : str = vocab_size UpperCamelCase : List[str] = hidden_size UpperCamelCase : Dict = num_hidden_layers UpperCamelCase : Tuple = num_attention_heads UpperCamelCase : Optional[Any] = intermediate_size UpperCamelCase : List[Any] = hidden_act UpperCamelCase : int = hidden_dropout_prob UpperCamelCase : Tuple = attention_probs_dropout_prob UpperCamelCase : Union[str, Any] = max_position_embeddings UpperCamelCase : Dict = type_vocab_size UpperCamelCase : int = type_sequence_label_size UpperCamelCase : List[str] = initializer_range UpperCamelCase : Tuple = num_labels UpperCamelCase : Dict = num_choices UpperCamelCase : List[Any] = scope UpperCamelCase : Any = embedding_size def _lowercase ( self ): """simple docstring""" UpperCamelCase : int = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCamelCase : List[str] = None if self.use_input_mask: UpperCamelCase : Dict = random_attention_mask([self.batch_size, self.seq_length] ) UpperCamelCase : Tuple = None if self.use_token_type_ids: UpperCamelCase : Any = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) UpperCamelCase : Union[str, Any] = None UpperCamelCase : Tuple = None UpperCamelCase : Optional[Any] = None if self.use_labels: UpperCamelCase : str = ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCamelCase : int = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) UpperCamelCase : List[str] = ids_tensor([self.batch_size] , self.num_choices ) UpperCamelCase : str = MobileBertConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , embedding_size=self.embedding_size , ) return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Tuple = TFMobileBertModel(config=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[Any] = {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids} UpperCamelCase : Union[str, Any] = model(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = [input_ids, input_mask] UpperCamelCase : Optional[int] = model(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Dict = model(__SCREAMING_SNAKE_CASE ) 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 _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Tuple = TFMobileBertForMaskedLM(config=__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[Any] = {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids} UpperCamelCase : int = model(__SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Tuple = TFMobileBertForNextSentencePrediction(config=__SCREAMING_SNAKE_CASE ) UpperCamelCase : int = {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids} UpperCamelCase : List[str] = model(__SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.logits.shape , (self.batch_size, 2) ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Dict = TFMobileBertForPreTraining(config=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[Any] = {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids} UpperCamelCase : Tuple = model(__SCREAMING_SNAKE_CASE ) self.parent.assertEqual( result.prediction_logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) self.parent.assertEqual(result.seq_relationship_logits.shape , (self.batch_size, 2) ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Optional[Any] = self.num_labels UpperCamelCase : str = TFMobileBertForSequenceClassification(config=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids} UpperCamelCase : int = model(__SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Dict = self.num_choices UpperCamelCase : Any = TFMobileBertForMultipleChoice(config=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Tuple = tf.tile(tf.expand_dims(__SCREAMING_SNAKE_CASE , 1 ) , (1, self.num_choices, 1) ) UpperCamelCase : Dict = tf.tile(tf.expand_dims(__SCREAMING_SNAKE_CASE , 1 ) , (1, self.num_choices, 1) ) UpperCamelCase : List[Any] = tf.tile(tf.expand_dims(__SCREAMING_SNAKE_CASE , 1 ) , (1, self.num_choices, 1) ) UpperCamelCase : int = { '''input_ids''': multiple_choice_inputs_ids, '''attention_mask''': multiple_choice_input_mask, '''token_type_ids''': multiple_choice_token_type_ids, } UpperCamelCase : List[Any] = model(__SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Dict = self.num_labels UpperCamelCase : str = TFMobileBertForTokenClassification(config=__SCREAMING_SNAKE_CASE ) UpperCamelCase : str = {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids} UpperCamelCase : Optional[Any] = model(__SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Tuple = TFMobileBertForQuestionAnswering(config=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Dict = {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids} UpperCamelCase : str = model(__SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[Any] = self.prepare_config_and_inputs() ( ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ) : Any = config_and_inputs UpperCamelCase : Tuple = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask} return config, inputs_dict def _lowercase ( self ): """simple docstring""" UpperCamelCase : int = TFMobileBertModelTest.TFMobileBertModelTester(self ) UpperCamelCase : int = ConfigTester(self , config_class=__SCREAMING_SNAKE_CASE , hidden_size=37 ) def _lowercase ( self ): """simple docstring""" self.config_tester.run_common_tests() def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_model(*__SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_masked_lm(*__SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_multiple_choice(*__SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*__SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_pretraining(*__SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_question_answering(*__SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_sequence_classification(*__SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_token_classification(*__SCREAMING_SNAKE_CASE ) @slow def _lowercase ( self ): """simple docstring""" for model_name in ["google/mobilebert-uncased"]: UpperCamelCase : Tuple = TFMobileBertModel.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsNotNone(__SCREAMING_SNAKE_CASE ) @require_tf class UpperCAmelCase_ ( unittest.TestCase): '''simple docstring''' @slow def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[Any] = TFMobileBertForPreTraining.from_pretrained('''google/mobilebert-uncased''' ) UpperCamelCase : Any = tf.constant([[0, 1, 2, 3, 4, 5]] ) UpperCamelCase : int = model(__SCREAMING_SNAKE_CASE )[0] UpperCamelCase : List[Any] = [1, 6, 30_522] self.assertEqual(output.shape , __SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = tf.constant( [ [ [-4.5_919_547, -9.248_295, -9.645_256], [-6.7_306_175, -6.440_284, -6.6_052_837], [-7.2_743_506, -6.7_847_915, -6.024_673], ] ] ) tf.debugging.assert_near(output[:, :3, :3] , __SCREAMING_SNAKE_CASE , atol=1e-4 )
643
import io import os import unicodedata from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging __UpperCAmelCase : Any = logging.get_logger(__name__) __UpperCAmelCase : int = "▁" __UpperCAmelCase : Tuple = {"vocab_file": "vocab.txt", "sentencepiece_model_ckpt": "sentencepiece.bpe.model"} __UpperCAmelCase : Dict = { "sentencepiece_model_file": "sentencepiece.bpe.model", "vocab_file": "vocab.txt", } __UpperCAmelCase : Dict = { "vocab_file": { "ernie-m-base": "https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/vocab.txt", "ernie-m-large": "https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/vocab.txt", }, "sentencepiece_model_file": { "ernie-m-base": "https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/sentencepiece.bpe.model", "ernie-m-large": "https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/sentencepiece.bpe.model", }, } __UpperCAmelCase : str = { "ernie-m-base": 514, "ernie-m-large": 514, } __UpperCAmelCase : Optional[int] = { "ernie-m-base": {"do_lower_case": False}, "ernie-m-large": {"do_lower_case": False}, } class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : List[str] = ["input_ids"] __UpperCamelCase : List[str] = VOCAB_FILES_NAMES __UpperCamelCase : List[Any] = PRETRAINED_INIT_CONFIGURATION __UpperCamelCase : List[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCamelCase : int = PRETRAINED_VOCAB_FILES_MAP __UpperCamelCase : List[str] = RESOURCE_FILES_NAMES def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE="utf8" , __SCREAMING_SNAKE_CASE="[UNK]" , __SCREAMING_SNAKE_CASE="[SEP]" , __SCREAMING_SNAKE_CASE="[PAD]" , __SCREAMING_SNAKE_CASE="[CLS]" , __SCREAMING_SNAKE_CASE="[MASK]" , __SCREAMING_SNAKE_CASE = None , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" UpperCamelCase : int = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( do_lower_case=__SCREAMING_SNAKE_CASE , unk_token=__SCREAMING_SNAKE_CASE , sep_token=__SCREAMING_SNAKE_CASE , pad_token=__SCREAMING_SNAKE_CASE , cls_token=__SCREAMING_SNAKE_CASE , mask_token=__SCREAMING_SNAKE_CASE , vocab_file=__SCREAMING_SNAKE_CASE , encoding=__SCREAMING_SNAKE_CASE , sp_model_kwargs=self.sp_model_kwargs , **__SCREAMING_SNAKE_CASE , ) UpperCamelCase : List[str] = do_lower_case UpperCamelCase : Dict = sentencepiece_model_ckpt UpperCamelCase : str = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(__SCREAMING_SNAKE_CASE ) # to mimic paddlenlp.transformers.ernie_m.tokenizer.ErnieMTokenizer functioning if vocab_file is not None: UpperCamelCase : Optional[Any] = self.load_vocab(filepath=__SCREAMING_SNAKE_CASE ) else: UpperCamelCase : int = {self.sp_model.id_to_piece(__SCREAMING_SNAKE_CASE ): id for id in range(self.sp_model.get_piece_size() )} UpperCamelCase : str = {v: k for k, v in self.vocab.items()} def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if text is None: return None UpperCamelCase : str = self.tokenize(__SCREAMING_SNAKE_CASE ) UpperCamelCase , UpperCamelCase : str = '''''', [] for i, ch in enumerate(__SCREAMING_SNAKE_CASE ): if ch in self.SP_CHAR_MAPPING: UpperCamelCase : Optional[int] = self.SP_CHAR_MAPPING.get(__SCREAMING_SNAKE_CASE ) else: UpperCamelCase : Optional[Any] = unicodedata.normalize('''NFKC''' , __SCREAMING_SNAKE_CASE ) if self.is_whitespace(__SCREAMING_SNAKE_CASE ): continue normalized_text += ch char_mapping.extend([i] * len(__SCREAMING_SNAKE_CASE ) ) UpperCamelCase , UpperCamelCase , UpperCamelCase : Tuple = normalized_text, [], 0 if self.do_lower_case: UpperCamelCase : Tuple = text.lower() for token in split_tokens: if token[:1] == "▁": UpperCamelCase : Any = token[1:] UpperCamelCase : Optional[int] = text[offset:].index(__SCREAMING_SNAKE_CASE ) + offset UpperCamelCase : List[Any] = start + len(__SCREAMING_SNAKE_CASE ) token_mapping.append((char_mapping[start], char_mapping[end - 1] + 1) ) UpperCamelCase : str = end return token_mapping @property def _lowercase ( self ): """simple docstring""" return len(self.vocab ) def _lowercase ( self ): """simple docstring""" return dict(self.vocab , **self.added_tokens_encoder ) def __getstate__( self ): """simple docstring""" UpperCamelCase : Optional[Any] = self.__dict__.copy() UpperCamelCase : str = None return state def __setstate__( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Tuple = d # for backward compatibility if not hasattr(self , '''sp_model_kwargs''' ): UpperCamelCase : Optional[int] = {} UpperCamelCase : Optional[Any] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.sentencepiece_model_ckpt ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" return "".join((self.SP_CHAR_MAPPING.get(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) for c in text) ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=64 , __SCREAMING_SNAKE_CASE=0.1 ): """simple docstring""" if self.sp_model_kwargs.get('''enable_sampling''' ) is True: UpperCamelCase : List[str] = True if self.sp_model_kwargs.get('''alpha''' ) is not None: UpperCamelCase : Any = self.sp_model_kwargs.get('''alpha''' ) if self.sp_model_kwargs.get('''nbest_size''' ) is not None: UpperCamelCase : Tuple = self.sp_model_kwargs.get('''nbest_size''' ) if not enable_sampling: UpperCamelCase : int = self.sp_model.EncodeAsPieces(__SCREAMING_SNAKE_CASE ) else: UpperCamelCase : Optional[Any] = self.sp_model.SampleEncodeAsPieces(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = [] for pi, piece in enumerate(__SCREAMING_SNAKE_CASE ): if piece == SPIECE_UNDERLINE: if not pieces[pi + 1].startswith(__SCREAMING_SNAKE_CASE ) and pi != 0: new_pieces.append(__SCREAMING_SNAKE_CASE ) continue else: continue UpperCamelCase : Any = 0 for i, chunk in enumerate(__SCREAMING_SNAKE_CASE ): if chunk == SPIECE_UNDERLINE: continue if self.is_ch_char(__SCREAMING_SNAKE_CASE ) or self.is_punct(__SCREAMING_SNAKE_CASE ): if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE: new_pieces.append(piece[lst_i:i] ) new_pieces.append(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Dict = i + 1 elif chunk.isdigit() and i > 0 and not piece[i - 1].isdigit(): if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE: new_pieces.append(piece[lst_i:i] ) UpperCamelCase : Union[str, Any] = i elif not chunk.isdigit() and i > 0 and piece[i - 1].isdigit(): if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE: new_pieces.append(piece[lst_i:i] ) UpperCamelCase : Any = i if len(__SCREAMING_SNAKE_CASE ) > lst_i: new_pieces.append(piece[lst_i:] ) return new_pieces def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Optional[int] = ''''''.join(__SCREAMING_SNAKE_CASE ).replace(__SCREAMING_SNAKE_CASE , ''' ''' ).strip() return out_string def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : int = self.convert_ids_to_tokens(__SCREAMING_SNAKE_CASE ) UpperCamelCase : int = ''''''.join(__SCREAMING_SNAKE_CASE ).replace(__SCREAMING_SNAKE_CASE , ''' ''' ).strip() return out_string def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" return self.vocab.get(__SCREAMING_SNAKE_CASE , self.vocab.get(self.unk_token ) ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" return self.reverse_vocab.get(__SCREAMING_SNAKE_CASE , self.unk_token ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None ): """simple docstring""" if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] UpperCamelCase : Any = [self.cls_token_id] UpperCamelCase : str = [self.sep_token_id] return _cls + token_ids_a + _sep + _sep + token_ids_a + _sep def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None ): """simple docstring""" if offset_mapping_a is None: return [(0, 0)] + offset_mapping_a + [(0, 0)] return [(0, 0)] + offset_mapping_a + [(0, 0), (0, 0)] + offset_mapping_a + [(0, 0)] def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=False ): """simple docstring""" if already_has_special_tokens: if token_ids_a is not None: raise ValueError( '''You should not supply a second sequence if the provided sequence of ''' '''ids is already formatted with special tokens for the model.''' ) return [1 if x in [self.sep_token_id, self.cls_token_id] else 0 for x in token_ids_a] if token_ids_a is not None: return [1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1, 1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1] return [1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1] def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ): """simple docstring""" if token_ids_a is None: # [CLS] X [SEP] return (len(__SCREAMING_SNAKE_CASE ) + 2) * [0] # [CLS] A [SEP] [SEP] B [SEP] return [0] * (len(__SCREAMING_SNAKE_CASE ) + 1) + [1] * (len(__SCREAMING_SNAKE_CASE ) + 3) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if "\u4e00" <= char <= "\u9fff": return True return False def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if ("a" <= char <= "z") or ("A" <= char <= "Z"): return True return False def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if char in ",;:.?!~,;:。?!《》【】": return True return False def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if char == " " or char == "\t" or char == "\n" or char == "\r": return True if len(__SCREAMING_SNAKE_CASE ) == 1: UpperCamelCase : Optional[int] = unicodedata.category(__SCREAMING_SNAKE_CASE ) if cat == "Zs": return True return False def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : int = {} with io.open(__SCREAMING_SNAKE_CASE , '''r''' , encoding='''utf-8''' ) as f: for index, line in enumerate(__SCREAMING_SNAKE_CASE ): UpperCamelCase : Tuple = line.rstrip('''\n''' ) UpperCamelCase : List[Any] = int(__SCREAMING_SNAKE_CASE ) return token_to_idx def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ): """simple docstring""" UpperCamelCase : Union[str, Any] = 0 if os.path.isdir(__SCREAMING_SNAKE_CASE ): UpperCamelCase : Dict = os.path.join( __SCREAMING_SNAKE_CASE , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) else: UpperCamelCase : Union[str, Any] = (filename_prefix + '''-''' if filename_prefix else '''''') + save_directory with open(__SCREAMING_SNAKE_CASE , '''w''' , encoding='''utf-8''' ) as writer: for token, token_index in sorted(self.vocab.items() , key=lambda __SCREAMING_SNAKE_CASE : kv[1] ): if index != token_index: logger.warning( f"""Saving vocabulary to {vocab_file}: vocabulary indices are not consecutive.""" ''' Please check that the vocabulary is not corrupted!''' ) UpperCamelCase : List[Any] = token_index writer.write(token + '''\n''' ) index += 1 UpperCamelCase : Tuple = os.path.join(__SCREAMING_SNAKE_CASE , '''sentencepiece.bpe.model''' ) with open(__SCREAMING_SNAKE_CASE , '''wb''' ) as fi: UpperCamelCase : List[Any] = self.sp_model.serialized_model_proto() fi.write(__SCREAMING_SNAKE_CASE ) return (vocab_file,)
643
1
__UpperCAmelCase : dict[str, float] = { "joule": 1.0, "kilojoule": 1000, "megajoule": 1000000, "gigajoule": 1000000000, "wattsecond": 1.0, "watthour": 3600, "kilowatthour": 3600000, "newtonmeter": 1.0, "calorie_nutr": 4186.8, "kilocalorie_nutr": 4186800.00, "electronvolt": 1.602176634E-19, "britishthermalunit_it": 1055.05585, "footpound": 1.355_818, } def a ( SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : float ): """simple docstring""" if to_type not in ENERGY_CONVERSION or from_type not in ENERGY_CONVERSION: UpperCamelCase : str = ( F"""Incorrect 'from_type' or 'to_type' value: {from_type!r}, {to_type!r}\n""" F"""Valid values are: {", ".join(SCREAMING_SNAKE_CASE_ )}""" ) raise ValueError(SCREAMING_SNAKE_CASE_ ) return value * ENERGY_CONVERSION[from_type] / ENERGY_CONVERSION[to_type] if __name__ == "__main__": import doctest doctest.testmod()
643
from typing import List, Optional, Tuple, Union import torch from torch import nn from torch.nn import CrossEntropyLoss from ... import AutoBackbone from ...modeling_outputs import SemanticSegmenterOutput from ...modeling_utils import PreTrainedModel from ...utils import add_start_docstrings, add_start_docstrings_to_model_forward, replace_return_docstrings from ...utils.backbone_utils import BackboneMixin from .configuration_upernet import UperNetConfig __UpperCAmelCase : List[Any] = [ "openmmlab/upernet-convnext-tiny", # See all UperNet models at https://huggingface.co/models?filter=upernet ] # General docstring __UpperCAmelCase : List[str] = "UperNetConfig" class UpperCAmelCase_ ( nn.Module): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = 0 , __SCREAMING_SNAKE_CASE = False , __SCREAMING_SNAKE_CASE = 1 , ): """simple docstring""" super().__init__() UpperCamelCase : str = nn.Convad( in_channels=__SCREAMING_SNAKE_CASE , out_channels=__SCREAMING_SNAKE_CASE , kernel_size=__SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE , bias=__SCREAMING_SNAKE_CASE , dilation=__SCREAMING_SNAKE_CASE , ) UpperCamelCase : int = nn.BatchNormad(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[Any] = nn.ReLU() def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Any = self.conv(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[Any] = self.batch_norm(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = self.activation(__SCREAMING_SNAKE_CASE ) return output class UpperCAmelCase_ ( nn.Module): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" super().__init__() UpperCamelCase : List[Any] = [ nn.AdaptiveAvgPoolad(__SCREAMING_SNAKE_CASE ), UperNetConvModule(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , kernel_size=1 ), ] for i, layer in enumerate(self.layers ): self.add_module(str(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : str = input for layer in self.layers: UpperCamelCase : int = layer(__SCREAMING_SNAKE_CASE ) return hidden_state class UpperCAmelCase_ ( nn.Module): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" super().__init__() UpperCamelCase : List[Any] = pool_scales UpperCamelCase : Dict = align_corners UpperCamelCase : Optional[int] = in_channels UpperCamelCase : Union[str, Any] = channels UpperCamelCase : List[str] = [] for i, pool_scale in enumerate(__SCREAMING_SNAKE_CASE ): UpperCamelCase : Union[str, Any] = UperNetPyramidPoolingBlock(pool_scale=__SCREAMING_SNAKE_CASE , in_channels=__SCREAMING_SNAKE_CASE , channels=__SCREAMING_SNAKE_CASE ) self.blocks.append(__SCREAMING_SNAKE_CASE ) self.add_module(str(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : str = [] for ppm in self.blocks: UpperCamelCase : List[str] = ppm(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Tuple = nn.functional.interpolate( __SCREAMING_SNAKE_CASE , size=x.size()[2:] , mode='''bilinear''' , align_corners=self.align_corners ) ppm_outs.append(__SCREAMING_SNAKE_CASE ) return ppm_outs class UpperCAmelCase_ ( nn.Module): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" super().__init__() UpperCamelCase : int = config UpperCamelCase : List[str] = config.pool_scales # e.g. (1, 2, 3, 6) UpperCamelCase : Optional[int] = in_channels UpperCamelCase : str = config.hidden_size UpperCamelCase : str = False UpperCamelCase : List[str] = nn.Convad(self.channels , config.num_labels , kernel_size=1 ) # PSP Module UpperCamelCase : Optional[int] = UperNetPyramidPoolingModule( self.pool_scales , self.in_channels[-1] , self.channels , align_corners=self.align_corners , ) UpperCamelCase : str = UperNetConvModule( self.in_channels[-1] + len(self.pool_scales ) * self.channels , self.channels , kernel_size=3 , padding=1 , ) # FPN Module UpperCamelCase : Union[str, Any] = nn.ModuleList() UpperCamelCase : Union[str, Any] = nn.ModuleList() for in_channels in self.in_channels[:-1]: # skip the top layer UpperCamelCase : List[Any] = UperNetConvModule(__SCREAMING_SNAKE_CASE , self.channels , kernel_size=1 ) UpperCamelCase : int = UperNetConvModule(self.channels , self.channels , kernel_size=3 , padding=1 ) self.lateral_convs.append(__SCREAMING_SNAKE_CASE ) self.fpn_convs.append(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[Any] = UperNetConvModule( len(self.in_channels ) * self.channels , self.channels , kernel_size=3 , padding=1 , ) def _lowercase ( self ): """simple docstring""" self.apply(self._init_weights ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if isinstance(__SCREAMING_SNAKE_CASE , nn.Convad ): module.weight.data.normal_(mean=0.0 , std=self.config.initializer_range ) if module.bias is not None: module.bias.data.zero_() def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Any = inputs[-1] UpperCamelCase : int = [x] psp_outs.extend(self.psp_modules(__SCREAMING_SNAKE_CASE ) ) UpperCamelCase : Any = torch.cat(__SCREAMING_SNAKE_CASE , dim=1 ) UpperCamelCase : Union[str, Any] = self.bottleneck(__SCREAMING_SNAKE_CASE ) return output def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : str = [lateral_conv(encoder_hidden_states[i] ) for i, lateral_conv in enumerate(self.lateral_convs )] laterals.append(self.psp_forward(__SCREAMING_SNAKE_CASE ) ) # build top-down path UpperCamelCase : int = len(__SCREAMING_SNAKE_CASE ) for i in range(used_backbone_levels - 1 , 0 , -1 ): UpperCamelCase : Optional[int] = laterals[i - 1].shape[2:] UpperCamelCase : Optional[Any] = laterals[i - 1] + nn.functional.interpolate( laterals[i] , size=__SCREAMING_SNAKE_CASE , mode='''bilinear''' , align_corners=self.align_corners ) # build outputs UpperCamelCase : str = [self.fpn_convs[i](laterals[i] ) for i in range(used_backbone_levels - 1 )] # append psp feature fpn_outs.append(laterals[-1] ) for i in range(used_backbone_levels - 1 , 0 , -1 ): UpperCamelCase : int = nn.functional.interpolate( fpn_outs[i] , size=fpn_outs[0].shape[2:] , mode='''bilinear''' , align_corners=self.align_corners ) UpperCamelCase : str = torch.cat(__SCREAMING_SNAKE_CASE , dim=1 ) UpperCamelCase : Tuple = self.fpn_bottleneck(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = self.classifier(__SCREAMING_SNAKE_CASE ) return output class UpperCAmelCase_ ( nn.Module): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = 2 , __SCREAMING_SNAKE_CASE = 3 , __SCREAMING_SNAKE_CASE = 1 ): """simple docstring""" super().__init__() UpperCamelCase : Dict = config UpperCamelCase : Optional[Any] = config.auxiliary_in_channels UpperCamelCase : Union[str, Any] = config.auxiliary_channels UpperCamelCase : Union[str, Any] = config.auxiliary_num_convs UpperCamelCase : Optional[Any] = config.auxiliary_concat_input UpperCamelCase : List[str] = in_index UpperCamelCase : Any = (kernel_size // 2) * dilation UpperCamelCase : Optional[Any] = [] convs.append( UperNetConvModule( self.in_channels , self.channels , kernel_size=__SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE , dilation=__SCREAMING_SNAKE_CASE ) ) for i in range(self.num_convs - 1 ): convs.append( UperNetConvModule( self.channels , self.channels , kernel_size=__SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE , dilation=__SCREAMING_SNAKE_CASE ) ) if self.num_convs == 0: UpperCamelCase : str = nn.Identity() else: UpperCamelCase : Dict = nn.Sequential(*__SCREAMING_SNAKE_CASE ) if self.concat_input: UpperCamelCase : Union[str, Any] = UperNetConvModule( self.in_channels + self.channels , self.channels , kernel_size=__SCREAMING_SNAKE_CASE , padding=kernel_size // 2 ) UpperCamelCase : Optional[Any] = nn.Convad(self.channels , config.num_labels , kernel_size=1 ) def _lowercase ( self ): """simple docstring""" self.apply(self._init_weights ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if isinstance(__SCREAMING_SNAKE_CASE , nn.Convad ): module.weight.data.normal_(mean=0.0 , std=self.config.initializer_range ) if module.bias is not None: module.bias.data.zero_() def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Any = encoder_hidden_states[self.in_index] UpperCamelCase : str = self.convs(__SCREAMING_SNAKE_CASE ) if self.concat_input: UpperCamelCase : int = self.conv_cat(torch.cat([hidden_states, output] , dim=1 ) ) UpperCamelCase : Union[str, Any] = self.classifier(__SCREAMING_SNAKE_CASE ) return output class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : Optional[Any] = UperNetConfig __UpperCamelCase : Optional[int] = "pixel_values" __UpperCamelCase : Dict = True def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): module.backbone.init_weights() module.decode_head.init_weights() module.auxiliary_head.init_weights() def _lowercase ( self ): """simple docstring""" self.backbone.init_weights() self.decode_head.init_weights() self.auxiliary_head.init_weights() def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=False ): """simple docstring""" if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): UpperCamelCase : str = value __UpperCAmelCase : List[Any] = r"\n Parameters:\n This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) sub-class. Use\n it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and\n behavior.\n config ([`UperNetConfig`]): 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" __UpperCAmelCase : Union[str, Any] = r"\n Args:\n pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n Pixel values. Padding will be ignored by default should you provide it. Pixel values can be obtained using\n [`AutoImageProcessor`]. See [`SegformerImageProcessor.__call__`] for details.\n output_attentions (`bool`, *optional*):\n Whether or not to return the attentions tensors of all attention layers in case the backbone has them. See\n `attentions` under returned tensors for more detail.\n output_hidden_states (`bool`, *optional*):\n Whether or not to return the hidden states of all layers of the backbone. See `hidden_states` under\n returned tensors for 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( "UperNet framework leveraging any vision backbone e.g. for ADE20k, CityScapes.", _a, ) class UpperCAmelCase_ ( _a): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" super().__init__(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Dict = AutoBackbone.from_config(config.backbone_config ) # Semantic segmentation head(s) UpperCamelCase : int = UperNetHead(__SCREAMING_SNAKE_CASE , in_channels=self.backbone.channels ) UpperCamelCase : int = UperNetFCNHead(__SCREAMING_SNAKE_CASE ) if config.use_auxiliary_head else None # Initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(UPERNET_INPUTS_DOCSTRING.format('''batch_size, sequence_length''' ) ) @replace_return_docstrings(output_type=__SCREAMING_SNAKE_CASE , config_class=_CONFIG_FOR_DOC ) def _lowercase ( self , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , ): """simple docstring""" UpperCamelCase : List[str] = return_dict if return_dict is not None else self.config.use_return_dict UpperCamelCase : str = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) UpperCamelCase : Tuple = output_attentions if output_attentions is not None else self.config.output_attentions UpperCamelCase : Tuple = self.backbone.forward_with_filtered_kwargs( __SCREAMING_SNAKE_CASE , output_hidden_states=__SCREAMING_SNAKE_CASE , output_attentions=__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = outputs.feature_maps UpperCamelCase : Union[str, Any] = self.decode_head(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = nn.functional.interpolate(__SCREAMING_SNAKE_CASE , size=pixel_values.shape[2:] , mode='''bilinear''' , align_corners=__SCREAMING_SNAKE_CASE ) UpperCamelCase : int = None if self.auxiliary_head is not None: UpperCamelCase : int = self.auxiliary_head(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Tuple = nn.functional.interpolate( __SCREAMING_SNAKE_CASE , size=pixel_values.shape[2:] , mode='''bilinear''' , align_corners=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = None if labels is not None: if self.config.num_labels == 1: raise ValueError('''The number of labels should be greater than one''' ) else: # compute weighted loss UpperCamelCase : Optional[int] = CrossEntropyLoss(ignore_index=self.config.loss_ignore_index ) UpperCamelCase : Tuple = loss_fct(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[Any] = loss_fct(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = main_loss + self.config.auxiliary_loss_weight * auxiliary_loss if not return_dict: if output_hidden_states: UpperCamelCase : Optional[Any] = (logits,) + outputs[1:] else: UpperCamelCase : int = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return SemanticSegmenterOutput( loss=__SCREAMING_SNAKE_CASE , logits=__SCREAMING_SNAKE_CASE , hidden_states=outputs.hidden_states , attentions=outputs.attentions , )
643
1
import os import tempfile import unittest from pathlib import Path from transformers import AutoConfig, is_torch_available from transformers.testing_utils import require_torch, torch_device if is_torch_available(): from transformers import PyTorchBenchmark, PyTorchBenchmarkArguments @require_torch class UpperCAmelCase_ ( unittest.TestCase): '''simple docstring''' def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" for model_result in results.values(): for batch_size, sequence_length in zip(model_result['''bs'''] , model_result['''ss'''] ): UpperCamelCase : Union[str, Any] = model_result['''result'''][batch_size][sequence_length] self.assertIsNotNone(__SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Any = '''sshleifer/tiny-gpt2''' UpperCamelCase : int = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=__SCREAMING_SNAKE_CASE , inference=__SCREAMING_SNAKE_CASE , sequence_lengths=[8] , batch_sizes=[1] , multi_process=__SCREAMING_SNAKE_CASE , ) UpperCamelCase : Optional[int] = PyTorchBenchmark(__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[Any] = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[str] = '''sgugger/tiny-distilbert-classification''' UpperCamelCase : Optional[Any] = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=__SCREAMING_SNAKE_CASE , inference=__SCREAMING_SNAKE_CASE , sequence_lengths=[8] , batch_sizes=[1] , multi_process=__SCREAMING_SNAKE_CASE , only_pretrain_model=__SCREAMING_SNAKE_CASE , ) UpperCamelCase : List[Any] = PyTorchBenchmark(__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[Any] = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[str] = '''sshleifer/tiny-gpt2''' UpperCamelCase : List[Any] = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=__SCREAMING_SNAKE_CASE , inference=__SCREAMING_SNAKE_CASE , torchscript=__SCREAMING_SNAKE_CASE , sequence_lengths=[8] , batch_sizes=[1] , multi_process=__SCREAMING_SNAKE_CASE , ) UpperCamelCase : List[Any] = PyTorchBenchmark(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) @unittest.skipIf(torch_device == '''cpu''' , '''Cant do half precision''' ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Dict = '''sshleifer/tiny-gpt2''' UpperCamelCase : str = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=__SCREAMING_SNAKE_CASE , inference=__SCREAMING_SNAKE_CASE , fpaa=__SCREAMING_SNAKE_CASE , sequence_lengths=[8] , batch_sizes=[1] , multi_process=__SCREAMING_SNAKE_CASE , ) UpperCamelCase : List[str] = PyTorchBenchmark(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : str = '''sshleifer/tiny-gpt2''' UpperCamelCase : Optional[int] = AutoConfig.from_pretrained(__SCREAMING_SNAKE_CASE ) # set architectures equal to `None` UpperCamelCase : Optional[int] = None UpperCamelCase : Optional[int] = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=__SCREAMING_SNAKE_CASE , inference=__SCREAMING_SNAKE_CASE , sequence_lengths=[8] , batch_sizes=[1] , multi_process=__SCREAMING_SNAKE_CASE , ) UpperCamelCase : str = PyTorchBenchmark(__SCREAMING_SNAKE_CASE , configs=[config] ) UpperCamelCase : List[str] = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Optional[Any] = '''sshleifer/tiny-gpt2''' UpperCamelCase : List[str] = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=__SCREAMING_SNAKE_CASE , inference=__SCREAMING_SNAKE_CASE , sequence_lengths=[8] , batch_sizes=[1] , multi_process=__SCREAMING_SNAKE_CASE , ) UpperCamelCase : str = PyTorchBenchmark(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = benchmark.run() self.check_results_dict_not_empty(results.time_train_result ) self.check_results_dict_not_empty(results.memory_train_result ) @unittest.skipIf(torch_device == '''cpu''' , '''Can\'t do half precision''' ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Optional[Any] = '''sshleifer/tiny-gpt2''' UpperCamelCase : List[Any] = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=__SCREAMING_SNAKE_CASE , inference=__SCREAMING_SNAKE_CASE , sequence_lengths=[8] , batch_sizes=[1] , fpaa=__SCREAMING_SNAKE_CASE , multi_process=__SCREAMING_SNAKE_CASE , ) UpperCamelCase : Optional[Any] = PyTorchBenchmark(__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[Any] = benchmark.run() self.check_results_dict_not_empty(results.time_train_result ) self.check_results_dict_not_empty(results.memory_train_result ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Tuple = '''sshleifer/tiny-gpt2''' UpperCamelCase : str = AutoConfig.from_pretrained(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=__SCREAMING_SNAKE_CASE , inference=__SCREAMING_SNAKE_CASE , sequence_lengths=[8] , batch_sizes=[1] , multi_process=__SCREAMING_SNAKE_CASE , ) UpperCamelCase : int = PyTorchBenchmark(__SCREAMING_SNAKE_CASE , configs=[config] ) UpperCamelCase : Tuple = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : str = '''sshleifer/tinier_bart''' UpperCamelCase : List[Any] = AutoConfig.from_pretrained(__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[Any] = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=__SCREAMING_SNAKE_CASE , inference=__SCREAMING_SNAKE_CASE , sequence_lengths=[8] , batch_sizes=[1] , multi_process=__SCREAMING_SNAKE_CASE , ) UpperCamelCase : List[Any] = PyTorchBenchmark(__SCREAMING_SNAKE_CASE , configs=[config] ) UpperCamelCase : Union[str, Any] = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result ) self.check_results_dict_not_empty(results.memory_inference_result ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Tuple = '''sshleifer/tiny-gpt2''' UpperCamelCase : Any = AutoConfig.from_pretrained(__SCREAMING_SNAKE_CASE ) UpperCamelCase : int = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=__SCREAMING_SNAKE_CASE , inference=__SCREAMING_SNAKE_CASE , sequence_lengths=[8] , batch_sizes=[1] , multi_process=__SCREAMING_SNAKE_CASE , ) UpperCamelCase : Tuple = PyTorchBenchmark(__SCREAMING_SNAKE_CASE , configs=[config] ) UpperCamelCase : Any = benchmark.run() self.check_results_dict_not_empty(results.time_train_result ) self.check_results_dict_not_empty(results.memory_train_result ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Any = '''sshleifer/tinier_bart''' UpperCamelCase : Any = AutoConfig.from_pretrained(__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[Any] = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=__SCREAMING_SNAKE_CASE , inference=__SCREAMING_SNAKE_CASE , sequence_lengths=[8] , batch_sizes=[1] , multi_process=__SCREAMING_SNAKE_CASE , ) UpperCamelCase : Optional[Any] = PyTorchBenchmark(__SCREAMING_SNAKE_CASE , configs=[config] ) UpperCamelCase : Any = benchmark.run() self.check_results_dict_not_empty(results.time_train_result ) self.check_results_dict_not_empty(results.memory_train_result ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : str = '''sshleifer/tiny-gpt2''' with tempfile.TemporaryDirectory() as tmp_dir: UpperCamelCase : Union[str, Any] = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=__SCREAMING_SNAKE_CASE , inference=__SCREAMING_SNAKE_CASE , save_to_csv=__SCREAMING_SNAKE_CASE , sequence_lengths=[8] , batch_sizes=[1] , inference_time_csv_file=os.path.join(__SCREAMING_SNAKE_CASE , '''inf_time.csv''' ) , train_memory_csv_file=os.path.join(__SCREAMING_SNAKE_CASE , '''train_mem.csv''' ) , inference_memory_csv_file=os.path.join(__SCREAMING_SNAKE_CASE , '''inf_mem.csv''' ) , train_time_csv_file=os.path.join(__SCREAMING_SNAKE_CASE , '''train_time.csv''' ) , env_info_csv_file=os.path.join(__SCREAMING_SNAKE_CASE , '''env.csv''' ) , multi_process=__SCREAMING_SNAKE_CASE , ) UpperCamelCase : Tuple = PyTorchBenchmark(__SCREAMING_SNAKE_CASE ) benchmark.run() self.assertTrue(Path(os.path.join(__SCREAMING_SNAKE_CASE , '''inf_time.csv''' ) ).exists() ) self.assertTrue(Path(os.path.join(__SCREAMING_SNAKE_CASE , '''train_time.csv''' ) ).exists() ) self.assertTrue(Path(os.path.join(__SCREAMING_SNAKE_CASE , '''inf_mem.csv''' ) ).exists() ) self.assertTrue(Path(os.path.join(__SCREAMING_SNAKE_CASE , '''train_mem.csv''' ) ).exists() ) self.assertTrue(Path(os.path.join(__SCREAMING_SNAKE_CASE , '''env.csv''' ) ).exists() ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[str] = '''sshleifer/tiny-gpt2''' def _check_summary_is_not_empty(__SCREAMING_SNAKE_CASE ): self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''sequential''' ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''cumulative''' ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''current''' ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''total''' ) ) with tempfile.TemporaryDirectory() as tmp_dir: UpperCamelCase : Any = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=__SCREAMING_SNAKE_CASE , inference=__SCREAMING_SNAKE_CASE , sequence_lengths=[8] , batch_sizes=[1] , log_filename=os.path.join(__SCREAMING_SNAKE_CASE , '''log.txt''' ) , log_print=__SCREAMING_SNAKE_CASE , trace_memory_line_by_line=__SCREAMING_SNAKE_CASE , multi_process=__SCREAMING_SNAKE_CASE , ) UpperCamelCase : List[Any] = PyTorchBenchmark(__SCREAMING_SNAKE_CASE ) UpperCamelCase : str = benchmark.run() _check_summary_is_not_empty(result.inference_summary ) _check_summary_is_not_empty(result.train_summary ) self.assertTrue(Path(os.path.join(__SCREAMING_SNAKE_CASE , '''log.txt''' ) ).exists() )
643
import json import os import tempfile import transformers import datasets from utils import generate_example_dataset, get_duration __UpperCAmelCase : Optional[int] = 500000 __UpperCAmelCase , __UpperCAmelCase : Any = os.path.split(__file__) __UpperCAmelCase : int = os.path.join(RESULTS_BASEPATH, "results", RESULTS_FILENAME.replace(".py", ".json")) @get_duration def a ( SCREAMING_SNAKE_CASE_ : datasets.Dataset , **SCREAMING_SNAKE_CASE_ : Union[str, Any] ): """simple docstring""" UpperCamelCase : Tuple = dataset.map(**SCREAMING_SNAKE_CASE_ ) @get_duration def a ( SCREAMING_SNAKE_CASE_ : datasets.Dataset , **SCREAMING_SNAKE_CASE_ : Any ): """simple docstring""" UpperCamelCase : int = dataset.filter(**SCREAMING_SNAKE_CASE_ ) def a ( ): """simple docstring""" UpperCamelCase : Optional[int] = {'''num examples''': SPEED_TEST_N_EXAMPLES} with tempfile.TemporaryDirectory() as tmp_dir: UpperCamelCase : Dict = datasets.Features({'''text''': datasets.Value('''string''' ), '''numbers''': datasets.Value('''float32''' )} ) UpperCamelCase : List[str] = generate_example_dataset( os.path.join(SCREAMING_SNAKE_CASE_ , '''dataset.arrow''' ) , SCREAMING_SNAKE_CASE_ , num_examples=SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Union[str, Any] = transformers.AutoTokenizer.from_pretrained('''bert-base-cased''' , use_fast=SCREAMING_SNAKE_CASE_ ) def tokenize(SCREAMING_SNAKE_CASE_ : Dict ): return tokenizer(examples['''text'''] ) UpperCamelCase : List[Any] = map(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : List[str] = map(SCREAMING_SNAKE_CASE_ , batched=SCREAMING_SNAKE_CASE_ ) UpperCamelCase : List[Any] = map(SCREAMING_SNAKE_CASE_ , function=lambda SCREAMING_SNAKE_CASE_ : None , batched=SCREAMING_SNAKE_CASE_ ) with dataset.formatted_as(type='''numpy''' ): UpperCamelCase : Tuple = map(SCREAMING_SNAKE_CASE_ , function=lambda SCREAMING_SNAKE_CASE_ : None , batched=SCREAMING_SNAKE_CASE_ ) with dataset.formatted_as(type='''pandas''' ): UpperCamelCase : int = map(SCREAMING_SNAKE_CASE_ , function=lambda SCREAMING_SNAKE_CASE_ : None , batched=SCREAMING_SNAKE_CASE_ ) with dataset.formatted_as(type='''torch''' , columns='''numbers''' ): UpperCamelCase : Dict = map(SCREAMING_SNAKE_CASE_ , function=lambda SCREAMING_SNAKE_CASE_ : None , batched=SCREAMING_SNAKE_CASE_ ) with dataset.formatted_as(type='''tensorflow''' , columns='''numbers''' ): UpperCamelCase : Tuple = map(SCREAMING_SNAKE_CASE_ , function=lambda SCREAMING_SNAKE_CASE_ : None , batched=SCREAMING_SNAKE_CASE_ ) UpperCamelCase : List[str] = map(SCREAMING_SNAKE_CASE_ , function=SCREAMING_SNAKE_CASE_ , batched=SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Dict = filter(SCREAMING_SNAKE_CASE_ ) # Activate later when tokenizer support batched inputs # with dataset.formatted_as(type='numpy'): # times[func.__name__ + " fast-tokenizer batched numpy"] = func(dataset, function=tokenize, batched=True) with open(SCREAMING_SNAKE_CASE_ , '''wb''' ) as f: f.write(json.dumps(SCREAMING_SNAKE_CASE_ ).encode('''utf-8''' ) ) if __name__ == "__main__": # useful to run the profiler benchmark_map_filter()
643
1
from __future__ import annotations import typing from collections import Counter def a ( SCREAMING_SNAKE_CASE_ : int ): """simple docstring""" UpperCamelCase : typing.Counter[int] = Counter() for base in range(1 , max_perimeter + 1 ): for perpendicular in range(SCREAMING_SNAKE_CASE_ , max_perimeter + 1 ): UpperCamelCase : int = (base * base + perpendicular * perpendicular) ** 0.5 if hypotenuse == int(SCREAMING_SNAKE_CASE_ ): UpperCamelCase : List[str] = int(base + perpendicular + hypotenuse ) if perimeter > max_perimeter: continue triplets[perimeter] += 1 return triplets def a ( SCREAMING_SNAKE_CASE_ : int = 1_0_0_0 ): """simple docstring""" UpperCamelCase : Any = pythagorean_triple(SCREAMING_SNAKE_CASE_ ) return triplets.most_common(1 )[0][0] if __name__ == "__main__": print(f'''Perimeter {solution()} has maximum solutions''')
643
from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import ShapEPipeline else: from .camera import create_pan_cameras from .pipeline_shap_e import ShapEPipeline from .pipeline_shap_e_img2img import ShapEImgaImgPipeline from .renderer import ( BoundingBoxVolume, ImportanceRaySampler, MLPNeRFModelOutput, MLPNeRSTFModel, ShapEParamsProjModel, ShapERenderer, StratifiedRaySampler, VoidNeRFModel, )
643
1
import torch from diffusers import DDPMScheduler from .test_schedulers import SchedulerCommonTest class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : Dict = (DDPMScheduler,) def _lowercase ( self , **__SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Any = { '''num_train_timesteps''': 1_000, '''beta_start''': 0.0_001, '''beta_end''': 0.02, '''beta_schedule''': '''linear''', '''variance_type''': '''fixed_small''', '''clip_sample''': True, } config.update(**__SCREAMING_SNAKE_CASE ) return config def _lowercase ( self ): """simple docstring""" for timesteps in [1, 5, 100, 1_000]: self.check_over_configs(num_train_timesteps=__SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" for beta_start, beta_end in zip([0.0_001, 0.001, 0.01, 0.1] , [0.002, 0.02, 0.2, 2] ): self.check_over_configs(beta_start=__SCREAMING_SNAKE_CASE , beta_end=__SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" for schedule in ["linear", "squaredcos_cap_v2"]: self.check_over_configs(beta_schedule=__SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" for variance in ["fixed_small", "fixed_large", "other"]: self.check_over_configs(variance_type=__SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" for clip_sample in [True, False]: self.check_over_configs(clip_sample=__SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" self.check_over_configs(thresholding=__SCREAMING_SNAKE_CASE ) for threshold in [0.5, 1.0, 2.0]: for prediction_type in ["epsilon", "sample", "v_prediction"]: self.check_over_configs( thresholding=__SCREAMING_SNAKE_CASE , prediction_type=__SCREAMING_SNAKE_CASE , sample_max_value=__SCREAMING_SNAKE_CASE , ) def _lowercase ( self ): """simple docstring""" for prediction_type in ["epsilon", "sample", "v_prediction"]: self.check_over_configs(prediction_type=__SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" for t in [0, 500, 999]: self.check_over_forward(time_step=__SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Dict = self.scheduler_classes[0] UpperCamelCase : Union[str, Any] = self.get_scheduler_config() UpperCamelCase : Optional[int] = scheduler_class(**__SCREAMING_SNAKE_CASE ) assert torch.sum(torch.abs(scheduler._get_variance(0 ) - 0.0 ) ) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(487 ) - 0.00_979 ) ) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(999 ) - 0.02 ) ) < 1e-5 def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[Any] = self.scheduler_classes[0] UpperCamelCase : int = self.get_scheduler_config() UpperCamelCase : Optional[Any] = scheduler_class(**__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = len(__SCREAMING_SNAKE_CASE ) UpperCamelCase : int = self.dummy_model() UpperCamelCase : str = self.dummy_sample_deter UpperCamelCase : str = torch.manual_seed(0 ) for t in reversed(range(__SCREAMING_SNAKE_CASE ) ): # 1. predict noise residual UpperCamelCase : List[Any] = model(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # 2. predict previous mean of sample x_t-1 UpperCamelCase : List[str] = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , generator=__SCREAMING_SNAKE_CASE ).prev_sample # if t > 0: # noise = self.dummy_sample_deter # variance = scheduler.get_variance(t) ** (0.5) * noise # # sample = pred_prev_sample + variance UpperCamelCase : Optional[int] = pred_prev_sample UpperCamelCase : Tuple = torch.sum(torch.abs(__SCREAMING_SNAKE_CASE ) ) UpperCamelCase : List[str] = torch.mean(torch.abs(__SCREAMING_SNAKE_CASE ) ) assert abs(result_sum.item() - 258.9_606 ) < 1e-2 assert abs(result_mean.item() - 0.3_372 ) < 1e-3 def _lowercase ( self ): """simple docstring""" UpperCamelCase : str = self.scheduler_classes[0] UpperCamelCase : List[str] = self.get_scheduler_config(prediction_type='''v_prediction''' ) UpperCamelCase : Optional[int] = scheduler_class(**__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = len(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[Any] = self.dummy_model() UpperCamelCase : str = self.dummy_sample_deter UpperCamelCase : str = torch.manual_seed(0 ) for t in reversed(range(__SCREAMING_SNAKE_CASE ) ): # 1. predict noise residual UpperCamelCase : List[Any] = model(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # 2. predict previous mean of sample x_t-1 UpperCamelCase : str = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , generator=__SCREAMING_SNAKE_CASE ).prev_sample # if t > 0: # noise = self.dummy_sample_deter # variance = scheduler.get_variance(t) ** (0.5) * noise # # sample = pred_prev_sample + variance UpperCamelCase : Optional[int] = pred_prev_sample UpperCamelCase : Dict = torch.sum(torch.abs(__SCREAMING_SNAKE_CASE ) ) UpperCamelCase : Union[str, Any] = torch.mean(torch.abs(__SCREAMING_SNAKE_CASE ) ) assert abs(result_sum.item() - 202.0_296 ) < 1e-2 assert abs(result_mean.item() - 0.2_631 ) < 1e-3 def _lowercase ( self ): """simple docstring""" UpperCamelCase : Any = self.scheduler_classes[0] UpperCamelCase : Dict = self.get_scheduler_config() UpperCamelCase : Any = scheduler_class(**__SCREAMING_SNAKE_CASE ) UpperCamelCase : Dict = [100, 87, 50, 1, 0] scheduler.set_timesteps(timesteps=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = scheduler.timesteps for i, timestep in enumerate(__SCREAMING_SNAKE_CASE ): if i == len(__SCREAMING_SNAKE_CASE ) - 1: UpperCamelCase : int = -1 else: UpperCamelCase : List[Any] = timesteps[i + 1] UpperCamelCase : int = scheduler.previous_timestep(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Dict = prev_t.item() self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : str = self.scheduler_classes[0] UpperCamelCase : Optional[Any] = self.get_scheduler_config() UpperCamelCase : List[str] = scheduler_class(**__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[Any] = [100, 87, 50, 51, 0] with self.assertRaises(__SCREAMING_SNAKE_CASE , msg='''`custom_timesteps` must be in descending order.''' ): scheduler.set_timesteps(timesteps=__SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : str = self.scheduler_classes[0] UpperCamelCase : Tuple = self.get_scheduler_config() UpperCamelCase : Optional[Any] = scheduler_class(**__SCREAMING_SNAKE_CASE ) UpperCamelCase : Dict = [100, 87, 50, 1, 0] UpperCamelCase : Any = len(__SCREAMING_SNAKE_CASE ) with self.assertRaises(__SCREAMING_SNAKE_CASE , msg='''Can only pass one of `num_inference_steps` or `custom_timesteps`.''' ): scheduler.set_timesteps(num_inference_steps=__SCREAMING_SNAKE_CASE , timesteps=__SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Tuple = self.scheduler_classes[0] UpperCamelCase : Tuple = self.get_scheduler_config() UpperCamelCase : Any = scheduler_class(**__SCREAMING_SNAKE_CASE ) UpperCamelCase : Tuple = [scheduler.config.num_train_timesteps] with self.assertRaises( __SCREAMING_SNAKE_CASE , msg='''`timesteps` must start before `self.config.train_timesteps`: {scheduler.config.num_train_timesteps}}''' , ): scheduler.set_timesteps(timesteps=__SCREAMING_SNAKE_CASE )
643
import torch from transformers import AutoModel class UpperCAmelCase_ ( torch.nn.Module): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE="sayef/fsner-bert-base-uncased" ): """simple docstring""" super(__SCREAMING_SNAKE_CASE , self ).__init__() UpperCamelCase : List[str] = AutoModel.from_pretrained(__SCREAMING_SNAKE_CASE , return_dict=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = torch.nn.CosineSimilarity(3 , 1e-08 ) UpperCamelCase : List[Any] = torch.nn.Softmax(dim=1 ) def _lowercase ( self , **__SCREAMING_SNAKE_CASE ): """simple docstring""" return self.bert(**__SCREAMING_SNAKE_CASE ).last_hidden_state def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" return token_embeddings.sum(2 , keepdim=__SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=1 ): """simple docstring""" return self.softmax(T * self.cos(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Any = W_supports['''sizes'''].tolist() UpperCamelCase : Optional[int] = W_supports['''start_token_id'''].item() UpperCamelCase : Any = W_supports['''end_token_id'''].item() del W_supports["sizes"] del W_supports["start_token_id"] del W_supports["end_token_id"] UpperCamelCase : Union[str, Any] = self.BERT(**__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = self.BERT(**__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[Any] = None UpperCamelCase : Any = None UpperCamelCase : Optional[Any] = W_supports['''input_ids'''] == start_token_id UpperCamelCase : Any = W_supports['''input_ids'''] == end_token_id for i, size in enumerate(__SCREAMING_SNAKE_CASE ): if i == 0: UpperCamelCase : Optional[int] = 0 else: UpperCamelCase : Tuple = support_sizes[i - 1] UpperCamelCase : Tuple = S[s : s + size][start_token_masks[s : s + size]] UpperCamelCase : List[str] = S[s : s + size][end_token_masks[s : s + size]] UpperCamelCase : Dict = torch.matmul(q[i] , s_start.T ).sum(1 ).softmax(0 ) UpperCamelCase : Tuple = torch.matmul(q[i] , s_end.T ).sum(1 ).softmax(0 ) if p_starts is not None: UpperCamelCase : List[str] = torch.vstack((p_starts, p_start) ) UpperCamelCase : Union[str, Any] = torch.vstack((p_ends, p_end) ) else: UpperCamelCase : str = p_start UpperCamelCase : Optional[int] = p_end return p_starts, p_ends
643
1
import re from pathlib import Path from unittest import TestCase import pytest @pytest.mark.integration class UpperCAmelCase_ ( _a): '''simple docstring''' def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" with open(__SCREAMING_SNAKE_CASE , encoding='''utf-8''' ) as input_file: UpperCamelCase : str = re.compile(R'''(?!.*\b(?:encoding|rb|w|wb|w+|wb+|ab|ab+)\b)(?<=\s)(open)\((.*)\)''' ) UpperCamelCase : Optional[int] = input_file.read() UpperCamelCase : Union[str, Any] = regexp.search(__SCREAMING_SNAKE_CASE ) return match def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" with open(__SCREAMING_SNAKE_CASE , encoding='''utf-8''' ) as input_file: UpperCamelCase : Optional[int] = re.compile(R'''#[^\r\n]*print\(|\"[^\r\n]*print\(|\"\"\".*?print\(.*?\"\"\"|(print\()''' , re.DOTALL ) UpperCamelCase : Tuple = input_file.read() # use `re.finditer` to handle the case where the ignored groups would be matched first by `re.search` UpperCamelCase : Dict = regexp.finditer(__SCREAMING_SNAKE_CASE ) UpperCamelCase : int = [match for match in matches if match is not None and match.group(1 ) is not None] return matches[0] if matches else None def _lowercase ( self ): """simple docstring""" UpperCamelCase : int = Path('''./datasets''' ) UpperCamelCase : Tuple = list(dataset_paths.absolute().glob('''**/*.py''' ) ) for dataset in dataset_files: if self._no_encoding_on_file_open(str(__SCREAMING_SNAKE_CASE ) ): raise AssertionError(f"""open(...) must use utf-8 encoding in {dataset}""" ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Optional[int] = Path('''./datasets''' ) UpperCamelCase : Tuple = list(dataset_paths.absolute().glob('''**/*.py''' ) ) for dataset in dataset_files: if self._no_print_statements(str(__SCREAMING_SNAKE_CASE ) ): raise AssertionError(f"""print statement found in {dataset}. Use datasets.logger/logging instead.""" )
643
import json import os import unittest from transformers import DebertaTokenizer, DebertaTokenizerFast from transformers.models.deberta.tokenization_deberta import VOCAB_FILES_NAMES from transformers.testing_utils import slow from ...test_tokenization_common import TokenizerTesterMixin class UpperCAmelCase_ ( _a, unittest.TestCase): '''simple docstring''' __UpperCamelCase : str = DebertaTokenizer __UpperCamelCase : Optional[int] = True __UpperCamelCase : Optional[int] = DebertaTokenizerFast def _lowercase ( self ): """simple docstring""" super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt UpperCamelCase : Optional[int] = [ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''\u0120''', '''\u0120l''', '''\u0120n''', '''\u0120lo''', '''\u0120low''', '''er''', '''\u0120lowest''', '''\u0120newer''', '''\u0120wider''', '''[UNK]''', ] UpperCamelCase : Tuple = dict(zip(__SCREAMING_SNAKE_CASE , range(len(__SCREAMING_SNAKE_CASE ) ) ) ) UpperCamelCase : Any = ['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', ''''''] UpperCamelCase : List[Any] = {'''unk_token''': '''[UNK]'''} UpperCamelCase : Union[str, Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) UpperCamelCase : Optional[int] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''merges_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(__SCREAMING_SNAKE_CASE ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(__SCREAMING_SNAKE_CASE ) ) def _lowercase ( self , **__SCREAMING_SNAKE_CASE ): """simple docstring""" kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname , **__SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : int = '''lower newer''' UpperCamelCase : Union[str, Any] = '''lower newer''' return input_text, output_text def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[str] = self.get_tokenizer() UpperCamelCase : int = '''lower newer''' UpperCamelCase : Union[str, Any] = ['''l''', '''o''', '''w''', '''er''', '''\u0120''', '''n''', '''e''', '''w''', '''er'''] UpperCamelCase : Tuple = tokenizer.tokenize(__SCREAMING_SNAKE_CASE ) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) UpperCamelCase : List[Any] = tokens + [tokenizer.unk_token] UpperCamelCase : Optional[int] = [0, 1, 2, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(tokenizer.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : int = self.get_tokenizer() UpperCamelCase : Optional[Any] = tokenizer('''Hello''' , '''World''' ) UpperCamelCase : List[str] = [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1] self.assertListEqual(tokd['''token_type_ids'''] , __SCREAMING_SNAKE_CASE ) @slow def _lowercase ( self ): """simple docstring""" UpperCamelCase : Union[str, Any] = self.tokenizer_class.from_pretrained('''microsoft/deberta-base''' ) UpperCamelCase : Optional[Any] = tokenizer.encode('''sequence builders''' , add_special_tokens=__SCREAMING_SNAKE_CASE ) UpperCamelCase : int = tokenizer.encode('''multi-sequence build''' , add_special_tokens=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Dict = tokenizer.encode( '''sequence builders''' , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = tokenizer.encode( '''sequence builders''' , '''multi-sequence build''' , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = tokenizer.build_inputs_with_special_tokens(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = tokenizer.build_inputs_with_special_tokens(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) assert encoded_sentence == encoded_text_from_decode assert encoded_pair == encoded_pair_from_decode @slow def _lowercase ( self ): """simple docstring""" UpperCamelCase : Optional[Any] = [self.tokenizer_class] if self.test_rust_tokenizer: tokenizer_classes.append(self.rust_tokenizer_class ) for tokenizer_class in tokenizer_classes: UpperCamelCase : Optional[int] = tokenizer_class.from_pretrained('''microsoft/deberta-base''' ) UpperCamelCase : str = [ '''ALBERT: A Lite BERT for Self-supervised Learning of Language Representations''', '''ALBERT incorporates two parameter reduction techniques''', '''The first one is a factorized embedding parameterization. By decomposing the large vocabulary''' ''' embedding matrix into two small matrices, we separate the size of the hidden layers from the size of''' ''' vocabulary embedding.''', ] UpperCamelCase : Union[str, Any] = tokenizer(__SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = [tokenizer.decode(__SCREAMING_SNAKE_CASE , skip_special_tokens=__SCREAMING_SNAKE_CASE ) for seq in encoding['''input_ids''']] # fmt: off UpperCamelCase : int = { '''input_ids''': [ [1, 2_118, 11_126, 565, 35, 83, 25_191, 163, 18_854, 13, 12_156, 12, 16_101, 25_376, 13_807, 9, 22_205, 27_893, 1_635, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 2_118, 11_126, 565, 24_536, 80, 43_797, 4_878, 7_373, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 133, 78, 65, 16, 10, 3_724, 1_538, 33_183, 11_303, 43_797, 1_938, 4, 870, 24_165, 29_105, 5, 739, 32_644, 33_183, 11_303, 36_173, 88, 80, 650, 7_821, 45_940, 6, 52, 2_559, 5, 1_836, 9, 5, 7_397, 13_171, 31, 5, 1_836, 9, 32_644, 33_183, 11_303, 4, 2] ], '''token_type_ids''': [ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ], '''attention_mask''': [ [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] ] } # fmt: on UpperCamelCase : List[str] = [ '''ALBERT: A Lite BERT for Self-supervised Learning of Language Representations''', '''ALBERT incorporates two parameter reduction techniques''', '''The first one is a factorized embedding parameterization. By decomposing the large vocabulary''' ''' embedding matrix into two small matrices, we separate the size of the hidden layers from the size of''' ''' vocabulary embedding.''', ] self.assertDictEqual(encoding.data , __SCREAMING_SNAKE_CASE ) for expected, decoded in zip(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE )
643
1
import json from typing import List, Optional, Tuple from tokenizers import normalizers from ....tokenization_utils_fast import PreTrainedTokenizerFast from ....utils import logging from .tokenization_retribert import RetriBertTokenizer __UpperCAmelCase : Dict = logging.get_logger(__name__) __UpperCAmelCase : Any = {"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"} __UpperCAmelCase : Tuple = { "vocab_file": { "yjernite/retribert-base-uncased": ( "https://huggingface.co/yjernite/retribert-base-uncased/resolve/main/vocab.txt" ), }, "tokenizer_file": { "yjernite/retribert-base-uncased": ( "https://huggingface.co/yjernite/retribert-base-uncased/resolve/main/tokenizer.json" ), }, } __UpperCAmelCase : Optional[Any] = { "yjernite/retribert-base-uncased": 512, } __UpperCAmelCase : int = { "yjernite/retribert-base-uncased": {"do_lower_case": True}, } class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : Dict = VOCAB_FILES_NAMES __UpperCamelCase : Dict = PRETRAINED_VOCAB_FILES_MAP __UpperCamelCase : Optional[int] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCamelCase : Any = PRETRAINED_INIT_CONFIGURATION __UpperCamelCase : List[Any] = RetriBertTokenizer __UpperCamelCase : Tuple = ["input_ids", "attention_mask"] def __init__( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE="[UNK]" , __SCREAMING_SNAKE_CASE="[SEP]" , __SCREAMING_SNAKE_CASE="[PAD]" , __SCREAMING_SNAKE_CASE="[CLS]" , __SCREAMING_SNAKE_CASE="[MASK]" , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" super().__init__( __SCREAMING_SNAKE_CASE , tokenizer_file=__SCREAMING_SNAKE_CASE , do_lower_case=__SCREAMING_SNAKE_CASE , unk_token=__SCREAMING_SNAKE_CASE , sep_token=__SCREAMING_SNAKE_CASE , pad_token=__SCREAMING_SNAKE_CASE , cls_token=__SCREAMING_SNAKE_CASE , mask_token=__SCREAMING_SNAKE_CASE , tokenize_chinese_chars=__SCREAMING_SNAKE_CASE , strip_accents=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) UpperCamelCase : Optional[Any] = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get('''lowercase''' , __SCREAMING_SNAKE_CASE ) != do_lower_case or normalizer_state.get('''strip_accents''' , __SCREAMING_SNAKE_CASE ) != strip_accents or normalizer_state.get('''handle_chinese_chars''' , __SCREAMING_SNAKE_CASE ) != tokenize_chinese_chars ): UpperCamelCase : int = getattr(__SCREAMING_SNAKE_CASE , normalizer_state.pop('''type''' ) ) UpperCamelCase : Optional[Any] = do_lower_case UpperCamelCase : Union[str, Any] = strip_accents UpperCamelCase : Any = tokenize_chinese_chars UpperCamelCase : int = normalizer_class(**__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[Any] = do_lower_case def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None ): """simple docstring""" UpperCamelCase : Optional[Any] = [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 _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ): """simple docstring""" UpperCamelCase : Dict = [self.sep_token_id] UpperCamelCase : List[str] = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ): """simple docstring""" UpperCamelCase : str = self._tokenizer.model.save(__SCREAMING_SNAKE_CASE , name=__SCREAMING_SNAKE_CASE ) return tuple(__SCREAMING_SNAKE_CASE )
643
import re from pathlib import Path from unittest import TestCase import pytest @pytest.mark.integration class UpperCAmelCase_ ( _a): '''simple docstring''' def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" with open(__SCREAMING_SNAKE_CASE , encoding='''utf-8''' ) as input_file: UpperCamelCase : str = re.compile(R'''(?!.*\b(?:encoding|rb|w|wb|w+|wb+|ab|ab+)\b)(?<=\s)(open)\((.*)\)''' ) UpperCamelCase : Optional[int] = input_file.read() UpperCamelCase : Union[str, Any] = regexp.search(__SCREAMING_SNAKE_CASE ) return match def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" with open(__SCREAMING_SNAKE_CASE , encoding='''utf-8''' ) as input_file: UpperCamelCase : Optional[int] = re.compile(R'''#[^\r\n]*print\(|\"[^\r\n]*print\(|\"\"\".*?print\(.*?\"\"\"|(print\()''' , re.DOTALL ) UpperCamelCase : Tuple = input_file.read() # use `re.finditer` to handle the case where the ignored groups would be matched first by `re.search` UpperCamelCase : Dict = regexp.finditer(__SCREAMING_SNAKE_CASE ) UpperCamelCase : int = [match for match in matches if match is not None and match.group(1 ) is not None] return matches[0] if matches else None def _lowercase ( self ): """simple docstring""" UpperCamelCase : int = Path('''./datasets''' ) UpperCamelCase : Tuple = list(dataset_paths.absolute().glob('''**/*.py''' ) ) for dataset in dataset_files: if self._no_encoding_on_file_open(str(__SCREAMING_SNAKE_CASE ) ): raise AssertionError(f"""open(...) must use utf-8 encoding in {dataset}""" ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Optional[int] = Path('''./datasets''' ) UpperCamelCase : Tuple = list(dataset_paths.absolute().glob('''**/*.py''' ) ) for dataset in dataset_files: if self._no_print_statements(str(__SCREAMING_SNAKE_CASE ) ): raise AssertionError(f"""print statement found in {dataset}. Use datasets.logger/logging instead.""" )
643
1
from __future__ import annotations def a ( SCREAMING_SNAKE_CASE_ : list[int] , SCREAMING_SNAKE_CASE_ : int ): """simple docstring""" UpperCamelCase : Optional[int] = 0 UpperCamelCase : int = len(SCREAMING_SNAKE_CASE_ ) - 1 while i < j: if nums[i] + nums[j] == target: return [i, j] elif nums[i] + nums[j] < target: UpperCamelCase : Tuple = i + 1 else: UpperCamelCase : List[Any] = j - 1 return [] if __name__ == "__main__": import doctest doctest.testmod() print(f'''{two_pointer([2, 7, 11, 15], 9) = }''')
643
from __future__ import annotations import unittest from transformers import XGLMConfig, XGLMTokenizer, 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 tensorflow as tf from transformers.models.xglm.modeling_tf_xglm import ( TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFXGLMForCausalLM, TFXGLMModel, ) @require_tf class UpperCAmelCase_ : '''simple docstring''' __UpperCamelCase : Any = XGLMConfig __UpperCamelCase : Dict = {} __UpperCamelCase : List[str] = "gelu" def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=14 , __SCREAMING_SNAKE_CASE=7 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=99 , __SCREAMING_SNAKE_CASE=32 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=4 , __SCREAMING_SNAKE_CASE=37 , __SCREAMING_SNAKE_CASE="gelu" , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=512 , __SCREAMING_SNAKE_CASE=0.02 , ): """simple docstring""" UpperCamelCase : Any = parent UpperCamelCase : Optional[int] = batch_size UpperCamelCase : str = seq_length UpperCamelCase : List[str] = is_training UpperCamelCase : Tuple = use_input_mask UpperCamelCase : Union[str, Any] = use_labels UpperCamelCase : int = vocab_size UpperCamelCase : Optional[int] = d_model UpperCamelCase : Any = num_hidden_layers UpperCamelCase : List[str] = num_attention_heads UpperCamelCase : Optional[Any] = ffn_dim UpperCamelCase : Optional[int] = activation_function UpperCamelCase : List[str] = activation_dropout UpperCamelCase : Any = attention_dropout UpperCamelCase : str = max_position_embeddings UpperCamelCase : Union[str, Any] = initializer_range UpperCamelCase : int = None UpperCamelCase : Dict = 0 UpperCamelCase : int = 2 UpperCamelCase : Any = 1 def _lowercase ( self ): """simple docstring""" return XGLMConfig.from_pretrained('''facebook/xglm-564M''' ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Optional[int] = tf.clip_by_value( ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) , clip_value_min=0 , clip_value_max=3 ) UpperCamelCase : int = None if self.use_input_mask: UpperCamelCase : Any = random_attention_mask([self.batch_size, self.seq_length] ) UpperCamelCase : Tuple = self.get_config() UpperCamelCase : str = floats_tensor([self.num_hidden_layers, self.num_attention_heads] , 2 ) return ( config, input_ids, input_mask, head_mask, ) def _lowercase ( self ): """simple docstring""" return XGLMConfig( vocab_size=self.vocab_size , d_model=self.hidden_size , num_layers=self.num_hidden_layers , attention_heads=self.num_attention_heads , ffn_dim=self.ffn_dim , activation_function=self.activation_function , activation_dropout=self.activation_dropout , attention_dropout=self.attention_dropout , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , use_cache=__SCREAMING_SNAKE_CASE , bos_token_id=self.bos_token_id , eos_token_id=self.eos_token_id , pad_token_id=self.pad_token_id , return_dict=__SCREAMING_SNAKE_CASE , ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[str] = self.prepare_config_and_inputs() ( ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ) : Dict = config_and_inputs UpperCamelCase : List[str] = { '''input_ids''': input_ids, '''head_mask''': head_mask, } return config, inputs_dict @require_tf class UpperCAmelCase_ ( _a, _a, unittest.TestCase): '''simple docstring''' __UpperCamelCase : Optional[Any] = (TFXGLMModel, TFXGLMForCausalLM) if is_tf_available() else () __UpperCamelCase : Union[str, Any] = (TFXGLMForCausalLM,) if is_tf_available() else () __UpperCamelCase : Any = ( {"feature-extraction": TFXGLMModel, "text-generation": TFXGLMForCausalLM} if is_tf_available() else {} ) __UpperCamelCase : Optional[int] = False __UpperCamelCase : List[Any] = False __UpperCamelCase : List[Any] = False def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[Any] = TFXGLMModelTester(self ) UpperCamelCase : Any = ConfigTester(self , config_class=__SCREAMING_SNAKE_CASE , n_embd=37 ) def _lowercase ( self ): """simple docstring""" self.config_tester.run_common_tests() @slow def _lowercase ( self ): """simple docstring""" for model_name in TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCamelCase : List[Any] = TFXGLMModel.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsNotNone(__SCREAMING_SNAKE_CASE ) @unittest.skip(reason='''Currently, model embeddings are going to undergo a major refactor.''' ) def _lowercase ( self ): """simple docstring""" super().test_resize_token_embeddings() @require_tf class UpperCAmelCase_ ( unittest.TestCase): '''simple docstring''' @slow def _lowercase ( self , __SCREAMING_SNAKE_CASE=True ): """simple docstring""" UpperCamelCase : List[str] = TFXGLMForCausalLM.from_pretrained('''facebook/xglm-564M''' ) UpperCamelCase : List[Any] = tf.convert_to_tensor([[2, 268, 9_865]] , dtype=tf.intaa ) # The dog # </s> The dog is a very friendly dog. He is very affectionate and loves to play with other # fmt: off UpperCamelCase : str = [2, 268, 9_865, 67, 11, 1_988, 57_252, 9_865, 5, 984, 67, 1_988, 213_838, 1_658, 53, 70_446, 33, 6_657, 278, 1_581] # fmt: on UpperCamelCase : Union[str, Any] = model.generate(__SCREAMING_SNAKE_CASE , do_sample=__SCREAMING_SNAKE_CASE , num_beams=1 ) if verify_outputs: self.assertListEqual(output_ids[0].numpy().tolist() , __SCREAMING_SNAKE_CASE ) @slow def _lowercase ( self ): """simple docstring""" UpperCamelCase : str = XGLMTokenizer.from_pretrained('''facebook/xglm-564M''' ) UpperCamelCase : List[str] = TFXGLMForCausalLM.from_pretrained('''facebook/xglm-564M''' ) tf.random.set_seed(0 ) UpperCamelCase : Tuple = tokenizer('''Today is a nice day and''' , return_tensors='''tf''' ) UpperCamelCase : int = tokenized.input_ids # forces the generation to happen on CPU, to avoid GPU-related quirks (and assure same output regardless of the available devices) with tf.device(''':/CPU:0''' ): UpperCamelCase : str = model.generate(__SCREAMING_SNAKE_CASE , do_sample=__SCREAMING_SNAKE_CASE , seed=[7, 0] ) UpperCamelCase : Dict = tokenizer.decode(output_ids[0] , skip_special_tokens=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = ( '''Today is a nice day and warm evening here over Southern Alberta!! Today when they closed schools due''' ) self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) @slow def _lowercase ( self ): """simple docstring""" UpperCamelCase : Dict = TFXGLMForCausalLM.from_pretrained('''facebook/xglm-564M''' ) UpperCamelCase : Tuple = XGLMTokenizer.from_pretrained('''facebook/xglm-564M''' ) UpperCamelCase : Tuple = '''left''' # use different length sentences to test batching UpperCamelCase : Any = [ '''This is an extremelly long sentence that only exists to test the ability of the model to cope with ''' '''left-padding, such as in batched generation. The output for the sequence below should be the same ''' '''regardless of whether left padding is applied or not. When''', '''Hello, my dog is a little''', ] UpperCamelCase : List[Any] = tokenizer(__SCREAMING_SNAKE_CASE , return_tensors='''tf''' , padding=__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = inputs['''input_ids'''] UpperCamelCase : Optional[int] = model.generate(input_ids=__SCREAMING_SNAKE_CASE , attention_mask=inputs['''attention_mask'''] , max_new_tokens=12 ) UpperCamelCase : Optional[int] = tokenizer(sentences[0] , return_tensors='''tf''' ).input_ids UpperCamelCase : Optional[Any] = model.generate(input_ids=__SCREAMING_SNAKE_CASE , max_new_tokens=12 ) UpperCamelCase : str = tokenizer(sentences[1] , return_tensors='''tf''' ).input_ids UpperCamelCase : List[Any] = model.generate(input_ids=__SCREAMING_SNAKE_CASE , max_new_tokens=12 ) UpperCamelCase : Any = tokenizer.batch_decode(__SCREAMING_SNAKE_CASE , skip_special_tokens=__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = tokenizer.decode(output_non_padded[0] , skip_special_tokens=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Tuple = tokenizer.decode(output_padded[0] , skip_special_tokens=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = [ '''This is an extremelly long sentence that only exists to test the ability of the model to cope with ''' '''left-padding, such as in batched generation. The output for the sequence below should be the same ''' '''regardless of whether left padding is applied or not. When left padding is applied, the sequence will be ''' '''a single''', '''Hello, my dog is a little bit of a shy one, but he is very friendly''', ] self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) self.assertListEqual(__SCREAMING_SNAKE_CASE , [non_padded_sentence, padded_sentence] )
643
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available __UpperCAmelCase : List[str] = {"configuration_yolos": ["YOLOS_PRETRAINED_CONFIG_ARCHIVE_MAP", "YolosConfig", "YolosOnnxConfig"]} try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase : List[Any] = ["YolosFeatureExtractor"] __UpperCAmelCase : List[str] = ["YolosImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase : List[Any] = [ "YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST", "YolosForObjectDetection", "YolosModel", "YolosPreTrainedModel", ] if TYPE_CHECKING: from .configuration_yolos import YOLOS_PRETRAINED_CONFIG_ARCHIVE_MAP, YolosConfig, YolosOnnxConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_yolos import YolosFeatureExtractor from .image_processing_yolos import YolosImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_yolos import ( YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST, YolosForObjectDetection, YolosModel, YolosPreTrainedModel, ) else: import sys __UpperCAmelCase : Optional[int] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
643
import os from shutil import copyfile from typing import List, Optional, Tuple from tokenizers import processors from ...tokenization_utils import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_mbart import MBartTokenizer else: __UpperCAmelCase : Optional[int] = None __UpperCAmelCase : int = logging.get_logger(__name__) __UpperCAmelCase : List[Any] = {"vocab_file": "sentencepiece.bpe.model", "tokenizer_file": "tokenizer.json"} __UpperCAmelCase : str = { "vocab_file": { "facebook/mbart-large-en-ro": ( "https://huggingface.co/facebook/mbart-large-en-ro/resolve/main/sentencepiece.bpe.model" ), "facebook/mbart-large-cc25": ( "https://huggingface.co/facebook/mbart-large-cc25/resolve/main/sentencepiece.bpe.model" ), }, "tokenizer_file": { "facebook/mbart-large-en-ro": "https://huggingface.co/facebook/mbart-large-en-ro/resolve/main/tokenizer.json", "facebook/mbart-large-cc25": "https://huggingface.co/facebook/mbart-large-cc25/resolve/main/tokenizer.json", }, } __UpperCAmelCase : Union[str, Any] = { "facebook/mbart-large-en-ro": 1024, "facebook/mbart-large-cc25": 1024, } # fmt: off __UpperCAmelCase : Any = ["ar_AR", "cs_CZ", "de_DE", "en_XX", "es_XX", "et_EE", "fi_FI", "fr_XX", "gu_IN", "hi_IN", "it_IT", "ja_XX", "kk_KZ", "ko_KR", "lt_LT", "lv_LV", "my_MM", "ne_NP", "nl_XX", "ro_RO", "ru_RU", "si_LK", "tr_TR", "vi_VN", "zh_CN"] class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : List[str] = VOCAB_FILES_NAMES __UpperCamelCase : str = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCamelCase : Tuple = PRETRAINED_VOCAB_FILES_MAP __UpperCamelCase : Union[str, Any] = ["input_ids", "attention_mask"] __UpperCamelCase : Any = MBartTokenizer __UpperCamelCase : List[int] = [] __UpperCamelCase : List[int] = [] def __init__( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE="<s>" , __SCREAMING_SNAKE_CASE="</s>" , __SCREAMING_SNAKE_CASE="</s>" , __SCREAMING_SNAKE_CASE="<s>" , __SCREAMING_SNAKE_CASE="<unk>" , __SCREAMING_SNAKE_CASE="<pad>" , __SCREAMING_SNAKE_CASE="<mask>" , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" UpperCamelCase : Union[str, Any] = AddedToken(__SCREAMING_SNAKE_CASE , lstrip=__SCREAMING_SNAKE_CASE , rstrip=__SCREAMING_SNAKE_CASE ) if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else mask_token super().__init__( vocab_file=__SCREAMING_SNAKE_CASE , tokenizer_file=__SCREAMING_SNAKE_CASE , bos_token=__SCREAMING_SNAKE_CASE , eos_token=__SCREAMING_SNAKE_CASE , sep_token=__SCREAMING_SNAKE_CASE , cls_token=__SCREAMING_SNAKE_CASE , unk_token=__SCREAMING_SNAKE_CASE , pad_token=__SCREAMING_SNAKE_CASE , mask_token=__SCREAMING_SNAKE_CASE , src_lang=__SCREAMING_SNAKE_CASE , tgt_lang=__SCREAMING_SNAKE_CASE , additional_special_tokens=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) UpperCamelCase : Dict = vocab_file UpperCamelCase : List[str] = False if not self.vocab_file else True UpperCamelCase : List[Any] = FAIRSEQ_LANGUAGE_CODES.copy() if additional_special_tokens is not None: # Only add those special tokens if they are not already there. _additional_special_tokens.extend( [t for t in additional_special_tokens if t not in _additional_special_tokens] ) self.add_special_tokens({'''additional_special_tokens''': _additional_special_tokens} ) UpperCamelCase : List[Any] = { lang_code: self.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) for lang_code in FAIRSEQ_LANGUAGE_CODES } UpperCamelCase : Dict = src_lang if src_lang is not None else '''en_XX''' UpperCamelCase : List[Any] = self.convert_tokens_to_ids(self._src_lang ) UpperCamelCase : str = tgt_lang self.set_src_lang_special_tokens(self._src_lang ) @property def _lowercase ( self ): """simple docstring""" return self._src_lang @src_lang.setter def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Union[str, Any] = new_src_lang self.set_src_lang_special_tokens(self._src_lang ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ): """simple docstring""" if token_ids_a is None: return self.prefix_tokens + token_ids_a + self.suffix_tokens # We don't expect to process pairs, but leave the pair logic for API consistency return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ): """simple docstring""" UpperCamelCase : str = [self.sep_token_id] UpperCamelCase : Union[str, Any] = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): """simple docstring""" if src_lang is None or tgt_lang is None: raise ValueError('''Translation requires a `src_lang` and a `tgt_lang` for this model''' ) UpperCamelCase : List[str] = src_lang UpperCamelCase : Dict = self(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , return_tensors=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[Any] = self.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Any = tgt_lang_id return inputs def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = "en_XX" , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = "ro_RO" , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" UpperCamelCase : Optional[Any] = src_lang UpperCamelCase : Optional[int] = tgt_lang return super().prepare_seqaseq_batch(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" return self.set_src_lang_special_tokens(self.src_lang ) def _lowercase ( self ): """simple docstring""" return self.set_tgt_lang_special_tokens(self.tgt_lang ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Optional[int] = self.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Any = [] UpperCamelCase : Dict = [self.eos_token_id, self.cur_lang_code] UpperCamelCase : Union[str, Any] = self.convert_ids_to_tokens(self.prefix_tokens ) UpperCamelCase : int = self.convert_ids_to_tokens(self.suffix_tokens ) UpperCamelCase : Tuple = processors.TemplateProcessing( single=prefix_tokens_str + ['''$A'''] + suffix_tokens_str , pair=prefix_tokens_str + ['''$A''', '''$B'''] + suffix_tokens_str , special_tokens=list(zip(prefix_tokens_str + suffix_tokens_str , self.prefix_tokens + self.suffix_tokens ) ) , ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Optional[int] = self.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) UpperCamelCase : int = [] UpperCamelCase : Optional[Any] = [self.eos_token_id, self.cur_lang_code] UpperCamelCase : Optional[Any] = self.convert_ids_to_tokens(self.prefix_tokens ) UpperCamelCase : List[str] = self.convert_ids_to_tokens(self.suffix_tokens ) UpperCamelCase : Optional[int] = processors.TemplateProcessing( single=prefix_tokens_str + ['''$A'''] + suffix_tokens_str , pair=prefix_tokens_str + ['''$A''', '''$B'''] + suffix_tokens_str , special_tokens=list(zip(prefix_tokens_str + suffix_tokens_str , self.prefix_tokens + self.suffix_tokens ) ) , ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ): """simple docstring""" if not self.can_save_slow_tokenizer: raise ValueError( '''Your fast tokenizer does not have the necessary information to save the vocabulary for a slow ''' '''tokenizer.''' ) if not os.path.isdir(__SCREAMING_SNAKE_CASE ): logger.error(f"""Vocabulary path ({save_directory}) should be a directory.""" ) return UpperCamelCase : Optional[int] = os.path.join( __SCREAMING_SNAKE_CASE , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(__SCREAMING_SNAKE_CASE ): copyfile(self.vocab_file , __SCREAMING_SNAKE_CASE ) return (out_vocab_file,)
643
1
import os from distutils.util import strtobool def a ( SCREAMING_SNAKE_CASE_ : List[Any] , SCREAMING_SNAKE_CASE_ : Tuple ): """simple docstring""" for e in env_keys: UpperCamelCase : List[Any] = int(os.environ.get(SCREAMING_SNAKE_CASE_ , -1 ) ) if val >= 0: return val return default def a ( SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Any=False ): """simple docstring""" UpperCamelCase : int = os.environ.get(SCREAMING_SNAKE_CASE_ , str(SCREAMING_SNAKE_CASE_ ) ) return strtobool(SCREAMING_SNAKE_CASE_ ) == 1 # As its name indicates `strtobool` actually returns an int... def a ( SCREAMING_SNAKE_CASE_ : Tuple , SCREAMING_SNAKE_CASE_ : List[str]="no" ): """simple docstring""" UpperCamelCase : List[Any] = os.environ.get(SCREAMING_SNAKE_CASE_ , str(SCREAMING_SNAKE_CASE_ ) ) return value
643
import unittest import numpy as np import torch from diffusers import VersatileDiffusionImageVariationPipeline from diffusers.utils.testing_utils import load_image, require_torch_gpu, slow, torch_device __UpperCAmelCase : Dict = False class UpperCAmelCase_ ( unittest.TestCase): '''simple docstring''' pass @slow @require_torch_gpu class UpperCAmelCase_ ( unittest.TestCase): '''simple docstring''' def _lowercase ( self ): """simple docstring""" UpperCamelCase : Dict = VersatileDiffusionImageVariationPipeline.from_pretrained('''shi-labs/versatile-diffusion''' ) pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg''' ) UpperCamelCase : str = torch.manual_seed(0 ) UpperCamelCase : Union[str, Any] = pipe( image=__SCREAMING_SNAKE_CASE , generator=__SCREAMING_SNAKE_CASE , guidance_scale=7.5 , num_inference_steps=50 , output_type='''numpy''' , ).images UpperCamelCase : List[Any] = image[0, 253:256, 253:256, -1] assert image.shape == (1, 512, 512, 3) UpperCamelCase : Dict = np.array([0.0_441, 0.0_469, 0.0_507, 0.0_575, 0.0_632, 0.0_650, 0.0_865, 0.0_909, 0.0_945] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
643
1
import logging import torch from torch import nn from torch.nn import CrossEntropyLoss, MSELoss from transformers.file_utils import add_start_docstrings, add_start_docstrings_to_model_forward from transformers.models.bert.modeling_bert import ( BERT_INPUTS_DOCSTRING, BERT_START_DOCSTRING, BertEncoder, BertModel, BertPreTrainedModel, ) __UpperCAmelCase : List[str] = logging.getLogger(__name__) class UpperCAmelCase_ ( _a): '''simple docstring''' def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None ): """simple docstring""" UpperCamelCase : int = self.layer[current_layer](__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , head_mask[current_layer] ) UpperCamelCase : List[str] = layer_outputs[0] return hidden_states @add_start_docstrings( "The bare Bert Model transformer with PABEE outputting raw hidden-states without any specific head on top.", _a, ) class UpperCAmelCase_ ( _a): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" super().__init__(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Dict = BertEncoderWithPabee(__SCREAMING_SNAKE_CASE ) self.init_weights() UpperCamelCase : Optional[Any] = 0 UpperCamelCase : Optional[Any] = 0 UpperCamelCase : List[Any] = 0 UpperCamelCase : List[str] = 0 def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : str = threshold def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : str = patience def _lowercase ( self ): """simple docstring""" UpperCamelCase : Dict = 0 UpperCamelCase : Dict = 0 def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[Any] = self.inference_layers_num / self.inference_instances_num UpperCamelCase : Any = ( f"""*** Patience = {self.patience} Avg. Inference Layers = {avg_inf_layers:.2f} Speed Up =""" f""" {1 - avg_inf_layers / self.config.num_hidden_layers:.2f} ***""" ) print(__SCREAMING_SNAKE_CASE ) @add_start_docstrings_to_model_forward(__SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=False , ): """simple docstring""" if input_ids is not None and inputs_embeds is not None: raise ValueError('''You cannot specify both input_ids and inputs_embeds at the same time''' ) elif input_ids is not None: UpperCamelCase : List[str] = input_ids.size() elif inputs_embeds is not None: UpperCamelCase : str = inputs_embeds.size()[:-1] else: raise ValueError('''You have to specify either input_ids or inputs_embeds''' ) UpperCamelCase : Optional[Any] = input_ids.device if input_ids is not None else inputs_embeds.device if attention_mask is None: UpperCamelCase : Union[str, Any] = torch.ones(__SCREAMING_SNAKE_CASE , device=__SCREAMING_SNAKE_CASE ) if token_type_ids is None: UpperCamelCase : Any = torch.zeros(__SCREAMING_SNAKE_CASE , dtype=torch.long , device=__SCREAMING_SNAKE_CASE ) # We can provide a self-attention mask of dimensions [batch_size, from_seq_length, to_seq_length] # ourselves in which case we just need to make it broadcastable to all heads. UpperCamelCase : torch.Tensor = self.get_extended_attention_mask(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # If a 2D ou 3D attention mask is provided for the cross-attention # we need to make broadcastable to [batch_size, num_heads, seq_length, seq_length] if self.config.is_decoder and encoder_hidden_states is not None: UpperCamelCase , UpperCamelCase , UpperCamelCase : Union[str, Any] = encoder_hidden_states.size() UpperCamelCase : List[Any] = (encoder_batch_size, encoder_sequence_length) if encoder_attention_mask is None: UpperCamelCase : Any = torch.ones(__SCREAMING_SNAKE_CASE , device=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Any = self.invert_attention_mask(__SCREAMING_SNAKE_CASE ) else: UpperCamelCase : Any = None # Prepare head mask if needed # 1.0 in head_mask indicate we keep the head # attention_probs has shape bsz x n_heads x N x N # input head_mask has shape [num_heads] or [num_hidden_layers x num_heads] # and head_mask is converted to shape [num_hidden_layers x batch x num_heads x seq_length x seq_length] UpperCamelCase : List[Any] = self.get_head_mask(__SCREAMING_SNAKE_CASE , self.config.num_hidden_layers ) UpperCamelCase : int = self.embeddings( input_ids=__SCREAMING_SNAKE_CASE , position_ids=__SCREAMING_SNAKE_CASE , token_type_ids=__SCREAMING_SNAKE_CASE , inputs_embeds=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = embedding_output if self.training: UpperCamelCase : Any = [] for i in range(self.config.num_hidden_layers ): UpperCamelCase : Dict = self.encoder.adaptive_forward( __SCREAMING_SNAKE_CASE , current_layer=__SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE , head_mask=__SCREAMING_SNAKE_CASE ) UpperCamelCase : int = self.pooler(__SCREAMING_SNAKE_CASE ) UpperCamelCase : int = output_layers[i](output_dropout(__SCREAMING_SNAKE_CASE ) ) res.append(__SCREAMING_SNAKE_CASE ) elif self.patience == 0: # Use all layers for inference UpperCamelCase : int = self.encoder( __SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE , head_mask=__SCREAMING_SNAKE_CASE , encoder_hidden_states=__SCREAMING_SNAKE_CASE , encoder_attention_mask=__SCREAMING_SNAKE_CASE , ) UpperCamelCase : Dict = self.pooler(encoder_outputs[0] ) UpperCamelCase : Optional[Any] = [output_layers[self.config.num_hidden_layers - 1](__SCREAMING_SNAKE_CASE )] else: UpperCamelCase : Optional[Any] = 0 UpperCamelCase : Optional[int] = None UpperCamelCase : int = 0 for i in range(self.config.num_hidden_layers ): calculated_layer_num += 1 UpperCamelCase : Optional[int] = self.encoder.adaptive_forward( __SCREAMING_SNAKE_CASE , current_layer=__SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE , head_mask=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = self.pooler(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = output_layers[i](__SCREAMING_SNAKE_CASE ) if regression: UpperCamelCase : Optional[Any] = logits.detach() if patient_result is not None: UpperCamelCase : Dict = patient_result.detach() if (patient_result is not None) and torch.abs(patient_result - labels ) < self.regression_threshold: patient_counter += 1 else: UpperCamelCase : Optional[int] = 0 else: UpperCamelCase : int = logits.detach().argmax(dim=1 ) if patient_result is not None: UpperCamelCase : List[Any] = patient_result.detach().argmax(dim=1 ) if (patient_result is not None) and torch.all(labels.eq(__SCREAMING_SNAKE_CASE ) ): patient_counter += 1 else: UpperCamelCase : str = 0 UpperCamelCase : int = logits if patient_counter == self.patience: break UpperCamelCase : Tuple = [patient_result] self.inference_layers_num += calculated_layer_num self.inference_instances_num += 1 return res @add_start_docstrings( "Bert Model transformer with PABEE and a sequence classification/regression head on top (a linear layer on top of\n the pooled output) e.g. for GLUE tasks. ", _a, ) class UpperCAmelCase_ ( _a): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" super().__init__(__SCREAMING_SNAKE_CASE ) UpperCamelCase : int = config.num_labels UpperCamelCase : List[Any] = BertModelWithPabee(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = nn.Dropout(config.hidden_dropout_prob ) UpperCamelCase : Optional[int] = nn.ModuleList( [nn.Linear(config.hidden_size , self.config.num_labels ) for _ in range(config.num_hidden_layers )] ) self.init_weights() @add_start_docstrings_to_model_forward(__SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , ): """simple docstring""" UpperCamelCase : Tuple = self.bert( input_ids=__SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE , token_type_ids=__SCREAMING_SNAKE_CASE , position_ids=__SCREAMING_SNAKE_CASE , head_mask=__SCREAMING_SNAKE_CASE , inputs_embeds=__SCREAMING_SNAKE_CASE , output_dropout=self.dropout , output_layers=self.classifiers , regression=self.num_labels == 1 , ) UpperCamelCase : Any = (logits[-1],) if labels is not None: UpperCamelCase : Dict = None UpperCamelCase : int = 0 for ix, logits_item in enumerate(__SCREAMING_SNAKE_CASE ): if self.num_labels == 1: # We are doing regression UpperCamelCase : Any = MSELoss() UpperCamelCase : List[Any] = loss_fct(logits_item.view(-1 ) , labels.view(-1 ) ) else: UpperCamelCase : List[Any] = CrossEntropyLoss() UpperCamelCase : int = loss_fct(logits_item.view(-1 , self.num_labels ) , labels.view(-1 ) ) if total_loss is None: UpperCamelCase : Any = loss else: total_loss += loss * (ix + 1) total_weights += ix + 1 UpperCamelCase : List[str] = (total_loss / total_weights,) + outputs return outputs
643
import copy from typing import Any, Dict, List, Optional, Union import numpy as np 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 __UpperCAmelCase : Dict = logging.get_logger(__name__) class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : Any = ["input_features"] def __init__( self , __SCREAMING_SNAKE_CASE=80 , __SCREAMING_SNAKE_CASE=16_000 , __SCREAMING_SNAKE_CASE=160 , __SCREAMING_SNAKE_CASE=30 , __SCREAMING_SNAKE_CASE=400 , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=False , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" super().__init__( feature_size=__SCREAMING_SNAKE_CASE , sampling_rate=__SCREAMING_SNAKE_CASE , padding_value=__SCREAMING_SNAKE_CASE , return_attention_mask=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) UpperCamelCase : List[str] = n_fft UpperCamelCase : Dict = hop_length UpperCamelCase : Dict = chunk_length UpperCamelCase : List[str] = chunk_length * sampling_rate UpperCamelCase : Dict = self.n_samples // hop_length UpperCamelCase : str = sampling_rate UpperCamelCase : Union[str, Any] = mel_filter_bank( num_frequency_bins=1 + n_fft // 2 , num_mel_filters=__SCREAMING_SNAKE_CASE , min_frequency=0.0 , max_frequency=8_000.0 , sampling_rate=__SCREAMING_SNAKE_CASE , norm='''slaney''' , mel_scale='''slaney''' , ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : List[str] = spectrogram( __SCREAMING_SNAKE_CASE , window_function(self.n_fft , '''hann''' ) , frame_length=self.n_fft , hop_length=self.hop_length , power=2.0 , mel_filters=self.mel_filters , log_mel='''log10''' , ) UpperCamelCase : int = log_spec[:, :-1] UpperCamelCase : int = np.maximum(__SCREAMING_SNAKE_CASE , log_spec.max() - 8.0 ) UpperCamelCase : Any = (log_spec + 4.0) / 4.0 return log_spec @staticmethod # Copied from transformers.models.wav2vec2.feature_extraction_wav2vec2.Wav2Vec2FeatureExtractor.zero_mean_unit_var_norm def _lowercase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = 0.0 ): """simple docstring""" if attention_mask is not None: UpperCamelCase : List[Any] = np.array(__SCREAMING_SNAKE_CASE , np.intaa ) UpperCamelCase : Optional[Any] = [] for vector, length in zip(__SCREAMING_SNAKE_CASE , attention_mask.sum(-1 ) ): UpperCamelCase : Optional[Any] = (vector - vector[:length].mean()) / np.sqrt(vector[:length].var() + 1e-7 ) if length < normed_slice.shape[0]: UpperCamelCase : Optional[int] = padding_value normed_input_values.append(__SCREAMING_SNAKE_CASE ) else: UpperCamelCase : Union[str, Any] = [(x - x.mean()) / np.sqrt(x.var() + 1e-7 ) for x in input_values] return normed_input_values def __call__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = True , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = "max_length" , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( f"""The model corresponding to this feature extractor: {self.__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 : Tuple = isinstance(__SCREAMING_SNAKE_CASE , np.ndarray ) and len(raw_speech.shape ) > 1 if is_batched_numpy and len(raw_speech.shape ) > 2: raise ValueError(f"""Only mono-channel audio is supported for input to {self}""" ) UpperCamelCase : Union[str, Any] = is_batched_numpy or ( isinstance(__SCREAMING_SNAKE_CASE , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: UpperCamelCase : List[Any] = [np.asarray([speech] , dtype=np.floataa ).T for speech in raw_speech] elif not is_batched and not isinstance(__SCREAMING_SNAKE_CASE , np.ndarray ): UpperCamelCase : int = np.asarray(__SCREAMING_SNAKE_CASE , dtype=np.floataa ) elif isinstance(__SCREAMING_SNAKE_CASE , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): UpperCamelCase : Union[str, Any] = raw_speech.astype(np.floataa ) # always return batch if not is_batched: UpperCamelCase : Optional[int] = [np.asarray([raw_speech] ).T] UpperCamelCase : Optional[int] = BatchFeature({'''input_features''': raw_speech} ) # convert into correct format for padding UpperCamelCase : Optional[Any] = self.pad( __SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE , max_length=max_length if max_length else self.n_samples , truncation=__SCREAMING_SNAKE_CASE , pad_to_multiple_of=__SCREAMING_SNAKE_CASE , return_attention_mask=return_attention_mask or do_normalize , ) # zero-mean and unit-variance normalization if do_normalize: UpperCamelCase : Optional[Any] = self.zero_mean_unit_var_norm( padded_inputs['''input_features'''] , attention_mask=padded_inputs['''attention_mask'''] , padding_value=self.padding_value , ) UpperCamelCase : List[str] = np.stack(padded_inputs['''input_features'''] , axis=0 ) # make sure list is in array format UpperCamelCase : Dict = padded_inputs.get('''input_features''' ).transpose(2 , 0 , 1 ) UpperCamelCase : Tuple = [self._np_extract_fbank_features(__SCREAMING_SNAKE_CASE ) for waveform in input_features[0]] if isinstance(input_features[0] , __SCREAMING_SNAKE_CASE ): UpperCamelCase : Optional[int] = [np.asarray(__SCREAMING_SNAKE_CASE , dtype=np.floataa ) for feature in input_features] else: UpperCamelCase : Dict = input_features if return_attention_mask: # rescale from sample (48000) to feature (3000) UpperCamelCase : Union[str, Any] = padded_inputs['''attention_mask'''][:, :: self.hop_length] if return_tensors is not None: UpperCamelCase : Dict = padded_inputs.convert_to_tensors(__SCREAMING_SNAKE_CASE ) return padded_inputs def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[Any] = copy.deepcopy(self.__dict__ ) UpperCamelCase : List[str] = self.__class__.__name__ if "mel_filters" in output: del output["mel_filters"] return output
643
1
def a ( SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int ): """simple docstring""" return x if y == 0 else greatest_common_divisor(SCREAMING_SNAKE_CASE_ , x % y ) def a ( SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int ): """simple docstring""" return (x * y) // greatest_common_divisor(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def a ( SCREAMING_SNAKE_CASE_ : int = 2_0 ): """simple docstring""" UpperCamelCase : Union[str, Any] = 1 for i in range(1 , n + 1 ): UpperCamelCase : int = lcm(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) return g if __name__ == "__main__": print(f'''{solution() = }''')
643
import json from typing import List, Optional, Tuple from tokenizers import normalizers from tokenizers.pre_tokenizers import BertPreTokenizer, PreTokenizer from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_roformer import RoFormerTokenizer from .tokenization_utils import JiebaPreTokenizer __UpperCAmelCase : Dict = logging.get_logger(__name__) __UpperCAmelCase : Optional[Any] = {"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"} __UpperCAmelCase : Dict = { "vocab_file": { "junnyu/roformer_chinese_small": "https://huggingface.co/junnyu/roformer_chinese_small/resolve/main/vocab.txt", "junnyu/roformer_chinese_base": "https://huggingface.co/junnyu/roformer_chinese_base/resolve/main/vocab.txt", "junnyu/roformer_chinese_char_small": ( "https://huggingface.co/junnyu/roformer_chinese_char_small/resolve/main/vocab.txt" ), "junnyu/roformer_chinese_char_base": ( "https://huggingface.co/junnyu/roformer_chinese_char_base/resolve/main/vocab.txt" ), "junnyu/roformer_small_discriminator": ( "https://huggingface.co/junnyu/roformer_small_discriminator/resolve/main/vocab.txt" ), "junnyu/roformer_small_generator": ( "https://huggingface.co/junnyu/roformer_small_generator/resolve/main/vocab.txt" ), } } __UpperCAmelCase : Tuple = { "junnyu/roformer_chinese_small": 1536, "junnyu/roformer_chinese_base": 1536, "junnyu/roformer_chinese_char_small": 512, "junnyu/roformer_chinese_char_base": 512, "junnyu/roformer_small_discriminator": 128, "junnyu/roformer_small_generator": 128, } __UpperCAmelCase : Any = { "junnyu/roformer_chinese_small": {"do_lower_case": True}, "junnyu/roformer_chinese_base": {"do_lower_case": True}, "junnyu/roformer_chinese_char_small": {"do_lower_case": True}, "junnyu/roformer_chinese_char_base": {"do_lower_case": True}, "junnyu/roformer_small_discriminator": {"do_lower_case": True}, "junnyu/roformer_small_generator": {"do_lower_case": True}, } class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : str = VOCAB_FILES_NAMES __UpperCamelCase : Optional[int] = PRETRAINED_VOCAB_FILES_MAP __UpperCamelCase : Union[str, Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCamelCase : int = PRETRAINED_INIT_CONFIGURATION __UpperCamelCase : Any = RoFormerTokenizer def __init__( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE="[UNK]" , __SCREAMING_SNAKE_CASE="[SEP]" , __SCREAMING_SNAKE_CASE="[PAD]" , __SCREAMING_SNAKE_CASE="[CLS]" , __SCREAMING_SNAKE_CASE="[MASK]" , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" super().__init__( __SCREAMING_SNAKE_CASE , tokenizer_file=__SCREAMING_SNAKE_CASE , do_lower_case=__SCREAMING_SNAKE_CASE , unk_token=__SCREAMING_SNAKE_CASE , sep_token=__SCREAMING_SNAKE_CASE , pad_token=__SCREAMING_SNAKE_CASE , cls_token=__SCREAMING_SNAKE_CASE , mask_token=__SCREAMING_SNAKE_CASE , tokenize_chinese_chars=__SCREAMING_SNAKE_CASE , strip_accents=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) UpperCamelCase : List[Any] = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( pre_tok_state.get('''lowercase''' , __SCREAMING_SNAKE_CASE ) != do_lower_case or pre_tok_state.get('''strip_accents''' , __SCREAMING_SNAKE_CASE ) != strip_accents ): UpperCamelCase : List[Any] = getattr(__SCREAMING_SNAKE_CASE , pre_tok_state.pop('''type''' ) ) UpperCamelCase : Optional[int] = do_lower_case UpperCamelCase : Optional[Any] = strip_accents UpperCamelCase : List[Any] = pre_tok_class(**__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[Any] = do_lower_case def __getstate__( self ): """simple docstring""" UpperCamelCase : Optional[int] = self.__dict__.copy() UpperCamelCase : Any = BertPreTokenizer() return state def __setstate__( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Any = d UpperCamelCase : List[str] = self.__dict__['''_tokenizer'''].get_vocab() UpperCamelCase : Any = PreTokenizer.custom(JiebaPreTokenizer(__SCREAMING_SNAKE_CASE ) ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None ): """simple docstring""" UpperCamelCase : Any = [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 _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ): """simple docstring""" UpperCamelCase : Dict = [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 ) * [0] + len(token_ids_a + sep ) * [1] def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ): """simple docstring""" UpperCamelCase : List[Any] = self._tokenizer.model.save(__SCREAMING_SNAKE_CASE , name=__SCREAMING_SNAKE_CASE ) return tuple(__SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=False , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" UpperCamelCase : Any = BertPreTokenizer() return super().save_pretrained(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE )
643
1
import unittest import numpy as np import torch from diffusers import VersatileDiffusionImageVariationPipeline from diffusers.utils.testing_utils import load_image, require_torch_gpu, slow, torch_device __UpperCAmelCase : Dict = False class UpperCAmelCase_ ( unittest.TestCase): '''simple docstring''' pass @slow @require_torch_gpu class UpperCAmelCase_ ( unittest.TestCase): '''simple docstring''' def _lowercase ( self ): """simple docstring""" UpperCamelCase : Dict = VersatileDiffusionImageVariationPipeline.from_pretrained('''shi-labs/versatile-diffusion''' ) pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg''' ) UpperCamelCase : str = torch.manual_seed(0 ) UpperCamelCase : Union[str, Any] = pipe( image=__SCREAMING_SNAKE_CASE , generator=__SCREAMING_SNAKE_CASE , guidance_scale=7.5 , num_inference_steps=50 , output_type='''numpy''' , ).images UpperCamelCase : List[Any] = image[0, 253:256, 253:256, -1] assert image.shape == (1, 512, 512, 3) UpperCamelCase : Dict = np.array([0.0_441, 0.0_469, 0.0_507, 0.0_575, 0.0_632, 0.0_650, 0.0_865, 0.0_909, 0.0_945] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
643
from __future__ import annotations def a ( SCREAMING_SNAKE_CASE_ : list[int] ): """simple docstring""" if len(SCREAMING_SNAKE_CASE_ ) == 0: return array UpperCamelCase , UpperCamelCase : Union[str, Any] = min(SCREAMING_SNAKE_CASE_ ), max(SCREAMING_SNAKE_CASE_ ) # Compute the variables UpperCamelCase : Union[str, Any] = _max - _min + 1 UpperCamelCase , UpperCamelCase : Optional[Any] = [0] * holes_range, [0] * holes_range # Make the sorting. for i in array: UpperCamelCase : Optional[int] = i - _min UpperCamelCase : Any = i holes_repeat[index] += 1 # Makes the array back by replacing the numbers. UpperCamelCase : str = 0 for i in range(SCREAMING_SNAKE_CASE_ ): while holes_repeat[i] > 0: UpperCamelCase : List[Any] = holes[i] index += 1 holes_repeat[i] -= 1 # Returns the sorted array. return array if __name__ == "__main__": import doctest doctest.testmod() __UpperCAmelCase : Any = input("Enter numbers separated by comma:\n") __UpperCAmelCase : int = [int(x) for x in user_input.split(",")] print(pigeon_sort(unsorted))
643
1
from __future__ import annotations import copy import inspect import unittest import numpy as np from transformers import is_tf_available, is_vision_available from transformers.models.auto import get_values from transformers.testing_utils import require_tf, slow from transformers.utils import cached_property 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 tensorflow as tf from transformers import ( TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST, TF_MODEL_FOR_MULTIPLE_CHOICE_MAPPING, TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING, TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, LayoutLMvaConfig, TFLayoutLMvaForQuestionAnswering, TFLayoutLMvaForSequenceClassification, TFLayoutLMvaForTokenClassification, TFLayoutLMvaModel, ) if is_vision_available(): from PIL import Image from transformers import LayoutLMvaImageProcessor class UpperCAmelCase_ : '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=4 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=7 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=99 , __SCREAMING_SNAKE_CASE=36 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=4 , __SCREAMING_SNAKE_CASE=37 , __SCREAMING_SNAKE_CASE="gelu" , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=512 , __SCREAMING_SNAKE_CASE=16 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=0.02 , __SCREAMING_SNAKE_CASE=6 , __SCREAMING_SNAKE_CASE=6 , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=4 , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=1_000 , ): """simple docstring""" UpperCamelCase : Tuple = parent UpperCamelCase : Union[str, Any] = batch_size UpperCamelCase : Union[str, Any] = num_channels UpperCamelCase : int = image_size UpperCamelCase : int = patch_size UpperCamelCase : int = is_training UpperCamelCase : int = use_input_mask UpperCamelCase : int = use_token_type_ids UpperCamelCase : Union[str, Any] = use_labels UpperCamelCase : Tuple = vocab_size UpperCamelCase : Optional[int] = hidden_size UpperCamelCase : int = num_hidden_layers UpperCamelCase : List[Any] = num_attention_heads UpperCamelCase : Tuple = intermediate_size UpperCamelCase : Tuple = hidden_act UpperCamelCase : str = hidden_dropout_prob UpperCamelCase : int = attention_probs_dropout_prob UpperCamelCase : Optional[Any] = max_position_embeddings UpperCamelCase : Optional[int] = type_vocab_size UpperCamelCase : Any = type_sequence_label_size UpperCamelCase : Optional[Any] = initializer_range UpperCamelCase : Dict = coordinate_size UpperCamelCase : Optional[int] = shape_size UpperCamelCase : str = num_labels UpperCamelCase : Any = num_choices UpperCamelCase : Union[str, Any] = scope UpperCamelCase : Optional[Any] = range_bbox # LayoutLMv3's sequence length equals the number of text tokens + number of patches + 1 (we add 1 for the CLS token) UpperCamelCase : Tuple = text_seq_length UpperCamelCase : Any = (image_size // patch_size) ** 2 + 1 UpperCamelCase : Dict = self.text_seq_length + self.image_seq_length def _lowercase ( self ): """simple docstring""" UpperCamelCase : Union[str, Any] = ids_tensor([self.batch_size, self.text_seq_length] , self.vocab_size ) UpperCamelCase : Union[str, Any] = ids_tensor([self.batch_size, self.text_seq_length, 4] , self.range_bbox ) UpperCamelCase : List[str] = bbox.numpy() # Ensure that bbox is legal for i in range(bbox.shape[0] ): for j in range(bbox.shape[1] ): if bbox[i, j, 3] < bbox[i, j, 1]: UpperCamelCase : Dict = bbox[i, j, 3] UpperCamelCase : Union[str, Any] = bbox[i, j, 1] UpperCamelCase : Union[str, Any] = tmp_coordinate if bbox[i, j, 2] < bbox[i, j, 0]: UpperCamelCase : Union[str, Any] = bbox[i, j, 2] UpperCamelCase : str = bbox[i, j, 0] UpperCamelCase : Tuple = tmp_coordinate UpperCamelCase : Optional[Any] = tf.constant(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[Any] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) UpperCamelCase : Optional[Any] = None if self.use_input_mask: UpperCamelCase : List[Any] = random_attention_mask([self.batch_size, self.text_seq_length] ) UpperCamelCase : Dict = None if self.use_token_type_ids: UpperCamelCase : Optional[Any] = ids_tensor([self.batch_size, self.text_seq_length] , self.type_vocab_size ) UpperCamelCase : List[str] = None UpperCamelCase : List[Any] = None if self.use_labels: UpperCamelCase : Tuple = ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCamelCase : str = ids_tensor([self.batch_size, self.text_seq_length] , self.num_labels ) UpperCamelCase : Dict = LayoutLMvaConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , coordinate_size=self.coordinate_size , shape_size=self.shape_size , input_size=self.image_size , patch_size=self.patch_size , ) return config, input_ids, bbox, pixel_values, token_type_ids, input_mask, sequence_labels, token_labels def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Tuple = TFLayoutLMvaModel(config=__SCREAMING_SNAKE_CASE ) # text + image UpperCamelCase : Dict = model(__SCREAMING_SNAKE_CASE , pixel_values=__SCREAMING_SNAKE_CASE , training=__SCREAMING_SNAKE_CASE ) UpperCamelCase : str = model( __SCREAMING_SNAKE_CASE , bbox=__SCREAMING_SNAKE_CASE , pixel_values=__SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE , token_type_ids=__SCREAMING_SNAKE_CASE , training=__SCREAMING_SNAKE_CASE , ) UpperCamelCase : Optional[Any] = model(__SCREAMING_SNAKE_CASE , bbox=__SCREAMING_SNAKE_CASE , pixel_values=__SCREAMING_SNAKE_CASE , training=__SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) # text only UpperCamelCase : int = model(__SCREAMING_SNAKE_CASE , training=__SCREAMING_SNAKE_CASE ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.text_seq_length, self.hidden_size) ) # image only UpperCamelCase : List[Any] = model({'''pixel_values''': pixel_values} , training=__SCREAMING_SNAKE_CASE ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.image_seq_length, self.hidden_size) ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : List[Any] = self.num_labels UpperCamelCase : str = TFLayoutLMvaForSequenceClassification(config=__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = model( __SCREAMING_SNAKE_CASE , bbox=__SCREAMING_SNAKE_CASE , pixel_values=__SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE , token_type_ids=__SCREAMING_SNAKE_CASE , labels=__SCREAMING_SNAKE_CASE , training=__SCREAMING_SNAKE_CASE , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : List[str] = self.num_labels UpperCamelCase : Dict = TFLayoutLMvaForTokenClassification(config=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[Any] = model( __SCREAMING_SNAKE_CASE , bbox=__SCREAMING_SNAKE_CASE , pixel_values=__SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE , token_type_ids=__SCREAMING_SNAKE_CASE , labels=__SCREAMING_SNAKE_CASE , training=__SCREAMING_SNAKE_CASE , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.text_seq_length, self.num_labels) ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : List[str] = 2 UpperCamelCase : Optional[int] = TFLayoutLMvaForQuestionAnswering(config=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Any = model( __SCREAMING_SNAKE_CASE , bbox=__SCREAMING_SNAKE_CASE , pixel_values=__SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE , token_type_ids=__SCREAMING_SNAKE_CASE , start_positions=__SCREAMING_SNAKE_CASE , end_positions=__SCREAMING_SNAKE_CASE , training=__SCREAMING_SNAKE_CASE , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Optional[Any] = self.prepare_config_and_inputs() ((UpperCamelCase) , (UpperCamelCase) , (UpperCamelCase) , (UpperCamelCase) , (UpperCamelCase) , (UpperCamelCase) , (UpperCamelCase) , (UpperCamelCase)) : Optional[Any] = config_and_inputs UpperCamelCase : Union[str, Any] = { '''input_ids''': input_ids, '''bbox''': bbox, '''pixel_values''': pixel_values, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask, } return config, inputs_dict @require_tf class UpperCAmelCase_ ( _a, _a, unittest.TestCase): '''simple docstring''' __UpperCamelCase : Optional[int] = ( ( TFLayoutLMvaModel, TFLayoutLMvaForQuestionAnswering, TFLayoutLMvaForSequenceClassification, TFLayoutLMvaForTokenClassification, ) if is_tf_available() else () ) __UpperCamelCase : Any = ( {"document-question-answering": TFLayoutLMvaForQuestionAnswering, "feature-extraction": TFLayoutLMvaModel} if is_tf_available() else {} ) __UpperCamelCase : Tuple = False __UpperCamelCase : Any = False __UpperCamelCase : Dict = False def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" return True def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=False ): """simple docstring""" UpperCamelCase : List[str] = copy.deepcopy(__SCREAMING_SNAKE_CASE ) if model_class in get_values(__SCREAMING_SNAKE_CASE ): UpperCamelCase : List[str] = { k: tf.tile(tf.expand_dims(__SCREAMING_SNAKE_CASE , 1 ) , (1, self.model_tester.num_choices) + (1,) * (v.ndim - 1) ) if isinstance(__SCREAMING_SNAKE_CASE , tf.Tensor ) and v.ndim > 0 else v for k, v in inputs_dict.items() } if return_labels: if model_class in get_values(__SCREAMING_SNAKE_CASE ): UpperCamelCase : Optional[Any] = tf.ones(self.model_tester.batch_size , dtype=tf.intaa ) elif model_class in get_values(__SCREAMING_SNAKE_CASE ): UpperCamelCase : Tuple = tf.zeros(self.model_tester.batch_size , dtype=tf.intaa ) UpperCamelCase : Dict = tf.zeros(self.model_tester.batch_size , dtype=tf.intaa ) elif model_class in get_values(__SCREAMING_SNAKE_CASE ): UpperCamelCase : Tuple = tf.zeros(self.model_tester.batch_size , dtype=tf.intaa ) elif model_class in get_values(__SCREAMING_SNAKE_CASE ): UpperCamelCase : Optional[int] = tf.zeros( (self.model_tester.batch_size, self.model_tester.text_seq_length) , dtype=tf.intaa ) return inputs_dict def _lowercase ( self ): """simple docstring""" UpperCamelCase : int = TFLayoutLMvaModelTester(self ) UpperCamelCase : int = ConfigTester(self , config_class=__SCREAMING_SNAKE_CASE , hidden_size=37 ) def _lowercase ( self ): """simple docstring""" self.config_tester.run_common_tests() def _lowercase ( self ): """simple docstring""" UpperCamelCase , UpperCamelCase : Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCamelCase : Dict = model_class(__SCREAMING_SNAKE_CASE ) if getattr(__SCREAMING_SNAKE_CASE , '''hf_compute_loss''' , __SCREAMING_SNAKE_CASE ): # The number of elements in the loss should be the same as the number of elements in the label UpperCamelCase : int = self._prepare_for_class(inputs_dict.copy() , __SCREAMING_SNAKE_CASE , return_labels=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = prepared_for_class[ sorted(prepared_for_class.keys() - inputs_dict.keys() , reverse=__SCREAMING_SNAKE_CASE )[0] ] UpperCamelCase : Optional[int] = added_label.shape.as_list()[:1] # Test that model correctly compute the loss with kwargs UpperCamelCase : Optional[int] = self._prepare_for_class(inputs_dict.copy() , __SCREAMING_SNAKE_CASE , return_labels=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[Any] = prepared_for_class.pop('''input_ids''' ) UpperCamelCase : Optional[int] = model(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) # Test that model correctly compute the loss when we mask some positions UpperCamelCase : List[str] = self._prepare_for_class(inputs_dict.copy() , __SCREAMING_SNAKE_CASE , return_labels=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Tuple = prepared_for_class.pop('''input_ids''' ) if "labels" in prepared_for_class: UpperCamelCase : Optional[Any] = prepared_for_class['''labels'''].numpy() if len(labels.shape ) > 1 and labels.shape[1] != 1: UpperCamelCase : Any = -100 UpperCamelCase : Union[str, Any] = tf.convert_to_tensor(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = model(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) self.assertTrue(not np.any(np.isnan(loss.numpy() ) ) ) # Test that model correctly compute the loss with a dict UpperCamelCase : Union[str, Any] = self._prepare_for_class(inputs_dict.copy() , __SCREAMING_SNAKE_CASE , return_labels=__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = model(__SCREAMING_SNAKE_CASE )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) # Test that model correctly compute the loss with a tuple UpperCamelCase : Optional[int] = self._prepare_for_class(inputs_dict.copy() , __SCREAMING_SNAKE_CASE , return_labels=__SCREAMING_SNAKE_CASE ) # Get keys that were added with the _prepare_for_class function UpperCamelCase : Dict = prepared_for_class.keys() - inputs_dict.keys() UpperCamelCase : str = inspect.signature(model.call ).parameters UpperCamelCase : Tuple = list(signature.keys() ) # Create a dictionary holding the location of the tensors in the tuple UpperCamelCase : Optional[int] = {0: '''input_ids'''} for label_key in label_keys: UpperCamelCase : str = signature_names.index(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[Any] = label_key UpperCamelCase : List[Any] = sorted(tuple_index_mapping.items() ) # Initialize a list with their default values, update the values and convert to a tuple UpperCamelCase : str = [] for name in signature_names: if name != "kwargs": list_input.append(signature[name].default ) for index, value in sorted_tuple_index_mapping: UpperCamelCase : Dict = prepared_for_class[value] UpperCamelCase : List[str] = tuple(__SCREAMING_SNAKE_CASE ) # Send to model UpperCamelCase : Optional[int] = model(tuple_input[:-1] )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) def _lowercase ( self ): """simple docstring""" ( ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ) : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" ( ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ) : Dict = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: UpperCamelCase : Any = type self.model_tester.create_and_check_model(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" ( ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ) : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" ( ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ) : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" ( ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ) : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) @slow def _lowercase ( self ): """simple docstring""" for model_name in TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCamelCase : Optional[Any] = TFLayoutLMvaModel.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsNotNone(__SCREAMING_SNAKE_CASE ) def a ( ): """simple docstring""" UpperCamelCase : List[Any] = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_tf class UpperCAmelCase_ ( unittest.TestCase): '''simple docstring''' @cached_property def _lowercase ( self ): """simple docstring""" return LayoutLMvaImageProcessor(apply_ocr=__SCREAMING_SNAKE_CASE ) if is_vision_available() else None @slow def _lowercase ( self ): """simple docstring""" UpperCamelCase : str = TFLayoutLMvaModel.from_pretrained('''microsoft/layoutlmv3-base''' ) UpperCamelCase : str = self.default_image_processor UpperCamelCase : Optional[int] = prepare_img() UpperCamelCase : Any = image_processor(images=__SCREAMING_SNAKE_CASE , return_tensors='''tf''' ).pixel_values UpperCamelCase : int = tf.constant([[1, 2]] ) UpperCamelCase : str = tf.expand_dims(tf.constant([[1, 2, 3, 4], [5, 6, 7, 8]] ) , axis=0 ) # forward pass UpperCamelCase : List[str] = model(input_ids=__SCREAMING_SNAKE_CASE , bbox=__SCREAMING_SNAKE_CASE , pixel_values=__SCREAMING_SNAKE_CASE , training=__SCREAMING_SNAKE_CASE ) # verify the logits UpperCamelCase : str = (1, 199, 768) self.assertEqual(outputs.last_hidden_state.shape , __SCREAMING_SNAKE_CASE ) UpperCamelCase : Dict = tf.constant( [[-0.0_529, 0.3_618, 0.1_632], [-0.1_587, -0.1_667, -0.0_400], [-0.1_557, -0.1_671, -0.0_505]] ) self.assertTrue(np.allclose(outputs.last_hidden_state[0, :3, :3] , __SCREAMING_SNAKE_CASE , atol=1e-4 ) )
643
import json import os import shutil import warnings from argparse import ArgumentParser, Namespace from pathlib import Path from typing import List from ..utils import logging from . import BaseTransformersCLICommand try: from cookiecutter.main import cookiecutter __UpperCAmelCase : List[Any] = True except ImportError: __UpperCAmelCase : List[str] = False __UpperCAmelCase : Any = logging.get_logger(__name__) # pylint: disable=invalid-name def a ( SCREAMING_SNAKE_CASE_ : Namespace ): """simple docstring""" return AddNewModelCommand(args.testing , args.testing_file , path=args.path ) class UpperCAmelCase_ ( _a): '''simple docstring''' @staticmethod def _lowercase ( __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : List[Any] = parser.add_parser('''add-new-model''' ) add_new_model_parser.add_argument('''--testing''' , action='''store_true''' , help='''If in testing mode.''' ) add_new_model_parser.add_argument('''--testing_file''' , type=__SCREAMING_SNAKE_CASE , help='''Configuration file on which to run.''' ) add_new_model_parser.add_argument( '''--path''' , type=__SCREAMING_SNAKE_CASE , help='''Path to cookiecutter. Should only be used for testing purposes.''' ) add_new_model_parser.set_defaults(func=__SCREAMING_SNAKE_CASE ) def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , *__SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Tuple = testing UpperCamelCase : Any = testing_file UpperCamelCase : Dict = path def _lowercase ( self ): """simple docstring""" warnings.warn( '''The command `transformers-cli add-new-model` is deprecated and will be removed in v5 of Transformers. ''' '''It is not actively maintained anymore, so might give a result that won\'t pass all tests and quality ''' '''checks, you should use `transformers-cli add-new-model-like` instead.''' ) if not _has_cookiecutter: raise ImportError( '''Model creation dependencies are required to use the `add_new_model` command. Install them by running ''' '''the following at the root of your `transformers` clone:\n\n\t$ pip install -e .[modelcreation]\n''' ) # Ensure that there is no other `cookiecutter-template-xxx` directory in the current working directory UpperCamelCase : List[str] = [directory for directory in os.listdir() if '''cookiecutter-template-''' == directory[:22]] if len(__SCREAMING_SNAKE_CASE ) > 0: raise ValueError( '''Several directories starting with `cookiecutter-template-` in current working directory. ''' '''Please clean your directory by removing all folders starting with `cookiecutter-template-` or ''' '''change your working directory.''' ) UpperCamelCase : Dict = ( Path(__SCREAMING_SNAKE_CASE ).parent.parent.parent.parent if self._path is None else Path(self._path ).parent.parent ) UpperCamelCase : List[Any] = path_to_transformer_root / '''templates''' / '''adding_a_new_model''' # Execute cookiecutter if not self._testing: cookiecutter(str(__SCREAMING_SNAKE_CASE ) ) else: with open(self._testing_file , '''r''' ) as configuration_file: UpperCamelCase : Tuple = json.load(__SCREAMING_SNAKE_CASE ) cookiecutter( str(path_to_cookiecutter if self._path is None else self._path ) , no_input=__SCREAMING_SNAKE_CASE , extra_context=__SCREAMING_SNAKE_CASE , ) UpperCamelCase : Dict = [directory for directory in os.listdir() if '''cookiecutter-template-''' in directory[:22]][0] # Retrieve configuration with open(directory + '''/configuration.json''' , '''r''' ) as configuration_file: UpperCamelCase : Tuple = json.load(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Dict = configuration['''lowercase_modelname'''] UpperCamelCase : int = configuration['''generate_tensorflow_pytorch_and_flax'''] os.remove(f"""{directory}/configuration.json""" ) UpperCamelCase : str = '''PyTorch''' in generate_tensorflow_pytorch_and_flax UpperCamelCase : Any = '''TensorFlow''' in generate_tensorflow_pytorch_and_flax UpperCamelCase : Union[str, Any] = '''Flax''' in generate_tensorflow_pytorch_and_flax UpperCamelCase : Optional[Any] = f"""{path_to_transformer_root}/src/transformers/models/{lowercase_model_name}""" os.makedirs(__SCREAMING_SNAKE_CASE , exist_ok=__SCREAMING_SNAKE_CASE ) os.makedirs(f"""{path_to_transformer_root}/tests/models/{lowercase_model_name}""" , exist_ok=__SCREAMING_SNAKE_CASE ) # Tests require submodules as they have parent imports with open(f"""{path_to_transformer_root}/tests/models/{lowercase_model_name}/__init__.py""" , '''w''' ): pass shutil.move( f"""{directory}/__init__.py""" , f"""{model_dir}/__init__.py""" , ) shutil.move( f"""{directory}/configuration_{lowercase_model_name}.py""" , f"""{model_dir}/configuration_{lowercase_model_name}.py""" , ) def remove_copy_lines(__SCREAMING_SNAKE_CASE ): with open(__SCREAMING_SNAKE_CASE , '''r''' ) as f: UpperCamelCase : Any = f.readlines() with open(__SCREAMING_SNAKE_CASE , '''w''' ) as f: for line in lines: if "# Copied from transformers." not in line: f.write(__SCREAMING_SNAKE_CASE ) if output_pytorch: if not self._testing: remove_copy_lines(f"""{directory}/modeling_{lowercase_model_name}.py""" ) shutil.move( f"""{directory}/modeling_{lowercase_model_name}.py""" , f"""{model_dir}/modeling_{lowercase_model_name}.py""" , ) shutil.move( f"""{directory}/test_modeling_{lowercase_model_name}.py""" , f"""{path_to_transformer_root}/tests/models/{lowercase_model_name}/test_modeling_{lowercase_model_name}.py""" , ) else: os.remove(f"""{directory}/modeling_{lowercase_model_name}.py""" ) os.remove(f"""{directory}/test_modeling_{lowercase_model_name}.py""" ) if output_tensorflow: if not self._testing: remove_copy_lines(f"""{directory}/modeling_tf_{lowercase_model_name}.py""" ) shutil.move( f"""{directory}/modeling_tf_{lowercase_model_name}.py""" , f"""{model_dir}/modeling_tf_{lowercase_model_name}.py""" , ) shutil.move( f"""{directory}/test_modeling_tf_{lowercase_model_name}.py""" , f"""{path_to_transformer_root}/tests/models/{lowercase_model_name}/test_modeling_tf_{lowercase_model_name}.py""" , ) else: os.remove(f"""{directory}/modeling_tf_{lowercase_model_name}.py""" ) os.remove(f"""{directory}/test_modeling_tf_{lowercase_model_name}.py""" ) if output_flax: if not self._testing: remove_copy_lines(f"""{directory}/modeling_flax_{lowercase_model_name}.py""" ) shutil.move( f"""{directory}/modeling_flax_{lowercase_model_name}.py""" , f"""{model_dir}/modeling_flax_{lowercase_model_name}.py""" , ) shutil.move( f"""{directory}/test_modeling_flax_{lowercase_model_name}.py""" , f"""{path_to_transformer_root}/tests/models/{lowercase_model_name}/test_modeling_flax_{lowercase_model_name}.py""" , ) else: os.remove(f"""{directory}/modeling_flax_{lowercase_model_name}.py""" ) os.remove(f"""{directory}/test_modeling_flax_{lowercase_model_name}.py""" ) shutil.move( f"""{directory}/{lowercase_model_name}.md""" , f"""{path_to_transformer_root}/docs/source/en/model_doc/{lowercase_model_name}.md""" , ) shutil.move( f"""{directory}/tokenization_{lowercase_model_name}.py""" , f"""{model_dir}/tokenization_{lowercase_model_name}.py""" , ) shutil.move( f"""{directory}/tokenization_fast_{lowercase_model_name}.py""" , f"""{model_dir}/tokenization_{lowercase_model_name}_fast.py""" , ) from os import fdopen, remove from shutil import copymode, move from tempfile import mkstemp def replace(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): # Create temp file UpperCamelCase , UpperCamelCase : Optional[Any] = mkstemp() UpperCamelCase : Tuple = False with fdopen(__SCREAMING_SNAKE_CASE , '''w''' ) as new_file: with open(__SCREAMING_SNAKE_CASE ) as old_file: for line in old_file: new_file.write(__SCREAMING_SNAKE_CASE ) if line_to_copy_below in line: UpperCamelCase : Optional[int] = True for line_to_copy in lines_to_copy: new_file.write(__SCREAMING_SNAKE_CASE ) if not line_found: raise ValueError(f"""Line {line_to_copy_below} was not found in file.""" ) # Copy the file permissions from the old file to the new file copymode(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # Remove original file remove(__SCREAMING_SNAKE_CASE ) # Move new file move(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def skip_units(__SCREAMING_SNAKE_CASE ): return ( ("generating PyTorch" in line and not output_pytorch) or ("generating TensorFlow" in line and not output_tensorflow) or ("generating Flax" in line and not output_flax) ) def replace_in_files(__SCREAMING_SNAKE_CASE ): with open(__SCREAMING_SNAKE_CASE ) as datafile: UpperCamelCase : int = [] UpperCamelCase : Dict = False UpperCamelCase : List[Any] = False for line in datafile: if "# To replace in: " in line and "##" not in line: UpperCamelCase : Dict = line.split('''"''' )[1] UpperCamelCase : int = skip_units(__SCREAMING_SNAKE_CASE ) elif "# Below: " in line and "##" not in line: UpperCamelCase : Dict = line.split('''"''' )[1] UpperCamelCase : List[str] = skip_units(__SCREAMING_SNAKE_CASE ) elif "# End." in line and "##" not in line: if not skip_file and not skip_snippet: replace(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) UpperCamelCase : Dict = [] elif "# Replace with" in line and "##" not in line: UpperCamelCase : Tuple = [] elif "##" not in line: lines_to_copy.append(__SCREAMING_SNAKE_CASE ) remove(__SCREAMING_SNAKE_CASE ) replace_in_files(f"""{directory}/to_replace_{lowercase_model_name}.py""" ) os.rmdir(__SCREAMING_SNAKE_CASE )
643
1
import torch from diffusers import DiffusionPipeline class UpperCAmelCase_ ( _a): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" super().__init__() self.register_modules(unet=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) def __call__( self ): """simple docstring""" UpperCamelCase : Tuple = torch.randn( (1, self.unet.config.in_channels, self.unet.config.sample_size, self.unet.config.sample_size) , ) UpperCamelCase : Union[str, Any] = 1 UpperCamelCase : Tuple = self.unet(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ).sample UpperCamelCase : List[Any] = self.scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ).prev_sample UpperCamelCase : Optional[int] = scheduler_output - scheduler_output + torch.ones_like(__SCREAMING_SNAKE_CASE ) return result
643
from pathlib import Path import cva import numpy as np from matplotlib import pyplot as plt def a ( SCREAMING_SNAKE_CASE_ : np.ndarray , SCREAMING_SNAKE_CASE_ : np.ndarray , SCREAMING_SNAKE_CASE_ : np.ndarray , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int ): """simple docstring""" UpperCamelCase : str = cva.getAffineTransform(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) return cva.warpAffine(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , (rows, cols) ) if __name__ == "__main__": # read original image __UpperCAmelCase : Tuple = cva.imread( str(Path(__file__).resolve().parent.parent / "image_data" / "lena.jpg") ) # turn image in gray scale value __UpperCAmelCase : int = cva.cvtColor(image, cva.COLOR_BGR2GRAY) # get image shape __UpperCAmelCase , __UpperCAmelCase : Tuple = gray_img.shape # set different points to rotate image __UpperCAmelCase : Optional[int] = np.array([[50, 50], [200, 50], [50, 200]], np.floataa) __UpperCAmelCase : Optional[int] = np.array([[10, 100], [200, 50], [100, 250]], np.floataa) __UpperCAmelCase : Any = np.array([[50, 50], [150, 50], [120, 200]], np.floataa) __UpperCAmelCase : int = np.array([[10, 100], [80, 50], [180, 250]], np.floataa) # add all rotated images in a list __UpperCAmelCase : Union[str, Any] = [ gray_img, get_rotation(gray_img, ptsa, ptsa, img_rows, img_cols), get_rotation(gray_img, ptsa, ptsa, img_rows, img_cols), get_rotation(gray_img, ptsa, ptsa, img_rows, img_cols), ] # plot different image rotations __UpperCAmelCase : List[str] = plt.figure(1) __UpperCAmelCase : Dict = ["Original", "Rotation 1", "Rotation 2", "Rotation 3"] for i, image in enumerate(images): plt.subplot(2, 2, i + 1), plt.imshow(image, "gray") plt.title(titles[i]) plt.axis("off") plt.subplots_adjust(left=0.0, bottom=0.05, right=1.0, top=0.95) plt.show()
643
1
# This is the module that test_patching.py uses to test patch_submodule() import os # noqa: this is just for tests import os as renamed_os # noqa: this is just for tests from os import path # noqa: this is just for tests from os import path as renamed_path # noqa: this is just for tests from os.path import join # noqa: this is just for tests from os.path import join as renamed_join # noqa: this is just for tests __UpperCAmelCase : Any = open # noqa: we just need to have a builtin inside this module to test it properly
643
import copy from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ..auto import CONFIG_MAPPING __UpperCAmelCase : Optional[Any] = logging.get_logger(__name__) __UpperCAmelCase : List[str] = { "microsoft/conditional-detr-resnet-50": ( "https://huggingface.co/microsoft/conditional-detr-resnet-50/resolve/main/config.json" ), } class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : Optional[int] = "conditional_detr" __UpperCamelCase : Optional[Any] = ["past_key_values"] __UpperCamelCase : Union[str, Any] = { "hidden_size": "d_model", "num_attention_heads": "encoder_attention_heads", } def __init__( self , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=300 , __SCREAMING_SNAKE_CASE=6 , __SCREAMING_SNAKE_CASE=2_048 , __SCREAMING_SNAKE_CASE=8 , __SCREAMING_SNAKE_CASE=6 , __SCREAMING_SNAKE_CASE=2_048 , __SCREAMING_SNAKE_CASE=8 , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE="relu" , __SCREAMING_SNAKE_CASE=256 , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=0.02 , __SCREAMING_SNAKE_CASE=1.0 , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE="sine" , __SCREAMING_SNAKE_CASE="resnet50" , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=5 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=1 , __SCREAMING_SNAKE_CASE=1 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=5 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=0.25 , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" if backbone_config is not None and use_timm_backbone: raise ValueError('''You can\'t specify both `backbone_config` and `use_timm_backbone`.''' ) if not use_timm_backbone: if backbone_config is None: logger.info('''`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.''' ) UpperCamelCase : str = CONFIG_MAPPING['''resnet'''](out_features=['''stage4'''] ) elif isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): UpperCamelCase : Tuple = backbone_config.get('''model_type''' ) UpperCamelCase : Optional[Any] = CONFIG_MAPPING[backbone_model_type] UpperCamelCase : Any = config_class.from_dict(__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = use_timm_backbone UpperCamelCase : int = backbone_config UpperCamelCase : Any = num_channels UpperCamelCase : Optional[Any] = num_queries UpperCamelCase : Tuple = d_model UpperCamelCase : Optional[Any] = encoder_ffn_dim UpperCamelCase : Optional[int] = encoder_layers UpperCamelCase : Union[str, Any] = encoder_attention_heads UpperCamelCase : Optional[Any] = decoder_ffn_dim UpperCamelCase : Optional[int] = decoder_layers UpperCamelCase : Optional[Any] = decoder_attention_heads UpperCamelCase : Any = dropout UpperCamelCase : List[Any] = attention_dropout UpperCamelCase : List[Any] = activation_dropout UpperCamelCase : List[str] = activation_function UpperCamelCase : Optional[int] = init_std UpperCamelCase : Optional[Any] = init_xavier_std UpperCamelCase : Union[str, Any] = encoder_layerdrop UpperCamelCase : Optional[Any] = decoder_layerdrop UpperCamelCase : Tuple = encoder_layers UpperCamelCase : Optional[Any] = auxiliary_loss UpperCamelCase : Union[str, Any] = position_embedding_type UpperCamelCase : Optional[int] = backbone UpperCamelCase : Dict = use_pretrained_backbone UpperCamelCase : Tuple = dilation # Hungarian matcher UpperCamelCase : Union[str, Any] = class_cost UpperCamelCase : List[Any] = bbox_cost UpperCamelCase : Optional[Any] = giou_cost # Loss coefficients UpperCamelCase : Optional[Any] = mask_loss_coefficient UpperCamelCase : Optional[int] = dice_loss_coefficient UpperCamelCase : Optional[Any] = cls_loss_coefficient UpperCamelCase : Optional[int] = bbox_loss_coefficient UpperCamelCase : Optional[int] = giou_loss_coefficient UpperCamelCase : Optional[int] = focal_alpha super().__init__(is_encoder_decoder=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) @property def _lowercase ( self ): """simple docstring""" return self.encoder_attention_heads @property def _lowercase ( self ): """simple docstring""" return self.d_model def _lowercase ( self ): """simple docstring""" UpperCamelCase : Union[str, Any] = copy.deepcopy(self.__dict__ ) if self.backbone_config is not None: UpperCamelCase : List[Any] = self.backbone_config.to_dict() UpperCamelCase : List[Any] = self.__class__.model_type return output class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : Dict = version.parse("1.11") @property def _lowercase ( self ): """simple docstring""" return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ('''pixel_mask''', {0: '''batch'''}), ] ) @property def _lowercase ( self ): """simple docstring""" return 1e-5 @property def _lowercase ( self ): """simple docstring""" return 12
643
1
from __future__ import annotations import inspect import unittest from transformers import ViTConfig from transformers.testing_utils import require_tf, require_vision, slow from transformers.utils import cached_property, is_tf_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFViTForImageClassification, TFViTModel if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class UpperCAmelCase_ : '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=13 , __SCREAMING_SNAKE_CASE=30 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=32 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=4 , __SCREAMING_SNAKE_CASE=37 , __SCREAMING_SNAKE_CASE="gelu" , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=10 , __SCREAMING_SNAKE_CASE=0.02 , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=None , ): """simple docstring""" UpperCamelCase : Any = parent UpperCamelCase : Optional[int] = batch_size UpperCamelCase : List[Any] = image_size UpperCamelCase : int = patch_size UpperCamelCase : Optional[int] = num_channels UpperCamelCase : List[Any] = is_training UpperCamelCase : int = use_labels UpperCamelCase : Union[str, Any] = hidden_size UpperCamelCase : Any = num_hidden_layers UpperCamelCase : str = num_attention_heads UpperCamelCase : Optional[Any] = intermediate_size UpperCamelCase : Union[str, Any] = hidden_act UpperCamelCase : Dict = hidden_dropout_prob UpperCamelCase : List[str] = attention_probs_dropout_prob UpperCamelCase : Union[str, Any] = type_sequence_label_size UpperCamelCase : Union[str, Any] = initializer_range UpperCamelCase : List[Any] = scope # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) UpperCamelCase : int = (image_size // patch_size) ** 2 UpperCamelCase : List[Any] = num_patches + 1 def _lowercase ( self ): """simple docstring""" UpperCamelCase : str = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) UpperCamelCase : Optional[Any] = None if self.use_labels: UpperCamelCase : Union[str, Any] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCamelCase : Union[str, Any] = self.get_config() return config, pixel_values, labels def _lowercase ( self ): """simple docstring""" return ViTConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=__SCREAMING_SNAKE_CASE , initializer_range=self.initializer_range , ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Dict = TFViTModel(config=__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = model(__SCREAMING_SNAKE_CASE , training=__SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) # Test with an image with different size than the one specified in config. UpperCamelCase : Any = self.image_size // 2 UpperCamelCase : Any = pixel_values[:, :, :image_size, :image_size] UpperCamelCase : Union[str, Any] = model(__SCREAMING_SNAKE_CASE , interpolate_pos_encoding=__SCREAMING_SNAKE_CASE , training=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Any = (image_size // self.patch_size) ** 2 + 1 self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, seq_length, self.hidden_size) ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : int = self.type_sequence_label_size UpperCamelCase : List[str] = TFViTForImageClassification(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Tuple = model(__SCREAMING_SNAKE_CASE , labels=__SCREAMING_SNAKE_CASE , training=__SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # Test with an image with different size than the one specified in config. UpperCamelCase : Any = self.image_size // 2 UpperCamelCase : Tuple = pixel_values[:, :, :image_size, :image_size] UpperCamelCase : Optional[Any] = model(__SCREAMING_SNAKE_CASE , interpolate_pos_encoding=__SCREAMING_SNAKE_CASE , training=__SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images UpperCamelCase : Optional[int] = 1 UpperCamelCase : List[str] = TFViTForImageClassification(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) UpperCamelCase : Union[str, Any] = model(__SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[Any] = self.prepare_config_and_inputs() UpperCamelCase , UpperCamelCase , UpperCamelCase : List[str] = config_and_inputs UpperCamelCase : Union[str, Any] = {'''pixel_values''': pixel_values} return config, inputs_dict @require_tf class UpperCAmelCase_ ( _a, _a, unittest.TestCase): '''simple docstring''' __UpperCamelCase : Optional[int] = (TFViTModel, TFViTForImageClassification) if is_tf_available() else () __UpperCamelCase : Optional[int] = ( {"feature-extraction": TFViTModel, "image-classification": TFViTForImageClassification} if is_tf_available() else {} ) __UpperCamelCase : int = False __UpperCamelCase : Dict = False __UpperCamelCase : Tuple = False def _lowercase ( self ): """simple docstring""" UpperCamelCase : Optional[Any] = TFViTModelTester(self ) UpperCamelCase : Any = ConfigTester(self , config_class=__SCREAMING_SNAKE_CASE , has_text_modality=__SCREAMING_SNAKE_CASE , hidden_size=37 ) def _lowercase ( self ): """simple docstring""" self.config_tester.run_common_tests() @unittest.skip(reason='''ViT does not use inputs_embeds''' ) def _lowercase ( self ): """simple docstring""" pass @unittest.skip(reason='''ViT does not use inputs_embeds''' ) def _lowercase ( self ): """simple docstring""" pass def _lowercase ( self ): """simple docstring""" UpperCamelCase , UpperCamelCase : Any = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCamelCase : Optional[Any] = model_class(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(model.get_input_embeddings() , (tf.keras.layers.Layer) ) UpperCamelCase : List[Any] = model.get_output_embeddings() self.assertTrue(x is None or isinstance(__SCREAMING_SNAKE_CASE , tf.keras.layers.Layer ) ) def _lowercase ( self ): """simple docstring""" UpperCamelCase , UpperCamelCase : Tuple = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCamelCase : Optional[Any] = model_class(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Dict = inspect.signature(model.call ) # signature.parameters is an OrderedDict => so arg_names order is deterministic UpperCamelCase : Any = [*signature.parameters.keys()] UpperCamelCase : Optional[int] = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , __SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*__SCREAMING_SNAKE_CASE ) @slow def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[Any] = TFViTModel.from_pretrained('''google/vit-base-patch16-224''' ) self.assertIsNotNone(__SCREAMING_SNAKE_CASE ) def a ( ): """simple docstring""" UpperCamelCase : Union[str, Any] = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_tf @require_vision class UpperCAmelCase_ ( unittest.TestCase): '''simple docstring''' @cached_property def _lowercase ( self ): """simple docstring""" return ViTImageProcessor.from_pretrained('''google/vit-base-patch16-224''' ) if is_vision_available() else None @slow def _lowercase ( self ): """simple docstring""" UpperCamelCase : Tuple = TFViTForImageClassification.from_pretrained('''google/vit-base-patch16-224''' ) UpperCamelCase : Any = self.default_image_processor UpperCamelCase : str = prepare_img() UpperCamelCase : Any = image_processor(images=__SCREAMING_SNAKE_CASE , return_tensors='''tf''' ) # forward pass UpperCamelCase : List[Any] = model(**__SCREAMING_SNAKE_CASE ) # verify the logits UpperCamelCase : Tuple = tf.TensorShape((1, 1_000) ) self.assertEqual(outputs.logits.shape , __SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[Any] = tf.constant([-0.2_744, 0.8_215, -0.0_836] ) tf.debugging.assert_near(outputs.logits[0, :3] , __SCREAMING_SNAKE_CASE , atol=1e-4 )
643
import requests from bsa import BeautifulSoup def a ( SCREAMING_SNAKE_CASE_ : str = "AAPL" ): """simple docstring""" UpperCamelCase : Dict = F"""https://in.finance.yahoo.com/quote/{symbol}?s={symbol}""" UpperCamelCase : Any = BeautifulSoup(requests.get(SCREAMING_SNAKE_CASE_ ).text , '''html.parser''' ) UpperCamelCase : Dict = '''My(6px) Pos(r) smartphone_Mt(6px)''' return soup.find('''div''' , class_=class_ ).find('''span''' ).text if __name__ == "__main__": for symbol in "AAPL AMZN IBM GOOG MSFT ORCL".split(): print(f'''Current {symbol:<4} stock price is {stock_price(symbol):>8}''')
643
1
from multiprocessing import Lock, Pipe, Process # lock used to ensure that two processes do not access a pipe at the same time __UpperCAmelCase : Union[str, Any] = Lock() def a ( SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : Optional[Any] , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : List[Any] , SCREAMING_SNAKE_CASE_ : List[Any] , SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : Optional[int] ): """simple docstring""" global process_lock # we perform n swaps since after n swaps we know we are sorted # we *could* stop early if we are sorted already, but it takes as long to # find out we are sorted as it does to sort the list with this algorithm for i in range(0 , 1_0 ): if (i + position) % 2 == 0 and r_send is not None: # send your value to your right neighbor process_lock.acquire() r_send[1].send(SCREAMING_SNAKE_CASE_ ) process_lock.release() # receive your right neighbor's value process_lock.acquire() UpperCamelCase : List[str] = rr_cv[0].recv() process_lock.release() # take the lower value since you are on the left UpperCamelCase : Any = min(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) elif (i + position) % 2 != 0 and l_send is not None: # send your value to your left neighbor process_lock.acquire() l_send[1].send(SCREAMING_SNAKE_CASE_ ) process_lock.release() # receive your left neighbor's value process_lock.acquire() UpperCamelCase : Union[str, Any] = lr_cv[0].recv() process_lock.release() # take the higher value since you are on the right UpperCamelCase : str = max(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # after all swaps are performed, send the values back to main result_pipe[1].send(SCREAMING_SNAKE_CASE_ ) def a ( SCREAMING_SNAKE_CASE_ : Any ): """simple docstring""" UpperCamelCase : Tuple = [] UpperCamelCase : int = [] # initialize the list of pipes where the values will be retrieved for _ in arr: result_pipe.append(Pipe() ) # creates the processes # the first and last process only have one neighbor so they are made outside # of the loop UpperCamelCase : str = Pipe() UpperCamelCase : Optional[Any] = Pipe() process_array_.append( Process( target=SCREAMING_SNAKE_CASE_ , args=(0, arr[0], None, temp_rs, None, temp_rr, result_pipe[0]) , ) ) UpperCamelCase : int = temp_rs UpperCamelCase : str = temp_rr for i in range(1 , len(SCREAMING_SNAKE_CASE_ ) - 1 ): UpperCamelCase : Any = Pipe() UpperCamelCase : List[Any] = Pipe() process_array_.append( Process( target=SCREAMING_SNAKE_CASE_ , args=(i, arr[i], temp_ls, temp_rs, temp_lr, temp_rr, result_pipe[i]) , ) ) UpperCamelCase : List[str] = temp_rs UpperCamelCase : Any = temp_rr process_array_.append( Process( target=SCREAMING_SNAKE_CASE_ , args=( len(SCREAMING_SNAKE_CASE_ ) - 1, arr[len(SCREAMING_SNAKE_CASE_ ) - 1], temp_ls, None, temp_lr, None, result_pipe[len(SCREAMING_SNAKE_CASE_ ) - 1], ) , ) ) # start the processes for p in process_array_: p.start() # wait for the processes to end and write their values to the list for p in range(0 , len(SCREAMING_SNAKE_CASE_ ) ): UpperCamelCase : int = result_pipe[p][0].recv() process_array_[p].join() return arr def a ( ): """simple docstring""" UpperCamelCase : int = list(range(1_0 , 0 , -1 ) ) print('''Initial List''' ) print(*SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Dict = odd_even_transposition(SCREAMING_SNAKE_CASE_ ) print('''Sorted List\n''' ) print(*SCREAMING_SNAKE_CASE_ ) if __name__ == "__main__": main()
643
def a ( SCREAMING_SNAKE_CASE_ : int ): """simple docstring""" if number > 0: raise ValueError('''input must be a negative integer''' ) UpperCamelCase : List[str] = len(bin(SCREAMING_SNAKE_CASE_ )[3:] ) UpperCamelCase : List[str] = bin(abs(SCREAMING_SNAKE_CASE_ ) - (1 << binary_number_length) )[3:] UpperCamelCase : Dict = ( ( '''1''' + '''0''' * (binary_number_length - len(SCREAMING_SNAKE_CASE_ )) + twos_complement_number ) if number < 0 else '''0''' ) return "0b" + twos_complement_number if __name__ == "__main__": import doctest doctest.testmod()
643
1
from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional, Union from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig, OnnxSeqaSeqConfigWithPast from ...utils import logging if TYPE_CHECKING: from ...feature_extraction_utils import FeatureExtractionMixin from ...tokenization_utils_base import PreTrainedTokenizerBase from ...utils import TensorType __UpperCAmelCase : Union[str, Any] = logging.get_logger(__name__) __UpperCAmelCase : List[Any] = { "openai/whisper-base": "https://huggingface.co/openai/whisper-base/resolve/main/config.json", } # fmt: off __UpperCAmelCase : List[Any] = [ 1, 2, 7, 8, 9, 10, 14, 25, 26, 27, 28, 29, 31, 58, 59, 60, 61, 62, 63, 90, 91, 92, 93, 357, 366, 438, 532, 685, 705, 796, 930, 1058, 1220, 1267, 1279, 1303, 1343, 1377, 1391, 1635, 1782, 1875, 2162, 2361, 2488, 3467, 4008, 4211, 4600, 4808, 5299, 5855, 6329, 7203, 9609, 9959, 10563, 10786, 11420, 11709, 11907, 13163, 13697, 13700, 14808, 15306, 16410, 16791, 17992, 19203, 19510, 20724, 22305, 22935, 27007, 30109, 30420, 33409, 34949, 40283, 40493, 40549, 47282, 49146, 50257, 50359, 50360, 50361 ] __UpperCAmelCase : Dict = [ 1, 2, 7, 8, 9, 10, 14, 25, 26, 27, 28, 29, 31, 58, 59, 60, 61, 62, 63, 90, 91, 92, 93, 359, 503, 522, 542, 873, 893, 902, 918, 922, 931, 1350, 1853, 1982, 2460, 2627, 3246, 3253, 3268, 3536, 3846, 3961, 4183, 4667, 6585, 6647, 7273, 9061, 9383, 10428, 10929, 11938, 12033, 12331, 12562, 13793, 14157, 14635, 15265, 15618, 16553, 16604, 18362, 18956, 20075, 21675, 22520, 26130, 26161, 26435, 28279, 29464, 31650, 32302, 32470, 36865, 42863, 47425, 49870, 50254, 50258, 50360, 50361, 50362 ] class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : int = "whisper" __UpperCamelCase : Tuple = ["past_key_values"] __UpperCamelCase : int = {"num_attention_heads": "encoder_attention_heads", "hidden_size": "d_model"} def __init__( self , __SCREAMING_SNAKE_CASE=51_865 , __SCREAMING_SNAKE_CASE=80 , __SCREAMING_SNAKE_CASE=6 , __SCREAMING_SNAKE_CASE=4 , __SCREAMING_SNAKE_CASE=6 , __SCREAMING_SNAKE_CASE=4 , __SCREAMING_SNAKE_CASE=1_536 , __SCREAMING_SNAKE_CASE=1_536 , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=50_257 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE="gelu" , __SCREAMING_SNAKE_CASE=256 , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=0.02 , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=1_500 , __SCREAMING_SNAKE_CASE=448 , __SCREAMING_SNAKE_CASE=50_256 , __SCREAMING_SNAKE_CASE=50_256 , __SCREAMING_SNAKE_CASE=50_256 , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=[220, 50_256] , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=256 , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=0.05 , __SCREAMING_SNAKE_CASE=10 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=10 , __SCREAMING_SNAKE_CASE=0 , __SCREAMING_SNAKE_CASE=7 , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" UpperCamelCase : Optional[int] = vocab_size UpperCamelCase : Dict = num_mel_bins UpperCamelCase : List[Any] = d_model UpperCamelCase : Optional[Any] = encoder_layers UpperCamelCase : Optional[int] = encoder_attention_heads UpperCamelCase : Dict = decoder_layers UpperCamelCase : List[Any] = decoder_attention_heads UpperCamelCase : List[Any] = decoder_ffn_dim UpperCamelCase : str = encoder_ffn_dim UpperCamelCase : Dict = dropout UpperCamelCase : Dict = attention_dropout UpperCamelCase : Optional[int] = activation_dropout UpperCamelCase : Optional[Any] = activation_function UpperCamelCase : Tuple = init_std UpperCamelCase : Tuple = encoder_layerdrop UpperCamelCase : Optional[int] = decoder_layerdrop UpperCamelCase : int = use_cache UpperCamelCase : Union[str, Any] = encoder_layers UpperCamelCase : Any = scale_embedding # scale factor will be sqrt(d_model) if True UpperCamelCase : int = max_source_positions UpperCamelCase : Union[str, Any] = max_target_positions # Audio Classification-specific parameters. Feel free to ignore for other classes. UpperCamelCase : str = classifier_proj_size UpperCamelCase : Optional[int] = use_weighted_layer_sum # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 UpperCamelCase : List[str] = apply_spec_augment UpperCamelCase : List[str] = mask_time_prob UpperCamelCase : Union[str, Any] = mask_time_length UpperCamelCase : str = mask_time_min_masks UpperCamelCase : int = mask_feature_prob UpperCamelCase : Any = mask_feature_length UpperCamelCase : List[str] = mask_feature_min_masks UpperCamelCase : Optional[int] = median_filter_width super().__init__( pad_token_id=__SCREAMING_SNAKE_CASE , bos_token_id=__SCREAMING_SNAKE_CASE , eos_token_id=__SCREAMING_SNAKE_CASE , is_encoder_decoder=__SCREAMING_SNAKE_CASE , decoder_start_token_id=__SCREAMING_SNAKE_CASE , suppress_tokens=__SCREAMING_SNAKE_CASE , begin_suppress_tokens=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) class UpperCAmelCase_ ( _a): '''simple docstring''' @property def _lowercase ( self ): """simple docstring""" UpperCamelCase : Tuple = OrderedDict( [ ('''input_features''', {0: '''batch''', 1: '''feature_size''', 2: '''encoder_sequence'''}), ] ) if self.use_past: UpperCamelCase : int = {0: '''batch'''} else: UpperCamelCase : Optional[Any] = {0: '''batch''', 1: '''decoder_sequence'''} if self.use_past: self.fill_with_past_key_values_(__SCREAMING_SNAKE_CASE , direction='''inputs''' ) return common_inputs def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = -1 , __SCREAMING_SNAKE_CASE = -1 , __SCREAMING_SNAKE_CASE = False , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = 22_050 , __SCREAMING_SNAKE_CASE = 5.0 , __SCREAMING_SNAKE_CASE = 220 , ): """simple docstring""" UpperCamelCase : Union[str, Any] = OrderedDict() UpperCamelCase : Any = OnnxConfig.generate_dummy_inputs( self , preprocessor=preprocessor.feature_extractor , batch_size=__SCREAMING_SNAKE_CASE , framework=__SCREAMING_SNAKE_CASE , sampling_rate=__SCREAMING_SNAKE_CASE , time_duration=__SCREAMING_SNAKE_CASE , frequency=__SCREAMING_SNAKE_CASE , ) UpperCamelCase : Tuple = encoder_inputs['''input_features'''].shape[2] UpperCamelCase : Union[str, Any] = encoder_sequence_length // 2 if self.use_past else seq_length UpperCamelCase : List[Any] = super().generate_dummy_inputs( preprocessor.tokenizer , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = encoder_inputs.pop('''input_features''' ) UpperCamelCase : Optional[int] = decoder_inputs.pop('''decoder_input_ids''' ) if "past_key_values" in decoder_inputs: UpperCamelCase : int = decoder_inputs.pop('''past_key_values''' ) return dummy_inputs @property def _lowercase ( self ): """simple docstring""" return 1e-3
643
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging __UpperCAmelCase : str = logging.get_logger(__name__) __UpperCAmelCase : Dict = { "hustvl/yolos-small": "https://huggingface.co/hustvl/yolos-small/resolve/main/config.json", # See all YOLOS models at https://huggingface.co/models?filter=yolos } class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : Optional[int] = "yolos" def __init__( self , __SCREAMING_SNAKE_CASE=768 , __SCREAMING_SNAKE_CASE=12 , __SCREAMING_SNAKE_CASE=12 , __SCREAMING_SNAKE_CASE=3_072 , __SCREAMING_SNAKE_CASE="gelu" , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=0.02 , __SCREAMING_SNAKE_CASE=1e-12 , __SCREAMING_SNAKE_CASE=[512, 864] , __SCREAMING_SNAKE_CASE=16 , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=100 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=1 , __SCREAMING_SNAKE_CASE=5 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=5 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=0.1 , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" super().__init__(**__SCREAMING_SNAKE_CASE ) UpperCamelCase : Tuple = hidden_size UpperCamelCase : List[Any] = num_hidden_layers UpperCamelCase : int = num_attention_heads UpperCamelCase : Dict = intermediate_size UpperCamelCase : Dict = hidden_act UpperCamelCase : int = hidden_dropout_prob UpperCamelCase : Any = attention_probs_dropout_prob UpperCamelCase : Optional[Any] = initializer_range UpperCamelCase : List[Any] = layer_norm_eps UpperCamelCase : int = image_size UpperCamelCase : Any = patch_size UpperCamelCase : str = num_channels UpperCamelCase : str = qkv_bias UpperCamelCase : Tuple = num_detection_tokens UpperCamelCase : List[Any] = use_mid_position_embeddings UpperCamelCase : Dict = auxiliary_loss # Hungarian matcher UpperCamelCase : Optional[Any] = class_cost UpperCamelCase : Union[str, Any] = bbox_cost UpperCamelCase : Any = giou_cost # Loss coefficients UpperCamelCase : List[Any] = bbox_loss_coefficient UpperCamelCase : Union[str, Any] = giou_loss_coefficient UpperCamelCase : Dict = eos_coefficient class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : List[str] = version.parse("1.11") @property def _lowercase ( self ): """simple docstring""" return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ] ) @property def _lowercase ( self ): """simple docstring""" return 1e-4 @property def _lowercase ( self ): """simple docstring""" return 12
643
1
def a ( SCREAMING_SNAKE_CASE_ : str ): """simple docstring""" return "".join(chr(ord(SCREAMING_SNAKE_CASE_ ) - 3_2 ) if '''a''' <= char <= '''z''' else char for char in word ) if __name__ == "__main__": from doctest import testmod testmod()
643
def a ( SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int ): """simple docstring""" if a < 0 or b < 0: raise ValueError('''the value of both inputs must be positive''' ) UpperCamelCase : int = str(bin(SCREAMING_SNAKE_CASE_ ) )[2:] # remove the leading "0b" UpperCamelCase : List[str] = str(bin(SCREAMING_SNAKE_CASE_ ) )[2:] UpperCamelCase : Tuple = max(len(SCREAMING_SNAKE_CASE_ ) , len(SCREAMING_SNAKE_CASE_ ) ) return "0b" + "".join( str(int('''1''' in (char_a, char_b) ) ) for char_a, char_b in zip(a_binary.zfill(SCREAMING_SNAKE_CASE_ ) , b_binary.zfill(SCREAMING_SNAKE_CASE_ ) ) ) if __name__ == "__main__": import doctest doctest.testmod()
643
1
# Imports import numpy as np class UpperCAmelCase_ : '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None ): """simple docstring""" self.set_matricies(red=__SCREAMING_SNAKE_CASE , green=__SCREAMING_SNAKE_CASE , blue=__SCREAMING_SNAKE_CASE , red_edge=__SCREAMING_SNAKE_CASE , nir=__SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None ): """simple docstring""" if red is not None: UpperCamelCase : List[str] = red if green is not None: UpperCamelCase : Dict = green if blue is not None: UpperCamelCase : List[str] = blue if red_edge is not None: UpperCamelCase : Union[str, Any] = red_edge if nir is not None: UpperCamelCase : Dict = nir return True def _lowercase ( self , __SCREAMING_SNAKE_CASE="" , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None ): """simple docstring""" self.set_matricies(red=__SCREAMING_SNAKE_CASE , green=__SCREAMING_SNAKE_CASE , blue=__SCREAMING_SNAKE_CASE , red_edge=__SCREAMING_SNAKE_CASE , nir=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Tuple = { '''ARVI2''': self.arvaa, '''CCCI''': self.ccci, '''CVI''': self.cvi, '''GLI''': self.gli, '''NDVI''': self.ndvi, '''BNDVI''': self.bndvi, '''redEdgeNDVI''': self.red_edge_ndvi, '''GNDVI''': self.gndvi, '''GBNDVI''': self.gbndvi, '''GRNDVI''': self.grndvi, '''RBNDVI''': self.rbndvi, '''PNDVI''': self.pndvi, '''ATSAVI''': self.atsavi, '''BWDRVI''': self.bwdrvi, '''CIgreen''': self.ci_green, '''CIrededge''': self.ci_rededge, '''CI''': self.ci, '''CTVI''': self.ctvi, '''GDVI''': self.gdvi, '''EVI''': self.evi, '''GEMI''': self.gemi, '''GOSAVI''': self.gosavi, '''GSAVI''': self.gsavi, '''Hue''': self.hue, '''IVI''': self.ivi, '''IPVI''': self.ipvi, '''I''': self.i, '''RVI''': self.rvi, '''MRVI''': self.mrvi, '''MSAVI''': self.m_savi, '''NormG''': self.norm_g, '''NormNIR''': self.norm_nir, '''NormR''': self.norm_r, '''NGRDI''': self.ngrdi, '''RI''': self.ri, '''S''': self.s, '''IF''': self._if, '''DVI''': self.dvi, '''TVI''': self.tvi, '''NDRE''': self.ndre, } try: return funcs[index]() except KeyError: print('''Index not in the list!''' ) return False def _lowercase ( self ): """simple docstring""" return -0.18 + (1.17 * ((self.nir - self.red) / (self.nir + self.red))) def _lowercase ( self ): """simple docstring""" return ((self.nir - self.redEdge) / (self.nir + self.redEdge)) / ( (self.nir - self.red) / (self.nir + self.red) ) def _lowercase ( self ): """simple docstring""" return self.nir * (self.red / (self.green**2)) def _lowercase ( self ): """simple docstring""" return (2 * self.green - self.red - self.blue) / ( 2 * self.green + self.red + self.blue ) def _lowercase ( self ): """simple docstring""" return (self.nir - self.red) / (self.nir + self.red) def _lowercase ( self ): """simple docstring""" return (self.nir - self.blue) / (self.nir + self.blue) def _lowercase ( self ): """simple docstring""" return (self.redEdge - self.red) / (self.redEdge + self.red) def _lowercase ( self ): """simple docstring""" return (self.nir - self.green) / (self.nir + self.green) def _lowercase ( self ): """simple docstring""" return (self.nir - (self.green + self.blue)) / ( self.nir + (self.green + self.blue) ) def _lowercase ( self ): """simple docstring""" return (self.nir - (self.green + self.red)) / ( self.nir + (self.green + self.red) ) def _lowercase ( self ): """simple docstring""" return (self.nir - (self.blue + self.red)) / (self.nir + (self.blue + self.red)) def _lowercase ( self ): """simple docstring""" return (self.nir - (self.green + self.red + self.blue)) / ( self.nir + (self.green + self.red + self.blue) ) def _lowercase ( self , __SCREAMING_SNAKE_CASE=0.08 , __SCREAMING_SNAKE_CASE=1.22 , __SCREAMING_SNAKE_CASE=0.03 ): """simple docstring""" return a * ( (self.nir - a * self.red - b) / (a * self.nir + self.red - a * b + x * (1 + a**2)) ) def _lowercase ( self ): """simple docstring""" return (0.1 * self.nir - self.blue) / (0.1 * self.nir + self.blue) def _lowercase ( self ): """simple docstring""" return (self.nir / self.green) - 1 def _lowercase ( self ): """simple docstring""" return (self.nir / self.redEdge) - 1 def _lowercase ( self ): """simple docstring""" return (self.red - self.blue) / self.red def _lowercase ( self ): """simple docstring""" UpperCamelCase : Any = self.ndvi() return ((ndvi + 0.5) / (abs(ndvi + 0.5 ))) * (abs(ndvi + 0.5 ) ** (1 / 2)) def _lowercase ( self ): """simple docstring""" return self.nir - self.green def _lowercase ( self ): """simple docstring""" return 2.5 * ( (self.nir - self.red) / (self.nir + 6 * self.red - 7.5 * self.blue + 1) ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Tuple = (2 * (self.nir**2 - self.red**2) + 1.5 * self.nir + 0.5 * self.red) / ( self.nir + self.red + 0.5 ) return n * (1 - 0.25 * n) - (self.red - 0.125) / (1 - self.red) def _lowercase ( self , __SCREAMING_SNAKE_CASE=0.16 ): """simple docstring""" return (self.nir - self.green) / (self.nir + self.green + y) def _lowercase ( self , __SCREAMING_SNAKE_CASE=0.5 ): """simple docstring""" return ((self.nir - self.green) / (self.nir + self.green + n)) * (1 + n) def _lowercase ( self ): """simple docstring""" return np.arctan( ((2 * self.red - self.green - self.blue) / 30.5) * (self.green - self.blue) ) def _lowercase ( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None ): """simple docstring""" return (self.nir - b) / (a * self.red) def _lowercase ( self ): """simple docstring""" return (self.nir / ((self.nir + self.red) / 2)) * (self.ndvi() + 1) def _lowercase ( self ): """simple docstring""" return (self.red + self.green + self.blue) / 30.5 def _lowercase ( self ): """simple docstring""" return self.nir / self.red def _lowercase ( self ): """simple docstring""" return (self.rvi() - 1) / (self.rvi() + 1) def _lowercase ( self ): """simple docstring""" return ( (2 * self.nir + 1) - ((2 * self.nir + 1) ** 2 - 8 * (self.nir - self.red)) ** (1 / 2) ) / 2 def _lowercase ( self ): """simple docstring""" return self.green / (self.nir + self.red + self.green) def _lowercase ( self ): """simple docstring""" return self.nir / (self.nir + self.red + self.green) def _lowercase ( self ): """simple docstring""" return self.red / (self.nir + self.red + self.green) def _lowercase ( self ): """simple docstring""" return (self.green - self.red) / (self.green + self.red) def _lowercase ( self ): """simple docstring""" return (self.red - self.green) / (self.red + self.green) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Tuple = np.max([np.max(self.red ), np.max(self.green ), np.max(self.blue )] ) UpperCamelCase : Any = np.min([np.min(self.red ), np.min(self.green ), np.min(self.blue )] ) return (max_value - min_value) / max_value def _lowercase ( self ): """simple docstring""" return (2 * self.red - self.green - self.blue) / (self.green - self.blue) def _lowercase ( self ): """simple docstring""" return self.nir / self.red def _lowercase ( self ): """simple docstring""" return (self.ndvi() + 0.5) ** (1 / 2) def _lowercase ( self ): """simple docstring""" return (self.nir - self.redEdge) / (self.nir + self.redEdge)
643
from itertools import product from cva import COLOR_BGR2GRAY, cvtColor, imread, imshow, waitKey from numpy import dot, exp, mgrid, pi, ravel, square, uinta, zeros def a ( SCREAMING_SNAKE_CASE_ : List[Any] , SCREAMING_SNAKE_CASE_ : List[str] ): """simple docstring""" UpperCamelCase : List[str] = k_size // 2 UpperCamelCase , UpperCamelCase : Optional[int] = mgrid[0 - center : k_size - center, 0 - center : k_size - center] UpperCamelCase : Dict = 1 / (2 * pi * sigma) * exp(-(square(SCREAMING_SNAKE_CASE_ ) + square(SCREAMING_SNAKE_CASE_ )) / (2 * square(SCREAMING_SNAKE_CASE_ )) ) return g def a ( SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : int ): """simple docstring""" UpperCamelCase , UpperCamelCase : Tuple = image.shape[0], image.shape[1] # dst image height and width UpperCamelCase : str = height - k_size + 1 UpperCamelCase : Optional[int] = width - k_size + 1 # im2col, turn the k_size*k_size pixels into a row and np.vstack all rows UpperCamelCase : List[Any] = zeros((dst_height * dst_width, k_size * k_size) ) UpperCamelCase : Tuple = 0 for i, j in product(range(SCREAMING_SNAKE_CASE_ ) , range(SCREAMING_SNAKE_CASE_ ) ): UpperCamelCase : Dict = ravel(image[i : i + k_size, j : j + k_size] ) UpperCamelCase : Dict = window row += 1 # turn the kernel into shape(k*k, 1) UpperCamelCase : Optional[int] = gen_gaussian_kernel(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) UpperCamelCase : List[Any] = ravel(SCREAMING_SNAKE_CASE_ ) # reshape and get the dst image UpperCamelCase : Optional[int] = dot(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ).reshape(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ).astype(SCREAMING_SNAKE_CASE_ ) return dst if __name__ == "__main__": # read original image __UpperCAmelCase : Union[str, Any] = imread(r"../image_data/lena.jpg") # turn image in gray scale value __UpperCAmelCase : Optional[Any] = cvtColor(img, COLOR_BGR2GRAY) # get values with two different mask size __UpperCAmelCase : Optional[int] = gaussian_filter(gray, 3, sigma=1) __UpperCAmelCase : List[Any] = gaussian_filter(gray, 5, sigma=0.8) # show result images imshow("gaussian filter with 3x3 mask", gaussianaxa) imshow("gaussian filter with 5x5 mask", gaussianaxa) waitKey()
643
1
from __future__ import annotations def a ( SCREAMING_SNAKE_CASE_ : list[float] , SCREAMING_SNAKE_CASE_ : Optional[Any] ): """simple docstring""" print(F"""Vertex\tShortest Distance from vertex {src}""" ) for i, d in enumerate(SCREAMING_SNAKE_CASE_ ): print(F"""{i}\t\t{d}""" ) def a ( SCREAMING_SNAKE_CASE_ : list[dict[str, int]] , SCREAMING_SNAKE_CASE_ : list[float] , SCREAMING_SNAKE_CASE_ : int ): """simple docstring""" for j in range(SCREAMING_SNAKE_CASE_ ): UpperCamelCase , UpperCamelCase , UpperCamelCase : Optional[int] = (graph[j][k] for k in ['''src''', '''dst''', '''weight''']) if distance[u] != float('''inf''' ) and distance[u] + w < distance[v]: return True return False def a ( SCREAMING_SNAKE_CASE_ : list[dict[str, int]] , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int ): """simple docstring""" UpperCamelCase : Union[str, Any] = [float('''inf''' )] * vertex_count UpperCamelCase : Tuple = 0.0 for _ in range(vertex_count - 1 ): for j in range(SCREAMING_SNAKE_CASE_ ): UpperCamelCase , UpperCamelCase , UpperCamelCase : str = (graph[j][k] for k in ['''src''', '''dst''', '''weight''']) if distance[u] != float('''inf''' ) and distance[u] + w < distance[v]: UpperCamelCase : str = distance[u] + w UpperCamelCase : str = check_negative_cycle(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if negative_cycle_exists: raise Exception('''Negative cycle found''' ) return distance if __name__ == "__main__": import doctest doctest.testmod() __UpperCAmelCase : Optional[Any] = int(input("Enter number of vertices: ").strip()) __UpperCAmelCase : Any = int(input("Enter number of edges: ").strip()) __UpperCAmelCase : list[dict[str, int]] = [{} for _ in range(E)] for i in range(E): print("Edge ", i + 1) __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase : str = ( int(x) for x in input("Enter source, destination, weight: ").strip().split(" ") ) __UpperCAmelCase : List[str] = {"src": src, "dst": dest, "weight": weight} __UpperCAmelCase : Optional[int] = int(input("\nEnter shortest path source:").strip()) __UpperCAmelCase : List[str] = bellman_ford(graph, V, E, source) print_distance(shortest_distance, 0)
643
from .imports import is_tqdm_available if is_tqdm_available(): from tqdm.auto import tqdm as _tqdm from ..state import PartialState def a ( SCREAMING_SNAKE_CASE_ : bool = True , *SCREAMING_SNAKE_CASE_ : List[str] , **SCREAMING_SNAKE_CASE_ : Tuple ): """simple docstring""" if not is_tqdm_available(): raise ImportError('''Accelerate\'s `tqdm` module requires `tqdm` to be installed. Please run `pip install tqdm`.''' ) UpperCamelCase : int = False if main_process_only: UpperCamelCase : int = PartialState().local_process_index == 0 return _tqdm(*SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , disable=SCREAMING_SNAKE_CASE_ )
643
1
import PIL.Image import PIL.ImageOps from packaging import version from PIL import Image if version.parse(version.parse(PIL.__version__).base_version) >= version.parse("9.1.0"): __UpperCAmelCase : Tuple = { "linear": PIL.Image.Resampling.BILINEAR, "bilinear": PIL.Image.Resampling.BILINEAR, "bicubic": PIL.Image.Resampling.BICUBIC, "lanczos": PIL.Image.Resampling.LANCZOS, "nearest": PIL.Image.Resampling.NEAREST, } else: __UpperCAmelCase : Any = { "linear": PIL.Image.LINEAR, "bilinear": PIL.Image.BILINEAR, "bicubic": PIL.Image.BICUBIC, "lanczos": PIL.Image.LANCZOS, "nearest": PIL.Image.NEAREST, } def a ( SCREAMING_SNAKE_CASE_ : str ): """simple docstring""" UpperCamelCase : int = (images / 2 + 0.5).clamp(0 , 1 ) UpperCamelCase : List[Any] = images.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() UpperCamelCase : Any = numpy_to_pil(SCREAMING_SNAKE_CASE_ ) return images def a ( SCREAMING_SNAKE_CASE_ : List[Any] ): """simple docstring""" if images.ndim == 3: UpperCamelCase : int = images[None, ...] UpperCamelCase : str = (images * 2_5_5).round().astype('''uint8''' ) if images.shape[-1] == 1: # special case for grayscale (single channel) images UpperCamelCase : Union[str, Any] = [Image.fromarray(image.squeeze() , mode='''L''' ) for image in images] else: UpperCamelCase : Optional[Any] = [Image.fromarray(SCREAMING_SNAKE_CASE_ ) for image in images] return pil_images
643
import io import os import unicodedata from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging __UpperCAmelCase : Any = logging.get_logger(__name__) __UpperCAmelCase : int = "▁" __UpperCAmelCase : Tuple = {"vocab_file": "vocab.txt", "sentencepiece_model_ckpt": "sentencepiece.bpe.model"} __UpperCAmelCase : Dict = { "sentencepiece_model_file": "sentencepiece.bpe.model", "vocab_file": "vocab.txt", } __UpperCAmelCase : Dict = { "vocab_file": { "ernie-m-base": "https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/vocab.txt", "ernie-m-large": "https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/vocab.txt", }, "sentencepiece_model_file": { "ernie-m-base": "https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/sentencepiece.bpe.model", "ernie-m-large": "https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/sentencepiece.bpe.model", }, } __UpperCAmelCase : str = { "ernie-m-base": 514, "ernie-m-large": 514, } __UpperCAmelCase : Optional[int] = { "ernie-m-base": {"do_lower_case": False}, "ernie-m-large": {"do_lower_case": False}, } class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : List[str] = ["input_ids"] __UpperCamelCase : List[str] = VOCAB_FILES_NAMES __UpperCamelCase : List[Any] = PRETRAINED_INIT_CONFIGURATION __UpperCamelCase : List[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCamelCase : int = PRETRAINED_VOCAB_FILES_MAP __UpperCamelCase : List[str] = RESOURCE_FILES_NAMES def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE="utf8" , __SCREAMING_SNAKE_CASE="[UNK]" , __SCREAMING_SNAKE_CASE="[SEP]" , __SCREAMING_SNAKE_CASE="[PAD]" , __SCREAMING_SNAKE_CASE="[CLS]" , __SCREAMING_SNAKE_CASE="[MASK]" , __SCREAMING_SNAKE_CASE = None , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" UpperCamelCase : int = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( do_lower_case=__SCREAMING_SNAKE_CASE , unk_token=__SCREAMING_SNAKE_CASE , sep_token=__SCREAMING_SNAKE_CASE , pad_token=__SCREAMING_SNAKE_CASE , cls_token=__SCREAMING_SNAKE_CASE , mask_token=__SCREAMING_SNAKE_CASE , vocab_file=__SCREAMING_SNAKE_CASE , encoding=__SCREAMING_SNAKE_CASE , sp_model_kwargs=self.sp_model_kwargs , **__SCREAMING_SNAKE_CASE , ) UpperCamelCase : List[str] = do_lower_case UpperCamelCase : Dict = sentencepiece_model_ckpt UpperCamelCase : str = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(__SCREAMING_SNAKE_CASE ) # to mimic paddlenlp.transformers.ernie_m.tokenizer.ErnieMTokenizer functioning if vocab_file is not None: UpperCamelCase : Optional[Any] = self.load_vocab(filepath=__SCREAMING_SNAKE_CASE ) else: UpperCamelCase : int = {self.sp_model.id_to_piece(__SCREAMING_SNAKE_CASE ): id for id in range(self.sp_model.get_piece_size() )} UpperCamelCase : str = {v: k for k, v in self.vocab.items()} def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if text is None: return None UpperCamelCase : str = self.tokenize(__SCREAMING_SNAKE_CASE ) UpperCamelCase , UpperCamelCase : str = '''''', [] for i, ch in enumerate(__SCREAMING_SNAKE_CASE ): if ch in self.SP_CHAR_MAPPING: UpperCamelCase : Optional[int] = self.SP_CHAR_MAPPING.get(__SCREAMING_SNAKE_CASE ) else: UpperCamelCase : Optional[Any] = unicodedata.normalize('''NFKC''' , __SCREAMING_SNAKE_CASE ) if self.is_whitespace(__SCREAMING_SNAKE_CASE ): continue normalized_text += ch char_mapping.extend([i] * len(__SCREAMING_SNAKE_CASE ) ) UpperCamelCase , UpperCamelCase , UpperCamelCase : Tuple = normalized_text, [], 0 if self.do_lower_case: UpperCamelCase : Tuple = text.lower() for token in split_tokens: if token[:1] == "▁": UpperCamelCase : Any = token[1:] UpperCamelCase : Optional[int] = text[offset:].index(__SCREAMING_SNAKE_CASE ) + offset UpperCamelCase : List[Any] = start + len(__SCREAMING_SNAKE_CASE ) token_mapping.append((char_mapping[start], char_mapping[end - 1] + 1) ) UpperCamelCase : str = end return token_mapping @property def _lowercase ( self ): """simple docstring""" return len(self.vocab ) def _lowercase ( self ): """simple docstring""" return dict(self.vocab , **self.added_tokens_encoder ) def __getstate__( self ): """simple docstring""" UpperCamelCase : Optional[Any] = self.__dict__.copy() UpperCamelCase : str = None return state def __setstate__( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Tuple = d # for backward compatibility if not hasattr(self , '''sp_model_kwargs''' ): UpperCamelCase : Optional[int] = {} UpperCamelCase : Optional[Any] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.sentencepiece_model_ckpt ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" return "".join((self.SP_CHAR_MAPPING.get(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) for c in text) ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=64 , __SCREAMING_SNAKE_CASE=0.1 ): """simple docstring""" if self.sp_model_kwargs.get('''enable_sampling''' ) is True: UpperCamelCase : List[str] = True if self.sp_model_kwargs.get('''alpha''' ) is not None: UpperCamelCase : Any = self.sp_model_kwargs.get('''alpha''' ) if self.sp_model_kwargs.get('''nbest_size''' ) is not None: UpperCamelCase : Tuple = self.sp_model_kwargs.get('''nbest_size''' ) if not enable_sampling: UpperCamelCase : int = self.sp_model.EncodeAsPieces(__SCREAMING_SNAKE_CASE ) else: UpperCamelCase : Optional[Any] = self.sp_model.SampleEncodeAsPieces(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = [] for pi, piece in enumerate(__SCREAMING_SNAKE_CASE ): if piece == SPIECE_UNDERLINE: if not pieces[pi + 1].startswith(__SCREAMING_SNAKE_CASE ) and pi != 0: new_pieces.append(__SCREAMING_SNAKE_CASE ) continue else: continue UpperCamelCase : Any = 0 for i, chunk in enumerate(__SCREAMING_SNAKE_CASE ): if chunk == SPIECE_UNDERLINE: continue if self.is_ch_char(__SCREAMING_SNAKE_CASE ) or self.is_punct(__SCREAMING_SNAKE_CASE ): if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE: new_pieces.append(piece[lst_i:i] ) new_pieces.append(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Dict = i + 1 elif chunk.isdigit() and i > 0 and not piece[i - 1].isdigit(): if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE: new_pieces.append(piece[lst_i:i] ) UpperCamelCase : Union[str, Any] = i elif not chunk.isdigit() and i > 0 and piece[i - 1].isdigit(): if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE: new_pieces.append(piece[lst_i:i] ) UpperCamelCase : Any = i if len(__SCREAMING_SNAKE_CASE ) > lst_i: new_pieces.append(piece[lst_i:] ) return new_pieces def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Optional[int] = ''''''.join(__SCREAMING_SNAKE_CASE ).replace(__SCREAMING_SNAKE_CASE , ''' ''' ).strip() return out_string def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : int = self.convert_ids_to_tokens(__SCREAMING_SNAKE_CASE ) UpperCamelCase : int = ''''''.join(__SCREAMING_SNAKE_CASE ).replace(__SCREAMING_SNAKE_CASE , ''' ''' ).strip() return out_string def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" return self.vocab.get(__SCREAMING_SNAKE_CASE , self.vocab.get(self.unk_token ) ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" return self.reverse_vocab.get(__SCREAMING_SNAKE_CASE , self.unk_token ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None ): """simple docstring""" if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] UpperCamelCase : Any = [self.cls_token_id] UpperCamelCase : str = [self.sep_token_id] return _cls + token_ids_a + _sep + _sep + token_ids_a + _sep def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None ): """simple docstring""" if offset_mapping_a is None: return [(0, 0)] + offset_mapping_a + [(0, 0)] return [(0, 0)] + offset_mapping_a + [(0, 0), (0, 0)] + offset_mapping_a + [(0, 0)] def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=False ): """simple docstring""" if already_has_special_tokens: if token_ids_a is not None: raise ValueError( '''You should not supply a second sequence if the provided sequence of ''' '''ids is already formatted with special tokens for the model.''' ) return [1 if x in [self.sep_token_id, self.cls_token_id] else 0 for x in token_ids_a] if token_ids_a is not None: return [1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1, 1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1] return [1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1] def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ): """simple docstring""" if token_ids_a is None: # [CLS] X [SEP] return (len(__SCREAMING_SNAKE_CASE ) + 2) * [0] # [CLS] A [SEP] [SEP] B [SEP] return [0] * (len(__SCREAMING_SNAKE_CASE ) + 1) + [1] * (len(__SCREAMING_SNAKE_CASE ) + 3) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if "\u4e00" <= char <= "\u9fff": return True return False def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if ("a" <= char <= "z") or ("A" <= char <= "Z"): return True return False def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if char in ",;:.?!~,;:。?!《》【】": return True return False def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if char == " " or char == "\t" or char == "\n" or char == "\r": return True if len(__SCREAMING_SNAKE_CASE ) == 1: UpperCamelCase : Optional[int] = unicodedata.category(__SCREAMING_SNAKE_CASE ) if cat == "Zs": return True return False def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : int = {} with io.open(__SCREAMING_SNAKE_CASE , '''r''' , encoding='''utf-8''' ) as f: for index, line in enumerate(__SCREAMING_SNAKE_CASE ): UpperCamelCase : Tuple = line.rstrip('''\n''' ) UpperCamelCase : List[Any] = int(__SCREAMING_SNAKE_CASE ) return token_to_idx def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ): """simple docstring""" UpperCamelCase : Union[str, Any] = 0 if os.path.isdir(__SCREAMING_SNAKE_CASE ): UpperCamelCase : Dict = os.path.join( __SCREAMING_SNAKE_CASE , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) else: UpperCamelCase : Union[str, Any] = (filename_prefix + '''-''' if filename_prefix else '''''') + save_directory with open(__SCREAMING_SNAKE_CASE , '''w''' , encoding='''utf-8''' ) as writer: for token, token_index in sorted(self.vocab.items() , key=lambda __SCREAMING_SNAKE_CASE : kv[1] ): if index != token_index: logger.warning( f"""Saving vocabulary to {vocab_file}: vocabulary indices are not consecutive.""" ''' Please check that the vocabulary is not corrupted!''' ) UpperCamelCase : List[Any] = token_index writer.write(token + '''\n''' ) index += 1 UpperCamelCase : Tuple = os.path.join(__SCREAMING_SNAKE_CASE , '''sentencepiece.bpe.model''' ) with open(__SCREAMING_SNAKE_CASE , '''wb''' ) as fi: UpperCamelCase : List[Any] = self.sp_model.serialized_model_proto() fi.write(__SCREAMING_SNAKE_CASE ) return (vocab_file,)
643
1
import tempfile import torch from diffusers import IPNDMScheduler from .test_schedulers import SchedulerCommonTest class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : Union[str, Any] = (IPNDMScheduler,) __UpperCamelCase : str = (("num_inference_steps", 50),) def _lowercase ( self , **__SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Optional[Any] = {'''num_train_timesteps''': 1_000} config.update(**__SCREAMING_SNAKE_CASE ) return config def _lowercase ( self , __SCREAMING_SNAKE_CASE=0 , **__SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Optional[int] = dict(self.forward_default_kwargs ) UpperCamelCase : int = kwargs.pop('''num_inference_steps''' , __SCREAMING_SNAKE_CASE ) UpperCamelCase : int = self.dummy_sample UpperCamelCase : Dict = 0.1 * sample UpperCamelCase : Any = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] for scheduler_class in self.scheduler_classes: UpperCamelCase : List[Any] = self.get_scheduler_config(**__SCREAMING_SNAKE_CASE ) UpperCamelCase : int = scheduler_class(**__SCREAMING_SNAKE_CASE ) scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) # copy over dummy past residuals UpperCamelCase : Optional[int] = dummy_past_residuals[:] if time_step is None: UpperCamelCase : Union[str, Any] = scheduler.timesteps[len(scheduler.timesteps ) // 2] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[Any] = scheduler_class.from_pretrained(__SCREAMING_SNAKE_CASE ) new_scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) # copy over dummy past residuals UpperCamelCase : List[str] = dummy_past_residuals[:] UpperCamelCase : List[Any] = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample UpperCamelCase : Optional[int] = new_scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" UpperCamelCase : int = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample UpperCamelCase : int = new_scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" def _lowercase ( self ): """simple docstring""" pass def _lowercase ( self , __SCREAMING_SNAKE_CASE=0 , **__SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : List[Any] = dict(self.forward_default_kwargs ) UpperCamelCase : Optional[Any] = kwargs.pop('''num_inference_steps''' , __SCREAMING_SNAKE_CASE ) UpperCamelCase : int = self.dummy_sample UpperCamelCase : Union[str, Any] = 0.1 * sample UpperCamelCase : Optional[int] = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] for scheduler_class in self.scheduler_classes: UpperCamelCase : Any = self.get_scheduler_config() UpperCamelCase : Any = scheduler_class(**__SCREAMING_SNAKE_CASE ) scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) # copy over dummy past residuals (must be after setting timesteps) UpperCamelCase : str = dummy_past_residuals[:] if time_step is None: UpperCamelCase : List[Any] = scheduler.timesteps[len(scheduler.timesteps ) // 2] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(__SCREAMING_SNAKE_CASE ) UpperCamelCase : str = scheduler_class.from_pretrained(__SCREAMING_SNAKE_CASE ) # copy over dummy past residuals new_scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) # copy over dummy past residual (must be after setting timesteps) UpperCamelCase : Tuple = dummy_past_residuals[:] UpperCamelCase : List[Any] = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample UpperCamelCase : Dict = new_scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" UpperCamelCase : Tuple = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample UpperCamelCase : str = new_scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" def _lowercase ( self , **__SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : List[str] = self.scheduler_classes[0] UpperCamelCase : Any = self.get_scheduler_config(**__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = scheduler_class(**__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[Any] = 10 UpperCamelCase : Optional[Any] = self.dummy_model() UpperCamelCase : Dict = self.dummy_sample_deter scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) for i, t in enumerate(scheduler.timesteps ): UpperCamelCase : str = model(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[Any] = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ).prev_sample for i, t in enumerate(scheduler.timesteps ): UpperCamelCase : List[str] = model(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) UpperCamelCase : Any = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ).prev_sample return sample def _lowercase ( self ): """simple docstring""" UpperCamelCase : Optional[int] = dict(self.forward_default_kwargs ) UpperCamelCase : Dict = kwargs.pop('''num_inference_steps''' , __SCREAMING_SNAKE_CASE ) for scheduler_class in self.scheduler_classes: UpperCamelCase : List[str] = self.get_scheduler_config() UpperCamelCase : Tuple = scheduler_class(**__SCREAMING_SNAKE_CASE ) UpperCamelCase : Tuple = self.dummy_sample UpperCamelCase : Union[str, Any] = 0.1 * sample if num_inference_steps is not None and hasattr(__SCREAMING_SNAKE_CASE , '''set_timesteps''' ): scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) elif num_inference_steps is not None and not hasattr(__SCREAMING_SNAKE_CASE , '''set_timesteps''' ): UpperCamelCase : Optional[Any] = num_inference_steps # copy over dummy past residuals (must be done after set_timesteps) UpperCamelCase : int = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] UpperCamelCase : Optional[Any] = dummy_past_residuals[:] UpperCamelCase : List[Any] = scheduler.timesteps[5] UpperCamelCase : List[Any] = scheduler.timesteps[6] UpperCamelCase : int = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample UpperCamelCase : Union[str, Any] = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample self.assertEqual(output_a.shape , sample.shape ) self.assertEqual(output_a.shape , output_a.shape ) UpperCamelCase : Dict = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample UpperCamelCase : Optional[Any] = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample self.assertEqual(output_a.shape , sample.shape ) self.assertEqual(output_a.shape , output_a.shape ) def _lowercase ( self ): """simple docstring""" for timesteps in [100, 1_000]: self.check_over_configs(num_train_timesteps=__SCREAMING_SNAKE_CASE , time_step=__SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" for t, num_inference_steps in zip([1, 5, 10] , [10, 50, 100] ): self.check_over_forward(num_inference_steps=__SCREAMING_SNAKE_CASE , time_step=__SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Dict = self.full_loop() UpperCamelCase : Dict = torch.mean(torch.abs(__SCREAMING_SNAKE_CASE ) ) assert abs(result_mean.item() - 2_540_529 ) < 10
643
from typing import List, Optional, Tuple, Union import torch from torch import nn from torch.nn import CrossEntropyLoss from ... import AutoBackbone from ...modeling_outputs import SemanticSegmenterOutput from ...modeling_utils import PreTrainedModel from ...utils import add_start_docstrings, add_start_docstrings_to_model_forward, replace_return_docstrings from ...utils.backbone_utils import BackboneMixin from .configuration_upernet import UperNetConfig __UpperCAmelCase : List[Any] = [ "openmmlab/upernet-convnext-tiny", # See all UperNet models at https://huggingface.co/models?filter=upernet ] # General docstring __UpperCAmelCase : List[str] = "UperNetConfig" class UpperCAmelCase_ ( nn.Module): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = 0 , __SCREAMING_SNAKE_CASE = False , __SCREAMING_SNAKE_CASE = 1 , ): """simple docstring""" super().__init__() UpperCamelCase : str = nn.Convad( in_channels=__SCREAMING_SNAKE_CASE , out_channels=__SCREAMING_SNAKE_CASE , kernel_size=__SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE , bias=__SCREAMING_SNAKE_CASE , dilation=__SCREAMING_SNAKE_CASE , ) UpperCamelCase : int = nn.BatchNormad(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[Any] = nn.ReLU() def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Any = self.conv(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[Any] = self.batch_norm(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = self.activation(__SCREAMING_SNAKE_CASE ) return output class UpperCAmelCase_ ( nn.Module): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" super().__init__() UpperCamelCase : List[Any] = [ nn.AdaptiveAvgPoolad(__SCREAMING_SNAKE_CASE ), UperNetConvModule(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , kernel_size=1 ), ] for i, layer in enumerate(self.layers ): self.add_module(str(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : str = input for layer in self.layers: UpperCamelCase : int = layer(__SCREAMING_SNAKE_CASE ) return hidden_state class UpperCAmelCase_ ( nn.Module): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" super().__init__() UpperCamelCase : List[Any] = pool_scales UpperCamelCase : Dict = align_corners UpperCamelCase : Optional[int] = in_channels UpperCamelCase : Union[str, Any] = channels UpperCamelCase : List[str] = [] for i, pool_scale in enumerate(__SCREAMING_SNAKE_CASE ): UpperCamelCase : Union[str, Any] = UperNetPyramidPoolingBlock(pool_scale=__SCREAMING_SNAKE_CASE , in_channels=__SCREAMING_SNAKE_CASE , channels=__SCREAMING_SNAKE_CASE ) self.blocks.append(__SCREAMING_SNAKE_CASE ) self.add_module(str(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : str = [] for ppm in self.blocks: UpperCamelCase : List[str] = ppm(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Tuple = nn.functional.interpolate( __SCREAMING_SNAKE_CASE , size=x.size()[2:] , mode='''bilinear''' , align_corners=self.align_corners ) ppm_outs.append(__SCREAMING_SNAKE_CASE ) return ppm_outs class UpperCAmelCase_ ( nn.Module): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" super().__init__() UpperCamelCase : int = config UpperCamelCase : List[str] = config.pool_scales # e.g. (1, 2, 3, 6) UpperCamelCase : Optional[int] = in_channels UpperCamelCase : str = config.hidden_size UpperCamelCase : str = False UpperCamelCase : List[str] = nn.Convad(self.channels , config.num_labels , kernel_size=1 ) # PSP Module UpperCamelCase : Optional[int] = UperNetPyramidPoolingModule( self.pool_scales , self.in_channels[-1] , self.channels , align_corners=self.align_corners , ) UpperCamelCase : str = UperNetConvModule( self.in_channels[-1] + len(self.pool_scales ) * self.channels , self.channels , kernel_size=3 , padding=1 , ) # FPN Module UpperCamelCase : Union[str, Any] = nn.ModuleList() UpperCamelCase : Union[str, Any] = nn.ModuleList() for in_channels in self.in_channels[:-1]: # skip the top layer UpperCamelCase : List[Any] = UperNetConvModule(__SCREAMING_SNAKE_CASE , self.channels , kernel_size=1 ) UpperCamelCase : int = UperNetConvModule(self.channels , self.channels , kernel_size=3 , padding=1 ) self.lateral_convs.append(__SCREAMING_SNAKE_CASE ) self.fpn_convs.append(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[Any] = UperNetConvModule( len(self.in_channels ) * self.channels , self.channels , kernel_size=3 , padding=1 , ) def _lowercase ( self ): """simple docstring""" self.apply(self._init_weights ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if isinstance(__SCREAMING_SNAKE_CASE , nn.Convad ): module.weight.data.normal_(mean=0.0 , std=self.config.initializer_range ) if module.bias is not None: module.bias.data.zero_() def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Any = inputs[-1] UpperCamelCase : int = [x] psp_outs.extend(self.psp_modules(__SCREAMING_SNAKE_CASE ) ) UpperCamelCase : Any = torch.cat(__SCREAMING_SNAKE_CASE , dim=1 ) UpperCamelCase : Union[str, Any] = self.bottleneck(__SCREAMING_SNAKE_CASE ) return output def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : str = [lateral_conv(encoder_hidden_states[i] ) for i, lateral_conv in enumerate(self.lateral_convs )] laterals.append(self.psp_forward(__SCREAMING_SNAKE_CASE ) ) # build top-down path UpperCamelCase : int = len(__SCREAMING_SNAKE_CASE ) for i in range(used_backbone_levels - 1 , 0 , -1 ): UpperCamelCase : Optional[int] = laterals[i - 1].shape[2:] UpperCamelCase : Optional[Any] = laterals[i - 1] + nn.functional.interpolate( laterals[i] , size=__SCREAMING_SNAKE_CASE , mode='''bilinear''' , align_corners=self.align_corners ) # build outputs UpperCamelCase : str = [self.fpn_convs[i](laterals[i] ) for i in range(used_backbone_levels - 1 )] # append psp feature fpn_outs.append(laterals[-1] ) for i in range(used_backbone_levels - 1 , 0 , -1 ): UpperCamelCase : int = nn.functional.interpolate( fpn_outs[i] , size=fpn_outs[0].shape[2:] , mode='''bilinear''' , align_corners=self.align_corners ) UpperCamelCase : str = torch.cat(__SCREAMING_SNAKE_CASE , dim=1 ) UpperCamelCase : Tuple = self.fpn_bottleneck(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = self.classifier(__SCREAMING_SNAKE_CASE ) return output class UpperCAmelCase_ ( nn.Module): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = 2 , __SCREAMING_SNAKE_CASE = 3 , __SCREAMING_SNAKE_CASE = 1 ): """simple docstring""" super().__init__() UpperCamelCase : Dict = config UpperCamelCase : Optional[Any] = config.auxiliary_in_channels UpperCamelCase : Union[str, Any] = config.auxiliary_channels UpperCamelCase : Union[str, Any] = config.auxiliary_num_convs UpperCamelCase : Optional[Any] = config.auxiliary_concat_input UpperCamelCase : List[str] = in_index UpperCamelCase : Any = (kernel_size // 2) * dilation UpperCamelCase : Optional[Any] = [] convs.append( UperNetConvModule( self.in_channels , self.channels , kernel_size=__SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE , dilation=__SCREAMING_SNAKE_CASE ) ) for i in range(self.num_convs - 1 ): convs.append( UperNetConvModule( self.channels , self.channels , kernel_size=__SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE , dilation=__SCREAMING_SNAKE_CASE ) ) if self.num_convs == 0: UpperCamelCase : str = nn.Identity() else: UpperCamelCase : Dict = nn.Sequential(*__SCREAMING_SNAKE_CASE ) if self.concat_input: UpperCamelCase : Union[str, Any] = UperNetConvModule( self.in_channels + self.channels , self.channels , kernel_size=__SCREAMING_SNAKE_CASE , padding=kernel_size // 2 ) UpperCamelCase : Optional[Any] = nn.Convad(self.channels , config.num_labels , kernel_size=1 ) def _lowercase ( self ): """simple docstring""" self.apply(self._init_weights ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if isinstance(__SCREAMING_SNAKE_CASE , nn.Convad ): module.weight.data.normal_(mean=0.0 , std=self.config.initializer_range ) if module.bias is not None: module.bias.data.zero_() def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Any = encoder_hidden_states[self.in_index] UpperCamelCase : str = self.convs(__SCREAMING_SNAKE_CASE ) if self.concat_input: UpperCamelCase : int = self.conv_cat(torch.cat([hidden_states, output] , dim=1 ) ) UpperCamelCase : Union[str, Any] = self.classifier(__SCREAMING_SNAKE_CASE ) return output class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : Optional[Any] = UperNetConfig __UpperCamelCase : Optional[int] = "pixel_values" __UpperCamelCase : Dict = True def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): module.backbone.init_weights() module.decode_head.init_weights() module.auxiliary_head.init_weights() def _lowercase ( self ): """simple docstring""" self.backbone.init_weights() self.decode_head.init_weights() self.auxiliary_head.init_weights() def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=False ): """simple docstring""" if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): UpperCamelCase : str = value __UpperCAmelCase : List[Any] = r"\n Parameters:\n This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) sub-class. Use\n it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and\n behavior.\n config ([`UperNetConfig`]): 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" __UpperCAmelCase : Union[str, Any] = r"\n Args:\n pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n Pixel values. Padding will be ignored by default should you provide it. Pixel values can be obtained using\n [`AutoImageProcessor`]. See [`SegformerImageProcessor.__call__`] for details.\n output_attentions (`bool`, *optional*):\n Whether or not to return the attentions tensors of all attention layers in case the backbone has them. See\n `attentions` under returned tensors for more detail.\n output_hidden_states (`bool`, *optional*):\n Whether or not to return the hidden states of all layers of the backbone. See `hidden_states` under\n returned tensors for 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( "UperNet framework leveraging any vision backbone e.g. for ADE20k, CityScapes.", _a, ) class UpperCAmelCase_ ( _a): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" super().__init__(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Dict = AutoBackbone.from_config(config.backbone_config ) # Semantic segmentation head(s) UpperCamelCase : int = UperNetHead(__SCREAMING_SNAKE_CASE , in_channels=self.backbone.channels ) UpperCamelCase : int = UperNetFCNHead(__SCREAMING_SNAKE_CASE ) if config.use_auxiliary_head else None # Initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(UPERNET_INPUTS_DOCSTRING.format('''batch_size, sequence_length''' ) ) @replace_return_docstrings(output_type=__SCREAMING_SNAKE_CASE , config_class=_CONFIG_FOR_DOC ) def _lowercase ( self , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , ): """simple docstring""" UpperCamelCase : List[str] = return_dict if return_dict is not None else self.config.use_return_dict UpperCamelCase : str = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) UpperCamelCase : Tuple = output_attentions if output_attentions is not None else self.config.output_attentions UpperCamelCase : Tuple = self.backbone.forward_with_filtered_kwargs( __SCREAMING_SNAKE_CASE , output_hidden_states=__SCREAMING_SNAKE_CASE , output_attentions=__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = outputs.feature_maps UpperCamelCase : Union[str, Any] = self.decode_head(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = nn.functional.interpolate(__SCREAMING_SNAKE_CASE , size=pixel_values.shape[2:] , mode='''bilinear''' , align_corners=__SCREAMING_SNAKE_CASE ) UpperCamelCase : int = None if self.auxiliary_head is not None: UpperCamelCase : int = self.auxiliary_head(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Tuple = nn.functional.interpolate( __SCREAMING_SNAKE_CASE , size=pixel_values.shape[2:] , mode='''bilinear''' , align_corners=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = None if labels is not None: if self.config.num_labels == 1: raise ValueError('''The number of labels should be greater than one''' ) else: # compute weighted loss UpperCamelCase : Optional[int] = CrossEntropyLoss(ignore_index=self.config.loss_ignore_index ) UpperCamelCase : Tuple = loss_fct(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[Any] = loss_fct(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = main_loss + self.config.auxiliary_loss_weight * auxiliary_loss if not return_dict: if output_hidden_states: UpperCamelCase : Optional[Any] = (logits,) + outputs[1:] else: UpperCamelCase : int = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return SemanticSegmenterOutput( loss=__SCREAMING_SNAKE_CASE , logits=__SCREAMING_SNAKE_CASE , hidden_states=outputs.hidden_states , attentions=outputs.attentions , )
643
1
import logging import os import sys from dataclasses import dataclass, field from itertools import chain from typing import Optional, Union import datasets import numpy as np import torch from datasets import load_dataset import transformers from transformers import ( AutoConfig, AutoModelForMultipleChoice, AutoTokenizer, HfArgumentParser, Trainer, TrainingArguments, default_data_collator, set_seed, ) from transformers.tokenization_utils_base import PreTrainedTokenizerBase from transformers.trainer_utils import get_last_checkpoint from transformers.utils import PaddingStrategy, check_min_version, send_example_telemetry # Will error if the minimal version of Transformers is not installed. Remove at your own risks. check_min_version("4.31.0") __UpperCAmelCase : Tuple = logging.getLogger(__name__) @dataclass class UpperCAmelCase_ : '''simple docstring''' __UpperCamelCase : str = field( metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models"}) __UpperCamelCase : Optional[str] = field( default=_a, metadata={"help": "Pretrained config name or path if not the same as model_name"}) __UpperCamelCase : Optional[str] = field( default=_a, metadata={"help": "Pretrained tokenizer name or path if not the same as model_name"}) __UpperCamelCase : Optional[str] = field( default=_a, metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co"}, ) __UpperCamelCase : bool = field( default=_a, metadata={"help": "Whether to use one of the fast tokenizer (backed by the tokenizers library) or not."}, ) __UpperCamelCase : str = field( default="main", metadata={"help": "The specific model version to use (can be a branch name, tag name or commit id)."}, ) __UpperCamelCase : bool = field( default=_a, metadata={ "help": ( "Will use the token generated when running `huggingface-cli login` (necessary to use this script " "with private models)." ) }, ) @dataclass class UpperCAmelCase_ : '''simple docstring''' __UpperCamelCase : Optional[str] = field(default=_a, metadata={"help": "The input training data file (a text file)."}) __UpperCamelCase : Optional[str] = field( default=_a, metadata={"help": "An optional input evaluation data file to evaluate the perplexity on (a text file)."}, ) __UpperCamelCase : bool = field( default=_a, metadata={"help": "Overwrite the cached training and evaluation sets"}) __UpperCamelCase : Optional[int] = field( default=_a, metadata={"help": "The number of processes to use for the preprocessing."}, ) __UpperCamelCase : Optional[int] = field( default=_a, metadata={ "help": ( "The maximum total input sequence length after tokenization. If passed, sequences longer " "than this will be truncated, sequences shorter will be padded." ) }, ) __UpperCamelCase : bool = field( default=_a, metadata={ "help": ( "Whether to pad all samples to the maximum sentence length. " "If False, will pad the samples dynamically when batching to the maximum length in the batch. More " "efficient on GPU but very bad for TPU." ) }, ) __UpperCamelCase : Optional[int] = field( default=_a, metadata={ "help": ( "For debugging purposes or quicker training, truncate the number of training examples to this " "value if set." ) }, ) __UpperCamelCase : Optional[int] = field( default=_a, metadata={ "help": ( "For debugging purposes or quicker training, truncate the number of evaluation examples to this " "value if set." ) }, ) def _lowercase ( self ): """simple docstring""" if self.train_file is not None: UpperCamelCase : Union[str, Any] = self.train_file.split('''.''' )[-1] assert extension in ["csv", "json"], "`train_file` should be a csv or a json file." if self.validation_file is not None: UpperCamelCase : List[Any] = self.validation_file.split('''.''' )[-1] assert extension in ["csv", "json"], "`validation_file` should be a csv or a json file." @dataclass class UpperCAmelCase_ : '''simple docstring''' __UpperCamelCase : PreTrainedTokenizerBase __UpperCamelCase : Union[bool, str, PaddingStrategy] = True __UpperCamelCase : Optional[int] = None __UpperCamelCase : Optional[int] = None def __call__( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : List[Any] = '''label''' if '''label''' in features[0].keys() else '''labels''' UpperCamelCase : List[Any] = [feature.pop(__SCREAMING_SNAKE_CASE ) for feature in features] UpperCamelCase : Dict = len(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Any = len(features[0]['''input_ids'''] ) UpperCamelCase : Union[str, Any] = [ [{k: v[i] for k, v in feature.items()} for i in range(__SCREAMING_SNAKE_CASE )] for feature in features ] UpperCamelCase : Any = list(chain(*__SCREAMING_SNAKE_CASE ) ) UpperCamelCase : str = self.tokenizer.pad( __SCREAMING_SNAKE_CASE , padding=self.padding , max_length=self.max_length , pad_to_multiple_of=self.pad_to_multiple_of , return_tensors='''pt''' , ) # Un-flatten UpperCamelCase : Dict = {k: v.view(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , -1 ) for k, v in batch.items()} # Add back labels UpperCamelCase : Any = torch.tensor(__SCREAMING_SNAKE_CASE , dtype=torch.intaa ) return batch def a ( ): """simple docstring""" UpperCamelCase : Union[str, Any] = 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 : Optional[int] = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: UpperCamelCase , UpperCamelCase , UpperCamelCase : int = 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_swag''' , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # 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 : List[str] = training_args.get_process_log_level() logger.setLevel(SCREAMING_SNAKE_CASE_ ) datasets.utils.logging.set_verbosity(SCREAMING_SNAKE_CASE_ ) transformers.utils.logging.set_verbosity(SCREAMING_SNAKE_CASE_ ) 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}""" ) # Detecting last checkpoint. UpperCamelCase : Tuple = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: UpperCamelCase : Union[str, 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 overcome.''' ) 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.''' ) # Set seed before initializing model. set_seed(training_args.seed ) # Get the datasets: you can either provide your own CSV/JSON/TXT training and evaluation files (see below) # or just provide the name of one of the public datasets available on the hub at https://huggingface.co/datasets/ # (the dataset will be downloaded automatically from the datasets Hub). # For CSV/JSON files, this script will use the column called 'text' or the first column if no column called # 'text' is found. You can easily tweak this behavior (see below). # In distributed training, the load_dataset function guarantee that only one local process can concurrently # download the dataset. if data_args.train_file is not None or data_args.validation_file is not None: UpperCamelCase : Tuple = {} if data_args.train_file is not None: UpperCamelCase : List[Any] = data_args.train_file if data_args.validation_file is not None: UpperCamelCase : Dict = data_args.validation_file UpperCamelCase : List[Any] = data_args.train_file.split('''.''' )[-1] UpperCamelCase : List[str] = load_dataset( SCREAMING_SNAKE_CASE_ , data_files=SCREAMING_SNAKE_CASE_ , cache_dir=model_args.cache_dir , use_auth_token=True if model_args.use_auth_token else None , ) else: # Downloading and loading the swag dataset from the hub. UpperCamelCase : Any = load_dataset( '''swag''' , '''regular''' , cache_dir=model_args.cache_dir , use_auth_token=True if model_args.use_auth_token else None , ) # See more about loading any type of standard or custom dataset (from files, python dict, pandas DataFrame, etc) at # https://huggingface.co/docs/datasets/loading_datasets.html. # Load pretrained model and tokenizer # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. UpperCamelCase : int = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) UpperCamelCase : Tuple = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , use_fast=model_args.use_fast_tokenizer , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) UpperCamelCase : Optional[Any] = AutoModelForMultipleChoice.from_pretrained( model_args.model_name_or_path , from_tf=bool('''.ckpt''' in model_args.model_name_or_path ) , config=SCREAMING_SNAKE_CASE_ , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) # When using your own dataset or a different dataset from swag, you will probably need to change this. UpperCamelCase : List[str] = [F"""ending{i}""" for i in range(4 )] UpperCamelCase : int = '''sent1''' UpperCamelCase : Optional[int] = '''sent2''' if data_args.max_seq_length is None: UpperCamelCase : Tuple = tokenizer.model_max_length if max_seq_length > 1_0_2_4: logger.warning( '''The chosen tokenizer supports a `model_max_length` that is longer than the default `block_size` value''' ''' of 1024. If you would like to use a longer `block_size` up to `tokenizer.model_max_length` you can''' ''' override this default with `--block_size xxx`.''' ) UpperCamelCase : List[str] = 1_0_2_4 else: if data_args.max_seq_length > tokenizer.model_max_length: logger.warning( F"""The max_seq_length passed ({data_args.max_seq_length}) is larger than the maximum length for the""" F"""model ({tokenizer.model_max_length}). Using max_seq_length={tokenizer.model_max_length}.""" ) UpperCamelCase : Any = min(data_args.max_seq_length , tokenizer.model_max_length ) # Preprocessing the datasets. def preprocess_function(SCREAMING_SNAKE_CASE_ : Optional[Any] ): UpperCamelCase : int = [[context] * 4 for context in examples[context_name]] UpperCamelCase : Union[str, Any] = examples[question_header_name] UpperCamelCase : List[str] = [ [F"""{header} {examples[end][i]}""" for end in ending_names] for i, header in enumerate(SCREAMING_SNAKE_CASE_ ) ] # Flatten out UpperCamelCase : Optional[Any] = list(chain(*SCREAMING_SNAKE_CASE_ ) ) UpperCamelCase : str = list(chain(*SCREAMING_SNAKE_CASE_ ) ) # Tokenize UpperCamelCase : Tuple = tokenizer( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , truncation=SCREAMING_SNAKE_CASE_ , max_length=SCREAMING_SNAKE_CASE_ , padding='''max_length''' if data_args.pad_to_max_length else False , ) # Un-flatten return {k: [v[i : i + 4] for i in range(0 , len(SCREAMING_SNAKE_CASE_ ) , 4 )] for k, v in tokenized_examples.items()} if training_args.do_train: if "train" not in raw_datasets: raise ValueError('''--do_train requires a train dataset''' ) UpperCamelCase : Tuple = raw_datasets['''train'''] if data_args.max_train_samples is not None: UpperCamelCase : Dict = min(len(SCREAMING_SNAKE_CASE_ ) , data_args.max_train_samples ) UpperCamelCase : Any = train_dataset.select(range(SCREAMING_SNAKE_CASE_ ) ) with training_args.main_process_first(desc='''train dataset map pre-processing''' ): UpperCamelCase : List[Any] = train_dataset.map( SCREAMING_SNAKE_CASE_ , batched=SCREAMING_SNAKE_CASE_ , num_proc=data_args.preprocessing_num_workers , load_from_cache_file=not data_args.overwrite_cache , ) if training_args.do_eval: if "validation" not in raw_datasets: raise ValueError('''--do_eval requires a validation dataset''' ) UpperCamelCase : int = raw_datasets['''validation'''] if data_args.max_eval_samples is not None: UpperCamelCase : int = min(len(SCREAMING_SNAKE_CASE_ ) , data_args.max_eval_samples ) UpperCamelCase : Union[str, Any] = eval_dataset.select(range(SCREAMING_SNAKE_CASE_ ) ) with training_args.main_process_first(desc='''validation dataset map pre-processing''' ): UpperCamelCase : Tuple = eval_dataset.map( SCREAMING_SNAKE_CASE_ , batched=SCREAMING_SNAKE_CASE_ , num_proc=data_args.preprocessing_num_workers , load_from_cache_file=not data_args.overwrite_cache , ) # Data collator UpperCamelCase : Tuple = ( default_data_collator if data_args.pad_to_max_length else DataCollatorForMultipleChoice(tokenizer=SCREAMING_SNAKE_CASE_ , pad_to_multiple_of=8 if training_args.fpaa else None ) ) # Metric def compute_metrics(SCREAMING_SNAKE_CASE_ : Optional[Any] ): UpperCamelCase , UpperCamelCase : List[str] = eval_predictions UpperCamelCase : Any = np.argmax(SCREAMING_SNAKE_CASE_ , axis=1 ) return {"accuracy": (preds == label_ids).astype(np.floataa ).mean().item()} # Initialize our Trainer UpperCamelCase : Optional[Any] = Trainer( model=SCREAMING_SNAKE_CASE_ , args=SCREAMING_SNAKE_CASE_ , train_dataset=train_dataset if training_args.do_train else None , eval_dataset=eval_dataset if training_args.do_eval else None , tokenizer=SCREAMING_SNAKE_CASE_ , data_collator=SCREAMING_SNAKE_CASE_ , compute_metrics=SCREAMING_SNAKE_CASE_ , ) # Training if training_args.do_train: UpperCamelCase : List[str] = None if training_args.resume_from_checkpoint is not None: UpperCamelCase : Dict = training_args.resume_from_checkpoint elif last_checkpoint is not None: UpperCamelCase : List[Any] = last_checkpoint UpperCamelCase : Any = trainer.train(resume_from_checkpoint=SCREAMING_SNAKE_CASE_ ) trainer.save_model() # Saves the tokenizer too for easy upload UpperCamelCase : Optional[int] = train_result.metrics UpperCamelCase : str = ( data_args.max_train_samples if data_args.max_train_samples is not None else len(SCREAMING_SNAKE_CASE_ ) ) UpperCamelCase : Optional[int] = min(SCREAMING_SNAKE_CASE_ , len(SCREAMING_SNAKE_CASE_ ) ) trainer.log_metrics('''train''' , SCREAMING_SNAKE_CASE_ ) trainer.save_metrics('''train''' , SCREAMING_SNAKE_CASE_ ) trainer.save_state() # Evaluation if training_args.do_eval: logger.info('''*** Evaluate ***''' ) UpperCamelCase : str = trainer.evaluate() UpperCamelCase : Dict = data_args.max_eval_samples if data_args.max_eval_samples is not None else len(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : List[Any] = min(SCREAMING_SNAKE_CASE_ , len(SCREAMING_SNAKE_CASE_ ) ) trainer.log_metrics('''eval''' , SCREAMING_SNAKE_CASE_ ) trainer.save_metrics('''eval''' , SCREAMING_SNAKE_CASE_ ) UpperCamelCase : str = { '''finetuned_from''': model_args.model_name_or_path, '''tasks''': '''multiple-choice''', '''dataset_tags''': '''swag''', '''dataset_args''': '''regular''', '''dataset''': '''SWAG''', '''language''': '''en''', } if training_args.push_to_hub: trainer.push_to_hub(**SCREAMING_SNAKE_CASE_ ) else: trainer.create_model_card(**SCREAMING_SNAKE_CASE_ ) def a ( SCREAMING_SNAKE_CASE_ : Optional[int] ): """simple docstring""" main() if __name__ == "__main__": main()
643
import json import os import tempfile import transformers import datasets from utils import generate_example_dataset, get_duration __UpperCAmelCase : Optional[int] = 500000 __UpperCAmelCase , __UpperCAmelCase : Any = os.path.split(__file__) __UpperCAmelCase : int = os.path.join(RESULTS_BASEPATH, "results", RESULTS_FILENAME.replace(".py", ".json")) @get_duration def a ( SCREAMING_SNAKE_CASE_ : datasets.Dataset , **SCREAMING_SNAKE_CASE_ : Union[str, Any] ): """simple docstring""" UpperCamelCase : Tuple = dataset.map(**SCREAMING_SNAKE_CASE_ ) @get_duration def a ( SCREAMING_SNAKE_CASE_ : datasets.Dataset , **SCREAMING_SNAKE_CASE_ : Any ): """simple docstring""" UpperCamelCase : int = dataset.filter(**SCREAMING_SNAKE_CASE_ ) def a ( ): """simple docstring""" UpperCamelCase : Optional[int] = {'''num examples''': SPEED_TEST_N_EXAMPLES} with tempfile.TemporaryDirectory() as tmp_dir: UpperCamelCase : Dict = datasets.Features({'''text''': datasets.Value('''string''' ), '''numbers''': datasets.Value('''float32''' )} ) UpperCamelCase : List[str] = generate_example_dataset( os.path.join(SCREAMING_SNAKE_CASE_ , '''dataset.arrow''' ) , SCREAMING_SNAKE_CASE_ , num_examples=SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Union[str, Any] = transformers.AutoTokenizer.from_pretrained('''bert-base-cased''' , use_fast=SCREAMING_SNAKE_CASE_ ) def tokenize(SCREAMING_SNAKE_CASE_ : Dict ): return tokenizer(examples['''text'''] ) UpperCamelCase : List[Any] = map(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : List[str] = map(SCREAMING_SNAKE_CASE_ , batched=SCREAMING_SNAKE_CASE_ ) UpperCamelCase : List[Any] = map(SCREAMING_SNAKE_CASE_ , function=lambda SCREAMING_SNAKE_CASE_ : None , batched=SCREAMING_SNAKE_CASE_ ) with dataset.formatted_as(type='''numpy''' ): UpperCamelCase : Tuple = map(SCREAMING_SNAKE_CASE_ , function=lambda SCREAMING_SNAKE_CASE_ : None , batched=SCREAMING_SNAKE_CASE_ ) with dataset.formatted_as(type='''pandas''' ): UpperCamelCase : int = map(SCREAMING_SNAKE_CASE_ , function=lambda SCREAMING_SNAKE_CASE_ : None , batched=SCREAMING_SNAKE_CASE_ ) with dataset.formatted_as(type='''torch''' , columns='''numbers''' ): UpperCamelCase : Dict = map(SCREAMING_SNAKE_CASE_ , function=lambda SCREAMING_SNAKE_CASE_ : None , batched=SCREAMING_SNAKE_CASE_ ) with dataset.formatted_as(type='''tensorflow''' , columns='''numbers''' ): UpperCamelCase : Tuple = map(SCREAMING_SNAKE_CASE_ , function=lambda SCREAMING_SNAKE_CASE_ : None , batched=SCREAMING_SNAKE_CASE_ ) UpperCamelCase : List[str] = map(SCREAMING_SNAKE_CASE_ , function=SCREAMING_SNAKE_CASE_ , batched=SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Dict = filter(SCREAMING_SNAKE_CASE_ ) # Activate later when tokenizer support batched inputs # with dataset.formatted_as(type='numpy'): # times[func.__name__ + " fast-tokenizer batched numpy"] = func(dataset, function=tokenize, batched=True) with open(SCREAMING_SNAKE_CASE_ , '''wb''' ) as f: f.write(json.dumps(SCREAMING_SNAKE_CASE_ ).encode('''utf-8''' ) ) if __name__ == "__main__": # useful to run the profiler benchmark_map_filter()
643
1
import copy from ...configuration_utils import PretrainedConfig from ...utils import logging from ..auto import CONFIG_MAPPING __UpperCAmelCase : Union[str, Any] = logging.get_logger(__name__) __UpperCAmelCase : Tuple = { "SenseTime/deformable-detr": "https://huggingface.co/sensetime/deformable-detr/resolve/main/config.json", # See all Deformable DETR models at https://huggingface.co/models?filter=deformable-detr } class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : List[Any] = "deformable_detr" __UpperCamelCase : str = { "hidden_size": "d_model", "num_attention_heads": "encoder_attention_heads", } def __init__( self , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=300 , __SCREAMING_SNAKE_CASE=1_024 , __SCREAMING_SNAKE_CASE=6 , __SCREAMING_SNAKE_CASE=1_024 , __SCREAMING_SNAKE_CASE=8 , __SCREAMING_SNAKE_CASE=6 , __SCREAMING_SNAKE_CASE=1_024 , __SCREAMING_SNAKE_CASE=8 , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE="relu" , __SCREAMING_SNAKE_CASE=256 , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=0.02 , __SCREAMING_SNAKE_CASE=1.0 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE="sine" , __SCREAMING_SNAKE_CASE="resnet50" , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=4 , __SCREAMING_SNAKE_CASE=4 , __SCREAMING_SNAKE_CASE=4 , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=300 , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=1 , __SCREAMING_SNAKE_CASE=5 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=1 , __SCREAMING_SNAKE_CASE=1 , __SCREAMING_SNAKE_CASE=5 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=0.25 , __SCREAMING_SNAKE_CASE=False , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" if backbone_config is not None and use_timm_backbone: raise ValueError('''You can\'t specify both `backbone_config` and `use_timm_backbone`.''' ) if not use_timm_backbone: if backbone_config is None: logger.info('''`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.''' ) UpperCamelCase : List[Any] = CONFIG_MAPPING['''resnet'''](out_features=['''stage4'''] ) elif isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): UpperCamelCase : Any = backbone_config.get('''model_type''' ) UpperCamelCase : List[str] = CONFIG_MAPPING[backbone_model_type] UpperCamelCase : str = config_class.from_dict(__SCREAMING_SNAKE_CASE ) UpperCamelCase : int = use_timm_backbone UpperCamelCase : Dict = backbone_config UpperCamelCase : str = num_channels UpperCamelCase : List[str] = num_queries UpperCamelCase : int = max_position_embeddings UpperCamelCase : str = d_model UpperCamelCase : List[Any] = encoder_ffn_dim UpperCamelCase : Tuple = encoder_layers UpperCamelCase : List[str] = encoder_attention_heads UpperCamelCase : List[str] = decoder_ffn_dim UpperCamelCase : Optional[int] = decoder_layers UpperCamelCase : int = decoder_attention_heads UpperCamelCase : Tuple = dropout UpperCamelCase : str = attention_dropout UpperCamelCase : List[Any] = activation_dropout UpperCamelCase : List[Any] = activation_function UpperCamelCase : Tuple = init_std UpperCamelCase : Optional[Any] = init_xavier_std UpperCamelCase : Any = encoder_layerdrop UpperCamelCase : List[Any] = auxiliary_loss UpperCamelCase : Union[str, Any] = position_embedding_type UpperCamelCase : Union[str, Any] = backbone UpperCamelCase : List[str] = use_pretrained_backbone UpperCamelCase : int = dilation # deformable attributes UpperCamelCase : str = num_feature_levels UpperCamelCase : Union[str, Any] = encoder_n_points UpperCamelCase : Tuple = decoder_n_points UpperCamelCase : Optional[Any] = two_stage UpperCamelCase : Union[str, Any] = two_stage_num_proposals UpperCamelCase : Tuple = with_box_refine if two_stage is True and with_box_refine is False: raise ValueError('''If two_stage is True, with_box_refine must be True.''' ) # Hungarian matcher UpperCamelCase : Dict = class_cost UpperCamelCase : List[str] = bbox_cost UpperCamelCase : Dict = giou_cost # Loss coefficients UpperCamelCase : int = mask_loss_coefficient UpperCamelCase : Tuple = dice_loss_coefficient UpperCamelCase : List[Any] = bbox_loss_coefficient UpperCamelCase : List[str] = giou_loss_coefficient UpperCamelCase : Tuple = eos_coefficient UpperCamelCase : Optional[int] = focal_alpha UpperCamelCase : str = disable_custom_kernels super().__init__(is_encoder_decoder=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) @property def _lowercase ( self ): """simple docstring""" return self.encoder_attention_heads @property def _lowercase ( self ): """simple docstring""" return self.d_model def _lowercase ( self ): """simple docstring""" UpperCamelCase : Dict = copy.deepcopy(self.__dict__ ) if self.backbone_config is not None: UpperCamelCase : str = self.backbone_config.to_dict() UpperCamelCase : List[str] = self.__class__.model_type return output
643
from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import ShapEPipeline else: from .camera import create_pan_cameras from .pipeline_shap_e import ShapEPipeline from .pipeline_shap_e_img2img import ShapEImgaImgPipeline from .renderer import ( BoundingBoxVolume, ImportanceRaySampler, MLPNeRFModelOutput, MLPNeRSTFModel, ShapEParamsProjModel, ShapERenderer, StratifiedRaySampler, VoidNeRFModel, )
643
1
from __future__ import annotations import unittest from transformers import DebertaVaConfig, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TFDebertaVaForMaskedLM, TFDebertaVaForQuestionAnswering, TFDebertaVaForSequenceClassification, TFDebertaVaForTokenClassification, TFDebertaVaModel, ) class UpperCAmelCase_ : '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=13 , __SCREAMING_SNAKE_CASE=7 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=99 , __SCREAMING_SNAKE_CASE=32 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=4 , __SCREAMING_SNAKE_CASE=37 , __SCREAMING_SNAKE_CASE="gelu" , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=512 , __SCREAMING_SNAKE_CASE=16 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=0.02 , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE="None" , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=4 , __SCREAMING_SNAKE_CASE=None , ): """simple docstring""" UpperCamelCase : Union[str, Any] = parent UpperCamelCase : List[str] = batch_size UpperCamelCase : Dict = seq_length UpperCamelCase : int = is_training UpperCamelCase : Tuple = use_input_mask UpperCamelCase : str = use_token_type_ids UpperCamelCase : int = use_labels UpperCamelCase : Optional[Any] = vocab_size UpperCamelCase : Optional[int] = hidden_size UpperCamelCase : int = num_hidden_layers UpperCamelCase : List[str] = num_attention_heads UpperCamelCase : Any = intermediate_size UpperCamelCase : List[str] = hidden_act UpperCamelCase : Dict = hidden_dropout_prob UpperCamelCase : int = attention_probs_dropout_prob UpperCamelCase : Any = max_position_embeddings UpperCamelCase : Tuple = type_vocab_size UpperCamelCase : Tuple = type_sequence_label_size UpperCamelCase : Optional[int] = initializer_range UpperCamelCase : Any = num_labels UpperCamelCase : Optional[Any] = num_choices UpperCamelCase : Optional[int] = relative_attention UpperCamelCase : Optional[Any] = position_biased_input UpperCamelCase : Optional[int] = pos_att_type UpperCamelCase : Union[str, Any] = scope def _lowercase ( self ): """simple docstring""" UpperCamelCase : Tuple = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCamelCase : Any = None if self.use_input_mask: UpperCamelCase : Tuple = random_attention_mask([self.batch_size, self.seq_length] ) UpperCamelCase : List[str] = None if self.use_token_type_ids: UpperCamelCase : Dict = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) UpperCamelCase : List[str] = None UpperCamelCase : Tuple = None UpperCamelCase : str = None if self.use_labels: UpperCamelCase : str = ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCamelCase : int = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) UpperCamelCase : List[str] = DebertaVaConfig( 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 , relative_attention=self.relative_attention , position_biased_input=self.position_biased_input , initializer_range=self.initializer_range , return_dict=__SCREAMING_SNAKE_CASE , ) return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : List[str] = TFDebertaVaModel(config=__SCREAMING_SNAKE_CASE ) UpperCamelCase : str = {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids} UpperCamelCase : List[str] = [input_ids, input_mask] UpperCamelCase : List[str] = model(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Tuple = model(__SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Dict = TFDebertaVaForMaskedLM(config=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Tuple = { '''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids, } UpperCamelCase : str = model(__SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : str = self.num_labels UpperCamelCase : Any = TFDebertaVaForSequenceClassification(config=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = { '''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids, } UpperCamelCase : Optional[Any] = model(__SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Dict = self.num_labels UpperCamelCase : Optional[int] = TFDebertaVaForTokenClassification(config=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = { '''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids, } UpperCamelCase : Optional[int] = model(__SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : int = TFDebertaVaForQuestionAnswering(config=__SCREAMING_SNAKE_CASE ) UpperCamelCase : str = { '''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids, } UpperCamelCase : Optional[Any] = model(__SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[Any] = self.prepare_config_and_inputs() ( ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ) : Tuple = config_and_inputs UpperCamelCase : int = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_tf class UpperCAmelCase_ ( _a, _a, unittest.TestCase): '''simple docstring''' __UpperCamelCase : int = ( ( TFDebertaVaModel, TFDebertaVaForMaskedLM, TFDebertaVaForQuestionAnswering, TFDebertaVaForSequenceClassification, TFDebertaVaForTokenClassification, ) if is_tf_available() else () ) __UpperCamelCase : List[str] = ( { "feature-extraction": TFDebertaVaModel, "fill-mask": TFDebertaVaForMaskedLM, "question-answering": TFDebertaVaForQuestionAnswering, "text-classification": TFDebertaVaForSequenceClassification, "token-classification": TFDebertaVaForTokenClassification, "zero-shot": TFDebertaVaForSequenceClassification, } if is_tf_available() else {} ) __UpperCamelCase : int = False __UpperCamelCase : List[Any] = False def _lowercase ( self ): """simple docstring""" UpperCamelCase : Optional[Any] = TFDebertaVaModelTester(self ) UpperCamelCase : Union[str, Any] = ConfigTester(self , config_class=__SCREAMING_SNAKE_CASE , hidden_size=37 ) def _lowercase ( self ): """simple docstring""" self.config_tester.run_common_tests() def _lowercase ( self ): """simple docstring""" UpperCamelCase : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*__SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*__SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*__SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*__SCREAMING_SNAKE_CASE ) @slow def _lowercase ( self ): """simple docstring""" UpperCamelCase : Dict = TFDebertaVaModel.from_pretrained('''kamalkraj/deberta-v2-xlarge''' ) self.assertIsNotNone(__SCREAMING_SNAKE_CASE ) @require_tf class UpperCAmelCase_ ( unittest.TestCase): '''simple docstring''' @unittest.skip(reason='''Model not available yet''' ) def _lowercase ( self ): """simple docstring""" pass @slow def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[Any] = TFDebertaVaModel.from_pretrained('''kamalkraj/deberta-v2-xlarge''' ) UpperCamelCase : List[str] = tf.constant([[0, 31_414, 232, 328, 740, 1_140, 12_695, 69, 46_078, 1_588, 2]] ) UpperCamelCase : Union[str, Any] = tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) UpperCamelCase : Any = model(__SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE )[0] UpperCamelCase : List[Any] = tf.constant( [[[0.2_356, 0.1_948, 0.0_369], [-0.1_063, 0.3_586, -0.5_152], [-0.6_399, -0.0_259, -0.2_525]]] ) tf.debugging.assert_near(output[:, 1:4, 1:4] , __SCREAMING_SNAKE_CASE , atol=1e-4 )
643
import torch from transformers import AutoModel class UpperCAmelCase_ ( torch.nn.Module): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE="sayef/fsner-bert-base-uncased" ): """simple docstring""" super(__SCREAMING_SNAKE_CASE , self ).__init__() UpperCamelCase : List[str] = AutoModel.from_pretrained(__SCREAMING_SNAKE_CASE , return_dict=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = torch.nn.CosineSimilarity(3 , 1e-08 ) UpperCamelCase : List[Any] = torch.nn.Softmax(dim=1 ) def _lowercase ( self , **__SCREAMING_SNAKE_CASE ): """simple docstring""" return self.bert(**__SCREAMING_SNAKE_CASE ).last_hidden_state def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" return token_embeddings.sum(2 , keepdim=__SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=1 ): """simple docstring""" return self.softmax(T * self.cos(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Any = W_supports['''sizes'''].tolist() UpperCamelCase : Optional[int] = W_supports['''start_token_id'''].item() UpperCamelCase : Any = W_supports['''end_token_id'''].item() del W_supports["sizes"] del W_supports["start_token_id"] del W_supports["end_token_id"] UpperCamelCase : Union[str, Any] = self.BERT(**__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = self.BERT(**__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[Any] = None UpperCamelCase : Any = None UpperCamelCase : Optional[Any] = W_supports['''input_ids'''] == start_token_id UpperCamelCase : Any = W_supports['''input_ids'''] == end_token_id for i, size in enumerate(__SCREAMING_SNAKE_CASE ): if i == 0: UpperCamelCase : Optional[int] = 0 else: UpperCamelCase : Tuple = support_sizes[i - 1] UpperCamelCase : Tuple = S[s : s + size][start_token_masks[s : s + size]] UpperCamelCase : List[str] = S[s : s + size][end_token_masks[s : s + size]] UpperCamelCase : Dict = torch.matmul(q[i] , s_start.T ).sum(1 ).softmax(0 ) UpperCamelCase : Tuple = torch.matmul(q[i] , s_end.T ).sum(1 ).softmax(0 ) if p_starts is not None: UpperCamelCase : List[str] = torch.vstack((p_starts, p_start) ) UpperCamelCase : Union[str, Any] = torch.vstack((p_ends, p_end) ) else: UpperCamelCase : str = p_start UpperCamelCase : Optional[int] = p_end return p_starts, p_ends
643
1
def a ( SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int ): """simple docstring""" if a < 0 or b < 0: raise ValueError('''the value of both inputs must be positive''' ) UpperCamelCase : int = str(bin(SCREAMING_SNAKE_CASE_ ) )[2:] # remove the leading "0b" UpperCamelCase : List[str] = str(bin(SCREAMING_SNAKE_CASE_ ) )[2:] UpperCamelCase : Tuple = max(len(SCREAMING_SNAKE_CASE_ ) , len(SCREAMING_SNAKE_CASE_ ) ) return "0b" + "".join( str(int('''1''' in (char_a, char_b) ) ) for char_a, char_b in zip(a_binary.zfill(SCREAMING_SNAKE_CASE_ ) , b_binary.zfill(SCREAMING_SNAKE_CASE_ ) ) ) if __name__ == "__main__": import doctest doctest.testmod()
643
import json import os import unittest from transformers import DebertaTokenizer, DebertaTokenizerFast from transformers.models.deberta.tokenization_deberta import VOCAB_FILES_NAMES from transformers.testing_utils import slow from ...test_tokenization_common import TokenizerTesterMixin class UpperCAmelCase_ ( _a, unittest.TestCase): '''simple docstring''' __UpperCamelCase : str = DebertaTokenizer __UpperCamelCase : Optional[int] = True __UpperCamelCase : Optional[int] = DebertaTokenizerFast def _lowercase ( self ): """simple docstring""" super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt UpperCamelCase : Optional[int] = [ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''\u0120''', '''\u0120l''', '''\u0120n''', '''\u0120lo''', '''\u0120low''', '''er''', '''\u0120lowest''', '''\u0120newer''', '''\u0120wider''', '''[UNK]''', ] UpperCamelCase : Tuple = dict(zip(__SCREAMING_SNAKE_CASE , range(len(__SCREAMING_SNAKE_CASE ) ) ) ) UpperCamelCase : Any = ['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', ''''''] UpperCamelCase : List[Any] = {'''unk_token''': '''[UNK]'''} UpperCamelCase : Union[str, Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) UpperCamelCase : Optional[int] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''merges_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(__SCREAMING_SNAKE_CASE ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(__SCREAMING_SNAKE_CASE ) ) def _lowercase ( self , **__SCREAMING_SNAKE_CASE ): """simple docstring""" kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname , **__SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : int = '''lower newer''' UpperCamelCase : Union[str, Any] = '''lower newer''' return input_text, output_text def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[str] = self.get_tokenizer() UpperCamelCase : int = '''lower newer''' UpperCamelCase : Union[str, Any] = ['''l''', '''o''', '''w''', '''er''', '''\u0120''', '''n''', '''e''', '''w''', '''er'''] UpperCamelCase : Tuple = tokenizer.tokenize(__SCREAMING_SNAKE_CASE ) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) UpperCamelCase : List[Any] = tokens + [tokenizer.unk_token] UpperCamelCase : Optional[int] = [0, 1, 2, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(tokenizer.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : int = self.get_tokenizer() UpperCamelCase : Optional[Any] = tokenizer('''Hello''' , '''World''' ) UpperCamelCase : List[str] = [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1] self.assertListEqual(tokd['''token_type_ids'''] , __SCREAMING_SNAKE_CASE ) @slow def _lowercase ( self ): """simple docstring""" UpperCamelCase : Union[str, Any] = self.tokenizer_class.from_pretrained('''microsoft/deberta-base''' ) UpperCamelCase : Optional[Any] = tokenizer.encode('''sequence builders''' , add_special_tokens=__SCREAMING_SNAKE_CASE ) UpperCamelCase : int = tokenizer.encode('''multi-sequence build''' , add_special_tokens=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Dict = tokenizer.encode( '''sequence builders''' , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = tokenizer.encode( '''sequence builders''' , '''multi-sequence build''' , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = tokenizer.build_inputs_with_special_tokens(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = tokenizer.build_inputs_with_special_tokens(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) assert encoded_sentence == encoded_text_from_decode assert encoded_pair == encoded_pair_from_decode @slow def _lowercase ( self ): """simple docstring""" UpperCamelCase : Optional[Any] = [self.tokenizer_class] if self.test_rust_tokenizer: tokenizer_classes.append(self.rust_tokenizer_class ) for tokenizer_class in tokenizer_classes: UpperCamelCase : Optional[int] = tokenizer_class.from_pretrained('''microsoft/deberta-base''' ) UpperCamelCase : str = [ '''ALBERT: A Lite BERT for Self-supervised Learning of Language Representations''', '''ALBERT incorporates two parameter reduction techniques''', '''The first one is a factorized embedding parameterization. By decomposing the large vocabulary''' ''' embedding matrix into two small matrices, we separate the size of the hidden layers from the size of''' ''' vocabulary embedding.''', ] UpperCamelCase : Union[str, Any] = tokenizer(__SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = [tokenizer.decode(__SCREAMING_SNAKE_CASE , skip_special_tokens=__SCREAMING_SNAKE_CASE ) for seq in encoding['''input_ids''']] # fmt: off UpperCamelCase : int = { '''input_ids''': [ [1, 2_118, 11_126, 565, 35, 83, 25_191, 163, 18_854, 13, 12_156, 12, 16_101, 25_376, 13_807, 9, 22_205, 27_893, 1_635, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 2_118, 11_126, 565, 24_536, 80, 43_797, 4_878, 7_373, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 133, 78, 65, 16, 10, 3_724, 1_538, 33_183, 11_303, 43_797, 1_938, 4, 870, 24_165, 29_105, 5, 739, 32_644, 33_183, 11_303, 36_173, 88, 80, 650, 7_821, 45_940, 6, 52, 2_559, 5, 1_836, 9, 5, 7_397, 13_171, 31, 5, 1_836, 9, 32_644, 33_183, 11_303, 4, 2] ], '''token_type_ids''': [ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ], '''attention_mask''': [ [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] ] } # fmt: on UpperCamelCase : List[str] = [ '''ALBERT: A Lite BERT for Self-supervised Learning of Language Representations''', '''ALBERT incorporates two parameter reduction techniques''', '''The first one is a factorized embedding parameterization. By decomposing the large vocabulary''' ''' embedding matrix into two small matrices, we separate the size of the hidden layers from the size of''' ''' vocabulary embedding.''', ] self.assertDictEqual(encoding.data , __SCREAMING_SNAKE_CASE ) for expected, decoded in zip(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE )
643
1
import argparse import collections import numpy as np import torch from flax import traverse_util from tax import checkpoints from transformers import MTaConfig, UMTaEncoderModel, UMTaForConditionalGeneration from transformers.utils import logging logging.set_verbosity_info() def a ( SCREAMING_SNAKE_CASE_ : Optional[Any] , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : str ): """simple docstring""" return params[F"""{prefix}/{prefix}/relpos_bias/rel_embedding"""][:, i, :] def a ( SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : Optional[Any] , SCREAMING_SNAKE_CASE_ : Union[str, Any]="attention" ): """simple docstring""" UpperCamelCase : int = np.ascontiguousarray(params[F"""{prefix}/{prefix}/{layer_name}/key/kernel"""][:, i, :, :] ) UpperCamelCase : List[Any] = k_tmp.reshape(k_tmp.shape[0] , k_tmp.shape[1] * k_tmp.shape[2] ) UpperCamelCase : List[str] = np.ascontiguousarray(params[F"""{prefix}/{prefix}/{layer_name}/out/kernel"""][:, i, :, :] ) UpperCamelCase : Union[str, Any] = o_tmp.reshape(o_tmp.shape[0] * o_tmp.shape[1] , o_tmp.shape[2] ) UpperCamelCase : Dict = np.ascontiguousarray(params[F"""{prefix}/{prefix}/{layer_name}/query/kernel"""][:, i, :, :] ) UpperCamelCase : Optional[int] = q_tmp.reshape(q_tmp.shape[0] , q_tmp.shape[1] * q_tmp.shape[2] ) UpperCamelCase : Union[str, Any] = np.ascontiguousarray(params[F"""{prefix}/{prefix}/{layer_name}/value/kernel"""][:, i, :, :] ) UpperCamelCase : Optional[Any] = v_tmp.reshape(v_tmp.shape[0] , v_tmp.shape[1] * v_tmp.shape[2] ) return k, o, q, v def a ( SCREAMING_SNAKE_CASE_ : List[Any] , SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : Optional[int]=False ): """simple docstring""" if split_mlp_wi: UpperCamelCase : Tuple = params[F"""{prefix}/{prefix}/mlp/wi_0/kernel"""][:, i, :] UpperCamelCase : int = params[F"""{prefix}/{prefix}/mlp/wi_1/kernel"""][:, i, :] UpperCamelCase : Tuple = (wi_a, wi_a) else: UpperCamelCase : Optional[Any] = params[F"""{prefix}/{prefix}/mlp/wi/kernel"""][:, i, :] UpperCamelCase : Dict = params[F"""{prefix}/{prefix}/mlp/wo/kernel"""][:, i, :] return wi, wo def a ( SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : Tuple , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : Tuple ): """simple docstring""" return params[F"""{prefix}/{prefix}/{layer_name}/scale"""][:, i] def a ( SCREAMING_SNAKE_CASE_ : dict , *, SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : bool , SCREAMING_SNAKE_CASE_ : bool = False ): """simple docstring""" UpperCamelCase : Dict = traverse_util.flatten_dict(variables['''target'''] ) UpperCamelCase : Dict = {'''/'''.join(SCREAMING_SNAKE_CASE_ ): v for k, v in old.items()} # v1.1 models have a gated GeLU with wi_0 and wi_1 instead of wi UpperCamelCase : Any = '''encoder/encoder/mlp/wi_0/kernel''' in old print('''Split MLP:''' , SCREAMING_SNAKE_CASE_ ) UpperCamelCase : List[str] = collections.OrderedDict() # Shared embeddings. UpperCamelCase : List[Any] = old['''token_embedder/embedding'''] # Encoder. for i in range(SCREAMING_SNAKE_CASE_ ): # Block i, layer 0 (Self Attention). UpperCamelCase : str = tax_layer_norm_lookup(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , '''encoder''' , '''pre_attention_layer_norm''' ) UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase : Optional[int] = tax_attention_lookup(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , '''encoder''' , '''attention''' ) UpperCamelCase : str = layer_norm UpperCamelCase : int = k.T UpperCamelCase : Tuple = o.T UpperCamelCase : int = q.T UpperCamelCase : Dict = v.T # Block i, layer 1 (MLP). UpperCamelCase : str = tax_layer_norm_lookup(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , '''encoder''' , '''pre_mlp_layer_norm''' ) UpperCamelCase , UpperCamelCase : Dict = tax_mlp_lookup(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , '''encoder''' , SCREAMING_SNAKE_CASE_ ) UpperCamelCase : int = layer_norm if split_mlp_wi: UpperCamelCase : Dict = wi[0].T UpperCamelCase : List[str] = wi[1].T else: UpperCamelCase : Optional[int] = wi.T UpperCamelCase : List[Any] = wo.T if scalable_attention: # convert the rel_embedding of each layer UpperCamelCase : Optional[Any] = tax_relpos_bias_lookup( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , '''encoder''' ).T UpperCamelCase : List[Any] = old['''encoder/encoder_norm/scale'''] if not scalable_attention: UpperCamelCase : Any = tax_relpos_bias_lookup( SCREAMING_SNAKE_CASE_ , 0 , '''encoder''' ).T UpperCamelCase : Union[str, Any] = tax_relpos_bias_lookup( SCREAMING_SNAKE_CASE_ , 0 , '''decoder''' ).T if not is_encoder_only: # Decoder. for i in range(SCREAMING_SNAKE_CASE_ ): # Block i, layer 0 (Self Attention). UpperCamelCase : Union[str, Any] = tax_layer_norm_lookup(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , '''decoder''' , '''pre_self_attention_layer_norm''' ) UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase : List[str] = tax_attention_lookup(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , '''decoder''' , '''self_attention''' ) UpperCamelCase : Optional[Any] = layer_norm UpperCamelCase : List[Any] = k.T UpperCamelCase : Dict = o.T UpperCamelCase : Optional[Any] = q.T UpperCamelCase : Tuple = v.T # Block i, layer 1 (Cross Attention). UpperCamelCase : List[Any] = tax_layer_norm_lookup(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , '''decoder''' , '''pre_cross_attention_layer_norm''' ) UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase : str = tax_attention_lookup(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , '''decoder''' , '''encoder_decoder_attention''' ) UpperCamelCase : Optional[int] = layer_norm UpperCamelCase : List[str] = k.T UpperCamelCase : int = o.T UpperCamelCase : Optional[int] = q.T UpperCamelCase : List[str] = v.T # Block i, layer 2 (MLP). UpperCamelCase : Optional[int] = tax_layer_norm_lookup(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , '''decoder''' , '''pre_mlp_layer_norm''' ) UpperCamelCase , UpperCamelCase : List[Any] = tax_mlp_lookup(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , '''decoder''' , SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Dict = layer_norm if split_mlp_wi: UpperCamelCase : Tuple = wi[0].T UpperCamelCase : List[str] = wi[1].T else: UpperCamelCase : List[Any] = wi.T UpperCamelCase : Tuple = wo.T if scalable_attention: # convert the rel_embedding of each layer UpperCamelCase : Optional[Any] = tax_relpos_bias_lookup(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , '''decoder''' ).T UpperCamelCase : Any = old['''decoder/decoder_norm/scale'''] # LM Head (only in v1.1 checkpoints, in v1.0 embeddings are used instead) if "decoder/logits_dense/kernel" in old: UpperCamelCase : int = old['''decoder/logits_dense/kernel'''].T return new def a ( SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : bool ): """simple docstring""" UpperCamelCase : str = collections.OrderedDict([(k, torch.from_numpy(v.copy() )) for (k, v) in converted_params.items()] ) # Add what is missing. if "encoder.embed_tokens.weight" not in state_dict: UpperCamelCase : Optional[Any] = state_dict['''shared.weight'''] if not is_encoder_only: if "decoder.embed_tokens.weight" not in state_dict: UpperCamelCase : Any = state_dict['''shared.weight'''] if "lm_head.weight" not in state_dict: # For old 1.0 models. print('''Using shared word embeddings as lm_head.''' ) UpperCamelCase : List[str] = state_dict['''shared.weight'''] return state_dict def a ( SCREAMING_SNAKE_CASE_ : List[str] , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : Optional[Any] , SCREAMING_SNAKE_CASE_ : Optional[int] ): """simple docstring""" UpperCamelCase : str = checkpoints.load_tax_checkpoint(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Optional[int] = convert_tax_to_pytorch( SCREAMING_SNAKE_CASE_ , num_layers=config.num_layers , is_encoder_only=SCREAMING_SNAKE_CASE_ , scalable_attention=SCREAMING_SNAKE_CASE_ ) UpperCamelCase : List[str] = make_state_dict(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) model.load_state_dict(SCREAMING_SNAKE_CASE_ , strict=SCREAMING_SNAKE_CASE_ ) def a ( SCREAMING_SNAKE_CASE_ : List[str] , SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : bool = False , SCREAMING_SNAKE_CASE_ : bool = False , ): """simple docstring""" UpperCamelCase : Optional[Any] = MTaConfig.from_json_file(SCREAMING_SNAKE_CASE_ ) print(F"""Building PyTorch model from configuration: {config}""" ) # Non-v1.1 checkpoints could also use T5Model, but this works for all. # The v1.0 checkpoints will simply have an LM head that is the word embeddings. if is_encoder_only: UpperCamelCase : List[Any] = UMTaEncoderModel(SCREAMING_SNAKE_CASE_ ) else: UpperCamelCase : str = UMTaForConditionalGeneration(SCREAMING_SNAKE_CASE_ ) # Load weights from tf checkpoint load_tax_weights_in_ta(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # Save pytorch-model print(F"""Save PyTorch model to {pytorch_dump_path}""" ) model.save_pretrained(SCREAMING_SNAKE_CASE_ ) # Verify that we can load the checkpoint. model.from_pretrained(SCREAMING_SNAKE_CASE_ ) print('''Done''' ) if __name__ == "__main__": __UpperCAmelCase : Optional[int] = argparse.ArgumentParser(description="Converts a native T5X checkpoint into a PyTorch checkpoint.") # Required parameters parser.add_argument( "--t5x_checkpoint_path", default=None, type=str, required=True, help="Path to the T5X checkpoint." ) parser.add_argument( "--config_file", default=None, type=str, required=True, help="The config json file corresponding to the pre-trained T5 model.\nThis specifies the model architecture.", ) parser.add_argument( "--pytorch_dump_path", default=None, type=str, required=True, help="Path to the output PyTorch model." ) parser.add_argument( "--is_encoder_only", action="store_true", help="Check if the model is encoder-decoder model", default=False ) parser.add_argument( "--scalable_attention", action="store_true", help="Whether the model uses scaled attention (umt5 model)", default=False, ) __UpperCAmelCase : Optional[int] = parser.parse_args() convert_tax_checkpoint_to_pytorch( args.tax_checkpoint_path, args.config_file, args.pytorch_dump_path, args.is_encoder_only, args.scalable_attention, )
643
import re from pathlib import Path from unittest import TestCase import pytest @pytest.mark.integration class UpperCAmelCase_ ( _a): '''simple docstring''' def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" with open(__SCREAMING_SNAKE_CASE , encoding='''utf-8''' ) as input_file: UpperCamelCase : str = re.compile(R'''(?!.*\b(?:encoding|rb|w|wb|w+|wb+|ab|ab+)\b)(?<=\s)(open)\((.*)\)''' ) UpperCamelCase : Optional[int] = input_file.read() UpperCamelCase : Union[str, Any] = regexp.search(__SCREAMING_SNAKE_CASE ) return match def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" with open(__SCREAMING_SNAKE_CASE , encoding='''utf-8''' ) as input_file: UpperCamelCase : Optional[int] = re.compile(R'''#[^\r\n]*print\(|\"[^\r\n]*print\(|\"\"\".*?print\(.*?\"\"\"|(print\()''' , re.DOTALL ) UpperCamelCase : Tuple = input_file.read() # use `re.finditer` to handle the case where the ignored groups would be matched first by `re.search` UpperCamelCase : Dict = regexp.finditer(__SCREAMING_SNAKE_CASE ) UpperCamelCase : int = [match for match in matches if match is not None and match.group(1 ) is not None] return matches[0] if matches else None def _lowercase ( self ): """simple docstring""" UpperCamelCase : int = Path('''./datasets''' ) UpperCamelCase : Tuple = list(dataset_paths.absolute().glob('''**/*.py''' ) ) for dataset in dataset_files: if self._no_encoding_on_file_open(str(__SCREAMING_SNAKE_CASE ) ): raise AssertionError(f"""open(...) must use utf-8 encoding in {dataset}""" ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Optional[int] = Path('''./datasets''' ) UpperCamelCase : Tuple = list(dataset_paths.absolute().glob('''**/*.py''' ) ) for dataset in dataset_files: if self._no_print_statements(str(__SCREAMING_SNAKE_CASE ) ): raise AssertionError(f"""print statement found in {dataset}. Use datasets.logger/logging instead.""" )
643
1
import numpy as np def a ( SCREAMING_SNAKE_CASE_ : np.array ): """simple docstring""" return (2 / (1 + np.exp(-2 * vector ))) - 1 if __name__ == "__main__": import doctest doctest.testmod()
643
from __future__ import annotations import unittest from transformers import XGLMConfig, XGLMTokenizer, 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 tensorflow as tf from transformers.models.xglm.modeling_tf_xglm import ( TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFXGLMForCausalLM, TFXGLMModel, ) @require_tf class UpperCAmelCase_ : '''simple docstring''' __UpperCamelCase : Any = XGLMConfig __UpperCamelCase : Dict = {} __UpperCamelCase : List[str] = "gelu" def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=14 , __SCREAMING_SNAKE_CASE=7 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=99 , __SCREAMING_SNAKE_CASE=32 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=4 , __SCREAMING_SNAKE_CASE=37 , __SCREAMING_SNAKE_CASE="gelu" , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=512 , __SCREAMING_SNAKE_CASE=0.02 , ): """simple docstring""" UpperCamelCase : Any = parent UpperCamelCase : Optional[int] = batch_size UpperCamelCase : str = seq_length UpperCamelCase : List[str] = is_training UpperCamelCase : Tuple = use_input_mask UpperCamelCase : Union[str, Any] = use_labels UpperCamelCase : int = vocab_size UpperCamelCase : Optional[int] = d_model UpperCamelCase : Any = num_hidden_layers UpperCamelCase : List[str] = num_attention_heads UpperCamelCase : Optional[Any] = ffn_dim UpperCamelCase : Optional[int] = activation_function UpperCamelCase : List[str] = activation_dropout UpperCamelCase : Any = attention_dropout UpperCamelCase : str = max_position_embeddings UpperCamelCase : Union[str, Any] = initializer_range UpperCamelCase : int = None UpperCamelCase : Dict = 0 UpperCamelCase : int = 2 UpperCamelCase : Any = 1 def _lowercase ( self ): """simple docstring""" return XGLMConfig.from_pretrained('''facebook/xglm-564M''' ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Optional[int] = tf.clip_by_value( ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) , clip_value_min=0 , clip_value_max=3 ) UpperCamelCase : int = None if self.use_input_mask: UpperCamelCase : Any = random_attention_mask([self.batch_size, self.seq_length] ) UpperCamelCase : Tuple = self.get_config() UpperCamelCase : str = floats_tensor([self.num_hidden_layers, self.num_attention_heads] , 2 ) return ( config, input_ids, input_mask, head_mask, ) def _lowercase ( self ): """simple docstring""" return XGLMConfig( vocab_size=self.vocab_size , d_model=self.hidden_size , num_layers=self.num_hidden_layers , attention_heads=self.num_attention_heads , ffn_dim=self.ffn_dim , activation_function=self.activation_function , activation_dropout=self.activation_dropout , attention_dropout=self.attention_dropout , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , use_cache=__SCREAMING_SNAKE_CASE , bos_token_id=self.bos_token_id , eos_token_id=self.eos_token_id , pad_token_id=self.pad_token_id , return_dict=__SCREAMING_SNAKE_CASE , ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[str] = self.prepare_config_and_inputs() ( ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ) : Dict = config_and_inputs UpperCamelCase : List[str] = { '''input_ids''': input_ids, '''head_mask''': head_mask, } return config, inputs_dict @require_tf class UpperCAmelCase_ ( _a, _a, unittest.TestCase): '''simple docstring''' __UpperCamelCase : Optional[Any] = (TFXGLMModel, TFXGLMForCausalLM) if is_tf_available() else () __UpperCamelCase : Union[str, Any] = (TFXGLMForCausalLM,) if is_tf_available() else () __UpperCamelCase : Any = ( {"feature-extraction": TFXGLMModel, "text-generation": TFXGLMForCausalLM} if is_tf_available() else {} ) __UpperCamelCase : Optional[int] = False __UpperCamelCase : List[Any] = False __UpperCamelCase : List[Any] = False def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[Any] = TFXGLMModelTester(self ) UpperCamelCase : Any = ConfigTester(self , config_class=__SCREAMING_SNAKE_CASE , n_embd=37 ) def _lowercase ( self ): """simple docstring""" self.config_tester.run_common_tests() @slow def _lowercase ( self ): """simple docstring""" for model_name in TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCamelCase : List[Any] = TFXGLMModel.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsNotNone(__SCREAMING_SNAKE_CASE ) @unittest.skip(reason='''Currently, model embeddings are going to undergo a major refactor.''' ) def _lowercase ( self ): """simple docstring""" super().test_resize_token_embeddings() @require_tf class UpperCAmelCase_ ( unittest.TestCase): '''simple docstring''' @slow def _lowercase ( self , __SCREAMING_SNAKE_CASE=True ): """simple docstring""" UpperCamelCase : List[str] = TFXGLMForCausalLM.from_pretrained('''facebook/xglm-564M''' ) UpperCamelCase : List[Any] = tf.convert_to_tensor([[2, 268, 9_865]] , dtype=tf.intaa ) # The dog # </s> The dog is a very friendly dog. He is very affectionate and loves to play with other # fmt: off UpperCamelCase : str = [2, 268, 9_865, 67, 11, 1_988, 57_252, 9_865, 5, 984, 67, 1_988, 213_838, 1_658, 53, 70_446, 33, 6_657, 278, 1_581] # fmt: on UpperCamelCase : Union[str, Any] = model.generate(__SCREAMING_SNAKE_CASE , do_sample=__SCREAMING_SNAKE_CASE , num_beams=1 ) if verify_outputs: self.assertListEqual(output_ids[0].numpy().tolist() , __SCREAMING_SNAKE_CASE ) @slow def _lowercase ( self ): """simple docstring""" UpperCamelCase : str = XGLMTokenizer.from_pretrained('''facebook/xglm-564M''' ) UpperCamelCase : List[str] = TFXGLMForCausalLM.from_pretrained('''facebook/xglm-564M''' ) tf.random.set_seed(0 ) UpperCamelCase : Tuple = tokenizer('''Today is a nice day and''' , return_tensors='''tf''' ) UpperCamelCase : int = tokenized.input_ids # forces the generation to happen on CPU, to avoid GPU-related quirks (and assure same output regardless of the available devices) with tf.device(''':/CPU:0''' ): UpperCamelCase : str = model.generate(__SCREAMING_SNAKE_CASE , do_sample=__SCREAMING_SNAKE_CASE , seed=[7, 0] ) UpperCamelCase : Dict = tokenizer.decode(output_ids[0] , skip_special_tokens=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = ( '''Today is a nice day and warm evening here over Southern Alberta!! Today when they closed schools due''' ) self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) @slow def _lowercase ( self ): """simple docstring""" UpperCamelCase : Dict = TFXGLMForCausalLM.from_pretrained('''facebook/xglm-564M''' ) UpperCamelCase : Tuple = XGLMTokenizer.from_pretrained('''facebook/xglm-564M''' ) UpperCamelCase : Tuple = '''left''' # use different length sentences to test batching UpperCamelCase : Any = [ '''This is an extremelly long sentence that only exists to test the ability of the model to cope with ''' '''left-padding, such as in batched generation. The output for the sequence below should be the same ''' '''regardless of whether left padding is applied or not. When''', '''Hello, my dog is a little''', ] UpperCamelCase : List[Any] = tokenizer(__SCREAMING_SNAKE_CASE , return_tensors='''tf''' , padding=__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = inputs['''input_ids'''] UpperCamelCase : Optional[int] = model.generate(input_ids=__SCREAMING_SNAKE_CASE , attention_mask=inputs['''attention_mask'''] , max_new_tokens=12 ) UpperCamelCase : Optional[int] = tokenizer(sentences[0] , return_tensors='''tf''' ).input_ids UpperCamelCase : Optional[Any] = model.generate(input_ids=__SCREAMING_SNAKE_CASE , max_new_tokens=12 ) UpperCamelCase : str = tokenizer(sentences[1] , return_tensors='''tf''' ).input_ids UpperCamelCase : List[Any] = model.generate(input_ids=__SCREAMING_SNAKE_CASE , max_new_tokens=12 ) UpperCamelCase : Any = tokenizer.batch_decode(__SCREAMING_SNAKE_CASE , skip_special_tokens=__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = tokenizer.decode(output_non_padded[0] , skip_special_tokens=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Tuple = tokenizer.decode(output_padded[0] , skip_special_tokens=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = [ '''This is an extremelly long sentence that only exists to test the ability of the model to cope with ''' '''left-padding, such as in batched generation. The output for the sequence below should be the same ''' '''regardless of whether left padding is applied or not. When left padding is applied, the sequence will be ''' '''a single''', '''Hello, my dog is a little bit of a shy one, but he is very friendly''', ] self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) self.assertListEqual(__SCREAMING_SNAKE_CASE , [non_padded_sentence, padded_sentence] )
643
1
import argparse import re import requests import torch # git clone https://github.com/salesforce/BLIP.git from models.blip import blip_decoder from models.blip_itm import blip_itm from models.blip_vqa import blip_vqa from PIL import Image from torchvision import transforms from torchvision.transforms.functional import InterpolationMode from transformers import ( BertTokenizer, BlipConfig, BlipForConditionalGeneration, BlipForImageTextRetrieval, BlipForQuestionAnswering, ) def a ( SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : Optional[Any] ): """simple docstring""" UpperCamelCase : Tuple = '''https://storage.googleapis.com/sfr-vision-language-research/BLIP/demo.jpg''' UpperCamelCase : Optional[int] = Image.open(requests.get(SCREAMING_SNAKE_CASE_ , stream=SCREAMING_SNAKE_CASE_ ).raw ).convert('''RGB''' ) UpperCamelCase : Optional[Any] = transforms.Compose( [ transforms.Resize((image_size, image_size) , interpolation=InterpolationMode.BICUBIC ), transforms.ToTensor(), transforms.Normalize((0.48145466, 0.4578275, 0.40821073) , (0.26862954, 0.26130258, 0.27577711) ), ] ) UpperCamelCase : str = transform(SCREAMING_SNAKE_CASE_ ).unsqueeze(0 ).to(SCREAMING_SNAKE_CASE_ ) return image def a ( SCREAMING_SNAKE_CASE_ : Tuple ): """simple docstring""" if "visual_encoder" in key: UpperCamelCase : str = re.sub('''visual_encoder*''' , '''vision_model.encoder''' , SCREAMING_SNAKE_CASE_ ) if "blocks" in key: UpperCamelCase : Any = re.sub(R'''blocks''' , '''layers''' , SCREAMING_SNAKE_CASE_ ) if "attn" in key: UpperCamelCase : Dict = re.sub(R'''attn''' , '''self_attn''' , SCREAMING_SNAKE_CASE_ ) if "norm1" in key: UpperCamelCase : Tuple = re.sub(R'''norm1''' , '''layer_norm1''' , SCREAMING_SNAKE_CASE_ ) if "norm2" in key: UpperCamelCase : str = re.sub(R'''norm2''' , '''layer_norm2''' , SCREAMING_SNAKE_CASE_ ) if "encoder.norm" in key: UpperCamelCase : Optional[int] = re.sub(R'''encoder.norm''' , '''post_layernorm''' , SCREAMING_SNAKE_CASE_ ) if "encoder.patch_embed.proj" in key: UpperCamelCase : List[str] = re.sub(R'''encoder.patch_embed.proj''' , '''embeddings.patch_embedding''' , SCREAMING_SNAKE_CASE_ ) if "encoder.pos_embed" in key: UpperCamelCase : str = re.sub(R'''encoder.pos_embed''' , '''embeddings.position_embedding''' , SCREAMING_SNAKE_CASE_ ) if "encoder.cls_token" in key: UpperCamelCase : Any = re.sub(R'''encoder.cls_token''' , '''embeddings.class_embedding''' , SCREAMING_SNAKE_CASE_ ) if "self_attn" in key: UpperCamelCase : str = re.sub(R'''self_attn.proj''' , '''self_attn.projection''' , SCREAMING_SNAKE_CASE_ ) return key @torch.no_grad() def a ( SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : str=None ): """simple docstring""" if config_path is not None: UpperCamelCase : Optional[int] = BlipConfig.from_pretrained(SCREAMING_SNAKE_CASE_ ) else: UpperCamelCase : Dict = BlipConfig(projection_dim=5_1_2 , text_config={} , vision_config={} ) UpperCamelCase : Optional[int] = BlipForConditionalGeneration(SCREAMING_SNAKE_CASE_ ).eval() UpperCamelCase : Union[str, Any] = '''https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model_base_capfilt_large.pth''' UpperCamelCase : Optional[int] = blip_decoder(pretrained=SCREAMING_SNAKE_CASE_ , image_size=3_8_4 , vit='''base''' ) UpperCamelCase : Dict = pt_model.eval() UpperCamelCase : List[str] = pt_model.state_dict() for key in modified_state_dict.copy(): UpperCamelCase : Optional[Any] = modified_state_dict.pop(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Optional[Any] = rename_key(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Dict = value hf_model.load_state_dict(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : int = 3_8_4 UpperCamelCase : Union[str, Any] = load_demo_image(image_size=SCREAMING_SNAKE_CASE_ , device='''cpu''' ) UpperCamelCase : Tuple = BertTokenizer.from_pretrained('''bert-base-uncased''' ) UpperCamelCase : Tuple = tokenizer(['''a picture of'''] ).input_ids UpperCamelCase : Union[str, Any] = hf_model.generate(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) assert out[0].tolist() == [3_0_5_2_2, 1_0_3_7, 3_8_6_1, 1_9_9_7, 1_0_3_7, 2_4_5_0, 3_5_6_4, 2_0_0_6, 1_9_9_6, 3_5_0_9, 2_0_0_7, 2_0_1_4, 3_8_9_9, 1_0_2] UpperCamelCase : int = hf_model.generate(SCREAMING_SNAKE_CASE_ ) assert out[0].tolist() == [3_0_5_2_2, 1_0_3_7, 2_4_5_0, 3_5_6_4, 2_0_0_6, 1_9_9_6, 3_5_0_9, 2_0_0_7, 2_0_1_4, 3_8_9_9, 1_0_2] if pytorch_dump_folder_path is not None: hf_model.save_pretrained(SCREAMING_SNAKE_CASE_ ) # model_url = 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model_vqa.pth' UpperCamelCase : int = ( '''https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model_base_vqa_capfilt_large.pth''' ) UpperCamelCase : Tuple = blip_vqa(pretrained=SCREAMING_SNAKE_CASE_ , image_size=SCREAMING_SNAKE_CASE_ , vit='''base''' ) vqa_model.eval() UpperCamelCase : Union[str, Any] = vqa_model.state_dict() for key in modified_state_dict.copy(): UpperCamelCase : Optional[int] = modified_state_dict.pop(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Union[str, Any] = rename_key(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : List[str] = value UpperCamelCase : int = BlipForQuestionAnswering(SCREAMING_SNAKE_CASE_ ) hf_vqa_model.load_state_dict(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Union[str, Any] = ['''How many dogs are in this image?'''] UpperCamelCase : Dict = tokenizer(SCREAMING_SNAKE_CASE_ , return_tensors='''pt''' ).input_ids UpperCamelCase : Any = hf_vqa_model.generate(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) print(tokenizer.decode(answer[0] ) ) assert tokenizer.decode(answer[0] ) == "[UNK] 1 [SEP]" if pytorch_dump_folder_path is not None: hf_vqa_model.save_pretrained(pytorch_dump_folder_path + '''_vqa''' ) UpperCamelCase : List[str] = '''https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model_base_retrieval_coco.pth''' UpperCamelCase : Optional[int] = blip_itm(pretrained=SCREAMING_SNAKE_CASE_ , image_size=SCREAMING_SNAKE_CASE_ , vit='''base''' ) itm_model.eval() UpperCamelCase : Dict = itm_model.state_dict() for key in modified_state_dict.copy(): UpperCamelCase : List[Any] = modified_state_dict.pop(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Dict = rename_key(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : str = value UpperCamelCase : List[str] = BlipForImageTextRetrieval(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Optional[Any] = ['''A picture of a woman with a dog sitting in a beach'''] UpperCamelCase : List[Any] = tokenizer( SCREAMING_SNAKE_CASE_ , return_tensors='''pt''' , padding='''max_length''' , truncation=SCREAMING_SNAKE_CASE_ , max_length=3_5 , ).input_ids hf_itm_model.load_state_dict(SCREAMING_SNAKE_CASE_ ) hf_itm_model.eval() UpperCamelCase : Optional[int] = hf_itm_model(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , use_itm_head=SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Union[str, Any] = hf_itm_model(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , use_itm_head=SCREAMING_SNAKE_CASE_ ) assert out[0].item() == 0.2110687494277954 assert torch.nn.functional.softmax(out_itm[0] , dim=1 )[:, 1].item() == 0.45698845386505127 if pytorch_dump_folder_path is not None: hf_itm_model.save_pretrained(pytorch_dump_folder_path + '''_itm''' ) if __name__ == "__main__": __UpperCAmelCase : Union[str, Any] = argparse.ArgumentParser() parser.add_argument("--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.") parser.add_argument("--config_path", default=None, type=str, help="Path to hf config.json of model to convert") __UpperCAmelCase : List[Any] = parser.parse_args() convert_blip_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path)
643
import os from shutil import copyfile from typing import List, Optional, Tuple from tokenizers import processors from ...tokenization_utils import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_mbart import MBartTokenizer else: __UpperCAmelCase : Optional[int] = None __UpperCAmelCase : int = logging.get_logger(__name__) __UpperCAmelCase : List[Any] = {"vocab_file": "sentencepiece.bpe.model", "tokenizer_file": "tokenizer.json"} __UpperCAmelCase : str = { "vocab_file": { "facebook/mbart-large-en-ro": ( "https://huggingface.co/facebook/mbart-large-en-ro/resolve/main/sentencepiece.bpe.model" ), "facebook/mbart-large-cc25": ( "https://huggingface.co/facebook/mbart-large-cc25/resolve/main/sentencepiece.bpe.model" ), }, "tokenizer_file": { "facebook/mbart-large-en-ro": "https://huggingface.co/facebook/mbart-large-en-ro/resolve/main/tokenizer.json", "facebook/mbart-large-cc25": "https://huggingface.co/facebook/mbart-large-cc25/resolve/main/tokenizer.json", }, } __UpperCAmelCase : Union[str, Any] = { "facebook/mbart-large-en-ro": 1024, "facebook/mbart-large-cc25": 1024, } # fmt: off __UpperCAmelCase : Any = ["ar_AR", "cs_CZ", "de_DE", "en_XX", "es_XX", "et_EE", "fi_FI", "fr_XX", "gu_IN", "hi_IN", "it_IT", "ja_XX", "kk_KZ", "ko_KR", "lt_LT", "lv_LV", "my_MM", "ne_NP", "nl_XX", "ro_RO", "ru_RU", "si_LK", "tr_TR", "vi_VN", "zh_CN"] class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : List[str] = VOCAB_FILES_NAMES __UpperCamelCase : str = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCamelCase : Tuple = PRETRAINED_VOCAB_FILES_MAP __UpperCamelCase : Union[str, Any] = ["input_ids", "attention_mask"] __UpperCamelCase : Any = MBartTokenizer __UpperCamelCase : List[int] = [] __UpperCamelCase : List[int] = [] def __init__( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE="<s>" , __SCREAMING_SNAKE_CASE="</s>" , __SCREAMING_SNAKE_CASE="</s>" , __SCREAMING_SNAKE_CASE="<s>" , __SCREAMING_SNAKE_CASE="<unk>" , __SCREAMING_SNAKE_CASE="<pad>" , __SCREAMING_SNAKE_CASE="<mask>" , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" UpperCamelCase : Union[str, Any] = AddedToken(__SCREAMING_SNAKE_CASE , lstrip=__SCREAMING_SNAKE_CASE , rstrip=__SCREAMING_SNAKE_CASE ) if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else mask_token super().__init__( vocab_file=__SCREAMING_SNAKE_CASE , tokenizer_file=__SCREAMING_SNAKE_CASE , bos_token=__SCREAMING_SNAKE_CASE , eos_token=__SCREAMING_SNAKE_CASE , sep_token=__SCREAMING_SNAKE_CASE , cls_token=__SCREAMING_SNAKE_CASE , unk_token=__SCREAMING_SNAKE_CASE , pad_token=__SCREAMING_SNAKE_CASE , mask_token=__SCREAMING_SNAKE_CASE , src_lang=__SCREAMING_SNAKE_CASE , tgt_lang=__SCREAMING_SNAKE_CASE , additional_special_tokens=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) UpperCamelCase : Dict = vocab_file UpperCamelCase : List[str] = False if not self.vocab_file else True UpperCamelCase : List[Any] = FAIRSEQ_LANGUAGE_CODES.copy() if additional_special_tokens is not None: # Only add those special tokens if they are not already there. _additional_special_tokens.extend( [t for t in additional_special_tokens if t not in _additional_special_tokens] ) self.add_special_tokens({'''additional_special_tokens''': _additional_special_tokens} ) UpperCamelCase : List[Any] = { lang_code: self.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) for lang_code in FAIRSEQ_LANGUAGE_CODES } UpperCamelCase : Dict = src_lang if src_lang is not None else '''en_XX''' UpperCamelCase : List[Any] = self.convert_tokens_to_ids(self._src_lang ) UpperCamelCase : str = tgt_lang self.set_src_lang_special_tokens(self._src_lang ) @property def _lowercase ( self ): """simple docstring""" return self._src_lang @src_lang.setter def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Union[str, Any] = new_src_lang self.set_src_lang_special_tokens(self._src_lang ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ): """simple docstring""" if token_ids_a is None: return self.prefix_tokens + token_ids_a + self.suffix_tokens # We don't expect to process pairs, but leave the pair logic for API consistency return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ): """simple docstring""" UpperCamelCase : str = [self.sep_token_id] UpperCamelCase : Union[str, Any] = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): """simple docstring""" if src_lang is None or tgt_lang is None: raise ValueError('''Translation requires a `src_lang` and a `tgt_lang` for this model''' ) UpperCamelCase : List[str] = src_lang UpperCamelCase : Dict = self(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , return_tensors=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[Any] = self.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Any = tgt_lang_id return inputs def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = "en_XX" , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = "ro_RO" , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" UpperCamelCase : Optional[Any] = src_lang UpperCamelCase : Optional[int] = tgt_lang return super().prepare_seqaseq_batch(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" return self.set_src_lang_special_tokens(self.src_lang ) def _lowercase ( self ): """simple docstring""" return self.set_tgt_lang_special_tokens(self.tgt_lang ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Optional[int] = self.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Any = [] UpperCamelCase : Dict = [self.eos_token_id, self.cur_lang_code] UpperCamelCase : Union[str, Any] = self.convert_ids_to_tokens(self.prefix_tokens ) UpperCamelCase : int = self.convert_ids_to_tokens(self.suffix_tokens ) UpperCamelCase : Tuple = processors.TemplateProcessing( single=prefix_tokens_str + ['''$A'''] + suffix_tokens_str , pair=prefix_tokens_str + ['''$A''', '''$B'''] + suffix_tokens_str , special_tokens=list(zip(prefix_tokens_str + suffix_tokens_str , self.prefix_tokens + self.suffix_tokens ) ) , ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Optional[int] = self.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) UpperCamelCase : int = [] UpperCamelCase : Optional[Any] = [self.eos_token_id, self.cur_lang_code] UpperCamelCase : Optional[Any] = self.convert_ids_to_tokens(self.prefix_tokens ) UpperCamelCase : List[str] = self.convert_ids_to_tokens(self.suffix_tokens ) UpperCamelCase : Optional[int] = processors.TemplateProcessing( single=prefix_tokens_str + ['''$A'''] + suffix_tokens_str , pair=prefix_tokens_str + ['''$A''', '''$B'''] + suffix_tokens_str , special_tokens=list(zip(prefix_tokens_str + suffix_tokens_str , self.prefix_tokens + self.suffix_tokens ) ) , ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ): """simple docstring""" if not self.can_save_slow_tokenizer: raise ValueError( '''Your fast tokenizer does not have the necessary information to save the vocabulary for a slow ''' '''tokenizer.''' ) if not os.path.isdir(__SCREAMING_SNAKE_CASE ): logger.error(f"""Vocabulary path ({save_directory}) should be a directory.""" ) return UpperCamelCase : Optional[int] = os.path.join( __SCREAMING_SNAKE_CASE , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(__SCREAMING_SNAKE_CASE ): copyfile(self.vocab_file , __SCREAMING_SNAKE_CASE ) return (out_vocab_file,)
643
1
__UpperCAmelCase : Optional[Any] = { "A": ["B", "C", "E"], "B": ["A", "D", "E"], "C": ["A", "F", "G"], "D": ["B"], "E": ["A", "B", "D"], "F": ["C"], "G": ["C"], } def a ( SCREAMING_SNAKE_CASE_ : dict , SCREAMING_SNAKE_CASE_ : Tuple , SCREAMING_SNAKE_CASE_ : Any ): """simple docstring""" UpperCamelCase : List[str] = set() # keep track of all the paths to be checked UpperCamelCase : str = [[start]] # return path if start is goal if start == goal: return [start] # keeps looping until all possible paths have been checked while queue: # pop the first path from the queue UpperCamelCase : Optional[Any] = queue.pop(0 ) # get the last node from the path UpperCamelCase : List[Any] = path[-1] if node not in explored: UpperCamelCase : int = graph[node] # go through all neighbour nodes, construct a new path and # push it into the queue for neighbour in neighbours: UpperCamelCase : Union[str, Any] = list(SCREAMING_SNAKE_CASE_ ) new_path.append(SCREAMING_SNAKE_CASE_ ) queue.append(SCREAMING_SNAKE_CASE_ ) # return path if neighbour is goal if neighbour == goal: return new_path # mark node as explored explored.add(SCREAMING_SNAKE_CASE_ ) # in case there's no path between the 2 nodes return [] def a ( SCREAMING_SNAKE_CASE_ : dict , SCREAMING_SNAKE_CASE_ : Tuple , SCREAMING_SNAKE_CASE_ : Optional[Any] ): """simple docstring""" if not graph or start not in graph or target not in graph: return -1 if start == target: return 0 UpperCamelCase : Tuple = [start] UpperCamelCase : List[str] = set(SCREAMING_SNAKE_CASE_ ) # Keep tab on distances from `start` node. UpperCamelCase : Tuple = {start: 0, target: -1} while queue: UpperCamelCase : List[Any] = queue.pop(0 ) if node == target: UpperCamelCase : int = ( dist[node] if dist[target] == -1 else min(dist[target] , dist[node] ) ) for adjacent in graph[node]: if adjacent not in visited: visited.add(SCREAMING_SNAKE_CASE_ ) queue.append(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : List[str] = dist[node] + 1 return dist[target] if __name__ == "__main__": print(bfs_shortest_path(demo_graph, "G", "D")) # returns ['G', 'C', 'A', 'B', 'D'] print(bfs_shortest_path_distance(demo_graph, "G", "D")) # returns 4
643
import unittest import numpy as np import torch from diffusers import VersatileDiffusionImageVariationPipeline from diffusers.utils.testing_utils import load_image, require_torch_gpu, slow, torch_device __UpperCAmelCase : Dict = False class UpperCAmelCase_ ( unittest.TestCase): '''simple docstring''' pass @slow @require_torch_gpu class UpperCAmelCase_ ( unittest.TestCase): '''simple docstring''' def _lowercase ( self ): """simple docstring""" UpperCamelCase : Dict = VersatileDiffusionImageVariationPipeline.from_pretrained('''shi-labs/versatile-diffusion''' ) pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg''' ) UpperCamelCase : str = torch.manual_seed(0 ) UpperCamelCase : Union[str, Any] = pipe( image=__SCREAMING_SNAKE_CASE , generator=__SCREAMING_SNAKE_CASE , guidance_scale=7.5 , num_inference_steps=50 , output_type='''numpy''' , ).images UpperCamelCase : List[Any] = image[0, 253:256, 253:256, -1] assert image.shape == (1, 512, 512, 3) UpperCamelCase : Dict = np.array([0.0_441, 0.0_469, 0.0_507, 0.0_575, 0.0_632, 0.0_650, 0.0_865, 0.0_909, 0.0_945] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
643
1
import gc import random import unittest import torch from diffusers import ( IFImgaImgPipeline, IFImgaImgSuperResolutionPipeline, IFInpaintingPipeline, IFInpaintingSuperResolutionPipeline, IFPipeline, IFSuperResolutionPipeline, ) from diffusers.models.attention_processor import AttnAddedKVProcessor from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import floats_tensor, load_numpy, require_torch_gpu, skip_mps, slow, torch_device from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference from . import IFPipelineTesterMixin @skip_mps class UpperCAmelCase_ ( _a, _a, unittest.TestCase): '''simple docstring''' __UpperCamelCase : Tuple = IFPipeline __UpperCamelCase : Dict = TEXT_TO_IMAGE_PARAMS - {"width", "height", "latents"} __UpperCamelCase : int = TEXT_TO_IMAGE_BATCH_PARAMS __UpperCamelCase : Optional[int] = PipelineTesterMixin.required_optional_params - {"latents"} def _lowercase ( self ): """simple docstring""" return self._get_dummy_components() def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=0 ): """simple docstring""" if str(__SCREAMING_SNAKE_CASE ).startswith('''mps''' ): UpperCamelCase : List[Any] = torch.manual_seed(__SCREAMING_SNAKE_CASE ) else: UpperCamelCase : Any = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = { '''prompt''': '''A painting of a squirrel eating a burger''', '''generator''': generator, '''num_inference_steps''': 2, '''output_type''': '''numpy''', } return inputs def _lowercase ( self ): """simple docstring""" self._test_save_load_optional_components() @unittest.skipIf(torch_device != '''cuda''' , reason='''float16 requires CUDA''' ) def _lowercase ( self ): """simple docstring""" super().test_save_load_floataa(expected_max_diff=1e-1 ) def _lowercase ( self ): """simple docstring""" self._test_attention_slicing_forward_pass(expected_max_diff=1e-2 ) def _lowercase ( self ): """simple docstring""" self._test_save_load_local() def _lowercase ( self ): """simple docstring""" self._test_inference_batch_single_identical( expected_max_diff=1e-2 , ) @unittest.skipIf( torch_device != '''cuda''' or not is_xformers_available() , reason='''XFormers attention is only available with CUDA and `xformers` installed''' , ) def _lowercase ( self ): """simple docstring""" self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3 ) @slow @require_torch_gpu class UpperCAmelCase_ ( unittest.TestCase): '''simple docstring''' def _lowercase ( self ): """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[Any] = IFPipeline.from_pretrained('''DeepFloyd/IF-I-XL-v1.0''' , variant='''fp16''' , torch_dtype=torch.floataa ) UpperCamelCase : Optional[int] = IFSuperResolutionPipeline.from_pretrained( '''DeepFloyd/IF-II-L-v1.0''' , variant='''fp16''' , torch_dtype=torch.floataa , text_encoder=__SCREAMING_SNAKE_CASE , tokenizer=__SCREAMING_SNAKE_CASE ) # pre compute text embeddings and remove T5 to save memory pipe_a.text_encoder.to('''cuda''' ) UpperCamelCase , UpperCamelCase : Tuple = pipe_a.encode_prompt('''anime turtle''' , device='''cuda''' ) del pipe_a.tokenizer del pipe_a.text_encoder gc.collect() UpperCamelCase : List[str] = None UpperCamelCase : int = None pipe_a.enable_model_cpu_offload() pipe_a.enable_model_cpu_offload() pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) self._test_if(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) pipe_a.remove_all_hooks() pipe_a.remove_all_hooks() # img2img UpperCamelCase : Tuple = IFImgaImgPipeline(**pipe_a.components ) UpperCamelCase : Optional[int] = IFImgaImgSuperResolutionPipeline(**pipe_a.components ) pipe_a.enable_model_cpu_offload() pipe_a.enable_model_cpu_offload() pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) self._test_if_imgaimg(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) pipe_a.remove_all_hooks() pipe_a.remove_all_hooks() # inpainting UpperCamelCase : List[str] = IFInpaintingPipeline(**pipe_a.components ) UpperCamelCase : Dict = IFInpaintingSuperResolutionPipeline(**pipe_a.components ) pipe_a.enable_model_cpu_offload() pipe_a.enable_model_cpu_offload() pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) pipe_a.unet.set_attn_processor(AttnAddedKVProcessor() ) self._test_if_inpainting(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" _start_torch_memory_measurement() UpperCamelCase : List[Any] = torch.Generator(device='''cpu''' ).manual_seed(0 ) UpperCamelCase : List[Any] = pipe_a( prompt_embeds=__SCREAMING_SNAKE_CASE , negative_prompt_embeds=__SCREAMING_SNAKE_CASE , num_inference_steps=2 , generator=__SCREAMING_SNAKE_CASE , output_type='''np''' , ) UpperCamelCase : Any = output.images[0] assert image.shape == (64, 64, 3) UpperCamelCase : Dict = torch.cuda.max_memory_allocated() assert mem_bytes < 13 * 10**9 UpperCamelCase : str = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if.npy''' ) assert_mean_pixel_difference(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # pipeline 2 _start_torch_memory_measurement() UpperCamelCase : Optional[Any] = torch.Generator(device='''cpu''' ).manual_seed(0 ) UpperCamelCase : Any = floats_tensor((1, 3, 64, 64) , rng=random.Random(0 ) ).to(__SCREAMING_SNAKE_CASE ) UpperCamelCase : str = pipe_a( prompt_embeds=__SCREAMING_SNAKE_CASE , negative_prompt_embeds=__SCREAMING_SNAKE_CASE , image=__SCREAMING_SNAKE_CASE , generator=__SCREAMING_SNAKE_CASE , num_inference_steps=2 , output_type='''np''' , ) UpperCamelCase : Any = output.images[0] assert image.shape == (256, 256, 3) UpperCamelCase : Union[str, Any] = torch.cuda.max_memory_allocated() assert mem_bytes < 4 * 10**9 UpperCamelCase : Union[str, Any] = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_superresolution_stage_II.npy''' ) assert_mean_pixel_difference(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" _start_torch_memory_measurement() UpperCamelCase : int = floats_tensor((1, 3, 64, 64) , rng=random.Random(0 ) ).to(__SCREAMING_SNAKE_CASE ) UpperCamelCase : int = torch.Generator(device='''cpu''' ).manual_seed(0 ) UpperCamelCase : List[str] = pipe_a( prompt_embeds=__SCREAMING_SNAKE_CASE , negative_prompt_embeds=__SCREAMING_SNAKE_CASE , image=__SCREAMING_SNAKE_CASE , num_inference_steps=2 , generator=__SCREAMING_SNAKE_CASE , output_type='''np''' , ) UpperCamelCase : Optional[int] = output.images[0] assert image.shape == (64, 64, 3) UpperCamelCase : Union[str, Any] = torch.cuda.max_memory_allocated() assert mem_bytes < 10 * 10**9 UpperCamelCase : Any = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_img2img.npy''' ) assert_mean_pixel_difference(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # pipeline 2 _start_torch_memory_measurement() UpperCamelCase : Tuple = torch.Generator(device='''cpu''' ).manual_seed(0 ) UpperCamelCase : Dict = floats_tensor((1, 3, 256, 256) , rng=random.Random(0 ) ).to(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = floats_tensor((1, 3, 64, 64) , rng=random.Random(0 ) ).to(__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[Any] = pipe_a( prompt_embeds=__SCREAMING_SNAKE_CASE , negative_prompt_embeds=__SCREAMING_SNAKE_CASE , image=__SCREAMING_SNAKE_CASE , original_image=__SCREAMING_SNAKE_CASE , generator=__SCREAMING_SNAKE_CASE , num_inference_steps=2 , output_type='''np''' , ) UpperCamelCase : Tuple = output.images[0] assert image.shape == (256, 256, 3) UpperCamelCase : str = torch.cuda.max_memory_allocated() assert mem_bytes < 4 * 10**9 UpperCamelCase : List[str] = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_img2img_superresolution_stage_II.npy''' ) assert_mean_pixel_difference(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" _start_torch_memory_measurement() UpperCamelCase : Optional[int] = floats_tensor((1, 3, 64, 64) , rng=random.Random(0 ) ).to(__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = floats_tensor((1, 3, 64, 64) , rng=random.Random(1 ) ).to(__SCREAMING_SNAKE_CASE ) UpperCamelCase : int = torch.Generator(device='''cpu''' ).manual_seed(0 ) UpperCamelCase : Dict = pipe_a( prompt_embeds=__SCREAMING_SNAKE_CASE , negative_prompt_embeds=__SCREAMING_SNAKE_CASE , image=__SCREAMING_SNAKE_CASE , mask_image=__SCREAMING_SNAKE_CASE , num_inference_steps=2 , generator=__SCREAMING_SNAKE_CASE , output_type='''np''' , ) UpperCamelCase : Tuple = output.images[0] assert image.shape == (64, 64, 3) UpperCamelCase : int = torch.cuda.max_memory_allocated() assert mem_bytes < 10 * 10**9 UpperCamelCase : Optional[int] = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_inpainting.npy''' ) assert_mean_pixel_difference(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # pipeline 2 _start_torch_memory_measurement() UpperCamelCase : Any = torch.Generator(device='''cpu''' ).manual_seed(0 ) UpperCamelCase : Union[str, Any] = floats_tensor((1, 3, 64, 64) , rng=random.Random(0 ) ).to(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = floats_tensor((1, 3, 256, 256) , rng=random.Random(0 ) ).to(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Tuple = floats_tensor((1, 3, 256, 256) , rng=random.Random(1 ) ).to(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Tuple = pipe_a( prompt_embeds=__SCREAMING_SNAKE_CASE , negative_prompt_embeds=__SCREAMING_SNAKE_CASE , image=__SCREAMING_SNAKE_CASE , mask_image=__SCREAMING_SNAKE_CASE , original_image=__SCREAMING_SNAKE_CASE , generator=__SCREAMING_SNAKE_CASE , num_inference_steps=2 , output_type='''np''' , ) UpperCamelCase : Optional[Any] = output.images[0] assert image.shape == (256, 256, 3) UpperCamelCase : Tuple = torch.cuda.max_memory_allocated() assert mem_bytes < 4 * 10**9 UpperCamelCase : List[Any] = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_inpainting_superresolution_stage_II.npy''' ) assert_mean_pixel_difference(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def a ( ): """simple docstring""" torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats()
643
import copy from typing import Any, Dict, List, Optional, Union import numpy as np 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 __UpperCAmelCase : Dict = logging.get_logger(__name__) class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : Any = ["input_features"] def __init__( self , __SCREAMING_SNAKE_CASE=80 , __SCREAMING_SNAKE_CASE=16_000 , __SCREAMING_SNAKE_CASE=160 , __SCREAMING_SNAKE_CASE=30 , __SCREAMING_SNAKE_CASE=400 , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=False , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" super().__init__( feature_size=__SCREAMING_SNAKE_CASE , sampling_rate=__SCREAMING_SNAKE_CASE , padding_value=__SCREAMING_SNAKE_CASE , return_attention_mask=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) UpperCamelCase : List[str] = n_fft UpperCamelCase : Dict = hop_length UpperCamelCase : Dict = chunk_length UpperCamelCase : List[str] = chunk_length * sampling_rate UpperCamelCase : Dict = self.n_samples // hop_length UpperCamelCase : str = sampling_rate UpperCamelCase : Union[str, Any] = mel_filter_bank( num_frequency_bins=1 + n_fft // 2 , num_mel_filters=__SCREAMING_SNAKE_CASE , min_frequency=0.0 , max_frequency=8_000.0 , sampling_rate=__SCREAMING_SNAKE_CASE , norm='''slaney''' , mel_scale='''slaney''' , ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : List[str] = spectrogram( __SCREAMING_SNAKE_CASE , window_function(self.n_fft , '''hann''' ) , frame_length=self.n_fft , hop_length=self.hop_length , power=2.0 , mel_filters=self.mel_filters , log_mel='''log10''' , ) UpperCamelCase : int = log_spec[:, :-1] UpperCamelCase : int = np.maximum(__SCREAMING_SNAKE_CASE , log_spec.max() - 8.0 ) UpperCamelCase : Any = (log_spec + 4.0) / 4.0 return log_spec @staticmethod # Copied from transformers.models.wav2vec2.feature_extraction_wav2vec2.Wav2Vec2FeatureExtractor.zero_mean_unit_var_norm def _lowercase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = 0.0 ): """simple docstring""" if attention_mask is not None: UpperCamelCase : List[Any] = np.array(__SCREAMING_SNAKE_CASE , np.intaa ) UpperCamelCase : Optional[Any] = [] for vector, length in zip(__SCREAMING_SNAKE_CASE , attention_mask.sum(-1 ) ): UpperCamelCase : Optional[Any] = (vector - vector[:length].mean()) / np.sqrt(vector[:length].var() + 1e-7 ) if length < normed_slice.shape[0]: UpperCamelCase : Optional[int] = padding_value normed_input_values.append(__SCREAMING_SNAKE_CASE ) else: UpperCamelCase : Union[str, Any] = [(x - x.mean()) / np.sqrt(x.var() + 1e-7 ) for x in input_values] return normed_input_values def __call__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = True , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = "max_length" , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( f"""The model corresponding to this feature extractor: {self.__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 : Tuple = isinstance(__SCREAMING_SNAKE_CASE , np.ndarray ) and len(raw_speech.shape ) > 1 if is_batched_numpy and len(raw_speech.shape ) > 2: raise ValueError(f"""Only mono-channel audio is supported for input to {self}""" ) UpperCamelCase : Union[str, Any] = is_batched_numpy or ( isinstance(__SCREAMING_SNAKE_CASE , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: UpperCamelCase : List[Any] = [np.asarray([speech] , dtype=np.floataa ).T for speech in raw_speech] elif not is_batched and not isinstance(__SCREAMING_SNAKE_CASE , np.ndarray ): UpperCamelCase : int = np.asarray(__SCREAMING_SNAKE_CASE , dtype=np.floataa ) elif isinstance(__SCREAMING_SNAKE_CASE , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): UpperCamelCase : Union[str, Any] = raw_speech.astype(np.floataa ) # always return batch if not is_batched: UpperCamelCase : Optional[int] = [np.asarray([raw_speech] ).T] UpperCamelCase : Optional[int] = BatchFeature({'''input_features''': raw_speech} ) # convert into correct format for padding UpperCamelCase : Optional[Any] = self.pad( __SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE , max_length=max_length if max_length else self.n_samples , truncation=__SCREAMING_SNAKE_CASE , pad_to_multiple_of=__SCREAMING_SNAKE_CASE , return_attention_mask=return_attention_mask or do_normalize , ) # zero-mean and unit-variance normalization if do_normalize: UpperCamelCase : Optional[Any] = self.zero_mean_unit_var_norm( padded_inputs['''input_features'''] , attention_mask=padded_inputs['''attention_mask'''] , padding_value=self.padding_value , ) UpperCamelCase : List[str] = np.stack(padded_inputs['''input_features'''] , axis=0 ) # make sure list is in array format UpperCamelCase : Dict = padded_inputs.get('''input_features''' ).transpose(2 , 0 , 1 ) UpperCamelCase : Tuple = [self._np_extract_fbank_features(__SCREAMING_SNAKE_CASE ) for waveform in input_features[0]] if isinstance(input_features[0] , __SCREAMING_SNAKE_CASE ): UpperCamelCase : Optional[int] = [np.asarray(__SCREAMING_SNAKE_CASE , dtype=np.floataa ) for feature in input_features] else: UpperCamelCase : Dict = input_features if return_attention_mask: # rescale from sample (48000) to feature (3000) UpperCamelCase : Union[str, Any] = padded_inputs['''attention_mask'''][:, :: self.hop_length] if return_tensors is not None: UpperCamelCase : Dict = padded_inputs.convert_to_tensors(__SCREAMING_SNAKE_CASE ) return padded_inputs def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[Any] = copy.deepcopy(self.__dict__ ) UpperCamelCase : List[str] = self.__class__.__name__ if "mel_filters" in output: del output["mel_filters"] return output
643
1
from __future__ import annotations import math import numpy as np from numpy.linalg import norm def a ( SCREAMING_SNAKE_CASE_ : np.ndarray , SCREAMING_SNAKE_CASE_ : np.ndarray ): """simple docstring""" return math.sqrt(sum(pow(a - b , 2 ) for a, b in zip(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) ) def a ( SCREAMING_SNAKE_CASE_ : np.ndarray , SCREAMING_SNAKE_CASE_ : np.ndarray ): """simple docstring""" if dataset.ndim != value_array.ndim: UpperCamelCase : List[Any] = ( '''Wrong input data\'s dimensions... ''' F"""dataset : {dataset.ndim}, value_array : {value_array.ndim}""" ) raise ValueError(SCREAMING_SNAKE_CASE_ ) try: if dataset.shape[1] != value_array.shape[1]: UpperCamelCase : str = ( '''Wrong input data\'s shape... ''' F"""dataset : {dataset.shape[1]}, value_array : {value_array.shape[1]}""" ) raise ValueError(SCREAMING_SNAKE_CASE_ ) except IndexError: if dataset.ndim != value_array.ndim: raise TypeError('''Wrong shape''' ) if dataset.dtype != value_array.dtype: UpperCamelCase : Union[str, Any] = ( '''Input data have different datatype... ''' F"""dataset : {dataset.dtype}, value_array : {value_array.dtype}""" ) raise TypeError(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : int = [] for value in value_array: UpperCamelCase : Any = euclidean(SCREAMING_SNAKE_CASE_ , dataset[0] ) UpperCamelCase : Dict = dataset[0].tolist() for dataset_value in dataset[1:]: UpperCamelCase : Tuple = euclidean(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if dist > temp_dist: UpperCamelCase : Union[str, Any] = temp_dist UpperCamelCase : Tuple = dataset_value.tolist() answer.append([vector, dist] ) return answer def a ( SCREAMING_SNAKE_CASE_ : np.ndarray , SCREAMING_SNAKE_CASE_ : np.ndarray ): """simple docstring""" return np.dot(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) / (norm(SCREAMING_SNAKE_CASE_ ) * norm(SCREAMING_SNAKE_CASE_ )) if __name__ == "__main__": import doctest doctest.testmod()
643
import json from typing import List, Optional, Tuple from tokenizers import normalizers from tokenizers.pre_tokenizers import BertPreTokenizer, PreTokenizer from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_roformer import RoFormerTokenizer from .tokenization_utils import JiebaPreTokenizer __UpperCAmelCase : Dict = logging.get_logger(__name__) __UpperCAmelCase : Optional[Any] = {"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"} __UpperCAmelCase : Dict = { "vocab_file": { "junnyu/roformer_chinese_small": "https://huggingface.co/junnyu/roformer_chinese_small/resolve/main/vocab.txt", "junnyu/roformer_chinese_base": "https://huggingface.co/junnyu/roformer_chinese_base/resolve/main/vocab.txt", "junnyu/roformer_chinese_char_small": ( "https://huggingface.co/junnyu/roformer_chinese_char_small/resolve/main/vocab.txt" ), "junnyu/roformer_chinese_char_base": ( "https://huggingface.co/junnyu/roformer_chinese_char_base/resolve/main/vocab.txt" ), "junnyu/roformer_small_discriminator": ( "https://huggingface.co/junnyu/roformer_small_discriminator/resolve/main/vocab.txt" ), "junnyu/roformer_small_generator": ( "https://huggingface.co/junnyu/roformer_small_generator/resolve/main/vocab.txt" ), } } __UpperCAmelCase : Tuple = { "junnyu/roformer_chinese_small": 1536, "junnyu/roformer_chinese_base": 1536, "junnyu/roformer_chinese_char_small": 512, "junnyu/roformer_chinese_char_base": 512, "junnyu/roformer_small_discriminator": 128, "junnyu/roformer_small_generator": 128, } __UpperCAmelCase : Any = { "junnyu/roformer_chinese_small": {"do_lower_case": True}, "junnyu/roformer_chinese_base": {"do_lower_case": True}, "junnyu/roformer_chinese_char_small": {"do_lower_case": True}, "junnyu/roformer_chinese_char_base": {"do_lower_case": True}, "junnyu/roformer_small_discriminator": {"do_lower_case": True}, "junnyu/roformer_small_generator": {"do_lower_case": True}, } class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : str = VOCAB_FILES_NAMES __UpperCamelCase : Optional[int] = PRETRAINED_VOCAB_FILES_MAP __UpperCamelCase : Union[str, Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCamelCase : int = PRETRAINED_INIT_CONFIGURATION __UpperCamelCase : Any = RoFormerTokenizer def __init__( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE="[UNK]" , __SCREAMING_SNAKE_CASE="[SEP]" , __SCREAMING_SNAKE_CASE="[PAD]" , __SCREAMING_SNAKE_CASE="[CLS]" , __SCREAMING_SNAKE_CASE="[MASK]" , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" super().__init__( __SCREAMING_SNAKE_CASE , tokenizer_file=__SCREAMING_SNAKE_CASE , do_lower_case=__SCREAMING_SNAKE_CASE , unk_token=__SCREAMING_SNAKE_CASE , sep_token=__SCREAMING_SNAKE_CASE , pad_token=__SCREAMING_SNAKE_CASE , cls_token=__SCREAMING_SNAKE_CASE , mask_token=__SCREAMING_SNAKE_CASE , tokenize_chinese_chars=__SCREAMING_SNAKE_CASE , strip_accents=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) UpperCamelCase : List[Any] = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( pre_tok_state.get('''lowercase''' , __SCREAMING_SNAKE_CASE ) != do_lower_case or pre_tok_state.get('''strip_accents''' , __SCREAMING_SNAKE_CASE ) != strip_accents ): UpperCamelCase : List[Any] = getattr(__SCREAMING_SNAKE_CASE , pre_tok_state.pop('''type''' ) ) UpperCamelCase : Optional[int] = do_lower_case UpperCamelCase : Optional[Any] = strip_accents UpperCamelCase : List[Any] = pre_tok_class(**__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[Any] = do_lower_case def __getstate__( self ): """simple docstring""" UpperCamelCase : Optional[int] = self.__dict__.copy() UpperCamelCase : Any = BertPreTokenizer() return state def __setstate__( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Any = d UpperCamelCase : List[str] = self.__dict__['''_tokenizer'''].get_vocab() UpperCamelCase : Any = PreTokenizer.custom(JiebaPreTokenizer(__SCREAMING_SNAKE_CASE ) ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None ): """simple docstring""" UpperCamelCase : Any = [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 _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ): """simple docstring""" UpperCamelCase : Dict = [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 ) * [0] + len(token_ids_a + sep ) * [1] def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ): """simple docstring""" UpperCamelCase : List[Any] = self._tokenizer.model.save(__SCREAMING_SNAKE_CASE , name=__SCREAMING_SNAKE_CASE ) return tuple(__SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=False , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" UpperCamelCase : Any = BertPreTokenizer() return super().save_pretrained(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE )
643
1
def a ( SCREAMING_SNAKE_CASE_ : Optional[Any] , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : Optional[Any] , SCREAMING_SNAKE_CASE_ : Any ): """simple docstring""" if index == r: for j in range(SCREAMING_SNAKE_CASE_ ): print(data[j] , end=''' ''' ) print(''' ''' ) return # When no more elements are there to put in data[] if i >= n: return # current is included, put next at next location UpperCamelCase : Optional[Any] = arr[i] combination_util(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , index + 1 , SCREAMING_SNAKE_CASE_ , i + 1 ) # current is excluded, replace it with # next (Note that i+1 is passed, but # index is not changed) combination_util(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , i + 1 ) # The main function that prints all combinations # of size r in arr[] of size n. This function # mainly uses combinationUtil() def a ( SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : Any ): """simple docstring""" UpperCamelCase : List[str] = [0] * r # Print all combination using temporary array 'data[]' combination_util(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , 0 , SCREAMING_SNAKE_CASE_ , 0 ) if __name__ == "__main__": # Driver code to check the function above __UpperCAmelCase : Optional[int] = [10, 20, 30, 40, 50] print_combination(arr, len(arr), 3) # This code is contributed by Ambuj sahu
643
from __future__ import annotations def a ( SCREAMING_SNAKE_CASE_ : list[int] ): """simple docstring""" if len(SCREAMING_SNAKE_CASE_ ) == 0: return array UpperCamelCase , UpperCamelCase : Union[str, Any] = min(SCREAMING_SNAKE_CASE_ ), max(SCREAMING_SNAKE_CASE_ ) # Compute the variables UpperCamelCase : Union[str, Any] = _max - _min + 1 UpperCamelCase , UpperCamelCase : Optional[Any] = [0] * holes_range, [0] * holes_range # Make the sorting. for i in array: UpperCamelCase : Optional[int] = i - _min UpperCamelCase : Any = i holes_repeat[index] += 1 # Makes the array back by replacing the numbers. UpperCamelCase : str = 0 for i in range(SCREAMING_SNAKE_CASE_ ): while holes_repeat[i] > 0: UpperCamelCase : List[Any] = holes[i] index += 1 holes_repeat[i] -= 1 # Returns the sorted array. return array if __name__ == "__main__": import doctest doctest.testmod() __UpperCAmelCase : Any = input("Enter numbers separated by comma:\n") __UpperCAmelCase : int = [int(x) for x in user_input.split(",")] print(pigeon_sort(unsorted))
643
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) __UpperCAmelCase : Optional[Any] = { "configuration_mobilevit": ["MOBILEVIT_PRETRAINED_CONFIG_ARCHIVE_MAP", "MobileViTConfig", "MobileViTOnnxConfig"], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase : Union[str, Any] = ["MobileViTFeatureExtractor"] __UpperCAmelCase : Optional[int] = ["MobileViTImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase : List[Any] = [ "MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST", "MobileViTForImageClassification", "MobileViTForSemanticSegmentation", "MobileViTModel", "MobileViTPreTrainedModel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase : List[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 __UpperCAmelCase : Union[str, Any] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
643
import json import os import shutil import warnings from argparse import ArgumentParser, Namespace from pathlib import Path from typing import List from ..utils import logging from . import BaseTransformersCLICommand try: from cookiecutter.main import cookiecutter __UpperCAmelCase : List[Any] = True except ImportError: __UpperCAmelCase : List[str] = False __UpperCAmelCase : Any = logging.get_logger(__name__) # pylint: disable=invalid-name def a ( SCREAMING_SNAKE_CASE_ : Namespace ): """simple docstring""" return AddNewModelCommand(args.testing , args.testing_file , path=args.path ) class UpperCAmelCase_ ( _a): '''simple docstring''' @staticmethod def _lowercase ( __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : List[Any] = parser.add_parser('''add-new-model''' ) add_new_model_parser.add_argument('''--testing''' , action='''store_true''' , help='''If in testing mode.''' ) add_new_model_parser.add_argument('''--testing_file''' , type=__SCREAMING_SNAKE_CASE , help='''Configuration file on which to run.''' ) add_new_model_parser.add_argument( '''--path''' , type=__SCREAMING_SNAKE_CASE , help='''Path to cookiecutter. Should only be used for testing purposes.''' ) add_new_model_parser.set_defaults(func=__SCREAMING_SNAKE_CASE ) def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , *__SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Tuple = testing UpperCamelCase : Any = testing_file UpperCamelCase : Dict = path def _lowercase ( self ): """simple docstring""" warnings.warn( '''The command `transformers-cli add-new-model` is deprecated and will be removed in v5 of Transformers. ''' '''It is not actively maintained anymore, so might give a result that won\'t pass all tests and quality ''' '''checks, you should use `transformers-cli add-new-model-like` instead.''' ) if not _has_cookiecutter: raise ImportError( '''Model creation dependencies are required to use the `add_new_model` command. Install them by running ''' '''the following at the root of your `transformers` clone:\n\n\t$ pip install -e .[modelcreation]\n''' ) # Ensure that there is no other `cookiecutter-template-xxx` directory in the current working directory UpperCamelCase : List[str] = [directory for directory in os.listdir() if '''cookiecutter-template-''' == directory[:22]] if len(__SCREAMING_SNAKE_CASE ) > 0: raise ValueError( '''Several directories starting with `cookiecutter-template-` in current working directory. ''' '''Please clean your directory by removing all folders starting with `cookiecutter-template-` or ''' '''change your working directory.''' ) UpperCamelCase : Dict = ( Path(__SCREAMING_SNAKE_CASE ).parent.parent.parent.parent if self._path is None else Path(self._path ).parent.parent ) UpperCamelCase : List[Any] = path_to_transformer_root / '''templates''' / '''adding_a_new_model''' # Execute cookiecutter if not self._testing: cookiecutter(str(__SCREAMING_SNAKE_CASE ) ) else: with open(self._testing_file , '''r''' ) as configuration_file: UpperCamelCase : Tuple = json.load(__SCREAMING_SNAKE_CASE ) cookiecutter( str(path_to_cookiecutter if self._path is None else self._path ) , no_input=__SCREAMING_SNAKE_CASE , extra_context=__SCREAMING_SNAKE_CASE , ) UpperCamelCase : Dict = [directory for directory in os.listdir() if '''cookiecutter-template-''' in directory[:22]][0] # Retrieve configuration with open(directory + '''/configuration.json''' , '''r''' ) as configuration_file: UpperCamelCase : Tuple = json.load(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Dict = configuration['''lowercase_modelname'''] UpperCamelCase : int = configuration['''generate_tensorflow_pytorch_and_flax'''] os.remove(f"""{directory}/configuration.json""" ) UpperCamelCase : str = '''PyTorch''' in generate_tensorflow_pytorch_and_flax UpperCamelCase : Any = '''TensorFlow''' in generate_tensorflow_pytorch_and_flax UpperCamelCase : Union[str, Any] = '''Flax''' in generate_tensorflow_pytorch_and_flax UpperCamelCase : Optional[Any] = f"""{path_to_transformer_root}/src/transformers/models/{lowercase_model_name}""" os.makedirs(__SCREAMING_SNAKE_CASE , exist_ok=__SCREAMING_SNAKE_CASE ) os.makedirs(f"""{path_to_transformer_root}/tests/models/{lowercase_model_name}""" , exist_ok=__SCREAMING_SNAKE_CASE ) # Tests require submodules as they have parent imports with open(f"""{path_to_transformer_root}/tests/models/{lowercase_model_name}/__init__.py""" , '''w''' ): pass shutil.move( f"""{directory}/__init__.py""" , f"""{model_dir}/__init__.py""" , ) shutil.move( f"""{directory}/configuration_{lowercase_model_name}.py""" , f"""{model_dir}/configuration_{lowercase_model_name}.py""" , ) def remove_copy_lines(__SCREAMING_SNAKE_CASE ): with open(__SCREAMING_SNAKE_CASE , '''r''' ) as f: UpperCamelCase : Any = f.readlines() with open(__SCREAMING_SNAKE_CASE , '''w''' ) as f: for line in lines: if "# Copied from transformers." not in line: f.write(__SCREAMING_SNAKE_CASE ) if output_pytorch: if not self._testing: remove_copy_lines(f"""{directory}/modeling_{lowercase_model_name}.py""" ) shutil.move( f"""{directory}/modeling_{lowercase_model_name}.py""" , f"""{model_dir}/modeling_{lowercase_model_name}.py""" , ) shutil.move( f"""{directory}/test_modeling_{lowercase_model_name}.py""" , f"""{path_to_transformer_root}/tests/models/{lowercase_model_name}/test_modeling_{lowercase_model_name}.py""" , ) else: os.remove(f"""{directory}/modeling_{lowercase_model_name}.py""" ) os.remove(f"""{directory}/test_modeling_{lowercase_model_name}.py""" ) if output_tensorflow: if not self._testing: remove_copy_lines(f"""{directory}/modeling_tf_{lowercase_model_name}.py""" ) shutil.move( f"""{directory}/modeling_tf_{lowercase_model_name}.py""" , f"""{model_dir}/modeling_tf_{lowercase_model_name}.py""" , ) shutil.move( f"""{directory}/test_modeling_tf_{lowercase_model_name}.py""" , f"""{path_to_transformer_root}/tests/models/{lowercase_model_name}/test_modeling_tf_{lowercase_model_name}.py""" , ) else: os.remove(f"""{directory}/modeling_tf_{lowercase_model_name}.py""" ) os.remove(f"""{directory}/test_modeling_tf_{lowercase_model_name}.py""" ) if output_flax: if not self._testing: remove_copy_lines(f"""{directory}/modeling_flax_{lowercase_model_name}.py""" ) shutil.move( f"""{directory}/modeling_flax_{lowercase_model_name}.py""" , f"""{model_dir}/modeling_flax_{lowercase_model_name}.py""" , ) shutil.move( f"""{directory}/test_modeling_flax_{lowercase_model_name}.py""" , f"""{path_to_transformer_root}/tests/models/{lowercase_model_name}/test_modeling_flax_{lowercase_model_name}.py""" , ) else: os.remove(f"""{directory}/modeling_flax_{lowercase_model_name}.py""" ) os.remove(f"""{directory}/test_modeling_flax_{lowercase_model_name}.py""" ) shutil.move( f"""{directory}/{lowercase_model_name}.md""" , f"""{path_to_transformer_root}/docs/source/en/model_doc/{lowercase_model_name}.md""" , ) shutil.move( f"""{directory}/tokenization_{lowercase_model_name}.py""" , f"""{model_dir}/tokenization_{lowercase_model_name}.py""" , ) shutil.move( f"""{directory}/tokenization_fast_{lowercase_model_name}.py""" , f"""{model_dir}/tokenization_{lowercase_model_name}_fast.py""" , ) from os import fdopen, remove from shutil import copymode, move from tempfile import mkstemp def replace(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): # Create temp file UpperCamelCase , UpperCamelCase : Optional[Any] = mkstemp() UpperCamelCase : Tuple = False with fdopen(__SCREAMING_SNAKE_CASE , '''w''' ) as new_file: with open(__SCREAMING_SNAKE_CASE ) as old_file: for line in old_file: new_file.write(__SCREAMING_SNAKE_CASE ) if line_to_copy_below in line: UpperCamelCase : Optional[int] = True for line_to_copy in lines_to_copy: new_file.write(__SCREAMING_SNAKE_CASE ) if not line_found: raise ValueError(f"""Line {line_to_copy_below} was not found in file.""" ) # Copy the file permissions from the old file to the new file copymode(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # Remove original file remove(__SCREAMING_SNAKE_CASE ) # Move new file move(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def skip_units(__SCREAMING_SNAKE_CASE ): return ( ("generating PyTorch" in line and not output_pytorch) or ("generating TensorFlow" in line and not output_tensorflow) or ("generating Flax" in line and not output_flax) ) def replace_in_files(__SCREAMING_SNAKE_CASE ): with open(__SCREAMING_SNAKE_CASE ) as datafile: UpperCamelCase : int = [] UpperCamelCase : Dict = False UpperCamelCase : List[Any] = False for line in datafile: if "# To replace in: " in line and "##" not in line: UpperCamelCase : Dict = line.split('''"''' )[1] UpperCamelCase : int = skip_units(__SCREAMING_SNAKE_CASE ) elif "# Below: " in line and "##" not in line: UpperCamelCase : Dict = line.split('''"''' )[1] UpperCamelCase : List[str] = skip_units(__SCREAMING_SNAKE_CASE ) elif "# End." in line and "##" not in line: if not skip_file and not skip_snippet: replace(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) UpperCamelCase : Dict = [] elif "# Replace with" in line and "##" not in line: UpperCamelCase : Tuple = [] elif "##" not in line: lines_to_copy.append(__SCREAMING_SNAKE_CASE ) remove(__SCREAMING_SNAKE_CASE ) replace_in_files(f"""{directory}/to_replace_{lowercase_model_name}.py""" ) os.rmdir(__SCREAMING_SNAKE_CASE )
643
1
import unittest import numpy as np from transformers import MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING, TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING from transformers.pipelines import AudioClassificationPipeline, pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_torch, require_torchaudio, slow, ) from .test_pipelines_common import ANY @is_pipeline_test class UpperCAmelCase_ ( unittest.TestCase): '''simple docstring''' __UpperCamelCase : int = MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING __UpperCamelCase : Any = TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Any = AudioClassificationPipeline(model=__SCREAMING_SNAKE_CASE , feature_extractor=__SCREAMING_SNAKE_CASE ) # test with a raw waveform UpperCamelCase : Any = np.zeros((34_000,) ) UpperCamelCase : List[str] = np.zeros((14_000,) ) return audio_classifier, [audioa, audio] def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase , UpperCamelCase : Optional[Any] = examples UpperCamelCase : Union[str, Any] = audio_classifier(__SCREAMING_SNAKE_CASE ) # by default a model is initialized with num_labels=2 self.assertEqual( __SCREAMING_SNAKE_CASE , [ {'''score''': ANY(__SCREAMING_SNAKE_CASE ), '''label''': ANY(__SCREAMING_SNAKE_CASE )}, {'''score''': ANY(__SCREAMING_SNAKE_CASE ), '''label''': ANY(__SCREAMING_SNAKE_CASE )}, ] , ) UpperCamelCase : Tuple = audio_classifier(__SCREAMING_SNAKE_CASE , top_k=1 ) self.assertEqual( __SCREAMING_SNAKE_CASE , [ {'''score''': ANY(__SCREAMING_SNAKE_CASE ), '''label''': ANY(__SCREAMING_SNAKE_CASE )}, ] , ) self.run_torchaudio(__SCREAMING_SNAKE_CASE ) @require_torchaudio def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" import datasets # test with a local file UpperCamelCase : Union[str, Any] = datasets.load_dataset('''hf-internal-testing/librispeech_asr_dummy''' , '''clean''' , split='''validation''' ) UpperCamelCase : int = dataset[0]['''audio''']['''array'''] UpperCamelCase : List[str] = audio_classifier(__SCREAMING_SNAKE_CASE ) self.assertEqual( __SCREAMING_SNAKE_CASE , [ {'''score''': ANY(__SCREAMING_SNAKE_CASE ), '''label''': ANY(__SCREAMING_SNAKE_CASE )}, {'''score''': ANY(__SCREAMING_SNAKE_CASE ), '''label''': ANY(__SCREAMING_SNAKE_CASE )}, ] , ) @require_torch def _lowercase ( self ): """simple docstring""" UpperCamelCase : Tuple = '''anton-l/wav2vec2-random-tiny-classifier''' UpperCamelCase : Tuple = pipeline('''audio-classification''' , model=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Tuple = np.ones((8_000,) ) UpperCamelCase : Optional[int] = audio_classifier(__SCREAMING_SNAKE_CASE , top_k=4 ) UpperCamelCase : int = [ {'''score''': 0.0_842, '''label''': '''no'''}, {'''score''': 0.0_838, '''label''': '''up'''}, {'''score''': 0.0_837, '''label''': '''go'''}, {'''score''': 0.0_834, '''label''': '''right'''}, ] UpperCamelCase : Optional[int] = [ {'''score''': 0.0_845, '''label''': '''stop'''}, {'''score''': 0.0_844, '''label''': '''on'''}, {'''score''': 0.0_841, '''label''': '''right'''}, {'''score''': 0.0_834, '''label''': '''left'''}, ] self.assertIn(nested_simplify(__SCREAMING_SNAKE_CASE , decimals=4 ) , [EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2] ) UpperCamelCase : List[str] = {'''array''': np.ones((8_000,) ), '''sampling_rate''': audio_classifier.feature_extractor.sampling_rate} UpperCamelCase : List[str] = audio_classifier(__SCREAMING_SNAKE_CASE , top_k=4 ) self.assertIn(nested_simplify(__SCREAMING_SNAKE_CASE , decimals=4 ) , [EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2] ) @require_torch @slow def _lowercase ( self ): """simple docstring""" import datasets UpperCamelCase : Dict = '''superb/wav2vec2-base-superb-ks''' UpperCamelCase : List[str] = pipeline('''audio-classification''' , model=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = datasets.load_dataset('''anton-l/superb_dummy''' , '''ks''' , split='''test''' ) UpperCamelCase : List[str] = np.array(dataset[3]['''speech'''] , dtype=np.floataa ) UpperCamelCase : str = audio_classifier(__SCREAMING_SNAKE_CASE , top_k=4 ) self.assertEqual( nested_simplify(__SCREAMING_SNAKE_CASE , decimals=3 ) , [ {'''score''': 0.981, '''label''': '''go'''}, {'''score''': 0.007, '''label''': '''up'''}, {'''score''': 0.006, '''label''': '''_unknown_'''}, {'''score''': 0.001, '''label''': '''down'''}, ] , ) @require_tf @unittest.skip('''Audio classification is not implemented for TF''' ) def _lowercase ( self ): """simple docstring""" pass
643
from pathlib import Path import cva import numpy as np from matplotlib import pyplot as plt def a ( SCREAMING_SNAKE_CASE_ : np.ndarray , SCREAMING_SNAKE_CASE_ : np.ndarray , SCREAMING_SNAKE_CASE_ : np.ndarray , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int ): """simple docstring""" UpperCamelCase : str = cva.getAffineTransform(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) return cva.warpAffine(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , (rows, cols) ) if __name__ == "__main__": # read original image __UpperCAmelCase : Tuple = cva.imread( str(Path(__file__).resolve().parent.parent / "image_data" / "lena.jpg") ) # turn image in gray scale value __UpperCAmelCase : int = cva.cvtColor(image, cva.COLOR_BGR2GRAY) # get image shape __UpperCAmelCase , __UpperCAmelCase : Tuple = gray_img.shape # set different points to rotate image __UpperCAmelCase : Optional[int] = np.array([[50, 50], [200, 50], [50, 200]], np.floataa) __UpperCAmelCase : Optional[int] = np.array([[10, 100], [200, 50], [100, 250]], np.floataa) __UpperCAmelCase : Any = np.array([[50, 50], [150, 50], [120, 200]], np.floataa) __UpperCAmelCase : int = np.array([[10, 100], [80, 50], [180, 250]], np.floataa) # add all rotated images in a list __UpperCAmelCase : Union[str, Any] = [ gray_img, get_rotation(gray_img, ptsa, ptsa, img_rows, img_cols), get_rotation(gray_img, ptsa, ptsa, img_rows, img_cols), get_rotation(gray_img, ptsa, ptsa, img_rows, img_cols), ] # plot different image rotations __UpperCAmelCase : List[str] = plt.figure(1) __UpperCAmelCase : Dict = ["Original", "Rotation 1", "Rotation 2", "Rotation 3"] for i, image in enumerate(images): plt.subplot(2, 2, i + 1), plt.imshow(image, "gray") plt.title(titles[i]) plt.axis("off") plt.subplots_adjust(left=0.0, bottom=0.05, right=1.0, top=0.95) plt.show()
643
1
import inspect import os import unittest from pathlib import Path import torch import accelerate from accelerate.test_utils import execute_subprocess_async from accelerate.test_utils.testing import run_command class UpperCAmelCase_ ( unittest.TestCase): '''simple docstring''' __UpperCamelCase : Dict = inspect.getfile(accelerate.test_utils) __UpperCamelCase : Optional[Any] = os.path.sep.join(mod_file.split(os.path.sep)[:-1] + ["scripts", "test_cli.py"]) __UpperCamelCase : Tuple = ["accelerate", "launch"] __UpperCamelCase : str = Path.home() / ".cache/huggingface/accelerate" __UpperCamelCase : List[Any] = "default_config.yaml" __UpperCamelCase : Dict = config_folder / config_file __UpperCamelCase : Any = config_folder / "_default_config.yaml" __UpperCamelCase : Union[str, Any] = Path("tests/test_configs") @classmethod def _lowercase ( cls ): """simple docstring""" if cls.config_path.is_file(): cls.config_path.rename(cls.changed_path ) @classmethod def _lowercase ( cls ): """simple docstring""" if cls.changed_path.is_file(): cls.changed_path.rename(cls.config_path ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : int = self.base_cmd if torch.cuda.is_available() and (torch.cuda.device_count() > 1): cmd += ["--multi_gpu"] execute_subprocess_async(cmd + [self.test_file_path] , env=os.environ.copy() ) def _lowercase ( self ): """simple docstring""" for config in sorted(self.test_config_path.glob('''**/*.yaml''' ) ): with self.subTest(config_file=__SCREAMING_SNAKE_CASE ): execute_subprocess_async( self.base_cmd + ['''--config_file''', str(__SCREAMING_SNAKE_CASE ), self.test_file_path] , env=os.environ.copy() ) def _lowercase ( self ): """simple docstring""" execute_subprocess_async(['''accelerate''', '''test'''] , env=os.environ.copy() ) class UpperCAmelCase_ ( unittest.TestCase): '''simple docstring''' __UpperCamelCase : List[Any] = "test-tpu" __UpperCamelCase : Tuple = "us-central1-a" __UpperCamelCase : Any = "ls" __UpperCamelCase : Union[str, Any] = ["accelerate", "tpu-config"] __UpperCamelCase : str = "cd /usr/share" __UpperCamelCase : Optional[int] = "tests/test_samples/test_command_file.sh" __UpperCamelCase : List[str] = "Running gcloud compute tpus tpu-vm ssh" def _lowercase ( self ): """simple docstring""" UpperCamelCase : Dict = run_command( self.cmd + ['''--command''', self.command, '''--tpu_zone''', self.tpu_zone, '''--tpu_name''', self.tpu_name, '''--debug'''] , return_stdout=__SCREAMING_SNAKE_CASE , ) self.assertIn( f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls --worker all""" , __SCREAMING_SNAKE_CASE , ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : int = run_command( self.cmd + [ '''--config_file''', '''tests/test_configs/0_12_0.yaml''', '''--command''', self.command, '''--tpu_zone''', self.tpu_zone, '''--tpu_name''', self.tpu_name, '''--debug''', ] , return_stdout=__SCREAMING_SNAKE_CASE , ) self.assertIn( f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls --worker all""" , __SCREAMING_SNAKE_CASE , ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : int = run_command( self.cmd + ['''--config_file''', '''tests/test_configs/latest.yaml''', '''--debug'''] , return_stdout=__SCREAMING_SNAKE_CASE ) self.assertIn( f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; echo \"hello world\"; echo \"this is a second command\" --worker all""" , __SCREAMING_SNAKE_CASE , ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Union[str, Any] = run_command( self.cmd + ['''--config_file''', '''tests/test_configs/latest.yaml''', '''--command''', self.command, '''--debug'''] , return_stdout=__SCREAMING_SNAKE_CASE , ) self.assertIn( f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls --worker all""" , __SCREAMING_SNAKE_CASE , ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Dict = run_command( self.cmd + [ '''--config_file''', '''tests/test_configs/latest.yaml''', '''--command''', self.command, '''--command''', '''echo "Hello World"''', '''--debug''', ] , return_stdout=__SCREAMING_SNAKE_CASE , ) self.assertIn( f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls; echo \"Hello World\" --worker all""" , __SCREAMING_SNAKE_CASE , ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Optional[Any] = run_command( self.cmd + ['''--config_file''', '''tests/test_configs/latest.yaml''', '''--command_file''', self.command_file, '''--debug'''] , return_stdout=__SCREAMING_SNAKE_CASE , ) self.assertIn( f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; echo \"hello world\"; echo \"this is a second command\" --worker all""" , __SCREAMING_SNAKE_CASE , ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : str = run_command( self.cmd + [ '''--config_file''', '''tests/test_configs/0_12_0.yaml''', '''--command_file''', self.command_file, '''--tpu_zone''', self.tpu_zone, '''--tpu_name''', self.tpu_name, '''--debug''', ] , return_stdout=__SCREAMING_SNAKE_CASE , ) self.assertIn( f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; echo \"hello world\"; echo \"this is a second command\" --worker all""" , __SCREAMING_SNAKE_CASE , ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : int = run_command( self.cmd + ['''--config_file''', '''tests/test_configs/latest.yaml''', '''--install_accelerate''', '''--debug'''] , return_stdout=__SCREAMING_SNAKE_CASE , ) self.assertIn( f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; pip install accelerate -U; echo \"hello world\"; echo \"this is a second command\" --worker all""" , __SCREAMING_SNAKE_CASE , ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : str = run_command( self.cmd + [ '''--config_file''', '''tests/test_configs/latest.yaml''', '''--install_accelerate''', '''--accelerate_version''', '''12.0.0''', '''--debug''', ] , return_stdout=__SCREAMING_SNAKE_CASE , ) self.assertIn( f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; pip install accelerate==12.0.0; echo \"hello world\"; echo \"this is a second command\" --worker all""" , __SCREAMING_SNAKE_CASE , )
643
import copy from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ..auto import CONFIG_MAPPING __UpperCAmelCase : Optional[Any] = logging.get_logger(__name__) __UpperCAmelCase : List[str] = { "microsoft/conditional-detr-resnet-50": ( "https://huggingface.co/microsoft/conditional-detr-resnet-50/resolve/main/config.json" ), } class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : Optional[int] = "conditional_detr" __UpperCamelCase : Optional[Any] = ["past_key_values"] __UpperCamelCase : Union[str, Any] = { "hidden_size": "d_model", "num_attention_heads": "encoder_attention_heads", } def __init__( self , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=300 , __SCREAMING_SNAKE_CASE=6 , __SCREAMING_SNAKE_CASE=2_048 , __SCREAMING_SNAKE_CASE=8 , __SCREAMING_SNAKE_CASE=6 , __SCREAMING_SNAKE_CASE=2_048 , __SCREAMING_SNAKE_CASE=8 , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE="relu" , __SCREAMING_SNAKE_CASE=256 , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=0.02 , __SCREAMING_SNAKE_CASE=1.0 , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE="sine" , __SCREAMING_SNAKE_CASE="resnet50" , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=5 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=1 , __SCREAMING_SNAKE_CASE=1 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=5 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=0.25 , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" if backbone_config is not None and use_timm_backbone: raise ValueError('''You can\'t specify both `backbone_config` and `use_timm_backbone`.''' ) if not use_timm_backbone: if backbone_config is None: logger.info('''`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.''' ) UpperCamelCase : str = CONFIG_MAPPING['''resnet'''](out_features=['''stage4'''] ) elif isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): UpperCamelCase : Tuple = backbone_config.get('''model_type''' ) UpperCamelCase : Optional[Any] = CONFIG_MAPPING[backbone_model_type] UpperCamelCase : Any = config_class.from_dict(__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = use_timm_backbone UpperCamelCase : int = backbone_config UpperCamelCase : Any = num_channels UpperCamelCase : Optional[Any] = num_queries UpperCamelCase : Tuple = d_model UpperCamelCase : Optional[Any] = encoder_ffn_dim UpperCamelCase : Optional[int] = encoder_layers UpperCamelCase : Union[str, Any] = encoder_attention_heads UpperCamelCase : Optional[Any] = decoder_ffn_dim UpperCamelCase : Optional[int] = decoder_layers UpperCamelCase : Optional[Any] = decoder_attention_heads UpperCamelCase : Any = dropout UpperCamelCase : List[Any] = attention_dropout UpperCamelCase : List[Any] = activation_dropout UpperCamelCase : List[str] = activation_function UpperCamelCase : Optional[int] = init_std UpperCamelCase : Optional[Any] = init_xavier_std UpperCamelCase : Union[str, Any] = encoder_layerdrop UpperCamelCase : Optional[Any] = decoder_layerdrop UpperCamelCase : Tuple = encoder_layers UpperCamelCase : Optional[Any] = auxiliary_loss UpperCamelCase : Union[str, Any] = position_embedding_type UpperCamelCase : Optional[int] = backbone UpperCamelCase : Dict = use_pretrained_backbone UpperCamelCase : Tuple = dilation # Hungarian matcher UpperCamelCase : Union[str, Any] = class_cost UpperCamelCase : List[Any] = bbox_cost UpperCamelCase : Optional[Any] = giou_cost # Loss coefficients UpperCamelCase : Optional[Any] = mask_loss_coefficient UpperCamelCase : Optional[int] = dice_loss_coefficient UpperCamelCase : Optional[Any] = cls_loss_coefficient UpperCamelCase : Optional[int] = bbox_loss_coefficient UpperCamelCase : Optional[int] = giou_loss_coefficient UpperCamelCase : Optional[int] = focal_alpha super().__init__(is_encoder_decoder=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) @property def _lowercase ( self ): """simple docstring""" return self.encoder_attention_heads @property def _lowercase ( self ): """simple docstring""" return self.d_model def _lowercase ( self ): """simple docstring""" UpperCamelCase : Union[str, Any] = copy.deepcopy(self.__dict__ ) if self.backbone_config is not None: UpperCamelCase : List[Any] = self.backbone_config.to_dict() UpperCamelCase : List[Any] = self.__class__.model_type return output class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : Dict = version.parse("1.11") @property def _lowercase ( self ): """simple docstring""" return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ('''pixel_mask''', {0: '''batch'''}), ] ) @property def _lowercase ( self ): """simple docstring""" return 1e-5 @property def _lowercase ( self ): """simple docstring""" return 12
643
1
from typing import List, Optional, Tuple, Union import torch from torch import nn from torch.nn import CrossEntropyLoss from ... import AutoBackbone from ...modeling_outputs import SemanticSegmenterOutput from ...modeling_utils import PreTrainedModel from ...utils import add_start_docstrings, add_start_docstrings_to_model_forward, replace_return_docstrings from ...utils.backbone_utils import BackboneMixin from .configuration_upernet import UperNetConfig __UpperCAmelCase : List[Any] = [ "openmmlab/upernet-convnext-tiny", # See all UperNet models at https://huggingface.co/models?filter=upernet ] # General docstring __UpperCAmelCase : List[str] = "UperNetConfig" class UpperCAmelCase_ ( nn.Module): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = 0 , __SCREAMING_SNAKE_CASE = False , __SCREAMING_SNAKE_CASE = 1 , ): """simple docstring""" super().__init__() UpperCamelCase : str = nn.Convad( in_channels=__SCREAMING_SNAKE_CASE , out_channels=__SCREAMING_SNAKE_CASE , kernel_size=__SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE , bias=__SCREAMING_SNAKE_CASE , dilation=__SCREAMING_SNAKE_CASE , ) UpperCamelCase : int = nn.BatchNormad(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[Any] = nn.ReLU() def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Any = self.conv(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[Any] = self.batch_norm(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = self.activation(__SCREAMING_SNAKE_CASE ) return output class UpperCAmelCase_ ( nn.Module): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" super().__init__() UpperCamelCase : List[Any] = [ nn.AdaptiveAvgPoolad(__SCREAMING_SNAKE_CASE ), UperNetConvModule(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , kernel_size=1 ), ] for i, layer in enumerate(self.layers ): self.add_module(str(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : str = input for layer in self.layers: UpperCamelCase : int = layer(__SCREAMING_SNAKE_CASE ) return hidden_state class UpperCAmelCase_ ( nn.Module): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" super().__init__() UpperCamelCase : List[Any] = pool_scales UpperCamelCase : Dict = align_corners UpperCamelCase : Optional[int] = in_channels UpperCamelCase : Union[str, Any] = channels UpperCamelCase : List[str] = [] for i, pool_scale in enumerate(__SCREAMING_SNAKE_CASE ): UpperCamelCase : Union[str, Any] = UperNetPyramidPoolingBlock(pool_scale=__SCREAMING_SNAKE_CASE , in_channels=__SCREAMING_SNAKE_CASE , channels=__SCREAMING_SNAKE_CASE ) self.blocks.append(__SCREAMING_SNAKE_CASE ) self.add_module(str(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : str = [] for ppm in self.blocks: UpperCamelCase : List[str] = ppm(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Tuple = nn.functional.interpolate( __SCREAMING_SNAKE_CASE , size=x.size()[2:] , mode='''bilinear''' , align_corners=self.align_corners ) ppm_outs.append(__SCREAMING_SNAKE_CASE ) return ppm_outs class UpperCAmelCase_ ( nn.Module): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" super().__init__() UpperCamelCase : int = config UpperCamelCase : List[str] = config.pool_scales # e.g. (1, 2, 3, 6) UpperCamelCase : Optional[int] = in_channels UpperCamelCase : str = config.hidden_size UpperCamelCase : str = False UpperCamelCase : List[str] = nn.Convad(self.channels , config.num_labels , kernel_size=1 ) # PSP Module UpperCamelCase : Optional[int] = UperNetPyramidPoolingModule( self.pool_scales , self.in_channels[-1] , self.channels , align_corners=self.align_corners , ) UpperCamelCase : str = UperNetConvModule( self.in_channels[-1] + len(self.pool_scales ) * self.channels , self.channels , kernel_size=3 , padding=1 , ) # FPN Module UpperCamelCase : Union[str, Any] = nn.ModuleList() UpperCamelCase : Union[str, Any] = nn.ModuleList() for in_channels in self.in_channels[:-1]: # skip the top layer UpperCamelCase : List[Any] = UperNetConvModule(__SCREAMING_SNAKE_CASE , self.channels , kernel_size=1 ) UpperCamelCase : int = UperNetConvModule(self.channels , self.channels , kernel_size=3 , padding=1 ) self.lateral_convs.append(__SCREAMING_SNAKE_CASE ) self.fpn_convs.append(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[Any] = UperNetConvModule( len(self.in_channels ) * self.channels , self.channels , kernel_size=3 , padding=1 , ) def _lowercase ( self ): """simple docstring""" self.apply(self._init_weights ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if isinstance(__SCREAMING_SNAKE_CASE , nn.Convad ): module.weight.data.normal_(mean=0.0 , std=self.config.initializer_range ) if module.bias is not None: module.bias.data.zero_() def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Any = inputs[-1] UpperCamelCase : int = [x] psp_outs.extend(self.psp_modules(__SCREAMING_SNAKE_CASE ) ) UpperCamelCase : Any = torch.cat(__SCREAMING_SNAKE_CASE , dim=1 ) UpperCamelCase : Union[str, Any] = self.bottleneck(__SCREAMING_SNAKE_CASE ) return output def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : str = [lateral_conv(encoder_hidden_states[i] ) for i, lateral_conv in enumerate(self.lateral_convs )] laterals.append(self.psp_forward(__SCREAMING_SNAKE_CASE ) ) # build top-down path UpperCamelCase : int = len(__SCREAMING_SNAKE_CASE ) for i in range(used_backbone_levels - 1 , 0 , -1 ): UpperCamelCase : Optional[int] = laterals[i - 1].shape[2:] UpperCamelCase : Optional[Any] = laterals[i - 1] + nn.functional.interpolate( laterals[i] , size=__SCREAMING_SNAKE_CASE , mode='''bilinear''' , align_corners=self.align_corners ) # build outputs UpperCamelCase : str = [self.fpn_convs[i](laterals[i] ) for i in range(used_backbone_levels - 1 )] # append psp feature fpn_outs.append(laterals[-1] ) for i in range(used_backbone_levels - 1 , 0 , -1 ): UpperCamelCase : int = nn.functional.interpolate( fpn_outs[i] , size=fpn_outs[0].shape[2:] , mode='''bilinear''' , align_corners=self.align_corners ) UpperCamelCase : str = torch.cat(__SCREAMING_SNAKE_CASE , dim=1 ) UpperCamelCase : Tuple = self.fpn_bottleneck(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = self.classifier(__SCREAMING_SNAKE_CASE ) return output class UpperCAmelCase_ ( nn.Module): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = 2 , __SCREAMING_SNAKE_CASE = 3 , __SCREAMING_SNAKE_CASE = 1 ): """simple docstring""" super().__init__() UpperCamelCase : Dict = config UpperCamelCase : Optional[Any] = config.auxiliary_in_channels UpperCamelCase : Union[str, Any] = config.auxiliary_channels UpperCamelCase : Union[str, Any] = config.auxiliary_num_convs UpperCamelCase : Optional[Any] = config.auxiliary_concat_input UpperCamelCase : List[str] = in_index UpperCamelCase : Any = (kernel_size // 2) * dilation UpperCamelCase : Optional[Any] = [] convs.append( UperNetConvModule( self.in_channels , self.channels , kernel_size=__SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE , dilation=__SCREAMING_SNAKE_CASE ) ) for i in range(self.num_convs - 1 ): convs.append( UperNetConvModule( self.channels , self.channels , kernel_size=__SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE , dilation=__SCREAMING_SNAKE_CASE ) ) if self.num_convs == 0: UpperCamelCase : str = nn.Identity() else: UpperCamelCase : Dict = nn.Sequential(*__SCREAMING_SNAKE_CASE ) if self.concat_input: UpperCamelCase : Union[str, Any] = UperNetConvModule( self.in_channels + self.channels , self.channels , kernel_size=__SCREAMING_SNAKE_CASE , padding=kernel_size // 2 ) UpperCamelCase : Optional[Any] = nn.Convad(self.channels , config.num_labels , kernel_size=1 ) def _lowercase ( self ): """simple docstring""" self.apply(self._init_weights ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if isinstance(__SCREAMING_SNAKE_CASE , nn.Convad ): module.weight.data.normal_(mean=0.0 , std=self.config.initializer_range ) if module.bias is not None: module.bias.data.zero_() def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Any = encoder_hidden_states[self.in_index] UpperCamelCase : str = self.convs(__SCREAMING_SNAKE_CASE ) if self.concat_input: UpperCamelCase : int = self.conv_cat(torch.cat([hidden_states, output] , dim=1 ) ) UpperCamelCase : Union[str, Any] = self.classifier(__SCREAMING_SNAKE_CASE ) return output class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : Optional[Any] = UperNetConfig __UpperCamelCase : Optional[int] = "pixel_values" __UpperCamelCase : Dict = True def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): module.backbone.init_weights() module.decode_head.init_weights() module.auxiliary_head.init_weights() def _lowercase ( self ): """simple docstring""" self.backbone.init_weights() self.decode_head.init_weights() self.auxiliary_head.init_weights() def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=False ): """simple docstring""" if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): UpperCamelCase : str = value __UpperCAmelCase : List[Any] = r"\n Parameters:\n This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) sub-class. Use\n it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and\n behavior.\n config ([`UperNetConfig`]): 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" __UpperCAmelCase : Union[str, Any] = r"\n Args:\n pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n Pixel values. Padding will be ignored by default should you provide it. Pixel values can be obtained using\n [`AutoImageProcessor`]. See [`SegformerImageProcessor.__call__`] for details.\n output_attentions (`bool`, *optional*):\n Whether or not to return the attentions tensors of all attention layers in case the backbone has them. See\n `attentions` under returned tensors for more detail.\n output_hidden_states (`bool`, *optional*):\n Whether or not to return the hidden states of all layers of the backbone. See `hidden_states` under\n returned tensors for 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( "UperNet framework leveraging any vision backbone e.g. for ADE20k, CityScapes.", _a, ) class UpperCAmelCase_ ( _a): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" super().__init__(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Dict = AutoBackbone.from_config(config.backbone_config ) # Semantic segmentation head(s) UpperCamelCase : int = UperNetHead(__SCREAMING_SNAKE_CASE , in_channels=self.backbone.channels ) UpperCamelCase : int = UperNetFCNHead(__SCREAMING_SNAKE_CASE ) if config.use_auxiliary_head else None # Initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(UPERNET_INPUTS_DOCSTRING.format('''batch_size, sequence_length''' ) ) @replace_return_docstrings(output_type=__SCREAMING_SNAKE_CASE , config_class=_CONFIG_FOR_DOC ) def _lowercase ( self , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , ): """simple docstring""" UpperCamelCase : List[str] = return_dict if return_dict is not None else self.config.use_return_dict UpperCamelCase : str = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) UpperCamelCase : Tuple = output_attentions if output_attentions is not None else self.config.output_attentions UpperCamelCase : Tuple = self.backbone.forward_with_filtered_kwargs( __SCREAMING_SNAKE_CASE , output_hidden_states=__SCREAMING_SNAKE_CASE , output_attentions=__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = outputs.feature_maps UpperCamelCase : Union[str, Any] = self.decode_head(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = nn.functional.interpolate(__SCREAMING_SNAKE_CASE , size=pixel_values.shape[2:] , mode='''bilinear''' , align_corners=__SCREAMING_SNAKE_CASE ) UpperCamelCase : int = None if self.auxiliary_head is not None: UpperCamelCase : int = self.auxiliary_head(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Tuple = nn.functional.interpolate( __SCREAMING_SNAKE_CASE , size=pixel_values.shape[2:] , mode='''bilinear''' , align_corners=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = None if labels is not None: if self.config.num_labels == 1: raise ValueError('''The number of labels should be greater than one''' ) else: # compute weighted loss UpperCamelCase : Optional[int] = CrossEntropyLoss(ignore_index=self.config.loss_ignore_index ) UpperCamelCase : Tuple = loss_fct(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[Any] = loss_fct(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = main_loss + self.config.auxiliary_loss_weight * auxiliary_loss if not return_dict: if output_hidden_states: UpperCamelCase : Optional[Any] = (logits,) + outputs[1:] else: UpperCamelCase : int = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return SemanticSegmenterOutput( loss=__SCREAMING_SNAKE_CASE , logits=__SCREAMING_SNAKE_CASE , hidden_states=outputs.hidden_states , attentions=outputs.attentions , )
643
import requests from bsa import BeautifulSoup def a ( SCREAMING_SNAKE_CASE_ : str = "AAPL" ): """simple docstring""" UpperCamelCase : Dict = F"""https://in.finance.yahoo.com/quote/{symbol}?s={symbol}""" UpperCamelCase : Any = BeautifulSoup(requests.get(SCREAMING_SNAKE_CASE_ ).text , '''html.parser''' ) UpperCamelCase : Dict = '''My(6px) Pos(r) smartphone_Mt(6px)''' return soup.find('''div''' , class_=class_ ).find('''span''' ).text if __name__ == "__main__": for symbol in "AAPL AMZN IBM GOOG MSFT ORCL".split(): print(f'''Current {symbol:<4} stock price is {stock_price(symbol):>8}''')
643
1
import unittest from diffusers.pipelines.pipeline_utils import is_safetensors_compatible class UpperCAmelCase_ ( unittest.TestCase): '''simple docstring''' def _lowercase ( self ): """simple docstring""" UpperCamelCase : str = [ '''safety_checker/pytorch_model.bin''', '''safety_checker/model.safetensors''', '''vae/diffusion_pytorch_model.bin''', '''vae/diffusion_pytorch_model.safetensors''', '''text_encoder/pytorch_model.bin''', '''text_encoder/model.safetensors''', '''unet/diffusion_pytorch_model.bin''', '''unet/diffusion_pytorch_model.safetensors''', ] self.assertTrue(is_safetensors_compatible(__SCREAMING_SNAKE_CASE ) ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Optional[int] = [ '''unet/diffusion_pytorch_model.bin''', '''unet/diffusion_pytorch_model.safetensors''', ] self.assertTrue(is_safetensors_compatible(__SCREAMING_SNAKE_CASE ) ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Dict = [ '''safety_checker/pytorch_model.bin''', '''safety_checker/model.safetensors''', '''vae/diffusion_pytorch_model.bin''', '''vae/diffusion_pytorch_model.safetensors''', '''text_encoder/pytorch_model.bin''', '''text_encoder/model.safetensors''', '''unet/diffusion_pytorch_model.bin''', # Removed: 'unet/diffusion_pytorch_model.safetensors', ] self.assertFalse(is_safetensors_compatible(__SCREAMING_SNAKE_CASE ) ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[str] = [ '''text_encoder/pytorch_model.bin''', '''text_encoder/model.safetensors''', ] self.assertTrue(is_safetensors_compatible(__SCREAMING_SNAKE_CASE ) ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : int = [ '''safety_checker/pytorch_model.bin''', '''safety_checker/model.safetensors''', '''vae/diffusion_pytorch_model.bin''', '''vae/diffusion_pytorch_model.safetensors''', '''text_encoder/pytorch_model.bin''', # Removed: 'text_encoder/model.safetensors', '''unet/diffusion_pytorch_model.bin''', '''unet/diffusion_pytorch_model.safetensors''', ] self.assertFalse(is_safetensors_compatible(__SCREAMING_SNAKE_CASE ) ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Optional[int] = [ '''safety_checker/pytorch_model.fp16.bin''', '''safety_checker/model.fp16.safetensors''', '''vae/diffusion_pytorch_model.fp16.bin''', '''vae/diffusion_pytorch_model.fp16.safetensors''', '''text_encoder/pytorch_model.fp16.bin''', '''text_encoder/model.fp16.safetensors''', '''unet/diffusion_pytorch_model.fp16.bin''', '''unet/diffusion_pytorch_model.fp16.safetensors''', ] UpperCamelCase : List[Any] = '''fp16''' self.assertTrue(is_safetensors_compatible(__SCREAMING_SNAKE_CASE , variant=__SCREAMING_SNAKE_CASE ) ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[Any] = [ '''unet/diffusion_pytorch_model.fp16.bin''', '''unet/diffusion_pytorch_model.fp16.safetensors''', ] UpperCamelCase : str = '''fp16''' self.assertTrue(is_safetensors_compatible(__SCREAMING_SNAKE_CASE , variant=__SCREAMING_SNAKE_CASE ) ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Union[str, Any] = [ '''unet/diffusion_pytorch_model.bin''', '''unet/diffusion_pytorch_model.safetensors''', ] UpperCamelCase : Optional[int] = '''fp16''' self.assertTrue(is_safetensors_compatible(__SCREAMING_SNAKE_CASE , variant=__SCREAMING_SNAKE_CASE ) ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[str] = [ '''safety_checker/pytorch_model.fp16.bin''', '''safety_checker/model.fp16.safetensors''', '''vae/diffusion_pytorch_model.fp16.bin''', '''vae/diffusion_pytorch_model.fp16.safetensors''', '''text_encoder/pytorch_model.fp16.bin''', '''text_encoder/model.fp16.safetensors''', '''unet/diffusion_pytorch_model.fp16.bin''', # Removed: 'unet/diffusion_pytorch_model.fp16.safetensors', ] UpperCamelCase : List[str] = '''fp16''' self.assertFalse(is_safetensors_compatible(__SCREAMING_SNAKE_CASE , variant=__SCREAMING_SNAKE_CASE ) ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[str] = [ '''text_encoder/pytorch_model.fp16.bin''', '''text_encoder/model.fp16.safetensors''', ] UpperCamelCase : int = '''fp16''' self.assertTrue(is_safetensors_compatible(__SCREAMING_SNAKE_CASE , variant=__SCREAMING_SNAKE_CASE ) ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Optional[Any] = [ '''text_encoder/pytorch_model.bin''', '''text_encoder/model.safetensors''', ] UpperCamelCase : Any = '''fp16''' self.assertTrue(is_safetensors_compatible(__SCREAMING_SNAKE_CASE , variant=__SCREAMING_SNAKE_CASE ) ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : str = [ '''safety_checker/pytorch_model.fp16.bin''', '''safety_checker/model.fp16.safetensors''', '''vae/diffusion_pytorch_model.fp16.bin''', '''vae/diffusion_pytorch_model.fp16.safetensors''', '''text_encoder/pytorch_model.fp16.bin''', # 'text_encoder/model.fp16.safetensors', '''unet/diffusion_pytorch_model.fp16.bin''', '''unet/diffusion_pytorch_model.fp16.safetensors''', ] UpperCamelCase : Any = '''fp16''' self.assertFalse(is_safetensors_compatible(__SCREAMING_SNAKE_CASE , variant=__SCREAMING_SNAKE_CASE ) )
643
def a ( SCREAMING_SNAKE_CASE_ : int ): """simple docstring""" if number > 0: raise ValueError('''input must be a negative integer''' ) UpperCamelCase : List[str] = len(bin(SCREAMING_SNAKE_CASE_ )[3:] ) UpperCamelCase : List[str] = bin(abs(SCREAMING_SNAKE_CASE_ ) - (1 << binary_number_length) )[3:] UpperCamelCase : Dict = ( ( '''1''' + '''0''' * (binary_number_length - len(SCREAMING_SNAKE_CASE_ )) + twos_complement_number ) if number < 0 else '''0''' ) return "0b" + twos_complement_number if __name__ == "__main__": import doctest doctest.testmod()
643
1
import math import flax.linen as nn import jax.numpy as jnp def a ( SCREAMING_SNAKE_CASE_ : jnp.ndarray , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : float = 1 , SCREAMING_SNAKE_CASE_ : float = 1 , SCREAMING_SNAKE_CASE_ : float = 1.0E4 , SCREAMING_SNAKE_CASE_ : bool = False , SCREAMING_SNAKE_CASE_ : float = 1.0 , ): """simple docstring""" assert timesteps.ndim == 1, "Timesteps should be a 1d-array" assert embedding_dim % 2 == 0, F"""Embedding dimension {embedding_dim} should be even""" UpperCamelCase : Dict = float(embedding_dim // 2 ) UpperCamelCase : Union[str, Any] = math.log(max_timescale / min_timescale ) / (num_timescales - freq_shift) UpperCamelCase : Tuple = min_timescale * jnp.exp(jnp.arange(SCREAMING_SNAKE_CASE_ , dtype=jnp.floataa ) * -log_timescale_increment ) UpperCamelCase : List[str] = jnp.expand_dims(SCREAMING_SNAKE_CASE_ , 1 ) * jnp.expand_dims(SCREAMING_SNAKE_CASE_ , 0 ) # scale embeddings UpperCamelCase : Optional[Any] = scale * emb if flip_sin_to_cos: UpperCamelCase : Union[str, Any] = jnp.concatenate([jnp.cos(SCREAMING_SNAKE_CASE_ ), jnp.sin(SCREAMING_SNAKE_CASE_ )] , axis=1 ) else: UpperCamelCase : Union[str, Any] = jnp.concatenate([jnp.sin(SCREAMING_SNAKE_CASE_ ), jnp.cos(SCREAMING_SNAKE_CASE_ )] , axis=1 ) UpperCamelCase : Dict = jnp.reshape(SCREAMING_SNAKE_CASE_ , [jnp.shape(SCREAMING_SNAKE_CASE_ )[0], embedding_dim] ) return signal class UpperCAmelCase_ ( nn.Module): '''simple docstring''' __UpperCamelCase : int = 32 __UpperCamelCase : jnp.dtype = jnp.floataa @nn.compact def __call__( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : int = nn.Dense(self.time_embed_dim , dtype=self.dtype , name='''linear_1''' )(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = nn.silu(__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = nn.Dense(self.time_embed_dim , dtype=self.dtype , name='''linear_2''' )(__SCREAMING_SNAKE_CASE ) return temb class UpperCAmelCase_ ( nn.Module): '''simple docstring''' __UpperCamelCase : int = 32 __UpperCamelCase : bool = False __UpperCamelCase : float = 1 @nn.compact def __call__( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" return get_sinusoidal_embeddings( __SCREAMING_SNAKE_CASE , embedding_dim=self.dim , flip_sin_to_cos=self.flip_sin_to_cos , freq_shift=self.freq_shift )
643
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging __UpperCAmelCase : str = logging.get_logger(__name__) __UpperCAmelCase : Dict = { "hustvl/yolos-small": "https://huggingface.co/hustvl/yolos-small/resolve/main/config.json", # See all YOLOS models at https://huggingface.co/models?filter=yolos } class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : Optional[int] = "yolos" def __init__( self , __SCREAMING_SNAKE_CASE=768 , __SCREAMING_SNAKE_CASE=12 , __SCREAMING_SNAKE_CASE=12 , __SCREAMING_SNAKE_CASE=3_072 , __SCREAMING_SNAKE_CASE="gelu" , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=0.02 , __SCREAMING_SNAKE_CASE=1e-12 , __SCREAMING_SNAKE_CASE=[512, 864] , __SCREAMING_SNAKE_CASE=16 , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=100 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=1 , __SCREAMING_SNAKE_CASE=5 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=5 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=0.1 , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" super().__init__(**__SCREAMING_SNAKE_CASE ) UpperCamelCase : Tuple = hidden_size UpperCamelCase : List[Any] = num_hidden_layers UpperCamelCase : int = num_attention_heads UpperCamelCase : Dict = intermediate_size UpperCamelCase : Dict = hidden_act UpperCamelCase : int = hidden_dropout_prob UpperCamelCase : Any = attention_probs_dropout_prob UpperCamelCase : Optional[Any] = initializer_range UpperCamelCase : List[Any] = layer_norm_eps UpperCamelCase : int = image_size UpperCamelCase : Any = patch_size UpperCamelCase : str = num_channels UpperCamelCase : str = qkv_bias UpperCamelCase : Tuple = num_detection_tokens UpperCamelCase : List[Any] = use_mid_position_embeddings UpperCamelCase : Dict = auxiliary_loss # Hungarian matcher UpperCamelCase : Optional[Any] = class_cost UpperCamelCase : Union[str, Any] = bbox_cost UpperCamelCase : Any = giou_cost # Loss coefficients UpperCamelCase : List[Any] = bbox_loss_coefficient UpperCamelCase : Union[str, Any] = giou_loss_coefficient UpperCamelCase : Dict = eos_coefficient class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : List[str] = version.parse("1.11") @property def _lowercase ( self ): """simple docstring""" return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ] ) @property def _lowercase ( self ): """simple docstring""" return 1e-4 @property def _lowercase ( self ): """simple docstring""" return 12
643
1
def a ( SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int ): """simple docstring""" return 1 if input_a == input_a else 0 def a ( ): """simple docstring""" assert xnor_gate(0 , 0 ) == 1 assert xnor_gate(0 , 1 ) == 0 assert xnor_gate(1 , 0 ) == 0 assert xnor_gate(1 , 1 ) == 1 if __name__ == "__main__": print(xnor_gate(0, 0)) print(xnor_gate(0, 1)) print(xnor_gate(1, 0)) print(xnor_gate(1, 1))
643
def a ( SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int ): """simple docstring""" if a < 0 or b < 0: raise ValueError('''the value of both inputs must be positive''' ) UpperCamelCase : int = str(bin(SCREAMING_SNAKE_CASE_ ) )[2:] # remove the leading "0b" UpperCamelCase : List[str] = str(bin(SCREAMING_SNAKE_CASE_ ) )[2:] UpperCamelCase : Tuple = max(len(SCREAMING_SNAKE_CASE_ ) , len(SCREAMING_SNAKE_CASE_ ) ) return "0b" + "".join( str(int('''1''' in (char_a, char_b) ) ) for char_a, char_b in zip(a_binary.zfill(SCREAMING_SNAKE_CASE_ ) , b_binary.zfill(SCREAMING_SNAKE_CASE_ ) ) ) if __name__ == "__main__": import doctest doctest.testmod()
643
1
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 __UpperCAmelCase : Optional[List[str]] = None __UpperCAmelCase : List[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 __UpperCAmelCase : Optional[int] = [ 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 UpperCAmelCase_ : '''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=_a, repr=_a) def __call__( self ): """simple docstring""" return self.pa_type def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError('''To support encoding images, please install \'Pillow\'.''' ) if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): UpperCamelCase : List[str] = np.array(__SCREAMING_SNAKE_CASE ) if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): return {"path": value, "bytes": None} elif isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): return {"path": None, "bytes": value} elif isinstance(__SCREAMING_SNAKE_CASE , np.ndarray ): # convert the image array to PNG/TIFF bytes return encode_np_array(__SCREAMING_SNAKE_CASE ) elif isinstance(__SCREAMING_SNAKE_CASE , PIL.Image.Image ): # convert the PIL image to bytes (default format is PNG/TIFF) return encode_pil_image(__SCREAMING_SNAKE_CASE ) 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 _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None ): """simple docstring""" 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 : Union[str, Any] = {} UpperCamelCase , UpperCamelCase : Union[str, 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(__SCREAMING_SNAKE_CASE ): UpperCamelCase : str = PIL.Image.open(__SCREAMING_SNAKE_CASE ) else: UpperCamelCase : Dict = path.split('''::''' )[-1] try: UpperCamelCase : List[Any] = string_to_dict(__SCREAMING_SNAKE_CASE , config.HUB_DATASETS_URL )['''repo_id'''] UpperCamelCase : List[str] = token_per_repo_id.get(__SCREAMING_SNAKE_CASE ) except ValueError: UpperCamelCase : List[str] = None with xopen(__SCREAMING_SNAKE_CASE , '''rb''' , use_auth_token=__SCREAMING_SNAKE_CASE ) as f: UpperCamelCase : Optional[Any] = BytesIO(f.read() ) UpperCamelCase : Tuple = PIL.Image.open(bytes_ ) else: UpperCamelCase : List[Any] = PIL.Image.open(BytesIO(bytes_ ) ) image.load() # to avoid "Too many open files" errors return image def _lowercase ( self ): """simple docstring""" from .features import Value return ( self if self.decode else { "bytes": Value('''binary''' ), "path": Value('''string''' ), } ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if pa.types.is_string(storage.type ): UpperCamelCase : List[str] = pa.array([None] * len(__SCREAMING_SNAKE_CASE ) , type=pa.binary() ) UpperCamelCase : Dict = pa.StructArray.from_arrays([bytes_array, storage] , ['''bytes''', '''path'''] , mask=storage.is_null() ) elif pa.types.is_binary(storage.type ): UpperCamelCase : List[str] = pa.array([None] * len(__SCREAMING_SNAKE_CASE ) , type=pa.string() ) UpperCamelCase : List[str] = 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 : Optional[int] = storage.field('''bytes''' ) else: UpperCamelCase : Dict = pa.array([None] * len(__SCREAMING_SNAKE_CASE ) , type=pa.binary() ) if storage.type.get_field_index('''path''' ) >= 0: UpperCamelCase : str = storage.field('''path''' ) else: UpperCamelCase : List[str] = pa.array([None] * len(__SCREAMING_SNAKE_CASE ) , type=pa.string() ) UpperCamelCase : Union[str, Any] = pa.StructArray.from_arrays([bytes_array, path_array] , ['''bytes''', '''path'''] , mask=storage.is_null() ) elif pa.types.is_list(storage.type ): UpperCamelCase : str = pa.array( [encode_np_array(np.array(__SCREAMING_SNAKE_CASE ) )['''bytes'''] if arr is not None else None for arr in storage.to_pylist()] , type=pa.binary() , ) UpperCamelCase : Dict = pa.array([None] * len(__SCREAMING_SNAKE_CASE ) , type=pa.string() ) UpperCamelCase : Any = pa.StructArray.from_arrays( [bytes_array, path_array] , ['''bytes''', '''path'''] , mask=bytes_array.is_null() ) return array_cast(__SCREAMING_SNAKE_CASE , self.pa_type ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" @no_op_if_value_is_null def path_to_bytes(__SCREAMING_SNAKE_CASE ): with xopen(__SCREAMING_SNAKE_CASE , '''rb''' ) as f: UpperCamelCase : Optional[Any] = f.read() return bytes_ UpperCamelCase : List[Any] = 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 : List[Any] = pa.array( [os.path.basename(__SCREAMING_SNAKE_CASE ) if path is not None else None for path in storage.field('''path''' ).to_pylist()] , type=pa.string() , ) UpperCamelCase : List[str] = pa.StructArray.from_arrays([bytes_array, path_array] , ['''bytes''', '''path'''] , mask=bytes_array.is_null() ) return array_cast(__SCREAMING_SNAKE_CASE , self.pa_type ) def a ( ): """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 : Union[str, Any] = list(set(PIL.Image.OPEN.keys() ) & set(PIL.Image.SAVE.keys() ) ) return _IMAGE_COMPRESSION_FORMATS def a ( SCREAMING_SNAKE_CASE_ : "PIL.Image.Image" ): """simple docstring""" UpperCamelCase : Union[str, Any] = BytesIO() if image.format in list_image_compression_formats(): UpperCamelCase : List[Any] = image.format else: UpperCamelCase : str = '''PNG''' if image.mode in ['''1''', '''L''', '''LA''', '''RGB''', '''RGBA'''] else '''TIFF''' image.save(SCREAMING_SNAKE_CASE_ , format=SCREAMING_SNAKE_CASE_ ) return buffer.getvalue() def a ( SCREAMING_SNAKE_CASE_ : "PIL.Image.Image" ): """simple docstring""" if hasattr(SCREAMING_SNAKE_CASE_ , '''filename''' ) and image.filename != "": return {"path": image.filename, "bytes": None} else: return {"path": None, "bytes": image_to_bytes(SCREAMING_SNAKE_CASE_ )} def a ( SCREAMING_SNAKE_CASE_ : np.ndarray ): """simple docstring""" if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError('''To support encoding images, please install \'Pillow\'.''' ) UpperCamelCase : Dict = array.dtype UpperCamelCase : List[Any] = dtype.byteorder if dtype.byteorder != '''=''' else _NATIVE_BYTEORDER UpperCamelCase : List[Any] = dtype.kind UpperCamelCase : int = dtype.itemsize UpperCamelCase : Any = None # Multi-channel array case (only np.dtype("|u1") is allowed) if array.shape[2:]: UpperCamelCase : Optional[int] = 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 : str = 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 : Optional[int] = dtype_byteorder + dtype_kind + str(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Union[str, Any] = np.dtype(SCREAMING_SNAKE_CASE_ ) 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 : Tuple = PIL.Image.fromarray(array.astype(SCREAMING_SNAKE_CASE_ ) ) return {"path": None, "bytes": image_to_bytes(SCREAMING_SNAKE_CASE_ )} def a ( SCREAMING_SNAKE_CASE_ : Union[List[str], List[dict], List[np.ndarray], List["PIL.Image.Image"]] ): """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(SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): return [{"path": obj, "bytes": None} if obj is not None else None for obj in objs] if isinstance(SCREAMING_SNAKE_CASE_ , np.ndarray ): UpperCamelCase : Optional[int] = no_op_if_value_is_null(SCREAMING_SNAKE_CASE_ ) return [obj_to_image_dict_func(SCREAMING_SNAKE_CASE_ ) for obj in objs] elif isinstance(SCREAMING_SNAKE_CASE_ , PIL.Image.Image ): UpperCamelCase : Tuple = no_op_if_value_is_null(SCREAMING_SNAKE_CASE_ ) return [obj_to_image_dict_func(SCREAMING_SNAKE_CASE_ ) for obj in objs] else: return objs else: return objs
643
from itertools import product from cva import COLOR_BGR2GRAY, cvtColor, imread, imshow, waitKey from numpy import dot, exp, mgrid, pi, ravel, square, uinta, zeros def a ( SCREAMING_SNAKE_CASE_ : List[Any] , SCREAMING_SNAKE_CASE_ : List[str] ): """simple docstring""" UpperCamelCase : List[str] = k_size // 2 UpperCamelCase , UpperCamelCase : Optional[int] = mgrid[0 - center : k_size - center, 0 - center : k_size - center] UpperCamelCase : Dict = 1 / (2 * pi * sigma) * exp(-(square(SCREAMING_SNAKE_CASE_ ) + square(SCREAMING_SNAKE_CASE_ )) / (2 * square(SCREAMING_SNAKE_CASE_ )) ) return g def a ( SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : int ): """simple docstring""" UpperCamelCase , UpperCamelCase : Tuple = image.shape[0], image.shape[1] # dst image height and width UpperCamelCase : str = height - k_size + 1 UpperCamelCase : Optional[int] = width - k_size + 1 # im2col, turn the k_size*k_size pixels into a row and np.vstack all rows UpperCamelCase : List[Any] = zeros((dst_height * dst_width, k_size * k_size) ) UpperCamelCase : Tuple = 0 for i, j in product(range(SCREAMING_SNAKE_CASE_ ) , range(SCREAMING_SNAKE_CASE_ ) ): UpperCamelCase : Dict = ravel(image[i : i + k_size, j : j + k_size] ) UpperCamelCase : Dict = window row += 1 # turn the kernel into shape(k*k, 1) UpperCamelCase : Optional[int] = gen_gaussian_kernel(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) UpperCamelCase : List[Any] = ravel(SCREAMING_SNAKE_CASE_ ) # reshape and get the dst image UpperCamelCase : Optional[int] = dot(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ).reshape(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ).astype(SCREAMING_SNAKE_CASE_ ) return dst if __name__ == "__main__": # read original image __UpperCAmelCase : Union[str, Any] = imread(r"../image_data/lena.jpg") # turn image in gray scale value __UpperCAmelCase : Optional[Any] = cvtColor(img, COLOR_BGR2GRAY) # get values with two different mask size __UpperCAmelCase : Optional[int] = gaussian_filter(gray, 3, sigma=1) __UpperCAmelCase : List[Any] = gaussian_filter(gray, 5, sigma=0.8) # show result images imshow("gaussian filter with 3x3 mask", gaussianaxa) imshow("gaussian filter with 5x5 mask", gaussianaxa) waitKey()
643
1
from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, convert_to_rgb, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging __UpperCAmelCase : Optional[int] = logging.get_logger(__name__) if is_vision_available(): import PIL class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : Dict = ["pixel_values"] def __init__( self , __SCREAMING_SNAKE_CASE = True , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = PILImageResampling.BICUBIC , __SCREAMING_SNAKE_CASE = True , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = True , __SCREAMING_SNAKE_CASE = 1 / 255 , __SCREAMING_SNAKE_CASE = True , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = True , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" super().__init__(**__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[Any] = size if size is not None else {'''shortest_edge''': 224} UpperCamelCase : Dict = get_size_dict(__SCREAMING_SNAKE_CASE , default_to_square=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Any = crop_size if crop_size is not None else {'''height''': 224, '''width''': 224} UpperCamelCase : Dict = get_size_dict(__SCREAMING_SNAKE_CASE , default_to_square=__SCREAMING_SNAKE_CASE , param_name='''crop_size''' ) UpperCamelCase : Any = do_resize UpperCamelCase : Tuple = size UpperCamelCase : Optional[Any] = resample UpperCamelCase : Optional[Any] = do_center_crop UpperCamelCase : Any = crop_size UpperCamelCase : Dict = do_rescale UpperCamelCase : Optional[int] = rescale_factor UpperCamelCase : str = do_normalize UpperCamelCase : Union[str, Any] = image_mean if image_mean is not None else OPENAI_CLIP_MEAN UpperCamelCase : str = image_std if image_std is not None else OPENAI_CLIP_STD UpperCamelCase : Optional[Any] = do_convert_rgb def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = PILImageResampling.BICUBIC , __SCREAMING_SNAKE_CASE = None , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" UpperCamelCase : List[str] = get_size_dict(__SCREAMING_SNAKE_CASE , default_to_square=__SCREAMING_SNAKE_CASE ) if "shortest_edge" not in size: raise ValueError(f"""The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}""" ) UpperCamelCase : Optional[int] = get_resize_output_image_size(__SCREAMING_SNAKE_CASE , size=size['''shortest_edge'''] , default_to_square=__SCREAMING_SNAKE_CASE ) return resize(__SCREAMING_SNAKE_CASE , size=__SCREAMING_SNAKE_CASE , resample=__SCREAMING_SNAKE_CASE , data_format=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" UpperCamelCase : Tuple = get_size_dict(__SCREAMING_SNAKE_CASE ) if "height" not in size or "width" not in size: raise ValueError(f"""The `size` parameter must contain the keys (height, width). Got {size.keys()}""" ) return center_crop(__SCREAMING_SNAKE_CASE , size=(size['''height'''], size['''width''']) , data_format=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" return rescale(__SCREAMING_SNAKE_CASE , scale=__SCREAMING_SNAKE_CASE , data_format=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" return normalize(__SCREAMING_SNAKE_CASE , mean=__SCREAMING_SNAKE_CASE , std=__SCREAMING_SNAKE_CASE , data_format=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = ChannelDimension.FIRST , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" UpperCamelCase : int = do_resize if do_resize is not None else self.do_resize UpperCamelCase : Union[str, Any] = size if size is not None else self.size UpperCamelCase : int = get_size_dict(__SCREAMING_SNAKE_CASE , param_name='''size''' , default_to_square=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Any = resample if resample is not None else self.resample UpperCamelCase : str = do_center_crop if do_center_crop is not None else self.do_center_crop UpperCamelCase : Any = crop_size if crop_size is not None else self.crop_size UpperCamelCase : Union[str, Any] = get_size_dict(__SCREAMING_SNAKE_CASE , param_name='''crop_size''' , default_to_square=__SCREAMING_SNAKE_CASE ) UpperCamelCase : int = do_rescale if do_rescale is not None else self.do_rescale UpperCamelCase : Tuple = rescale_factor if rescale_factor is not None else self.rescale_factor UpperCamelCase : Optional[int] = do_normalize if do_normalize is not None else self.do_normalize UpperCamelCase : int = image_mean if image_mean is not None else self.image_mean UpperCamelCase : Tuple = image_std if image_std is not None else self.image_std UpperCamelCase : Union[str, Any] = do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb UpperCamelCase : Any = make_list_of_images(__SCREAMING_SNAKE_CASE ) if not valid_images(__SCREAMING_SNAKE_CASE ): raise ValueError( '''Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ''' '''torch.Tensor, tf.Tensor or jax.ndarray.''' ) if do_resize and size is None: raise ValueError('''Size must be specified if do_resize is True.''' ) if do_center_crop and crop_size is None: raise ValueError('''Crop size must be specified if do_center_crop is True.''' ) if do_rescale and rescale_factor is None: raise ValueError('''Rescale factor must be specified if do_rescale is True.''' ) if do_normalize and (image_mean is None or image_std is None): raise ValueError('''Image mean and std must be specified if do_normalize is True.''' ) # PIL RGBA images are converted to RGB if do_convert_rgb: UpperCamelCase : Any = [convert_to_rgb(__SCREAMING_SNAKE_CASE ) for image in images] # All transformations expect numpy arrays. UpperCamelCase : Tuple = [to_numpy_array(__SCREAMING_SNAKE_CASE ) for image in images] if do_resize: UpperCamelCase : Union[str, Any] = [self.resize(image=__SCREAMING_SNAKE_CASE , size=__SCREAMING_SNAKE_CASE , resample=__SCREAMING_SNAKE_CASE ) for image in images] if do_center_crop: UpperCamelCase : str = [self.center_crop(image=__SCREAMING_SNAKE_CASE , size=__SCREAMING_SNAKE_CASE ) for image in images] if do_rescale: UpperCamelCase : str = [self.rescale(image=__SCREAMING_SNAKE_CASE , scale=__SCREAMING_SNAKE_CASE ) for image in images] if do_normalize: UpperCamelCase : Optional[int] = [self.normalize(image=__SCREAMING_SNAKE_CASE , mean=__SCREAMING_SNAKE_CASE , std=__SCREAMING_SNAKE_CASE ) for image in images] UpperCamelCase : Any = [to_channel_dimension_format(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) for image in images] UpperCamelCase : List[str] = {'''pixel_values''': images} return BatchFeature(data=__SCREAMING_SNAKE_CASE , tensor_type=__SCREAMING_SNAKE_CASE )
643
from .imports import is_tqdm_available if is_tqdm_available(): from tqdm.auto import tqdm as _tqdm from ..state import PartialState def a ( SCREAMING_SNAKE_CASE_ : bool = True , *SCREAMING_SNAKE_CASE_ : List[str] , **SCREAMING_SNAKE_CASE_ : Tuple ): """simple docstring""" if not is_tqdm_available(): raise ImportError('''Accelerate\'s `tqdm` module requires `tqdm` to be installed. Please run `pip install tqdm`.''' ) UpperCamelCase : int = False if main_process_only: UpperCamelCase : int = PartialState().local_process_index == 0 return _tqdm(*SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , disable=SCREAMING_SNAKE_CASE_ )
643
1
from torch import nn def a ( SCREAMING_SNAKE_CASE_ : Union[str, Any] ): """simple docstring""" if act_fn in ["swish", "silu"]: return nn.SiLU() elif act_fn == "mish": return nn.Mish() elif act_fn == "gelu": return nn.GELU() else: raise ValueError(F"""Unsupported activation function: {act_fn}""" )
643
import io import os import unicodedata from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging __UpperCAmelCase : Any = logging.get_logger(__name__) __UpperCAmelCase : int = "▁" __UpperCAmelCase : Tuple = {"vocab_file": "vocab.txt", "sentencepiece_model_ckpt": "sentencepiece.bpe.model"} __UpperCAmelCase : Dict = { "sentencepiece_model_file": "sentencepiece.bpe.model", "vocab_file": "vocab.txt", } __UpperCAmelCase : Dict = { "vocab_file": { "ernie-m-base": "https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/vocab.txt", "ernie-m-large": "https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/vocab.txt", }, "sentencepiece_model_file": { "ernie-m-base": "https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/sentencepiece.bpe.model", "ernie-m-large": "https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/sentencepiece.bpe.model", }, } __UpperCAmelCase : str = { "ernie-m-base": 514, "ernie-m-large": 514, } __UpperCAmelCase : Optional[int] = { "ernie-m-base": {"do_lower_case": False}, "ernie-m-large": {"do_lower_case": False}, } class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : List[str] = ["input_ids"] __UpperCamelCase : List[str] = VOCAB_FILES_NAMES __UpperCamelCase : List[Any] = PRETRAINED_INIT_CONFIGURATION __UpperCamelCase : List[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCamelCase : int = PRETRAINED_VOCAB_FILES_MAP __UpperCamelCase : List[str] = RESOURCE_FILES_NAMES def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE="utf8" , __SCREAMING_SNAKE_CASE="[UNK]" , __SCREAMING_SNAKE_CASE="[SEP]" , __SCREAMING_SNAKE_CASE="[PAD]" , __SCREAMING_SNAKE_CASE="[CLS]" , __SCREAMING_SNAKE_CASE="[MASK]" , __SCREAMING_SNAKE_CASE = None , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" UpperCamelCase : int = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( do_lower_case=__SCREAMING_SNAKE_CASE , unk_token=__SCREAMING_SNAKE_CASE , sep_token=__SCREAMING_SNAKE_CASE , pad_token=__SCREAMING_SNAKE_CASE , cls_token=__SCREAMING_SNAKE_CASE , mask_token=__SCREAMING_SNAKE_CASE , vocab_file=__SCREAMING_SNAKE_CASE , encoding=__SCREAMING_SNAKE_CASE , sp_model_kwargs=self.sp_model_kwargs , **__SCREAMING_SNAKE_CASE , ) UpperCamelCase : List[str] = do_lower_case UpperCamelCase : Dict = sentencepiece_model_ckpt UpperCamelCase : str = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(__SCREAMING_SNAKE_CASE ) # to mimic paddlenlp.transformers.ernie_m.tokenizer.ErnieMTokenizer functioning if vocab_file is not None: UpperCamelCase : Optional[Any] = self.load_vocab(filepath=__SCREAMING_SNAKE_CASE ) else: UpperCamelCase : int = {self.sp_model.id_to_piece(__SCREAMING_SNAKE_CASE ): id for id in range(self.sp_model.get_piece_size() )} UpperCamelCase : str = {v: k for k, v in self.vocab.items()} def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if text is None: return None UpperCamelCase : str = self.tokenize(__SCREAMING_SNAKE_CASE ) UpperCamelCase , UpperCamelCase : str = '''''', [] for i, ch in enumerate(__SCREAMING_SNAKE_CASE ): if ch in self.SP_CHAR_MAPPING: UpperCamelCase : Optional[int] = self.SP_CHAR_MAPPING.get(__SCREAMING_SNAKE_CASE ) else: UpperCamelCase : Optional[Any] = unicodedata.normalize('''NFKC''' , __SCREAMING_SNAKE_CASE ) if self.is_whitespace(__SCREAMING_SNAKE_CASE ): continue normalized_text += ch char_mapping.extend([i] * len(__SCREAMING_SNAKE_CASE ) ) UpperCamelCase , UpperCamelCase , UpperCamelCase : Tuple = normalized_text, [], 0 if self.do_lower_case: UpperCamelCase : Tuple = text.lower() for token in split_tokens: if token[:1] == "▁": UpperCamelCase : Any = token[1:] UpperCamelCase : Optional[int] = text[offset:].index(__SCREAMING_SNAKE_CASE ) + offset UpperCamelCase : List[Any] = start + len(__SCREAMING_SNAKE_CASE ) token_mapping.append((char_mapping[start], char_mapping[end - 1] + 1) ) UpperCamelCase : str = end return token_mapping @property def _lowercase ( self ): """simple docstring""" return len(self.vocab ) def _lowercase ( self ): """simple docstring""" return dict(self.vocab , **self.added_tokens_encoder ) def __getstate__( self ): """simple docstring""" UpperCamelCase : Optional[Any] = self.__dict__.copy() UpperCamelCase : str = None return state def __setstate__( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Tuple = d # for backward compatibility if not hasattr(self , '''sp_model_kwargs''' ): UpperCamelCase : Optional[int] = {} UpperCamelCase : Optional[Any] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.sentencepiece_model_ckpt ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" return "".join((self.SP_CHAR_MAPPING.get(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) for c in text) ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=64 , __SCREAMING_SNAKE_CASE=0.1 ): """simple docstring""" if self.sp_model_kwargs.get('''enable_sampling''' ) is True: UpperCamelCase : List[str] = True if self.sp_model_kwargs.get('''alpha''' ) is not None: UpperCamelCase : Any = self.sp_model_kwargs.get('''alpha''' ) if self.sp_model_kwargs.get('''nbest_size''' ) is not None: UpperCamelCase : Tuple = self.sp_model_kwargs.get('''nbest_size''' ) if not enable_sampling: UpperCamelCase : int = self.sp_model.EncodeAsPieces(__SCREAMING_SNAKE_CASE ) else: UpperCamelCase : Optional[Any] = self.sp_model.SampleEncodeAsPieces(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = [] for pi, piece in enumerate(__SCREAMING_SNAKE_CASE ): if piece == SPIECE_UNDERLINE: if not pieces[pi + 1].startswith(__SCREAMING_SNAKE_CASE ) and pi != 0: new_pieces.append(__SCREAMING_SNAKE_CASE ) continue else: continue UpperCamelCase : Any = 0 for i, chunk in enumerate(__SCREAMING_SNAKE_CASE ): if chunk == SPIECE_UNDERLINE: continue if self.is_ch_char(__SCREAMING_SNAKE_CASE ) or self.is_punct(__SCREAMING_SNAKE_CASE ): if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE: new_pieces.append(piece[lst_i:i] ) new_pieces.append(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Dict = i + 1 elif chunk.isdigit() and i > 0 and not piece[i - 1].isdigit(): if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE: new_pieces.append(piece[lst_i:i] ) UpperCamelCase : Union[str, Any] = i elif not chunk.isdigit() and i > 0 and piece[i - 1].isdigit(): if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE: new_pieces.append(piece[lst_i:i] ) UpperCamelCase : Any = i if len(__SCREAMING_SNAKE_CASE ) > lst_i: new_pieces.append(piece[lst_i:] ) return new_pieces def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Optional[int] = ''''''.join(__SCREAMING_SNAKE_CASE ).replace(__SCREAMING_SNAKE_CASE , ''' ''' ).strip() return out_string def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : int = self.convert_ids_to_tokens(__SCREAMING_SNAKE_CASE ) UpperCamelCase : int = ''''''.join(__SCREAMING_SNAKE_CASE ).replace(__SCREAMING_SNAKE_CASE , ''' ''' ).strip() return out_string def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" return self.vocab.get(__SCREAMING_SNAKE_CASE , self.vocab.get(self.unk_token ) ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" return self.reverse_vocab.get(__SCREAMING_SNAKE_CASE , self.unk_token ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None ): """simple docstring""" if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] UpperCamelCase : Any = [self.cls_token_id] UpperCamelCase : str = [self.sep_token_id] return _cls + token_ids_a + _sep + _sep + token_ids_a + _sep def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None ): """simple docstring""" if offset_mapping_a is None: return [(0, 0)] + offset_mapping_a + [(0, 0)] return [(0, 0)] + offset_mapping_a + [(0, 0), (0, 0)] + offset_mapping_a + [(0, 0)] def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=False ): """simple docstring""" if already_has_special_tokens: if token_ids_a is not None: raise ValueError( '''You should not supply a second sequence if the provided sequence of ''' '''ids is already formatted with special tokens for the model.''' ) return [1 if x in [self.sep_token_id, self.cls_token_id] else 0 for x in token_ids_a] if token_ids_a is not None: return [1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1, 1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1] return [1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1] def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ): """simple docstring""" if token_ids_a is None: # [CLS] X [SEP] return (len(__SCREAMING_SNAKE_CASE ) + 2) * [0] # [CLS] A [SEP] [SEP] B [SEP] return [0] * (len(__SCREAMING_SNAKE_CASE ) + 1) + [1] * (len(__SCREAMING_SNAKE_CASE ) + 3) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if "\u4e00" <= char <= "\u9fff": return True return False def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if ("a" <= char <= "z") or ("A" <= char <= "Z"): return True return False def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if char in ",;:.?!~,;:。?!《》【】": return True return False def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if char == " " or char == "\t" or char == "\n" or char == "\r": return True if len(__SCREAMING_SNAKE_CASE ) == 1: UpperCamelCase : Optional[int] = unicodedata.category(__SCREAMING_SNAKE_CASE ) if cat == "Zs": return True return False def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : int = {} with io.open(__SCREAMING_SNAKE_CASE , '''r''' , encoding='''utf-8''' ) as f: for index, line in enumerate(__SCREAMING_SNAKE_CASE ): UpperCamelCase : Tuple = line.rstrip('''\n''' ) UpperCamelCase : List[Any] = int(__SCREAMING_SNAKE_CASE ) return token_to_idx def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ): """simple docstring""" UpperCamelCase : Union[str, Any] = 0 if os.path.isdir(__SCREAMING_SNAKE_CASE ): UpperCamelCase : Dict = os.path.join( __SCREAMING_SNAKE_CASE , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) else: UpperCamelCase : Union[str, Any] = (filename_prefix + '''-''' if filename_prefix else '''''') + save_directory with open(__SCREAMING_SNAKE_CASE , '''w''' , encoding='''utf-8''' ) as writer: for token, token_index in sorted(self.vocab.items() , key=lambda __SCREAMING_SNAKE_CASE : kv[1] ): if index != token_index: logger.warning( f"""Saving vocabulary to {vocab_file}: vocabulary indices are not consecutive.""" ''' Please check that the vocabulary is not corrupted!''' ) UpperCamelCase : List[Any] = token_index writer.write(token + '''\n''' ) index += 1 UpperCamelCase : Tuple = os.path.join(__SCREAMING_SNAKE_CASE , '''sentencepiece.bpe.model''' ) with open(__SCREAMING_SNAKE_CASE , '''wb''' ) as fi: UpperCamelCase : List[Any] = self.sp_model.serialized_model_proto() fi.write(__SCREAMING_SNAKE_CASE ) return (vocab_file,)
643
1
from __future__ import annotations import unittest from transformers import BlenderbotConfig, BlenderbotTokenizer, is_tf_available from transformers.testing_utils import require_tf, require_tokenizers, slow from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFAutoModelForSeqaSeqLM, TFBlenderbotForConditionalGeneration, TFBlenderbotModel @require_tf class UpperCAmelCase_ : '''simple docstring''' __UpperCamelCase : str = BlenderbotConfig __UpperCamelCase : str = {} __UpperCamelCase : List[Any] = "gelu" def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=13 , __SCREAMING_SNAKE_CASE=7 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=99 , __SCREAMING_SNAKE_CASE=32 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=4 , __SCREAMING_SNAKE_CASE=37 , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=20 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=1 , __SCREAMING_SNAKE_CASE=0 , ): """simple docstring""" UpperCamelCase : Tuple = parent UpperCamelCase : str = batch_size UpperCamelCase : Dict = seq_length UpperCamelCase : List[str] = is_training UpperCamelCase : int = use_labels UpperCamelCase : Optional[int] = vocab_size UpperCamelCase : Any = hidden_size UpperCamelCase : Tuple = num_hidden_layers UpperCamelCase : Dict = num_attention_heads UpperCamelCase : List[str] = intermediate_size UpperCamelCase : List[Any] = hidden_dropout_prob UpperCamelCase : List[str] = attention_probs_dropout_prob UpperCamelCase : int = max_position_embeddings UpperCamelCase : List[str] = eos_token_id UpperCamelCase : str = pad_token_id UpperCamelCase : Tuple = bos_token_id def _lowercase ( self ): """simple docstring""" UpperCamelCase : Optional[int] = ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size ) UpperCamelCase : int = tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size ) , 1 ) UpperCamelCase : Any = tf.concat([input_ids, eos_tensor] , axis=1 ) UpperCamelCase : Optional[int] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCamelCase : Dict = self.config_cls( vocab_size=self.vocab_size , d_model=self.hidden_size , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , eos_token_ids=[2] , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , decoder_start_token_id=self.pad_token_id , **self.config_updates , ) UpperCamelCase : Union[str, Any] = prepare_blenderbot_inputs_dict(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) return config, inputs_dict def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Any = TFBlenderbotModel(config=__SCREAMING_SNAKE_CASE ).get_decoder() UpperCamelCase : List[str] = inputs_dict['''input_ids'''] UpperCamelCase : List[Any] = input_ids[:1, :] UpperCamelCase : Tuple = inputs_dict['''attention_mask'''][:1, :] UpperCamelCase : Optional[int] = inputs_dict['''head_mask'''] UpperCamelCase : Tuple = 1 # first forward pass UpperCamelCase : Tuple = model(__SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE , head_mask=__SCREAMING_SNAKE_CASE , use_cache=__SCREAMING_SNAKE_CASE ) UpperCamelCase , UpperCamelCase : Optional[int] = outputs.to_tuple() # create hypothetical next token and extent to next_input_ids UpperCamelCase : Tuple = ids_tensor((self.batch_size, 3) , config.vocab_size ) UpperCamelCase : Any = tf.cast(ids_tensor((self.batch_size, 3) , 2 ) , tf.inta ) # append to next input_ids and UpperCamelCase : Dict = tf.concat([input_ids, next_tokens] , axis=-1 ) UpperCamelCase : Union[str, Any] = tf.concat([attention_mask, next_attn_mask] , axis=-1 ) UpperCamelCase : Union[str, Any] = model(__SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE )[0] UpperCamelCase : Optional[Any] = model(__SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE , past_key_values=__SCREAMING_SNAKE_CASE )[0] self.parent.assertEqual(next_tokens.shape[1] , output_from_past.shape[1] ) # select random slice UpperCamelCase : int = int(ids_tensor((1,) , output_from_past.shape[-1] ) ) UpperCamelCase : Tuple = output_from_no_past[:, -3:, random_slice_idx] UpperCamelCase : str = output_from_past[:, :, random_slice_idx] # test that outputs are equal for slice tf.debugging.assert_near(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , rtol=1e-3 ) def a ( SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : List[str] , SCREAMING_SNAKE_CASE_ : List[str]=None , SCREAMING_SNAKE_CASE_ : int=None , SCREAMING_SNAKE_CASE_ : List[Any]=None , SCREAMING_SNAKE_CASE_ : List[Any]=None , SCREAMING_SNAKE_CASE_ : Union[str, Any]=None , ): """simple docstring""" if attention_mask is None: UpperCamelCase : Dict = tf.cast(tf.math.not_equal(SCREAMING_SNAKE_CASE_ , config.pad_token_id ) , tf.inta ) if decoder_attention_mask is None: UpperCamelCase : Optional[int] = tf.concat( [ tf.ones(decoder_input_ids[:, :1].shape , dtype=tf.inta ), tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:] , config.pad_token_id ) , tf.inta ), ] , axis=-1 , ) if head_mask is None: UpperCamelCase : Union[str, Any] = tf.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: UpperCamelCase : str = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) if cross_attn_head_mask is None: UpperCamelCase : List[str] = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": decoder_attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } @require_tf class UpperCAmelCase_ ( _a, _a, unittest.TestCase): '''simple docstring''' __UpperCamelCase : Optional[int] = (TFBlenderbotForConditionalGeneration, TFBlenderbotModel) if is_tf_available() else () __UpperCamelCase : Optional[Any] = (TFBlenderbotForConditionalGeneration,) if is_tf_available() else () __UpperCamelCase : Union[str, Any] = ( { "conversational": TFBlenderbotForConditionalGeneration, "feature-extraction": TFBlenderbotModel, "summarization": TFBlenderbotForConditionalGeneration, "text2text-generation": TFBlenderbotForConditionalGeneration, "translation": TFBlenderbotForConditionalGeneration, } if is_tf_available() else {} ) __UpperCamelCase : Tuple = True __UpperCamelCase : List[str] = False __UpperCamelCase : List[str] = False def _lowercase ( self ): """simple docstring""" UpperCamelCase : int = TFBlenderbotModelTester(self ) UpperCamelCase : Any = ConfigTester(self , config_class=__SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" self.config_tester.run_common_tests() def _lowercase ( self ): """simple docstring""" UpperCamelCase : Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_decoder_model_past_large_inputs(*__SCREAMING_SNAKE_CASE ) @require_tokenizers @require_tf class UpperCAmelCase_ ( unittest.TestCase): '''simple docstring''' __UpperCamelCase : Optional[Any] = ["My friends are cool but they eat too many carbs."] __UpperCamelCase : Any = "facebook/blenderbot-400M-distill" @cached_property def _lowercase ( self ): """simple docstring""" return BlenderbotTokenizer.from_pretrained(self.model_name ) @cached_property def _lowercase ( self ): """simple docstring""" UpperCamelCase : Optional[int] = TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name ) return model @slow def _lowercase ( self ): """simple docstring""" UpperCamelCase : int = self.tokenizer(self.src_text , return_tensors='''tf''' ) UpperCamelCase : Optional[Any] = self.model.generate( model_inputs.input_ids , ) UpperCamelCase : int = self.tokenizer.batch_decode(generated_ids.numpy() , skip_special_tokens=__SCREAMING_SNAKE_CASE )[0] assert ( generated_words == " That's unfortunate. Are they trying to lose weight or are they just trying to be healthier?" )
643
from typing import List, Optional, Tuple, Union import torch from torch import nn from torch.nn import CrossEntropyLoss from ... import AutoBackbone from ...modeling_outputs import SemanticSegmenterOutput from ...modeling_utils import PreTrainedModel from ...utils import add_start_docstrings, add_start_docstrings_to_model_forward, replace_return_docstrings from ...utils.backbone_utils import BackboneMixin from .configuration_upernet import UperNetConfig __UpperCAmelCase : List[Any] = [ "openmmlab/upernet-convnext-tiny", # See all UperNet models at https://huggingface.co/models?filter=upernet ] # General docstring __UpperCAmelCase : List[str] = "UperNetConfig" class UpperCAmelCase_ ( nn.Module): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = 0 , __SCREAMING_SNAKE_CASE = False , __SCREAMING_SNAKE_CASE = 1 , ): """simple docstring""" super().__init__() UpperCamelCase : str = nn.Convad( in_channels=__SCREAMING_SNAKE_CASE , out_channels=__SCREAMING_SNAKE_CASE , kernel_size=__SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE , bias=__SCREAMING_SNAKE_CASE , dilation=__SCREAMING_SNAKE_CASE , ) UpperCamelCase : int = nn.BatchNormad(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[Any] = nn.ReLU() def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Any = self.conv(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[Any] = self.batch_norm(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = self.activation(__SCREAMING_SNAKE_CASE ) return output class UpperCAmelCase_ ( nn.Module): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" super().__init__() UpperCamelCase : List[Any] = [ nn.AdaptiveAvgPoolad(__SCREAMING_SNAKE_CASE ), UperNetConvModule(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , kernel_size=1 ), ] for i, layer in enumerate(self.layers ): self.add_module(str(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : str = input for layer in self.layers: UpperCamelCase : int = layer(__SCREAMING_SNAKE_CASE ) return hidden_state class UpperCAmelCase_ ( nn.Module): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" super().__init__() UpperCamelCase : List[Any] = pool_scales UpperCamelCase : Dict = align_corners UpperCamelCase : Optional[int] = in_channels UpperCamelCase : Union[str, Any] = channels UpperCamelCase : List[str] = [] for i, pool_scale in enumerate(__SCREAMING_SNAKE_CASE ): UpperCamelCase : Union[str, Any] = UperNetPyramidPoolingBlock(pool_scale=__SCREAMING_SNAKE_CASE , in_channels=__SCREAMING_SNAKE_CASE , channels=__SCREAMING_SNAKE_CASE ) self.blocks.append(__SCREAMING_SNAKE_CASE ) self.add_module(str(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : str = [] for ppm in self.blocks: UpperCamelCase : List[str] = ppm(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Tuple = nn.functional.interpolate( __SCREAMING_SNAKE_CASE , size=x.size()[2:] , mode='''bilinear''' , align_corners=self.align_corners ) ppm_outs.append(__SCREAMING_SNAKE_CASE ) return ppm_outs class UpperCAmelCase_ ( nn.Module): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" super().__init__() UpperCamelCase : int = config UpperCamelCase : List[str] = config.pool_scales # e.g. (1, 2, 3, 6) UpperCamelCase : Optional[int] = in_channels UpperCamelCase : str = config.hidden_size UpperCamelCase : str = False UpperCamelCase : List[str] = nn.Convad(self.channels , config.num_labels , kernel_size=1 ) # PSP Module UpperCamelCase : Optional[int] = UperNetPyramidPoolingModule( self.pool_scales , self.in_channels[-1] , self.channels , align_corners=self.align_corners , ) UpperCamelCase : str = UperNetConvModule( self.in_channels[-1] + len(self.pool_scales ) * self.channels , self.channels , kernel_size=3 , padding=1 , ) # FPN Module UpperCamelCase : Union[str, Any] = nn.ModuleList() UpperCamelCase : Union[str, Any] = nn.ModuleList() for in_channels in self.in_channels[:-1]: # skip the top layer UpperCamelCase : List[Any] = UperNetConvModule(__SCREAMING_SNAKE_CASE , self.channels , kernel_size=1 ) UpperCamelCase : int = UperNetConvModule(self.channels , self.channels , kernel_size=3 , padding=1 ) self.lateral_convs.append(__SCREAMING_SNAKE_CASE ) self.fpn_convs.append(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[Any] = UperNetConvModule( len(self.in_channels ) * self.channels , self.channels , kernel_size=3 , padding=1 , ) def _lowercase ( self ): """simple docstring""" self.apply(self._init_weights ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if isinstance(__SCREAMING_SNAKE_CASE , nn.Convad ): module.weight.data.normal_(mean=0.0 , std=self.config.initializer_range ) if module.bias is not None: module.bias.data.zero_() def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Any = inputs[-1] UpperCamelCase : int = [x] psp_outs.extend(self.psp_modules(__SCREAMING_SNAKE_CASE ) ) UpperCamelCase : Any = torch.cat(__SCREAMING_SNAKE_CASE , dim=1 ) UpperCamelCase : Union[str, Any] = self.bottleneck(__SCREAMING_SNAKE_CASE ) return output def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : str = [lateral_conv(encoder_hidden_states[i] ) for i, lateral_conv in enumerate(self.lateral_convs )] laterals.append(self.psp_forward(__SCREAMING_SNAKE_CASE ) ) # build top-down path UpperCamelCase : int = len(__SCREAMING_SNAKE_CASE ) for i in range(used_backbone_levels - 1 , 0 , -1 ): UpperCamelCase : Optional[int] = laterals[i - 1].shape[2:] UpperCamelCase : Optional[Any] = laterals[i - 1] + nn.functional.interpolate( laterals[i] , size=__SCREAMING_SNAKE_CASE , mode='''bilinear''' , align_corners=self.align_corners ) # build outputs UpperCamelCase : str = [self.fpn_convs[i](laterals[i] ) for i in range(used_backbone_levels - 1 )] # append psp feature fpn_outs.append(laterals[-1] ) for i in range(used_backbone_levels - 1 , 0 , -1 ): UpperCamelCase : int = nn.functional.interpolate( fpn_outs[i] , size=fpn_outs[0].shape[2:] , mode='''bilinear''' , align_corners=self.align_corners ) UpperCamelCase : str = torch.cat(__SCREAMING_SNAKE_CASE , dim=1 ) UpperCamelCase : Tuple = self.fpn_bottleneck(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = self.classifier(__SCREAMING_SNAKE_CASE ) return output class UpperCAmelCase_ ( nn.Module): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = 2 , __SCREAMING_SNAKE_CASE = 3 , __SCREAMING_SNAKE_CASE = 1 ): """simple docstring""" super().__init__() UpperCamelCase : Dict = config UpperCamelCase : Optional[Any] = config.auxiliary_in_channels UpperCamelCase : Union[str, Any] = config.auxiliary_channels UpperCamelCase : Union[str, Any] = config.auxiliary_num_convs UpperCamelCase : Optional[Any] = config.auxiliary_concat_input UpperCamelCase : List[str] = in_index UpperCamelCase : Any = (kernel_size // 2) * dilation UpperCamelCase : Optional[Any] = [] convs.append( UperNetConvModule( self.in_channels , self.channels , kernel_size=__SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE , dilation=__SCREAMING_SNAKE_CASE ) ) for i in range(self.num_convs - 1 ): convs.append( UperNetConvModule( self.channels , self.channels , kernel_size=__SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE , dilation=__SCREAMING_SNAKE_CASE ) ) if self.num_convs == 0: UpperCamelCase : str = nn.Identity() else: UpperCamelCase : Dict = nn.Sequential(*__SCREAMING_SNAKE_CASE ) if self.concat_input: UpperCamelCase : Union[str, Any] = UperNetConvModule( self.in_channels + self.channels , self.channels , kernel_size=__SCREAMING_SNAKE_CASE , padding=kernel_size // 2 ) UpperCamelCase : Optional[Any] = nn.Convad(self.channels , config.num_labels , kernel_size=1 ) def _lowercase ( self ): """simple docstring""" self.apply(self._init_weights ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if isinstance(__SCREAMING_SNAKE_CASE , nn.Convad ): module.weight.data.normal_(mean=0.0 , std=self.config.initializer_range ) if module.bias is not None: module.bias.data.zero_() def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Any = encoder_hidden_states[self.in_index] UpperCamelCase : str = self.convs(__SCREAMING_SNAKE_CASE ) if self.concat_input: UpperCamelCase : int = self.conv_cat(torch.cat([hidden_states, output] , dim=1 ) ) UpperCamelCase : Union[str, Any] = self.classifier(__SCREAMING_SNAKE_CASE ) return output class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : Optional[Any] = UperNetConfig __UpperCamelCase : Optional[int] = "pixel_values" __UpperCamelCase : Dict = True def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): module.backbone.init_weights() module.decode_head.init_weights() module.auxiliary_head.init_weights() def _lowercase ( self ): """simple docstring""" self.backbone.init_weights() self.decode_head.init_weights() self.auxiliary_head.init_weights() def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=False ): """simple docstring""" if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): UpperCamelCase : str = value __UpperCAmelCase : List[Any] = r"\n Parameters:\n This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) sub-class. Use\n it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and\n behavior.\n config ([`UperNetConfig`]): 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" __UpperCAmelCase : Union[str, Any] = r"\n Args:\n pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n Pixel values. Padding will be ignored by default should you provide it. Pixel values can be obtained using\n [`AutoImageProcessor`]. See [`SegformerImageProcessor.__call__`] for details.\n output_attentions (`bool`, *optional*):\n Whether or not to return the attentions tensors of all attention layers in case the backbone has them. See\n `attentions` under returned tensors for more detail.\n output_hidden_states (`bool`, *optional*):\n Whether or not to return the hidden states of all layers of the backbone. See `hidden_states` under\n returned tensors for 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( "UperNet framework leveraging any vision backbone e.g. for ADE20k, CityScapes.", _a, ) class UpperCAmelCase_ ( _a): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" super().__init__(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Dict = AutoBackbone.from_config(config.backbone_config ) # Semantic segmentation head(s) UpperCamelCase : int = UperNetHead(__SCREAMING_SNAKE_CASE , in_channels=self.backbone.channels ) UpperCamelCase : int = UperNetFCNHead(__SCREAMING_SNAKE_CASE ) if config.use_auxiliary_head else None # Initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(UPERNET_INPUTS_DOCSTRING.format('''batch_size, sequence_length''' ) ) @replace_return_docstrings(output_type=__SCREAMING_SNAKE_CASE , config_class=_CONFIG_FOR_DOC ) def _lowercase ( self , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , ): """simple docstring""" UpperCamelCase : List[str] = return_dict if return_dict is not None else self.config.use_return_dict UpperCamelCase : str = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) UpperCamelCase : Tuple = output_attentions if output_attentions is not None else self.config.output_attentions UpperCamelCase : Tuple = self.backbone.forward_with_filtered_kwargs( __SCREAMING_SNAKE_CASE , output_hidden_states=__SCREAMING_SNAKE_CASE , output_attentions=__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = outputs.feature_maps UpperCamelCase : Union[str, Any] = self.decode_head(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = nn.functional.interpolate(__SCREAMING_SNAKE_CASE , size=pixel_values.shape[2:] , mode='''bilinear''' , align_corners=__SCREAMING_SNAKE_CASE ) UpperCamelCase : int = None if self.auxiliary_head is not None: UpperCamelCase : int = self.auxiliary_head(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Tuple = nn.functional.interpolate( __SCREAMING_SNAKE_CASE , size=pixel_values.shape[2:] , mode='''bilinear''' , align_corners=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = None if labels is not None: if self.config.num_labels == 1: raise ValueError('''The number of labels should be greater than one''' ) else: # compute weighted loss UpperCamelCase : Optional[int] = CrossEntropyLoss(ignore_index=self.config.loss_ignore_index ) UpperCamelCase : Tuple = loss_fct(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[Any] = loss_fct(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = main_loss + self.config.auxiliary_loss_weight * auxiliary_loss if not return_dict: if output_hidden_states: UpperCamelCase : Optional[Any] = (logits,) + outputs[1:] else: UpperCamelCase : int = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return SemanticSegmenterOutput( loss=__SCREAMING_SNAKE_CASE , logits=__SCREAMING_SNAKE_CASE , hidden_states=outputs.hidden_states , attentions=outputs.attentions , )
643
1
from .imports import is_tqdm_available if is_tqdm_available(): from tqdm.auto import tqdm as _tqdm from ..state import PartialState def a ( SCREAMING_SNAKE_CASE_ : bool = True , *SCREAMING_SNAKE_CASE_ : List[str] , **SCREAMING_SNAKE_CASE_ : Tuple ): """simple docstring""" if not is_tqdm_available(): raise ImportError('''Accelerate\'s `tqdm` module requires `tqdm` to be installed. Please run `pip install tqdm`.''' ) UpperCamelCase : int = False if main_process_only: UpperCamelCase : int = PartialState().local_process_index == 0 return _tqdm(*SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , disable=SCREAMING_SNAKE_CASE_ )
643
import json import os import tempfile import transformers import datasets from utils import generate_example_dataset, get_duration __UpperCAmelCase : Optional[int] = 500000 __UpperCAmelCase , __UpperCAmelCase : Any = os.path.split(__file__) __UpperCAmelCase : int = os.path.join(RESULTS_BASEPATH, "results", RESULTS_FILENAME.replace(".py", ".json")) @get_duration def a ( SCREAMING_SNAKE_CASE_ : datasets.Dataset , **SCREAMING_SNAKE_CASE_ : Union[str, Any] ): """simple docstring""" UpperCamelCase : Tuple = dataset.map(**SCREAMING_SNAKE_CASE_ ) @get_duration def a ( SCREAMING_SNAKE_CASE_ : datasets.Dataset , **SCREAMING_SNAKE_CASE_ : Any ): """simple docstring""" UpperCamelCase : int = dataset.filter(**SCREAMING_SNAKE_CASE_ ) def a ( ): """simple docstring""" UpperCamelCase : Optional[int] = {'''num examples''': SPEED_TEST_N_EXAMPLES} with tempfile.TemporaryDirectory() as tmp_dir: UpperCamelCase : Dict = datasets.Features({'''text''': datasets.Value('''string''' ), '''numbers''': datasets.Value('''float32''' )} ) UpperCamelCase : List[str] = generate_example_dataset( os.path.join(SCREAMING_SNAKE_CASE_ , '''dataset.arrow''' ) , SCREAMING_SNAKE_CASE_ , num_examples=SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Union[str, Any] = transformers.AutoTokenizer.from_pretrained('''bert-base-cased''' , use_fast=SCREAMING_SNAKE_CASE_ ) def tokenize(SCREAMING_SNAKE_CASE_ : Dict ): return tokenizer(examples['''text'''] ) UpperCamelCase : List[Any] = map(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : List[str] = map(SCREAMING_SNAKE_CASE_ , batched=SCREAMING_SNAKE_CASE_ ) UpperCamelCase : List[Any] = map(SCREAMING_SNAKE_CASE_ , function=lambda SCREAMING_SNAKE_CASE_ : None , batched=SCREAMING_SNAKE_CASE_ ) with dataset.formatted_as(type='''numpy''' ): UpperCamelCase : Tuple = map(SCREAMING_SNAKE_CASE_ , function=lambda SCREAMING_SNAKE_CASE_ : None , batched=SCREAMING_SNAKE_CASE_ ) with dataset.formatted_as(type='''pandas''' ): UpperCamelCase : int = map(SCREAMING_SNAKE_CASE_ , function=lambda SCREAMING_SNAKE_CASE_ : None , batched=SCREAMING_SNAKE_CASE_ ) with dataset.formatted_as(type='''torch''' , columns='''numbers''' ): UpperCamelCase : Dict = map(SCREAMING_SNAKE_CASE_ , function=lambda SCREAMING_SNAKE_CASE_ : None , batched=SCREAMING_SNAKE_CASE_ ) with dataset.formatted_as(type='''tensorflow''' , columns='''numbers''' ): UpperCamelCase : Tuple = map(SCREAMING_SNAKE_CASE_ , function=lambda SCREAMING_SNAKE_CASE_ : None , batched=SCREAMING_SNAKE_CASE_ ) UpperCamelCase : List[str] = map(SCREAMING_SNAKE_CASE_ , function=SCREAMING_SNAKE_CASE_ , batched=SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Dict = filter(SCREAMING_SNAKE_CASE_ ) # Activate later when tokenizer support batched inputs # with dataset.formatted_as(type='numpy'): # times[func.__name__ + " fast-tokenizer batched numpy"] = func(dataset, function=tokenize, batched=True) with open(SCREAMING_SNAKE_CASE_ , '''wb''' ) as f: f.write(json.dumps(SCREAMING_SNAKE_CASE_ ).encode('''utf-8''' ) ) if __name__ == "__main__": # useful to run the profiler benchmark_map_filter()
643
1
import unittest from transformers import GPTNeoXJapaneseConfig, is_torch_available from transformers.models.gpt_neox_japanese.tokenization_gpt_neox_japanese import GPTNeoXJapaneseTokenizer 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 GPTNeoXJapaneseForCausalLM, GPTNeoXJapaneseModel class UpperCAmelCase_ : '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=13 , __SCREAMING_SNAKE_CASE=7 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=99 , __SCREAMING_SNAKE_CASE=32 , __SCREAMING_SNAKE_CASE=5 , __SCREAMING_SNAKE_CASE=4 , __SCREAMING_SNAKE_CASE=4 , __SCREAMING_SNAKE_CASE="gelu" , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=512 , __SCREAMING_SNAKE_CASE=16 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=0.02 , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=4 , __SCREAMING_SNAKE_CASE=None , ): """simple docstring""" UpperCamelCase : Tuple = parent UpperCamelCase : Tuple = batch_size UpperCamelCase : Union[str, Any] = seq_length UpperCamelCase : Union[str, Any] = is_training UpperCamelCase : Union[str, Any] = use_input_mask UpperCamelCase : str = use_token_type_ids UpperCamelCase : Union[str, Any] = use_labels UpperCamelCase : int = vocab_size UpperCamelCase : Union[str, Any] = hidden_size UpperCamelCase : Tuple = num_hidden_layers UpperCamelCase : Optional[int] = num_attention_heads UpperCamelCase : Union[str, Any] = intermediate_multiple_size UpperCamelCase : List[Any] = hidden_act UpperCamelCase : Union[str, Any] = hidden_dropout UpperCamelCase : Any = attention_dropout UpperCamelCase : Optional[int] = weight_tying UpperCamelCase : int = max_position_embeddings UpperCamelCase : Any = type_vocab_size UpperCamelCase : Tuple = type_sequence_label_size UpperCamelCase : Optional[Any] = initializer_range UpperCamelCase : Tuple = num_labels UpperCamelCase : Union[str, Any] = num_choices UpperCamelCase : Optional[int] = scope def _lowercase ( self ): """simple docstring""" UpperCamelCase : int = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCamelCase : int = None if self.use_input_mask: UpperCamelCase : int = random_attention_mask([self.batch_size, self.seq_length] ) UpperCamelCase : Tuple = None if self.use_labels: UpperCamelCase : Tuple = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) UpperCamelCase : List[str] = self.get_config() return config, input_ids, input_mask, token_labels def _lowercase ( self ): """simple docstring""" return GPTNeoXJapaneseConfig( 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_multiple_size=self.intermediate_multiple_size , hidden_act=self.hidden_act , hidden_dropout=self.hidden_dropout , attention_dropout=self.attention_dropout , weight_tying=self.weight_tying , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=__SCREAMING_SNAKE_CASE , initializer_range=self.initializer_range , ) def _lowercase ( self ): """simple docstring""" UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase : Union[str, Any] = self.prepare_config_and_inputs() UpperCamelCase : str = True return config, input_ids, input_mask, token_labels def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : List[str] = GPTNeoXJapaneseModel(config=__SCREAMING_SNAKE_CASE ) model.to(__SCREAMING_SNAKE_CASE ) model.eval() UpperCamelCase : Tuple = model(__SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = model(__SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Optional[Any] = True UpperCamelCase : List[str] = GPTNeoXJapaneseModel(__SCREAMING_SNAKE_CASE ) model.to(__SCREAMING_SNAKE_CASE ) model.eval() UpperCamelCase : int = 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 , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Optional[Any] = GPTNeoXJapaneseForCausalLM(config=__SCREAMING_SNAKE_CASE ) model.to(__SCREAMING_SNAKE_CASE ) model.eval() UpperCamelCase : Optional[Any] = model(__SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE , labels=__SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Optional[int] = True UpperCamelCase : str = GPTNeoXJapaneseForCausalLM(config=__SCREAMING_SNAKE_CASE ) model.to(__SCREAMING_SNAKE_CASE ) model.eval() # first forward pass UpperCamelCase : Optional[int] = model(__SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE , use_cache=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = outputs.past_key_values # create hypothetical multiple next token and extent to next_input_ids UpperCamelCase : Dict = ids_tensor((self.batch_size, 3) , config.vocab_size ) UpperCamelCase : str = ids_tensor((self.batch_size, 3) , vocab_size=2 ) # append to next input_ids and UpperCamelCase : Dict = torch.cat([input_ids, next_tokens] , dim=-1 ) UpperCamelCase : str = torch.cat([input_mask, next_mask] , dim=-1 ) UpperCamelCase : Optional[int] = model(__SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE , output_hidden_states=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = output_from_no_past['''hidden_states'''][0] UpperCamelCase : List[Any] = model( __SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE , past_key_values=__SCREAMING_SNAKE_CASE , output_hidden_states=__SCREAMING_SNAKE_CASE , )['''hidden_states'''][0] # select random slice UpperCamelCase : Any = ids_tensor((1,) , output_from_past.shape[-1] ).item() UpperCamelCase : List[str] = output_from_no_past[:, -3:, random_slice_idx].detach() UpperCamelCase : Optional[Any] = output_from_past[:, :, random_slice_idx].detach() self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1] ) # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , atol=1e-3 ) ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Tuple = self.prepare_config_and_inputs() UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase : Union[str, Any] = config_and_inputs UpperCamelCase : Any = {'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_torch class UpperCAmelCase_ ( _a, _a, unittest.TestCase): '''simple docstring''' __UpperCamelCase : Optional[Any] = (GPTNeoXJapaneseModel, GPTNeoXJapaneseForCausalLM) if is_torch_available() else () __UpperCamelCase : Any = (GPTNeoXJapaneseForCausalLM,) if is_torch_available() else () __UpperCamelCase : List[str] = ( {"feature-extraction": GPTNeoXJapaneseModel, "text-generation": GPTNeoXJapaneseForCausalLM} if is_torch_available() else {} ) __UpperCamelCase : Tuple = False __UpperCamelCase : List[str] = False __UpperCamelCase : List[Any] = False __UpperCamelCase : List[Any] = False def _lowercase ( self ): """simple docstring""" UpperCamelCase : Tuple = GPTNeoXJapaneseModelTester(self ) UpperCamelCase : Union[str, Any] = ConfigTester(self , config_class=__SCREAMING_SNAKE_CASE , hidden_size=37 ) def _lowercase ( self ): """simple docstring""" self.config_tester.run_common_tests() def _lowercase ( self ): """simple docstring""" UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase : Tuple = self.model_tester.prepare_config_and_inputs_for_decoder() self.model_tester.create_and_check_model_as_decoder(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_decoder() UpperCamelCase : List[str] = None self.model_tester.create_and_check_model_as_decoder(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_decoder_model_past_large_inputs(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_causal_lm(*__SCREAMING_SNAKE_CASE ) @slow def _lowercase ( self ): """simple docstring""" UpperCamelCase : Union[str, Any] = '''abeja/gpt-neox-japanese-2.7b''' UpperCamelCase : Optional[Any] = ['''データサイエンティストとは、''', '''100年後に必要とされる会社は、''', '''フルリモートの環境で働くために必要なことは、''', '''国境の長いトンネルを抜けると''', '''美味しい日本食といえば、'''] UpperCamelCase : Any = [ '''データサイエンティストとは、データを分析し、ビジネスに役立つ知見を導き出す専門家のことです。''', '''100年後に必要とされる会社は、「人」が中心の会社です。''', '''フルリモートの環境で働くために必要なことは、「自分の時間をコントロールする」ことです。''', '''国境の長いトンネルを抜けると、そこは雪国だった。''', '''美味しい日本食といえば、やっぱりお寿司ですよね。''', ] UpperCamelCase : Optional[int] = GPTNeoXJapaneseTokenizer.from_pretrained(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = GPTNeoXJapaneseForCausalLM.from_pretrained(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Dict = [] for prompt in prompts: UpperCamelCase : int = tokenizer(__SCREAMING_SNAKE_CASE , return_tensors='''pt''' ).input_ids UpperCamelCase : int = model.generate(__SCREAMING_SNAKE_CASE , max_length=50 ) UpperCamelCase : List[str] = tokenizer.batch_decode(__SCREAMING_SNAKE_CASE , skip_special_tokens=__SCREAMING_SNAKE_CASE ) predicted_outputs += generated_string self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE )
643
from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import ShapEPipeline else: from .camera import create_pan_cameras from .pipeline_shap_e import ShapEPipeline from .pipeline_shap_e_img2img import ShapEImgaImgPipeline from .renderer import ( BoundingBoxVolume, ImportanceRaySampler, MLPNeRFModelOutput, MLPNeRSTFModel, ShapEParamsProjModel, ShapERenderer, StratifiedRaySampler, VoidNeRFModel, )
643
1
import copy from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ..auto import CONFIG_MAPPING __UpperCAmelCase : Optional[Any] = logging.get_logger(__name__) __UpperCAmelCase : List[str] = { "microsoft/conditional-detr-resnet-50": ( "https://huggingface.co/microsoft/conditional-detr-resnet-50/resolve/main/config.json" ), } class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : Optional[int] = "conditional_detr" __UpperCamelCase : Optional[Any] = ["past_key_values"] __UpperCamelCase : Union[str, Any] = { "hidden_size": "d_model", "num_attention_heads": "encoder_attention_heads", } def __init__( self , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=300 , __SCREAMING_SNAKE_CASE=6 , __SCREAMING_SNAKE_CASE=2_048 , __SCREAMING_SNAKE_CASE=8 , __SCREAMING_SNAKE_CASE=6 , __SCREAMING_SNAKE_CASE=2_048 , __SCREAMING_SNAKE_CASE=8 , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE="relu" , __SCREAMING_SNAKE_CASE=256 , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=0.02 , __SCREAMING_SNAKE_CASE=1.0 , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE="sine" , __SCREAMING_SNAKE_CASE="resnet50" , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=5 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=1 , __SCREAMING_SNAKE_CASE=1 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=5 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=0.25 , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" if backbone_config is not None and use_timm_backbone: raise ValueError('''You can\'t specify both `backbone_config` and `use_timm_backbone`.''' ) if not use_timm_backbone: if backbone_config is None: logger.info('''`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.''' ) UpperCamelCase : str = CONFIG_MAPPING['''resnet'''](out_features=['''stage4'''] ) elif isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): UpperCamelCase : Tuple = backbone_config.get('''model_type''' ) UpperCamelCase : Optional[Any] = CONFIG_MAPPING[backbone_model_type] UpperCamelCase : Any = config_class.from_dict(__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = use_timm_backbone UpperCamelCase : int = backbone_config UpperCamelCase : Any = num_channels UpperCamelCase : Optional[Any] = num_queries UpperCamelCase : Tuple = d_model UpperCamelCase : Optional[Any] = encoder_ffn_dim UpperCamelCase : Optional[int] = encoder_layers UpperCamelCase : Union[str, Any] = encoder_attention_heads UpperCamelCase : Optional[Any] = decoder_ffn_dim UpperCamelCase : Optional[int] = decoder_layers UpperCamelCase : Optional[Any] = decoder_attention_heads UpperCamelCase : Any = dropout UpperCamelCase : List[Any] = attention_dropout UpperCamelCase : List[Any] = activation_dropout UpperCamelCase : List[str] = activation_function UpperCamelCase : Optional[int] = init_std UpperCamelCase : Optional[Any] = init_xavier_std UpperCamelCase : Union[str, Any] = encoder_layerdrop UpperCamelCase : Optional[Any] = decoder_layerdrop UpperCamelCase : Tuple = encoder_layers UpperCamelCase : Optional[Any] = auxiliary_loss UpperCamelCase : Union[str, Any] = position_embedding_type UpperCamelCase : Optional[int] = backbone UpperCamelCase : Dict = use_pretrained_backbone UpperCamelCase : Tuple = dilation # Hungarian matcher UpperCamelCase : Union[str, Any] = class_cost UpperCamelCase : List[Any] = bbox_cost UpperCamelCase : Optional[Any] = giou_cost # Loss coefficients UpperCamelCase : Optional[Any] = mask_loss_coefficient UpperCamelCase : Optional[int] = dice_loss_coefficient UpperCamelCase : Optional[Any] = cls_loss_coefficient UpperCamelCase : Optional[int] = bbox_loss_coefficient UpperCamelCase : Optional[int] = giou_loss_coefficient UpperCamelCase : Optional[int] = focal_alpha super().__init__(is_encoder_decoder=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) @property def _lowercase ( self ): """simple docstring""" return self.encoder_attention_heads @property def _lowercase ( self ): """simple docstring""" return self.d_model def _lowercase ( self ): """simple docstring""" UpperCamelCase : Union[str, Any] = copy.deepcopy(self.__dict__ ) if self.backbone_config is not None: UpperCamelCase : List[Any] = self.backbone_config.to_dict() UpperCamelCase : List[Any] = self.__class__.model_type return output class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : Dict = version.parse("1.11") @property def _lowercase ( self ): """simple docstring""" return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ('''pixel_mask''', {0: '''batch'''}), ] ) @property def _lowercase ( self ): """simple docstring""" return 1e-5 @property def _lowercase ( self ): """simple docstring""" return 12
643
import torch from transformers import AutoModel class UpperCAmelCase_ ( torch.nn.Module): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE="sayef/fsner-bert-base-uncased" ): """simple docstring""" super(__SCREAMING_SNAKE_CASE , self ).__init__() UpperCamelCase : List[str] = AutoModel.from_pretrained(__SCREAMING_SNAKE_CASE , return_dict=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = torch.nn.CosineSimilarity(3 , 1e-08 ) UpperCamelCase : List[Any] = torch.nn.Softmax(dim=1 ) def _lowercase ( self , **__SCREAMING_SNAKE_CASE ): """simple docstring""" return self.bert(**__SCREAMING_SNAKE_CASE ).last_hidden_state def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" return token_embeddings.sum(2 , keepdim=__SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=1 ): """simple docstring""" return self.softmax(T * self.cos(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Any = W_supports['''sizes'''].tolist() UpperCamelCase : Optional[int] = W_supports['''start_token_id'''].item() UpperCamelCase : Any = W_supports['''end_token_id'''].item() del W_supports["sizes"] del W_supports["start_token_id"] del W_supports["end_token_id"] UpperCamelCase : Union[str, Any] = self.BERT(**__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = self.BERT(**__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[Any] = None UpperCamelCase : Any = None UpperCamelCase : Optional[Any] = W_supports['''input_ids'''] == start_token_id UpperCamelCase : Any = W_supports['''input_ids'''] == end_token_id for i, size in enumerate(__SCREAMING_SNAKE_CASE ): if i == 0: UpperCamelCase : Optional[int] = 0 else: UpperCamelCase : Tuple = support_sizes[i - 1] UpperCamelCase : Tuple = S[s : s + size][start_token_masks[s : s + size]] UpperCamelCase : List[str] = S[s : s + size][end_token_masks[s : s + size]] UpperCamelCase : Dict = torch.matmul(q[i] , s_start.T ).sum(1 ).softmax(0 ) UpperCamelCase : Tuple = torch.matmul(q[i] , s_end.T ).sum(1 ).softmax(0 ) if p_starts is not None: UpperCamelCase : List[str] = torch.vstack((p_starts, p_start) ) UpperCamelCase : Union[str, Any] = torch.vstack((p_ends, p_end) ) else: UpperCamelCase : str = p_start UpperCamelCase : Optional[int] = p_end return p_starts, p_ends
643
1
from typing import TYPE_CHECKING from ....utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __UpperCAmelCase : Tuple = { "configuration_mctct": ["MCTCT_PRETRAINED_CONFIG_ARCHIVE_MAP", "MCTCTConfig"], "feature_extraction_mctct": ["MCTCTFeatureExtractor"], "processing_mctct": ["MCTCTProcessor"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase : Union[str, Any] = [ "MCTCT_PRETRAINED_MODEL_ARCHIVE_LIST", "MCTCTForCTC", "MCTCTModel", "MCTCTPreTrainedModel", ] if TYPE_CHECKING: from .configuration_mctct import MCTCT_PRETRAINED_CONFIG_ARCHIVE_MAP, MCTCTConfig from .feature_extraction_mctct import MCTCTFeatureExtractor from .processing_mctct import MCTCTProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mctct import MCTCT_PRETRAINED_MODEL_ARCHIVE_LIST, MCTCTForCTC, MCTCTModel, MCTCTPreTrainedModel else: import sys __UpperCAmelCase : int = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
643
import json import os import unittest from transformers import DebertaTokenizer, DebertaTokenizerFast from transformers.models.deberta.tokenization_deberta import VOCAB_FILES_NAMES from transformers.testing_utils import slow from ...test_tokenization_common import TokenizerTesterMixin class UpperCAmelCase_ ( _a, unittest.TestCase): '''simple docstring''' __UpperCamelCase : str = DebertaTokenizer __UpperCamelCase : Optional[int] = True __UpperCamelCase : Optional[int] = DebertaTokenizerFast def _lowercase ( self ): """simple docstring""" super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt UpperCamelCase : Optional[int] = [ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''\u0120''', '''\u0120l''', '''\u0120n''', '''\u0120lo''', '''\u0120low''', '''er''', '''\u0120lowest''', '''\u0120newer''', '''\u0120wider''', '''[UNK]''', ] UpperCamelCase : Tuple = dict(zip(__SCREAMING_SNAKE_CASE , range(len(__SCREAMING_SNAKE_CASE ) ) ) ) UpperCamelCase : Any = ['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', ''''''] UpperCamelCase : List[Any] = {'''unk_token''': '''[UNK]'''} UpperCamelCase : Union[str, Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) UpperCamelCase : Optional[int] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''merges_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(__SCREAMING_SNAKE_CASE ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(__SCREAMING_SNAKE_CASE ) ) def _lowercase ( self , **__SCREAMING_SNAKE_CASE ): """simple docstring""" kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname , **__SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : int = '''lower newer''' UpperCamelCase : Union[str, Any] = '''lower newer''' return input_text, output_text def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[str] = self.get_tokenizer() UpperCamelCase : int = '''lower newer''' UpperCamelCase : Union[str, Any] = ['''l''', '''o''', '''w''', '''er''', '''\u0120''', '''n''', '''e''', '''w''', '''er'''] UpperCamelCase : Tuple = tokenizer.tokenize(__SCREAMING_SNAKE_CASE ) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) UpperCamelCase : List[Any] = tokens + [tokenizer.unk_token] UpperCamelCase : Optional[int] = [0, 1, 2, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(tokenizer.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : int = self.get_tokenizer() UpperCamelCase : Optional[Any] = tokenizer('''Hello''' , '''World''' ) UpperCamelCase : List[str] = [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1] self.assertListEqual(tokd['''token_type_ids'''] , __SCREAMING_SNAKE_CASE ) @slow def _lowercase ( self ): """simple docstring""" UpperCamelCase : Union[str, Any] = self.tokenizer_class.from_pretrained('''microsoft/deberta-base''' ) UpperCamelCase : Optional[Any] = tokenizer.encode('''sequence builders''' , add_special_tokens=__SCREAMING_SNAKE_CASE ) UpperCamelCase : int = tokenizer.encode('''multi-sequence build''' , add_special_tokens=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Dict = tokenizer.encode( '''sequence builders''' , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = tokenizer.encode( '''sequence builders''' , '''multi-sequence build''' , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = tokenizer.build_inputs_with_special_tokens(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = tokenizer.build_inputs_with_special_tokens(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) assert encoded_sentence == encoded_text_from_decode assert encoded_pair == encoded_pair_from_decode @slow def _lowercase ( self ): """simple docstring""" UpperCamelCase : Optional[Any] = [self.tokenizer_class] if self.test_rust_tokenizer: tokenizer_classes.append(self.rust_tokenizer_class ) for tokenizer_class in tokenizer_classes: UpperCamelCase : Optional[int] = tokenizer_class.from_pretrained('''microsoft/deberta-base''' ) UpperCamelCase : str = [ '''ALBERT: A Lite BERT for Self-supervised Learning of Language Representations''', '''ALBERT incorporates two parameter reduction techniques''', '''The first one is a factorized embedding parameterization. By decomposing the large vocabulary''' ''' embedding matrix into two small matrices, we separate the size of the hidden layers from the size of''' ''' vocabulary embedding.''', ] UpperCamelCase : Union[str, Any] = tokenizer(__SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = [tokenizer.decode(__SCREAMING_SNAKE_CASE , skip_special_tokens=__SCREAMING_SNAKE_CASE ) for seq in encoding['''input_ids''']] # fmt: off UpperCamelCase : int = { '''input_ids''': [ [1, 2_118, 11_126, 565, 35, 83, 25_191, 163, 18_854, 13, 12_156, 12, 16_101, 25_376, 13_807, 9, 22_205, 27_893, 1_635, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 2_118, 11_126, 565, 24_536, 80, 43_797, 4_878, 7_373, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 133, 78, 65, 16, 10, 3_724, 1_538, 33_183, 11_303, 43_797, 1_938, 4, 870, 24_165, 29_105, 5, 739, 32_644, 33_183, 11_303, 36_173, 88, 80, 650, 7_821, 45_940, 6, 52, 2_559, 5, 1_836, 9, 5, 7_397, 13_171, 31, 5, 1_836, 9, 32_644, 33_183, 11_303, 4, 2] ], '''token_type_ids''': [ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ], '''attention_mask''': [ [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] ] } # fmt: on UpperCamelCase : List[str] = [ '''ALBERT: A Lite BERT for Self-supervised Learning of Language Representations''', '''ALBERT incorporates two parameter reduction techniques''', '''The first one is a factorized embedding parameterization. By decomposing the large vocabulary''' ''' embedding matrix into two small matrices, we separate the size of the hidden layers from the size of''' ''' vocabulary embedding.''', ] self.assertDictEqual(encoding.data , __SCREAMING_SNAKE_CASE ) for expected, decoded in zip(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE )
643
1
import json import os import unittest from transformers import DebertaTokenizer, DebertaTokenizerFast from transformers.models.deberta.tokenization_deberta import VOCAB_FILES_NAMES from transformers.testing_utils import slow from ...test_tokenization_common import TokenizerTesterMixin class UpperCAmelCase_ ( _a, unittest.TestCase): '''simple docstring''' __UpperCamelCase : str = DebertaTokenizer __UpperCamelCase : Optional[int] = True __UpperCamelCase : Optional[int] = DebertaTokenizerFast def _lowercase ( self ): """simple docstring""" super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt UpperCamelCase : Optional[int] = [ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''\u0120''', '''\u0120l''', '''\u0120n''', '''\u0120lo''', '''\u0120low''', '''er''', '''\u0120lowest''', '''\u0120newer''', '''\u0120wider''', '''[UNK]''', ] UpperCamelCase : Tuple = dict(zip(__SCREAMING_SNAKE_CASE , range(len(__SCREAMING_SNAKE_CASE ) ) ) ) UpperCamelCase : Any = ['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', ''''''] UpperCamelCase : List[Any] = {'''unk_token''': '''[UNK]'''} UpperCamelCase : Union[str, Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) UpperCamelCase : Optional[int] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''merges_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(__SCREAMING_SNAKE_CASE ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(__SCREAMING_SNAKE_CASE ) ) def _lowercase ( self , **__SCREAMING_SNAKE_CASE ): """simple docstring""" kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname , **__SCREAMING_SNAKE_CASE ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : int = '''lower newer''' UpperCamelCase : Union[str, Any] = '''lower newer''' return input_text, output_text def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[str] = self.get_tokenizer() UpperCamelCase : int = '''lower newer''' UpperCamelCase : Union[str, Any] = ['''l''', '''o''', '''w''', '''er''', '''\u0120''', '''n''', '''e''', '''w''', '''er'''] UpperCamelCase : Tuple = tokenizer.tokenize(__SCREAMING_SNAKE_CASE ) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) UpperCamelCase : List[Any] = tokens + [tokenizer.unk_token] UpperCamelCase : Optional[int] = [0, 1, 2, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(tokenizer.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : int = self.get_tokenizer() UpperCamelCase : Optional[Any] = tokenizer('''Hello''' , '''World''' ) UpperCamelCase : List[str] = [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1] self.assertListEqual(tokd['''token_type_ids'''] , __SCREAMING_SNAKE_CASE ) @slow def _lowercase ( self ): """simple docstring""" UpperCamelCase : Union[str, Any] = self.tokenizer_class.from_pretrained('''microsoft/deberta-base''' ) UpperCamelCase : Optional[Any] = tokenizer.encode('''sequence builders''' , add_special_tokens=__SCREAMING_SNAKE_CASE ) UpperCamelCase : int = tokenizer.encode('''multi-sequence build''' , add_special_tokens=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Dict = tokenizer.encode( '''sequence builders''' , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = tokenizer.encode( '''sequence builders''' , '''multi-sequence build''' , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = tokenizer.build_inputs_with_special_tokens(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = tokenizer.build_inputs_with_special_tokens(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) assert encoded_sentence == encoded_text_from_decode assert encoded_pair == encoded_pair_from_decode @slow def _lowercase ( self ): """simple docstring""" UpperCamelCase : Optional[Any] = [self.tokenizer_class] if self.test_rust_tokenizer: tokenizer_classes.append(self.rust_tokenizer_class ) for tokenizer_class in tokenizer_classes: UpperCamelCase : Optional[int] = tokenizer_class.from_pretrained('''microsoft/deberta-base''' ) UpperCamelCase : str = [ '''ALBERT: A Lite BERT for Self-supervised Learning of Language Representations''', '''ALBERT incorporates two parameter reduction techniques''', '''The first one is a factorized embedding parameterization. By decomposing the large vocabulary''' ''' embedding matrix into two small matrices, we separate the size of the hidden layers from the size of''' ''' vocabulary embedding.''', ] UpperCamelCase : Union[str, Any] = tokenizer(__SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Union[str, Any] = [tokenizer.decode(__SCREAMING_SNAKE_CASE , skip_special_tokens=__SCREAMING_SNAKE_CASE ) for seq in encoding['''input_ids''']] # fmt: off UpperCamelCase : int = { '''input_ids''': [ [1, 2_118, 11_126, 565, 35, 83, 25_191, 163, 18_854, 13, 12_156, 12, 16_101, 25_376, 13_807, 9, 22_205, 27_893, 1_635, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 2_118, 11_126, 565, 24_536, 80, 43_797, 4_878, 7_373, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 133, 78, 65, 16, 10, 3_724, 1_538, 33_183, 11_303, 43_797, 1_938, 4, 870, 24_165, 29_105, 5, 739, 32_644, 33_183, 11_303, 36_173, 88, 80, 650, 7_821, 45_940, 6, 52, 2_559, 5, 1_836, 9, 5, 7_397, 13_171, 31, 5, 1_836, 9, 32_644, 33_183, 11_303, 4, 2] ], '''token_type_ids''': [ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ], '''attention_mask''': [ [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] ] } # fmt: on UpperCamelCase : List[str] = [ '''ALBERT: A Lite BERT for Self-supervised Learning of Language Representations''', '''ALBERT incorporates two parameter reduction techniques''', '''The first one is a factorized embedding parameterization. By decomposing the large vocabulary''' ''' embedding matrix into two small matrices, we separate the size of the hidden layers from the size of''' ''' vocabulary embedding.''', ] self.assertDictEqual(encoding.data , __SCREAMING_SNAKE_CASE ) for expected, decoded in zip(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE )
643
import re from pathlib import Path from unittest import TestCase import pytest @pytest.mark.integration class UpperCAmelCase_ ( _a): '''simple docstring''' def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" with open(__SCREAMING_SNAKE_CASE , encoding='''utf-8''' ) as input_file: UpperCamelCase : str = re.compile(R'''(?!.*\b(?:encoding|rb|w|wb|w+|wb+|ab|ab+)\b)(?<=\s)(open)\((.*)\)''' ) UpperCamelCase : Optional[int] = input_file.read() UpperCamelCase : Union[str, Any] = regexp.search(__SCREAMING_SNAKE_CASE ) return match def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" with open(__SCREAMING_SNAKE_CASE , encoding='''utf-8''' ) as input_file: UpperCamelCase : Optional[int] = re.compile(R'''#[^\r\n]*print\(|\"[^\r\n]*print\(|\"\"\".*?print\(.*?\"\"\"|(print\()''' , re.DOTALL ) UpperCamelCase : Tuple = input_file.read() # use `re.finditer` to handle the case where the ignored groups would be matched first by `re.search` UpperCamelCase : Dict = regexp.finditer(__SCREAMING_SNAKE_CASE ) UpperCamelCase : int = [match for match in matches if match is not None and match.group(1 ) is not None] return matches[0] if matches else None def _lowercase ( self ): """simple docstring""" UpperCamelCase : int = Path('''./datasets''' ) UpperCamelCase : Tuple = list(dataset_paths.absolute().glob('''**/*.py''' ) ) for dataset in dataset_files: if self._no_encoding_on_file_open(str(__SCREAMING_SNAKE_CASE ) ): raise AssertionError(f"""open(...) must use utf-8 encoding in {dataset}""" ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Optional[int] = Path('''./datasets''' ) UpperCamelCase : Tuple = list(dataset_paths.absolute().glob('''**/*.py''' ) ) for dataset in dataset_files: if self._no_print_statements(str(__SCREAMING_SNAKE_CASE ) ): raise AssertionError(f"""print statement found in {dataset}. Use datasets.logger/logging instead.""" )
643
1
import argparse import json import os import fairseq import torch from fairseq.data import Dictionary # Register SEW's fairseq modules from sew_asapp import tasks # noqa: F401 from transformers import ( SEWConfig, SEWForCTC, SEWModel, WavaVecaCTCTokenizer, WavaVecaFeatureExtractor, WavaVecaProcessor, logging, ) logging.set_verbosity_info() __UpperCAmelCase : Tuple = logging.get_logger(__name__) __UpperCAmelCase : Dict = { "post_extract_proj": "feature_projection", "encoder.pos_conv.0": "encoder.pos_conv_embed.conv", "self_attn.k_proj": "encoder.layers.*.attention.k_proj", "self_attn.v_proj": "encoder.layers.*.attention.v_proj", "self_attn.q_proj": "encoder.layers.*.attention.q_proj", "self_attn.out_proj": "encoder.layers.*.attention.out_proj", "self_attn_layer_norm": "encoder.layers.*.layer_norm", "fc1": "encoder.layers.*.feed_forward.intermediate_dense", "fc2": "encoder.layers.*.feed_forward.output_dense", "final_layer_norm": "encoder.layers.*.final_layer_norm", "encoder.upsample.0": "encoder.upsample.projection", "encoder.layer_norm": "encoder.layer_norm", "w2v_model.layer_norm": "layer_norm", "w2v_encoder.proj": "lm_head", "mask_emb": "masked_spec_embed", } def a ( SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : List[Any] , SCREAMING_SNAKE_CASE_ : List[Any] , SCREAMING_SNAKE_CASE_ : Tuple ): """simple docstring""" for attribute in key.split('''.''' ): UpperCamelCase : Any = getattr(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if weight_type is not None: UpperCamelCase : List[Any] = getattr(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ).shape else: UpperCamelCase : Dict = hf_pointer.shape assert hf_shape == value.shape, ( F"""Shape of hf {key + "." + weight_type if weight_type is not None else ""} is {hf_shape}, but should be""" F""" {value.shape} for {full_name}""" ) if weight_type == "weight": UpperCamelCase : Optional[Any] = value elif weight_type == "weight_g": UpperCamelCase : int = value elif weight_type == "weight_v": UpperCamelCase : Tuple = value elif weight_type == "bias": UpperCamelCase : str = value else: UpperCamelCase : str = value logger.info(F"""{key + "." + weight_type if weight_type is not None else ""} was initialized from {full_name}.""" ) def a ( SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : str ): """simple docstring""" UpperCamelCase : Any = [] UpperCamelCase : Optional[int] = fairseq_model.state_dict() UpperCamelCase : int = hf_model.sew.feature_extractor if is_finetuned else hf_model.feature_extractor for name, value in fairseq_dict.items(): UpperCamelCase : Optional[int] = False if "conv_layers" in name: load_conv_layer( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , hf_model.config.feat_extract_norm == '''group''' , ) UpperCamelCase : Optional[int] = True else: for key, mapped_key in MAPPING.items(): UpperCamelCase : List[str] = '''sew.''' + mapped_key if (is_finetuned and mapped_key != '''lm_head''') else mapped_key if key in name or key.split('''w2v_model.''' )[-1] == name.split('''.''' )[0]: UpperCamelCase : str = True if "*" in mapped_key: UpperCamelCase : Optional[Any] = name.split(SCREAMING_SNAKE_CASE_ )[0].split('''.''' )[-2] UpperCamelCase : str = mapped_key.replace('''*''' , SCREAMING_SNAKE_CASE_ ) if "weight_g" in name: UpperCamelCase : Optional[int] = '''weight_g''' elif "weight_v" in name: UpperCamelCase : Optional[int] = '''weight_v''' elif "weight" in name: UpperCamelCase : Tuple = '''weight''' elif "bias" in name: UpperCamelCase : Dict = '''bias''' else: UpperCamelCase : int = None set_recursively(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) continue if not is_used: unused_weights.append(SCREAMING_SNAKE_CASE_ ) logger.warning(F"""Unused weights: {unused_weights}""" ) def a ( SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : Tuple , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : Optional[int] ): """simple docstring""" UpperCamelCase : int = full_name.split('''conv_layers.''' )[-1] UpperCamelCase : Optional[int] = name.split('''.''' ) UpperCamelCase : Optional[int] = int(items[0] ) UpperCamelCase : Union[str, Any] = int(items[1] ) if type_id == 0: if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, ( F"""{full_name} has size {value.shape}, but""" F""" {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found.""" ) UpperCamelCase : Any = value logger.info(F"""Feat extract conv layer {layer_id} was initialized from {full_name}.""" ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape, ( F"""{full_name} has size {value.shape}, but""" F""" {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found.""" ) UpperCamelCase : List[Any] = value logger.info(F"""Feat extract conv layer {layer_id} was initialized from {full_name}.""" ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, ( F"""{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was""" " found." ) UpperCamelCase : List[Any] = value logger.info(F"""Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.""" ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, ( F"""{full_name} has size {value.shape}, but""" F""" {feature_extractor[layer_id].layer_norm.weight.data.shape} was found.""" ) UpperCamelCase : Optional[int] = value logger.info(F"""Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.""" ) else: unused_weights.append(SCREAMING_SNAKE_CASE_ ) def a ( SCREAMING_SNAKE_CASE_ : List[Any] , SCREAMING_SNAKE_CASE_ : List[str] ): """simple docstring""" UpperCamelCase : Union[str, Any] = SEWConfig() if is_finetuned: UpperCamelCase : Any = model.wav_encoder.wav_model.cfg else: UpperCamelCase : List[str] = model.cfg UpperCamelCase : str = fs_config.conv_bias UpperCamelCase : Any = eval(fs_config.conv_feature_layers ) UpperCamelCase : Optional[int] = [x[0] for x in conv_layers] UpperCamelCase : Optional[int] = [x[1] for x in conv_layers] UpperCamelCase : str = [x[2] for x in conv_layers] UpperCamelCase : Union[str, Any] = '''gelu''' UpperCamelCase : int = '''layer''' if fs_config.extractor_mode == '''layer_norm''' else '''group''' UpperCamelCase : Optional[Any] = 0.0 UpperCamelCase : Union[str, Any] = fs_config.activation_fn.name UpperCamelCase : int = fs_config.encoder_embed_dim UpperCamelCase : Union[str, Any] = 0.02 UpperCamelCase : Union[str, Any] = fs_config.encoder_ffn_embed_dim UpperCamelCase : str = 1E-5 UpperCamelCase : int = fs_config.encoder_layerdrop UpperCamelCase : List[str] = fs_config.encoder_attention_heads UpperCamelCase : Union[str, Any] = fs_config.conv_pos_groups UpperCamelCase : Union[str, Any] = fs_config.conv_pos UpperCamelCase : Dict = len(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Tuple = fs_config.encoder_layers UpperCamelCase : int = fs_config.squeeze_factor # take care of any params that are overridden by the Wav2VecCtc model if is_finetuned: UpperCamelCase : List[Any] = model.cfg UpperCamelCase : Optional[int] = fs_config.final_dropout UpperCamelCase : Optional[Any] = fs_config.layerdrop UpperCamelCase : Any = fs_config.activation_dropout UpperCamelCase : Union[str, Any] = fs_config.mask_prob > 0 or fs_config.mask_channel_prob > 0 UpperCamelCase : Tuple = fs_config.attention_dropout UpperCamelCase : Optional[Any] = fs_config.dropout_input UpperCamelCase : Optional[int] = fs_config.dropout UpperCamelCase : List[str] = fs_config.mask_channel_length UpperCamelCase : Union[str, Any] = fs_config.mask_channel_prob UpperCamelCase : List[Any] = fs_config.mask_length UpperCamelCase : List[str] = fs_config.mask_prob UpperCamelCase : Any = '''Wav2Vec2FeatureExtractor''' UpperCamelCase : List[str] = '''Wav2Vec2CTCTokenizer''' return config @torch.no_grad() def a ( SCREAMING_SNAKE_CASE_ : Tuple , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : List[Any]=None , SCREAMING_SNAKE_CASE_ : Union[str, Any]=None , SCREAMING_SNAKE_CASE_ : str=True ): """simple docstring""" if is_finetuned: UpperCamelCase , UpperCamelCase , UpperCamelCase : Any = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={'''data''': '''/'''.join(dict_path.split('''/''' )[:-1] )} ) else: UpperCamelCase , UpperCamelCase , UpperCamelCase : Optional[int] = fairseq.checkpoint_utils.load_model_ensemble_and_task([checkpoint_path] ) if config_path is not None: UpperCamelCase : Union[str, Any] = SEWConfig.from_pretrained(SCREAMING_SNAKE_CASE_ ) else: UpperCamelCase : Dict = convert_config(model[0] , SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Any = model[0].eval() UpperCamelCase : Optional[Any] = True if config.feat_extract_norm == '''layer''' else False UpperCamelCase : Optional[int] = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=1_6_0_0_0 , padding_value=0 , do_normalize=SCREAMING_SNAKE_CASE_ , return_attention_mask=SCREAMING_SNAKE_CASE_ , ) if is_finetuned: if dict_path: UpperCamelCase : List[str] = Dictionary.load(SCREAMING_SNAKE_CASE_ ) # important change bos & pad token id since CTC symbol is <pad> and # not <s> as in fairseq UpperCamelCase : List[Any] = target_dict.pad_index UpperCamelCase : List[str] = target_dict.bos_index UpperCamelCase : Optional[Any] = target_dict.pad_index UpperCamelCase : List[Any] = target_dict.bos_index UpperCamelCase : Optional[Any] = target_dict.eos_index UpperCamelCase : List[Any] = len(target_dict.symbols ) UpperCamelCase : Union[str, Any] = os.path.join(SCREAMING_SNAKE_CASE_ , '''vocab.json''' ) if not os.path.isdir(SCREAMING_SNAKE_CASE_ ): logger.error('''--pytorch_dump_folder_path ({}) should be a directory'''.format(SCREAMING_SNAKE_CASE_ ) ) return os.makedirs(SCREAMING_SNAKE_CASE_ , exist_ok=SCREAMING_SNAKE_CASE_ ) with open(SCREAMING_SNAKE_CASE_ , '''w''' , encoding='''utf-8''' ) as vocab_handle: json.dump(target_dict.indices , SCREAMING_SNAKE_CASE_ ) UpperCamelCase : Union[str, Any] = WavaVecaCTCTokenizer( SCREAMING_SNAKE_CASE_ , unk_token=target_dict.unk_word , pad_token=target_dict.pad_word , bos_token=target_dict.bos_word , eos_token=target_dict.eos_word , word_delimiter_token='''|''' , do_lower_case=SCREAMING_SNAKE_CASE_ , ) UpperCamelCase : List[str] = WavaVecaProcessor(feature_extractor=SCREAMING_SNAKE_CASE_ , tokenizer=SCREAMING_SNAKE_CASE_ ) processor.save_pretrained(SCREAMING_SNAKE_CASE_ ) UpperCamelCase : List[str] = SEWForCTC(SCREAMING_SNAKE_CASE_ ) else: UpperCamelCase : Any = SEWModel(SCREAMING_SNAKE_CASE_ ) feature_extractor.save_pretrained(SCREAMING_SNAKE_CASE_ ) recursively_load_weights(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) hf_model.save_pretrained(SCREAMING_SNAKE_CASE_ ) if __name__ == "__main__": __UpperCAmelCase : Union[str, Any] = argparse.ArgumentParser() parser.add_argument("--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.") parser.add_argument("--checkpoint_path", default=None, type=str, help="Path to fairseq checkpoint") parser.add_argument("--dict_path", default=None, type=str, help="Path to dict of fine-tuned model") parser.add_argument("--config_path", default=None, type=str, help="Path to hf config.json of model to convert") parser.add_argument( "--is_finetuned", action="store_true", help="Whether the model to convert is a fine-tuned model or not" ) __UpperCAmelCase : List[str] = parser.parse_args() convert_sew_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.dict_path, args.is_finetuned )
643
from __future__ import annotations import unittest from transformers import XGLMConfig, XGLMTokenizer, 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 tensorflow as tf from transformers.models.xglm.modeling_tf_xglm import ( TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFXGLMForCausalLM, TFXGLMModel, ) @require_tf class UpperCAmelCase_ : '''simple docstring''' __UpperCamelCase : Any = XGLMConfig __UpperCamelCase : Dict = {} __UpperCamelCase : List[str] = "gelu" def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=14 , __SCREAMING_SNAKE_CASE=7 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=99 , __SCREAMING_SNAKE_CASE=32 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=4 , __SCREAMING_SNAKE_CASE=37 , __SCREAMING_SNAKE_CASE="gelu" , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=512 , __SCREAMING_SNAKE_CASE=0.02 , ): """simple docstring""" UpperCamelCase : Any = parent UpperCamelCase : Optional[int] = batch_size UpperCamelCase : str = seq_length UpperCamelCase : List[str] = is_training UpperCamelCase : Tuple = use_input_mask UpperCamelCase : Union[str, Any] = use_labels UpperCamelCase : int = vocab_size UpperCamelCase : Optional[int] = d_model UpperCamelCase : Any = num_hidden_layers UpperCamelCase : List[str] = num_attention_heads UpperCamelCase : Optional[Any] = ffn_dim UpperCamelCase : Optional[int] = activation_function UpperCamelCase : List[str] = activation_dropout UpperCamelCase : Any = attention_dropout UpperCamelCase : str = max_position_embeddings UpperCamelCase : Union[str, Any] = initializer_range UpperCamelCase : int = None UpperCamelCase : Dict = 0 UpperCamelCase : int = 2 UpperCamelCase : Any = 1 def _lowercase ( self ): """simple docstring""" return XGLMConfig.from_pretrained('''facebook/xglm-564M''' ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : Optional[int] = tf.clip_by_value( ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) , clip_value_min=0 , clip_value_max=3 ) UpperCamelCase : int = None if self.use_input_mask: UpperCamelCase : Any = random_attention_mask([self.batch_size, self.seq_length] ) UpperCamelCase : Tuple = self.get_config() UpperCamelCase : str = floats_tensor([self.num_hidden_layers, self.num_attention_heads] , 2 ) return ( config, input_ids, input_mask, head_mask, ) def _lowercase ( self ): """simple docstring""" return XGLMConfig( vocab_size=self.vocab_size , d_model=self.hidden_size , num_layers=self.num_hidden_layers , attention_heads=self.num_attention_heads , ffn_dim=self.ffn_dim , activation_function=self.activation_function , activation_dropout=self.activation_dropout , attention_dropout=self.attention_dropout , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , use_cache=__SCREAMING_SNAKE_CASE , bos_token_id=self.bos_token_id , eos_token_id=self.eos_token_id , pad_token_id=self.pad_token_id , return_dict=__SCREAMING_SNAKE_CASE , ) def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[str] = self.prepare_config_and_inputs() ( ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ( UpperCamelCase ) , ) : Dict = config_and_inputs UpperCamelCase : List[str] = { '''input_ids''': input_ids, '''head_mask''': head_mask, } return config, inputs_dict @require_tf class UpperCAmelCase_ ( _a, _a, unittest.TestCase): '''simple docstring''' __UpperCamelCase : Optional[Any] = (TFXGLMModel, TFXGLMForCausalLM) if is_tf_available() else () __UpperCamelCase : Union[str, Any] = (TFXGLMForCausalLM,) if is_tf_available() else () __UpperCamelCase : Any = ( {"feature-extraction": TFXGLMModel, "text-generation": TFXGLMForCausalLM} if is_tf_available() else {} ) __UpperCamelCase : Optional[int] = False __UpperCamelCase : List[Any] = False __UpperCamelCase : List[Any] = False def _lowercase ( self ): """simple docstring""" UpperCamelCase : List[Any] = TFXGLMModelTester(self ) UpperCamelCase : Any = ConfigTester(self , config_class=__SCREAMING_SNAKE_CASE , n_embd=37 ) def _lowercase ( self ): """simple docstring""" self.config_tester.run_common_tests() @slow def _lowercase ( self ): """simple docstring""" for model_name in TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCamelCase : List[Any] = TFXGLMModel.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsNotNone(__SCREAMING_SNAKE_CASE ) @unittest.skip(reason='''Currently, model embeddings are going to undergo a major refactor.''' ) def _lowercase ( self ): """simple docstring""" super().test_resize_token_embeddings() @require_tf class UpperCAmelCase_ ( unittest.TestCase): '''simple docstring''' @slow def _lowercase ( self , __SCREAMING_SNAKE_CASE=True ): """simple docstring""" UpperCamelCase : List[str] = TFXGLMForCausalLM.from_pretrained('''facebook/xglm-564M''' ) UpperCamelCase : List[Any] = tf.convert_to_tensor([[2, 268, 9_865]] , dtype=tf.intaa ) # The dog # </s> The dog is a very friendly dog. He is very affectionate and loves to play with other # fmt: off UpperCamelCase : str = [2, 268, 9_865, 67, 11, 1_988, 57_252, 9_865, 5, 984, 67, 1_988, 213_838, 1_658, 53, 70_446, 33, 6_657, 278, 1_581] # fmt: on UpperCamelCase : Union[str, Any] = model.generate(__SCREAMING_SNAKE_CASE , do_sample=__SCREAMING_SNAKE_CASE , num_beams=1 ) if verify_outputs: self.assertListEqual(output_ids[0].numpy().tolist() , __SCREAMING_SNAKE_CASE ) @slow def _lowercase ( self ): """simple docstring""" UpperCamelCase : str = XGLMTokenizer.from_pretrained('''facebook/xglm-564M''' ) UpperCamelCase : List[str] = TFXGLMForCausalLM.from_pretrained('''facebook/xglm-564M''' ) tf.random.set_seed(0 ) UpperCamelCase : Tuple = tokenizer('''Today is a nice day and''' , return_tensors='''tf''' ) UpperCamelCase : int = tokenized.input_ids # forces the generation to happen on CPU, to avoid GPU-related quirks (and assure same output regardless of the available devices) with tf.device(''':/CPU:0''' ): UpperCamelCase : str = model.generate(__SCREAMING_SNAKE_CASE , do_sample=__SCREAMING_SNAKE_CASE , seed=[7, 0] ) UpperCamelCase : Dict = tokenizer.decode(output_ids[0] , skip_special_tokens=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = ( '''Today is a nice day and warm evening here over Southern Alberta!! Today when they closed schools due''' ) self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) @slow def _lowercase ( self ): """simple docstring""" UpperCamelCase : Dict = TFXGLMForCausalLM.from_pretrained('''facebook/xglm-564M''' ) UpperCamelCase : Tuple = XGLMTokenizer.from_pretrained('''facebook/xglm-564M''' ) UpperCamelCase : Tuple = '''left''' # use different length sentences to test batching UpperCamelCase : Any = [ '''This is an extremelly long sentence that only exists to test the ability of the model to cope with ''' '''left-padding, such as in batched generation. The output for the sequence below should be the same ''' '''regardless of whether left padding is applied or not. When''', '''Hello, my dog is a little''', ] UpperCamelCase : List[Any] = tokenizer(__SCREAMING_SNAKE_CASE , return_tensors='''tf''' , padding=__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = inputs['''input_ids'''] UpperCamelCase : Optional[int] = model.generate(input_ids=__SCREAMING_SNAKE_CASE , attention_mask=inputs['''attention_mask'''] , max_new_tokens=12 ) UpperCamelCase : Optional[int] = tokenizer(sentences[0] , return_tensors='''tf''' ).input_ids UpperCamelCase : Optional[Any] = model.generate(input_ids=__SCREAMING_SNAKE_CASE , max_new_tokens=12 ) UpperCamelCase : str = tokenizer(sentences[1] , return_tensors='''tf''' ).input_ids UpperCamelCase : List[Any] = model.generate(input_ids=__SCREAMING_SNAKE_CASE , max_new_tokens=12 ) UpperCamelCase : Any = tokenizer.batch_decode(__SCREAMING_SNAKE_CASE , skip_special_tokens=__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[str] = tokenizer.decode(output_non_padded[0] , skip_special_tokens=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Tuple = tokenizer.decode(output_padded[0] , skip_special_tokens=__SCREAMING_SNAKE_CASE ) UpperCamelCase : Optional[int] = [ '''This is an extremelly long sentence that only exists to test the ability of the model to cope with ''' '''left-padding, such as in batched generation. The output for the sequence below should be the same ''' '''regardless of whether left padding is applied or not. When left padding is applied, the sequence will be ''' '''a single''', '''Hello, my dog is a little bit of a shy one, but he is very friendly''', ] self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) self.assertListEqual(__SCREAMING_SNAKE_CASE , [non_padded_sentence, padded_sentence] )
643
1
import enum import shutil import sys __UpperCAmelCase , __UpperCAmelCase : Union[str, Any] = shutil.get_terminal_size() __UpperCAmelCase : Tuple = {"UP": "A", "DOWN": "B", "RIGHT": "C", "LEFT": "D"} class UpperCAmelCase_ ( enum.Enum): '''simple docstring''' __UpperCamelCase : List[Any] = 0 __UpperCamelCase : Tuple = 1 def a ( SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Union[str, Any]="" ): """simple docstring""" sys.stdout.write(str(SCREAMING_SNAKE_CASE_ ) + end ) sys.stdout.flush() def a ( SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : Optional[Any] , SCREAMING_SNAKE_CASE_ : int="" ): """simple docstring""" forceWrite(F"""\u001b[{color}m{content}\u001b[0m""" , SCREAMING_SNAKE_CASE_ ) def a ( ): """simple docstring""" forceWrite('''\r''' ) def a ( SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : str ): """simple docstring""" forceWrite(F"""\033[{num_lines}{CURSOR_TO_CHAR[direction.upper()]}""" ) def a ( ): """simple docstring""" forceWrite(''' ''' * TERMINAL_WIDTH ) reset_cursor() def a ( ): """simple docstring""" reset_cursor() forceWrite('''-''' * TERMINAL_WIDTH )
643
import os from shutil import copyfile from typing import List, Optional, Tuple from tokenizers import processors from ...tokenization_utils import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_mbart import MBartTokenizer else: __UpperCAmelCase : Optional[int] = None __UpperCAmelCase : int = logging.get_logger(__name__) __UpperCAmelCase : List[Any] = {"vocab_file": "sentencepiece.bpe.model", "tokenizer_file": "tokenizer.json"} __UpperCAmelCase : str = { "vocab_file": { "facebook/mbart-large-en-ro": ( "https://huggingface.co/facebook/mbart-large-en-ro/resolve/main/sentencepiece.bpe.model" ), "facebook/mbart-large-cc25": ( "https://huggingface.co/facebook/mbart-large-cc25/resolve/main/sentencepiece.bpe.model" ), }, "tokenizer_file": { "facebook/mbart-large-en-ro": "https://huggingface.co/facebook/mbart-large-en-ro/resolve/main/tokenizer.json", "facebook/mbart-large-cc25": "https://huggingface.co/facebook/mbart-large-cc25/resolve/main/tokenizer.json", }, } __UpperCAmelCase : Union[str, Any] = { "facebook/mbart-large-en-ro": 1024, "facebook/mbart-large-cc25": 1024, } # fmt: off __UpperCAmelCase : Any = ["ar_AR", "cs_CZ", "de_DE", "en_XX", "es_XX", "et_EE", "fi_FI", "fr_XX", "gu_IN", "hi_IN", "it_IT", "ja_XX", "kk_KZ", "ko_KR", "lt_LT", "lv_LV", "my_MM", "ne_NP", "nl_XX", "ro_RO", "ru_RU", "si_LK", "tr_TR", "vi_VN", "zh_CN"] class UpperCAmelCase_ ( _a): '''simple docstring''' __UpperCamelCase : List[str] = VOCAB_FILES_NAMES __UpperCamelCase : str = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCamelCase : Tuple = PRETRAINED_VOCAB_FILES_MAP __UpperCamelCase : Union[str, Any] = ["input_ids", "attention_mask"] __UpperCamelCase : Any = MBartTokenizer __UpperCamelCase : List[int] = [] __UpperCamelCase : List[int] = [] def __init__( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE="<s>" , __SCREAMING_SNAKE_CASE="</s>" , __SCREAMING_SNAKE_CASE="</s>" , __SCREAMING_SNAKE_CASE="<s>" , __SCREAMING_SNAKE_CASE="<unk>" , __SCREAMING_SNAKE_CASE="<pad>" , __SCREAMING_SNAKE_CASE="<mask>" , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" UpperCamelCase : Union[str, Any] = AddedToken(__SCREAMING_SNAKE_CASE , lstrip=__SCREAMING_SNAKE_CASE , rstrip=__SCREAMING_SNAKE_CASE ) if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else mask_token super().__init__( vocab_file=__SCREAMING_SNAKE_CASE , tokenizer_file=__SCREAMING_SNAKE_CASE , bos_token=__SCREAMING_SNAKE_CASE , eos_token=__SCREAMING_SNAKE_CASE , sep_token=__SCREAMING_SNAKE_CASE , cls_token=__SCREAMING_SNAKE_CASE , unk_token=__SCREAMING_SNAKE_CASE , pad_token=__SCREAMING_SNAKE_CASE , mask_token=__SCREAMING_SNAKE_CASE , src_lang=__SCREAMING_SNAKE_CASE , tgt_lang=__SCREAMING_SNAKE_CASE , additional_special_tokens=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) UpperCamelCase : Dict = vocab_file UpperCamelCase : List[str] = False if not self.vocab_file else True UpperCamelCase : List[Any] = FAIRSEQ_LANGUAGE_CODES.copy() if additional_special_tokens is not None: # Only add those special tokens if they are not already there. _additional_special_tokens.extend( [t for t in additional_special_tokens if t not in _additional_special_tokens] ) self.add_special_tokens({'''additional_special_tokens''': _additional_special_tokens} ) UpperCamelCase : List[Any] = { lang_code: self.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) for lang_code in FAIRSEQ_LANGUAGE_CODES } UpperCamelCase : Dict = src_lang if src_lang is not None else '''en_XX''' UpperCamelCase : List[Any] = self.convert_tokens_to_ids(self._src_lang ) UpperCamelCase : str = tgt_lang self.set_src_lang_special_tokens(self._src_lang ) @property def _lowercase ( self ): """simple docstring""" return self._src_lang @src_lang.setter def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Union[str, Any] = new_src_lang self.set_src_lang_special_tokens(self._src_lang ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ): """simple docstring""" if token_ids_a is None: return self.prefix_tokens + token_ids_a + self.suffix_tokens # We don't expect to process pairs, but leave the pair logic for API consistency return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ): """simple docstring""" UpperCamelCase : str = [self.sep_token_id] UpperCamelCase : Union[str, Any] = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): """simple docstring""" if src_lang is None or tgt_lang is None: raise ValueError('''Translation requires a `src_lang` and a `tgt_lang` for this model''' ) UpperCamelCase : List[str] = src_lang UpperCamelCase : Dict = self(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , return_tensors=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) UpperCamelCase : List[Any] = self.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Any = tgt_lang_id return inputs def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = "en_XX" , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = "ro_RO" , **__SCREAMING_SNAKE_CASE , ): """simple docstring""" UpperCamelCase : Optional[Any] = src_lang UpperCamelCase : Optional[int] = tgt_lang return super().prepare_seqaseq_batch(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def _lowercase ( self ): """simple docstring""" return self.set_src_lang_special_tokens(self.src_lang ) def _lowercase ( self ): """simple docstring""" return self.set_tgt_lang_special_tokens(self.tgt_lang ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Optional[int] = self.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) UpperCamelCase : Any = [] UpperCamelCase : Dict = [self.eos_token_id, self.cur_lang_code] UpperCamelCase : Union[str, Any] = self.convert_ids_to_tokens(self.prefix_tokens ) UpperCamelCase : int = self.convert_ids_to_tokens(self.suffix_tokens ) UpperCamelCase : Tuple = processors.TemplateProcessing( single=prefix_tokens_str + ['''$A'''] + suffix_tokens_str , pair=prefix_tokens_str + ['''$A''', '''$B'''] + suffix_tokens_str , special_tokens=list(zip(prefix_tokens_str + suffix_tokens_str , self.prefix_tokens + self.suffix_tokens ) ) , ) def _lowercase ( self , __SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase : Optional[int] = self.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) UpperCamelCase : int = [] UpperCamelCase : Optional[Any] = [self.eos_token_id, self.cur_lang_code] UpperCamelCase : Optional[Any] = self.convert_ids_to_tokens(self.prefix_tokens ) UpperCamelCase : List[str] = self.convert_ids_to_tokens(self.suffix_tokens ) UpperCamelCase : Optional[int] = processors.TemplateProcessing( single=prefix_tokens_str + ['''$A'''] + suffix_tokens_str , pair=prefix_tokens_str + ['''$A''', '''$B'''] + suffix_tokens_str , special_tokens=list(zip(prefix_tokens_str + suffix_tokens_str , self.prefix_tokens + self.suffix_tokens ) ) , ) def _lowercase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ): """simple docstring""" if not self.can_save_slow_tokenizer: raise ValueError( '''Your fast tokenizer does not have the necessary information to save the vocabulary for a slow ''' '''tokenizer.''' ) if not os.path.isdir(__SCREAMING_SNAKE_CASE ): logger.error(f"""Vocabulary path ({save_directory}) should be a directory.""" ) return UpperCamelCase : Optional[int] = os.path.join( __SCREAMING_SNAKE_CASE , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(__SCREAMING_SNAKE_CASE ): copyfile(self.vocab_file , __SCREAMING_SNAKE_CASE ) return (out_vocab_file,)
643
1