code
stringlengths
87
55.2k
code_codestyle
int64
0
349
style_context
stringlengths
135
49.1k
style_context_codestyle
int64
0
349
label
int64
0
1
"""simple docstring""" from 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 lowerCAmelCase : Any = logging.get_logger(__name__) if is_vision_available(): import PIL class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' __UpperCamelCase = ["pixel_values"] def __init__( self , _a = True , _a = None , _a = PILImageResampling.BICUBIC , _a = True , _a = None , _a = True , _a = 1 / 255 , _a = True , _a = None , _a = None , _a = True , **_a , ): """simple docstring""" super().__init__(**_a ) lowerCamelCase = size if size is not None else {"""shortest_edge""": 224} lowerCamelCase = get_size_dict(_a , default_to_square=_a ) lowerCamelCase = crop_size if crop_size is not None else {"""height""": 224, """width""": 224} lowerCamelCase = get_size_dict(_a , default_to_square=_a , param_name="""crop_size""" ) lowerCamelCase = do_resize lowerCamelCase = size lowerCamelCase = resample lowerCamelCase = do_center_crop lowerCamelCase = crop_size lowerCamelCase = do_rescale lowerCamelCase = rescale_factor lowerCamelCase = do_normalize lowerCamelCase = image_mean if image_mean is not None else OPENAI_CLIP_MEAN lowerCamelCase = image_std if image_std is not None else OPENAI_CLIP_STD lowerCamelCase = do_convert_rgb def _lowerCAmelCase ( self , _a , _a , _a = PILImageResampling.BICUBIC , _a = None , **_a , ): """simple docstring""" lowerCamelCase = get_size_dict(_a , default_to_square=_a ) if "shortest_edge" not in size: raise ValueError(f'The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}' ) lowerCamelCase = get_resize_output_image_size(_a , size=size["""shortest_edge"""] , default_to_square=_a ) return resize(_a , size=_a , resample=_a , data_format=_a , **_a ) def _lowerCAmelCase ( self , _a , _a , _a = None , **_a , ): """simple docstring""" lowerCamelCase = get_size_dict(_a ) if "height" not in size or "width" not in size: raise ValueError(f'The `size` parameter must contain the keys (height, width). Got {size.keys()}' ) return center_crop(_a , size=(size["""height"""], size["""width"""]) , data_format=_a , **_a ) def _lowerCAmelCase ( self , _a , _a , _a = None , **_a , ): """simple docstring""" return rescale(_a , scale=_a , data_format=_a , **_a ) def _lowerCAmelCase ( self , _a , _a , _a , _a = None , **_a , ): """simple docstring""" return normalize(_a , mean=_a , std=_a , data_format=_a , **_a ) def _lowerCAmelCase ( self , _a , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = ChannelDimension.FIRST , **_a , ): """simple docstring""" lowerCamelCase = do_resize if do_resize is not None else self.do_resize lowerCamelCase = size if size is not None else self.size lowerCamelCase = get_size_dict(_a , param_name="""size""" , default_to_square=_a ) lowerCamelCase = resample if resample is not None else self.resample lowerCamelCase = do_center_crop if do_center_crop is not None else self.do_center_crop lowerCamelCase = crop_size if crop_size is not None else self.crop_size lowerCamelCase = get_size_dict(_a , param_name="""crop_size""" , default_to_square=_a ) lowerCamelCase = do_rescale if do_rescale is not None else self.do_rescale lowerCamelCase = rescale_factor if rescale_factor is not None else self.rescale_factor lowerCamelCase = do_normalize if do_normalize is not None else self.do_normalize lowerCamelCase = image_mean if image_mean is not None else self.image_mean lowerCamelCase = image_std if image_std is not None else self.image_std lowerCamelCase = do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb lowerCamelCase = make_list_of_images(_a ) if not valid_images(_a ): raise ValueError( """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """ """torch.Tensor, tf.Tensor or jax.ndarray.""" ) if do_resize and size is None: raise ValueError("""Size must be specified if do_resize is True.""" ) if do_center_crop and crop_size is None: raise ValueError("""Crop size must be specified if do_center_crop is True.""" ) if do_rescale and rescale_factor is None: raise ValueError("""Rescale factor must be specified if do_rescale is True.""" ) if do_normalize and (image_mean is None or image_std is None): raise ValueError("""Image mean and std must be specified if do_normalize is True.""" ) # PIL RGBA images are converted to RGB if do_convert_rgb: lowerCamelCase = [convert_to_rgb(_a ) for image in images] # All transformations expect numpy arrays. lowerCamelCase = [to_numpy_array(_a ) for image in images] if do_resize: lowerCamelCase = [self.resize(image=_a , size=_a , resample=_a ) for image in images] if do_center_crop: lowerCamelCase = [self.center_crop(image=_a , size=_a ) for image in images] if do_rescale: lowerCamelCase = [self.rescale(image=_a , scale=_a ) for image in images] if do_normalize: lowerCamelCase = [self.normalize(image=_a , mean=_a , std=_a ) for image in images] lowerCamelCase = [to_channel_dimension_format(_a , _a ) for image in images] lowerCamelCase = {"""pixel_values""": images} return BatchFeature(data=_a , tensor_type=_a )
291
"""simple docstring""" def a__ ( snake_case__ ) -> list: if len(snake_case__ ) < 2: return collection def circle_sort_util(snake_case__ , snake_case__ , snake_case__ ) -> bool: lowerCamelCase = False if low == high: return swapped lowerCamelCase = low lowerCamelCase = high while left < right: if collection[left] > collection[right]: lowerCamelCase , lowerCamelCase = ( collection[right], collection[left], ) lowerCamelCase = True left += 1 right -= 1 if left == right and collection[left] > collection[right + 1]: lowerCamelCase , lowerCamelCase = ( collection[right + 1], collection[left], ) lowerCamelCase = True lowerCamelCase = low + int((high - low) / 2 ) lowerCamelCase = circle_sort_util(snake_case__ , snake_case__ , snake_case__ ) lowerCamelCase = circle_sort_util(snake_case__ , mid + 1 , snake_case__ ) return swapped or left_swap or right_swap lowerCamelCase = True while is_not_sorted is True: lowerCamelCase = circle_sort_util(snake_case__ , 0 , len(snake_case__ ) - 1 ) return collection if __name__ == "__main__": lowerCAmelCase : Tuple = input("""Enter numbers separated by a comma:\n""").strip() lowerCAmelCase : List[Any] = [int(item) for item in user_input.split(""",""")] print(circle_sort(unsorted))
291
1
"""simple docstring""" from ..utils import DummyObject, requires_backends class __magic_name__ ( metaclass=UpperCAmelCase__ ): '''simple docstring''' __UpperCamelCase = ["speech"] def __init__( self , *_a , **_a ): """simple docstring""" requires_backends(self , ["""speech"""] ) class __magic_name__ ( metaclass=UpperCAmelCase__ ): '''simple docstring''' __UpperCamelCase = ["speech"] def __init__( self , *_a , **_a ): """simple docstring""" requires_backends(self , ["""speech"""] )
291
"""simple docstring""" from collections.abc import Generator def a__ ( ) -> Generator[int, None, None]: lowerCamelCase , lowerCamelCase = 0, 1 while True: lowerCamelCase , lowerCamelCase = b, a + b yield b def a__ ( snake_case__ = 10_00 ) -> int: lowerCamelCase = 1 lowerCamelCase = fibonacci_generator() while len(str(next(snake_case__ ) ) ) < n: answer += 1 return answer + 1 if __name__ == "__main__": print(solution(int(str(input()).strip())))
291
1
"""simple docstring""" 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__ ( snake_case__ , snake_case__ , snake_case__ ) -> List[Any]: return params[F'{prefix}/{prefix}/relpos_bias/rel_embedding'][:, i, :] def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__="attention" ) -> List[Any]: lowerCamelCase = lowerCamelCase = np.ascontiguousarray(params[F'{prefix}/{prefix}/{layer_name}/key/kernel'][:, i, :, :] ) lowerCamelCase = k_tmp.reshape(k_tmp.shape[0] , k_tmp.shape[1] * k_tmp.shape[2] ) lowerCamelCase = np.ascontiguousarray(params[F'{prefix}/{prefix}/{layer_name}/out/kernel'][:, i, :, :] ) lowerCamelCase = o_tmp.reshape(o_tmp.shape[0] * o_tmp.shape[1] , o_tmp.shape[2] ) lowerCamelCase = np.ascontiguousarray(params[F'{prefix}/{prefix}/{layer_name}/query/kernel'][:, i, :, :] ) lowerCamelCase = q_tmp.reshape(q_tmp.shape[0] , q_tmp.shape[1] * q_tmp.shape[2] ) lowerCamelCase = np.ascontiguousarray(params[F'{prefix}/{prefix}/{layer_name}/value/kernel'][:, i, :, :] ) lowerCamelCase = v_tmp.reshape(v_tmp.shape[0] , v_tmp.shape[1] * v_tmp.shape[2] ) return k, o, q, v def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__=False ) -> List[str]: if split_mlp_wi: lowerCamelCase = params[F'{prefix}/{prefix}/mlp/wi_0/kernel'][:, i, :] lowerCamelCase = params[F'{prefix}/{prefix}/mlp/wi_1/kernel'][:, i, :] lowerCamelCase = (wi_a, wi_a) else: lowerCamelCase = params[F'{prefix}/{prefix}/mlp/wi/kernel'][:, i, :] lowerCamelCase = params[F'{prefix}/{prefix}/mlp/wo/kernel'][:, i, :] return wi, wo def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ ) -> Tuple: return params[F'{prefix}/{prefix}/{layer_name}/scale'][:, i] def a__ ( snake_case__ , *, snake_case__ , snake_case__ , snake_case__ = False ) -> Dict: lowerCamelCase = traverse_util.flatten_dict(variables["""target"""] ) lowerCamelCase = {"""/""".join(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 lowerCamelCase = """encoder/encoder/mlp/wi_0/kernel""" in old print("""Split MLP:""" , snake_case__ ) lowerCamelCase = collections.OrderedDict() # Shared embeddings. lowerCamelCase = old["""token_embedder/embedding"""] # Encoder. for i in range(snake_case__ ): # Block i, layer 0 (Self Attention). lowerCamelCase = tax_layer_norm_lookup(snake_case__ , snake_case__ , """encoder""" , """pre_attention_layer_norm""" ) lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase = tax_attention_lookup(snake_case__ , snake_case__ , """encoder""" , """attention""" ) lowerCamelCase = layer_norm lowerCamelCase = k.T lowerCamelCase = o.T lowerCamelCase = q.T lowerCamelCase = v.T # Block i, layer 1 (MLP). lowerCamelCase = tax_layer_norm_lookup(snake_case__ , snake_case__ , """encoder""" , """pre_mlp_layer_norm""" ) lowerCamelCase , lowerCamelCase = tax_mlp_lookup(snake_case__ , snake_case__ , """encoder""" , snake_case__ ) lowerCamelCase = layer_norm if split_mlp_wi: lowerCamelCase = wi[0].T lowerCamelCase = wi[1].T else: lowerCamelCase = wi.T lowerCamelCase = wo.T if scalable_attention: # convert the rel_embedding of each layer lowerCamelCase = tax_relpos_bias_lookup( snake_case__ , snake_case__ , """encoder""" ).T lowerCamelCase = old["""encoder/encoder_norm/scale"""] if not scalable_attention: lowerCamelCase = tax_relpos_bias_lookup( snake_case__ , 0 , """encoder""" ).T lowerCamelCase = tax_relpos_bias_lookup( snake_case__ , 0 , """decoder""" ).T if not is_encoder_only: # Decoder. for i in range(snake_case__ ): # Block i, layer 0 (Self Attention). lowerCamelCase = tax_layer_norm_lookup(snake_case__ , snake_case__ , """decoder""" , """pre_self_attention_layer_norm""" ) lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase = tax_attention_lookup(snake_case__ , snake_case__ , """decoder""" , """self_attention""" ) lowerCamelCase = layer_norm lowerCamelCase = k.T lowerCamelCase = o.T lowerCamelCase = q.T lowerCamelCase = v.T # Block i, layer 1 (Cross Attention). lowerCamelCase = tax_layer_norm_lookup(snake_case__ , snake_case__ , """decoder""" , """pre_cross_attention_layer_norm""" ) lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase = tax_attention_lookup(snake_case__ , snake_case__ , """decoder""" , """encoder_decoder_attention""" ) lowerCamelCase = layer_norm lowerCamelCase = k.T lowerCamelCase = o.T lowerCamelCase = q.T lowerCamelCase = v.T # Block i, layer 2 (MLP). lowerCamelCase = tax_layer_norm_lookup(snake_case__ , snake_case__ , """decoder""" , """pre_mlp_layer_norm""" ) lowerCamelCase , lowerCamelCase = tax_mlp_lookup(snake_case__ , snake_case__ , """decoder""" , snake_case__ ) lowerCamelCase = layer_norm if split_mlp_wi: lowerCamelCase = wi[0].T lowerCamelCase = wi[1].T else: lowerCamelCase = wi.T lowerCamelCase = wo.T if scalable_attention: # convert the rel_embedding of each layer lowerCamelCase = tax_relpos_bias_lookup(snake_case__ , snake_case__ , """decoder""" ).T lowerCamelCase = 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: lowerCamelCase = old["""decoder/logits_dense/kernel"""].T return new def a__ ( snake_case__ , snake_case__ ) -> Optional[int]: lowerCamelCase = 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: lowerCamelCase = state_dict["""shared.weight"""] if not is_encoder_only: if "decoder.embed_tokens.weight" not in state_dict: lowerCamelCase = 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.""" ) lowerCamelCase = state_dict["""shared.weight"""] return state_dict def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ ) -> List[Any]: lowerCamelCase = checkpoints.load_tax_checkpoint(snake_case__ ) lowerCamelCase = convert_tax_to_pytorch( snake_case__ , num_layers=config.num_layers , is_encoder_only=snake_case__ , scalable_attention=snake_case__ ) lowerCamelCase = make_state_dict(snake_case__ , snake_case__ ) model.load_state_dict(snake_case__ , strict=snake_case__ ) def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ = False , snake_case__ = False , ) -> str: lowerCamelCase = MTaConfig.from_json_file(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: lowerCamelCase = UMTaEncoderModel(snake_case__ ) else: lowerCamelCase = UMTaForConditionalGeneration(snake_case__ ) # Load weights from tf checkpoint load_tax_weights_in_ta(snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ ) # Save pytorch-model print(F'Save PyTorch model to {pytorch_dump_path}' ) model.save_pretrained(snake_case__ ) # Verify that we can load the checkpoint. model.from_pretrained(snake_case__ ) print("""Done""" ) if __name__ == "__main__": lowerCAmelCase : 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, ) lowerCAmelCase : 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, )
291
"""simple docstring""" from math import ceil from typing import List, Optional, Union import numpy as np from ...audio_utils import mel_filter_bank, spectrogram, window_function from ...feature_extraction_sequence_utils import BatchFeature, SequenceFeatureExtractor from ...utils import TensorType, logging lowerCAmelCase : List[str] = logging.get_logger(__name__) class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' __UpperCamelCase = ["audio_values", "audio_mask"] def __init__( self , _a=2_048 , _a=1 , _a=[16, 16] , _a=128 , _a=44_100 , _a=86 , _a=2_048 , _a=0.0 , **_a , ): """simple docstring""" super().__init__( feature_size=_a , sampling_rate=_a , padding_value=_a , **_a , ) lowerCamelCase = spectrogram_length lowerCamelCase = num_channels lowerCamelCase = patch_size lowerCamelCase = feature_size // self.patch_size[1] lowerCamelCase = n_fft lowerCamelCase = sampling_rate // hop_length_to_sampling_rate lowerCamelCase = sampling_rate lowerCamelCase = padding_value lowerCamelCase = mel_filter_bank( num_frequency_bins=1 + n_fft // 2 , num_mel_filters=_a , min_frequency=0.0 , max_frequency=22_050.0 , sampling_rate=_a , norm="""slaney""" , mel_scale="""slaney""" , ).T def _lowerCAmelCase ( self , _a ): """simple docstring""" lowerCamelCase = spectrogram( _a , window_function(self.n_fft , """hann""" ) , frame_length=self.n_fft , hop_length=self.hop_length , power=2.0 , mel_filters=self.mel_filters.T , log_mel="""dB""" , db_range=80.0 , ) lowerCamelCase = log_spec[:, :-1] lowerCamelCase = log_spec - 20.0 lowerCamelCase = np.clip(log_spec / 40.0 , -2.0 , 0.0 ) + 1.0 return log_spec def __call__( self , _a , _a = None , _a = True , _a = None , _a = False , _a = False , **_a , ): """simple docstring""" if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( """This feature extractor is set to support sampling rate""" f' of {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled' f' 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.""" ) lowerCamelCase = isinstance(_a , np.ndarray ) and len(raw_speech.shape ) > 1 if is_batched_numpy and len(raw_speech.shape ) > 2: raise ValueError(f'Only mono-channel audio is supported for input to {self}' ) lowerCamelCase = is_batched_numpy or ( isinstance(_a , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: lowerCamelCase = [np.asarray([speech] , dtype=np.floataa ).T for speech in raw_speech] elif not is_batched and not isinstance(_a , np.ndarray ): lowerCamelCase = np.asarray(_a , dtype=np.floataa ) elif isinstance(_a , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): lowerCamelCase = raw_speech.astype(np.floataa ) # always return batch if not is_batched: lowerCamelCase = [np.asarray([raw_speech] ).T] # Convert audio signals to log mel spectrograms, truncate by time axis lowerCamelCase = [ self._np_extract_fbank_features(waveform.squeeze() ).T[: self.spectrogram_length] for waveform in raw_speech ] if isinstance(audio_features[0] , _a ): lowerCamelCase = [np.asarray(_a , dtype=np.floataa ) for feature in audio_features] # Create audio attention mask lowerCamelCase = max( [ceil(feature.shape[0] / self.patch_size[0] ) * self.freq_len for feature in audio_features] ) # The maximum number of audio patches in a batch if return_attention_mask: lowerCamelCase = [ (ceil(feature.shape[0] / self.patch_size[0] ) * self.freq_len) * [1] + (max_patch_len - ceil(feature.shape[0] / self.patch_size[0] ) * self.freq_len) * [0] for feature in audio_features ] lowerCamelCase = np.array(_a ).astype(np.floataa ) # convert into correct format for padding lowerCamelCase = max_patch_len // self.freq_len * self.patch_size[0] # The maximum audio size in a batch lowerCamelCase = np.ones([len(_a ), 1, max_time_len, self.feature_size] ).astype(np.floataa ) lowerCamelCase = padded_audio_features * self.padding_value for i in range(len(_a ) ): lowerCamelCase = audio_features[i] lowerCamelCase = feature # return as BatchFeature if return_attention_mask: lowerCamelCase = {"""audio_values""": padded_audio_features, """audio_mask""": audio_mask} else: lowerCamelCase = {"""audio_values""": padded_audio_features} lowerCamelCase = BatchFeature(data=_a , tensor_type=_a ) return encoded_inputs
291
1
"""simple docstring""" from PIL import Image def a__ ( snake_case__ , snake_case__ ) -> Image: lowerCamelCase = (2_59 * (level + 2_55)) / (2_55 * (2_59 - level)) def contrast(snake_case__ ) -> int: return int(1_28 + factor * (c - 1_28) ) return img.point(snake_case__ ) if __name__ == "__main__": # Load image with Image.open("""image_data/lena.jpg""") as img: # Change contrast to 170 lowerCAmelCase : Optional[Any] = change_contrast(img, 170) cont_img.save("""image_data/lena_high_contrast.png""", format="""png""")
291
"""simple docstring""" from math import ceil def a__ ( snake_case__ , snake_case__ ) -> Optional[int]: lowerCamelCase = list(range(0 , snake_case__ ) ) lowerCamelCase = [item for sublist in list(device_map.values() ) for item in sublist] # Duplicate check lowerCamelCase = [] for i in device_map_blocks: if device_map_blocks.count(snake_case__ ) > 1 and i not in duplicate_blocks: duplicate_blocks.append(snake_case__ ) # Missing blocks lowerCamelCase = [i for i in blocks if i not in device_map_blocks] lowerCamelCase = [i for i in device_map_blocks if i not in blocks] if len(snake_case__ ) != 0: raise ValueError( """Duplicate attention blocks specified in device_map. Attention blocks must be specified to one device.""" """ These attention blocks were specified more than once: """ + str(snake_case__ ) ) if len(snake_case__ ) != 0: raise ValueError( """There are attention blocks for this model that are not specified in the device_map. Add these attention """ """blocks to a device on the device_map: """ + str(snake_case__ ) ) if len(snake_case__ ) != 0: raise ValueError( """The device_map contains more attention blocks than this model has. Remove these from the device_map:""" + str(snake_case__ ) ) def a__ ( snake_case__ , snake_case__ ) -> List[Any]: lowerCamelCase = list(range(snake_case__ ) ) lowerCamelCase = int(ceil(n_layers / len(snake_case__ ) ) ) lowerCamelCase = [layers[i : i + n_blocks] for i in range(0 , snake_case__ , snake_case__ )] return dict(zip(snake_case__ , snake_case__ ) )
291
1
"""simple docstring""" # Copyright 2023 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available lowerCAmelCase : Union[str, Any] = { """configuration_vivit""": ["""VIVIT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """VivitConfig"""], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : List[Any] = ["""VivitImageProcessor"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : int = [ """VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST""", """VivitModel""", """VivitPreTrainedModel""", """VivitForVideoClassification""", ] if TYPE_CHECKING: from .configuration_vivit import VIVIT_PRETRAINED_CONFIG_ARCHIVE_MAP, VivitConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_vivit import VivitImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vivit import ( VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST, VivitForVideoClassification, VivitModel, VivitPreTrainedModel, ) else: import sys lowerCAmelCase : Tuple = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
291
"""simple docstring""" import unittest import numpy as np from transformers import RoFormerConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax.numpy as jnp from transformers.models.roformer.modeling_flax_roformer import ( FlaxRoFormerForMaskedLM, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerModel, ) class __magic_name__ ( unittest.TestCase ): '''simple docstring''' def __init__( self , _a , _a=13 , _a=7 , _a=True , _a=True , _a=True , _a=True , _a=99 , _a=32 , _a=5 , _a=4 , _a=37 , _a="gelu" , _a=0.1 , _a=0.1 , _a=512 , _a=16 , _a=2 , _a=0.02 , _a=4 , ): """simple docstring""" lowerCamelCase = parent lowerCamelCase = batch_size lowerCamelCase = seq_length lowerCamelCase = is_training lowerCamelCase = use_attention_mask lowerCamelCase = use_token_type_ids lowerCamelCase = use_labels lowerCamelCase = vocab_size lowerCamelCase = hidden_size lowerCamelCase = num_hidden_layers lowerCamelCase = num_attention_heads lowerCamelCase = intermediate_size lowerCamelCase = hidden_act lowerCamelCase = hidden_dropout_prob lowerCamelCase = attention_probs_dropout_prob lowerCamelCase = max_position_embeddings lowerCamelCase = type_vocab_size lowerCamelCase = type_sequence_label_size lowerCamelCase = initializer_range lowerCamelCase = num_choices def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) lowerCamelCase = None if self.use_attention_mask: lowerCamelCase = random_attention_mask([self.batch_size, self.seq_length] ) lowerCamelCase = None if self.use_token_type_ids: lowerCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) lowerCamelCase = RoFormerConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=_a , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.prepare_config_and_inputs() lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase = config_and_inputs lowerCamelCase = {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": attention_mask} return config, inputs_dict @require_flax class __magic_name__ ( UpperCAmelCase__ , unittest.TestCase ): '''simple docstring''' __UpperCamelCase = True __UpperCamelCase = ( ( FlaxRoFormerModel, FlaxRoFormerForMaskedLM, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, ) if is_flax_available() else () ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = FlaxRoFormerModelTester(self ) @slow def _lowerCAmelCase ( self ): """simple docstring""" for model_class_name in self.all_model_classes: lowerCamelCase = model_class_name.from_pretrained("""junnyu/roformer_chinese_small""" , from_pt=_a ) lowerCamelCase = model(np.ones((1, 1) ) ) self.assertIsNotNone(_a ) @require_flax class __magic_name__ ( unittest.TestCase ): '''simple docstring''' @slow def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = FlaxRoFormerForMaskedLM.from_pretrained("""junnyu/roformer_chinese_base""" ) lowerCamelCase = jnp.array([[0, 1, 2, 3, 4, 5]] ) lowerCamelCase = model(_a )[0] lowerCamelCase = 50_000 lowerCamelCase = (1, 6, vocab_size) self.assertEqual(output.shape , _a ) lowerCamelCase = jnp.array( [[[-0.1_205, -1.0_265, 0.2_922], [-1.5_134, 0.1_974, 0.1_519], [-5.0_135, -3.9_003, -0.8_404]]] ) self.assertTrue(jnp.allclose(output[:, :3, :3] , _a , atol=1e-4 ) )
291
1
"""simple docstring""" import os import sys import warnings from dataclasses import dataclass, field from io import BytesIO from typing import TYPE_CHECKING, Any, ClassVar, Dict, List, Optional, Union import numpy as np import pyarrow as pa from .. import config from ..download.streaming_download_manager import xopen from ..table import array_cast from ..utils.file_utils import is_local_path from ..utils.py_utils import first_non_null_value, no_op_if_value_is_null, string_to_dict if TYPE_CHECKING: import PIL.Image from .features import FeatureType lowerCAmelCase : Optional[List[str]] = None lowerCAmelCase : Optional[int] = """<""" 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 lowerCAmelCase : List[Any] = [ 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 __magic_name__ : '''simple docstring''' __UpperCamelCase = True __UpperCamelCase = None # Automatically constructed __UpperCamelCase = "PIL.Image.Image" __UpperCamelCase = pa.struct({"bytes": pa.binary(), "path": pa.string()} ) __UpperCamelCase = field(default="Image" , init=UpperCAmelCase__ , repr=UpperCAmelCase__ ) def __call__( self ): """simple docstring""" return self.pa_type def _lowerCAmelCase ( self , _a ): """simple docstring""" if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError("""To support encoding images, please install 'Pillow'.""" ) if isinstance(_a , _a ): lowerCamelCase = np.array(_a ) if isinstance(_a , _a ): return {"path": value, "bytes": None} elif isinstance(_a , _a ): return {"path": None, "bytes": value} elif isinstance(_a , np.ndarray ): # convert the image array to PNG/TIFF bytes return encode_np_array(_a ) elif isinstance(_a , PIL.Image.Image ): # convert the PIL image to bytes (default format is PNG/TIFF) return encode_pil_image(_a ) elif value.get("""path""" ) is not None and os.path.isfile(value["""path"""] ): # we set "bytes": None to not duplicate the data if they're already available locally return {"bytes": None, "path": value.get("""path""" )} elif value.get("""bytes""" ) is not None or value.get("""path""" ) is not None: # store the image bytes, and path is used to infer the image format using the file extension return {"bytes": value.get("""bytes""" ), "path": value.get("""path""" )} else: raise ValueError( f'An image sample should have one of \'path\' or \'bytes\' but they are missing or None in {value}.' ) def _lowerCAmelCase ( self , _a , _a=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: lowerCamelCase = {} lowerCamelCase , lowerCamelCase = value["""path"""], value["""bytes"""] if bytes_ is None: if path is None: raise ValueError(f'An image should have one of \'path\' or \'bytes\' but both are None in {value}.' ) else: if is_local_path(_a ): lowerCamelCase = PIL.Image.open(_a ) else: lowerCamelCase = path.split("""::""" )[-1] try: lowerCamelCase = string_to_dict(_a , config.HUB_DATASETS_URL )["""repo_id"""] lowerCamelCase = token_per_repo_id.get(_a ) except ValueError: lowerCamelCase = None with xopen(_a , """rb""" , use_auth_token=_a ) as f: lowerCamelCase = BytesIO(f.read() ) lowerCamelCase = PIL.Image.open(bytes_ ) else: lowerCamelCase = PIL.Image.open(BytesIO(bytes_ ) ) image.load() # to avoid "Too many open files" errors return image def _lowerCAmelCase ( self ): """simple docstring""" from .features import Value return ( self if self.decode else { "bytes": Value("""binary""" ), "path": Value("""string""" ), } ) def _lowerCAmelCase ( self , _a ): """simple docstring""" if pa.types.is_string(storage.type ): lowerCamelCase = pa.array([None] * len(_a ) , type=pa.binary() ) lowerCamelCase = pa.StructArray.from_arrays([bytes_array, storage] , ["""bytes""", """path"""] , mask=storage.is_null() ) elif pa.types.is_binary(storage.type ): lowerCamelCase = pa.array([None] * len(_a ) , type=pa.string() ) lowerCamelCase = 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: lowerCamelCase = storage.field("""bytes""" ) else: lowerCamelCase = pa.array([None] * len(_a ) , type=pa.binary() ) if storage.type.get_field_index("""path""" ) >= 0: lowerCamelCase = storage.field("""path""" ) else: lowerCamelCase = pa.array([None] * len(_a ) , type=pa.string() ) lowerCamelCase = pa.StructArray.from_arrays([bytes_array, path_array] , ["""bytes""", """path"""] , mask=storage.is_null() ) elif pa.types.is_list(storage.type ): lowerCamelCase = pa.array( [encode_np_array(np.array(_a ) )["""bytes"""] if arr is not None else None for arr in storage.to_pylist()] , type=pa.binary() , ) lowerCamelCase = pa.array([None] * len(_a ) , type=pa.string() ) lowerCamelCase = pa.StructArray.from_arrays( [bytes_array, path_array] , ["""bytes""", """path"""] , mask=bytes_array.is_null() ) return array_cast(_a , self.pa_type ) def _lowerCAmelCase ( self , _a ): """simple docstring""" @no_op_if_value_is_null def path_to_bytes(_a ): with xopen(_a , """rb""" ) as f: lowerCamelCase = f.read() return bytes_ lowerCamelCase = 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() , ) lowerCamelCase = pa.array( [os.path.basename(_a ) if path is not None else None for path in storage.field("""path""" ).to_pylist()] , type=pa.string() , ) lowerCamelCase = pa.StructArray.from_arrays([bytes_array, path_array] , ["""bytes""", """path"""] , mask=bytes_array.is_null() ) return array_cast(_a , self.pa_type ) def a__ ( ) -> List[str]: 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() lowerCamelCase = list(set(PIL.Image.OPEN.keys() ) & set(PIL.Image.SAVE.keys() ) ) return _IMAGE_COMPRESSION_FORMATS def a__ ( snake_case__ ) -> bytes: lowerCamelCase = BytesIO() if image.format in list_image_compression_formats(): lowerCamelCase = image.format else: lowerCamelCase = """PNG""" if image.mode in ["""1""", """L""", """LA""", """RGB""", """RGBA"""] else """TIFF""" image.save(snake_case__ , format=snake_case__ ) return buffer.getvalue() def a__ ( snake_case__ ) -> dict: if hasattr(snake_case__ , """filename""" ) and image.filename != "": return {"path": image.filename, "bytes": None} else: return {"path": None, "bytes": image_to_bytes(snake_case__ )} def a__ ( snake_case__ ) -> dict: if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError("""To support encoding images, please install 'Pillow'.""" ) lowerCamelCase = array.dtype lowerCamelCase = dtype.byteorder if dtype.byteorder != """=""" else _NATIVE_BYTEORDER lowerCamelCase = dtype.kind lowerCamelCase = dtype.itemsize lowerCamelCase = None # Multi-channel array case (only np.dtype("|u1") is allowed) if array.shape[2:]: lowerCamelCase = 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: lowerCamelCase = 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: lowerCamelCase = dtype_byteorder + dtype_kind + str(snake_case__ ) lowerCamelCase = np.dtype(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}' ) lowerCamelCase = PIL.Image.fromarray(array.astype(snake_case__ ) ) return {"path": None, "bytes": image_to_bytes(snake_case__ )} def a__ ( snake_case__ ) -> List[dict]: if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError("""To support encoding images, please install 'Pillow'.""" ) if objs: lowerCamelCase , lowerCamelCase = first_non_null_value(snake_case__ ) if isinstance(snake_case__ , snake_case__ ): return [{"path": obj, "bytes": None} if obj is not None else None for obj in objs] if isinstance(snake_case__ , np.ndarray ): lowerCamelCase = no_op_if_value_is_null(snake_case__ ) return [obj_to_image_dict_func(snake_case__ ) for obj in objs] elif isinstance(snake_case__ , PIL.Image.Image ): lowerCamelCase = no_op_if_value_is_null(snake_case__ ) return [obj_to_image_dict_func(snake_case__ ) for obj in objs] else: return objs else: return objs
291
"""simple docstring""" from typing import Any def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ) -> list: _validation( snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ) # Creates data structures and fill initial step lowerCamelCase = {} lowerCamelCase = {} for state in states_space: lowerCamelCase = observations_space[0] lowerCamelCase = ( initial_probabilities[state] * emission_probabilities[state][observation] ) lowerCamelCase = None # Fills the data structure with the probabilities of # different transitions and pointers to previous states for o in range(1 , len(snake_case__ ) ): lowerCamelCase = observations_space[o] lowerCamelCase = observations_space[o - 1] for state in states_space: # Calculates the argmax for probability function lowerCamelCase = """""" lowerCamelCase = -1 for k_state in states_space: lowerCamelCase = ( probabilities[(k_state, prior_observation)] * transition_probabilities[k_state][state] * emission_probabilities[state][observation] ) if probability > max_probability: lowerCamelCase = probability lowerCamelCase = k_state # Update probabilities and pointers dicts lowerCamelCase = ( probabilities[(arg_max, prior_observation)] * transition_probabilities[arg_max][state] * emission_probabilities[state][observation] ) lowerCamelCase = arg_max # The final observation lowerCamelCase = observations_space[len(snake_case__ ) - 1] # argmax for given final observation lowerCamelCase = """""" lowerCamelCase = -1 for k_state in states_space: lowerCamelCase = probabilities[(k_state, final_observation)] if probability > max_probability: lowerCamelCase = probability lowerCamelCase = k_state lowerCamelCase = arg_max # Process pointers backwards lowerCamelCase = last_state lowerCamelCase = [] for o in range(len(snake_case__ ) - 1 , -1 , -1 ): result.append(snake_case__ ) lowerCamelCase = pointers[previous, observations_space[o]] result.reverse() return result def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ) -> None: _validate_not_empty( snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ) _validate_lists(snake_case__ , snake_case__ ) _validate_dicts( snake_case__ , snake_case__ , snake_case__ ) def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ) -> None: if not all( [ observations_space, states_space, initial_probabilities, transition_probabilities, emission_probabilities, ] ): raise ValueError("""There's an empty parameter""" ) def a__ ( snake_case__ , snake_case__ ) -> None: _validate_list(snake_case__ , """observations_space""" ) _validate_list(snake_case__ , """states_space""" ) def a__ ( snake_case__ , snake_case__ ) -> None: if not isinstance(_object , snake_case__ ): lowerCamelCase = F'{var_name} must be a list' raise ValueError(snake_case__ ) else: for x in _object: if not isinstance(snake_case__ , snake_case__ ): lowerCamelCase = F'{var_name} must be a list of strings' raise ValueError(snake_case__ ) def a__ ( snake_case__ , snake_case__ , snake_case__ , ) -> None: _validate_dict(snake_case__ , """initial_probabilities""" , snake_case__ ) _validate_nested_dict(snake_case__ , """transition_probabilities""" ) _validate_nested_dict(snake_case__ , """emission_probabilities""" ) def a__ ( snake_case__ , snake_case__ ) -> None: _validate_dict(_object , snake_case__ , snake_case__ ) for x in _object.values(): _validate_dict(snake_case__ , snake_case__ , snake_case__ , snake_case__ ) def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ = False ) -> None: if not isinstance(_object , snake_case__ ): lowerCamelCase = F'{var_name} must be a dict' raise ValueError(snake_case__ ) if not all(isinstance(snake_case__ , snake_case__ ) for x in _object ): lowerCamelCase = F'{var_name} all keys must be strings' raise ValueError(snake_case__ ) if not all(isinstance(snake_case__ , snake_case__ ) for x in _object.values() ): lowerCamelCase = """nested dictionary """ if nested else """""" lowerCamelCase = F'{var_name} {nested_text}all values must be {value_type.__name__}' raise ValueError(snake_case__ ) if __name__ == "__main__": from doctest import testmod testmod()
291
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tokenizers_available, is_torch_available, ) lowerCAmelCase : List[Any] = {"""configuration_reformer""": ["""REFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP""", """ReformerConfig"""]} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : List[str] = ["""ReformerTokenizer"""] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : Any = ["""ReformerTokenizerFast"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : List[str] = [ """REFORMER_PRETRAINED_MODEL_ARCHIVE_LIST""", """ReformerAttention""", """ReformerForMaskedLM""", """ReformerForQuestionAnswering""", """ReformerForSequenceClassification""", """ReformerLayer""", """ReformerModel""", """ReformerModelWithLMHead""", """ReformerPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_reformer import REFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, ReformerConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_reformer import ReformerTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_reformer_fast import ReformerTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_reformer import ( REFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, ReformerAttention, ReformerForMaskedLM, ReformerForQuestionAnswering, ReformerForSequenceClassification, ReformerLayer, ReformerModel, ReformerModelWithLMHead, ReformerPreTrainedModel, ) else: import sys lowerCAmelCase : str = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
291
"""simple docstring""" import argparse from pathlib import Path import torch from transformers import OPTConfig, OPTModel from transformers.utils import logging logging.set_verbosity_info() lowerCAmelCase : Dict = logging.get_logger(__name__) def a__ ( snake_case__ ) -> Dict: lowerCamelCase = torch.load(snake_case__ , map_location="""cpu""" ) if "model" in sd.keys(): lowerCamelCase = torch.load(snake_case__ , map_location="""cpu""" )["""model"""] # pop unnecessary weights lowerCamelCase = [ """decoder.version""", """decoder.output_projection.weight""", ] for key in keys_to_delete: if key in sd: sd.pop(snake_case__ ) lowerCamelCase = { """decoder.project_in_dim.weight""": """decoder.project_in.weight""", """decoder.project_out_dim.weight""": """decoder.project_out.weight""", """decoder.layer_norm.weight""": """decoder.final_layer_norm.weight""", """decoder.layer_norm.bias""": """decoder.final_layer_norm.bias""", } for old_key, new_key in keys_to_rename.items(): if old_key in sd: lowerCamelCase = sd.pop(snake_case__ ) lowerCamelCase = list(sd.keys() ) for key in keys: if ".qkv_proj." in key: lowerCamelCase = sd[key] # We split QKV in separate Q,K,V lowerCamelCase = key.replace(""".qkv_proj.""" , """.q_proj.""" ) lowerCamelCase = key.replace(""".qkv_proj.""" , """.k_proj.""" ) lowerCamelCase = key.replace(""".qkv_proj.""" , """.v_proj.""" ) lowerCamelCase = value.shape[0] assert depth % 3 == 0 # `SequeuceParallelTransformerBlock` has QKV weight is separated in K,V,Q despite the naming: # https://cs.github.com/facebookresearch/metaseq/blob/51871bd73cd04c038f239ea2a26db1d7f6b37927/metaseq/modules/sequence_parallel_transformer_layer.py#L97 lowerCamelCase , lowerCamelCase , lowerCamelCase = torch.split(snake_case__ , depth // 3 , dim=0 ) lowerCamelCase = q lowerCamelCase = k lowerCamelCase = v del sd[key] return sd @torch.no_grad() def a__ ( snake_case__ , snake_case__ , snake_case__=None ) -> Tuple: lowerCamelCase = load_checkpoint(snake_case__ ) if config is not None: lowerCamelCase = OPTConfig.from_pretrained(snake_case__ ) else: lowerCamelCase = OPTConfig() lowerCamelCase = OPTModel(snake_case__ ).half().eval() model.load_state_dict(snake_case__ ) # Check results Path(snake_case__ ).mkdir(exist_ok=snake_case__ ) model.save_pretrained(snake_case__ ) if __name__ == "__main__": lowerCAmelCase : List[str] = argparse.ArgumentParser() # Required parameters parser.add_argument( """--fairseq_path""", type=str, help=( """path to fairseq checkpoint in correct format. You can find all checkpoints in the correct format here:""" """ https://huggingface.co/models?other=opt_metasq""" ), ) parser.add_argument("""--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""") parser.add_argument("""--hf_config""", default=None, type=str, help="""Define HF config.""") lowerCAmelCase : Optional[Any] = parser.parse_args() convert_opt_checkpoint(args.fairseq_path, args.pytorch_dump_folder_path, config=args.hf_config)
291
1
"""simple docstring""" from typing import Dict from .base import GenericTensor, Pipeline class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' def _lowerCAmelCase ( self , _a=None , _a=None , _a=None , **_a ): """simple docstring""" if tokenize_kwargs is None: lowerCamelCase = {} if truncation is not None: if "truncation" in tokenize_kwargs: raise ValueError( """truncation parameter defined twice (given as keyword argument as well as in tokenize_kwargs)""" ) lowerCamelCase = truncation lowerCamelCase = tokenize_kwargs lowerCamelCase = {} if return_tensors is not None: lowerCamelCase = return_tensors return preprocess_params, {}, postprocess_params def _lowerCAmelCase ( self , _a , **_a ): """simple docstring""" lowerCamelCase = self.framework lowerCamelCase = self.tokenizer(_a , return_tensors=_a , **_a ) return model_inputs def _lowerCAmelCase ( self , _a ): """simple docstring""" lowerCamelCase = self.model(**_a ) return model_outputs def _lowerCAmelCase ( self , _a , _a=False ): """simple docstring""" # [0] is the first available tensor, logits or last_hidden_state. if return_tensors: return model_outputs[0] if self.framework == "pt": return model_outputs[0].tolist() elif self.framework == "tf": return model_outputs[0].numpy().tolist() def __call__( self , *_a , **_a ): """simple docstring""" return super().__call__(*_a , **_a )
291
"""simple docstring""" import json import os import shutil import tempfile import unittest import numpy as np from transformers import BertTokenizerFast from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES, BertTokenizer from transformers.testing_utils import require_tokenizers, require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import VisionTextDualEncoderProcessor, ViTImageProcessor @require_tokenizers @require_vision class __magic_name__ ( unittest.TestCase ): '''simple docstring''' def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = tempfile.mkdtemp() # fmt: off lowerCamelCase = ["""[UNK]""", """[CLS]""", """[SEP]""", """[PAD]""", """[MASK]""", """want""", """##want""", """##ed""", """wa""", """un""", """runn""", """##ing""", """,""", """low""", """lowest"""] # fmt: on lowerCamelCase = 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] ) ) lowerCamelCase = { """do_resize""": True, """size""": {"""height""": 18, """width""": 18}, """do_normalize""": True, """image_mean""": [0.5, 0.5, 0.5], """image_std""": [0.5, 0.5, 0.5], } lowerCamelCase = os.path.join(self.tmpdirname , _a ) with open(self.image_processor_file , """w""" , encoding="""utf-8""" ) as fp: json.dump(_a , _a ) def _lowerCAmelCase ( self , **_a ): """simple docstring""" return BertTokenizer.from_pretrained(self.tmpdirname , **_a ) def _lowerCAmelCase ( self , **_a ): """simple docstring""" return ViTImageProcessor.from_pretrained(self.tmpdirname , **_a ) def _lowerCAmelCase ( self ): """simple docstring""" shutil.rmtree(self.tmpdirname ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = [np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta )] lowerCamelCase = [Image.fromarray(np.moveaxis(_a , 0 , -1 ) ) for x in image_inputs] return image_inputs def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.get_tokenizer() lowerCamelCase = self.get_image_processor() lowerCamelCase = VisionTextDualEncoderProcessor(tokenizer=_a , image_processor=_a ) processor.save_pretrained(self.tmpdirname ) lowerCamelCase = VisionTextDualEncoderProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer.get_vocab() ) self.assertIsInstance(processor.tokenizer , (BertTokenizer, BertTokenizerFast) ) self.assertEqual(processor.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertIsInstance(processor.image_processor , _a ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = VisionTextDualEncoderProcessor( tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) lowerCamelCase = self.get_tokenizer(bos_token="""(BOS)""" , eos_token="""(EOS)""" ) lowerCamelCase = self.get_image_processor(do_normalize=_a , padding_value=1.0 ) lowerCamelCase = VisionTextDualEncoderProcessor.from_pretrained( self.tmpdirname , bos_token="""(BOS)""" , eos_token="""(EOS)""" , do_normalize=_a , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , (BertTokenizer, BertTokenizerFast) ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , _a ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.get_image_processor() lowerCamelCase = self.get_tokenizer() lowerCamelCase = VisionTextDualEncoderProcessor(tokenizer=_a , image_processor=_a ) lowerCamelCase = self.prepare_image_inputs() lowerCamelCase = image_processor(_a , return_tensors="""np""" ) lowerCamelCase = processor(images=_a , return_tensors="""np""" ) for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() , input_processor[key].sum() , delta=1e-2 ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.get_image_processor() lowerCamelCase = self.get_tokenizer() lowerCamelCase = VisionTextDualEncoderProcessor(tokenizer=_a , image_processor=_a ) lowerCamelCase = """lower newer""" lowerCamelCase = processor(text=_a ) lowerCamelCase = tokenizer(_a ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.get_image_processor() lowerCamelCase = self.get_tokenizer() lowerCamelCase = VisionTextDualEncoderProcessor(tokenizer=_a , image_processor=_a ) lowerCamelCase = """lower newer""" lowerCamelCase = self.prepare_image_inputs() lowerCamelCase = processor(text=_a , images=_a ) self.assertListEqual(list(inputs.keys() ) , ["""input_ids""", """token_type_ids""", """attention_mask""", """pixel_values"""] ) # test if it raises when no input is passed with self.assertRaises(_a ): processor() def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.get_image_processor() lowerCamelCase = self.get_tokenizer() lowerCamelCase = VisionTextDualEncoderProcessor(tokenizer=_a , image_processor=_a ) lowerCamelCase = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] lowerCamelCase = processor.batch_decode(_a ) lowerCamelCase = tokenizer.batch_decode(_a ) self.assertListEqual(_a , _a ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.get_image_processor() lowerCamelCase = self.get_tokenizer() lowerCamelCase = VisionTextDualEncoderProcessor(tokenizer=_a , image_processor=_a ) lowerCamelCase = """lower newer""" lowerCamelCase = self.prepare_image_inputs() lowerCamelCase = processor(text=_a , images=_a ) self.assertListEqual(list(inputs.keys() ) , processor.model_input_names )
291
1
"""simple docstring""" from collections import OrderedDict from ...utils import logging from .auto_factory import _BaseAutoModelClass, _LazyAutoMapping, auto_class_update from .configuration_auto import CONFIG_MAPPING_NAMES lowerCAmelCase : List[Any] = logging.get_logger(__name__) lowerCAmelCase : Any = OrderedDict( [ # Base model mapping ("""albert""", """FlaxAlbertModel"""), ("""bart""", """FlaxBartModel"""), ("""beit""", """FlaxBeitModel"""), ("""bert""", """FlaxBertModel"""), ("""big_bird""", """FlaxBigBirdModel"""), ("""blenderbot""", """FlaxBlenderbotModel"""), ("""blenderbot-small""", """FlaxBlenderbotSmallModel"""), ("""clip""", """FlaxCLIPModel"""), ("""distilbert""", """FlaxDistilBertModel"""), ("""electra""", """FlaxElectraModel"""), ("""gpt-sw3""", """FlaxGPT2Model"""), ("""gpt2""", """FlaxGPT2Model"""), ("""gpt_neo""", """FlaxGPTNeoModel"""), ("""gptj""", """FlaxGPTJModel"""), ("""longt5""", """FlaxLongT5Model"""), ("""marian""", """FlaxMarianModel"""), ("""mbart""", """FlaxMBartModel"""), ("""mt5""", """FlaxMT5Model"""), ("""opt""", """FlaxOPTModel"""), ("""pegasus""", """FlaxPegasusModel"""), ("""regnet""", """FlaxRegNetModel"""), ("""resnet""", """FlaxResNetModel"""), ("""roberta""", """FlaxRobertaModel"""), ("""roberta-prelayernorm""", """FlaxRobertaPreLayerNormModel"""), ("""roformer""", """FlaxRoFormerModel"""), ("""t5""", """FlaxT5Model"""), ("""vision-text-dual-encoder""", """FlaxVisionTextDualEncoderModel"""), ("""vit""", """FlaxViTModel"""), ("""wav2vec2""", """FlaxWav2Vec2Model"""), ("""whisper""", """FlaxWhisperModel"""), ("""xglm""", """FlaxXGLMModel"""), ("""xlm-roberta""", """FlaxXLMRobertaModel"""), ] ) lowerCAmelCase : List[str] = OrderedDict( [ # Model for pre-training mapping ("""albert""", """FlaxAlbertForPreTraining"""), ("""bart""", """FlaxBartForConditionalGeneration"""), ("""bert""", """FlaxBertForPreTraining"""), ("""big_bird""", """FlaxBigBirdForPreTraining"""), ("""electra""", """FlaxElectraForPreTraining"""), ("""longt5""", """FlaxLongT5ForConditionalGeneration"""), ("""mbart""", """FlaxMBartForConditionalGeneration"""), ("""mt5""", """FlaxMT5ForConditionalGeneration"""), ("""roberta""", """FlaxRobertaForMaskedLM"""), ("""roberta-prelayernorm""", """FlaxRobertaPreLayerNormForMaskedLM"""), ("""roformer""", """FlaxRoFormerForMaskedLM"""), ("""t5""", """FlaxT5ForConditionalGeneration"""), ("""wav2vec2""", """FlaxWav2Vec2ForPreTraining"""), ("""whisper""", """FlaxWhisperForConditionalGeneration"""), ("""xlm-roberta""", """FlaxXLMRobertaForMaskedLM"""), ] ) lowerCAmelCase : int = OrderedDict( [ # Model for Masked LM mapping ("""albert""", """FlaxAlbertForMaskedLM"""), ("""bart""", """FlaxBartForConditionalGeneration"""), ("""bert""", """FlaxBertForMaskedLM"""), ("""big_bird""", """FlaxBigBirdForMaskedLM"""), ("""distilbert""", """FlaxDistilBertForMaskedLM"""), ("""electra""", """FlaxElectraForMaskedLM"""), ("""mbart""", """FlaxMBartForConditionalGeneration"""), ("""roberta""", """FlaxRobertaForMaskedLM"""), ("""roberta-prelayernorm""", """FlaxRobertaPreLayerNormForMaskedLM"""), ("""roformer""", """FlaxRoFormerForMaskedLM"""), ("""xlm-roberta""", """FlaxXLMRobertaForMaskedLM"""), ] ) lowerCAmelCase : List[Any] = OrderedDict( [ # Model for Seq2Seq Causal LM mapping ("""bart""", """FlaxBartForConditionalGeneration"""), ("""blenderbot""", """FlaxBlenderbotForConditionalGeneration"""), ("""blenderbot-small""", """FlaxBlenderbotSmallForConditionalGeneration"""), ("""encoder-decoder""", """FlaxEncoderDecoderModel"""), ("""longt5""", """FlaxLongT5ForConditionalGeneration"""), ("""marian""", """FlaxMarianMTModel"""), ("""mbart""", """FlaxMBartForConditionalGeneration"""), ("""mt5""", """FlaxMT5ForConditionalGeneration"""), ("""pegasus""", """FlaxPegasusForConditionalGeneration"""), ("""t5""", """FlaxT5ForConditionalGeneration"""), ] ) lowerCAmelCase : Any = OrderedDict( [ # Model for Image-classsification ("""beit""", """FlaxBeitForImageClassification"""), ("""regnet""", """FlaxRegNetForImageClassification"""), ("""resnet""", """FlaxResNetForImageClassification"""), ("""vit""", """FlaxViTForImageClassification"""), ] ) lowerCAmelCase : str = OrderedDict( [ ("""vision-encoder-decoder""", """FlaxVisionEncoderDecoderModel"""), ] ) lowerCAmelCase : List[str] = OrderedDict( [ # Model for Causal LM mapping ("""bart""", """FlaxBartForCausalLM"""), ("""bert""", """FlaxBertForCausalLM"""), ("""big_bird""", """FlaxBigBirdForCausalLM"""), ("""electra""", """FlaxElectraForCausalLM"""), ("""gpt-sw3""", """FlaxGPT2LMHeadModel"""), ("""gpt2""", """FlaxGPT2LMHeadModel"""), ("""gpt_neo""", """FlaxGPTNeoForCausalLM"""), ("""gptj""", """FlaxGPTJForCausalLM"""), ("""opt""", """FlaxOPTForCausalLM"""), ("""roberta""", """FlaxRobertaForCausalLM"""), ("""roberta-prelayernorm""", """FlaxRobertaPreLayerNormForCausalLM"""), ("""xglm""", """FlaxXGLMForCausalLM"""), ("""xlm-roberta""", """FlaxXLMRobertaForCausalLM"""), ] ) lowerCAmelCase : Union[str, Any] = OrderedDict( [ # Model for Sequence Classification mapping ("""albert""", """FlaxAlbertForSequenceClassification"""), ("""bart""", """FlaxBartForSequenceClassification"""), ("""bert""", """FlaxBertForSequenceClassification"""), ("""big_bird""", """FlaxBigBirdForSequenceClassification"""), ("""distilbert""", """FlaxDistilBertForSequenceClassification"""), ("""electra""", """FlaxElectraForSequenceClassification"""), ("""mbart""", """FlaxMBartForSequenceClassification"""), ("""roberta""", """FlaxRobertaForSequenceClassification"""), ("""roberta-prelayernorm""", """FlaxRobertaPreLayerNormForSequenceClassification"""), ("""roformer""", """FlaxRoFormerForSequenceClassification"""), ("""xlm-roberta""", """FlaxXLMRobertaForSequenceClassification"""), ] ) lowerCAmelCase : Optional[Any] = OrderedDict( [ # Model for Question Answering mapping ("""albert""", """FlaxAlbertForQuestionAnswering"""), ("""bart""", """FlaxBartForQuestionAnswering"""), ("""bert""", """FlaxBertForQuestionAnswering"""), ("""big_bird""", """FlaxBigBirdForQuestionAnswering"""), ("""distilbert""", """FlaxDistilBertForQuestionAnswering"""), ("""electra""", """FlaxElectraForQuestionAnswering"""), ("""mbart""", """FlaxMBartForQuestionAnswering"""), ("""roberta""", """FlaxRobertaForQuestionAnswering"""), ("""roberta-prelayernorm""", """FlaxRobertaPreLayerNormForQuestionAnswering"""), ("""roformer""", """FlaxRoFormerForQuestionAnswering"""), ("""xlm-roberta""", """FlaxXLMRobertaForQuestionAnswering"""), ] ) lowerCAmelCase : List[str] = OrderedDict( [ # Model for Token Classification mapping ("""albert""", """FlaxAlbertForTokenClassification"""), ("""bert""", """FlaxBertForTokenClassification"""), ("""big_bird""", """FlaxBigBirdForTokenClassification"""), ("""distilbert""", """FlaxDistilBertForTokenClassification"""), ("""electra""", """FlaxElectraForTokenClassification"""), ("""roberta""", """FlaxRobertaForTokenClassification"""), ("""roberta-prelayernorm""", """FlaxRobertaPreLayerNormForTokenClassification"""), ("""roformer""", """FlaxRoFormerForTokenClassification"""), ("""xlm-roberta""", """FlaxXLMRobertaForTokenClassification"""), ] ) lowerCAmelCase : Optional[Any] = OrderedDict( [ # Model for Multiple Choice mapping ("""albert""", """FlaxAlbertForMultipleChoice"""), ("""bert""", """FlaxBertForMultipleChoice"""), ("""big_bird""", """FlaxBigBirdForMultipleChoice"""), ("""distilbert""", """FlaxDistilBertForMultipleChoice"""), ("""electra""", """FlaxElectraForMultipleChoice"""), ("""roberta""", """FlaxRobertaForMultipleChoice"""), ("""roberta-prelayernorm""", """FlaxRobertaPreLayerNormForMultipleChoice"""), ("""roformer""", """FlaxRoFormerForMultipleChoice"""), ("""xlm-roberta""", """FlaxXLMRobertaForMultipleChoice"""), ] ) lowerCAmelCase : Union[str, Any] = OrderedDict( [ ("""bert""", """FlaxBertForNextSentencePrediction"""), ] ) lowerCAmelCase : int = OrderedDict( [ ("""speech-encoder-decoder""", """FlaxSpeechEncoderDecoderModel"""), ("""whisper""", """FlaxWhisperForConditionalGeneration"""), ] ) lowerCAmelCase : List[str] = OrderedDict( [ ("""whisper""", """FlaxWhisperForAudioClassification"""), ] ) lowerCAmelCase : List[Any] = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_MAPPING_NAMES) lowerCAmelCase : Union[str, Any] = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_PRETRAINING_MAPPING_NAMES) lowerCAmelCase : Tuple = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_MASKED_LM_MAPPING_NAMES) lowerCAmelCase : Any = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES ) lowerCAmelCase : Optional[int] = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES ) lowerCAmelCase : int = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_VISION_2_SEQ_MAPPING_NAMES) lowerCAmelCase : Any = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_CAUSAL_LM_MAPPING_NAMES) lowerCAmelCase : Any = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES ) lowerCAmelCase : str = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES ) lowerCAmelCase : str = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES ) lowerCAmelCase : Union[str, Any] = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_MULTIPLE_CHOICE_MAPPING_NAMES ) lowerCAmelCase : List[str] = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPING_NAMES ) lowerCAmelCase : int = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING_NAMES ) lowerCAmelCase : str = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES ) class __magic_name__ ( _BaseAutoModelClass ): '''simple docstring''' __UpperCamelCase = FLAX_MODEL_MAPPING lowerCAmelCase : Tuple = auto_class_update(FlaxAutoModel) class __magic_name__ ( _BaseAutoModelClass ): '''simple docstring''' __UpperCamelCase = FLAX_MODEL_FOR_PRETRAINING_MAPPING lowerCAmelCase : Union[str, Any] = auto_class_update(FlaxAutoModelForPreTraining, head_doc="""pretraining""") class __magic_name__ ( _BaseAutoModelClass ): '''simple docstring''' __UpperCamelCase = FLAX_MODEL_FOR_CAUSAL_LM_MAPPING lowerCAmelCase : int = auto_class_update(FlaxAutoModelForCausalLM, head_doc="""causal language modeling""") class __magic_name__ ( _BaseAutoModelClass ): '''simple docstring''' __UpperCamelCase = FLAX_MODEL_FOR_MASKED_LM_MAPPING lowerCAmelCase : str = auto_class_update(FlaxAutoModelForMaskedLM, head_doc="""masked language modeling""") class __magic_name__ ( _BaseAutoModelClass ): '''simple docstring''' __UpperCamelCase = FLAX_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING lowerCAmelCase : Tuple = auto_class_update( FlaxAutoModelForSeqaSeqLM, head_doc="""sequence-to-sequence language modeling""", checkpoint_for_example="""t5-base""" ) class __magic_name__ ( _BaseAutoModelClass ): '''simple docstring''' __UpperCamelCase = FLAX_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING lowerCAmelCase : Optional[Any] = auto_class_update( FlaxAutoModelForSequenceClassification, head_doc="""sequence classification""" ) class __magic_name__ ( _BaseAutoModelClass ): '''simple docstring''' __UpperCamelCase = FLAX_MODEL_FOR_QUESTION_ANSWERING_MAPPING lowerCAmelCase : Optional[int] = auto_class_update(FlaxAutoModelForQuestionAnswering, head_doc="""question answering""") class __magic_name__ ( _BaseAutoModelClass ): '''simple docstring''' __UpperCamelCase = FLAX_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING lowerCAmelCase : Tuple = auto_class_update( FlaxAutoModelForTokenClassification, head_doc="""token classification""" ) class __magic_name__ ( _BaseAutoModelClass ): '''simple docstring''' __UpperCamelCase = FLAX_MODEL_FOR_MULTIPLE_CHOICE_MAPPING lowerCAmelCase : Dict = auto_class_update(FlaxAutoModelForMultipleChoice, head_doc="""multiple choice""") class __magic_name__ ( _BaseAutoModelClass ): '''simple docstring''' __UpperCamelCase = FLAX_MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPING lowerCAmelCase : int = auto_class_update( FlaxAutoModelForNextSentencePrediction, head_doc="""next sentence prediction""" ) class __magic_name__ ( _BaseAutoModelClass ): '''simple docstring''' __UpperCamelCase = FLAX_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING lowerCAmelCase : Optional[int] = auto_class_update( FlaxAutoModelForImageClassification, head_doc="""image classification""" ) class __magic_name__ ( _BaseAutoModelClass ): '''simple docstring''' __UpperCamelCase = FLAX_MODEL_FOR_VISION_2_SEQ_MAPPING lowerCAmelCase : List[Any] = auto_class_update(FlaxAutoModelForVisionaSeq, head_doc="""vision-to-text modeling""") class __magic_name__ ( _BaseAutoModelClass ): '''simple docstring''' __UpperCamelCase = FLAX_MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING lowerCAmelCase : str = auto_class_update( FlaxAutoModelForSpeechSeqaSeq, head_doc="""sequence-to-sequence speech-to-text modeling""" )
291
"""simple docstring""" import importlib import sys from argparse import REMAINDER, ArgumentParser from pathlib import Path import torch_xla.distributed.xla_multiprocessing as xmp def a__ ( ) -> Union[str, Any]: lowerCamelCase = ArgumentParser( description=( """PyTorch TPU distributed training launch """ """helper utility that will spawn up """ """multiple distributed processes""" ) ) # Optional arguments for the launch helper parser.add_argument("""--num_cores""" , type=snake_case__ , default=1 , help="""Number of TPU cores to use (1 or 8).""" ) # positional parser.add_argument( """training_script""" , type=snake_case__ , help=( """The full path to the single TPU training """ """program/script to be launched in parallel, """ """followed by all the arguments for the """ """training script""" ) , ) # rest from the training program parser.add_argument("""training_script_args""" , nargs=snake_case__ ) return parser.parse_args() def a__ ( ) -> List[str]: lowerCamelCase = parse_args() # Import training_script as a module. lowerCamelCase = Path(args.training_script ) sys.path.append(str(script_fpath.parent.resolve() ) ) lowerCamelCase = script_fpath.stem lowerCamelCase = importlib.import_module(snake_case__ ) # Patch sys.argv lowerCamelCase = [args.training_script] + args.training_script_args + ["""--tpu_num_cores""", str(args.num_cores )] xmp.spawn(mod._mp_fn , args=() , nprocs=args.num_cores ) if __name__ == "__main__": main()
291
1
"""simple docstring""" import unittest from knapsack import greedy_knapsack as kp class __magic_name__ ( unittest.TestCase ): '''simple docstring''' def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = [10, 20, 30, 40, 50, 60] lowerCamelCase = [2, 4, 6, 8, 10, 12] lowerCamelCase = 100 self.assertEqual(kp.calc_profit(_a , _a , _a ) , 210 ) def _lowerCAmelCase ( self ): """simple docstring""" self.assertRaisesRegex(_a , """max_weight must greater than zero.""" ) def _lowerCAmelCase ( self ): """simple docstring""" self.assertRaisesRegex(_a , """Weight can not be negative.""" ) def _lowerCAmelCase ( self ): """simple docstring""" self.assertRaisesRegex(_a , """Profit can not be negative.""" ) def _lowerCAmelCase ( self ): """simple docstring""" self.assertRaisesRegex(_a , """max_weight must greater than zero.""" ) def _lowerCAmelCase ( self ): """simple docstring""" self.assertRaisesRegex( _a , """The length of profit and weight must be same.""" ) if __name__ == "__main__": unittest.main()
291
"""simple docstring""" import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase : int = logging.get_logger(__name__) lowerCAmelCase : List[str] = { """asapp/sew-d-tiny-100k""": """https://huggingface.co/asapp/sew-d-tiny-100k/resolve/main/config.json""", # See all SEW-D models at https://huggingface.co/models?filter=sew-d } class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' __UpperCamelCase = "sew-d" def __init__( self , _a=32 , _a=768 , _a=12 , _a=12 , _a=3_072 , _a=2 , _a=512 , _a=256 , _a=True , _a=True , _a=("p2c", "c2p") , _a="layer_norm" , _a="gelu_python" , _a=0.1 , _a=0.1 , _a=0.1 , _a=0.0 , _a=0.1 , _a=0.02 , _a=1e-7 , _a=1e-5 , _a="group" , _a="gelu" , _a=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512) , _a=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1) , _a=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1) , _a=False , _a=128 , _a=16 , _a=True , _a=0.05 , _a=10 , _a=2 , _a=0.0 , _a=10 , _a=0 , _a="mean" , _a=False , _a=False , _a=256 , _a=0 , _a=1 , _a=2 , **_a , ): """simple docstring""" super().__init__(**_a , pad_token_id=_a , bos_token_id=_a , eos_token_id=_a ) lowerCamelCase = hidden_size lowerCamelCase = feat_extract_norm lowerCamelCase = feat_extract_activation lowerCamelCase = list(_a ) lowerCamelCase = list(_a ) lowerCamelCase = list(_a ) lowerCamelCase = conv_bias lowerCamelCase = num_conv_pos_embeddings lowerCamelCase = num_conv_pos_embedding_groups lowerCamelCase = len(self.conv_dim ) lowerCamelCase = num_hidden_layers lowerCamelCase = intermediate_size lowerCamelCase = squeeze_factor lowerCamelCase = max_position_embeddings lowerCamelCase = position_buckets lowerCamelCase = share_att_key lowerCamelCase = relative_attention lowerCamelCase = norm_rel_ebd lowerCamelCase = list(_a ) lowerCamelCase = hidden_act lowerCamelCase = num_attention_heads lowerCamelCase = hidden_dropout lowerCamelCase = attention_dropout lowerCamelCase = activation_dropout lowerCamelCase = feat_proj_dropout lowerCamelCase = final_dropout lowerCamelCase = layer_norm_eps lowerCamelCase = feature_layer_norm_eps lowerCamelCase = initializer_range lowerCamelCase = vocab_size if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( """Configuration for convolutional layers is incorrect.""" """It is required that `len(config.conv_dim)` == `len(config.conv_stride)` == `len(config.conv_kernel)`,""" f'but is `len(config.conv_dim) = {len(self.conv_dim )}`, `len(config.conv_stride)' f'= {len(self.conv_stride )}`, `len(config.conv_kernel) = {len(self.conv_kernel )}`.' ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 lowerCamelCase = apply_spec_augment lowerCamelCase = mask_time_prob lowerCamelCase = mask_time_length lowerCamelCase = mask_time_min_masks lowerCamelCase = mask_feature_prob lowerCamelCase = mask_feature_length lowerCamelCase = mask_feature_min_masks # ctc loss lowerCamelCase = ctc_loss_reduction lowerCamelCase = ctc_zero_infinity # sequence classification lowerCamelCase = use_weighted_layer_sum lowerCamelCase = classifier_proj_size @property def _lowerCAmelCase ( self ): """simple docstring""" return functools.reduce(operator.mul , self.conv_stride , 1 )
291
1
"""simple docstring""" def a__ ( snake_case__ ) -> list: if len(snake_case__ ) < 2: return collection def circle_sort_util(snake_case__ , snake_case__ , snake_case__ ) -> bool: lowerCamelCase = False if low == high: return swapped lowerCamelCase = low lowerCamelCase = high while left < right: if collection[left] > collection[right]: lowerCamelCase , lowerCamelCase = ( collection[right], collection[left], ) lowerCamelCase = True left += 1 right -= 1 if left == right and collection[left] > collection[right + 1]: lowerCamelCase , lowerCamelCase = ( collection[right + 1], collection[left], ) lowerCamelCase = True lowerCamelCase = low + int((high - low) / 2 ) lowerCamelCase = circle_sort_util(snake_case__ , snake_case__ , snake_case__ ) lowerCamelCase = circle_sort_util(snake_case__ , mid + 1 , snake_case__ ) return swapped or left_swap or right_swap lowerCamelCase = True while is_not_sorted is True: lowerCamelCase = circle_sort_util(snake_case__ , 0 , len(snake_case__ ) - 1 ) return collection if __name__ == "__main__": lowerCAmelCase : Tuple = input("""Enter numbers separated by a comma:\n""").strip() lowerCAmelCase : List[Any] = [int(item) for item in user_input.split(""",""")] print(circle_sort(unsorted))
291
"""simple docstring""" from sklearn.metrics import recall_score import datasets lowerCAmelCase : Any = """ Recall is the fraction of the positive examples that were correctly labeled by the model as positive. It can be computed with the equation: Recall = TP / (TP + FN) Where TP is the true positives and FN is the false negatives. """ lowerCAmelCase : Any = """ Args: - **predictions** (`list` of `int`): The predicted labels. - **references** (`list` of `int`): The ground truth labels. - **labels** (`list` of `int`): The set of labels to include when `average` is not set to `binary`, and their order when average is `None`. Labels present in the data can be excluded in this input, for example to calculate a multiclass average ignoring a majority negative class, while labels not present in the data will result in 0 components in a macro average. For multilabel targets, labels are column indices. By default, all labels in y_true and y_pred are used in sorted order. Defaults to None. - **pos_label** (`int`): The class label to use as the 'positive class' when calculating the recall. Defaults to `1`. - **average** (`string`): This parameter is required for multiclass/multilabel targets. If None, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data. Defaults to `'binary'`. - `'binary'`: Only report results for the class specified by `pos_label`. This is applicable only if the target labels and predictions are binary. - `'micro'`: Calculate metrics globally by counting the total true positives, false negatives, and false positives. - `'macro'`: Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account. - `'weighted'`: Calculate metrics for each label, and find their average weighted by support (the number of true instances for each label). This alters `'macro'` to account for label imbalance. Note that it can result in an F-score that is not between precision and recall. - `'samples'`: Calculate metrics for each instance, and find their average (only meaningful for multilabel classification). - **sample_weight** (`list` of `float`): Sample weights Defaults to `None`. - **zero_division** (): Sets the value to return when there is a zero division. Defaults to . - `'warn'`: If there is a zero division, the return value is `0`, but warnings are also raised. - `0`: If there is a zero division, the return value is `0`. - `1`: If there is a zero division, the return value is `1`. Returns: - **recall** (`float`, or `array` of `float`): Either the general recall score, or the recall scores for individual classes, depending on the values input to `labels` and `average`. Minimum possible value is 0. Maximum possible value is 1. A higher recall means that more of the positive examples have been labeled correctly. Therefore, a higher recall is generally considered better. Examples: Example 1-A simple example with some errors >>> recall_metric = datasets.load_metric('recall') >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1]) >>> print(results) {'recall': 0.6666666666666666} Example 2-The same example as Example 1, but with `pos_label=0` instead of the default `pos_label=1`. >>> recall_metric = datasets.load_metric('recall') >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1], pos_label=0) >>> print(results) {'recall': 0.5} Example 3-The same example as Example 1, but with `sample_weight` included. >>> recall_metric = datasets.load_metric('recall') >>> sample_weight = [0.9, 0.2, 0.9, 0.3, 0.8] >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1], sample_weight=sample_weight) >>> print(results) {'recall': 0.55} Example 4-A multiclass example, using different averages. >>> recall_metric = datasets.load_metric('recall') >>> predictions = [0, 2, 1, 0, 0, 1] >>> references = [0, 1, 2, 0, 1, 2] >>> results = recall_metric.compute(predictions=predictions, references=references, average='macro') >>> print(results) {'recall': 0.3333333333333333} >>> results = recall_metric.compute(predictions=predictions, references=references, average='micro') >>> print(results) {'recall': 0.3333333333333333} >>> results = recall_metric.compute(predictions=predictions, references=references, average='weighted') >>> print(results) {'recall': 0.3333333333333333} >>> results = recall_metric.compute(predictions=predictions, references=references, average=None) >>> print(results) {'recall': array([1., 0., 0.])} """ lowerCAmelCase : Any = """ @article{scikit-learn, title={Scikit-learn: Machine Learning in {P}ython}, author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V. and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P. and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.}, journal={Journal of Machine Learning Research}, volume={12}, pages={2825--2830}, year={2011} """ @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __magic_name__ ( datasets.Metric ): '''simple docstring''' def _lowerCAmelCase ( self ): """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": datasets.Sequence(datasets.Value("""int32""" ) ), """references""": datasets.Sequence(datasets.Value("""int32""" ) ), } if self.config_name == """multilabel""" else { """predictions""": datasets.Value("""int32""" ), """references""": datasets.Value("""int32""" ), } ) , reference_urls=["""https://scikit-learn.org/stable/modules/generated/sklearn.metrics.recall_score.html"""] , ) def _lowerCAmelCase ( self , _a , _a , _a=None , _a=1 , _a="binary" , _a=None , _a="warn" , ): """simple docstring""" lowerCamelCase = recall_score( _a , _a , labels=_a , pos_label=_a , average=_a , sample_weight=_a , zero_division=_a , ) return {"recall": float(_a ) if score.size == 1 else score}
291
1
"""simple docstring""" import warnings from ...utils import logging from .image_processing_imagegpt import ImageGPTImageProcessor lowerCAmelCase : Dict = logging.get_logger(__name__) class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' def __init__( self , *_a , **_a ): """simple docstring""" warnings.warn( """The class ImageGPTFeatureExtractor is deprecated and will be removed in version 5 of Transformers.""" """ Please use ImageGPTImageProcessor instead.""" , _a , ) super().__init__(*_a , **_a )
291
"""simple docstring""" import numpy as np import torch from torch.utils.data import Dataset, IterableDataset from ..utils.generic import ModelOutput class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' def __init__( self , _a , _a , _a ): """simple docstring""" lowerCamelCase = dataset lowerCamelCase = process lowerCamelCase = params def __len__( self ): """simple docstring""" return len(self.dataset ) def __getitem__( self , _a ): """simple docstring""" lowerCamelCase = self.dataset[i] lowerCamelCase = self.process(_a , **self.params ) return processed class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' def __init__( self , _a , _a , _a , _a=None ): """simple docstring""" lowerCamelCase = loader lowerCamelCase = infer lowerCamelCase = params if loader_batch_size == 1: # Let's spare some time by deactivating altogether lowerCamelCase = None lowerCamelCase = loader_batch_size # Internal bookkeeping lowerCamelCase = None lowerCamelCase = None def __len__( self ): """simple docstring""" return len(self.loader ) def __iter__( self ): """simple docstring""" lowerCamelCase = iter(self.loader ) return self def _lowerCAmelCase ( self ): """simple docstring""" if isinstance(self._loader_batch_data , torch.Tensor ): # Batch data is simple tensor, just fetch the slice lowerCamelCase = self._loader_batch_data[self._loader_batch_index] else: # Batch data is assumed to be BaseModelOutput (or dict) lowerCamelCase = {} for k, element in self._loader_batch_data.items(): if isinstance(_a , _a ): # Convert ModelOutput to tuple first lowerCamelCase = element.to_tuple() if isinstance(element[0] , torch.Tensor ): lowerCamelCase = tuple(el[self._loader_batch_index].unsqueeze(0 ) for el in element ) elif isinstance(element[0] , np.ndarray ): lowerCamelCase = tuple(np.expand_dims(el[self._loader_batch_index] , 0 ) for el in element ) continue if k in {"hidden_states", "past_key_values", "attentions"} and isinstance(_a , _a ): # Those are stored as lists of tensors so need specific unbatching. if isinstance(element[0] , torch.Tensor ): lowerCamelCase = tuple(el[self._loader_batch_index].unsqueeze(0 ) for el in element ) elif isinstance(element[0] , np.ndarray ): lowerCamelCase = tuple(np.expand_dims(el[self._loader_batch_index] , 0 ) for el in element ) continue if element is None: # This can happen for optional data that get passed around lowerCamelCase = None elif isinstance(element[self._loader_batch_index] , torch.Tensor ): # Take correct batch data, but make it looked like batch_size=1 # For compatibility with other methods within transformers lowerCamelCase = element[self._loader_batch_index].unsqueeze(0 ) elif isinstance(element[self._loader_batch_index] , np.ndarray ): # Take correct batch data, but make it looked like batch_size=1 # For compatibility with other methods within transformers lowerCamelCase = np.expand_dims(element[self._loader_batch_index] , 0 ) else: # This is typically a list, so no need to `unsqueeze`. lowerCamelCase = element[self._loader_batch_index] # Recreate the element by reusing the original class to make it look # batch_size=1 lowerCamelCase = self._loader_batch_data.__class__(_a ) self._loader_batch_index += 1 return result def _lowerCAmelCase ( self ): """simple docstring""" if self._loader_batch_index is not None and self._loader_batch_index < self.loader_batch_size: # We are currently unrolling a batch so we just need to return # the current item within a batch return self.loader_batch_item() # We're out of items within a batch lowerCamelCase = next(self.iterator ) lowerCamelCase = self.infer(_a , **self.params ) # We now have a batch of "inferred things". if self.loader_batch_size is not None: # Try to infer the size of the batch if isinstance(_a , torch.Tensor ): lowerCamelCase = processed else: lowerCamelCase = list(processed.keys() )[0] lowerCamelCase = processed[key] if isinstance(_a , _a ): lowerCamelCase = len(_a ) else: lowerCamelCase = first_tensor.shape[0] if 0 < observed_batch_size < self.loader_batch_size: # could be last batch so we can't unroll as many # elements. lowerCamelCase = observed_batch_size # Setting internal index to unwrap the batch lowerCamelCase = processed lowerCamelCase = 0 return self.loader_batch_item() else: # We're not unrolling batches return processed class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' def __init__( self , _a , _a , _a , _a=None ): """simple docstring""" super().__init__(_a , _a , _a ) def __iter__( self ): """simple docstring""" lowerCamelCase = iter(self.loader ) lowerCamelCase = None return self def _lowerCAmelCase ( self ): """simple docstring""" if self.subiterator is None: lowerCamelCase = self.infer(next(self.iterator ) , **self.params ) try: # Try to return next item lowerCamelCase = next(self.subiterator ) except StopIteration: # When a preprocess iterator ends, we can start lookig at the next item # ChunkIterator will keep feeding until ALL elements of iterator # all have created their subiterator and have been iterating against. # # Another way to look at it, is we're basically flattening lists of lists # into a single list, but with generators lowerCamelCase = self.infer(next(self.iterator ) , **self.params ) lowerCamelCase = next(self.subiterator ) return processed class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' def __iter__( self ): """simple docstring""" lowerCamelCase = iter(self.loader ) return self def _lowerCAmelCase ( self ): """simple docstring""" # Extremely similar to PipelineIterator in its unpacking mechanism # BUT, we have an extra required item which is the presence of `is_last` # That is because everything is flattened by `PipelineChunkIterator` we # need to keep track of how to regroup here in the original `process` # boundaries so that `process` and `postprocess` see the same data. # This iterator accumulates items (possibly while unbatching) until it # its a `is_last` and then just passes it on to the caller. lowerCamelCase = False lowerCamelCase = [] if self._loader_batch_index is not None and self._loader_batch_index < self.loader_batch_size: while self._loader_batch_index < self.loader_batch_size: lowerCamelCase = self.loader_batch_item() lowerCamelCase = item.pop("""is_last""" ) accumulator.append(_a ) if is_last: return accumulator while not is_last: lowerCamelCase = self.infer(next(self.iterator ) , **self.params ) if self.loader_batch_size is not None: if isinstance(_a , torch.Tensor ): lowerCamelCase = processed else: lowerCamelCase = list(processed.keys() )[0] lowerCamelCase = processed[key] if isinstance(_a , _a ): lowerCamelCase = len(_a ) else: lowerCamelCase = first_tensor.shape[0] if 0 < observed_batch_size < self.loader_batch_size: # could be last batch so we can't unroll as many # elements. lowerCamelCase = observed_batch_size lowerCamelCase = processed lowerCamelCase = 0 while self._loader_batch_index < self.loader_batch_size: lowerCamelCase = self.loader_batch_item() lowerCamelCase = item.pop("""is_last""" ) accumulator.append(_a ) if is_last: return accumulator else: lowerCamelCase = processed lowerCamelCase = item.pop("""is_last""" ) accumulator.append(_a ) return accumulator class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' def __init__( self , _a , _a ): """simple docstring""" lowerCamelCase = dataset lowerCamelCase = key def __len__( self ): """simple docstring""" return len(self.dataset ) def __getitem__( self , _a ): """simple docstring""" return self.dataset[i][self.key] class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' def __init__( self , _a , _a , _a ): """simple docstring""" lowerCamelCase = dataset lowerCamelCase = keya lowerCamelCase = keya def __len__( self ): """simple docstring""" return len(self.dataset ) def __getitem__( self , _a ): """simple docstring""" return {"text": self.dataset[i][self.keya], "text_pair": self.dataset[i][self.keya]}
291
1
"""simple docstring""" from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCAmelCase : str = { """configuration_informer""": [ """INFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP""", """InformerConfig""", ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : int = [ """INFORMER_PRETRAINED_MODEL_ARCHIVE_LIST""", """InformerForPrediction""", """InformerModel""", """InformerPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_informer import INFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, InformerConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_informer import ( INFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, InformerForPrediction, InformerModel, InformerPreTrainedModel, ) else: import sys lowerCAmelCase : Optional[int] = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
291
"""simple docstring""" def a__ ( snake_case__ ) -> bool: lowerCamelCase = (1 + 24 * n) ** 0.5 return ((1 + root) / 6) % 1 == 0 def a__ ( snake_case__ = 50_00 ) -> int: lowerCamelCase = [(i * (3 * i - 1)) // 2 for i in range(1 , snake_case__ )] for i, pentagonal_i in enumerate(snake_case__ ): for j in range(snake_case__ , len(snake_case__ ) ): lowerCamelCase = pentagonal_nums[j] lowerCamelCase = pentagonal_i + pentagonal_j lowerCamelCase = pentagonal_j - pentagonal_i if is_pentagonal(snake_case__ ) and is_pentagonal(snake_case__ ): return b return -1 if __name__ == "__main__": print(F"""{solution() = }""")
291
1
"""simple docstring""" from __future__ import annotations from collections.abc import Callable from typing import Generic, TypeVar lowerCAmelCase : Optional[int] = TypeVar("""T""") lowerCAmelCase : Dict = TypeVar("""U""") class __magic_name__ ( Generic[T, U] ): '''simple docstring''' def __init__( self , _a , _a ): """simple docstring""" lowerCamelCase = key lowerCamelCase = val lowerCamelCase = None lowerCamelCase = None def __repr__( self ): """simple docstring""" return ( f'Node: key: {self.key}, val: {self.val}, ' f'has next: {bool(self.next )}, has prev: {bool(self.prev )}' ) class __magic_name__ ( Generic[T, U] ): '''simple docstring''' def __init__( self ): """simple docstring""" lowerCamelCase = DoubleLinkedListNode(_a , _a ) lowerCamelCase = DoubleLinkedListNode(_a , _a ) lowerCamelCase , lowerCamelCase = self.rear, self.head def __repr__( self ): """simple docstring""" lowerCamelCase = ["""DoubleLinkedList"""] lowerCamelCase = self.head while node.next is not None: rep.append(str(_a ) ) lowerCamelCase = node.next rep.append(str(self.rear ) ) return ",\n ".join(_a ) def _lowerCAmelCase ( self , _a ): """simple docstring""" lowerCamelCase = self.rear.prev # All nodes other than self.head are guaranteed to have non-None previous assert previous is not None lowerCamelCase = node lowerCamelCase = previous lowerCamelCase = node lowerCamelCase = self.rear def _lowerCAmelCase ( self , _a ): """simple docstring""" if node.prev is None or node.next is None: return None lowerCamelCase = node.next lowerCamelCase = node.prev lowerCamelCase = None lowerCamelCase = None return node class __magic_name__ ( Generic[T, U] ): '''simple docstring''' __UpperCamelCase = {} def __init__( self , _a ): """simple docstring""" lowerCamelCase = DoubleLinkedList() lowerCamelCase = capacity lowerCamelCase = 0 lowerCamelCase = 0 lowerCamelCase = 0 lowerCamelCase = {} def __repr__( self ): """simple docstring""" return ( f'CacheInfo(hits={self.hits}, misses={self.miss}, ' f'capacity={self.capacity}, current size={self.num_keys})' ) def __contains__( self , _a ): """simple docstring""" return key in self.cache def _lowerCAmelCase ( self , _a ): """simple docstring""" # Note: pythonic interface would throw KeyError rather than return None if key in self.cache: self.hits += 1 lowerCamelCase = self.cache[key] lowerCamelCase = self.list.remove(self.cache[key] ) assert node == value_node # node is guaranteed not None because it is in self.cache assert node is not None self.list.add(_a ) return node.val self.miss += 1 return None def _lowerCAmelCase ( self , _a , _a ): """simple docstring""" if key not in self.cache: if self.num_keys >= self.capacity: # delete first node (oldest) when over capacity lowerCamelCase = self.list.head.next # guaranteed to have a non-None first node when num_keys > 0 # explain to type checker via assertions assert first_node is not None assert first_node.key is not None assert ( self.list.remove(_a ) is not None ) # node guaranteed to be in list assert node.key is not None del self.cache[first_node.key] self.num_keys -= 1 lowerCamelCase = DoubleLinkedListNode(_a , _a ) self.list.add(self.cache[key] ) self.num_keys += 1 else: # bump node to the end of the list, update value lowerCamelCase = self.list.remove(self.cache[key] ) assert node is not None # node guaranteed to be in list lowerCamelCase = value self.list.add(_a ) @classmethod def _lowerCAmelCase ( cls , _a = 128 ): """simple docstring""" def cache_decorator_inner(_a ) -> Callable[..., U]: def cache_decorator_wrapper(*_a ) -> U: if func not in cls.decorator_function_to_instance_map: lowerCamelCase = LRUCache(_a ) lowerCamelCase = cls.decorator_function_to_instance_map[func].get(args[0] ) if result is None: lowerCamelCase = func(*_a ) cls.decorator_function_to_instance_map[func].put(args[0] , _a ) return result def cache_info() -> LRUCache[T, U]: return cls.decorator_function_to_instance_map[func] setattr(_a , """cache_info""" , _a ) # noqa: B010 return cache_decorator_wrapper return cache_decorator_inner if __name__ == "__main__": import doctest doctest.testmod()
291
"""simple docstring""" from pickle import UnpicklingError import jax import jax.numpy as jnp import numpy as np from flax.serialization import from_bytes from flax.traverse_util import flatten_dict from ..utils import logging lowerCAmelCase : Tuple = logging.get_logger(__name__) def a__ ( snake_case__ , snake_case__ ) -> Tuple: try: with open(snake_case__ , """rb""" ) as flax_state_f: lowerCamelCase = from_bytes(snake_case__ , flax_state_f.read() ) except UnpicklingError as e: try: with open(snake_case__ ) as f: if f.read().startswith("""version""" ): raise OSError( """You seem to have cloned a repository without having git-lfs installed. Please""" """ install git-lfs and run `git lfs install` followed by `git lfs pull` in the""" """ folder you cloned.""" ) else: raise ValueError from e except (UnicodeDecodeError, ValueError): raise EnvironmentError(F'Unable to convert {model_file} to Flax deserializable object. ' ) return load_flax_weights_in_pytorch_model(snake_case__ , snake_case__ ) def a__ ( snake_case__ , snake_case__ ) -> Tuple: try: import torch # noqa: F401 except ImportError: logger.error( """Loading Flax weights in PyTorch requires both PyTorch and Flax to be installed. Please see""" """ https://pytorch.org/ and https://flax.readthedocs.io/en/latest/installation.html for installation""" """ instructions.""" ) raise # check if we have bf16 weights lowerCamelCase = flatten_dict(jax.tree_util.tree_map(lambda snake_case__ : x.dtype == jnp.bfloataa , snake_case__ ) ).values() if any(snake_case__ ): # convert all weights to fp32 if they are bf16 since torch.from_numpy can-not handle bf16 # and bf16 is not fully supported in PT yet. logger.warning( """Found ``bfloat16`` weights in Flax model. Casting all ``bfloat16`` weights to ``float32`` """ """before loading those in PyTorch model.""" ) lowerCamelCase = jax.tree_util.tree_map( lambda snake_case__ : params.astype(np.floataa ) if params.dtype == jnp.bfloataa else params , snake_case__ ) lowerCamelCase = """""" lowerCamelCase = flatten_dict(snake_case__ , sep=""".""" ) lowerCamelCase = pt_model.state_dict() # keep track of unexpected & missing keys lowerCamelCase = [] lowerCamelCase = set(pt_model_dict.keys() ) for flax_key_tuple, flax_tensor in flax_state_dict.items(): lowerCamelCase = flax_key_tuple.split(""".""" ) if flax_key_tuple_array[-1] == "kernel" and flax_tensor.ndim == 4: lowerCamelCase = flax_key_tuple_array[:-1] + ["""weight"""] lowerCamelCase = jnp.transpose(snake_case__ , (3, 2, 0, 1) ) elif flax_key_tuple_array[-1] == "kernel": lowerCamelCase = flax_key_tuple_array[:-1] + ["""weight"""] lowerCamelCase = flax_tensor.T elif flax_key_tuple_array[-1] == "scale": lowerCamelCase = flax_key_tuple_array[:-1] + ["""weight"""] if "time_embedding" not in flax_key_tuple_array: for i, flax_key_tuple_string in enumerate(snake_case__ ): lowerCamelCase = ( flax_key_tuple_string.replace("""_0""" , """.0""" ) .replace("""_1""" , """.1""" ) .replace("""_2""" , """.2""" ) .replace("""_3""" , """.3""" ) .replace("""_4""" , """.4""" ) .replace("""_5""" , """.5""" ) .replace("""_6""" , """.6""" ) .replace("""_7""" , """.7""" ) .replace("""_8""" , """.8""" ) .replace("""_9""" , """.9""" ) ) lowerCamelCase = """.""".join(snake_case__ ) if flax_key in pt_model_dict: if flax_tensor.shape != pt_model_dict[flax_key].shape: raise ValueError( F'Flax checkpoint seems to be incorrect. Weight {flax_key_tuple} was expected ' F'to be of shape {pt_model_dict[flax_key].shape}, but is {flax_tensor.shape}.' ) else: # add weight to pytorch dict lowerCamelCase = np.asarray(snake_case__ ) if not isinstance(snake_case__ , np.ndarray ) else flax_tensor lowerCamelCase = torch.from_numpy(snake_case__ ) # remove from missing keys missing_keys.remove(snake_case__ ) else: # weight is not expected by PyTorch model unexpected_keys.append(snake_case__ ) pt_model.load_state_dict(snake_case__ ) # re-transform missing_keys to list lowerCamelCase = list(snake_case__ ) if len(snake_case__ ) > 0: logger.warning( """Some weights of the Flax model were not used when initializing the PyTorch model""" F' {pt_model.__class__.__name__}: {unexpected_keys}\n- This IS expected if you are initializing' F' {pt_model.__class__.__name__} from a Flax model trained on another task or with another architecture' """ (e.g. initializing a BertForSequenceClassification model from a FlaxBertForPreTraining model).\n- This""" F' IS NOT expected if you are initializing {pt_model.__class__.__name__} from a Flax model that you expect' """ to be exactly identical (e.g. initializing a BertForSequenceClassification model from a""" """ FlaxBertForSequenceClassification model).""" ) if len(snake_case__ ) > 0: logger.warning( F'Some weights of {pt_model.__class__.__name__} were not initialized from the Flax model and are newly' F' initialized: {missing_keys}\nYou should probably TRAIN this model on a down-stream task to be able to' """ use it for predictions and inference.""" ) return pt_model
291
1
"""simple docstring""" import argparse import os # New Code # import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType from accelerate.utils import find_executable_batch_size ######################################################################## # This is a fully working simple example to use Accelerate, # specifically showcasing how to ensure out-of-memory errors never # interrupt training, and builds off the `nlp_example.py` script. # # This example trains a Bert base model on GLUE MRPC # in any of the following settings (with the same script): # - single CPU or single GPU # - multi GPUS (using PyTorch distributed mode) # - (multi) TPUs # - fp16 (mixed-precision) or fp32 (normal precision) # # New additions from the base script can be found quickly by # looking for the # New Code # tags # # To run it in each of these various modes, follow the instructions # in the readme for examples: # https://github.com/huggingface/accelerate/tree/main/examples # ######################################################################## lowerCAmelCase : Optional[int] = 16 lowerCAmelCase : Any = 32 def a__ ( snake_case__ , snake_case__ = 16 ) -> List[str]: lowerCamelCase = AutoTokenizer.from_pretrained("""bert-base-cased""" ) lowerCamelCase = load_dataset("""glue""" , """mrpc""" ) def tokenize_function(snake_case__ ): # max_length=None => use the model max length (it's actually the default) lowerCamelCase = tokenizer(examples["""sentence1"""] , examples["""sentence2"""] , truncation=snake_case__ , max_length=snake_case__ ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset # starting with the main process first: with accelerator.main_process_first(): lowerCamelCase = datasets.map( snake_case__ , batched=snake_case__ , remove_columns=["""idx""", """sentence1""", """sentence2"""] , ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library lowerCamelCase = tokenized_datasets.rename_column("""label""" , """labels""" ) def collate_fn(snake_case__ ): # On TPU it's best to pad everything to the same length or training will be very slow. lowerCamelCase = 1_28 if accelerator.distributed_type == DistributedType.TPU else None # When using mixed precision we want round multiples of 8/16 if accelerator.mixed_precision == "fp8": lowerCamelCase = 16 elif accelerator.mixed_precision != "no": lowerCamelCase = 8 else: lowerCamelCase = None return tokenizer.pad( snake_case__ , padding="""longest""" , max_length=snake_case__ , pad_to_multiple_of=snake_case__ , return_tensors="""pt""" , ) # Instantiate dataloaders. lowerCamelCase = DataLoader( tokenized_datasets["""train"""] , shuffle=snake_case__ , collate_fn=snake_case__ , batch_size=snake_case__ ) lowerCamelCase = DataLoader( tokenized_datasets["""validation"""] , shuffle=snake_case__ , collate_fn=snake_case__ , batch_size=snake_case__ ) return train_dataloader, eval_dataloader # For testing only if os.environ.get("""TESTING_MOCKED_DATALOADERS""", None) == "1": from accelerate.test_utils.training import mocked_dataloaders lowerCAmelCase : Tuple = mocked_dataloaders # noqa: F811 def a__ ( snake_case__ , snake_case__ ) -> Dict: # For testing only if os.environ.get("""TESTING_MOCKED_DATALOADERS""" , snake_case__ ) == "1": lowerCamelCase = 2 # Initialize accelerator lowerCamelCase = Accelerator(cpu=args.cpu , mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs lowerCamelCase = config["""lr"""] lowerCamelCase = int(config["""num_epochs"""] ) lowerCamelCase = int(config["""seed"""] ) lowerCamelCase = int(config["""batch_size"""] ) lowerCamelCase = evaluate.load("""glue""" , """mrpc""" ) # New Code # # We now can define an inner training loop function. It should take a batch size as the only parameter, # and build the dataloaders in there. # It also gets our decorator @find_executable_batch_size(starting_batch_size=snake_case__ ) def inner_training_loop(snake_case__ ): # And now just move everything below under this function # We need to bring in the Accelerator object from earlier nonlocal accelerator # And reset all of its attributes that could hold onto any memory: accelerator.free_memory() # Then we can declare the model, optimizer, and everything else: set_seed(snake_case__ ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) lowerCamelCase = AutoModelForSequenceClassification.from_pretrained("""bert-base-cased""" , return_dict=snake_case__ ) # We could avoid this line since the accelerator is set with `device_placement=True` (default value). # Note that if you are placing tensors on devices manually, this line absolutely needs to be before the optimizer # creation otherwise training will not work on TPU (`accelerate` will kindly throw an error to make us aware of that). lowerCamelCase = model.to(accelerator.device ) # Instantiate optimizer lowerCamelCase = AdamW(params=model.parameters() , lr=snake_case__ ) lowerCamelCase , lowerCamelCase = get_dataloaders(snake_case__ , snake_case__ ) # Instantiate scheduler lowerCamelCase = get_linear_schedule_with_warmup( optimizer=snake_case__ , num_warmup_steps=1_00 , num_training_steps=(len(snake_case__ ) * num_epochs) , ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase = accelerator.prepare( snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ ) # Now we train the model for epoch in range(snake_case__ ): model.train() for step, batch in enumerate(snake_case__ ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) lowerCamelCase = model(**snake_case__ ) lowerCamelCase = outputs.loss accelerator.backward(snake_case__ ) optimizer.step() lr_scheduler.step() optimizer.zero_grad() model.eval() for step, batch in enumerate(snake_case__ ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): lowerCamelCase = model(**snake_case__ ) lowerCamelCase = outputs.logits.argmax(dim=-1 ) lowerCamelCase , lowerCamelCase = accelerator.gather_for_metrics((predictions, batch["""labels"""]) ) metric.add_batch( predictions=snake_case__ , references=snake_case__ , ) lowerCamelCase = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(F'epoch {epoch}:' , snake_case__ ) # New Code # # And call it at the end with no arguments # Note: You could also refactor this outside of your training loop function inner_training_loop() def a__ ( ) -> List[str]: lowerCamelCase = argparse.ArgumentParser(description="""Simple example of training script.""" ) parser.add_argument( """--mixed_precision""" , type=snake_case__ , default=snake_case__ , choices=["""no""", """fp16""", """bf16""", """fp8"""] , help="""Whether to use mixed precision. Choose""" """between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10.""" """and an Nvidia Ampere GPU.""" , ) parser.add_argument("""--cpu""" , action="""store_true""" , help="""If passed, will train on the CPU.""" ) lowerCamelCase = parser.parse_args() lowerCamelCase = {"""lr""": 2E-5, """num_epochs""": 3, """seed""": 42, """batch_size""": 16} training_function(snake_case__ , snake_case__ ) if __name__ == "__main__": main()
291
"""simple docstring""" import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_xlnet import XLNetTokenizer else: lowerCAmelCase : int = None lowerCAmelCase : Tuple = logging.get_logger(__name__) lowerCAmelCase : Tuple = {"""vocab_file""": """spiece.model""", """tokenizer_file""": """tokenizer.json"""} lowerCAmelCase : Union[str, Any] = { """vocab_file""": { """xlnet-base-cased""": """https://huggingface.co/xlnet-base-cased/resolve/main/spiece.model""", """xlnet-large-cased""": """https://huggingface.co/xlnet-large-cased/resolve/main/spiece.model""", }, """tokenizer_file""": { """xlnet-base-cased""": """https://huggingface.co/xlnet-base-cased/resolve/main/tokenizer.json""", """xlnet-large-cased""": """https://huggingface.co/xlnet-large-cased/resolve/main/tokenizer.json""", }, } lowerCAmelCase : Optional[int] = { """xlnet-base-cased""": None, """xlnet-large-cased""": None, } lowerCAmelCase : Union[str, Any] = """▁""" # Segments (not really needed) lowerCAmelCase : str = 0 lowerCAmelCase : Optional[int] = 1 lowerCAmelCase : Tuple = 2 lowerCAmelCase : Optional[Any] = 3 lowerCAmelCase : List[Any] = 4 class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' __UpperCamelCase = VOCAB_FILES_NAMES __UpperCamelCase = PRETRAINED_VOCAB_FILES_MAP __UpperCamelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCamelCase = "left" __UpperCamelCase = XLNetTokenizer def __init__( self , _a=None , _a=None , _a=False , _a=True , _a=False , _a="<s>" , _a="</s>" , _a="<unk>" , _a="<sep>" , _a="<pad>" , _a="<cls>" , _a="<mask>" , _a=["<eop>", "<eod>"] , **_a , ): """simple docstring""" # Mask token behave like a normal word, i.e. include the space before it lowerCamelCase = AddedToken(_a , lstrip=_a , rstrip=_a ) if isinstance(_a , _a ) else mask_token super().__init__( vocab_file=_a , tokenizer_file=_a , do_lower_case=_a , remove_space=_a , keep_accents=_a , bos_token=_a , eos_token=_a , unk_token=_a , sep_token=_a , pad_token=_a , cls_token=_a , mask_token=_a , additional_special_tokens=_a , **_a , ) lowerCamelCase = 3 lowerCamelCase = do_lower_case lowerCamelCase = remove_space lowerCamelCase = keep_accents lowerCamelCase = vocab_file lowerCamelCase = False if not self.vocab_file else True def _lowerCAmelCase ( self , _a , _a = None ): """simple docstring""" lowerCamelCase = [self.sep_token_id] lowerCamelCase = [self.cls_token_id] if token_ids_a is None: return token_ids_a + sep + cls return token_ids_a + sep + token_ids_a + sep + cls def _lowerCAmelCase ( self , _a , _a = None ): """simple docstring""" lowerCamelCase = [self.sep_token_id] lowerCamelCase = [2] if token_ids_a is None: return len(token_ids_a + sep ) * [0] + cls_segment_id return len(token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] + cls_segment_id def _lowerCAmelCase ( self , _a , _a = 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(_a ): logger.error(f'Vocabulary path ({save_directory}) should be a directory' ) return lowerCamelCase = os.path.join( _a , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_a ): copyfile(self.vocab_file , _a ) return (out_vocab_file,)
291
1
"""simple docstring""" from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowerCAmelCase : Tuple = logging.get_logger(__name__) lowerCAmelCase : Optional[Any] = { """facebook/data2vec-text-base""": """https://huggingface.co/data2vec/resolve/main/config.json""", } class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' __UpperCamelCase = "data2vec-text" def __init__( self , _a=30_522 , _a=768 , _a=12 , _a=12 , _a=3_072 , _a="gelu" , _a=0.1 , _a=0.1 , _a=512 , _a=2 , _a=0.02 , _a=1e-1_2 , _a=1 , _a=0 , _a=2 , _a="absolute" , _a=True , _a=None , **_a , ): """simple docstring""" super().__init__(pad_token_id=_a , bos_token_id=_a , eos_token_id=_a , **_a ) lowerCamelCase = vocab_size lowerCamelCase = hidden_size lowerCamelCase = num_hidden_layers lowerCamelCase = num_attention_heads lowerCamelCase = hidden_act lowerCamelCase = intermediate_size lowerCamelCase = hidden_dropout_prob lowerCamelCase = attention_probs_dropout_prob lowerCamelCase = max_position_embeddings lowerCamelCase = type_vocab_size lowerCamelCase = initializer_range lowerCamelCase = layer_norm_eps lowerCamelCase = position_embedding_type lowerCamelCase = use_cache lowerCamelCase = classifier_dropout class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' @property def _lowerCAmelCase ( self ): """simple docstring""" if self.task == "multiple-choice": lowerCamelCase = {0: """batch""", 1: """choice""", 2: """sequence"""} else: lowerCamelCase = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ] )
291
"""simple docstring""" import unittest from transformers import ( MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING, TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING, TextaTextGenerationPipeline, pipeline, ) from transformers.testing_utils import is_pipeline_test, require_tf, require_torch from transformers.utils import is_torch_available from .test_pipelines_common import ANY if is_torch_available(): import torch @is_pipeline_test class __magic_name__ ( unittest.TestCase ): '''simple docstring''' __UpperCamelCase = MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING __UpperCamelCase = TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING def _lowerCAmelCase ( self , _a , _a , _a ): """simple docstring""" lowerCamelCase = TextaTextGenerationPipeline(model=_a , tokenizer=_a ) return generator, ["Something to write", "Something else"] def _lowerCAmelCase ( self , _a , _a ): """simple docstring""" lowerCamelCase = generator("""Something there""" ) self.assertEqual(_a , [{"""generated_text""": ANY(_a )}] ) # These are encoder decoder, they don't just append to incoming string self.assertFalse(outputs[0]["""generated_text"""].startswith("""Something there""" ) ) lowerCamelCase = generator(["""This is great !""", """Something else"""] , num_return_sequences=2 , do_sample=_a ) self.assertEqual( _a , [ [{"""generated_text""": ANY(_a )}, {"""generated_text""": ANY(_a )}], [{"""generated_text""": ANY(_a )}, {"""generated_text""": ANY(_a )}], ] , ) lowerCamelCase = generator( ["""This is great !""", """Something else"""] , num_return_sequences=2 , batch_size=2 , do_sample=_a ) self.assertEqual( _a , [ [{"""generated_text""": ANY(_a )}, {"""generated_text""": ANY(_a )}], [{"""generated_text""": ANY(_a )}, {"""generated_text""": ANY(_a )}], ] , ) with self.assertRaises(_a ): generator(4 ) @require_torch def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = pipeline("""text2text-generation""" , model="""patrickvonplaten/t5-tiny-random""" , framework="""pt""" ) # do_sample=False necessary for reproducibility lowerCamelCase = generator("""Something there""" , do_sample=_a ) self.assertEqual(_a , [{"""generated_text""": """"""}] ) lowerCamelCase = 3 lowerCamelCase = generator( """Something there""" , num_return_sequences=_a , num_beams=_a , ) lowerCamelCase = [ {"""generated_text""": """Beide Beide Beide Beide Beide Beide Beide Beide Beide"""}, {"""generated_text""": """Beide Beide Beide Beide Beide Beide Beide Beide"""}, {"""generated_text""": """"""}, ] self.assertEqual(_a , _a ) lowerCamelCase = generator("""This is a test""" , do_sample=_a , num_return_sequences=2 , return_tensors=_a ) self.assertEqual( _a , [ {"""generated_token_ids""": ANY(torch.Tensor )}, {"""generated_token_ids""": ANY(torch.Tensor )}, ] , ) lowerCamelCase = generator.model.config.eos_token_id lowerCamelCase = """<pad>""" lowerCamelCase = generator( ["""This is a test""", """This is a second test"""] , do_sample=_a , num_return_sequences=2 , batch_size=2 , return_tensors=_a , ) self.assertEqual( _a , [ [ {"""generated_token_ids""": ANY(torch.Tensor )}, {"""generated_token_ids""": ANY(torch.Tensor )}, ], [ {"""generated_token_ids""": ANY(torch.Tensor )}, {"""generated_token_ids""": ANY(torch.Tensor )}, ], ] , ) @require_tf def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = pipeline("""text2text-generation""" , model="""patrickvonplaten/t5-tiny-random""" , framework="""tf""" ) # do_sample=False necessary for reproducibility lowerCamelCase = generator("""Something there""" , do_sample=_a ) self.assertEqual(_a , [{"""generated_text""": """"""}] )
291
1
"""simple docstring""" from typing import List, Union from ..utils import ( add_end_docstrings, is_tf_available, is_torch_available, is_vision_available, logging, requires_backends, ) from .base import PIPELINE_INIT_ARGS, Pipeline if is_vision_available(): from PIL import Image from ..image_utils import load_image if is_tf_available(): import tensorflow as tf from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING from ..tf_utils import stable_softmax if is_torch_available(): from ..models.auto.modeling_auto import MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING lowerCAmelCase : Dict = logging.get_logger(__name__) @add_end_docstrings(UpperCAmelCase__ ) class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' def __init__( self , *_a , **_a ): """simple docstring""" super().__init__(*_a , **_a ) requires_backends(self , """vision""" ) self.check_model_type( TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING if self.framework == """tf""" else MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING ) def _lowerCAmelCase ( self , _a=None ): """simple docstring""" lowerCamelCase = {} if top_k is not None: lowerCamelCase = top_k return {}, {}, postprocess_params def __call__( self , _a , **_a ): """simple docstring""" return super().__call__(_a , **_a ) def _lowerCAmelCase ( self , _a ): """simple docstring""" lowerCamelCase = load_image(_a ) lowerCamelCase = self.image_processor(images=_a , return_tensors=self.framework ) return model_inputs def _lowerCAmelCase ( self , _a ): """simple docstring""" lowerCamelCase = self.model(**_a ) return model_outputs def _lowerCAmelCase ( self , _a , _a=5 ): """simple docstring""" if top_k > self.model.config.num_labels: lowerCamelCase = self.model.config.num_labels if self.framework == "pt": lowerCamelCase = model_outputs.logits.softmax(-1 )[0] lowerCamelCase , lowerCamelCase = probs.topk(_a ) elif self.framework == "tf": lowerCamelCase = stable_softmax(model_outputs.logits , axis=-1 )[0] lowerCamelCase = tf.math.top_k(_a , k=_a ) lowerCamelCase , lowerCamelCase = topk.values.numpy(), topk.indices.numpy() else: raise ValueError(f'Unsupported framework: {self.framework}' ) lowerCamelCase = scores.tolist() lowerCamelCase = ids.tolist() return [{"score": score, "label": self.model.config.idalabel[_id]} for score, _id in zip(_a , _a )]
291
"""simple docstring""" def a__ ( snake_case__ , snake_case__ = False ) -> str: if not isinstance(snake_case__ , snake_case__ ): lowerCamelCase = F'Expected string as input, found {type(snake_case__ )}' raise ValueError(snake_case__ ) if not isinstance(snake_case__ , snake_case__ ): lowerCamelCase = F'Expected boolean as use_pascal parameter, found {type(snake_case__ )}' raise ValueError(snake_case__ ) lowerCamelCase = input_str.split("""_""" ) lowerCamelCase = 0 if use_pascal else 1 lowerCamelCase = words[start_index:] lowerCamelCase = [word[0].upper() + word[1:] for word in words_to_capitalize] lowerCamelCase = """""" if use_pascal else words[0] return "".join([initial_word, *capitalized_words] ) if __name__ == "__main__": from doctest import testmod testmod()
291
1
"""simple docstring""" def a__ ( snake_case__ , snake_case__ = False ) -> str: if not isinstance(snake_case__ , snake_case__ ): lowerCamelCase = F'Expected string as input, found {type(snake_case__ )}' raise ValueError(snake_case__ ) if not isinstance(snake_case__ , snake_case__ ): lowerCamelCase = F'Expected boolean as use_pascal parameter, found {type(snake_case__ )}' raise ValueError(snake_case__ ) lowerCamelCase = input_str.split("""_""" ) lowerCamelCase = 0 if use_pascal else 1 lowerCamelCase = words[start_index:] lowerCamelCase = [word[0].upper() + word[1:] for word in words_to_capitalize] lowerCamelCase = """""" if use_pascal else words[0] return "".join([initial_word, *capitalized_words] ) if __name__ == "__main__": from doctest import testmod testmod()
291
"""simple docstring""" from typing import Dict, List, Optional, Tuple, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_torch_available, is_torch_tensor, logging if is_torch_available(): import torch lowerCAmelCase : int = logging.get_logger(__name__) class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' __UpperCamelCase = ["pixel_values"] def __init__( self , _a = True , _a = None , _a = PILImageResampling.BILINEAR , _a = True , _a = None , _a = True , _a = 1 / 255 , _a = True , _a = None , _a = None , **_a , ): """simple docstring""" super().__init__(**_a ) lowerCamelCase = size if size is not None else {"""shortest_edge""": 256} lowerCamelCase = get_size_dict(_a , default_to_square=_a ) lowerCamelCase = crop_size if crop_size is not None else {"""height""": 224, """width""": 224} lowerCamelCase = get_size_dict(_a , param_name="""crop_size""" ) lowerCamelCase = do_resize lowerCamelCase = size lowerCamelCase = resample lowerCamelCase = do_center_crop lowerCamelCase = crop_size lowerCamelCase = do_rescale lowerCamelCase = rescale_factor lowerCamelCase = do_normalize lowerCamelCase = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN lowerCamelCase = image_std if image_std is not None else IMAGENET_STANDARD_STD def _lowerCAmelCase ( self , _a , _a , _a = PILImageResampling.BICUBIC , _a = None , **_a , ): """simple docstring""" lowerCamelCase = get_size_dict(_a , default_to_square=_a ) if "shortest_edge" not in size: raise ValueError(f'The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}' ) lowerCamelCase = get_resize_output_image_size(_a , size=size["""shortest_edge"""] , default_to_square=_a ) return resize(_a , size=_a , resample=_a , data_format=_a , **_a ) def _lowerCAmelCase ( self , _a , _a , _a = None , **_a , ): """simple docstring""" lowerCamelCase = get_size_dict(_a ) if "height" not in size or "width" not in size: raise ValueError(f'The `size` parameter must contain the keys `height` and `width`. Got {size.keys()}' ) return center_crop(_a , size=(size["""height"""], size["""width"""]) , data_format=_a , **_a ) def _lowerCAmelCase ( self , _a , _a , _a = None , **_a ): """simple docstring""" return rescale(_a , scale=_a , data_format=_a , **_a ) def _lowerCAmelCase ( self , _a , _a , _a , _a = None , **_a , ): """simple docstring""" return normalize(_a , mean=_a , std=_a , data_format=_a , **_a ) def _lowerCAmelCase ( self , _a , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = ChannelDimension.FIRST , **_a , ): """simple docstring""" lowerCamelCase = do_resize if do_resize is not None else self.do_resize lowerCamelCase = size if size is not None else self.size lowerCamelCase = get_size_dict(_a , default_to_square=_a ) lowerCamelCase = resample if resample is not None else self.resample lowerCamelCase = do_center_crop if do_center_crop is not None else self.do_center_crop lowerCamelCase = crop_size if crop_size is not None else self.crop_size lowerCamelCase = get_size_dict(_a , param_name="""crop_size""" ) lowerCamelCase = do_rescale if do_rescale is not None else self.do_rescale lowerCamelCase = rescale_factor if rescale_factor is not None else self.rescale_factor lowerCamelCase = do_normalize if do_normalize is not None else self.do_normalize lowerCamelCase = image_mean if image_mean is not None else self.image_mean lowerCamelCase = image_std if image_std is not None else self.image_std lowerCamelCase = make_list_of_images(_a ) if not valid_images(_a ): raise ValueError( """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """ """torch.Tensor, tf.Tensor or jax.ndarray.""" ) if do_resize and size is None: raise ValueError("""Size must be specified if do_resize is True.""" ) if do_center_crop and crop_size is None: raise ValueError("""Crop size must be specified if do_center_crop is True.""" ) if do_rescale and rescale_factor is None: raise ValueError("""Rescale factor must be specified if do_rescale is True.""" ) if do_normalize and (image_mean is None or image_std is None): raise ValueError("""Image mean and std must be specified if do_normalize is True.""" ) # All transformations expect numpy arrays. lowerCamelCase = [to_numpy_array(_a ) for image in images] if do_resize: lowerCamelCase = [self.resize(image=_a , size=_a , resample=_a ) for image in images] if do_center_crop: lowerCamelCase = [self.center_crop(image=_a , size=_a ) for image in images] if do_rescale: lowerCamelCase = [self.rescale(image=_a , scale=_a ) for image in images] if do_normalize: lowerCamelCase = [self.normalize(image=_a , mean=_a , std=_a ) for image in images] lowerCamelCase = [to_channel_dimension_format(_a , _a ) for image in images] lowerCamelCase = {"""pixel_values""": images} return BatchFeature(data=_a , tensor_type=_a ) def _lowerCAmelCase ( self , _a , _a = None ): """simple docstring""" lowerCamelCase = outputs.logits # Resize logits and compute semantic segmentation maps if target_sizes is not None: if len(_a ) != len(_a ): raise ValueError( """Make sure that you pass in as many target sizes as the batch dimension of the logits""" ) if is_torch_tensor(_a ): lowerCamelCase = target_sizes.numpy() lowerCamelCase = [] for idx in range(len(_a ) ): lowerCamelCase = torch.nn.functional.interpolate( logits[idx].unsqueeze(dim=0 ) , size=target_sizes[idx] , mode="""bilinear""" , align_corners=_a ) lowerCamelCase = resized_logits[0].argmax(dim=0 ) semantic_segmentation.append(_a ) else: lowerCamelCase = logits.argmax(dim=1 ) lowerCamelCase = [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0] )] return semantic_segmentation
291
1
"""simple docstring""" import random import unittest import torch from diffusers import IFInpaintingPipeline from diffusers.utils import floats_tensor from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import skip_mps, torch_device from ..pipeline_params import ( TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS, ) from ..test_pipelines_common import PipelineTesterMixin from . import IFPipelineTesterMixin @skip_mps class __magic_name__ ( UpperCAmelCase__ , UpperCAmelCase__ , unittest.TestCase ): '''simple docstring''' __UpperCamelCase = IFInpaintingPipeline __UpperCamelCase = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {"width", "height"} __UpperCamelCase = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS __UpperCamelCase = PipelineTesterMixin.required_optional_params - {"latents"} def _lowerCAmelCase ( self ): """simple docstring""" return self._get_dummy_components() def _lowerCAmelCase ( self , _a , _a=0 ): """simple docstring""" if str(_a ).startswith("""mps""" ): lowerCamelCase = torch.manual_seed(_a ) else: lowerCamelCase = torch.Generator(device=_a ).manual_seed(_a ) lowerCamelCase = floats_tensor((1, 3, 32, 32) , rng=random.Random(_a ) ).to(_a ) lowerCamelCase = floats_tensor((1, 3, 32, 32) , rng=random.Random(_a ) ).to(_a ) lowerCamelCase = { """prompt""": """A painting of a squirrel eating a burger""", """image""": image, """mask_image""": mask_image, """generator""": generator, """num_inference_steps""": 2, """output_type""": """numpy""", } return inputs @unittest.skipIf( torch_device != """cuda""" or not is_xformers_available() , reason="""XFormers attention is only available with CUDA and `xformers` installed""" , ) def _lowerCAmelCase ( self ): """simple docstring""" self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3 ) def _lowerCAmelCase ( self ): """simple docstring""" self._test_save_load_optional_components() @unittest.skipIf(torch_device != """cuda""" , reason="""float16 requires CUDA""" ) def _lowerCAmelCase ( self ): """simple docstring""" # Due to non-determinism in save load of the hf-internal-testing/tiny-random-t5 text encoder super().test_save_load_floataa(expected_max_diff=1e-1 ) def _lowerCAmelCase ( self ): """simple docstring""" self._test_attention_slicing_forward_pass(expected_max_diff=1e-2 ) def _lowerCAmelCase ( self ): """simple docstring""" self._test_save_load_local() def _lowerCAmelCase ( self ): """simple docstring""" self._test_inference_batch_single_identical( expected_max_diff=1e-2 , )
291
"""simple docstring""" import operator as op lowerCAmelCase : Dict = """scaler.pt""" lowerCAmelCase : Tuple = """pytorch_model""" lowerCAmelCase : Union[str, Any] = """random_states""" lowerCAmelCase : Union[str, Any] = """optimizer""" lowerCAmelCase : Dict = """scheduler""" lowerCAmelCase : int = """pytorch_model.bin""" lowerCAmelCase : str = """pytorch_model.bin.index.json""" lowerCAmelCase : Union[str, Any] = """model.safetensors""" lowerCAmelCase : List[Any] = """model.safetensors.index.json""" lowerCAmelCase : List[Any] = """1.10.2""" lowerCAmelCase : Any = """py38""" lowerCAmelCase : Optional[int] = """4.17.0""" lowerCAmelCase : str = ["""ml.p3.16xlarge""", """ml.p3dn.24xlarge""", """ml.p4dn.24xlarge"""] lowerCAmelCase : Tuple = ["""FULL_SHARD""", """SHARD_GRAD_OP""", """NO_SHARD""", """HYBRID_SHARD""", """HYBRID_SHARD_ZERO2"""] lowerCAmelCase : List[Any] = ["""TRANSFORMER_BASED_WRAP""", """SIZE_BASED_WRAP""", """NO_WRAP"""] lowerCAmelCase : List[str] = ["""BACKWARD_PRE""", """BACKWARD_POST""", """NO_PREFETCH"""] lowerCAmelCase : List[str] = ["""FULL_STATE_DICT""", """LOCAL_STATE_DICT""", """SHARDED_STATE_DICT"""] lowerCAmelCase : Any = """2.0.1""" lowerCAmelCase : List[Any] = ["""pdsh""", """standard""", """openmpi""", """mvapich"""] lowerCAmelCase : Union[str, Any] = ["""default""", """reduce-overhead""", """max-autotune"""] lowerCAmelCase : Optional[int] = {""">""": op.gt, """>=""": op.ge, """==""": op.eq, """!=""": op.ne, """<=""": op.le, """<""": op.lt} # These are the args for `torch.distributed.launch` for pytorch < 1.9 lowerCAmelCase : Union[str, Any] = [ """nnodes""", """nproc_per_node""", """rdzv_backend""", """rdzv_endpoint""", """rdzv_id""", """rdzv_conf""", """standalone""", """max_restarts""", """monitor_interval""", """start_method""", """role""", """module""", """m""", """no_python""", """run_path""", """log_dir""", """r""", """redirects""", """t""", """tee""", """node_rank""", """master_addr""", """master_port""", ] lowerCAmelCase : List[str] = ["""DEEPSPEED""", """MULTI_GPU""", """FSDP""", """MEGATRON_LM"""] lowerCAmelCase : Optional[Any] = ["""DEEPSPEED""", """MULTI_XPU""", """FSDP"""]
291
1
"""simple docstring""" from math import ceil from typing import List, Optional, Union import numpy as np from ...audio_utils import mel_filter_bank, spectrogram, window_function from ...feature_extraction_sequence_utils import BatchFeature, SequenceFeatureExtractor from ...utils import TensorType, logging lowerCAmelCase : List[str] = logging.get_logger(__name__) class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' __UpperCamelCase = ["audio_values", "audio_mask"] def __init__( self , _a=2_048 , _a=1 , _a=[16, 16] , _a=128 , _a=44_100 , _a=86 , _a=2_048 , _a=0.0 , **_a , ): """simple docstring""" super().__init__( feature_size=_a , sampling_rate=_a , padding_value=_a , **_a , ) lowerCamelCase = spectrogram_length lowerCamelCase = num_channels lowerCamelCase = patch_size lowerCamelCase = feature_size // self.patch_size[1] lowerCamelCase = n_fft lowerCamelCase = sampling_rate // hop_length_to_sampling_rate lowerCamelCase = sampling_rate lowerCamelCase = padding_value lowerCamelCase = mel_filter_bank( num_frequency_bins=1 + n_fft // 2 , num_mel_filters=_a , min_frequency=0.0 , max_frequency=22_050.0 , sampling_rate=_a , norm="""slaney""" , mel_scale="""slaney""" , ).T def _lowerCAmelCase ( self , _a ): """simple docstring""" lowerCamelCase = spectrogram( _a , window_function(self.n_fft , """hann""" ) , frame_length=self.n_fft , hop_length=self.hop_length , power=2.0 , mel_filters=self.mel_filters.T , log_mel="""dB""" , db_range=80.0 , ) lowerCamelCase = log_spec[:, :-1] lowerCamelCase = log_spec - 20.0 lowerCamelCase = np.clip(log_spec / 40.0 , -2.0 , 0.0 ) + 1.0 return log_spec def __call__( self , _a , _a = None , _a = True , _a = None , _a = False , _a = False , **_a , ): """simple docstring""" if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( """This feature extractor is set to support sampling rate""" f' of {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled' f' 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.""" ) lowerCamelCase = isinstance(_a , np.ndarray ) and len(raw_speech.shape ) > 1 if is_batched_numpy and len(raw_speech.shape ) > 2: raise ValueError(f'Only mono-channel audio is supported for input to {self}' ) lowerCamelCase = is_batched_numpy or ( isinstance(_a , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: lowerCamelCase = [np.asarray([speech] , dtype=np.floataa ).T for speech in raw_speech] elif not is_batched and not isinstance(_a , np.ndarray ): lowerCamelCase = np.asarray(_a , dtype=np.floataa ) elif isinstance(_a , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): lowerCamelCase = raw_speech.astype(np.floataa ) # always return batch if not is_batched: lowerCamelCase = [np.asarray([raw_speech] ).T] # Convert audio signals to log mel spectrograms, truncate by time axis lowerCamelCase = [ self._np_extract_fbank_features(waveform.squeeze() ).T[: self.spectrogram_length] for waveform in raw_speech ] if isinstance(audio_features[0] , _a ): lowerCamelCase = [np.asarray(_a , dtype=np.floataa ) for feature in audio_features] # Create audio attention mask lowerCamelCase = max( [ceil(feature.shape[0] / self.patch_size[0] ) * self.freq_len for feature in audio_features] ) # The maximum number of audio patches in a batch if return_attention_mask: lowerCamelCase = [ (ceil(feature.shape[0] / self.patch_size[0] ) * self.freq_len) * [1] + (max_patch_len - ceil(feature.shape[0] / self.patch_size[0] ) * self.freq_len) * [0] for feature in audio_features ] lowerCamelCase = np.array(_a ).astype(np.floataa ) # convert into correct format for padding lowerCamelCase = max_patch_len // self.freq_len * self.patch_size[0] # The maximum audio size in a batch lowerCamelCase = np.ones([len(_a ), 1, max_time_len, self.feature_size] ).astype(np.floataa ) lowerCamelCase = padded_audio_features * self.padding_value for i in range(len(_a ) ): lowerCamelCase = audio_features[i] lowerCamelCase = feature # return as BatchFeature if return_attention_mask: lowerCamelCase = {"""audio_values""": padded_audio_features, """audio_mask""": audio_mask} else: lowerCamelCase = {"""audio_values""": padded_audio_features} lowerCamelCase = BatchFeature(data=_a , tensor_type=_a ) return encoded_inputs
291
"""simple docstring""" import inspect import unittest from transformers import ViTMSNConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTMSNForImageClassification, ViTMSNModel from transformers.models.vit_msn.modeling_vit_msn import VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class __magic_name__ : '''simple docstring''' def __init__( self , _a , _a=13 , _a=30 , _a=2 , _a=3 , _a=True , _a=True , _a=32 , _a=5 , _a=4 , _a=37 , _a="gelu" , _a=0.1 , _a=0.1 , _a=10 , _a=0.02 , _a=None , ): """simple docstring""" lowerCamelCase = parent lowerCamelCase = batch_size lowerCamelCase = image_size lowerCamelCase = patch_size lowerCamelCase = num_channels lowerCamelCase = is_training lowerCamelCase = use_labels lowerCamelCase = hidden_size lowerCamelCase = num_hidden_layers lowerCamelCase = num_attention_heads lowerCamelCase = intermediate_size lowerCamelCase = hidden_act lowerCamelCase = hidden_dropout_prob lowerCamelCase = attention_probs_dropout_prob lowerCamelCase = type_sequence_label_size lowerCamelCase = initializer_range lowerCamelCase = scope # in ViT MSN, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) lowerCamelCase = (image_size // patch_size) ** 2 lowerCamelCase = num_patches + 1 def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) lowerCamelCase = None if self.use_labels: lowerCamelCase = ids_tensor([self.batch_size] , self.type_sequence_label_size ) lowerCamelCase = self.get_config() return config, pixel_values, labels def _lowerCAmelCase ( self ): """simple docstring""" return ViTMSNConfig( 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 , initializer_range=self.initializer_range , ) def _lowerCAmelCase ( self , _a , _a , _a ): """simple docstring""" lowerCamelCase = ViTMSNModel(config=_a ) model.to(_a ) model.eval() lowerCamelCase = model(_a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _lowerCAmelCase ( self , _a , _a , _a ): """simple docstring""" lowerCamelCase = self.type_sequence_label_size lowerCamelCase = ViTMSNForImageClassification(_a ) model.to(_a ) model.eval() lowerCamelCase = model(_a , labels=_a ) print("""Pixel and labels shape: {pixel_values.shape}, {labels.shape}""" ) print("""Labels: {labels}""" ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images lowerCamelCase = 1 lowerCamelCase = ViTMSNForImageClassification(_a ) model.to(_a ) model.eval() lowerCamelCase = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) lowerCamelCase = model(_a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.prepare_config_and_inputs() lowerCamelCase , lowerCamelCase , lowerCamelCase = config_and_inputs lowerCamelCase = {"""pixel_values""": pixel_values} return config, inputs_dict @require_torch class __magic_name__ ( UpperCAmelCase__ , UpperCAmelCase__ , unittest.TestCase ): '''simple docstring''' __UpperCamelCase = (ViTMSNModel, ViTMSNForImageClassification) if is_torch_available() else () __UpperCamelCase = ( {"feature-extraction": ViTMSNModel, "image-classification": ViTMSNForImageClassification} if is_torch_available() else {} ) __UpperCamelCase = False __UpperCamelCase = False __UpperCamelCase = False __UpperCamelCase = False def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = ViTMSNModelTester(self ) lowerCamelCase = ConfigTester(self , config_class=_a , has_text_modality=_a , hidden_size=37 ) def _lowerCAmelCase ( self ): """simple docstring""" self.config_tester.run_common_tests() @unittest.skip(reason="""ViTMSN does not use inputs_embeds""" ) def _lowerCAmelCase ( self ): """simple docstring""" pass def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase , lowerCamelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCamelCase = model_class(_a ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) lowerCamelCase = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_a , nn.Linear ) ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase , lowerCamelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCamelCase = model_class(_a ) lowerCamelCase = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic lowerCamelCase = [*signature.parameters.keys()] lowerCamelCase = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , _a ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_a ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_a ) @slow def _lowerCAmelCase ( self ): """simple docstring""" for model_name in VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowerCamelCase = ViTMSNModel.from_pretrained(_a ) self.assertIsNotNone(_a ) def a__ ( ) -> Any: lowerCamelCase = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) return image @require_torch @require_vision class __magic_name__ ( unittest.TestCase ): '''simple docstring''' @cached_property def _lowerCAmelCase ( self ): """simple docstring""" return ViTImageProcessor.from_pretrained("""facebook/vit-msn-small""" ) if is_vision_available() else None @slow def _lowerCAmelCase ( self ): """simple docstring""" torch.manual_seed(2 ) lowerCamelCase = ViTMSNForImageClassification.from_pretrained("""facebook/vit-msn-small""" ).to(_a ) lowerCamelCase = self.default_image_processor lowerCamelCase = prepare_img() lowerCamelCase = image_processor(images=_a , return_tensors="""pt""" ).to(_a ) # forward pass with torch.no_grad(): lowerCamelCase = model(**_a ) # verify the logits lowerCamelCase = torch.Size((1, 1_000) ) self.assertEqual(outputs.logits.shape , _a ) lowerCamelCase = torch.tensor([-0.0_803, -0.4_454, -0.2_375] ).to(_a ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , _a , atol=1e-4 ) )
291
1
"""simple docstring""" from __future__ import annotations from decimal import Decimal from numpy import array def a__ ( snake_case__ ) -> list[list[float]]: lowerCamelCase = Decimal # Check if the provided matrix has 2 rows and 2 columns # since this implementation only works for 2x2 matrices if len(snake_case__ ) == 2 and len(matrix[0] ) == 2 and len(matrix[1] ) == 2: # Calculate the determinant of the matrix lowerCamelCase = float( d(matrix[0][0] ) * d(matrix[1][1] ) - d(matrix[1][0] ) * d(matrix[0][1] ) ) if determinant == 0: raise ValueError("""This matrix has no inverse.""" ) # Creates a copy of the matrix with swapped positions of the elements lowerCamelCase = [[0.0, 0.0], [0.0, 0.0]] lowerCamelCase , lowerCamelCase = matrix[1][1], matrix[0][0] lowerCamelCase , lowerCamelCase = -matrix[1][0], -matrix[0][1] # Calculate the inverse of the matrix return [ [(float(d(snake_case__ ) ) / determinant) or 0.0 for n in row] for row in swapped_matrix ] elif ( len(snake_case__ ) == 3 and len(matrix[0] ) == 3 and len(matrix[1] ) == 3 and len(matrix[2] ) == 3 ): # Calculate the determinant of the matrix using Sarrus rule lowerCamelCase = float( ( (d(matrix[0][0] ) * d(matrix[1][1] ) * d(matrix[2][2] )) + (d(matrix[0][1] ) * d(matrix[1][2] ) * d(matrix[2][0] )) + (d(matrix[0][2] ) * d(matrix[1][0] ) * d(matrix[2][1] )) ) - ( (d(matrix[0][2] ) * d(matrix[1][1] ) * d(matrix[2][0] )) + (d(matrix[0][1] ) * d(matrix[1][0] ) * d(matrix[2][2] )) + (d(matrix[0][0] ) * d(matrix[1][2] ) * d(matrix[2][1] )) ) ) if determinant == 0: raise ValueError("""This matrix has no inverse.""" ) # Creating cofactor matrix lowerCamelCase = [ [d(0.0 ), d(0.0 ), d(0.0 )], [d(0.0 ), d(0.0 ), d(0.0 )], [d(0.0 ), d(0.0 ), d(0.0 )], ] lowerCamelCase = (d(matrix[1][1] ) * d(matrix[2][2] )) - ( d(matrix[1][2] ) * d(matrix[2][1] ) ) lowerCamelCase = -( (d(matrix[1][0] ) * d(matrix[2][2] )) - (d(matrix[1][2] ) * d(matrix[2][0] )) ) lowerCamelCase = (d(matrix[1][0] ) * d(matrix[2][1] )) - ( d(matrix[1][1] ) * d(matrix[2][0] ) ) lowerCamelCase = -( (d(matrix[0][1] ) * d(matrix[2][2] )) - (d(matrix[0][2] ) * d(matrix[2][1] )) ) lowerCamelCase = (d(matrix[0][0] ) * d(matrix[2][2] )) - ( d(matrix[0][2] ) * d(matrix[2][0] ) ) lowerCamelCase = -( (d(matrix[0][0] ) * d(matrix[2][1] )) - (d(matrix[0][1] ) * d(matrix[2][0] )) ) lowerCamelCase = (d(matrix[0][1] ) * d(matrix[1][2] )) - ( d(matrix[0][2] ) * d(matrix[1][1] ) ) lowerCamelCase = -( (d(matrix[0][0] ) * d(matrix[1][2] )) - (d(matrix[0][2] ) * d(matrix[1][0] )) ) lowerCamelCase = (d(matrix[0][0] ) * d(matrix[1][1] )) - ( d(matrix[0][1] ) * d(matrix[1][0] ) ) # Transpose the cofactor matrix (Adjoint matrix) lowerCamelCase = array(snake_case__ ) for i in range(3 ): for j in range(3 ): lowerCamelCase = cofactor_matrix[j][i] # Inverse of the matrix using the formula (1/determinant) * adjoint matrix lowerCamelCase = array(snake_case__ ) for i in range(3 ): for j in range(3 ): inverse_matrix[i][j] /= d(snake_case__ ) # Calculate the inverse of the matrix return [[float(d(snake_case__ ) ) or 0.0 for n in row] for row in inverse_matrix] raise ValueError("""Please provide a matrix of size 2x2 or 3x3.""" )
291
"""simple docstring""" 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__ ( snake_case__ , snake_case__ , snake_case__ ) -> List[Any]: return params[F'{prefix}/{prefix}/relpos_bias/rel_embedding'][:, i, :] def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__="attention" ) -> List[Any]: lowerCamelCase = lowerCamelCase = np.ascontiguousarray(params[F'{prefix}/{prefix}/{layer_name}/key/kernel'][:, i, :, :] ) lowerCamelCase = k_tmp.reshape(k_tmp.shape[0] , k_tmp.shape[1] * k_tmp.shape[2] ) lowerCamelCase = np.ascontiguousarray(params[F'{prefix}/{prefix}/{layer_name}/out/kernel'][:, i, :, :] ) lowerCamelCase = o_tmp.reshape(o_tmp.shape[0] * o_tmp.shape[1] , o_tmp.shape[2] ) lowerCamelCase = np.ascontiguousarray(params[F'{prefix}/{prefix}/{layer_name}/query/kernel'][:, i, :, :] ) lowerCamelCase = q_tmp.reshape(q_tmp.shape[0] , q_tmp.shape[1] * q_tmp.shape[2] ) lowerCamelCase = np.ascontiguousarray(params[F'{prefix}/{prefix}/{layer_name}/value/kernel'][:, i, :, :] ) lowerCamelCase = v_tmp.reshape(v_tmp.shape[0] , v_tmp.shape[1] * v_tmp.shape[2] ) return k, o, q, v def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__=False ) -> List[str]: if split_mlp_wi: lowerCamelCase = params[F'{prefix}/{prefix}/mlp/wi_0/kernel'][:, i, :] lowerCamelCase = params[F'{prefix}/{prefix}/mlp/wi_1/kernel'][:, i, :] lowerCamelCase = (wi_a, wi_a) else: lowerCamelCase = params[F'{prefix}/{prefix}/mlp/wi/kernel'][:, i, :] lowerCamelCase = params[F'{prefix}/{prefix}/mlp/wo/kernel'][:, i, :] return wi, wo def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ ) -> Tuple: return params[F'{prefix}/{prefix}/{layer_name}/scale'][:, i] def a__ ( snake_case__ , *, snake_case__ , snake_case__ , snake_case__ = False ) -> Dict: lowerCamelCase = traverse_util.flatten_dict(variables["""target"""] ) lowerCamelCase = {"""/""".join(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 lowerCamelCase = """encoder/encoder/mlp/wi_0/kernel""" in old print("""Split MLP:""" , snake_case__ ) lowerCamelCase = collections.OrderedDict() # Shared embeddings. lowerCamelCase = old["""token_embedder/embedding"""] # Encoder. for i in range(snake_case__ ): # Block i, layer 0 (Self Attention). lowerCamelCase = tax_layer_norm_lookup(snake_case__ , snake_case__ , """encoder""" , """pre_attention_layer_norm""" ) lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase = tax_attention_lookup(snake_case__ , snake_case__ , """encoder""" , """attention""" ) lowerCamelCase = layer_norm lowerCamelCase = k.T lowerCamelCase = o.T lowerCamelCase = q.T lowerCamelCase = v.T # Block i, layer 1 (MLP). lowerCamelCase = tax_layer_norm_lookup(snake_case__ , snake_case__ , """encoder""" , """pre_mlp_layer_norm""" ) lowerCamelCase , lowerCamelCase = tax_mlp_lookup(snake_case__ , snake_case__ , """encoder""" , snake_case__ ) lowerCamelCase = layer_norm if split_mlp_wi: lowerCamelCase = wi[0].T lowerCamelCase = wi[1].T else: lowerCamelCase = wi.T lowerCamelCase = wo.T if scalable_attention: # convert the rel_embedding of each layer lowerCamelCase = tax_relpos_bias_lookup( snake_case__ , snake_case__ , """encoder""" ).T lowerCamelCase = old["""encoder/encoder_norm/scale"""] if not scalable_attention: lowerCamelCase = tax_relpos_bias_lookup( snake_case__ , 0 , """encoder""" ).T lowerCamelCase = tax_relpos_bias_lookup( snake_case__ , 0 , """decoder""" ).T if not is_encoder_only: # Decoder. for i in range(snake_case__ ): # Block i, layer 0 (Self Attention). lowerCamelCase = tax_layer_norm_lookup(snake_case__ , snake_case__ , """decoder""" , """pre_self_attention_layer_norm""" ) lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase = tax_attention_lookup(snake_case__ , snake_case__ , """decoder""" , """self_attention""" ) lowerCamelCase = layer_norm lowerCamelCase = k.T lowerCamelCase = o.T lowerCamelCase = q.T lowerCamelCase = v.T # Block i, layer 1 (Cross Attention). lowerCamelCase = tax_layer_norm_lookup(snake_case__ , snake_case__ , """decoder""" , """pre_cross_attention_layer_norm""" ) lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase = tax_attention_lookup(snake_case__ , snake_case__ , """decoder""" , """encoder_decoder_attention""" ) lowerCamelCase = layer_norm lowerCamelCase = k.T lowerCamelCase = o.T lowerCamelCase = q.T lowerCamelCase = v.T # Block i, layer 2 (MLP). lowerCamelCase = tax_layer_norm_lookup(snake_case__ , snake_case__ , """decoder""" , """pre_mlp_layer_norm""" ) lowerCamelCase , lowerCamelCase = tax_mlp_lookup(snake_case__ , snake_case__ , """decoder""" , snake_case__ ) lowerCamelCase = layer_norm if split_mlp_wi: lowerCamelCase = wi[0].T lowerCamelCase = wi[1].T else: lowerCamelCase = wi.T lowerCamelCase = wo.T if scalable_attention: # convert the rel_embedding of each layer lowerCamelCase = tax_relpos_bias_lookup(snake_case__ , snake_case__ , """decoder""" ).T lowerCamelCase = 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: lowerCamelCase = old["""decoder/logits_dense/kernel"""].T return new def a__ ( snake_case__ , snake_case__ ) -> Optional[int]: lowerCamelCase = 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: lowerCamelCase = state_dict["""shared.weight"""] if not is_encoder_only: if "decoder.embed_tokens.weight" not in state_dict: lowerCamelCase = 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.""" ) lowerCamelCase = state_dict["""shared.weight"""] return state_dict def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ ) -> List[Any]: lowerCamelCase = checkpoints.load_tax_checkpoint(snake_case__ ) lowerCamelCase = convert_tax_to_pytorch( snake_case__ , num_layers=config.num_layers , is_encoder_only=snake_case__ , scalable_attention=snake_case__ ) lowerCamelCase = make_state_dict(snake_case__ , snake_case__ ) model.load_state_dict(snake_case__ , strict=snake_case__ ) def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ = False , snake_case__ = False , ) -> str: lowerCamelCase = MTaConfig.from_json_file(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: lowerCamelCase = UMTaEncoderModel(snake_case__ ) else: lowerCamelCase = UMTaForConditionalGeneration(snake_case__ ) # Load weights from tf checkpoint load_tax_weights_in_ta(snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ ) # Save pytorch-model print(F'Save PyTorch model to {pytorch_dump_path}' ) model.save_pretrained(snake_case__ ) # Verify that we can load the checkpoint. model.from_pretrained(snake_case__ ) print("""Done""" ) if __name__ == "__main__": lowerCAmelCase : 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, ) lowerCAmelCase : 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, )
291
1
"""simple docstring""" import itertools import json import os import unittest from transformers import AddedToken, RobertaTokenizer, RobertaTokenizerFast from transformers.models.roberta.tokenization_roberta import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class __magic_name__ ( UpperCAmelCase__ , unittest.TestCase ): '''simple docstring''' __UpperCamelCase = RobertaTokenizer __UpperCamelCase = RobertaTokenizerFast __UpperCamelCase = True __UpperCamelCase = {"cls_token": "<s>"} def _lowerCAmelCase ( self ): """simple docstring""" super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt lowerCamelCase = [ """l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """\u0120""", """\u0120l""", """\u0120n""", """\u0120lo""", """\u0120low""", """er""", """\u0120lowest""", """\u0120newer""", """\u0120wider""", """<unk>""", ] lowerCamelCase = dict(zip(_a , range(len(_a ) ) ) ) lowerCamelCase = ["""#version: 0.2""", """\u0120 l""", """\u0120l o""", """\u0120lo w""", """e r""", """"""] lowerCamelCase = {"""unk_token""": """<unk>"""} lowerCamelCase = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""vocab_file"""] ) lowerCamelCase = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""merges_file"""] ) with open(self.vocab_file , """w""" , encoding="""utf-8""" ) as fp: fp.write(json.dumps(_a ) + """\n""" ) with open(self.merges_file , """w""" , encoding="""utf-8""" ) as fp: fp.write("""\n""".join(_a ) ) def _lowerCAmelCase ( self , **_a ): """simple docstring""" kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname , **_a ) def _lowerCAmelCase ( self , **_a ): """simple docstring""" kwargs.update(self.special_tokens_map ) return RobertaTokenizerFast.from_pretrained(self.tmpdirname , **_a ) def _lowerCAmelCase ( self , _a ): """simple docstring""" lowerCamelCase = """lower newer""" lowerCamelCase = """lower newer""" return input_text, output_text def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.tokenizer_class(self.vocab_file , self.merges_file , **self.special_tokens_map ) lowerCamelCase = """lower newer""" lowerCamelCase = ["""l""", """o""", """w""", """er""", """\u0120""", """n""", """e""", """w""", """er"""] lowerCamelCase = tokenizer.tokenize(_a ) # , add_prefix_space=True) self.assertListEqual(_a , _a ) lowerCamelCase = tokens + [tokenizer.unk_token] lowerCamelCase = [0, 1, 2, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(tokenizer.convert_tokens_to_ids(_a ) , _a ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.get_tokenizer() self.assertListEqual(tokenizer.encode("""Hello world!""" , add_special_tokens=_a ) , [0, 31_414, 232, 328, 2] ) self.assertListEqual( tokenizer.encode("""Hello world! cécé herlolip 418""" , add_special_tokens=_a ) , [0, 31_414, 232, 328, 740, 1_140, 12_695, 69, 46_078, 1_588, 2] , ) @slow def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.tokenizer_class.from_pretrained("""roberta-base""" ) lowerCamelCase = tokenizer.encode("""sequence builders""" , add_special_tokens=_a ) lowerCamelCase = tokenizer.encode("""multi-sequence build""" , add_special_tokens=_a ) lowerCamelCase = tokenizer.encode( """sequence builders""" , add_special_tokens=_a , add_prefix_space=_a ) lowerCamelCase = tokenizer.encode( """sequence builders""" , """multi-sequence build""" , add_special_tokens=_a , add_prefix_space=_a ) lowerCamelCase = tokenizer.build_inputs_with_special_tokens(_a ) lowerCamelCase = tokenizer.build_inputs_with_special_tokens(_a , _a ) assert encoded_sentence == encoded_text_from_decode assert encoded_pair == encoded_pair_from_decode def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.get_tokenizer() lowerCamelCase = """Encode this sequence.""" lowerCamelCase = tokenizer.byte_encoder[""" """.encode("""utf-8""" )[0]] # Testing encoder arguments lowerCamelCase = tokenizer.encode(_a , add_special_tokens=_a , add_prefix_space=_a ) lowerCamelCase = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertNotEqual(_a , _a ) lowerCamelCase = tokenizer.encode(_a , add_special_tokens=_a , add_prefix_space=_a ) lowerCamelCase = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertEqual(_a , _a ) tokenizer.add_special_tokens({"""bos_token""": """<s>"""} ) lowerCamelCase = tokenizer.encode(_a , add_special_tokens=_a ) lowerCamelCase = tokenizer.convert_ids_to_tokens(encoded[1] )[0] self.assertNotEqual(_a , _a ) # Testing spaces after special tokens lowerCamelCase = """<mask>""" tokenizer.add_special_tokens( {"""mask_token""": AddedToken(_a , lstrip=_a , rstrip=_a )} ) # mask token has a left space lowerCamelCase = tokenizer.convert_tokens_to_ids(_a ) lowerCamelCase = """Encode <mask> sequence""" lowerCamelCase = """Encode <mask>sequence""" lowerCamelCase = tokenizer.encode(_a ) lowerCamelCase = encoded.index(_a ) lowerCamelCase = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertEqual(_a , _a ) lowerCamelCase = tokenizer.encode(_a ) lowerCamelCase = encoded.index(_a ) lowerCamelCase = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertNotEqual(_a , _a ) def _lowerCAmelCase ( self ): """simple docstring""" pass def _lowerCAmelCase ( self ): """simple docstring""" for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'{tokenizer.__class__.__name__} ({pretrained_name})' ): lowerCamelCase = self.rust_tokenizer_class.from_pretrained(_a , **_a ) lowerCamelCase = self.tokenizer_class.from_pretrained(_a , **_a ) lowerCamelCase = """A, <mask> AllenNLP sentence.""" lowerCamelCase = tokenizer_r.encode_plus(_a , add_special_tokens=_a , return_token_type_ids=_a ) lowerCamelCase = tokenizer_p.encode_plus(_a , add_special_tokens=_a , return_token_type_ids=_a ) # token_type_ids should put 0 everywhere self.assertEqual(sum(tokens_r["""token_type_ids"""] ) , sum(tokens_p["""token_type_ids"""] ) ) # attention_mask should put 1 everywhere, so sum over length should be 1 self.assertEqual( sum(tokens_r["""attention_mask"""] ) / len(tokens_r["""attention_mask"""] ) , sum(tokens_p["""attention_mask"""] ) / len(tokens_p["""attention_mask"""] ) , ) lowerCamelCase = tokenizer_r.convert_ids_to_tokens(tokens_r["""input_ids"""] ) lowerCamelCase = tokenizer_p.convert_ids_to_tokens(tokens_p["""input_ids"""] ) # Rust correctly handles the space before the mask while python doesnt self.assertSequenceEqual(tokens_p["""input_ids"""] , [0, 250, 6, 50_264, 3_823, 487, 21_992, 3_645, 4, 2] ) self.assertSequenceEqual(tokens_r["""input_ids"""] , [0, 250, 6, 50_264, 3_823, 487, 21_992, 3_645, 4, 2] ) self.assertSequenceEqual( _a , ["""<s>""", """A""", """,""", """<mask>""", """ĠAllen""", """N""", """LP""", """Ġsentence""", """.""", """</s>"""] ) self.assertSequenceEqual( _a , ["""<s>""", """A""", """,""", """<mask>""", """ĠAllen""", """N""", """LP""", """Ġsentence""", """.""", """</s>"""] ) def _lowerCAmelCase ( self ): """simple docstring""" for trim_offsets, add_prefix_space in itertools.product([True, False] , repeat=2 ): lowerCamelCase = self.rust_tokenizer_class.from_pretrained( self.tmpdirname , use_fast=_a , add_prefix_space=_a , trim_offsets=_a ) lowerCamelCase = json.loads(tokenizer_r.backend_tokenizer.pre_tokenizer.__getstate__() ) lowerCamelCase = json.loads(tokenizer_r.backend_tokenizer.post_processor.__getstate__() ) self.assertEqual(pre_tokenizer_state["""add_prefix_space"""] , _a ) self.assertEqual(post_processor_state["""add_prefix_space"""] , _a ) self.assertEqual(post_processor_state["""trim_offsets"""] , _a ) def _lowerCAmelCase ( self ): """simple docstring""" # Test which aims to verify that the offsets are well adapted to the argument `add_prefix_space` and # `trim_offsets` for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'{tokenizer.__class__.__name__} ({pretrained_name})' ): lowerCamelCase = """hello""" # `hello` is a token in the vocabulary of `pretrained_name` lowerCamelCase = f'{text_of_1_token} {text_of_1_token}' lowerCamelCase = self.rust_tokenizer_class.from_pretrained( _a , use_fast=_a , add_prefix_space=_a , trim_offsets=_a ) lowerCamelCase = tokenizer_r(_a , return_offsets_mapping=_a , add_special_tokens=_a ) self.assertEqual(encoding.offset_mapping[0] , (0, len(_a )) ) self.assertEqual( encoding.offset_mapping[1] , (len(_a ) + 1, len(_a ) + 1 + len(_a )) , ) lowerCamelCase = self.rust_tokenizer_class.from_pretrained( _a , use_fast=_a , add_prefix_space=_a , trim_offsets=_a ) lowerCamelCase = tokenizer_r(_a , return_offsets_mapping=_a , add_special_tokens=_a ) self.assertEqual(encoding.offset_mapping[0] , (0, len(_a )) ) self.assertEqual( encoding.offset_mapping[1] , (len(_a ) + 1, len(_a ) + 1 + len(_a )) , ) lowerCamelCase = self.rust_tokenizer_class.from_pretrained( _a , use_fast=_a , add_prefix_space=_a , trim_offsets=_a ) lowerCamelCase = tokenizer_r(_a , return_offsets_mapping=_a , add_special_tokens=_a ) self.assertEqual(encoding.offset_mapping[0] , (0, len(_a )) ) self.assertEqual( encoding.offset_mapping[1] , (len(_a ), len(_a ) + 1 + len(_a )) , ) lowerCamelCase = self.rust_tokenizer_class.from_pretrained( _a , use_fast=_a , add_prefix_space=_a , trim_offsets=_a ) lowerCamelCase = tokenizer_r(_a , return_offsets_mapping=_a , add_special_tokens=_a ) self.assertEqual(encoding.offset_mapping[0] , (0, len(_a )) ) self.assertEqual( encoding.offset_mapping[1] , (len(_a ), len(_a ) + 1 + len(_a )) , ) lowerCamelCase = f' {text}' # tokenizer_r = self.rust_tokenizer_class.from_pretrained( # pretrained_name, use_fast=True, add_prefix_space=True, trim_offsets=True # ) # encoding = tokenizer_r(text, return_offsets_mapping=True, add_special_tokens=False) # self.assertEqual(encoding.offset_mapping[0], (1, 1 + len(text_of_1_token))) # self.assertEqual( # encoding.offset_mapping[1], # (1 + len(text_of_1_token) + 1, 1 + len(text_of_1_token) + 1 + len(text_of_1_token)), # ) lowerCamelCase = self.rust_tokenizer_class.from_pretrained( _a , use_fast=_a , add_prefix_space=_a , trim_offsets=_a ) lowerCamelCase = tokenizer_r(_a , return_offsets_mapping=_a , add_special_tokens=_a ) self.assertEqual(encoding.offset_mapping[0] , (1, 1 + len(_a )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(_a ) + 1, 1 + len(_a ) + 1 + len(_a )) , ) lowerCamelCase = self.rust_tokenizer_class.from_pretrained( _a , use_fast=_a , add_prefix_space=_a , trim_offsets=_a ) lowerCamelCase = tokenizer_r(_a , return_offsets_mapping=_a , add_special_tokens=_a ) self.assertEqual(encoding.offset_mapping[0] , (0, 1 + len(_a )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(_a ), 1 + len(_a ) + 1 + len(_a )) , ) lowerCamelCase = self.rust_tokenizer_class.from_pretrained( _a , use_fast=_a , add_prefix_space=_a , trim_offsets=_a ) lowerCamelCase = tokenizer_r(_a , return_offsets_mapping=_a , add_special_tokens=_a ) self.assertEqual(encoding.offset_mapping[0] , (0, 1 + len(_a )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(_a ), 1 + len(_a ) + 1 + len(_a )) , )
291
"""simple docstring""" from __future__ import annotations def a__ ( snake_case__ , snake_case__ ) -> bool: if len(snake_case__ ) == 0: return False lowerCamelCase = len(snake_case__ ) // 2 if a_list[midpoint] == item: return True if item < a_list[midpoint]: return binary_search(a_list[:midpoint] , snake_case__ ) else: return binary_search(a_list[midpoint + 1 :] , snake_case__ ) if __name__ == "__main__": lowerCAmelCase : List[Any] = input("""Enter numbers separated by comma:\n""").strip() lowerCAmelCase : Optional[Any] = [int(item.strip()) for item in user_input.split(""",""")] lowerCAmelCase : Optional[int] = int(input("""Enter the number to be found in the list:\n""").strip()) lowerCAmelCase : Union[str, Any] = """""" if binary_search(sequence, target) else """not """ print(F"""{target} was {not_str}found in {sequence}""")
291
1
"""simple docstring""" from typing import Optional import torch import torch.utils.checkpoint from torch import Tensor, nn from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss from ...activations import ACTaFN from ...modeling_outputs import ( BackboneOutput, BaseModelOutputWithNoAttention, BaseModelOutputWithPoolingAndNoAttention, ImageClassifierOutputWithNoAttention, ) from ...modeling_utils import PreTrainedModel from ...utils import ( add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging, replace_return_docstrings, ) from ...utils.backbone_utils import BackboneMixin from .configuration_resnet import ResNetConfig lowerCAmelCase : Optional[int] = logging.get_logger(__name__) # General docstring lowerCAmelCase : int = """ResNetConfig""" # Base docstring lowerCAmelCase : Any = """microsoft/resnet-50""" lowerCAmelCase : Any = [1, 2048, 7, 7] # Image classification docstring lowerCAmelCase : int = """microsoft/resnet-50""" lowerCAmelCase : Tuple = """tiger cat""" lowerCAmelCase : Optional[Any] = [ """microsoft/resnet-50""", # See all resnet models at https://huggingface.co/models?filter=resnet ] class __magic_name__ ( nn.Module ): '''simple docstring''' def __init__( self , _a , _a , _a = 3 , _a = 1 , _a = "relu" ): """simple docstring""" super().__init__() lowerCamelCase = nn.Convad( _a , _a , kernel_size=_a , stride=_a , padding=kernel_size // 2 , bias=_a ) lowerCamelCase = nn.BatchNormad(_a ) lowerCamelCase = ACTaFN[activation] if activation is not None else nn.Identity() def _lowerCAmelCase ( self , _a ): """simple docstring""" lowerCamelCase = self.convolution(_a ) lowerCamelCase = self.normalization(_a ) lowerCamelCase = self.activation(_a ) return hidden_state class __magic_name__ ( nn.Module ): '''simple docstring''' def __init__( self , _a ): """simple docstring""" super().__init__() lowerCamelCase = ResNetConvLayer( config.num_channels , config.embedding_size , kernel_size=7 , stride=2 , activation=config.hidden_act ) lowerCamelCase = nn.MaxPoolad(kernel_size=3 , stride=2 , padding=1 ) lowerCamelCase = config.num_channels def _lowerCAmelCase ( self , _a ): """simple docstring""" lowerCamelCase = pixel_values.shape[1] if num_channels != self.num_channels: raise ValueError( """Make sure that the channel dimension of the pixel values match with the one set in the configuration.""" ) lowerCamelCase = self.embedder(_a ) lowerCamelCase = self.pooler(_a ) return embedding class __magic_name__ ( nn.Module ): '''simple docstring''' def __init__( self , _a , _a , _a = 2 ): """simple docstring""" super().__init__() lowerCamelCase = nn.Convad(_a , _a , kernel_size=1 , stride=_a , bias=_a ) lowerCamelCase = nn.BatchNormad(_a ) def _lowerCAmelCase ( self , _a ): """simple docstring""" lowerCamelCase = self.convolution(_a ) lowerCamelCase = self.normalization(_a ) return hidden_state class __magic_name__ ( nn.Module ): '''simple docstring''' def __init__( self , _a , _a , _a = 1 , _a = "relu" ): """simple docstring""" super().__init__() lowerCamelCase = in_channels != out_channels or stride != 1 lowerCamelCase = ( ResNetShortCut(_a , _a , stride=_a ) if should_apply_shortcut else nn.Identity() ) lowerCamelCase = nn.Sequential( ResNetConvLayer(_a , _a , stride=_a ) , ResNetConvLayer(_a , _a , activation=_a ) , ) lowerCamelCase = ACTaFN[activation] def _lowerCAmelCase ( self , _a ): """simple docstring""" lowerCamelCase = hidden_state lowerCamelCase = self.layer(_a ) lowerCamelCase = self.shortcut(_a ) hidden_state += residual lowerCamelCase = self.activation(_a ) return hidden_state class __magic_name__ ( nn.Module ): '''simple docstring''' def __init__( self , _a , _a , _a = 1 , _a = "relu" , _a = 4 ): """simple docstring""" super().__init__() lowerCamelCase = in_channels != out_channels or stride != 1 lowerCamelCase = out_channels // reduction lowerCamelCase = ( ResNetShortCut(_a , _a , stride=_a ) if should_apply_shortcut else nn.Identity() ) lowerCamelCase = nn.Sequential( ResNetConvLayer(_a , _a , kernel_size=1 ) , ResNetConvLayer(_a , _a , stride=_a ) , ResNetConvLayer(_a , _a , kernel_size=1 , activation=_a ) , ) lowerCamelCase = ACTaFN[activation] def _lowerCAmelCase ( self , _a ): """simple docstring""" lowerCamelCase = hidden_state lowerCamelCase = self.layer(_a ) lowerCamelCase = self.shortcut(_a ) hidden_state += residual lowerCamelCase = self.activation(_a ) return hidden_state class __magic_name__ ( nn.Module ): '''simple docstring''' def __init__( self , _a , _a , _a , _a = 2 , _a = 2 , ): """simple docstring""" super().__init__() lowerCamelCase = ResNetBottleNeckLayer if config.layer_type == """bottleneck""" else ResNetBasicLayer lowerCamelCase = nn.Sequential( # downsampling is done in the first layer with stride of 2 layer(_a , _a , stride=_a , activation=config.hidden_act ) , *[layer(_a , _a , activation=config.hidden_act ) for _ in range(depth - 1 )] , ) def _lowerCAmelCase ( self , _a ): """simple docstring""" lowerCamelCase = input for layer in self.layers: lowerCamelCase = layer(_a ) return hidden_state class __magic_name__ ( nn.Module ): '''simple docstring''' def __init__( self , _a ): """simple docstring""" super().__init__() lowerCamelCase = nn.ModuleList([] ) # based on `downsample_in_first_stage` the first layer of the first stage may or may not downsample the input self.stages.append( ResNetStage( _a , config.embedding_size , config.hidden_sizes[0] , stride=2 if config.downsample_in_first_stage else 1 , depth=config.depths[0] , ) ) lowerCamelCase = zip(config.hidden_sizes , config.hidden_sizes[1:] ) for (in_channels, out_channels), depth in zip(_a , config.depths[1:] ): self.stages.append(ResNetStage(_a , _a , _a , depth=_a ) ) def _lowerCAmelCase ( self , _a , _a = False , _a = True ): """simple docstring""" lowerCamelCase = () if output_hidden_states else None for stage_module in self.stages: if output_hidden_states: lowerCamelCase = hidden_states + (hidden_state,) lowerCamelCase = stage_module(_a ) if output_hidden_states: lowerCamelCase = hidden_states + (hidden_state,) if not return_dict: return tuple(v for v in [hidden_state, hidden_states] if v is not None ) return BaseModelOutputWithNoAttention( last_hidden_state=_a , hidden_states=_a , ) class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' __UpperCamelCase = ResNetConfig __UpperCamelCase = "resnet" __UpperCamelCase = "pixel_values" __UpperCamelCase = True def _lowerCAmelCase ( self , _a ): """simple docstring""" if isinstance(_a , nn.Convad ): nn.init.kaiming_normal_(module.weight , mode="""fan_out""" , nonlinearity="""relu""" ) elif isinstance(_a , (nn.BatchNormad, nn.GroupNorm) ): nn.init.constant_(module.weight , 1 ) nn.init.constant_(module.bias , 0 ) def _lowerCAmelCase ( self , _a , _a=False ): """simple docstring""" if isinstance(_a , _a ): lowerCamelCase = value lowerCAmelCase : int = r""" This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and behavior. Parameters: config ([`ResNetConfig`]): Model configuration class with all the parameters of the model. Initializing with a config file does not load the weights associated with the model, only the configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights. """ lowerCAmelCase : Optional[int] = r""" Args: pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`): Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See [`ConvNextImageProcessor.__call__`] for details. output_hidden_states (`bool`, *optional*): Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for more detail. return_dict (`bool`, *optional*): Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple. """ @add_start_docstrings( "The bare ResNet model outputting raw features without any specific head on top." , UpperCAmelCase__ , ) class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' def __init__( self , _a ): """simple docstring""" super().__init__(_a ) lowerCamelCase = config lowerCamelCase = ResNetEmbeddings(_a ) lowerCamelCase = ResNetEncoder(_a ) lowerCamelCase = nn.AdaptiveAvgPoolad((1, 1) ) # Initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(_a ) @add_code_sample_docstrings( checkpoint=_CHECKPOINT_FOR_DOC , output_type=_a , config_class=_CONFIG_FOR_DOC , modality="""vision""" , expected_output=_EXPECTED_OUTPUT_SHAPE , ) def _lowerCAmelCase ( self , _a , _a = None , _a = None ): """simple docstring""" lowerCamelCase = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) lowerCamelCase = return_dict if return_dict is not None else self.config.use_return_dict lowerCamelCase = self.embedder(_a ) lowerCamelCase = self.encoder( _a , output_hidden_states=_a , return_dict=_a ) lowerCamelCase = encoder_outputs[0] lowerCamelCase = self.pooler(_a ) if not return_dict: return (last_hidden_state, pooled_output) + encoder_outputs[1:] return BaseModelOutputWithPoolingAndNoAttention( last_hidden_state=_a , pooler_output=_a , hidden_states=encoder_outputs.hidden_states , ) @add_start_docstrings( "\n ResNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n ImageNet.\n " , UpperCAmelCase__ , ) class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' def __init__( self , _a ): """simple docstring""" super().__init__(_a ) lowerCamelCase = config.num_labels lowerCamelCase = ResNetModel(_a ) # classification head lowerCamelCase = nn.Sequential( nn.Flatten() , nn.Linear(config.hidden_sizes[-1] , config.num_labels ) if config.num_labels > 0 else nn.Identity() , ) # initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(_a ) @add_code_sample_docstrings( checkpoint=_IMAGE_CLASS_CHECKPOINT , output_type=_a , config_class=_CONFIG_FOR_DOC , expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT , ) def _lowerCAmelCase ( self , _a = None , _a = None , _a = None , _a = None , ): """simple docstring""" lowerCamelCase = return_dict if return_dict is not None else self.config.use_return_dict lowerCamelCase = self.resnet(_a , output_hidden_states=_a , return_dict=_a ) lowerCamelCase = outputs.pooler_output if return_dict else outputs[1] lowerCamelCase = self.classifier(_a ) lowerCamelCase = None if labels is not None: if self.config.problem_type is None: if self.num_labels == 1: lowerCamelCase = """regression""" elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int): lowerCamelCase = """single_label_classification""" else: lowerCamelCase = """multi_label_classification""" if self.config.problem_type == "regression": lowerCamelCase = MSELoss() if self.num_labels == 1: lowerCamelCase = loss_fct(logits.squeeze() , labels.squeeze() ) else: lowerCamelCase = loss_fct(_a , _a ) elif self.config.problem_type == "single_label_classification": lowerCamelCase = CrossEntropyLoss() lowerCamelCase = loss_fct(logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) elif self.config.problem_type == "multi_label_classification": lowerCamelCase = BCEWithLogitsLoss() lowerCamelCase = loss_fct(_a , _a ) if not return_dict: lowerCamelCase = (logits,) + outputs[2:] return (loss,) + output if loss is not None else output return ImageClassifierOutputWithNoAttention(loss=_a , logits=_a , hidden_states=outputs.hidden_states ) @add_start_docstrings( "\n ResNet backbone, to be used with frameworks like DETR and MaskFormer.\n " , UpperCAmelCase__ , ) class __magic_name__ ( UpperCAmelCase__ , UpperCAmelCase__ ): '''simple docstring''' def __init__( self , _a ): """simple docstring""" super().__init__(_a ) super()._init_backbone(_a ) lowerCamelCase = [config.embedding_size] + config.hidden_sizes lowerCamelCase = ResNetEmbeddings(_a ) lowerCamelCase = ResNetEncoder(_a ) # initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(_a ) @replace_return_docstrings(output_type=_a , config_class=_CONFIG_FOR_DOC ) def _lowerCAmelCase ( self , _a , _a = None , _a = None ): """simple docstring""" lowerCamelCase = return_dict if return_dict is not None else self.config.use_return_dict lowerCamelCase = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) lowerCamelCase = self.embedder(_a ) lowerCamelCase = self.encoder(_a , output_hidden_states=_a , return_dict=_a ) lowerCamelCase = outputs.hidden_states lowerCamelCase = () for idx, stage in enumerate(self.stage_names ): if stage in self.out_features: feature_maps += (hidden_states[idx],) if not return_dict: lowerCamelCase = (feature_maps,) if output_hidden_states: output += (outputs.hidden_states,) return output return BackboneOutput( feature_maps=_a , hidden_states=outputs.hidden_states if output_hidden_states else None , attentions=_a , )
291
"""simple docstring""" def a__ ( snake_case__ ) -> list: if len(snake_case__ ) < 2: return collection def circle_sort_util(snake_case__ , snake_case__ , snake_case__ ) -> bool: lowerCamelCase = False if low == high: return swapped lowerCamelCase = low lowerCamelCase = high while left < right: if collection[left] > collection[right]: lowerCamelCase , lowerCamelCase = ( collection[right], collection[left], ) lowerCamelCase = True left += 1 right -= 1 if left == right and collection[left] > collection[right + 1]: lowerCamelCase , lowerCamelCase = ( collection[right + 1], collection[left], ) lowerCamelCase = True lowerCamelCase = low + int((high - low) / 2 ) lowerCamelCase = circle_sort_util(snake_case__ , snake_case__ , snake_case__ ) lowerCamelCase = circle_sort_util(snake_case__ , mid + 1 , snake_case__ ) return swapped or left_swap or right_swap lowerCamelCase = True while is_not_sorted is True: lowerCamelCase = circle_sort_util(snake_case__ , 0 , len(snake_case__ ) - 1 ) return collection if __name__ == "__main__": lowerCAmelCase : Tuple = input("""Enter numbers separated by a comma:\n""").strip() lowerCAmelCase : List[Any] = [int(item) for item in user_input.split(""",""")] print(circle_sort(unsorted))
291
1
"""simple docstring""" import unittest from transformers import PegasusTokenizer, PegasusTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, require_torch, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin lowerCAmelCase : Optional[int] = get_tests_dir("""fixtures/test_sentencepiece_no_bos.model""") @require_sentencepiece @require_tokenizers class __magic_name__ ( UpperCAmelCase__ , unittest.TestCase ): '''simple docstring''' __UpperCamelCase = PegasusTokenizer __UpperCamelCase = PegasusTokenizerFast __UpperCamelCase = True __UpperCamelCase = True def _lowerCAmelCase ( self ): """simple docstring""" super().setUp() # We have a SentencePiece fixture for testing lowerCamelCase = PegasusTokenizer(_a ) tokenizer.save_pretrained(self.tmpdirname ) @cached_property def _lowerCAmelCase ( self ): """simple docstring""" return PegasusTokenizer.from_pretrained("""google/pegasus-large""" ) def _lowerCAmelCase ( self , **_a ): """simple docstring""" return PegasusTokenizer.from_pretrained(self.tmpdirname , **_a ) def _lowerCAmelCase ( self , _a ): """simple docstring""" return ("This is a test", "This is a test") def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = """</s>""" lowerCamelCase = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(_a ) , _a ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(_a ) , _a ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , """<pad>""" ) self.assertEqual(vocab_keys[1] , """</s>""" ) self.assertEqual(vocab_keys[-1] , """v""" ) self.assertEqual(len(_a ) , 1_103 ) def _lowerCAmelCase ( self ): """simple docstring""" self.assertEqual(self.get_tokenizer().vocab_size , 1_103 ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.rust_tokenizer_class.from_pretrained(self.tmpdirname ) lowerCamelCase = self.tokenizer_class.from_pretrained(self.tmpdirname ) lowerCamelCase = ( """Let's see which <unk> is the better <unk_token_11> one <mask_1> It seems like this <mask_2> was important""" """ </s> <pad> <pad> <pad>""" ) lowerCamelCase = rust_tokenizer([raw_input_str] , return_tensors=_a , add_special_tokens=_a ).input_ids[0] lowerCamelCase = py_tokenizer([raw_input_str] , return_tensors=_a , add_special_tokens=_a ).input_ids[0] self.assertListEqual(_a , _a ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self._large_tokenizer # <mask_1> masks whole sentence while <mask_2> masks single word lowerCamelCase = """<mask_1> To ensure a <mask_2> flow of bank resolutions.""" lowerCamelCase = [2, 413, 615, 114, 3, 1_971, 113, 1_679, 10_710, 107, 1] lowerCamelCase = tokenizer([raw_input_str] , return_tensors=_a ).input_ids[0] self.assertListEqual(_a , _a ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self._large_tokenizer # The tracebacks for the following asserts are **better** without messages or self.assertEqual assert tokenizer.vocab_size == 96_103 assert tokenizer.pad_token_id == 0 assert tokenizer.eos_token_id == 1 assert tokenizer.offset == 103 assert tokenizer.unk_token_id == tokenizer.offset + 2 == 105 assert tokenizer.unk_token == "<unk>" assert tokenizer.model_max_length == 1_024 lowerCamelCase = """To ensure a smooth flow of bank resolutions.""" lowerCamelCase = [413, 615, 114, 2_291, 1_971, 113, 1_679, 10_710, 107, 1] lowerCamelCase = tokenizer([raw_input_str] , return_tensors=_a ).input_ids[0] self.assertListEqual(_a , _a ) assert tokenizer.convert_ids_to_tokens([0, 1, 2, 3] ) == ["<pad>", "</s>", "<mask_1>", "<mask_2>"] @require_torch def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = ["""This is going to be way too long.""" * 150, """short example"""] lowerCamelCase = ["""not super long but more than 5 tokens""", """tiny"""] lowerCamelCase = self._large_tokenizer(_a , padding=_a , truncation=_a , return_tensors="""pt""" ) lowerCamelCase = self._large_tokenizer( text_target=_a , max_length=5 , padding=_a , truncation=_a , return_tensors="""pt""" ) assert batch.input_ids.shape == (2, 1_024) assert batch.attention_mask.shape == (2, 1_024) assert targets["input_ids"].shape == (2, 5) assert len(_a ) == 2 # input_ids, attention_mask. @slow def _lowerCAmelCase ( self ): """simple docstring""" # fmt: off lowerCamelCase = {"""input_ids""": [[38_979, 143, 18_485, 606, 130, 26_669, 87_686, 121, 54_189, 1_129, 111, 26_669, 87_686, 121, 9_114, 14_787, 121, 13_249, 158, 592, 956, 121, 14_621, 31_576, 143, 62_613, 108, 9_688, 930, 43_430, 11_562, 62_613, 304, 108, 11_443, 897, 108, 9_314, 17_415, 63_399, 108, 11_443, 7_614, 18_316, 118, 4_284, 7_148, 12_430, 143, 1_400, 25_703, 158, 111, 4_284, 7_148, 11_772, 143, 21_297, 1_064, 158, 122, 204, 3_506, 1_754, 1_133, 14_787, 1_581, 115, 33_224, 4_482, 111, 1_355, 110, 29_173, 317, 50_833, 108, 20_147, 94_665, 111, 77_198, 107, 1], [110, 62_613, 117, 638, 112, 1_133, 121, 20_098, 1_355, 79_050, 13_872, 135, 1_596, 53_541, 1_352, 141, 13_039, 5_542, 124, 302, 518, 111, 268, 2_956, 115, 149, 4_427, 107, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [139, 1_235, 2_799, 18_289, 17_780, 204, 109, 9_474, 1_296, 107, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], """attention_mask""": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=_a , model_name="""google/bigbird-pegasus-large-arxiv""" , revision="""ba85d0851d708441f91440d509690f1ab6353415""" , ) @require_sentencepiece @require_tokenizers class __magic_name__ ( UpperCAmelCase__ , unittest.TestCase ): '''simple docstring''' __UpperCamelCase = PegasusTokenizer __UpperCamelCase = PegasusTokenizerFast __UpperCamelCase = True __UpperCamelCase = True def _lowerCAmelCase ( self ): """simple docstring""" super().setUp() # We have a SentencePiece fixture for testing lowerCamelCase = PegasusTokenizer(_a , offset=0 , mask_token_sent=_a , mask_token="""[MASK]""" ) tokenizer.save_pretrained(self.tmpdirname ) @cached_property def _lowerCAmelCase ( self ): """simple docstring""" return PegasusTokenizer.from_pretrained("""google/bigbird-pegasus-large-arxiv""" ) def _lowerCAmelCase ( self , **_a ): """simple docstring""" return PegasusTokenizer.from_pretrained(self.tmpdirname , **_a ) def _lowerCAmelCase ( self , _a ): """simple docstring""" return ("This is a test", "This is a test") def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.rust_tokenizer_class.from_pretrained(self.tmpdirname ) lowerCamelCase = self.tokenizer_class.from_pretrained(self.tmpdirname ) lowerCamelCase = ( """Let's see which <unk> is the better <unk_token> one [MASK] It seems like this [MASK] was important </s>""" """ <pad> <pad> <pad>""" ) lowerCamelCase = rust_tokenizer([raw_input_str] , return_tensors=_a , add_special_tokens=_a ).input_ids[0] lowerCamelCase = py_tokenizer([raw_input_str] , return_tensors=_a , add_special_tokens=_a ).input_ids[0] self.assertListEqual(_a , _a ) @require_torch def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = ["""This is going to be way too long.""" * 1_000, """short example"""] lowerCamelCase = ["""not super long but more than 5 tokens""", """tiny"""] lowerCamelCase = self._large_tokenizer(_a , padding=_a , truncation=_a , return_tensors="""pt""" ) lowerCamelCase = self._large_tokenizer( text_target=_a , max_length=5 , padding=_a , truncation=_a , return_tensors="""pt""" ) assert batch.input_ids.shape == (2, 4_096) assert batch.attention_mask.shape == (2, 4_096) assert targets["input_ids"].shape == (2, 5) assert len(_a ) == 2 # input_ids, attention_mask. def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = ( """This is an example string that is used to test the original TF implementation against the HF""" """ implementation""" ) lowerCamelCase = self._large_tokenizer(_a ).input_ids self.assertListEqual( _a , [182, 117, 142, 587, 4_211, 120, 117, 263, 112, 804, 109, 856, 25_016, 3_137, 464, 109, 26_955, 3_137, 1] , )
291
"""simple docstring""" from collections.abc import Generator def a__ ( ) -> Generator[int, None, None]: lowerCamelCase , lowerCamelCase = 0, 1 while True: lowerCamelCase , lowerCamelCase = b, a + b yield b def a__ ( snake_case__ = 10_00 ) -> int: lowerCamelCase = 1 lowerCamelCase = fibonacci_generator() while len(str(next(snake_case__ ) ) ) < n: answer += 1 return answer + 1 if __name__ == "__main__": print(solution(int(str(input()).strip())))
291
1
"""simple docstring""" from ...processing_utils import ProcessorMixin class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' __UpperCamelCase = "WhisperFeatureExtractor" __UpperCamelCase = "WhisperTokenizer" def __init__( self , _a , _a ): """simple docstring""" super().__init__(_a , _a ) lowerCamelCase = self.feature_extractor lowerCamelCase = False def _lowerCAmelCase ( self , _a=None , _a=None , _a=True ): """simple docstring""" return self.tokenizer.get_decoder_prompt_ids(task=_a , language=_a , no_timestamps=_a ) def __call__( self , *_a , **_a ): """simple docstring""" # For backward compatibility if self._in_target_context_manager: return self.current_processor(*_a , **_a ) lowerCamelCase = kwargs.pop("""audio""" , _a ) lowerCamelCase = kwargs.pop("""sampling_rate""" , _a ) lowerCamelCase = kwargs.pop("""text""" , _a ) if len(_a ) > 0: lowerCamelCase = args[0] lowerCamelCase = args[1:] if audio is None and text is None: raise ValueError("""You need to specify either an `audio` or `text` input to process.""" ) if audio is not None: lowerCamelCase = self.feature_extractor(_a , *_a , sampling_rate=_a , **_a ) if text is not None: lowerCamelCase = self.tokenizer(_a , **_a ) if text is None: return inputs elif audio is None: return encodings else: lowerCamelCase = encodings["""input_ids"""] return inputs def _lowerCAmelCase ( self , *_a , **_a ): """simple docstring""" return self.tokenizer.batch_decode(*_a , **_a ) def _lowerCAmelCase ( self , *_a , **_a ): """simple docstring""" return self.tokenizer.decode(*_a , **_a ) def _lowerCAmelCase ( self , _a , _a="np" ): """simple docstring""" return self.tokenizer.get_prompt_ids(_a , return_tensors=_a )
291
"""simple docstring""" from math import ceil from typing import List, Optional, Union import numpy as np from ...audio_utils import mel_filter_bank, spectrogram, window_function from ...feature_extraction_sequence_utils import BatchFeature, SequenceFeatureExtractor from ...utils import TensorType, logging lowerCAmelCase : List[str] = logging.get_logger(__name__) class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' __UpperCamelCase = ["audio_values", "audio_mask"] def __init__( self , _a=2_048 , _a=1 , _a=[16, 16] , _a=128 , _a=44_100 , _a=86 , _a=2_048 , _a=0.0 , **_a , ): """simple docstring""" super().__init__( feature_size=_a , sampling_rate=_a , padding_value=_a , **_a , ) lowerCamelCase = spectrogram_length lowerCamelCase = num_channels lowerCamelCase = patch_size lowerCamelCase = feature_size // self.patch_size[1] lowerCamelCase = n_fft lowerCamelCase = sampling_rate // hop_length_to_sampling_rate lowerCamelCase = sampling_rate lowerCamelCase = padding_value lowerCamelCase = mel_filter_bank( num_frequency_bins=1 + n_fft // 2 , num_mel_filters=_a , min_frequency=0.0 , max_frequency=22_050.0 , sampling_rate=_a , norm="""slaney""" , mel_scale="""slaney""" , ).T def _lowerCAmelCase ( self , _a ): """simple docstring""" lowerCamelCase = spectrogram( _a , window_function(self.n_fft , """hann""" ) , frame_length=self.n_fft , hop_length=self.hop_length , power=2.0 , mel_filters=self.mel_filters.T , log_mel="""dB""" , db_range=80.0 , ) lowerCamelCase = log_spec[:, :-1] lowerCamelCase = log_spec - 20.0 lowerCamelCase = np.clip(log_spec / 40.0 , -2.0 , 0.0 ) + 1.0 return log_spec def __call__( self , _a , _a = None , _a = True , _a = None , _a = False , _a = False , **_a , ): """simple docstring""" if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( """This feature extractor is set to support sampling rate""" f' of {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled' f' 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.""" ) lowerCamelCase = isinstance(_a , np.ndarray ) and len(raw_speech.shape ) > 1 if is_batched_numpy and len(raw_speech.shape ) > 2: raise ValueError(f'Only mono-channel audio is supported for input to {self}' ) lowerCamelCase = is_batched_numpy or ( isinstance(_a , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: lowerCamelCase = [np.asarray([speech] , dtype=np.floataa ).T for speech in raw_speech] elif not is_batched and not isinstance(_a , np.ndarray ): lowerCamelCase = np.asarray(_a , dtype=np.floataa ) elif isinstance(_a , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): lowerCamelCase = raw_speech.astype(np.floataa ) # always return batch if not is_batched: lowerCamelCase = [np.asarray([raw_speech] ).T] # Convert audio signals to log mel spectrograms, truncate by time axis lowerCamelCase = [ self._np_extract_fbank_features(waveform.squeeze() ).T[: self.spectrogram_length] for waveform in raw_speech ] if isinstance(audio_features[0] , _a ): lowerCamelCase = [np.asarray(_a , dtype=np.floataa ) for feature in audio_features] # Create audio attention mask lowerCamelCase = max( [ceil(feature.shape[0] / self.patch_size[0] ) * self.freq_len for feature in audio_features] ) # The maximum number of audio patches in a batch if return_attention_mask: lowerCamelCase = [ (ceil(feature.shape[0] / self.patch_size[0] ) * self.freq_len) * [1] + (max_patch_len - ceil(feature.shape[0] / self.patch_size[0] ) * self.freq_len) * [0] for feature in audio_features ] lowerCamelCase = np.array(_a ).astype(np.floataa ) # convert into correct format for padding lowerCamelCase = max_patch_len // self.freq_len * self.patch_size[0] # The maximum audio size in a batch lowerCamelCase = np.ones([len(_a ), 1, max_time_len, self.feature_size] ).astype(np.floataa ) lowerCamelCase = padded_audio_features * self.padding_value for i in range(len(_a ) ): lowerCamelCase = audio_features[i] lowerCamelCase = feature # return as BatchFeature if return_attention_mask: lowerCamelCase = {"""audio_values""": padded_audio_features, """audio_mask""": audio_mask} else: lowerCamelCase = {"""audio_values""": padded_audio_features} lowerCamelCase = BatchFeature(data=_a , tensor_type=_a ) return encoded_inputs
291
1
"""simple docstring""" from typing import TYPE_CHECKING from ....utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCAmelCase : Union[str, Any] = { """configuration_trajectory_transformer""": [ """TRAJECTORY_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP""", """TrajectoryTransformerConfig""", ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : Optional[Any] = [ """TRAJECTORY_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST""", """TrajectoryTransformerModel""", """TrajectoryTransformerPreTrainedModel""", """load_tf_weights_in_trajectory_transformer""", ] if TYPE_CHECKING: from .configuration_trajectory_transformer import ( TRAJECTORY_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TrajectoryTransformerConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_trajectory_transformer import ( TRAJECTORY_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TrajectoryTransformerModel, TrajectoryTransformerPreTrainedModel, load_tf_weights_in_trajectory_transformer, ) else: import sys lowerCAmelCase : Optional[int] = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
291
"""simple docstring""" from math import ceil def a__ ( snake_case__ , snake_case__ ) -> Optional[int]: lowerCamelCase = list(range(0 , snake_case__ ) ) lowerCamelCase = [item for sublist in list(device_map.values() ) for item in sublist] # Duplicate check lowerCamelCase = [] for i in device_map_blocks: if device_map_blocks.count(snake_case__ ) > 1 and i not in duplicate_blocks: duplicate_blocks.append(snake_case__ ) # Missing blocks lowerCamelCase = [i for i in blocks if i not in device_map_blocks] lowerCamelCase = [i for i in device_map_blocks if i not in blocks] if len(snake_case__ ) != 0: raise ValueError( """Duplicate attention blocks specified in device_map. Attention blocks must be specified to one device.""" """ These attention blocks were specified more than once: """ + str(snake_case__ ) ) if len(snake_case__ ) != 0: raise ValueError( """There are attention blocks for this model that are not specified in the device_map. Add these attention """ """blocks to a device on the device_map: """ + str(snake_case__ ) ) if len(snake_case__ ) != 0: raise ValueError( """The device_map contains more attention blocks than this model has. Remove these from the device_map:""" + str(snake_case__ ) ) def a__ ( snake_case__ , snake_case__ ) -> List[Any]: lowerCamelCase = list(range(snake_case__ ) ) lowerCamelCase = int(ceil(n_layers / len(snake_case__ ) ) ) lowerCamelCase = [layers[i : i + n_blocks] for i in range(0 , snake_case__ , snake_case__ )] return dict(zip(snake_case__ , snake_case__ ) )
291
1
"""simple docstring""" import unittest import numpy as np from transformers import RoFormerConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax.numpy as jnp from transformers.models.roformer.modeling_flax_roformer import ( FlaxRoFormerForMaskedLM, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerModel, ) class __magic_name__ ( unittest.TestCase ): '''simple docstring''' def __init__( self , _a , _a=13 , _a=7 , _a=True , _a=True , _a=True , _a=True , _a=99 , _a=32 , _a=5 , _a=4 , _a=37 , _a="gelu" , _a=0.1 , _a=0.1 , _a=512 , _a=16 , _a=2 , _a=0.02 , _a=4 , ): """simple docstring""" lowerCamelCase = parent lowerCamelCase = batch_size lowerCamelCase = seq_length lowerCamelCase = is_training lowerCamelCase = use_attention_mask lowerCamelCase = use_token_type_ids lowerCamelCase = use_labels lowerCamelCase = vocab_size lowerCamelCase = hidden_size lowerCamelCase = num_hidden_layers lowerCamelCase = num_attention_heads lowerCamelCase = intermediate_size lowerCamelCase = hidden_act lowerCamelCase = hidden_dropout_prob lowerCamelCase = attention_probs_dropout_prob lowerCamelCase = max_position_embeddings lowerCamelCase = type_vocab_size lowerCamelCase = type_sequence_label_size lowerCamelCase = initializer_range lowerCamelCase = num_choices def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) lowerCamelCase = None if self.use_attention_mask: lowerCamelCase = random_attention_mask([self.batch_size, self.seq_length] ) lowerCamelCase = None if self.use_token_type_ids: lowerCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) lowerCamelCase = RoFormerConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=_a , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.prepare_config_and_inputs() lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase = config_and_inputs lowerCamelCase = {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": attention_mask} return config, inputs_dict @require_flax class __magic_name__ ( UpperCAmelCase__ , unittest.TestCase ): '''simple docstring''' __UpperCamelCase = True __UpperCamelCase = ( ( FlaxRoFormerModel, FlaxRoFormerForMaskedLM, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, ) if is_flax_available() else () ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = FlaxRoFormerModelTester(self ) @slow def _lowerCAmelCase ( self ): """simple docstring""" for model_class_name in self.all_model_classes: lowerCamelCase = model_class_name.from_pretrained("""junnyu/roformer_chinese_small""" , from_pt=_a ) lowerCamelCase = model(np.ones((1, 1) ) ) self.assertIsNotNone(_a ) @require_flax class __magic_name__ ( unittest.TestCase ): '''simple docstring''' @slow def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = FlaxRoFormerForMaskedLM.from_pretrained("""junnyu/roformer_chinese_base""" ) lowerCamelCase = jnp.array([[0, 1, 2, 3, 4, 5]] ) lowerCamelCase = model(_a )[0] lowerCamelCase = 50_000 lowerCamelCase = (1, 6, vocab_size) self.assertEqual(output.shape , _a ) lowerCamelCase = jnp.array( [[[-0.1_205, -1.0_265, 0.2_922], [-1.5_134, 0.1_974, 0.1_519], [-5.0_135, -3.9_003, -0.8_404]]] ) self.assertTrue(jnp.allclose(output[:, :3, :3] , _a , atol=1e-4 ) )
291
"""simple docstring""" import unittest import numpy as np from transformers import RoFormerConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax.numpy as jnp from transformers.models.roformer.modeling_flax_roformer import ( FlaxRoFormerForMaskedLM, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerModel, ) class __magic_name__ ( unittest.TestCase ): '''simple docstring''' def __init__( self , _a , _a=13 , _a=7 , _a=True , _a=True , _a=True , _a=True , _a=99 , _a=32 , _a=5 , _a=4 , _a=37 , _a="gelu" , _a=0.1 , _a=0.1 , _a=512 , _a=16 , _a=2 , _a=0.02 , _a=4 , ): """simple docstring""" lowerCamelCase = parent lowerCamelCase = batch_size lowerCamelCase = seq_length lowerCamelCase = is_training lowerCamelCase = use_attention_mask lowerCamelCase = use_token_type_ids lowerCamelCase = use_labels lowerCamelCase = vocab_size lowerCamelCase = hidden_size lowerCamelCase = num_hidden_layers lowerCamelCase = num_attention_heads lowerCamelCase = intermediate_size lowerCamelCase = hidden_act lowerCamelCase = hidden_dropout_prob lowerCamelCase = attention_probs_dropout_prob lowerCamelCase = max_position_embeddings lowerCamelCase = type_vocab_size lowerCamelCase = type_sequence_label_size lowerCamelCase = initializer_range lowerCamelCase = num_choices def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) lowerCamelCase = None if self.use_attention_mask: lowerCamelCase = random_attention_mask([self.batch_size, self.seq_length] ) lowerCamelCase = None if self.use_token_type_ids: lowerCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) lowerCamelCase = RoFormerConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=_a , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.prepare_config_and_inputs() lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase = config_and_inputs lowerCamelCase = {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": attention_mask} return config, inputs_dict @require_flax class __magic_name__ ( UpperCAmelCase__ , unittest.TestCase ): '''simple docstring''' __UpperCamelCase = True __UpperCamelCase = ( ( FlaxRoFormerModel, FlaxRoFormerForMaskedLM, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, ) if is_flax_available() else () ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = FlaxRoFormerModelTester(self ) @slow def _lowerCAmelCase ( self ): """simple docstring""" for model_class_name in self.all_model_classes: lowerCamelCase = model_class_name.from_pretrained("""junnyu/roformer_chinese_small""" , from_pt=_a ) lowerCamelCase = model(np.ones((1, 1) ) ) self.assertIsNotNone(_a ) @require_flax class __magic_name__ ( unittest.TestCase ): '''simple docstring''' @slow def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = FlaxRoFormerForMaskedLM.from_pretrained("""junnyu/roformer_chinese_base""" ) lowerCamelCase = jnp.array([[0, 1, 2, 3, 4, 5]] ) lowerCamelCase = model(_a )[0] lowerCamelCase = 50_000 lowerCamelCase = (1, 6, vocab_size) self.assertEqual(output.shape , _a ) lowerCamelCase = jnp.array( [[[-0.1_205, -1.0_265, 0.2_922], [-1.5_134, 0.1_974, 0.1_519], [-5.0_135, -3.9_003, -0.8_404]]] ) self.assertTrue(jnp.allclose(output[:, :3, :3] , _a , atol=1e-4 ) )
291
1
"""simple docstring""" def a__ ( snake_case__ ) -> bool: lowerCamelCase = 0 for ch in input_str: lowerCamelCase = ord(snake_case__ ) lowerCamelCase = pow(2 , snake_case__ ) # If we already turned on bit for current character's unicode if bitmap >> ch_unicode & 1 == 1: return False bitmap |= ch_bit_index_on return True if __name__ == "__main__": import doctest doctest.testmod()
291
"""simple docstring""" from typing import Any def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ) -> list: _validation( snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ) # Creates data structures and fill initial step lowerCamelCase = {} lowerCamelCase = {} for state in states_space: lowerCamelCase = observations_space[0] lowerCamelCase = ( initial_probabilities[state] * emission_probabilities[state][observation] ) lowerCamelCase = None # Fills the data structure with the probabilities of # different transitions and pointers to previous states for o in range(1 , len(snake_case__ ) ): lowerCamelCase = observations_space[o] lowerCamelCase = observations_space[o - 1] for state in states_space: # Calculates the argmax for probability function lowerCamelCase = """""" lowerCamelCase = -1 for k_state in states_space: lowerCamelCase = ( probabilities[(k_state, prior_observation)] * transition_probabilities[k_state][state] * emission_probabilities[state][observation] ) if probability > max_probability: lowerCamelCase = probability lowerCamelCase = k_state # Update probabilities and pointers dicts lowerCamelCase = ( probabilities[(arg_max, prior_observation)] * transition_probabilities[arg_max][state] * emission_probabilities[state][observation] ) lowerCamelCase = arg_max # The final observation lowerCamelCase = observations_space[len(snake_case__ ) - 1] # argmax for given final observation lowerCamelCase = """""" lowerCamelCase = -1 for k_state in states_space: lowerCamelCase = probabilities[(k_state, final_observation)] if probability > max_probability: lowerCamelCase = probability lowerCamelCase = k_state lowerCamelCase = arg_max # Process pointers backwards lowerCamelCase = last_state lowerCamelCase = [] for o in range(len(snake_case__ ) - 1 , -1 , -1 ): result.append(snake_case__ ) lowerCamelCase = pointers[previous, observations_space[o]] result.reverse() return result def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ) -> None: _validate_not_empty( snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ) _validate_lists(snake_case__ , snake_case__ ) _validate_dicts( snake_case__ , snake_case__ , snake_case__ ) def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ) -> None: if not all( [ observations_space, states_space, initial_probabilities, transition_probabilities, emission_probabilities, ] ): raise ValueError("""There's an empty parameter""" ) def a__ ( snake_case__ , snake_case__ ) -> None: _validate_list(snake_case__ , """observations_space""" ) _validate_list(snake_case__ , """states_space""" ) def a__ ( snake_case__ , snake_case__ ) -> None: if not isinstance(_object , snake_case__ ): lowerCamelCase = F'{var_name} must be a list' raise ValueError(snake_case__ ) else: for x in _object: if not isinstance(snake_case__ , snake_case__ ): lowerCamelCase = F'{var_name} must be a list of strings' raise ValueError(snake_case__ ) def a__ ( snake_case__ , snake_case__ , snake_case__ , ) -> None: _validate_dict(snake_case__ , """initial_probabilities""" , snake_case__ ) _validate_nested_dict(snake_case__ , """transition_probabilities""" ) _validate_nested_dict(snake_case__ , """emission_probabilities""" ) def a__ ( snake_case__ , snake_case__ ) -> None: _validate_dict(_object , snake_case__ , snake_case__ ) for x in _object.values(): _validate_dict(snake_case__ , snake_case__ , snake_case__ , snake_case__ ) def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ = False ) -> None: if not isinstance(_object , snake_case__ ): lowerCamelCase = F'{var_name} must be a dict' raise ValueError(snake_case__ ) if not all(isinstance(snake_case__ , snake_case__ ) for x in _object ): lowerCamelCase = F'{var_name} all keys must be strings' raise ValueError(snake_case__ ) if not all(isinstance(snake_case__ , snake_case__ ) for x in _object.values() ): lowerCamelCase = """nested dictionary """ if nested else """""" lowerCamelCase = F'{var_name} {nested_text}all values must be {value_type.__name__}' raise ValueError(snake_case__ ) if __name__ == "__main__": from doctest import testmod testmod()
291
1
"""simple docstring""" import html from ...feature_extraction_utils import BatchFeature, FeatureExtractionMixin from ...utils import is_bsa_available, logging, requires_backends if is_bsa_available(): import bsa from bsa import BeautifulSoup lowerCAmelCase : Dict = logging.get_logger(__name__) class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' def __init__( self , **_a ): """simple docstring""" requires_backends(self , ["""bs4"""] ) super().__init__(**_a ) def _lowerCAmelCase ( self , _a ): """simple docstring""" lowerCamelCase = [] lowerCamelCase = [] lowerCamelCase = element if element.name else element.parent for parent in child.parents: # type: bs4.element.Tag lowerCamelCase = parent.find_all(child.name , recursive=_a ) xpath_tags.append(child.name ) xpath_subscripts.append( 0 if 1 == len(_a ) else next(i for i, s in enumerate(_a , 1 ) if s is child ) ) lowerCamelCase = parent xpath_tags.reverse() xpath_subscripts.reverse() return xpath_tags, xpath_subscripts def _lowerCAmelCase ( self , _a ): """simple docstring""" lowerCamelCase = BeautifulSoup(_a , """html.parser""" ) lowerCamelCase = [] lowerCamelCase = [] lowerCamelCase = [] for element in html_code.descendants: if type(_a ) == bsa.element.NavigableString: if type(element.parent ) != bsa.element.Tag: continue lowerCamelCase = html.unescape(_a ).strip() if not text_in_this_tag: continue all_doc_strings.append(_a ) lowerCamelCase , lowerCamelCase = self.xpath_soup(_a ) stringaxtag_seq.append(_a ) stringaxsubs_seq.append(_a ) if len(_a ) != len(_a ): raise ValueError("""Number of doc strings and xtags does not correspond""" ) if len(_a ) != len(_a ): raise ValueError("""Number of doc strings and xsubs does not correspond""" ) return all_doc_strings, stringaxtag_seq, stringaxsubs_seq def _lowerCAmelCase ( self , _a , _a ): """simple docstring""" lowerCamelCase = """""" for tagname, subs in zip(_a , _a ): xpath += f'/{tagname}' if subs != 0: xpath += f'[{subs}]' return xpath def __call__( self , _a ): """simple docstring""" lowerCamelCase = False # Check that strings has a valid type if isinstance(_a , _a ): lowerCamelCase = True elif isinstance(_a , (list, tuple) ): if len(_a ) == 0 or isinstance(html_strings[0] , _a ): lowerCamelCase = True if not valid_strings: raise ValueError( """HTML strings must of type `str`, `List[str]` (batch of examples), """ f'but is of type {type(_a )}.' ) lowerCamelCase = bool(isinstance(_a , (list, tuple) ) and (isinstance(html_strings[0] , _a )) ) if not is_batched: lowerCamelCase = [html_strings] # Get nodes + xpaths lowerCamelCase = [] lowerCamelCase = [] for html_string in html_strings: lowerCamelCase , lowerCamelCase , lowerCamelCase = self.get_three_from_single(_a ) nodes.append(_a ) lowerCamelCase = [] for node, tag_list, sub_list in zip(_a , _a , _a ): lowerCamelCase = self.construct_xpath(_a , _a ) xpath_strings.append(_a ) xpaths.append(_a ) # return as Dict lowerCamelCase = {"""nodes""": nodes, """xpaths""": xpaths} lowerCamelCase = BatchFeature(data=_a , tensor_type=_a ) return encoded_inputs
291
"""simple docstring""" import argparse from pathlib import Path import torch from transformers import OPTConfig, OPTModel from transformers.utils import logging logging.set_verbosity_info() lowerCAmelCase : Dict = logging.get_logger(__name__) def a__ ( snake_case__ ) -> Dict: lowerCamelCase = torch.load(snake_case__ , map_location="""cpu""" ) if "model" in sd.keys(): lowerCamelCase = torch.load(snake_case__ , map_location="""cpu""" )["""model"""] # pop unnecessary weights lowerCamelCase = [ """decoder.version""", """decoder.output_projection.weight""", ] for key in keys_to_delete: if key in sd: sd.pop(snake_case__ ) lowerCamelCase = { """decoder.project_in_dim.weight""": """decoder.project_in.weight""", """decoder.project_out_dim.weight""": """decoder.project_out.weight""", """decoder.layer_norm.weight""": """decoder.final_layer_norm.weight""", """decoder.layer_norm.bias""": """decoder.final_layer_norm.bias""", } for old_key, new_key in keys_to_rename.items(): if old_key in sd: lowerCamelCase = sd.pop(snake_case__ ) lowerCamelCase = list(sd.keys() ) for key in keys: if ".qkv_proj." in key: lowerCamelCase = sd[key] # We split QKV in separate Q,K,V lowerCamelCase = key.replace(""".qkv_proj.""" , """.q_proj.""" ) lowerCamelCase = key.replace(""".qkv_proj.""" , """.k_proj.""" ) lowerCamelCase = key.replace(""".qkv_proj.""" , """.v_proj.""" ) lowerCamelCase = value.shape[0] assert depth % 3 == 0 # `SequeuceParallelTransformerBlock` has QKV weight is separated in K,V,Q despite the naming: # https://cs.github.com/facebookresearch/metaseq/blob/51871bd73cd04c038f239ea2a26db1d7f6b37927/metaseq/modules/sequence_parallel_transformer_layer.py#L97 lowerCamelCase , lowerCamelCase , lowerCamelCase = torch.split(snake_case__ , depth // 3 , dim=0 ) lowerCamelCase = q lowerCamelCase = k lowerCamelCase = v del sd[key] return sd @torch.no_grad() def a__ ( snake_case__ , snake_case__ , snake_case__=None ) -> Tuple: lowerCamelCase = load_checkpoint(snake_case__ ) if config is not None: lowerCamelCase = OPTConfig.from_pretrained(snake_case__ ) else: lowerCamelCase = OPTConfig() lowerCamelCase = OPTModel(snake_case__ ).half().eval() model.load_state_dict(snake_case__ ) # Check results Path(snake_case__ ).mkdir(exist_ok=snake_case__ ) model.save_pretrained(snake_case__ ) if __name__ == "__main__": lowerCAmelCase : List[str] = argparse.ArgumentParser() # Required parameters parser.add_argument( """--fairseq_path""", type=str, help=( """path to fairseq checkpoint in correct format. You can find all checkpoints in the correct format here:""" """ https://huggingface.co/models?other=opt_metasq""" ), ) parser.add_argument("""--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""") parser.add_argument("""--hf_config""", default=None, type=str, help="""Define HF config.""") lowerCAmelCase : Optional[Any] = parser.parse_args() convert_opt_checkpoint(args.fairseq_path, args.pytorch_dump_folder_path, config=args.hf_config)
291
1
"""simple docstring""" import math from collections.abc import Iterator from itertools import takewhile def a__ ( snake_case__ ) -> bool: if 1 < number < 4: # 2 and 3 are primes return True elif number < 2 or number % 2 == 0 or number % 3 == 0: # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes return False # All primes number are in format of 6k +/- 1 for i in range(5 , int(math.sqrt(snake_case__ ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True def a__ ( ) -> Iterator[int]: lowerCamelCase = 2 while True: if is_prime(snake_case__ ): yield num num += 1 def a__ ( snake_case__ = 2_00_00_00 ) -> int: return sum(takewhile(lambda snake_case__ : x < n , prime_generator() ) ) if __name__ == "__main__": print(F"""{solution() = }""")
291
"""simple docstring""" import json import os import shutil import tempfile import unittest import numpy as np from transformers import BertTokenizerFast from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES, BertTokenizer from transformers.testing_utils import require_tokenizers, require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import VisionTextDualEncoderProcessor, ViTImageProcessor @require_tokenizers @require_vision class __magic_name__ ( unittest.TestCase ): '''simple docstring''' def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = tempfile.mkdtemp() # fmt: off lowerCamelCase = ["""[UNK]""", """[CLS]""", """[SEP]""", """[PAD]""", """[MASK]""", """want""", """##want""", """##ed""", """wa""", """un""", """runn""", """##ing""", """,""", """low""", """lowest"""] # fmt: on lowerCamelCase = 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] ) ) lowerCamelCase = { """do_resize""": True, """size""": {"""height""": 18, """width""": 18}, """do_normalize""": True, """image_mean""": [0.5, 0.5, 0.5], """image_std""": [0.5, 0.5, 0.5], } lowerCamelCase = os.path.join(self.tmpdirname , _a ) with open(self.image_processor_file , """w""" , encoding="""utf-8""" ) as fp: json.dump(_a , _a ) def _lowerCAmelCase ( self , **_a ): """simple docstring""" return BertTokenizer.from_pretrained(self.tmpdirname , **_a ) def _lowerCAmelCase ( self , **_a ): """simple docstring""" return ViTImageProcessor.from_pretrained(self.tmpdirname , **_a ) def _lowerCAmelCase ( self ): """simple docstring""" shutil.rmtree(self.tmpdirname ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = [np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta )] lowerCamelCase = [Image.fromarray(np.moveaxis(_a , 0 , -1 ) ) for x in image_inputs] return image_inputs def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.get_tokenizer() lowerCamelCase = self.get_image_processor() lowerCamelCase = VisionTextDualEncoderProcessor(tokenizer=_a , image_processor=_a ) processor.save_pretrained(self.tmpdirname ) lowerCamelCase = VisionTextDualEncoderProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer.get_vocab() ) self.assertIsInstance(processor.tokenizer , (BertTokenizer, BertTokenizerFast) ) self.assertEqual(processor.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertIsInstance(processor.image_processor , _a ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = VisionTextDualEncoderProcessor( tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) lowerCamelCase = self.get_tokenizer(bos_token="""(BOS)""" , eos_token="""(EOS)""" ) lowerCamelCase = self.get_image_processor(do_normalize=_a , padding_value=1.0 ) lowerCamelCase = VisionTextDualEncoderProcessor.from_pretrained( self.tmpdirname , bos_token="""(BOS)""" , eos_token="""(EOS)""" , do_normalize=_a , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , (BertTokenizer, BertTokenizerFast) ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , _a ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.get_image_processor() lowerCamelCase = self.get_tokenizer() lowerCamelCase = VisionTextDualEncoderProcessor(tokenizer=_a , image_processor=_a ) lowerCamelCase = self.prepare_image_inputs() lowerCamelCase = image_processor(_a , return_tensors="""np""" ) lowerCamelCase = processor(images=_a , return_tensors="""np""" ) for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() , input_processor[key].sum() , delta=1e-2 ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.get_image_processor() lowerCamelCase = self.get_tokenizer() lowerCamelCase = VisionTextDualEncoderProcessor(tokenizer=_a , image_processor=_a ) lowerCamelCase = """lower newer""" lowerCamelCase = processor(text=_a ) lowerCamelCase = tokenizer(_a ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.get_image_processor() lowerCamelCase = self.get_tokenizer() lowerCamelCase = VisionTextDualEncoderProcessor(tokenizer=_a , image_processor=_a ) lowerCamelCase = """lower newer""" lowerCamelCase = self.prepare_image_inputs() lowerCamelCase = processor(text=_a , images=_a ) self.assertListEqual(list(inputs.keys() ) , ["""input_ids""", """token_type_ids""", """attention_mask""", """pixel_values"""] ) # test if it raises when no input is passed with self.assertRaises(_a ): processor() def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.get_image_processor() lowerCamelCase = self.get_tokenizer() lowerCamelCase = VisionTextDualEncoderProcessor(tokenizer=_a , image_processor=_a ) lowerCamelCase = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] lowerCamelCase = processor.batch_decode(_a ) lowerCamelCase = tokenizer.batch_decode(_a ) self.assertListEqual(_a , _a ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.get_image_processor() lowerCamelCase = self.get_tokenizer() lowerCamelCase = VisionTextDualEncoderProcessor(tokenizer=_a , image_processor=_a ) lowerCamelCase = """lower newer""" lowerCamelCase = self.prepare_image_inputs() lowerCamelCase = processor(text=_a , images=_a ) self.assertListEqual(list(inputs.keys() ) , processor.model_input_names )
291
1
"""simple docstring""" # 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. lowerCAmelCase : Tuple = abspath(join(dirname(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__ ( snake_case__ ) -> Optional[Any]: from transformers.testing_utils import pytest_addoption_shared pytest_addoption_shared(snake_case__ ) def a__ ( snake_case__ ) -> Union[str, Any]: from transformers.testing_utils import pytest_terminal_summary_main lowerCamelCase = terminalreporter.config.getoption("""--make-reports""" ) if make_reports: pytest_terminal_summary_main(snake_case__ , id=snake_case__ )
291
"""simple docstring""" import importlib import sys from argparse import REMAINDER, ArgumentParser from pathlib import Path import torch_xla.distributed.xla_multiprocessing as xmp def a__ ( ) -> Union[str, Any]: lowerCamelCase = ArgumentParser( description=( """PyTorch TPU distributed training launch """ """helper utility that will spawn up """ """multiple distributed processes""" ) ) # Optional arguments for the launch helper parser.add_argument("""--num_cores""" , type=snake_case__ , default=1 , help="""Number of TPU cores to use (1 or 8).""" ) # positional parser.add_argument( """training_script""" , type=snake_case__ , help=( """The full path to the single TPU training """ """program/script to be launched in parallel, """ """followed by all the arguments for the """ """training script""" ) , ) # rest from the training program parser.add_argument("""training_script_args""" , nargs=snake_case__ ) return parser.parse_args() def a__ ( ) -> List[str]: lowerCamelCase = parse_args() # Import training_script as a module. lowerCamelCase = Path(args.training_script ) sys.path.append(str(script_fpath.parent.resolve() ) ) lowerCamelCase = script_fpath.stem lowerCamelCase = importlib.import_module(snake_case__ ) # Patch sys.argv lowerCamelCase = [args.training_script] + args.training_script_args + ["""--tpu_num_cores""", str(args.num_cores )] xmp.spawn(mod._mp_fn , args=() , nprocs=args.num_cores ) if __name__ == "__main__": main()
291
1
"""simple docstring""" import qiskit def a__ ( snake_case__ = 2 ) -> qiskit.result.counts.Counts: lowerCamelCase = qubits # Using Aer's simulator lowerCamelCase = qiskit.Aer.get_backend("""aer_simulator""" ) # Creating a Quantum Circuit acting on the q register lowerCamelCase = qiskit.QuantumCircuit(snake_case__ , snake_case__ ) # Adding a H gate on qubit 0 (now q0 in superposition) circuit.h(0 ) for i in range(1 , snake_case__ ): # Adding CX (CNOT) gate circuit.cx(i - 1 , snake_case__ ) # Mapping the quantum measurement to the classical bits circuit.measure(list(range(snake_case__ ) ) , list(range(snake_case__ ) ) ) # Now measuring any one qubit would affect other qubits to collapse # their super position and have same state as the measured one. # Executing the circuit on the simulator lowerCamelCase = qiskit.execute(snake_case__ , snake_case__ , shots=10_00 ) return job.result().get_counts(snake_case__ ) if __name__ == "__main__": print(F"""Total count for various states are: {quantum_entanglement(3)}""")
291
"""simple docstring""" import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase : int = logging.get_logger(__name__) lowerCAmelCase : List[str] = { """asapp/sew-d-tiny-100k""": """https://huggingface.co/asapp/sew-d-tiny-100k/resolve/main/config.json""", # See all SEW-D models at https://huggingface.co/models?filter=sew-d } class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' __UpperCamelCase = "sew-d" def __init__( self , _a=32 , _a=768 , _a=12 , _a=12 , _a=3_072 , _a=2 , _a=512 , _a=256 , _a=True , _a=True , _a=("p2c", "c2p") , _a="layer_norm" , _a="gelu_python" , _a=0.1 , _a=0.1 , _a=0.1 , _a=0.0 , _a=0.1 , _a=0.02 , _a=1e-7 , _a=1e-5 , _a="group" , _a="gelu" , _a=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512) , _a=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1) , _a=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1) , _a=False , _a=128 , _a=16 , _a=True , _a=0.05 , _a=10 , _a=2 , _a=0.0 , _a=10 , _a=0 , _a="mean" , _a=False , _a=False , _a=256 , _a=0 , _a=1 , _a=2 , **_a , ): """simple docstring""" super().__init__(**_a , pad_token_id=_a , bos_token_id=_a , eos_token_id=_a ) lowerCamelCase = hidden_size lowerCamelCase = feat_extract_norm lowerCamelCase = feat_extract_activation lowerCamelCase = list(_a ) lowerCamelCase = list(_a ) lowerCamelCase = list(_a ) lowerCamelCase = conv_bias lowerCamelCase = num_conv_pos_embeddings lowerCamelCase = num_conv_pos_embedding_groups lowerCamelCase = len(self.conv_dim ) lowerCamelCase = num_hidden_layers lowerCamelCase = intermediate_size lowerCamelCase = squeeze_factor lowerCamelCase = max_position_embeddings lowerCamelCase = position_buckets lowerCamelCase = share_att_key lowerCamelCase = relative_attention lowerCamelCase = norm_rel_ebd lowerCamelCase = list(_a ) lowerCamelCase = hidden_act lowerCamelCase = num_attention_heads lowerCamelCase = hidden_dropout lowerCamelCase = attention_dropout lowerCamelCase = activation_dropout lowerCamelCase = feat_proj_dropout lowerCamelCase = final_dropout lowerCamelCase = layer_norm_eps lowerCamelCase = feature_layer_norm_eps lowerCamelCase = initializer_range lowerCamelCase = vocab_size if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( """Configuration for convolutional layers is incorrect.""" """It is required that `len(config.conv_dim)` == `len(config.conv_stride)` == `len(config.conv_kernel)`,""" f'but is `len(config.conv_dim) = {len(self.conv_dim )}`, `len(config.conv_stride)' f'= {len(self.conv_stride )}`, `len(config.conv_kernel) = {len(self.conv_kernel )}`.' ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 lowerCamelCase = apply_spec_augment lowerCamelCase = mask_time_prob lowerCamelCase = mask_time_length lowerCamelCase = mask_time_min_masks lowerCamelCase = mask_feature_prob lowerCamelCase = mask_feature_length lowerCamelCase = mask_feature_min_masks # ctc loss lowerCamelCase = ctc_loss_reduction lowerCamelCase = ctc_zero_infinity # sequence classification lowerCamelCase = use_weighted_layer_sum lowerCamelCase = classifier_proj_size @property def _lowerCAmelCase ( self ): """simple docstring""" return functools.reduce(operator.mul , self.conv_stride , 1 )
291
1
"""simple docstring""" def a__ ( snake_case__ ) -> Tuple: lowerCamelCase = len(snake_case__ ) for i in range(length - 1 ): lowerCamelCase = i for k in range(i + 1 , snake_case__ ): if collection[k] < collection[least]: lowerCamelCase = k if least != i: lowerCamelCase , lowerCamelCase = (collection[i], collection[least]) return collection if __name__ == "__main__": lowerCAmelCase : Optional[Any] = input("""Enter numbers separated by a comma:\n""").strip() lowerCAmelCase : str = [int(item) for item in user_input.split(""",""")] print(selection_sort(unsorted))
291
"""simple docstring""" from sklearn.metrics import recall_score import datasets lowerCAmelCase : Any = """ Recall is the fraction of the positive examples that were correctly labeled by the model as positive. It can be computed with the equation: Recall = TP / (TP + FN) Where TP is the true positives and FN is the false negatives. """ lowerCAmelCase : Any = """ Args: - **predictions** (`list` of `int`): The predicted labels. - **references** (`list` of `int`): The ground truth labels. - **labels** (`list` of `int`): The set of labels to include when `average` is not set to `binary`, and their order when average is `None`. Labels present in the data can be excluded in this input, for example to calculate a multiclass average ignoring a majority negative class, while labels not present in the data will result in 0 components in a macro average. For multilabel targets, labels are column indices. By default, all labels in y_true and y_pred are used in sorted order. Defaults to None. - **pos_label** (`int`): The class label to use as the 'positive class' when calculating the recall. Defaults to `1`. - **average** (`string`): This parameter is required for multiclass/multilabel targets. If None, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data. Defaults to `'binary'`. - `'binary'`: Only report results for the class specified by `pos_label`. This is applicable only if the target labels and predictions are binary. - `'micro'`: Calculate metrics globally by counting the total true positives, false negatives, and false positives. - `'macro'`: Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account. - `'weighted'`: Calculate metrics for each label, and find their average weighted by support (the number of true instances for each label). This alters `'macro'` to account for label imbalance. Note that it can result in an F-score that is not between precision and recall. - `'samples'`: Calculate metrics for each instance, and find their average (only meaningful for multilabel classification). - **sample_weight** (`list` of `float`): Sample weights Defaults to `None`. - **zero_division** (): Sets the value to return when there is a zero division. Defaults to . - `'warn'`: If there is a zero division, the return value is `0`, but warnings are also raised. - `0`: If there is a zero division, the return value is `0`. - `1`: If there is a zero division, the return value is `1`. Returns: - **recall** (`float`, or `array` of `float`): Either the general recall score, or the recall scores for individual classes, depending on the values input to `labels` and `average`. Minimum possible value is 0. Maximum possible value is 1. A higher recall means that more of the positive examples have been labeled correctly. Therefore, a higher recall is generally considered better. Examples: Example 1-A simple example with some errors >>> recall_metric = datasets.load_metric('recall') >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1]) >>> print(results) {'recall': 0.6666666666666666} Example 2-The same example as Example 1, but with `pos_label=0` instead of the default `pos_label=1`. >>> recall_metric = datasets.load_metric('recall') >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1], pos_label=0) >>> print(results) {'recall': 0.5} Example 3-The same example as Example 1, but with `sample_weight` included. >>> recall_metric = datasets.load_metric('recall') >>> sample_weight = [0.9, 0.2, 0.9, 0.3, 0.8] >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1], sample_weight=sample_weight) >>> print(results) {'recall': 0.55} Example 4-A multiclass example, using different averages. >>> recall_metric = datasets.load_metric('recall') >>> predictions = [0, 2, 1, 0, 0, 1] >>> references = [0, 1, 2, 0, 1, 2] >>> results = recall_metric.compute(predictions=predictions, references=references, average='macro') >>> print(results) {'recall': 0.3333333333333333} >>> results = recall_metric.compute(predictions=predictions, references=references, average='micro') >>> print(results) {'recall': 0.3333333333333333} >>> results = recall_metric.compute(predictions=predictions, references=references, average='weighted') >>> print(results) {'recall': 0.3333333333333333} >>> results = recall_metric.compute(predictions=predictions, references=references, average=None) >>> print(results) {'recall': array([1., 0., 0.])} """ lowerCAmelCase : Any = """ @article{scikit-learn, title={Scikit-learn: Machine Learning in {P}ython}, author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V. and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P. and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.}, journal={Journal of Machine Learning Research}, volume={12}, pages={2825--2830}, year={2011} """ @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __magic_name__ ( datasets.Metric ): '''simple docstring''' def _lowerCAmelCase ( self ): """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": datasets.Sequence(datasets.Value("""int32""" ) ), """references""": datasets.Sequence(datasets.Value("""int32""" ) ), } if self.config_name == """multilabel""" else { """predictions""": datasets.Value("""int32""" ), """references""": datasets.Value("""int32""" ), } ) , reference_urls=["""https://scikit-learn.org/stable/modules/generated/sklearn.metrics.recall_score.html"""] , ) def _lowerCAmelCase ( self , _a , _a , _a=None , _a=1 , _a="binary" , _a=None , _a="warn" , ): """simple docstring""" lowerCamelCase = recall_score( _a , _a , labels=_a , pos_label=_a , average=_a , sample_weight=_a , zero_division=_a , ) return {"recall": float(_a ) if score.size == 1 else score}
291
1
"""simple docstring""" import gc import random import unittest import numpy as np import torch from PIL import Image from diffusers import ( DDIMScheduler, KandinskyVaaControlnetImgaImgPipeline, KandinskyVaaPriorEmbaEmbPipeline, UNetaDConditionModel, VQModel, ) from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference enable_full_determinism() class __magic_name__ ( UpperCAmelCase__ , unittest.TestCase ): '''simple docstring''' __UpperCamelCase = KandinskyVaaControlnetImgaImgPipeline __UpperCamelCase = ["image_embeds", "negative_image_embeds", "image", "hint"] __UpperCamelCase = ["image_embeds", "negative_image_embeds", "image", "hint"] __UpperCamelCase = [ "generator", "height", "width", "strength", "guidance_scale", "num_inference_steps", "return_dict", "guidance_scale", "num_images_per_prompt", "output_type", "return_dict", ] __UpperCamelCase = False @property def _lowerCAmelCase ( self ): """simple docstring""" return 32 @property def _lowerCAmelCase ( self ): """simple docstring""" return 32 @property def _lowerCAmelCase ( self ): """simple docstring""" return self.time_input_dim @property def _lowerCAmelCase ( self ): """simple docstring""" return self.time_input_dim * 4 @property def _lowerCAmelCase ( self ): """simple docstring""" return 100 @property def _lowerCAmelCase ( self ): """simple docstring""" torch.manual_seed(0 ) lowerCamelCase = { """in_channels""": 8, # Out channels is double in channels because predicts mean and variance """out_channels""": 8, """addition_embed_type""": """image_hint""", """down_block_types""": ("""ResnetDownsampleBlock2D""", """SimpleCrossAttnDownBlock2D"""), """up_block_types""": ("""SimpleCrossAttnUpBlock2D""", """ResnetUpsampleBlock2D"""), """mid_block_type""": """UNetMidBlock2DSimpleCrossAttn""", """block_out_channels""": (self.block_out_channels_a, self.block_out_channels_a * 2), """layers_per_block""": 1, """encoder_hid_dim""": self.text_embedder_hidden_size, """encoder_hid_dim_type""": """image_proj""", """cross_attention_dim""": self.cross_attention_dim, """attention_head_dim""": 4, """resnet_time_scale_shift""": """scale_shift""", """class_embed_type""": None, } lowerCamelCase = UNetaDConditionModel(**_a ) return model @property def _lowerCAmelCase ( self ): """simple docstring""" return { "block_out_channels": [32, 32, 64, 64], "down_block_types": [ "DownEncoderBlock2D", "DownEncoderBlock2D", "DownEncoderBlock2D", "AttnDownEncoderBlock2D", ], "in_channels": 3, "latent_channels": 4, "layers_per_block": 1, "norm_num_groups": 8, "norm_type": "spatial", "num_vq_embeddings": 12, "out_channels": 3, "up_block_types": ["AttnUpDecoderBlock2D", "UpDecoderBlock2D", "UpDecoderBlock2D", "UpDecoderBlock2D"], "vq_embed_dim": 4, } @property def _lowerCAmelCase ( self ): """simple docstring""" torch.manual_seed(0 ) lowerCamelCase = VQModel(**self.dummy_movq_kwargs ) return model def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.dummy_unet lowerCamelCase = self.dummy_movq lowerCamelCase = { """num_train_timesteps""": 1_000, """beta_schedule""": """linear""", """beta_start""": 0.00_085, """beta_end""": 0.012, """clip_sample""": False, """set_alpha_to_one""": False, """steps_offset""": 0, """prediction_type""": """epsilon""", """thresholding""": False, } lowerCamelCase = DDIMScheduler(**_a ) lowerCamelCase = { """unet""": unet, """scheduler""": scheduler, """movq""": movq, } return components def _lowerCAmelCase ( self , _a , _a=0 ): """simple docstring""" lowerCamelCase = floats_tensor((1, self.text_embedder_hidden_size) , rng=random.Random(_a ) ).to(_a ) lowerCamelCase = floats_tensor((1, self.text_embedder_hidden_size) , rng=random.Random(seed + 1 ) ).to( _a ) # create init_image lowerCamelCase = floats_tensor((1, 3, 64, 64) , rng=random.Random(_a ) ).to(_a ) lowerCamelCase = image.cpu().permute(0 , 2 , 3 , 1 )[0] lowerCamelCase = Image.fromarray(np.uinta(_a ) ).convert("""RGB""" ).resize((256, 256) ) # create hint lowerCamelCase = floats_tensor((1, 3, 64, 64) , rng=random.Random(_a ) ).to(_a ) if str(_a ).startswith("""mps""" ): lowerCamelCase = torch.manual_seed(_a ) else: lowerCamelCase = torch.Generator(device=_a ).manual_seed(_a ) lowerCamelCase = { """image""": init_image, """image_embeds""": image_embeds, """negative_image_embeds""": negative_image_embeds, """hint""": hint, """generator""": generator, """height""": 64, """width""": 64, """num_inference_steps""": 10, """guidance_scale""": 7.0, """strength""": 0.2, """output_type""": """np""", } return inputs def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = """cpu""" lowerCamelCase = self.get_dummy_components() lowerCamelCase = self.pipeline_class(**_a ) lowerCamelCase = pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) lowerCamelCase = pipe(**self.get_dummy_inputs(_a ) ) lowerCamelCase = output.images lowerCamelCase = pipe( **self.get_dummy_inputs(_a ) , return_dict=_a , )[0] lowerCamelCase = image[0, -3:, -3:, -1] lowerCamelCase = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) lowerCamelCase = np.array( [0.54_985_034, 0.55_509_365, 0.52_561_504, 0.5_570_494, 0.5_593_818, 0.5_263_979, 0.50_285_643, 0.5_069_846, 0.51_196_736] ) assert ( np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 ), f' expected_slice {expected_slice}, but got {image_slice.flatten()}' assert ( np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 ), f' expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}' @slow @require_torch_gpu class __magic_name__ ( unittest.TestCase ): '''simple docstring''' def _lowerCAmelCase ( self ): """simple docstring""" # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/kandinskyv22/kandinskyv22_controlnet_img2img_robotcat_fp16.npy""" ) lowerCamelCase = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/kandinsky/cat.png""" ) lowerCamelCase = init_image.resize((512, 512) ) lowerCamelCase = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/kandinskyv22/hint_image_cat.png""" ) lowerCamelCase = torch.from_numpy(np.array(_a ) ).float() / 255.0 lowerCamelCase = hint.permute(2 , 0 , 1 ).unsqueeze(0 ) lowerCamelCase = """A robot, 4k photo""" lowerCamelCase = KandinskyVaaPriorEmbaEmbPipeline.from_pretrained( """kandinsky-community/kandinsky-2-2-prior""" , torch_dtype=torch.floataa ) pipe_prior.to(_a ) lowerCamelCase = KandinskyVaaControlnetImgaImgPipeline.from_pretrained( """kandinsky-community/kandinsky-2-2-controlnet-depth""" , torch_dtype=torch.floataa ) lowerCamelCase = pipeline.to(_a ) pipeline.set_progress_bar_config(disable=_a ) lowerCamelCase = torch.Generator(device="""cpu""" ).manual_seed(0 ) lowerCamelCase , lowerCamelCase = pipe_prior( _a , image=_a , strength=0.85 , generator=_a , negative_prompt="""""" , ).to_tuple() lowerCamelCase = pipeline( image=_a , image_embeds=_a , negative_image_embeds=_a , hint=_a , generator=_a , num_inference_steps=100 , height=512 , width=512 , strength=0.5 , output_type="""np""" , ) lowerCamelCase = output.images[0] assert image.shape == (512, 512, 3) assert_mean_pixel_difference(_a , _a )
291
"""simple docstring""" import numpy as np import torch from torch.utils.data import Dataset, IterableDataset from ..utils.generic import ModelOutput class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' def __init__( self , _a , _a , _a ): """simple docstring""" lowerCamelCase = dataset lowerCamelCase = process lowerCamelCase = params def __len__( self ): """simple docstring""" return len(self.dataset ) def __getitem__( self , _a ): """simple docstring""" lowerCamelCase = self.dataset[i] lowerCamelCase = self.process(_a , **self.params ) return processed class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' def __init__( self , _a , _a , _a , _a=None ): """simple docstring""" lowerCamelCase = loader lowerCamelCase = infer lowerCamelCase = params if loader_batch_size == 1: # Let's spare some time by deactivating altogether lowerCamelCase = None lowerCamelCase = loader_batch_size # Internal bookkeeping lowerCamelCase = None lowerCamelCase = None def __len__( self ): """simple docstring""" return len(self.loader ) def __iter__( self ): """simple docstring""" lowerCamelCase = iter(self.loader ) return self def _lowerCAmelCase ( self ): """simple docstring""" if isinstance(self._loader_batch_data , torch.Tensor ): # Batch data is simple tensor, just fetch the slice lowerCamelCase = self._loader_batch_data[self._loader_batch_index] else: # Batch data is assumed to be BaseModelOutput (or dict) lowerCamelCase = {} for k, element in self._loader_batch_data.items(): if isinstance(_a , _a ): # Convert ModelOutput to tuple first lowerCamelCase = element.to_tuple() if isinstance(element[0] , torch.Tensor ): lowerCamelCase = tuple(el[self._loader_batch_index].unsqueeze(0 ) for el in element ) elif isinstance(element[0] , np.ndarray ): lowerCamelCase = tuple(np.expand_dims(el[self._loader_batch_index] , 0 ) for el in element ) continue if k in {"hidden_states", "past_key_values", "attentions"} and isinstance(_a , _a ): # Those are stored as lists of tensors so need specific unbatching. if isinstance(element[0] , torch.Tensor ): lowerCamelCase = tuple(el[self._loader_batch_index].unsqueeze(0 ) for el in element ) elif isinstance(element[0] , np.ndarray ): lowerCamelCase = tuple(np.expand_dims(el[self._loader_batch_index] , 0 ) for el in element ) continue if element is None: # This can happen for optional data that get passed around lowerCamelCase = None elif isinstance(element[self._loader_batch_index] , torch.Tensor ): # Take correct batch data, but make it looked like batch_size=1 # For compatibility with other methods within transformers lowerCamelCase = element[self._loader_batch_index].unsqueeze(0 ) elif isinstance(element[self._loader_batch_index] , np.ndarray ): # Take correct batch data, but make it looked like batch_size=1 # For compatibility with other methods within transformers lowerCamelCase = np.expand_dims(element[self._loader_batch_index] , 0 ) else: # This is typically a list, so no need to `unsqueeze`. lowerCamelCase = element[self._loader_batch_index] # Recreate the element by reusing the original class to make it look # batch_size=1 lowerCamelCase = self._loader_batch_data.__class__(_a ) self._loader_batch_index += 1 return result def _lowerCAmelCase ( self ): """simple docstring""" if self._loader_batch_index is not None and self._loader_batch_index < self.loader_batch_size: # We are currently unrolling a batch so we just need to return # the current item within a batch return self.loader_batch_item() # We're out of items within a batch lowerCamelCase = next(self.iterator ) lowerCamelCase = self.infer(_a , **self.params ) # We now have a batch of "inferred things". if self.loader_batch_size is not None: # Try to infer the size of the batch if isinstance(_a , torch.Tensor ): lowerCamelCase = processed else: lowerCamelCase = list(processed.keys() )[0] lowerCamelCase = processed[key] if isinstance(_a , _a ): lowerCamelCase = len(_a ) else: lowerCamelCase = first_tensor.shape[0] if 0 < observed_batch_size < self.loader_batch_size: # could be last batch so we can't unroll as many # elements. lowerCamelCase = observed_batch_size # Setting internal index to unwrap the batch lowerCamelCase = processed lowerCamelCase = 0 return self.loader_batch_item() else: # We're not unrolling batches return processed class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' def __init__( self , _a , _a , _a , _a=None ): """simple docstring""" super().__init__(_a , _a , _a ) def __iter__( self ): """simple docstring""" lowerCamelCase = iter(self.loader ) lowerCamelCase = None return self def _lowerCAmelCase ( self ): """simple docstring""" if self.subiterator is None: lowerCamelCase = self.infer(next(self.iterator ) , **self.params ) try: # Try to return next item lowerCamelCase = next(self.subiterator ) except StopIteration: # When a preprocess iterator ends, we can start lookig at the next item # ChunkIterator will keep feeding until ALL elements of iterator # all have created their subiterator and have been iterating against. # # Another way to look at it, is we're basically flattening lists of lists # into a single list, but with generators lowerCamelCase = self.infer(next(self.iterator ) , **self.params ) lowerCamelCase = next(self.subiterator ) return processed class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' def __iter__( self ): """simple docstring""" lowerCamelCase = iter(self.loader ) return self def _lowerCAmelCase ( self ): """simple docstring""" # Extremely similar to PipelineIterator in its unpacking mechanism # BUT, we have an extra required item which is the presence of `is_last` # That is because everything is flattened by `PipelineChunkIterator` we # need to keep track of how to regroup here in the original `process` # boundaries so that `process` and `postprocess` see the same data. # This iterator accumulates items (possibly while unbatching) until it # its a `is_last` and then just passes it on to the caller. lowerCamelCase = False lowerCamelCase = [] if self._loader_batch_index is not None and self._loader_batch_index < self.loader_batch_size: while self._loader_batch_index < self.loader_batch_size: lowerCamelCase = self.loader_batch_item() lowerCamelCase = item.pop("""is_last""" ) accumulator.append(_a ) if is_last: return accumulator while not is_last: lowerCamelCase = self.infer(next(self.iterator ) , **self.params ) if self.loader_batch_size is not None: if isinstance(_a , torch.Tensor ): lowerCamelCase = processed else: lowerCamelCase = list(processed.keys() )[0] lowerCamelCase = processed[key] if isinstance(_a , _a ): lowerCamelCase = len(_a ) else: lowerCamelCase = first_tensor.shape[0] if 0 < observed_batch_size < self.loader_batch_size: # could be last batch so we can't unroll as many # elements. lowerCamelCase = observed_batch_size lowerCamelCase = processed lowerCamelCase = 0 while self._loader_batch_index < self.loader_batch_size: lowerCamelCase = self.loader_batch_item() lowerCamelCase = item.pop("""is_last""" ) accumulator.append(_a ) if is_last: return accumulator else: lowerCamelCase = processed lowerCamelCase = item.pop("""is_last""" ) accumulator.append(_a ) return accumulator class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' def __init__( self , _a , _a ): """simple docstring""" lowerCamelCase = dataset lowerCamelCase = key def __len__( self ): """simple docstring""" return len(self.dataset ) def __getitem__( self , _a ): """simple docstring""" return self.dataset[i][self.key] class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' def __init__( self , _a , _a , _a ): """simple docstring""" lowerCamelCase = dataset lowerCamelCase = keya lowerCamelCase = keya def __len__( self ): """simple docstring""" return len(self.dataset ) def __getitem__( self , _a ): """simple docstring""" return {"text": self.dataset[i][self.keya], "text_pair": self.dataset[i][self.keya]}
291
1
"""simple docstring""" import copy import inspect import unittest import numpy as np from huggingface_hub import hf_hub_download from transformers import VideoMAEConfig from transformers.models.auto import get_values from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ( MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING, VideoMAEForPreTraining, VideoMAEForVideoClassification, VideoMAEModel, ) from transformers.models.videomae.modeling_videomae import VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from transformers import VideoMAEImageProcessor class __magic_name__ : '''simple docstring''' def __init__( self , _a , _a=13 , _a=10 , _a=3 , _a=2 , _a=2 , _a=2 , _a=True , _a=True , _a=32 , _a=5 , _a=4 , _a=37 , _a="gelu" , _a=0.1 , _a=0.1 , _a=10 , _a=0.02 , _a=0.9 , _a=None , ): """simple docstring""" lowerCamelCase = parent lowerCamelCase = batch_size lowerCamelCase = image_size lowerCamelCase = num_channels lowerCamelCase = patch_size lowerCamelCase = tubelet_size lowerCamelCase = num_frames lowerCamelCase = is_training lowerCamelCase = use_labels lowerCamelCase = hidden_size lowerCamelCase = num_hidden_layers lowerCamelCase = num_attention_heads lowerCamelCase = intermediate_size lowerCamelCase = hidden_act lowerCamelCase = hidden_dropout_prob lowerCamelCase = attention_probs_dropout_prob lowerCamelCase = type_sequence_label_size lowerCamelCase = initializer_range lowerCamelCase = mask_ratio lowerCamelCase = scope # in VideoMAE, the number of tokens equals num_frames/tubelet_size * num_patches per frame lowerCamelCase = (image_size // patch_size) ** 2 lowerCamelCase = (num_frames // tubelet_size) * self.num_patches_per_frame # use this variable to define bool_masked_pos lowerCamelCase = int(mask_ratio * self.seq_length ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = floats_tensor( [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size] ) lowerCamelCase = None if self.use_labels: lowerCamelCase = ids_tensor([self.batch_size] , self.type_sequence_label_size ) lowerCamelCase = self.get_config() return config, pixel_values, labels def _lowerCAmelCase ( self ): """simple docstring""" return VideoMAEConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , num_frames=self.num_frames , tubelet_size=self.tubelet_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=_a , initializer_range=self.initializer_range , ) def _lowerCAmelCase ( self , _a , _a , _a ): """simple docstring""" lowerCamelCase = VideoMAEModel(config=_a ) model.to(_a ) model.eval() lowerCamelCase = model(_a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _lowerCAmelCase ( self , _a , _a , _a ): """simple docstring""" lowerCamelCase = VideoMAEForPreTraining(_a ) model.to(_a ) model.eval() # important: each video needs to have the same number of masked patches # hence we define a single mask, which we then repeat for each example in the batch lowerCamelCase = torch.ones((self.num_masks,) ) lowerCamelCase = torch.cat([mask, torch.zeros(self.seq_length - mask.size(0 ) )] ) lowerCamelCase = mask.expand(self.batch_size , -1 ).bool() lowerCamelCase = model(_a , _a ) # model only returns predictions for masked patches lowerCamelCase = mask.sum().item() lowerCamelCase = 3 * self.tubelet_size * self.patch_size**2 self.parent.assertEqual(result.logits.shape , (self.batch_size, num_masked_patches, decoder_num_labels) ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.prepare_config_and_inputs() lowerCamelCase , lowerCamelCase , lowerCamelCase = config_and_inputs lowerCamelCase = {"""pixel_values""": pixel_values} return config, inputs_dict @require_torch class __magic_name__ ( UpperCAmelCase__ , UpperCAmelCase__ , unittest.TestCase ): '''simple docstring''' __UpperCamelCase = ( (VideoMAEModel, VideoMAEForPreTraining, VideoMAEForVideoClassification) if is_torch_available() else () ) __UpperCamelCase = ( {"feature-extraction": VideoMAEModel, "video-classification": VideoMAEForVideoClassification} if is_torch_available() else {} ) __UpperCamelCase = False __UpperCamelCase = False __UpperCamelCase = False __UpperCamelCase = False def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = VideoMAEModelTester(self ) lowerCamelCase = ConfigTester(self , config_class=_a , has_text_modality=_a , hidden_size=37 ) def _lowerCAmelCase ( self , _a , _a , _a=False ): """simple docstring""" lowerCamelCase = copy.deepcopy(_a ) if model_class == VideoMAEForPreTraining: # important: each video needs to have the same number of masked patches # hence we define a single mask, which we then repeat for each example in the batch lowerCamelCase = torch.ones((self.model_tester.num_masks,) ) lowerCamelCase = torch.cat([mask, torch.zeros(self.model_tester.seq_length - mask.size(0 ) )] ) lowerCamelCase = mask.expand(self.model_tester.batch_size , -1 ).bool() lowerCamelCase = bool_masked_pos.to(_a ) if return_labels: if model_class in [ *get_values(_a ), ]: lowerCamelCase = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=_a ) return inputs_dict def _lowerCAmelCase ( self ): """simple docstring""" self.config_tester.run_common_tests() @unittest.skip(reason="""VideoMAE does not use inputs_embeds""" ) def _lowerCAmelCase ( self ): """simple docstring""" pass def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase , lowerCamelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCamelCase = model_class(_a ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) lowerCamelCase = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_a , nn.Linear ) ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase , lowerCamelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCamelCase = model_class(_a ) lowerCamelCase = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic lowerCamelCase = [*signature.parameters.keys()] lowerCamelCase = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , _a ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_a ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_pretraining(*_a ) @slow def _lowerCAmelCase ( self ): """simple docstring""" for model_name in VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowerCamelCase = VideoMAEModel.from_pretrained(_a ) self.assertIsNotNone(_a ) def _lowerCAmelCase ( self ): """simple docstring""" if not self.has_attentions: pass else: lowerCamelCase , lowerCamelCase = self.model_tester.prepare_config_and_inputs_for_common() lowerCamelCase = True for model_class in self.all_model_classes: lowerCamelCase = self.model_tester.seq_length - self.model_tester.num_masks lowerCamelCase = ( num_visible_patches if model_class == VideoMAEForPreTraining else self.model_tester.seq_length ) lowerCamelCase = True lowerCamelCase = False lowerCamelCase = True lowerCamelCase = model_class(_a ) model.to(_a ) model.eval() with torch.no_grad(): lowerCamelCase = model(**self._prepare_for_class(_a , _a ) ) lowerCamelCase = outputs.attentions self.assertEqual(len(_a ) , self.model_tester.num_hidden_layers ) # check that output_attentions also work using config del inputs_dict["output_attentions"] lowerCamelCase = True lowerCamelCase = model_class(_a ) model.to(_a ) model.eval() with torch.no_grad(): lowerCamelCase = model(**self._prepare_for_class(_a , _a ) ) lowerCamelCase = outputs.attentions self.assertEqual(len(_a ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, seq_len, seq_len] , ) lowerCamelCase = len(_a ) # Check attention is always last and order is fine lowerCamelCase = True lowerCamelCase = True lowerCamelCase = model_class(_a ) model.to(_a ) model.eval() with torch.no_grad(): lowerCamelCase = model(**self._prepare_for_class(_a , _a ) ) self.assertEqual(out_len + 1 , len(_a ) ) lowerCamelCase = outputs.attentions self.assertEqual(len(_a ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(self_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, seq_len, seq_len] , ) def _lowerCAmelCase ( self ): """simple docstring""" def check_hidden_states_output(_a , _a , _a ): lowerCamelCase = model_class(_a ) model.to(_a ) model.eval() with torch.no_grad(): lowerCamelCase = model(**self._prepare_for_class(_a , _a ) ) lowerCamelCase = outputs.hidden_states lowerCamelCase = self.model_tester.num_hidden_layers + 1 self.assertEqual(len(_a ) , _a ) lowerCamelCase = self.model_tester.seq_length - self.model_tester.num_masks lowerCamelCase = num_visible_patches if model_class == VideoMAEForPreTraining else self.model_tester.seq_length self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [seq_length, self.model_tester.hidden_size] , ) lowerCamelCase , lowerCamelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCamelCase = True check_hidden_states_output(_a , _a , _a ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] lowerCamelCase = True check_hidden_states_output(_a , _a , _a ) @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests.""" ) def _lowerCAmelCase ( self ): """simple docstring""" pass def a__ ( ) -> Dict: lowerCamelCase = hf_hub_download( repo_id="""hf-internal-testing/spaghetti-video""" , filename="""eating_spaghetti.npy""" , repo_type="""dataset""" ) lowerCamelCase = np.load(snake_case__ ) return list(snake_case__ ) @require_torch @require_vision class __magic_name__ ( unittest.TestCase ): '''simple docstring''' @cached_property def _lowerCAmelCase ( self ): """simple docstring""" # logits were tested with a different mean and std, so we use the same here return ( VideoMAEImageProcessor(image_mean=[0.5, 0.5, 0.5] , image_std=[0.5, 0.5, 0.5] ) if is_vision_available() else None ) @slow def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = VideoMAEForVideoClassification.from_pretrained("""MCG-NJU/videomae-base-finetuned-kinetics""" ).to( _a ) lowerCamelCase = self.default_image_processor lowerCamelCase = prepare_video() lowerCamelCase = image_processor(_a , return_tensors="""pt""" ).to(_a ) # forward pass with torch.no_grad(): lowerCamelCase = model(**_a ) # verify the logits lowerCamelCase = torch.Size((1, 400) ) self.assertEqual(outputs.logits.shape , _a ) lowerCamelCase = torch.tensor([0.3_669, -0.0_688, -0.2_421] ).to(_a ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , _a , atol=1e-4 ) ) @slow def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = VideoMAEForPreTraining.from_pretrained("""MCG-NJU/videomae-base-short""" ).to(_a ) lowerCamelCase = self.default_image_processor lowerCamelCase = prepare_video() lowerCamelCase = image_processor(_a , return_tensors="""pt""" ).to(_a ) # add boolean mask, indicating which patches to mask lowerCamelCase = hf_hub_download(repo_id="""hf-internal-testing/bool-masked-pos""" , filename="""bool_masked_pos.pt""" ) lowerCamelCase = torch.load(_a ) # forward pass with torch.no_grad(): lowerCamelCase = model(**_a ) # verify the logits lowerCamelCase = torch.Size([1, 1_408, 1_536] ) lowerCamelCase = torch.tensor( [[0.7_994, 0.9_612, 0.8_508], [0.7_401, 0.8_958, 0.8_302], [0.5_862, 0.7_468, 0.7_325]] , device=_a ) self.assertEqual(outputs.logits.shape , _a ) self.assertTrue(torch.allclose(outputs.logits[0, :3, :3] , _a , atol=1e-4 ) ) # verify the loss (`config.norm_pix_loss` = `True`) lowerCamelCase = torch.tensor([0.5_142] , device=_a ) self.assertTrue(torch.allclose(outputs.loss , _a , atol=1e-4 ) ) # verify the loss (`config.norm_pix_loss` = `False`) lowerCamelCase = VideoMAEForPreTraining.from_pretrained("""MCG-NJU/videomae-base-short""" , norm_pix_loss=_a ).to( _a ) with torch.no_grad(): lowerCamelCase = model(**_a ) lowerCamelCase = torch.tensor(torch.tensor([0.6_469] ) , device=_a ) self.assertTrue(torch.allclose(outputs.loss , _a , atol=1e-4 ) )
291
"""simple docstring""" def a__ ( snake_case__ ) -> bool: lowerCamelCase = (1 + 24 * n) ** 0.5 return ((1 + root) / 6) % 1 == 0 def a__ ( snake_case__ = 50_00 ) -> int: lowerCamelCase = [(i * (3 * i - 1)) // 2 for i in range(1 , snake_case__ )] for i, pentagonal_i in enumerate(snake_case__ ): for j in range(snake_case__ , len(snake_case__ ) ): lowerCamelCase = pentagonal_nums[j] lowerCamelCase = pentagonal_i + pentagonal_j lowerCamelCase = pentagonal_j - pentagonal_i if is_pentagonal(snake_case__ ) and is_pentagonal(snake_case__ ): return b return -1 if __name__ == "__main__": print(F"""{solution() = }""")
291
1
"""simple docstring""" import json import os from pathlib import Path from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple, Union import sentencepiece from ...tokenization_utils import BatchEncoding, PreTrainedTokenizer from ...utils import logging lowerCAmelCase : Tuple = logging.get_logger(__name__) lowerCAmelCase : Any = """▁""" lowerCAmelCase : List[Any] = { """vocab_file""": """vocab.json""", """spm_file""": """sentencepiece.bpe.model""", """tokenizer_config_file""": """tokenizer_config.json""", } lowerCAmelCase : List[str] = { """vocab_file""": { """facebook/m2m100_418M""": """https://huggingface.co/facebook/m2m100_418M/resolve/main/vocab.json""", """facebook/m2m100_1.2B""": """https://huggingface.co/facebook/m2m100_1.2B/resolve/main/vocab.json""", }, """spm_file""": { """facebook/m2m100_418M""": """https://huggingface.co/facebook/m2m100_418M/resolve/main/sentencepiece.bpe.model""", """facebook/m2m100_1.2B""": """https://huggingface.co/facebook/m2m100_1.2B/resolve/main/sentencepiece.bpe.model""", }, """tokenizer_config_file""": { """facebook/m2m100_418M""": """https://huggingface.co/facebook/m2m100_418M/resolve/main/tokenizer_config.json""", """facebook/m2m100_1.2B""": """https://huggingface.co/facebook/m2m100_1.2B/resolve/main/tokenizer_config.json""", }, } lowerCAmelCase : Optional[Any] = { """facebook/m2m100_418M""": 1024, } # fmt: off lowerCAmelCase : str = { """m2m100""": ["""af""", """am""", """ar""", """ast""", """az""", """ba""", """be""", """bg""", """bn""", """br""", """bs""", """ca""", """ceb""", """cs""", """cy""", """da""", """de""", """el""", """en""", """es""", """et""", """fa""", """ff""", """fi""", """fr""", """fy""", """ga""", """gd""", """gl""", """gu""", """ha""", """he""", """hi""", """hr""", """ht""", """hu""", """hy""", """id""", """ig""", """ilo""", """is""", """it""", """ja""", """jv""", """ka""", """kk""", """km""", """kn""", """ko""", """lb""", """lg""", """ln""", """lo""", """lt""", """lv""", """mg""", """mk""", """ml""", """mn""", """mr""", """ms""", """my""", """ne""", """nl""", """no""", """ns""", """oc""", """or""", """pa""", """pl""", """ps""", """pt""", """ro""", """ru""", """sd""", """si""", """sk""", """sl""", """so""", """sq""", """sr""", """ss""", """su""", """sv""", """sw""", """ta""", """th""", """tl""", """tn""", """tr""", """uk""", """ur""", """uz""", """vi""", """wo""", """xh""", """yi""", """yo""", """zh""", """zu"""], """wmt21""": ["""en""", """ha""", """is""", """ja""", """cs""", """ru""", """zh""", """de"""] } class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' __UpperCamelCase = VOCAB_FILES_NAMES __UpperCamelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCamelCase = PRETRAINED_VOCAB_FILES_MAP __UpperCamelCase = ["input_ids", "attention_mask"] __UpperCamelCase = [] __UpperCamelCase = [] def __init__( self , _a , _a , _a=None , _a=None , _a="<s>" , _a="</s>" , _a="</s>" , _a="<pad>" , _a="<unk>" , _a="m2m100" , _a = None , _a=8 , **_a , ): """simple docstring""" lowerCamelCase = {} if sp_model_kwargs is None else sp_model_kwargs lowerCamelCase = language_codes lowerCamelCase = FAIRSEQ_LANGUAGE_CODES[language_codes] lowerCamelCase = {lang_code: f'__{lang_code}__' for lang_code in fairseq_language_code} lowerCamelCase = kwargs.get("""additional_special_tokens""" , [] ) kwargs["additional_special_tokens"] += [ self.get_lang_token(_a ) for lang_code in fairseq_language_code if self.get_lang_token(_a ) not in kwargs["additional_special_tokens"] ] super().__init__( src_lang=_a , tgt_lang=_a , bos_token=_a , eos_token=_a , sep_token=_a , unk_token=_a , pad_token=_a , language_codes=_a , sp_model_kwargs=self.sp_model_kwargs , num_madeup_words=_a , **_a , ) lowerCamelCase = vocab_file lowerCamelCase = load_json(_a ) lowerCamelCase = {v: k for k, v in self.encoder.items()} lowerCamelCase = spm_file lowerCamelCase = load_spm(_a , self.sp_model_kwargs ) lowerCamelCase = len(self.encoder ) lowerCamelCase = { self.get_lang_token(_a ): self.encoder_size + i for i, lang_code in enumerate(_a ) } lowerCamelCase = {lang_code: self.encoder_size + i for i, lang_code in enumerate(_a )} lowerCamelCase = {v: k for k, v in self.lang_token_to_id.items()} lowerCamelCase = src_lang if src_lang is not None else """en""" lowerCamelCase = tgt_lang lowerCamelCase = self.get_lang_id(self._src_lang ) self.set_src_lang_special_tokens(self._src_lang ) lowerCamelCase = num_madeup_words @property def _lowerCAmelCase ( self ): """simple docstring""" return len(self.encoder ) + len(self.lang_token_to_id ) @property def _lowerCAmelCase ( self ): """simple docstring""" return self._src_lang @src_lang.setter def _lowerCAmelCase ( self , _a ): """simple docstring""" lowerCamelCase = new_src_lang self.set_src_lang_special_tokens(self._src_lang ) def _lowerCAmelCase ( self , _a ): """simple docstring""" return self.sp_model.encode(_a , out_type=_a ) def _lowerCAmelCase ( self , _a ): """simple docstring""" if token in self.lang_token_to_id: return self.lang_token_to_id[token] return self.encoder.get(_a , self.encoder[self.unk_token] ) def _lowerCAmelCase ( self , _a ): """simple docstring""" if index in self.id_to_lang_token: return self.id_to_lang_token[index] return self.decoder.get(_a , self.unk_token ) def _lowerCAmelCase ( self , _a ): """simple docstring""" lowerCamelCase = [] lowerCamelCase = """""" 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(_a ) + token lowerCamelCase = [] else: current_sub_tokens.append(_a ) out_string += self.sp_model.decode(_a ) return out_string.strip() def _lowerCAmelCase ( self , _a , _a = None , _a = False ): """simple docstring""" if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=_a , token_ids_a=_a , already_has_special_tokens=_a ) lowerCamelCase = [1] * len(self.prefix_tokens ) lowerCamelCase = [1] * len(self.suffix_tokens ) if token_ids_a is None: return prefix_ones + ([0] * len(_a )) + suffix_ones return prefix_ones + ([0] * len(_a )) + ([0] * len(_a )) + suffix_ones def _lowerCAmelCase ( self , _a , _a = 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 _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = {self.convert_ids_to_tokens(_a ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self ): """simple docstring""" lowerCamelCase = self.__dict__.copy() lowerCamelCase = None return state def __setstate__( self , _a ): """simple docstring""" lowerCamelCase = d # for backward compatibility if not hasattr(self , """sp_model_kwargs""" ): lowerCamelCase = {} lowerCamelCase = load_spm(self.spm_file , self.sp_model_kwargs ) def _lowerCAmelCase ( self , _a , _a = None ): """simple docstring""" lowerCamelCase = Path(_a ) if not save_dir.is_dir(): raise OSError(f'{save_directory} should be a directory' ) lowerCamelCase = save_dir / ( (filename_prefix + """-""" if filename_prefix else """""") + self.vocab_files_names["""vocab_file"""] ) lowerCamelCase = save_dir / ( (filename_prefix + """-""" if filename_prefix else """""") + self.vocab_files_names["""spm_file"""] ) save_json(self.encoder , _a ) if os.path.abspath(self.spm_file ) != os.path.abspath(_a ) and os.path.isfile(self.spm_file ): copyfile(self.spm_file , _a ) elif not os.path.isfile(self.spm_file ): with open(_a , """wb""" ) as fi: lowerCamelCase = self.sp_model.serialized_model_proto() fi.write(_a ) return (str(_a ), str(_a )) def _lowerCAmelCase ( self , _a , _a = "en" , _a = None , _a = "ro" , **_a , ): """simple docstring""" lowerCamelCase = src_lang lowerCamelCase = tgt_lang self.set_src_lang_special_tokens(self.src_lang ) return super().prepare_seqaseq_batch(_a , _a , **_a ) def _lowerCAmelCase ( self , _a , _a , _a , **_a ): """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""" ) lowerCamelCase = src_lang lowerCamelCase = self(_a , add_special_tokens=_a , **_a ) lowerCamelCase = self.get_lang_id(_a ) lowerCamelCase = tgt_lang_id return inputs def _lowerCAmelCase ( self ): """simple docstring""" self.set_src_lang_special_tokens(self.src_lang ) def _lowerCAmelCase ( self ): """simple docstring""" self.set_tgt_lang_special_tokens(self.tgt_lang ) def _lowerCAmelCase ( self , _a ): """simple docstring""" lowerCamelCase = self.get_lang_token(_a ) lowerCamelCase = self.lang_token_to_id[lang_token] lowerCamelCase = [self.cur_lang_id] lowerCamelCase = [self.eos_token_id] def _lowerCAmelCase ( self , _a ): """simple docstring""" lowerCamelCase = self.get_lang_token(_a ) lowerCamelCase = self.lang_token_to_id[lang_token] lowerCamelCase = [self.cur_lang_id] lowerCamelCase = [self.eos_token_id] def _lowerCAmelCase ( self , _a ): """simple docstring""" return self.lang_code_to_token[lang] def _lowerCAmelCase ( self , _a ): """simple docstring""" lowerCamelCase = self.get_lang_token(_a ) return self.lang_token_to_id[lang_token] def a__ ( snake_case__ , snake_case__ ) -> sentencepiece.SentencePieceProcessor: lowerCamelCase = sentencepiece.SentencePieceProcessor(**snake_case__ ) spm.Load(str(snake_case__ ) ) return spm def a__ ( snake_case__ ) -> Union[Dict, List]: with open(snake_case__ , """r""" ) as f: return json.load(snake_case__ ) def a__ ( snake_case__ , snake_case__ ) -> None: with open(snake_case__ , """w""" ) as f: json.dump(snake_case__ , snake_case__ , indent=2 )
291
"""simple docstring""" from pickle import UnpicklingError import jax import jax.numpy as jnp import numpy as np from flax.serialization import from_bytes from flax.traverse_util import flatten_dict from ..utils import logging lowerCAmelCase : Tuple = logging.get_logger(__name__) def a__ ( snake_case__ , snake_case__ ) -> Tuple: try: with open(snake_case__ , """rb""" ) as flax_state_f: lowerCamelCase = from_bytes(snake_case__ , flax_state_f.read() ) except UnpicklingError as e: try: with open(snake_case__ ) as f: if f.read().startswith("""version""" ): raise OSError( """You seem to have cloned a repository without having git-lfs installed. Please""" """ install git-lfs and run `git lfs install` followed by `git lfs pull` in the""" """ folder you cloned.""" ) else: raise ValueError from e except (UnicodeDecodeError, ValueError): raise EnvironmentError(F'Unable to convert {model_file} to Flax deserializable object. ' ) return load_flax_weights_in_pytorch_model(snake_case__ , snake_case__ ) def a__ ( snake_case__ , snake_case__ ) -> Tuple: try: import torch # noqa: F401 except ImportError: logger.error( """Loading Flax weights in PyTorch requires both PyTorch and Flax to be installed. Please see""" """ https://pytorch.org/ and https://flax.readthedocs.io/en/latest/installation.html for installation""" """ instructions.""" ) raise # check if we have bf16 weights lowerCamelCase = flatten_dict(jax.tree_util.tree_map(lambda snake_case__ : x.dtype == jnp.bfloataa , snake_case__ ) ).values() if any(snake_case__ ): # convert all weights to fp32 if they are bf16 since torch.from_numpy can-not handle bf16 # and bf16 is not fully supported in PT yet. logger.warning( """Found ``bfloat16`` weights in Flax model. Casting all ``bfloat16`` weights to ``float32`` """ """before loading those in PyTorch model.""" ) lowerCamelCase = jax.tree_util.tree_map( lambda snake_case__ : params.astype(np.floataa ) if params.dtype == jnp.bfloataa else params , snake_case__ ) lowerCamelCase = """""" lowerCamelCase = flatten_dict(snake_case__ , sep=""".""" ) lowerCamelCase = pt_model.state_dict() # keep track of unexpected & missing keys lowerCamelCase = [] lowerCamelCase = set(pt_model_dict.keys() ) for flax_key_tuple, flax_tensor in flax_state_dict.items(): lowerCamelCase = flax_key_tuple.split(""".""" ) if flax_key_tuple_array[-1] == "kernel" and flax_tensor.ndim == 4: lowerCamelCase = flax_key_tuple_array[:-1] + ["""weight"""] lowerCamelCase = jnp.transpose(snake_case__ , (3, 2, 0, 1) ) elif flax_key_tuple_array[-1] == "kernel": lowerCamelCase = flax_key_tuple_array[:-1] + ["""weight"""] lowerCamelCase = flax_tensor.T elif flax_key_tuple_array[-1] == "scale": lowerCamelCase = flax_key_tuple_array[:-1] + ["""weight"""] if "time_embedding" not in flax_key_tuple_array: for i, flax_key_tuple_string in enumerate(snake_case__ ): lowerCamelCase = ( flax_key_tuple_string.replace("""_0""" , """.0""" ) .replace("""_1""" , """.1""" ) .replace("""_2""" , """.2""" ) .replace("""_3""" , """.3""" ) .replace("""_4""" , """.4""" ) .replace("""_5""" , """.5""" ) .replace("""_6""" , """.6""" ) .replace("""_7""" , """.7""" ) .replace("""_8""" , """.8""" ) .replace("""_9""" , """.9""" ) ) lowerCamelCase = """.""".join(snake_case__ ) if flax_key in pt_model_dict: if flax_tensor.shape != pt_model_dict[flax_key].shape: raise ValueError( F'Flax checkpoint seems to be incorrect. Weight {flax_key_tuple} was expected ' F'to be of shape {pt_model_dict[flax_key].shape}, but is {flax_tensor.shape}.' ) else: # add weight to pytorch dict lowerCamelCase = np.asarray(snake_case__ ) if not isinstance(snake_case__ , np.ndarray ) else flax_tensor lowerCamelCase = torch.from_numpy(snake_case__ ) # remove from missing keys missing_keys.remove(snake_case__ ) else: # weight is not expected by PyTorch model unexpected_keys.append(snake_case__ ) pt_model.load_state_dict(snake_case__ ) # re-transform missing_keys to list lowerCamelCase = list(snake_case__ ) if len(snake_case__ ) > 0: logger.warning( """Some weights of the Flax model were not used when initializing the PyTorch model""" F' {pt_model.__class__.__name__}: {unexpected_keys}\n- This IS expected if you are initializing' F' {pt_model.__class__.__name__} from a Flax model trained on another task or with another architecture' """ (e.g. initializing a BertForSequenceClassification model from a FlaxBertForPreTraining model).\n- This""" F' IS NOT expected if you are initializing {pt_model.__class__.__name__} from a Flax model that you expect' """ to be exactly identical (e.g. initializing a BertForSequenceClassification model from a""" """ FlaxBertForSequenceClassification model).""" ) if len(snake_case__ ) > 0: logger.warning( F'Some weights of {pt_model.__class__.__name__} were not initialized from the Flax model and are newly' F' initialized: {missing_keys}\nYou should probably TRAIN this model on a down-stream task to be able to' """ use it for predictions and inference.""" ) return pt_model
291
1
"""simple docstring""" import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import XLMRobertaTokenizerFast from diffusers import DDIMScheduler, KandinskyImgaImgPipeline, KandinskyPriorPipeline, UNetaDConditionModel, VQModel from diffusers.pipelines.kandinsky.text_encoder import MCLIPConfig, MultilingualCLIP from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference enable_full_determinism() class __magic_name__ ( UpperCAmelCase__ , unittest.TestCase ): '''simple docstring''' __UpperCamelCase = KandinskyImgaImgPipeline __UpperCamelCase = ["prompt", "image_embeds", "negative_image_embeds", "image"] __UpperCamelCase = [ "prompt", "negative_prompt", "image_embeds", "negative_image_embeds", "image", ] __UpperCamelCase = [ "generator", "height", "width", "strength", "guidance_scale", "negative_prompt", "num_inference_steps", "return_dict", "guidance_scale", "num_images_per_prompt", "output_type", "return_dict", ] __UpperCamelCase = False @property def _lowerCAmelCase ( self ): """simple docstring""" return 32 @property def _lowerCAmelCase ( self ): """simple docstring""" return 32 @property def _lowerCAmelCase ( self ): """simple docstring""" return self.time_input_dim @property def _lowerCAmelCase ( self ): """simple docstring""" return self.time_input_dim * 4 @property def _lowerCAmelCase ( self ): """simple docstring""" return 100 @property def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = XLMRobertaTokenizerFast.from_pretrained("""YiYiXu/tiny-random-mclip-base""" ) return tokenizer @property def _lowerCAmelCase ( self ): """simple docstring""" torch.manual_seed(0 ) lowerCamelCase = MCLIPConfig( numDims=self.cross_attention_dim , transformerDimensions=self.text_embedder_hidden_size , hidden_size=self.text_embedder_hidden_size , intermediate_size=37 , num_attention_heads=4 , num_hidden_layers=5 , vocab_size=1_005 , ) lowerCamelCase = MultilingualCLIP(_a ) lowerCamelCase = text_encoder.eval() return text_encoder @property def _lowerCAmelCase ( self ): """simple docstring""" torch.manual_seed(0 ) lowerCamelCase = { """in_channels""": 4, # Out channels is double in channels because predicts mean and variance """out_channels""": 8, """addition_embed_type""": """text_image""", """down_block_types""": ("""ResnetDownsampleBlock2D""", """SimpleCrossAttnDownBlock2D"""), """up_block_types""": ("""SimpleCrossAttnUpBlock2D""", """ResnetUpsampleBlock2D"""), """mid_block_type""": """UNetMidBlock2DSimpleCrossAttn""", """block_out_channels""": (self.block_out_channels_a, self.block_out_channels_a * 2), """layers_per_block""": 1, """encoder_hid_dim""": self.text_embedder_hidden_size, """encoder_hid_dim_type""": """text_image_proj""", """cross_attention_dim""": self.cross_attention_dim, """attention_head_dim""": 4, """resnet_time_scale_shift""": """scale_shift""", """class_embed_type""": None, } lowerCamelCase = UNetaDConditionModel(**_a ) return model @property def _lowerCAmelCase ( self ): """simple docstring""" return { "block_out_channels": [32, 64], "down_block_types": ["DownEncoderBlock2D", "AttnDownEncoderBlock2D"], "in_channels": 3, "latent_channels": 4, "layers_per_block": 1, "norm_num_groups": 8, "norm_type": "spatial", "num_vq_embeddings": 12, "out_channels": 3, "up_block_types": [ "AttnUpDecoderBlock2D", "UpDecoderBlock2D", ], "vq_embed_dim": 4, } @property def _lowerCAmelCase ( self ): """simple docstring""" torch.manual_seed(0 ) lowerCamelCase = VQModel(**self.dummy_movq_kwargs ) return model def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.dummy_text_encoder lowerCamelCase = self.dummy_tokenizer lowerCamelCase = self.dummy_unet lowerCamelCase = self.dummy_movq lowerCamelCase = { """num_train_timesteps""": 1_000, """beta_schedule""": """linear""", """beta_start""": 0.00_085, """beta_end""": 0.012, """clip_sample""": False, """set_alpha_to_one""": False, """steps_offset""": 0, """prediction_type""": """epsilon""", """thresholding""": False, } lowerCamelCase = DDIMScheduler(**_a ) lowerCamelCase = { """text_encoder""": text_encoder, """tokenizer""": tokenizer, """unet""": unet, """scheduler""": scheduler, """movq""": movq, } return components def _lowerCAmelCase ( self , _a , _a=0 ): """simple docstring""" lowerCamelCase = floats_tensor((1, self.cross_attention_dim) , rng=random.Random(_a ) ).to(_a ) lowerCamelCase = floats_tensor((1, self.cross_attention_dim) , rng=random.Random(seed + 1 ) ).to(_a ) # create init_image lowerCamelCase = floats_tensor((1, 3, 64, 64) , rng=random.Random(_a ) ).to(_a ) lowerCamelCase = image.cpu().permute(0 , 2 , 3 , 1 )[0] lowerCamelCase = Image.fromarray(np.uinta(_a ) ).convert("""RGB""" ).resize((256, 256) ) if str(_a ).startswith("""mps""" ): lowerCamelCase = torch.manual_seed(_a ) else: lowerCamelCase = torch.Generator(device=_a ).manual_seed(_a ) lowerCamelCase = { """prompt""": """horse""", """image""": init_image, """image_embeds""": image_embeds, """negative_image_embeds""": negative_image_embeds, """generator""": generator, """height""": 64, """width""": 64, """num_inference_steps""": 10, """guidance_scale""": 7.0, """strength""": 0.2, """output_type""": """np""", } return inputs def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = """cpu""" lowerCamelCase = self.get_dummy_components() lowerCamelCase = self.pipeline_class(**_a ) lowerCamelCase = pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) lowerCamelCase = pipe(**self.get_dummy_inputs(_a ) ) lowerCamelCase = output.images lowerCamelCase = pipe( **self.get_dummy_inputs(_a ) , return_dict=_a , )[0] lowerCamelCase = image[0, -3:, -3:, -1] lowerCamelCase = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) lowerCamelCase = np.array( [0.61_474_943, 0.6_073_539, 0.43_308_544, 0.5_928_269, 0.47_493_595, 0.46_755_973, 0.4_613_838, 0.45_368_797, 0.50_119_233] ) assert ( np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 ), f' expected_slice {expected_slice}, but got {image_slice.flatten()}' assert ( np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 ), f' expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}' @slow @require_torch_gpu class __magic_name__ ( unittest.TestCase ): '''simple docstring''' def _lowerCAmelCase ( self ): """simple docstring""" # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/kandinsky/kandinsky_img2img_frog.npy""" ) lowerCamelCase = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/kandinsky/cat.png""" ) lowerCamelCase = """A red cartoon frog, 4k""" lowerCamelCase = KandinskyPriorPipeline.from_pretrained( """kandinsky-community/kandinsky-2-1-prior""" , torch_dtype=torch.floataa ) pipe_prior.to(_a ) lowerCamelCase = KandinskyImgaImgPipeline.from_pretrained( """kandinsky-community/kandinsky-2-1""" , torch_dtype=torch.floataa ) lowerCamelCase = pipeline.to(_a ) pipeline.set_progress_bar_config(disable=_a ) lowerCamelCase = torch.Generator(device="""cpu""" ).manual_seed(0 ) lowerCamelCase , lowerCamelCase = pipe_prior( _a , generator=_a , num_inference_steps=5 , negative_prompt="""""" , ).to_tuple() lowerCamelCase = pipeline( _a , image=_a , image_embeds=_a , negative_image_embeds=_a , generator=_a , num_inference_steps=100 , height=768 , width=768 , strength=0.2 , output_type="""np""" , ) lowerCamelCase = output.images[0] assert image.shape == (768, 768, 3) assert_mean_pixel_difference(_a , _a )
291
"""simple docstring""" import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_xlnet import XLNetTokenizer else: lowerCAmelCase : int = None lowerCAmelCase : Tuple = logging.get_logger(__name__) lowerCAmelCase : Tuple = {"""vocab_file""": """spiece.model""", """tokenizer_file""": """tokenizer.json"""} lowerCAmelCase : Union[str, Any] = { """vocab_file""": { """xlnet-base-cased""": """https://huggingface.co/xlnet-base-cased/resolve/main/spiece.model""", """xlnet-large-cased""": """https://huggingface.co/xlnet-large-cased/resolve/main/spiece.model""", }, """tokenizer_file""": { """xlnet-base-cased""": """https://huggingface.co/xlnet-base-cased/resolve/main/tokenizer.json""", """xlnet-large-cased""": """https://huggingface.co/xlnet-large-cased/resolve/main/tokenizer.json""", }, } lowerCAmelCase : Optional[int] = { """xlnet-base-cased""": None, """xlnet-large-cased""": None, } lowerCAmelCase : Union[str, Any] = """▁""" # Segments (not really needed) lowerCAmelCase : str = 0 lowerCAmelCase : Optional[int] = 1 lowerCAmelCase : Tuple = 2 lowerCAmelCase : Optional[Any] = 3 lowerCAmelCase : List[Any] = 4 class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' __UpperCamelCase = VOCAB_FILES_NAMES __UpperCamelCase = PRETRAINED_VOCAB_FILES_MAP __UpperCamelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCamelCase = "left" __UpperCamelCase = XLNetTokenizer def __init__( self , _a=None , _a=None , _a=False , _a=True , _a=False , _a="<s>" , _a="</s>" , _a="<unk>" , _a="<sep>" , _a="<pad>" , _a="<cls>" , _a="<mask>" , _a=["<eop>", "<eod>"] , **_a , ): """simple docstring""" # Mask token behave like a normal word, i.e. include the space before it lowerCamelCase = AddedToken(_a , lstrip=_a , rstrip=_a ) if isinstance(_a , _a ) else mask_token super().__init__( vocab_file=_a , tokenizer_file=_a , do_lower_case=_a , remove_space=_a , keep_accents=_a , bos_token=_a , eos_token=_a , unk_token=_a , sep_token=_a , pad_token=_a , cls_token=_a , mask_token=_a , additional_special_tokens=_a , **_a , ) lowerCamelCase = 3 lowerCamelCase = do_lower_case lowerCamelCase = remove_space lowerCamelCase = keep_accents lowerCamelCase = vocab_file lowerCamelCase = False if not self.vocab_file else True def _lowerCAmelCase ( self , _a , _a = None ): """simple docstring""" lowerCamelCase = [self.sep_token_id] lowerCamelCase = [self.cls_token_id] if token_ids_a is None: return token_ids_a + sep + cls return token_ids_a + sep + token_ids_a + sep + cls def _lowerCAmelCase ( self , _a , _a = None ): """simple docstring""" lowerCamelCase = [self.sep_token_id] lowerCamelCase = [2] if token_ids_a is None: return len(token_ids_a + sep ) * [0] + cls_segment_id return len(token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] + cls_segment_id def _lowerCAmelCase ( self , _a , _a = 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(_a ): logger.error(f'Vocabulary path ({save_directory}) should be a directory' ) return lowerCamelCase = os.path.join( _a , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_a ): copyfile(self.vocab_file , _a ) return (out_vocab_file,)
291
1
"""simple docstring""" from __future__ import annotations # This is the precision for this function which can be altered. # It is recommended for users to keep this number greater than or equal to 10. lowerCAmelCase : Union[str, Any] = 10 def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ ) -> int: for i in range(snake_case__ , snake_case__ ): if array[i] == target: return i return -1 def a__ ( snake_case__ , snake_case__ ) -> int: lowerCamelCase = 0 lowerCamelCase = len(snake_case__ ) while left <= right: if right - left < precision: return lin_search(snake_case__ , snake_case__ , snake_case__ , snake_case__ ) lowerCamelCase = (left + right) // 3 + 1 lowerCamelCase = 2 * (left + right) // 3 + 1 if array[one_third] == target: return one_third elif array[two_third] == target: return two_third elif target < array[one_third]: lowerCamelCase = one_third - 1 elif array[two_third] < target: lowerCamelCase = two_third + 1 else: lowerCamelCase = one_third + 1 lowerCamelCase = two_third - 1 else: return -1 def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ ) -> int: if left < right: if right - left < precision: return lin_search(snake_case__ , snake_case__ , snake_case__ , snake_case__ ) lowerCamelCase = (left + right) // 3 + 1 lowerCamelCase = 2 * (left + right) // 3 + 1 if array[one_third] == target: return one_third elif array[two_third] == target: return two_third elif target < array[one_third]: return rec_ternary_search(snake_case__ , one_third - 1 , snake_case__ , snake_case__ ) elif array[two_third] < target: return rec_ternary_search(two_third + 1 , snake_case__ , snake_case__ , snake_case__ ) else: return rec_ternary_search(one_third + 1 , two_third - 1 , snake_case__ , snake_case__ ) else: return -1 if __name__ == "__main__": import doctest doctest.testmod() lowerCAmelCase : List[Any] = input("""Enter numbers separated by comma:\n""").strip() lowerCAmelCase : Dict = [int(item.strip()) for item in user_input.split(""",""")] assert collection == sorted(collection), F"List must be ordered.\n{collection}." lowerCAmelCase : List[str] = int(input("""Enter the number to be found in the list:\n""").strip()) lowerCAmelCase : Tuple = ite_ternary_search(collection, target) lowerCAmelCase : str = rec_ternary_search(0, len(collection) - 1, collection, target) if resulta != -1: print(F"""Iterative search: {target} found at positions: {resulta}""") print(F"""Recursive search: {target} found at positions: {resulta}""") else: print("""Not found""")
291
"""simple docstring""" import unittest from transformers import ( MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING, TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING, TextaTextGenerationPipeline, pipeline, ) from transformers.testing_utils import is_pipeline_test, require_tf, require_torch from transformers.utils import is_torch_available from .test_pipelines_common import ANY if is_torch_available(): import torch @is_pipeline_test class __magic_name__ ( unittest.TestCase ): '''simple docstring''' __UpperCamelCase = MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING __UpperCamelCase = TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING def _lowerCAmelCase ( self , _a , _a , _a ): """simple docstring""" lowerCamelCase = TextaTextGenerationPipeline(model=_a , tokenizer=_a ) return generator, ["Something to write", "Something else"] def _lowerCAmelCase ( self , _a , _a ): """simple docstring""" lowerCamelCase = generator("""Something there""" ) self.assertEqual(_a , [{"""generated_text""": ANY(_a )}] ) # These are encoder decoder, they don't just append to incoming string self.assertFalse(outputs[0]["""generated_text"""].startswith("""Something there""" ) ) lowerCamelCase = generator(["""This is great !""", """Something else"""] , num_return_sequences=2 , do_sample=_a ) self.assertEqual( _a , [ [{"""generated_text""": ANY(_a )}, {"""generated_text""": ANY(_a )}], [{"""generated_text""": ANY(_a )}, {"""generated_text""": ANY(_a )}], ] , ) lowerCamelCase = generator( ["""This is great !""", """Something else"""] , num_return_sequences=2 , batch_size=2 , do_sample=_a ) self.assertEqual( _a , [ [{"""generated_text""": ANY(_a )}, {"""generated_text""": ANY(_a )}], [{"""generated_text""": ANY(_a )}, {"""generated_text""": ANY(_a )}], ] , ) with self.assertRaises(_a ): generator(4 ) @require_torch def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = pipeline("""text2text-generation""" , model="""patrickvonplaten/t5-tiny-random""" , framework="""pt""" ) # do_sample=False necessary for reproducibility lowerCamelCase = generator("""Something there""" , do_sample=_a ) self.assertEqual(_a , [{"""generated_text""": """"""}] ) lowerCamelCase = 3 lowerCamelCase = generator( """Something there""" , num_return_sequences=_a , num_beams=_a , ) lowerCamelCase = [ {"""generated_text""": """Beide Beide Beide Beide Beide Beide Beide Beide Beide"""}, {"""generated_text""": """Beide Beide Beide Beide Beide Beide Beide Beide"""}, {"""generated_text""": """"""}, ] self.assertEqual(_a , _a ) lowerCamelCase = generator("""This is a test""" , do_sample=_a , num_return_sequences=2 , return_tensors=_a ) self.assertEqual( _a , [ {"""generated_token_ids""": ANY(torch.Tensor )}, {"""generated_token_ids""": ANY(torch.Tensor )}, ] , ) lowerCamelCase = generator.model.config.eos_token_id lowerCamelCase = """<pad>""" lowerCamelCase = generator( ["""This is a test""", """This is a second test"""] , do_sample=_a , num_return_sequences=2 , batch_size=2 , return_tensors=_a , ) self.assertEqual( _a , [ [ {"""generated_token_ids""": ANY(torch.Tensor )}, {"""generated_token_ids""": ANY(torch.Tensor )}, ], [ {"""generated_token_ids""": ANY(torch.Tensor )}, {"""generated_token_ids""": ANY(torch.Tensor )}, ], ] , ) @require_tf def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = pipeline("""text2text-generation""" , model="""patrickvonplaten/t5-tiny-random""" , framework="""tf""" ) # do_sample=False necessary for reproducibility lowerCamelCase = generator("""Something there""" , do_sample=_a ) self.assertEqual(_a , [{"""generated_text""": """"""}] )
291
1
"""simple docstring""" from dataclasses import dataclass from typing import Optional, Tuple, Union import torch import torch.nn as nn from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput from .embeddings import GaussianFourierProjection, TimestepEmbedding, Timesteps from .modeling_utils import ModelMixin from .unet_ad_blocks import get_down_block, get_mid_block, get_out_block, get_up_block @dataclass class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' __UpperCamelCase = 42 class __magic_name__ ( UpperCAmelCase__ , UpperCAmelCase__ ): '''simple docstring''' @register_to_config def __init__( self , _a = 65_536 , _a = None , _a = 2 , _a = 2 , _a = 0 , _a = "fourier" , _a = True , _a = False , _a = 0.0 , _a = ("DownBlock1DNoSkip", "DownBlock1D", "AttnDownBlock1D") , _a = ("AttnUpBlock1D", "UpBlock1D", "UpBlock1DNoSkip") , _a = "UNetMidBlock1D" , _a = None , _a = (32, 32, 64) , _a = None , _a = 8 , _a = 1 , _a = False , ): """simple docstring""" super().__init__() lowerCamelCase = sample_size # time if time_embedding_type == "fourier": lowerCamelCase = GaussianFourierProjection( embedding_size=8 , set_W_to_weight=_a , log=_a , flip_sin_to_cos=_a ) lowerCamelCase = 2 * block_out_channels[0] elif time_embedding_type == "positional": lowerCamelCase = Timesteps( block_out_channels[0] , flip_sin_to_cos=_a , downscale_freq_shift=_a ) lowerCamelCase = block_out_channels[0] if use_timestep_embedding: lowerCamelCase = block_out_channels[0] * 4 lowerCamelCase = TimestepEmbedding( in_channels=_a , time_embed_dim=_a , act_fn=_a , out_dim=block_out_channels[0] , ) lowerCamelCase = nn.ModuleList([] ) lowerCamelCase = None lowerCamelCase = nn.ModuleList([] ) lowerCamelCase = None # down lowerCamelCase = in_channels for i, down_block_type in enumerate(_a ): lowerCamelCase = output_channel lowerCamelCase = block_out_channels[i] if i == 0: input_channel += extra_in_channels lowerCamelCase = i == len(_a ) - 1 lowerCamelCase = get_down_block( _a , num_layers=_a , in_channels=_a , out_channels=_a , temb_channels=block_out_channels[0] , add_downsample=not is_final_block or downsample_each_block , ) self.down_blocks.append(_a ) # mid lowerCamelCase = get_mid_block( _a , in_channels=block_out_channels[-1] , mid_channels=block_out_channels[-1] , out_channels=block_out_channels[-1] , embed_dim=block_out_channels[0] , num_layers=_a , add_downsample=_a , ) # up lowerCamelCase = list(reversed(_a ) ) lowerCamelCase = reversed_block_out_channels[0] if out_block_type is None: lowerCamelCase = out_channels else: lowerCamelCase = block_out_channels[0] for i, up_block_type in enumerate(_a ): lowerCamelCase = output_channel lowerCamelCase = ( reversed_block_out_channels[i + 1] if i < len(_a ) - 1 else final_upsample_channels ) lowerCamelCase = i == len(_a ) - 1 lowerCamelCase = get_up_block( _a , num_layers=_a , in_channels=_a , out_channels=_a , temb_channels=block_out_channels[0] , add_upsample=not is_final_block , ) self.up_blocks.append(_a ) lowerCamelCase = output_channel # out lowerCamelCase = norm_num_groups if norm_num_groups is not None else min(block_out_channels[0] // 4 , 32 ) lowerCamelCase = get_out_block( out_block_type=_a , num_groups_out=_a , embed_dim=block_out_channels[0] , out_channels=_a , act_fn=_a , fc_dim=block_out_channels[-1] // 4 , ) def _lowerCAmelCase ( self , _a , _a , _a = True , ): """simple docstring""" lowerCamelCase = timestep if not torch.is_tensor(_a ): lowerCamelCase = torch.tensor([timesteps] , dtype=torch.long , device=sample.device ) elif torch.is_tensor(_a ) and len(timesteps.shape ) == 0: lowerCamelCase = timesteps[None].to(sample.device ) lowerCamelCase = self.time_proj(_a ) if self.config.use_timestep_embedding: lowerCamelCase = self.time_mlp(_a ) else: lowerCamelCase = timestep_embed[..., None] lowerCamelCase = timestep_embed.repeat([1, 1, sample.shape[2]] ).to(sample.dtype ) lowerCamelCase = timestep_embed.broadcast_to((sample.shape[:1] + timestep_embed.shape[1:]) ) # 2. down lowerCamelCase = () for downsample_block in self.down_blocks: lowerCamelCase , lowerCamelCase = downsample_block(hidden_states=_a , temb=_a ) down_block_res_samples += res_samples # 3. mid if self.mid_block: lowerCamelCase = self.mid_block(_a , _a ) # 4. up for i, upsample_block in enumerate(self.up_blocks ): lowerCamelCase = down_block_res_samples[-1:] lowerCamelCase = down_block_res_samples[:-1] lowerCamelCase = upsample_block(_a , res_hidden_states_tuple=_a , temb=_a ) # 5. post-process if self.out_block: lowerCamelCase = self.out_block(_a , _a ) if not return_dict: return (sample,) return UNetaDOutput(sample=_a )
291
"""simple docstring""" def a__ ( snake_case__ , snake_case__ = False ) -> str: if not isinstance(snake_case__ , snake_case__ ): lowerCamelCase = F'Expected string as input, found {type(snake_case__ )}' raise ValueError(snake_case__ ) if not isinstance(snake_case__ , snake_case__ ): lowerCamelCase = F'Expected boolean as use_pascal parameter, found {type(snake_case__ )}' raise ValueError(snake_case__ ) lowerCamelCase = input_str.split("""_""" ) lowerCamelCase = 0 if use_pascal else 1 lowerCamelCase = words[start_index:] lowerCamelCase = [word[0].upper() + word[1:] for word in words_to_capitalize] lowerCamelCase = """""" if use_pascal else words[0] return "".join([initial_word, *capitalized_words] ) if __name__ == "__main__": from doctest import testmod testmod()
291
1
"""simple docstring""" import itertools import random import unittest import numpy as np from transformers import is_speech_available from transformers.testing_utils import require_torch, require_torchaudio from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin if is_speech_available(): from transformers import SpeechaTextFeatureExtractor lowerCAmelCase : Any = random.Random() def a__ ( snake_case__ , snake_case__=1.0 , snake_case__=None , snake_case__=None ) -> Optional[int]: if rng is None: lowerCamelCase = global_rng lowerCamelCase = [] for batch_idx in range(shape[0] ): values.append([] ) for _ in range(shape[1] ): values[-1].append(rng.random() * scale ) return values @require_torch @require_torchaudio class __magic_name__ ( unittest.TestCase ): '''simple docstring''' def __init__( self , _a , _a=7 , _a=400 , _a=2_000 , _a=24 , _a=24 , _a=0.0 , _a=16_000 , _a=True , _a=True , ): """simple docstring""" lowerCamelCase = parent lowerCamelCase = batch_size lowerCamelCase = min_seq_length lowerCamelCase = max_seq_length lowerCamelCase = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) lowerCamelCase = feature_size lowerCamelCase = num_mel_bins lowerCamelCase = padding_value lowerCamelCase = sampling_rate lowerCamelCase = return_attention_mask lowerCamelCase = do_normalize def _lowerCAmelCase ( self ): """simple docstring""" return { "feature_size": self.feature_size, "num_mel_bins": self.num_mel_bins, "padding_value": self.padding_value, "sampling_rate": self.sampling_rate, "return_attention_mask": self.return_attention_mask, "do_normalize": self.do_normalize, } def _lowerCAmelCase ( self , _a=False , _a=False ): """simple docstring""" def _flatten(_a ): return list(itertools.chain(*_a ) ) if equal_length: lowerCamelCase = [floats_list((self.max_seq_length, self.feature_size) ) for _ in range(self.batch_size )] else: # make sure that inputs increase in size lowerCamelCase = [ floats_list((x, self.feature_size) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: lowerCamelCase = [np.asarray(_a ) for x in speech_inputs] return speech_inputs @require_torch @require_torchaudio class __magic_name__ ( UpperCAmelCase__ , unittest.TestCase ): '''simple docstring''' __UpperCamelCase = SpeechaTextFeatureExtractor if is_speech_available() else None def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = SpeechaTextFeatureExtractionTester(self ) def _lowerCAmelCase ( self , _a ): """simple docstring""" self.assertTrue(np.all(np.mean(_a , axis=0 ) < 1e-3 ) ) self.assertTrue(np.all(np.abs(np.var(_a , axis=0 ) - 1 ) < 1e-3 ) ) def _lowerCAmelCase ( self ): """simple docstring""" # Tests that all call wrap to encode_plus and batch_encode_plus lowerCamelCase = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 lowerCamelCase = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] lowerCamelCase = [np.asarray(_a ) for speech_input in speech_inputs] # Test feature size lowerCamelCase = feature_extractor(_a , padding=_a , return_tensors="""np""" ).input_features self.assertTrue(input_features.ndim == 3 ) self.assertTrue(input_features.shape[-1] == feature_extractor.feature_size ) # Test not batched input lowerCamelCase = feature_extractor(speech_inputs[0] , return_tensors="""np""" ).input_features lowerCamelCase = feature_extractor(np_speech_inputs[0] , return_tensors="""np""" ).input_features self.assertTrue(np.allclose(_a , _a , atol=1e-3 ) ) # Test batched lowerCamelCase = feature_extractor(_a , return_tensors="""np""" ).input_features lowerCamelCase = feature_extractor(_a , return_tensors="""np""" ).input_features for enc_seq_a, enc_seq_a in zip(_a , _a ): self.assertTrue(np.allclose(_a , _a , atol=1e-3 ) ) # Test 2-D numpy arrays are batched. lowerCamelCase = [floats_list((1, x) )[0] for x in (800, 800, 800)] lowerCamelCase = np.asarray(_a ) lowerCamelCase = feature_extractor(_a , return_tensors="""np""" ).input_features lowerCamelCase = feature_extractor(_a , return_tensors="""np""" ).input_features for enc_seq_a, enc_seq_a in zip(_a , _a ): self.assertTrue(np.allclose(_a , _a , atol=1e-3 ) ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) lowerCamelCase = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] lowerCamelCase = ["""longest""", """max_length""", """do_not_pad"""] lowerCamelCase = [None, 16, None] for max_length, padding in zip(_a , _a ): lowerCamelCase = feature_extractor( _a , padding=_a , max_length=_a , return_attention_mask=_a ) lowerCamelCase = inputs.input_features lowerCamelCase = inputs.attention_mask lowerCamelCase = [np.sum(_a ) for x in attention_mask] self._check_zero_mean_unit_variance(input_features[0][: fbank_feat_lengths[0]] ) self._check_zero_mean_unit_variance(input_features[1][: fbank_feat_lengths[1]] ) self._check_zero_mean_unit_variance(input_features[2][: fbank_feat_lengths[2]] ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) lowerCamelCase = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] lowerCamelCase = ["""longest""", """max_length""", """do_not_pad"""] lowerCamelCase = [None, 16, None] for max_length, padding in zip(_a , _a ): lowerCamelCase = feature_extractor( _a , max_length=_a , padding=_a , return_tensors="""np""" , return_attention_mask=_a ) lowerCamelCase = inputs.input_features lowerCamelCase = inputs.attention_mask lowerCamelCase = [np.sum(_a ) for x in attention_mask] self._check_zero_mean_unit_variance(input_features[0][: fbank_feat_lengths[0]] ) self.assertTrue(input_features[0][fbank_feat_lengths[0] :].sum() < 1e-6 ) self._check_zero_mean_unit_variance(input_features[1][: fbank_feat_lengths[1]] ) self.assertTrue(input_features[0][fbank_feat_lengths[1] :].sum() < 1e-6 ) self._check_zero_mean_unit_variance(input_features[2][: fbank_feat_lengths[2]] ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) lowerCamelCase = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] lowerCamelCase = feature_extractor( _a , padding="""max_length""" , max_length=4 , truncation=_a , return_tensors="""np""" , return_attention_mask=_a , ) lowerCamelCase = inputs.input_features lowerCamelCase = inputs.attention_mask lowerCamelCase = np.sum(attention_mask == 1 , axis=1 ) self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]] ) self._check_zero_mean_unit_variance(input_features[1] ) self._check_zero_mean_unit_variance(input_features[2] ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) lowerCamelCase = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] lowerCamelCase = feature_extractor( _a , padding="""longest""" , max_length=4 , truncation=_a , return_tensors="""np""" , return_attention_mask=_a , ) lowerCamelCase = inputs.input_features lowerCamelCase = inputs.attention_mask lowerCamelCase = np.sum(attention_mask == 1 , axis=1 ) self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]] ) self._check_zero_mean_unit_variance(input_features[1, : fbank_feat_lengths[1]] ) self._check_zero_mean_unit_variance(input_features[2] ) # make sure that if max_length < longest -> then pad to max_length self.assertEqual(input_features.shape , (3, 4, 24) ) lowerCamelCase = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] lowerCamelCase = feature_extractor( _a , padding="""longest""" , max_length=16 , truncation=_a , return_tensors="""np""" , return_attention_mask=_a , ) lowerCamelCase = inputs.input_features lowerCamelCase = inputs.attention_mask lowerCamelCase = np.sum(attention_mask == 1 , axis=1 ) self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]] ) self._check_zero_mean_unit_variance(input_features[1, : fbank_feat_lengths[1]] ) self._check_zero_mean_unit_variance(input_features[2] ) # make sure that if max_length < longest -> then pad to max_length self.assertEqual(input_features.shape , (3, 6, 24) ) def _lowerCAmelCase ( self ): """simple docstring""" import torch lowerCamelCase = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) lowerCamelCase = np.random.rand(100 , 32 ).astype(np.floataa ) lowerCamelCase = np_speech_inputs.tolist() for inputs in [py_speech_inputs, np_speech_inputs]: lowerCamelCase = feature_extractor.pad([{"""input_features""": inputs}] , return_tensors="""np""" ) self.assertTrue(np_processed.input_features.dtype == np.floataa ) lowerCamelCase = feature_extractor.pad([{"""input_features""": inputs}] , return_tensors="""pt""" ) self.assertTrue(pt_processed.input_features.dtype == torch.floataa ) def _lowerCAmelCase ( self , _a ): """simple docstring""" from datasets import load_dataset lowerCamelCase = load_dataset("""hf-internal-testing/librispeech_asr_dummy""" , """clean""" , split="""validation""" ) # automatic decoding with librispeech lowerCamelCase = ds.sort("""id""" ).select(range(_a ) )[:num_samples]["""audio"""] return [x["array"] for x in speech_samples] def _lowerCAmelCase ( self ): """simple docstring""" # fmt: off lowerCamelCase = np.array([ -1.5_745, -1.7_713, -1.7_020, -1.6_069, -1.2_250, -1.1_105, -0.9_072, -0.8_241, -1.2_310, -0.8_098, -0.3_320, -0.4_101, -0.7_985, -0.4_996, -0.8_213, -0.9_128, -1.0_420, -1.1_286, -1.0_440, -0.7_999, -0.8_405, -1.2_275, -1.5_443, -1.4_625, ] ) # fmt: on lowerCamelCase = self._load_datasamples(1 ) lowerCamelCase = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) lowerCamelCase = feature_extractor(_a , return_tensors="""pt""" ).input_features self.assertEquals(input_features.shape , (1, 584, 24) ) self.assertTrue(np.allclose(input_features[0, 0, :30] , _a , atol=1e-4 ) )
291
"""simple docstring""" from typing import Dict, List, Optional, Tuple, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_torch_available, is_torch_tensor, logging if is_torch_available(): import torch lowerCAmelCase : int = logging.get_logger(__name__) class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' __UpperCamelCase = ["pixel_values"] def __init__( self , _a = True , _a = None , _a = PILImageResampling.BILINEAR , _a = True , _a = None , _a = True , _a = 1 / 255 , _a = True , _a = None , _a = None , **_a , ): """simple docstring""" super().__init__(**_a ) lowerCamelCase = size if size is not None else {"""shortest_edge""": 256} lowerCamelCase = get_size_dict(_a , default_to_square=_a ) lowerCamelCase = crop_size if crop_size is not None else {"""height""": 224, """width""": 224} lowerCamelCase = get_size_dict(_a , param_name="""crop_size""" ) lowerCamelCase = do_resize lowerCamelCase = size lowerCamelCase = resample lowerCamelCase = do_center_crop lowerCamelCase = crop_size lowerCamelCase = do_rescale lowerCamelCase = rescale_factor lowerCamelCase = do_normalize lowerCamelCase = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN lowerCamelCase = image_std if image_std is not None else IMAGENET_STANDARD_STD def _lowerCAmelCase ( self , _a , _a , _a = PILImageResampling.BICUBIC , _a = None , **_a , ): """simple docstring""" lowerCamelCase = get_size_dict(_a , default_to_square=_a ) if "shortest_edge" not in size: raise ValueError(f'The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}' ) lowerCamelCase = get_resize_output_image_size(_a , size=size["""shortest_edge"""] , default_to_square=_a ) return resize(_a , size=_a , resample=_a , data_format=_a , **_a ) def _lowerCAmelCase ( self , _a , _a , _a = None , **_a , ): """simple docstring""" lowerCamelCase = get_size_dict(_a ) if "height" not in size or "width" not in size: raise ValueError(f'The `size` parameter must contain the keys `height` and `width`. Got {size.keys()}' ) return center_crop(_a , size=(size["""height"""], size["""width"""]) , data_format=_a , **_a ) def _lowerCAmelCase ( self , _a , _a , _a = None , **_a ): """simple docstring""" return rescale(_a , scale=_a , data_format=_a , **_a ) def _lowerCAmelCase ( self , _a , _a , _a , _a = None , **_a , ): """simple docstring""" return normalize(_a , mean=_a , std=_a , data_format=_a , **_a ) def _lowerCAmelCase ( self , _a , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = ChannelDimension.FIRST , **_a , ): """simple docstring""" lowerCamelCase = do_resize if do_resize is not None else self.do_resize lowerCamelCase = size if size is not None else self.size lowerCamelCase = get_size_dict(_a , default_to_square=_a ) lowerCamelCase = resample if resample is not None else self.resample lowerCamelCase = do_center_crop if do_center_crop is not None else self.do_center_crop lowerCamelCase = crop_size if crop_size is not None else self.crop_size lowerCamelCase = get_size_dict(_a , param_name="""crop_size""" ) lowerCamelCase = do_rescale if do_rescale is not None else self.do_rescale lowerCamelCase = rescale_factor if rescale_factor is not None else self.rescale_factor lowerCamelCase = do_normalize if do_normalize is not None else self.do_normalize lowerCamelCase = image_mean if image_mean is not None else self.image_mean lowerCamelCase = image_std if image_std is not None else self.image_std lowerCamelCase = make_list_of_images(_a ) if not valid_images(_a ): raise ValueError( """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """ """torch.Tensor, tf.Tensor or jax.ndarray.""" ) if do_resize and size is None: raise ValueError("""Size must be specified if do_resize is True.""" ) if do_center_crop and crop_size is None: raise ValueError("""Crop size must be specified if do_center_crop is True.""" ) if do_rescale and rescale_factor is None: raise ValueError("""Rescale factor must be specified if do_rescale is True.""" ) if do_normalize and (image_mean is None or image_std is None): raise ValueError("""Image mean and std must be specified if do_normalize is True.""" ) # All transformations expect numpy arrays. lowerCamelCase = [to_numpy_array(_a ) for image in images] if do_resize: lowerCamelCase = [self.resize(image=_a , size=_a , resample=_a ) for image in images] if do_center_crop: lowerCamelCase = [self.center_crop(image=_a , size=_a ) for image in images] if do_rescale: lowerCamelCase = [self.rescale(image=_a , scale=_a ) for image in images] if do_normalize: lowerCamelCase = [self.normalize(image=_a , mean=_a , std=_a ) for image in images] lowerCamelCase = [to_channel_dimension_format(_a , _a ) for image in images] lowerCamelCase = {"""pixel_values""": images} return BatchFeature(data=_a , tensor_type=_a ) def _lowerCAmelCase ( self , _a , _a = None ): """simple docstring""" lowerCamelCase = outputs.logits # Resize logits and compute semantic segmentation maps if target_sizes is not None: if len(_a ) != len(_a ): raise ValueError( """Make sure that you pass in as many target sizes as the batch dimension of the logits""" ) if is_torch_tensor(_a ): lowerCamelCase = target_sizes.numpy() lowerCamelCase = [] for idx in range(len(_a ) ): lowerCamelCase = torch.nn.functional.interpolate( logits[idx].unsqueeze(dim=0 ) , size=target_sizes[idx] , mode="""bilinear""" , align_corners=_a ) lowerCamelCase = resized_logits[0].argmax(dim=0 ) semantic_segmentation.append(_a ) else: lowerCamelCase = logits.argmax(dim=1 ) lowerCamelCase = [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0] )] return semantic_segmentation
291
1
"""simple docstring""" import argparse from copy import deepcopy import numpy as np from datasets import ClassLabel, DatasetDict, load_dataset from evaluate import load from transformers import ( AutoModelForSequenceClassification, AutoTokenizer, DataCollatorWithPadding, Trainer, TrainerCallback, TrainingArguments, set_seed, ) def a__ ( ) -> Any: lowerCamelCase = argparse.ArgumentParser() parser.add_argument("""--model_ckpt""" , type=snake_case__ , default="""microsoft/unixcoder-base-nine""" ) parser.add_argument("""--num_epochs""" , type=snake_case__ , default=5 ) parser.add_argument("""--batch_size""" , type=snake_case__ , default=6 ) parser.add_argument("""--gradient_accumulation_steps""" , type=snake_case__ , default=1 ) parser.add_argument("""--freeze""" , type=snake_case__ , default=snake_case__ ) parser.add_argument("""--learning_rate""" , type=snake_case__ , default=5E-4 ) parser.add_argument("""--seed""" , type=snake_case__ , default=0 ) parser.add_argument("""--lr_scheduler_type""" , type=snake_case__ , default="""cosine""" ) parser.add_argument("""--num_warmup_steps""" , type=snake_case__ , default=10 ) parser.add_argument("""--weight_decay""" , type=snake_case__ , default=0.01 ) parser.add_argument("""--output_dir""" , type=snake_case__ , default="""./results""" ) return parser.parse_args() lowerCAmelCase : List[Any] = load("""accuracy""") def a__ ( snake_case__ ) -> str: lowerCamelCase , lowerCamelCase = eval_pred lowerCamelCase = np.argmax(snake_case__ , axis=1 ) return metric.compute(predictions=snake_case__ , references=snake_case__ ) class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' def __init__( self , _a ): """simple docstring""" super().__init__() lowerCamelCase = trainer def _lowerCAmelCase ( self , _a , _a , _a , **_a ): """simple docstring""" if control.should_evaluate: lowerCamelCase = deepcopy(_a ) self._trainer.evaluate(eval_dataset=self._trainer.train_dataset , metric_key_prefix="""train""" ) return control_copy def a__ ( ) -> Optional[Any]: lowerCamelCase = get_args() set_seed(args.seed ) lowerCamelCase = load_dataset("""codeparrot/codecomplex""" , split="""train""" ) lowerCamelCase = dataset.train_test_split(test_size=0.2 ) lowerCamelCase = train_test["""test"""].train_test_split(test_size=0.5 ) lowerCamelCase = DatasetDict( { """train""": train_test["""train"""], """test""": test_validation["""train"""], """valid""": test_validation["""test"""], } ) print("""Loading tokenizer and model""" ) lowerCamelCase = AutoTokenizer.from_pretrained(args.model_ckpt ) lowerCamelCase = tokenizer.eos_token lowerCamelCase = AutoModelForSequenceClassification.from_pretrained(args.model_ckpt , num_labels=7 ) lowerCamelCase = model.config.eos_token_id if args.freeze: for param in model.roberta.parameters(): lowerCamelCase = False lowerCamelCase = ClassLabel(num_classes=7 , names=list(set(train_test_validation["""train"""]["""complexity"""] ) ) ) def tokenize(snake_case__ ): lowerCamelCase = tokenizer(example["""src"""] , truncation=snake_case__ , max_length=10_24 ) lowerCamelCase = labels.straint(example["""complexity"""] ) return { "input_ids": inputs["input_ids"], "attention_mask": inputs["attention_mask"], "label": label, } lowerCamelCase = train_test_validation.map( snake_case__ , batched=snake_case__ , remove_columns=train_test_validation["""train"""].column_names , ) lowerCamelCase = DataCollatorWithPadding(tokenizer=snake_case__ ) lowerCamelCase = TrainingArguments( output_dir=args.output_dir , learning_rate=args.learning_rate , lr_scheduler_type=args.lr_scheduler_type , evaluation_strategy="""epoch""" , save_strategy="""epoch""" , logging_strategy="""epoch""" , per_device_train_batch_size=args.batch_size , per_device_eval_batch_size=args.batch_size , num_train_epochs=args.num_epochs , gradient_accumulation_steps=args.gradient_accumulation_steps , weight_decay=0.01 , metric_for_best_model="""accuracy""" , run_name="""complexity-java""" , report_to="""wandb""" , ) lowerCamelCase = Trainer( model=snake_case__ , args=snake_case__ , train_dataset=tokenized_datasets["""train"""] , eval_dataset=tokenized_datasets["""valid"""] , tokenizer=snake_case__ , data_collator=snake_case__ , compute_metrics=snake_case__ , ) print("""Training...""" ) trainer.add_callback(CustomCallback(snake_case__ ) ) trainer.train() if __name__ == "__main__": main()
291
"""simple docstring""" import operator as op lowerCAmelCase : Dict = """scaler.pt""" lowerCAmelCase : Tuple = """pytorch_model""" lowerCAmelCase : Union[str, Any] = """random_states""" lowerCAmelCase : Union[str, Any] = """optimizer""" lowerCAmelCase : Dict = """scheduler""" lowerCAmelCase : int = """pytorch_model.bin""" lowerCAmelCase : str = """pytorch_model.bin.index.json""" lowerCAmelCase : Union[str, Any] = """model.safetensors""" lowerCAmelCase : List[Any] = """model.safetensors.index.json""" lowerCAmelCase : List[Any] = """1.10.2""" lowerCAmelCase : Any = """py38""" lowerCAmelCase : Optional[int] = """4.17.0""" lowerCAmelCase : str = ["""ml.p3.16xlarge""", """ml.p3dn.24xlarge""", """ml.p4dn.24xlarge"""] lowerCAmelCase : Tuple = ["""FULL_SHARD""", """SHARD_GRAD_OP""", """NO_SHARD""", """HYBRID_SHARD""", """HYBRID_SHARD_ZERO2"""] lowerCAmelCase : List[Any] = ["""TRANSFORMER_BASED_WRAP""", """SIZE_BASED_WRAP""", """NO_WRAP"""] lowerCAmelCase : List[str] = ["""BACKWARD_PRE""", """BACKWARD_POST""", """NO_PREFETCH"""] lowerCAmelCase : List[str] = ["""FULL_STATE_DICT""", """LOCAL_STATE_DICT""", """SHARDED_STATE_DICT"""] lowerCAmelCase : Any = """2.0.1""" lowerCAmelCase : List[Any] = ["""pdsh""", """standard""", """openmpi""", """mvapich"""] lowerCAmelCase : Union[str, Any] = ["""default""", """reduce-overhead""", """max-autotune"""] lowerCAmelCase : Optional[int] = {""">""": op.gt, """>=""": op.ge, """==""": op.eq, """!=""": op.ne, """<=""": op.le, """<""": op.lt} # These are the args for `torch.distributed.launch` for pytorch < 1.9 lowerCAmelCase : Union[str, Any] = [ """nnodes""", """nproc_per_node""", """rdzv_backend""", """rdzv_endpoint""", """rdzv_id""", """rdzv_conf""", """standalone""", """max_restarts""", """monitor_interval""", """start_method""", """role""", """module""", """m""", """no_python""", """run_path""", """log_dir""", """r""", """redirects""", """t""", """tee""", """node_rank""", """master_addr""", """master_port""", ] lowerCAmelCase : List[str] = ["""DEEPSPEED""", """MULTI_GPU""", """FSDP""", """MEGATRON_LM"""] lowerCAmelCase : Optional[Any] = ["""DEEPSPEED""", """MULTI_XPU""", """FSDP"""]
291
1
"""simple docstring""" import numpy as np import torch from torch.utils.data import Dataset, IterableDataset from ..utils.generic import ModelOutput class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' def __init__( self , _a , _a , _a ): """simple docstring""" lowerCamelCase = dataset lowerCamelCase = process lowerCamelCase = params def __len__( self ): """simple docstring""" return len(self.dataset ) def __getitem__( self , _a ): """simple docstring""" lowerCamelCase = self.dataset[i] lowerCamelCase = self.process(_a , **self.params ) return processed class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' def __init__( self , _a , _a , _a , _a=None ): """simple docstring""" lowerCamelCase = loader lowerCamelCase = infer lowerCamelCase = params if loader_batch_size == 1: # Let's spare some time by deactivating altogether lowerCamelCase = None lowerCamelCase = loader_batch_size # Internal bookkeeping lowerCamelCase = None lowerCamelCase = None def __len__( self ): """simple docstring""" return len(self.loader ) def __iter__( self ): """simple docstring""" lowerCamelCase = iter(self.loader ) return self def _lowerCAmelCase ( self ): """simple docstring""" if isinstance(self._loader_batch_data , torch.Tensor ): # Batch data is simple tensor, just fetch the slice lowerCamelCase = self._loader_batch_data[self._loader_batch_index] else: # Batch data is assumed to be BaseModelOutput (or dict) lowerCamelCase = {} for k, element in self._loader_batch_data.items(): if isinstance(_a , _a ): # Convert ModelOutput to tuple first lowerCamelCase = element.to_tuple() if isinstance(element[0] , torch.Tensor ): lowerCamelCase = tuple(el[self._loader_batch_index].unsqueeze(0 ) for el in element ) elif isinstance(element[0] , np.ndarray ): lowerCamelCase = tuple(np.expand_dims(el[self._loader_batch_index] , 0 ) for el in element ) continue if k in {"hidden_states", "past_key_values", "attentions"} and isinstance(_a , _a ): # Those are stored as lists of tensors so need specific unbatching. if isinstance(element[0] , torch.Tensor ): lowerCamelCase = tuple(el[self._loader_batch_index].unsqueeze(0 ) for el in element ) elif isinstance(element[0] , np.ndarray ): lowerCamelCase = tuple(np.expand_dims(el[self._loader_batch_index] , 0 ) for el in element ) continue if element is None: # This can happen for optional data that get passed around lowerCamelCase = None elif isinstance(element[self._loader_batch_index] , torch.Tensor ): # Take correct batch data, but make it looked like batch_size=1 # For compatibility with other methods within transformers lowerCamelCase = element[self._loader_batch_index].unsqueeze(0 ) elif isinstance(element[self._loader_batch_index] , np.ndarray ): # Take correct batch data, but make it looked like batch_size=1 # For compatibility with other methods within transformers lowerCamelCase = np.expand_dims(element[self._loader_batch_index] , 0 ) else: # This is typically a list, so no need to `unsqueeze`. lowerCamelCase = element[self._loader_batch_index] # Recreate the element by reusing the original class to make it look # batch_size=1 lowerCamelCase = self._loader_batch_data.__class__(_a ) self._loader_batch_index += 1 return result def _lowerCAmelCase ( self ): """simple docstring""" if self._loader_batch_index is not None and self._loader_batch_index < self.loader_batch_size: # We are currently unrolling a batch so we just need to return # the current item within a batch return self.loader_batch_item() # We're out of items within a batch lowerCamelCase = next(self.iterator ) lowerCamelCase = self.infer(_a , **self.params ) # We now have a batch of "inferred things". if self.loader_batch_size is not None: # Try to infer the size of the batch if isinstance(_a , torch.Tensor ): lowerCamelCase = processed else: lowerCamelCase = list(processed.keys() )[0] lowerCamelCase = processed[key] if isinstance(_a , _a ): lowerCamelCase = len(_a ) else: lowerCamelCase = first_tensor.shape[0] if 0 < observed_batch_size < self.loader_batch_size: # could be last batch so we can't unroll as many # elements. lowerCamelCase = observed_batch_size # Setting internal index to unwrap the batch lowerCamelCase = processed lowerCamelCase = 0 return self.loader_batch_item() else: # We're not unrolling batches return processed class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' def __init__( self , _a , _a , _a , _a=None ): """simple docstring""" super().__init__(_a , _a , _a ) def __iter__( self ): """simple docstring""" lowerCamelCase = iter(self.loader ) lowerCamelCase = None return self def _lowerCAmelCase ( self ): """simple docstring""" if self.subiterator is None: lowerCamelCase = self.infer(next(self.iterator ) , **self.params ) try: # Try to return next item lowerCamelCase = next(self.subiterator ) except StopIteration: # When a preprocess iterator ends, we can start lookig at the next item # ChunkIterator will keep feeding until ALL elements of iterator # all have created their subiterator and have been iterating against. # # Another way to look at it, is we're basically flattening lists of lists # into a single list, but with generators lowerCamelCase = self.infer(next(self.iterator ) , **self.params ) lowerCamelCase = next(self.subiterator ) return processed class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' def __iter__( self ): """simple docstring""" lowerCamelCase = iter(self.loader ) return self def _lowerCAmelCase ( self ): """simple docstring""" # Extremely similar to PipelineIterator in its unpacking mechanism # BUT, we have an extra required item which is the presence of `is_last` # That is because everything is flattened by `PipelineChunkIterator` we # need to keep track of how to regroup here in the original `process` # boundaries so that `process` and `postprocess` see the same data. # This iterator accumulates items (possibly while unbatching) until it # its a `is_last` and then just passes it on to the caller. lowerCamelCase = False lowerCamelCase = [] if self._loader_batch_index is not None and self._loader_batch_index < self.loader_batch_size: while self._loader_batch_index < self.loader_batch_size: lowerCamelCase = self.loader_batch_item() lowerCamelCase = item.pop("""is_last""" ) accumulator.append(_a ) if is_last: return accumulator while not is_last: lowerCamelCase = self.infer(next(self.iterator ) , **self.params ) if self.loader_batch_size is not None: if isinstance(_a , torch.Tensor ): lowerCamelCase = processed else: lowerCamelCase = list(processed.keys() )[0] lowerCamelCase = processed[key] if isinstance(_a , _a ): lowerCamelCase = len(_a ) else: lowerCamelCase = first_tensor.shape[0] if 0 < observed_batch_size < self.loader_batch_size: # could be last batch so we can't unroll as many # elements. lowerCamelCase = observed_batch_size lowerCamelCase = processed lowerCamelCase = 0 while self._loader_batch_index < self.loader_batch_size: lowerCamelCase = self.loader_batch_item() lowerCamelCase = item.pop("""is_last""" ) accumulator.append(_a ) if is_last: return accumulator else: lowerCamelCase = processed lowerCamelCase = item.pop("""is_last""" ) accumulator.append(_a ) return accumulator class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' def __init__( self , _a , _a ): """simple docstring""" lowerCamelCase = dataset lowerCamelCase = key def __len__( self ): """simple docstring""" return len(self.dataset ) def __getitem__( self , _a ): """simple docstring""" return self.dataset[i][self.key] class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' def __init__( self , _a , _a , _a ): """simple docstring""" lowerCamelCase = dataset lowerCamelCase = keya lowerCamelCase = keya def __len__( self ): """simple docstring""" return len(self.dataset ) def __getitem__( self , _a ): """simple docstring""" return {"text": self.dataset[i][self.keya], "text_pair": self.dataset[i][self.keya]}
291
"""simple docstring""" import inspect import unittest from transformers import ViTMSNConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTMSNForImageClassification, ViTMSNModel from transformers.models.vit_msn.modeling_vit_msn import VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class __magic_name__ : '''simple docstring''' def __init__( self , _a , _a=13 , _a=30 , _a=2 , _a=3 , _a=True , _a=True , _a=32 , _a=5 , _a=4 , _a=37 , _a="gelu" , _a=0.1 , _a=0.1 , _a=10 , _a=0.02 , _a=None , ): """simple docstring""" lowerCamelCase = parent lowerCamelCase = batch_size lowerCamelCase = image_size lowerCamelCase = patch_size lowerCamelCase = num_channels lowerCamelCase = is_training lowerCamelCase = use_labels lowerCamelCase = hidden_size lowerCamelCase = num_hidden_layers lowerCamelCase = num_attention_heads lowerCamelCase = intermediate_size lowerCamelCase = hidden_act lowerCamelCase = hidden_dropout_prob lowerCamelCase = attention_probs_dropout_prob lowerCamelCase = type_sequence_label_size lowerCamelCase = initializer_range lowerCamelCase = scope # in ViT MSN, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) lowerCamelCase = (image_size // patch_size) ** 2 lowerCamelCase = num_patches + 1 def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) lowerCamelCase = None if self.use_labels: lowerCamelCase = ids_tensor([self.batch_size] , self.type_sequence_label_size ) lowerCamelCase = self.get_config() return config, pixel_values, labels def _lowerCAmelCase ( self ): """simple docstring""" return ViTMSNConfig( 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 , initializer_range=self.initializer_range , ) def _lowerCAmelCase ( self , _a , _a , _a ): """simple docstring""" lowerCamelCase = ViTMSNModel(config=_a ) model.to(_a ) model.eval() lowerCamelCase = model(_a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _lowerCAmelCase ( self , _a , _a , _a ): """simple docstring""" lowerCamelCase = self.type_sequence_label_size lowerCamelCase = ViTMSNForImageClassification(_a ) model.to(_a ) model.eval() lowerCamelCase = model(_a , labels=_a ) print("""Pixel and labels shape: {pixel_values.shape}, {labels.shape}""" ) print("""Labels: {labels}""" ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images lowerCamelCase = 1 lowerCamelCase = ViTMSNForImageClassification(_a ) model.to(_a ) model.eval() lowerCamelCase = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) lowerCamelCase = model(_a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.prepare_config_and_inputs() lowerCamelCase , lowerCamelCase , lowerCamelCase = config_and_inputs lowerCamelCase = {"""pixel_values""": pixel_values} return config, inputs_dict @require_torch class __magic_name__ ( UpperCAmelCase__ , UpperCAmelCase__ , unittest.TestCase ): '''simple docstring''' __UpperCamelCase = (ViTMSNModel, ViTMSNForImageClassification) if is_torch_available() else () __UpperCamelCase = ( {"feature-extraction": ViTMSNModel, "image-classification": ViTMSNForImageClassification} if is_torch_available() else {} ) __UpperCamelCase = False __UpperCamelCase = False __UpperCamelCase = False __UpperCamelCase = False def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = ViTMSNModelTester(self ) lowerCamelCase = ConfigTester(self , config_class=_a , has_text_modality=_a , hidden_size=37 ) def _lowerCAmelCase ( self ): """simple docstring""" self.config_tester.run_common_tests() @unittest.skip(reason="""ViTMSN does not use inputs_embeds""" ) def _lowerCAmelCase ( self ): """simple docstring""" pass def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase , lowerCamelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCamelCase = model_class(_a ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) lowerCamelCase = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_a , nn.Linear ) ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase , lowerCamelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCamelCase = model_class(_a ) lowerCamelCase = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic lowerCamelCase = [*signature.parameters.keys()] lowerCamelCase = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , _a ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_a ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_a ) @slow def _lowerCAmelCase ( self ): """simple docstring""" for model_name in VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowerCamelCase = ViTMSNModel.from_pretrained(_a ) self.assertIsNotNone(_a ) def a__ ( ) -> Any: lowerCamelCase = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) return image @require_torch @require_vision class __magic_name__ ( unittest.TestCase ): '''simple docstring''' @cached_property def _lowerCAmelCase ( self ): """simple docstring""" return ViTImageProcessor.from_pretrained("""facebook/vit-msn-small""" ) if is_vision_available() else None @slow def _lowerCAmelCase ( self ): """simple docstring""" torch.manual_seed(2 ) lowerCamelCase = ViTMSNForImageClassification.from_pretrained("""facebook/vit-msn-small""" ).to(_a ) lowerCamelCase = self.default_image_processor lowerCamelCase = prepare_img() lowerCamelCase = image_processor(images=_a , return_tensors="""pt""" ).to(_a ) # forward pass with torch.no_grad(): lowerCamelCase = model(**_a ) # verify the logits lowerCamelCase = torch.Size((1, 1_000) ) self.assertEqual(outputs.logits.shape , _a ) lowerCamelCase = torch.tensor([-0.0_803, -0.4_454, -0.2_375] ).to(_a ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , _a , atol=1e-4 ) )
291
1
"""simple docstring""" import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import YolosConfig, YolosForObjectDetection, YolosImageProcessor from transformers.utils import logging logging.set_verbosity_info() lowerCAmelCase : List[Any] = logging.get_logger(__name__) def a__ ( snake_case__ ) -> YolosConfig: lowerCamelCase = YolosConfig() # size of the architecture if "yolos_ti" in yolos_name: lowerCamelCase = 1_92 lowerCamelCase = 7_68 lowerCamelCase = 12 lowerCamelCase = 3 lowerCamelCase = [8_00, 13_33] lowerCamelCase = False elif yolos_name == "yolos_s_dWr": lowerCamelCase = 3_30 lowerCamelCase = 14 lowerCamelCase = 6 lowerCamelCase = 13_20 elif "yolos_s" in yolos_name: lowerCamelCase = 3_84 lowerCamelCase = 15_36 lowerCamelCase = 12 lowerCamelCase = 6 elif "yolos_b" in yolos_name: lowerCamelCase = [8_00, 13_44] lowerCamelCase = 91 lowerCamelCase = """huggingface/label-files""" lowerCamelCase = """coco-detection-id2label.json""" lowerCamelCase = json.load(open(hf_hub_download(snake_case__ , snake_case__ , repo_type="""dataset""" ) , """r""" ) ) lowerCamelCase = {int(snake_case__ ): v for k, v in idalabel.items()} lowerCamelCase = idalabel lowerCamelCase = {v: k for k, v in idalabel.items()} return config def a__ ( snake_case__ , snake_case__ , snake_case__ = False ) -> int: for i in range(config.num_hidden_layers ): # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) lowerCamelCase = state_dict.pop(F'blocks.{i}.attn.qkv.weight' ) lowerCamelCase = state_dict.pop(F'blocks.{i}.attn.qkv.bias' ) # next, add query, keys and values (in that order) to the state dict lowerCamelCase = in_proj_weight[: config.hidden_size, :] lowerCamelCase = in_proj_bias[: config.hidden_size] lowerCamelCase = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] lowerCamelCase = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] lowerCamelCase = in_proj_weight[-config.hidden_size :, :] lowerCamelCase = in_proj_bias[-config.hidden_size :] def a__ ( snake_case__ ) -> str: if "backbone" in name: lowerCamelCase = name.replace("""backbone""" , """vit""" ) if "cls_token" in name: lowerCamelCase = name.replace("""cls_token""" , """embeddings.cls_token""" ) if "det_token" in name: lowerCamelCase = name.replace("""det_token""" , """embeddings.detection_tokens""" ) if "mid_pos_embed" in name: lowerCamelCase = name.replace("""mid_pos_embed""" , """encoder.mid_position_embeddings""" ) if "pos_embed" in name: lowerCamelCase = name.replace("""pos_embed""" , """embeddings.position_embeddings""" ) if "patch_embed.proj" in name: lowerCamelCase = name.replace("""patch_embed.proj""" , """embeddings.patch_embeddings.projection""" ) if "blocks" in name: lowerCamelCase = name.replace("""blocks""" , """encoder.layer""" ) if "attn.proj" in name: lowerCamelCase = name.replace("""attn.proj""" , """attention.output.dense""" ) if "attn" in name: lowerCamelCase = name.replace("""attn""" , """attention.self""" ) if "norm1" in name: lowerCamelCase = name.replace("""norm1""" , """layernorm_before""" ) if "norm2" in name: lowerCamelCase = name.replace("""norm2""" , """layernorm_after""" ) if "mlp.fc1" in name: lowerCamelCase = name.replace("""mlp.fc1""" , """intermediate.dense""" ) if "mlp.fc2" in name: lowerCamelCase = name.replace("""mlp.fc2""" , """output.dense""" ) if "class_embed" in name: lowerCamelCase = name.replace("""class_embed""" , """class_labels_classifier""" ) if "bbox_embed" in name: lowerCamelCase = name.replace("""bbox_embed""" , """bbox_predictor""" ) if "vit.norm" in name: lowerCamelCase = name.replace("""vit.norm""" , """vit.layernorm""" ) return name def a__ ( snake_case__ , snake_case__ ) -> dict: for key in orig_state_dict.copy().keys(): lowerCamelCase = orig_state_dict.pop(snake_case__ ) if "qkv" in key: lowerCamelCase = key.split(""".""" ) lowerCamelCase = int(key_split[2] ) lowerCamelCase = model.vit.encoder.layer[layer_num].attention.attention.all_head_size if "weight" in key: lowerCamelCase = val[:dim, :] lowerCamelCase = val[ dim : dim * 2, : ] lowerCamelCase = val[-dim:, :] else: lowerCamelCase = val[:dim] lowerCamelCase = val[dim : dim * 2] lowerCamelCase = val[-dim:] else: lowerCamelCase = val return orig_state_dict def a__ ( ) -> torch.Tensor: lowerCamelCase = """http://images.cocodataset.org/val2017/000000039769.jpg""" lowerCamelCase = Image.open(requests.get(snake_case__ , stream=snake_case__ ).raw ) return im @torch.no_grad() def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ = False ) -> Optional[int]: lowerCamelCase = get_yolos_config(snake_case__ ) # load original state_dict lowerCamelCase = torch.load(snake_case__ , map_location="""cpu""" )["""model"""] # load 🤗 model lowerCamelCase = YolosForObjectDetection(snake_case__ ) model.eval() lowerCamelCase = convert_state_dict(snake_case__ , snake_case__ ) model.load_state_dict(snake_case__ ) # Check outputs on an image, prepared by YolosImageProcessor lowerCamelCase = 8_00 if yolos_name != """yolos_ti""" else 5_12 lowerCamelCase = YolosImageProcessor(format="""coco_detection""" , size=snake_case__ ) lowerCamelCase = image_processor(images=prepare_img() , return_tensors="""pt""" ) lowerCamelCase = model(**snake_case__ ) lowerCamelCase , lowerCamelCase = outputs.logits, outputs.pred_boxes lowerCamelCase , lowerCamelCase = None, None if yolos_name == "yolos_ti": lowerCamelCase = torch.tensor( [[-39.5022, -11.9820, -17.6888], [-29.9574, -9.9769, -17.7691], [-42.3281, -20.7200, -30.6294]] ) lowerCamelCase = torch.tensor( [[0.4021, 0.0836, 0.7979], [0.0184, 0.2609, 0.0364], [0.1781, 0.2004, 0.2095]] ) elif yolos_name == "yolos_s_200_pre": lowerCamelCase = torch.tensor( [[-24.0248, -10.3024, -14.8290], [-42.0392, -16.8200, -27.4334], [-27.2743, -11.8154, -18.7148]] ) lowerCamelCase = torch.tensor( [[0.2559, 0.5455, 0.4706], [0.2989, 0.7279, 0.1875], [0.7732, 0.4017, 0.4462]] ) elif yolos_name == "yolos_s_300_pre": lowerCamelCase = torch.tensor( [[-36.2220, -14.4385, -23.5457], [-35.6970, -14.7583, -21.3935], [-31.5939, -13.6042, -16.8049]] ) lowerCamelCase = torch.tensor( [[0.7614, 0.2316, 0.4728], [0.7168, 0.4495, 0.3855], [0.4996, 0.1466, 0.9996]] ) elif yolos_name == "yolos_s_dWr": lowerCamelCase = torch.tensor( [[-42.8668, -24.1049, -41.1690], [-34.7456, -14.1274, -24.9194], [-33.7898, -12.1946, -25.6495]] ) lowerCamelCase = torch.tensor( [[0.5587, 0.2773, 0.0605], [0.5004, 0.3014, 0.9994], [0.4999, 0.1548, 0.9994]] ) elif yolos_name == "yolos_base": lowerCamelCase = torch.tensor( [[-40.6064, -24.3084, -32.6447], [-55.1990, -30.7719, -35.5877], [-51.4311, -33.3507, -35.6462]] ) lowerCamelCase = torch.tensor( [[0.5555, 0.2794, 0.0655], [0.9049, 0.2664, 0.1894], [0.9183, 0.1984, 0.1635]] ) else: raise ValueError(F'Unknown yolos_name: {yolos_name}' ) assert torch.allclose(logits[0, :3, :3] , snake_case__ , atol=1E-4 ) assert torch.allclose(pred_boxes[0, :3, :3] , snake_case__ , atol=1E-4 ) Path(snake_case__ ).mkdir(exist_ok=snake_case__ ) print(F'Saving model {yolos_name} to {pytorch_dump_folder_path}' ) model.save_pretrained(snake_case__ ) print(F'Saving image processor to {pytorch_dump_folder_path}' ) image_processor.save_pretrained(snake_case__ ) if push_to_hub: lowerCamelCase = { """yolos_ti""": """yolos-tiny""", """yolos_s_200_pre""": """yolos-small""", """yolos_s_300_pre""": """yolos-small-300""", """yolos_s_dWr""": """yolos-small-dwr""", """yolos_base""": """yolos-base""", } print("""Pushing to the hub...""" ) lowerCamelCase = model_mapping[yolos_name] image_processor.push_to_hub(snake_case__ , organization="""hustvl""" ) model.push_to_hub(snake_case__ , organization="""hustvl""" ) if __name__ == "__main__": lowerCAmelCase : Optional[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( """--yolos_name""", default="""yolos_s_200_pre""", type=str, help=( """Name of the YOLOS model you'd like to convert. Should be one of 'yolos_ti', 'yolos_s_200_pre',""" """ 'yolos_s_300_pre', 'yolos_s_dWr', 'yolos_base'.""" ), ) parser.add_argument( """--checkpoint_path""", default=None, type=str, help="""Path to the original state dict (.pth file).""" ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model directory.""" ) parser.add_argument( """--push_to_hub""", action="""store_true""", help="""Whether or not to push the converted model to the 🤗 hub.""" ) lowerCAmelCase : int = parser.parse_args() convert_yolos_checkpoint(args.yolos_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub)
291
"""simple docstring""" 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__ ( snake_case__ , snake_case__ , snake_case__ ) -> List[Any]: return params[F'{prefix}/{prefix}/relpos_bias/rel_embedding'][:, i, :] def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__="attention" ) -> List[Any]: lowerCamelCase = lowerCamelCase = np.ascontiguousarray(params[F'{prefix}/{prefix}/{layer_name}/key/kernel'][:, i, :, :] ) lowerCamelCase = k_tmp.reshape(k_tmp.shape[0] , k_tmp.shape[1] * k_tmp.shape[2] ) lowerCamelCase = np.ascontiguousarray(params[F'{prefix}/{prefix}/{layer_name}/out/kernel'][:, i, :, :] ) lowerCamelCase = o_tmp.reshape(o_tmp.shape[0] * o_tmp.shape[1] , o_tmp.shape[2] ) lowerCamelCase = np.ascontiguousarray(params[F'{prefix}/{prefix}/{layer_name}/query/kernel'][:, i, :, :] ) lowerCamelCase = q_tmp.reshape(q_tmp.shape[0] , q_tmp.shape[1] * q_tmp.shape[2] ) lowerCamelCase = np.ascontiguousarray(params[F'{prefix}/{prefix}/{layer_name}/value/kernel'][:, i, :, :] ) lowerCamelCase = v_tmp.reshape(v_tmp.shape[0] , v_tmp.shape[1] * v_tmp.shape[2] ) return k, o, q, v def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__=False ) -> List[str]: if split_mlp_wi: lowerCamelCase = params[F'{prefix}/{prefix}/mlp/wi_0/kernel'][:, i, :] lowerCamelCase = params[F'{prefix}/{prefix}/mlp/wi_1/kernel'][:, i, :] lowerCamelCase = (wi_a, wi_a) else: lowerCamelCase = params[F'{prefix}/{prefix}/mlp/wi/kernel'][:, i, :] lowerCamelCase = params[F'{prefix}/{prefix}/mlp/wo/kernel'][:, i, :] return wi, wo def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ ) -> Tuple: return params[F'{prefix}/{prefix}/{layer_name}/scale'][:, i] def a__ ( snake_case__ , *, snake_case__ , snake_case__ , snake_case__ = False ) -> Dict: lowerCamelCase = traverse_util.flatten_dict(variables["""target"""] ) lowerCamelCase = {"""/""".join(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 lowerCamelCase = """encoder/encoder/mlp/wi_0/kernel""" in old print("""Split MLP:""" , snake_case__ ) lowerCamelCase = collections.OrderedDict() # Shared embeddings. lowerCamelCase = old["""token_embedder/embedding"""] # Encoder. for i in range(snake_case__ ): # Block i, layer 0 (Self Attention). lowerCamelCase = tax_layer_norm_lookup(snake_case__ , snake_case__ , """encoder""" , """pre_attention_layer_norm""" ) lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase = tax_attention_lookup(snake_case__ , snake_case__ , """encoder""" , """attention""" ) lowerCamelCase = layer_norm lowerCamelCase = k.T lowerCamelCase = o.T lowerCamelCase = q.T lowerCamelCase = v.T # Block i, layer 1 (MLP). lowerCamelCase = tax_layer_norm_lookup(snake_case__ , snake_case__ , """encoder""" , """pre_mlp_layer_norm""" ) lowerCamelCase , lowerCamelCase = tax_mlp_lookup(snake_case__ , snake_case__ , """encoder""" , snake_case__ ) lowerCamelCase = layer_norm if split_mlp_wi: lowerCamelCase = wi[0].T lowerCamelCase = wi[1].T else: lowerCamelCase = wi.T lowerCamelCase = wo.T if scalable_attention: # convert the rel_embedding of each layer lowerCamelCase = tax_relpos_bias_lookup( snake_case__ , snake_case__ , """encoder""" ).T lowerCamelCase = old["""encoder/encoder_norm/scale"""] if not scalable_attention: lowerCamelCase = tax_relpos_bias_lookup( snake_case__ , 0 , """encoder""" ).T lowerCamelCase = tax_relpos_bias_lookup( snake_case__ , 0 , """decoder""" ).T if not is_encoder_only: # Decoder. for i in range(snake_case__ ): # Block i, layer 0 (Self Attention). lowerCamelCase = tax_layer_norm_lookup(snake_case__ , snake_case__ , """decoder""" , """pre_self_attention_layer_norm""" ) lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase = tax_attention_lookup(snake_case__ , snake_case__ , """decoder""" , """self_attention""" ) lowerCamelCase = layer_norm lowerCamelCase = k.T lowerCamelCase = o.T lowerCamelCase = q.T lowerCamelCase = v.T # Block i, layer 1 (Cross Attention). lowerCamelCase = tax_layer_norm_lookup(snake_case__ , snake_case__ , """decoder""" , """pre_cross_attention_layer_norm""" ) lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase = tax_attention_lookup(snake_case__ , snake_case__ , """decoder""" , """encoder_decoder_attention""" ) lowerCamelCase = layer_norm lowerCamelCase = k.T lowerCamelCase = o.T lowerCamelCase = q.T lowerCamelCase = v.T # Block i, layer 2 (MLP). lowerCamelCase = tax_layer_norm_lookup(snake_case__ , snake_case__ , """decoder""" , """pre_mlp_layer_norm""" ) lowerCamelCase , lowerCamelCase = tax_mlp_lookup(snake_case__ , snake_case__ , """decoder""" , snake_case__ ) lowerCamelCase = layer_norm if split_mlp_wi: lowerCamelCase = wi[0].T lowerCamelCase = wi[1].T else: lowerCamelCase = wi.T lowerCamelCase = wo.T if scalable_attention: # convert the rel_embedding of each layer lowerCamelCase = tax_relpos_bias_lookup(snake_case__ , snake_case__ , """decoder""" ).T lowerCamelCase = 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: lowerCamelCase = old["""decoder/logits_dense/kernel"""].T return new def a__ ( snake_case__ , snake_case__ ) -> Optional[int]: lowerCamelCase = 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: lowerCamelCase = state_dict["""shared.weight"""] if not is_encoder_only: if "decoder.embed_tokens.weight" not in state_dict: lowerCamelCase = 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.""" ) lowerCamelCase = state_dict["""shared.weight"""] return state_dict def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ ) -> List[Any]: lowerCamelCase = checkpoints.load_tax_checkpoint(snake_case__ ) lowerCamelCase = convert_tax_to_pytorch( snake_case__ , num_layers=config.num_layers , is_encoder_only=snake_case__ , scalable_attention=snake_case__ ) lowerCamelCase = make_state_dict(snake_case__ , snake_case__ ) model.load_state_dict(snake_case__ , strict=snake_case__ ) def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ = False , snake_case__ = False , ) -> str: lowerCamelCase = MTaConfig.from_json_file(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: lowerCamelCase = UMTaEncoderModel(snake_case__ ) else: lowerCamelCase = UMTaForConditionalGeneration(snake_case__ ) # Load weights from tf checkpoint load_tax_weights_in_ta(snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ ) # Save pytorch-model print(F'Save PyTorch model to {pytorch_dump_path}' ) model.save_pretrained(snake_case__ ) # Verify that we can load the checkpoint. model.from_pretrained(snake_case__ ) print("""Done""" ) if __name__ == "__main__": lowerCAmelCase : 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, ) lowerCAmelCase : 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, )
291
1
"""simple docstring""" lowerCAmelCase : Optional[int] = {0: [2, 3], 1: [0], 2: [1], 3: [4], 4: []} lowerCAmelCase : List[Any] = {0: [1, 2, 3], 1: [2], 2: [0], 3: [4], 4: [5], 5: [3]} def a__ ( snake_case__ , snake_case__ , snake_case__ ) -> list[int]: lowerCamelCase = True lowerCamelCase = [] for neighbour in graph[vert]: if not visited[neighbour]: order += topology_sort(snake_case__ , snake_case__ , snake_case__ ) order.append(snake_case__ ) return order def a__ ( snake_case__ , snake_case__ , snake_case__ ) -> list[int]: lowerCamelCase = True lowerCamelCase = [vert] for neighbour in reversed_graph[vert]: if not visited[neighbour]: component += find_components(snake_case__ , snake_case__ , snake_case__ ) return component def a__ ( snake_case__ ) -> list[list[int]]: lowerCamelCase = len(snake_case__ ) * [False] lowerCamelCase = {vert: [] for vert in range(len(snake_case__ ) )} for vert, neighbours in graph.items(): for neighbour in neighbours: reversed_graph[neighbour].append(snake_case__ ) lowerCamelCase = [] for i, was_visited in enumerate(snake_case__ ): if not was_visited: order += topology_sort(snake_case__ , snake_case__ , snake_case__ ) lowerCamelCase = [] lowerCamelCase = len(snake_case__ ) * [False] for i in range(len(snake_case__ ) ): lowerCamelCase = order[len(snake_case__ ) - i - 1] if not visited[vert]: lowerCamelCase = find_components(snake_case__ , snake_case__ , snake_case__ ) components_list.append(snake_case__ ) return components_list
291
"""simple docstring""" from __future__ import annotations def a__ ( snake_case__ , snake_case__ ) -> bool: if len(snake_case__ ) == 0: return False lowerCamelCase = len(snake_case__ ) // 2 if a_list[midpoint] == item: return True if item < a_list[midpoint]: return binary_search(a_list[:midpoint] , snake_case__ ) else: return binary_search(a_list[midpoint + 1 :] , snake_case__ ) if __name__ == "__main__": lowerCAmelCase : List[Any] = input("""Enter numbers separated by comma:\n""").strip() lowerCAmelCase : Optional[Any] = [int(item.strip()) for item in user_input.split(""",""")] lowerCAmelCase : Optional[int] = int(input("""Enter the number to be found in the list:\n""").strip()) lowerCAmelCase : Union[str, Any] = """""" if binary_search(sequence, target) else """not """ print(F"""{target} was {not_str}found in {sequence}""")
291
1
"""simple docstring""" import argparse import json import os import fairseq import torch from torch import nn from transformers import ( SpeechaTextaConfig, SpeechaTextaForCausalLM, SpeechaTextaTokenizer, SpeechEncoderDecoderConfig, SpeechEncoderDecoderModel, WavaVecaConfig, WavaVecaFeatureExtractor, WavaVecaModel, logging, ) logging.set_verbosity_info() lowerCAmelCase : int = logging.get_logger(__name__) lowerCAmelCase : List[str] = { """post_extract_proj""": """feature_projection.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.layer_norm""": """encoder.layer_norm""", """w2v_model.layer_norm""": """feature_projection.layer_norm""", """quantizer.weight_proj""": """quantizer.weight_proj""", """quantizer.vars""": """quantizer.codevectors""", """project_q""": """project_q""", """final_proj""": """project_hid""", """w2v_encoder.proj""": """lm_head""", """mask_emb""": """masked_spec_embed""", } lowerCAmelCase : Optional[Any] = [ """lm_head""", """quantizer.weight_proj""", """quantizer.codevectors""", """project_q""", """project_hid""", ] def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ ) -> Optional[int]: for attribute in key.split(""".""" ): lowerCamelCase = getattr(snake_case__ , snake_case__ ) if weight_type is not None: lowerCamelCase = getattr(snake_case__ , snake_case__ ).shape else: lowerCamelCase = 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": lowerCamelCase = value elif weight_type == "weight_g": lowerCamelCase = value elif weight_type == "weight_v": lowerCamelCase = value elif weight_type == "bias": lowerCamelCase = value else: lowerCamelCase = value logger.info(F'{key + "." + weight_type if weight_type is not None else ""} was initialized from {full_name}.' ) def a__ ( snake_case__ , snake_case__ ) -> int: lowerCamelCase = [] lowerCamelCase = fairseq_model.state_dict() lowerCamelCase = hf_model.feature_extractor # if encoder has different dim to decoder -> use proj_weight lowerCamelCase = None for name, value in fairseq_dict.items(): lowerCamelCase = False if "conv_layers" in name: load_conv_layer( snake_case__ , snake_case__ , snake_case__ , snake_case__ , hf_model.config.feat_extract_norm == """group""" , ) lowerCamelCase = True elif name.split(""".""" )[0] == "proj": lowerCamelCase = fairseq_model.proj lowerCamelCase = True else: for key, mapped_key in MAPPING.items(): if key in name or key.split("""w2v_model.""" )[-1] == name.split(""".""" )[0]: lowerCamelCase = True if "*" in mapped_key: lowerCamelCase = name.split(snake_case__ )[0].split(""".""" )[-2] lowerCamelCase = mapped_key.replace("""*""" , snake_case__ ) if "weight_g" in name: lowerCamelCase = """weight_g""" elif "weight_v" in name: lowerCamelCase = """weight_v""" elif "bias" in name: lowerCamelCase = """bias""" elif "weight" in name: lowerCamelCase = """weight""" else: lowerCamelCase = None set_recursively(snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ ) continue if not is_used: unused_weights.append(snake_case__ ) logger.warning(F'Unused weights: {unused_weights}' ) return proj_weight def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ ) -> Dict: lowerCamelCase = full_name.split("""conv_layers.""" )[-1] lowerCamelCase = name.split(""".""" ) lowerCamelCase = int(items[0] ) lowerCamelCase = 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.' ) lowerCamelCase = 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.' ) lowerCamelCase = 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." ) lowerCamelCase = 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.' ) lowerCamelCase = value logger.info(F'Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.' ) else: unused_weights.append(snake_case__ ) def a__ ( snake_case__ ) -> str: lowerCamelCase , lowerCamelCase = emb.weight.shape lowerCamelCase = nn.Linear(snake_case__ , snake_case__ , bias=snake_case__ ) lowerCamelCase = emb.weight.data return lin_layer def a__ ( snake_case__ ) -> Optional[int]: with open(snake_case__ , """r""" , encoding="""utf-8""" ) as f: lowerCamelCase = f.readlines() lowerCamelCase = [line.split(""" """ )[0] for line in lines] lowerCamelCase = len(snake_case__ ) lowerCamelCase = { """<s>""": 0, """<pad>""": 1, """</s>""": 2, """<unk>""": 3, } vocab_dict.update(dict(zip(snake_case__ , range(4 , num_words + 4 ) ) ) ) return vocab_dict @torch.no_grad() def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ) -> str: lowerCamelCase = WavaVecaConfig.from_pretrained(snake_case__ ) lowerCamelCase = SpeechaTextaConfig.from_pretrained( snake_case__ , vocab_size=snake_case__ , decoder_layers=snake_case__ , do_stable_layer_norm=snake_case__ ) lowerCamelCase = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=1_60_00 , padding_value=0 , do_normalize=snake_case__ , return_attention_mask=snake_case__ , ) lowerCamelCase , lowerCamelCase , lowerCamelCase = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={"""data""": """/""".join(dict_path.split("""/""" )[:-1] )} ) lowerCamelCase = model[0].eval() # set weights for wav2vec2 encoder lowerCamelCase = WavaVecaModel(snake_case__ ) lowerCamelCase = recursively_load_weights_wavaveca(model.encoder , snake_case__ ) lowerCamelCase = SpeechaTextaForCausalLM(snake_case__ ) lowerCamelCase , lowerCamelCase = hf_decoder.model.decoder.load_state_dict(model.decoder.state_dict() , strict=snake_case__ ) # set output linear layer unexpected_keys.remove("""embed_out""" ) lowerCamelCase = nn.Parameter(model.decoder.embed_out.detach() ) # layer norm is init to identity matrix so leaving it is fine logger.warning(F'The following keys are missing when loading the decoder weights: {missing_keys}' ) logger.warning(F'The following keys are unexpected when loading the decoder weights: {unexpected_keys}' ) lowerCamelCase = SpeechEncoderDecoderModel(encoder=snake_case__ , decoder=snake_case__ ) lowerCamelCase = False # add projection layer lowerCamelCase = nn.Parameter(projection_layer.weight ) lowerCamelCase = nn.Parameter(projection_layer.bias ) lowerCamelCase = create_vocab_dict(snake_case__ ) with open(os.path.join(snake_case__ , """vocab.json""" ) , """w""" ) as fp: json.dump(snake_case__ , snake_case__ ) lowerCamelCase = SpeechaTextaTokenizer(os.path.join(snake_case__ , """vocab.json""" ) ) tokenizer.save_pretrained(snake_case__ ) lowerCamelCase = hf_wavavec.config.to_dict() lowerCamelCase = tokenizer.pad_token_id lowerCamelCase = tokenizer.bos_token_id lowerCamelCase = tokenizer.eos_token_id lowerCamelCase = """speech_to_text_2""" lowerCamelCase = """wav2vec2""" lowerCamelCase = SpeechEncoderDecoderConfig.from_dict(snake_case__ ) hf_wavavec.save_pretrained(snake_case__ ) feature_extractor.save_pretrained(snake_case__ ) if __name__ == "__main__": lowerCAmelCase : Optional[int] = 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( """--encoder_config_path""", default="""facebook/wav2vec2-large-lv60""", type=str, help="""Path to hf encoder wav2vec2 checkpoint config""", ) parser.add_argument( """--decoder_config_path""", default="""facebook/s2t-small-mustc-en-fr-st""", type=str, help="""Path to hf decoder s2t checkpoint config""", ) parser.add_argument("""--vocab_size""", default=1_0224, type=int, help="""Vocab size of decoder""") parser.add_argument("""--num_decoder_layers""", default=7, type=int, help="""Number of decoder layers""") lowerCAmelCase : int = parser.parse_args() convert_wavaveca_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.dict_path, encoder_config_path=args.encoder_config_path, decoder_config_path=args.decoder_config_path, vocab_size=args.vocab_size, num_decoder_layers=args.num_decoder_layers, )
291
"""simple docstring""" def a__ ( snake_case__ ) -> list: if len(snake_case__ ) < 2: return collection def circle_sort_util(snake_case__ , snake_case__ , snake_case__ ) -> bool: lowerCamelCase = False if low == high: return swapped lowerCamelCase = low lowerCamelCase = high while left < right: if collection[left] > collection[right]: lowerCamelCase , lowerCamelCase = ( collection[right], collection[left], ) lowerCamelCase = True left += 1 right -= 1 if left == right and collection[left] > collection[right + 1]: lowerCamelCase , lowerCamelCase = ( collection[right + 1], collection[left], ) lowerCamelCase = True lowerCamelCase = low + int((high - low) / 2 ) lowerCamelCase = circle_sort_util(snake_case__ , snake_case__ , snake_case__ ) lowerCamelCase = circle_sort_util(snake_case__ , mid + 1 , snake_case__ ) return swapped or left_swap or right_swap lowerCamelCase = True while is_not_sorted is True: lowerCamelCase = circle_sort_util(snake_case__ , 0 , len(snake_case__ ) - 1 ) return collection if __name__ == "__main__": lowerCAmelCase : Tuple = input("""Enter numbers separated by a comma:\n""").strip() lowerCAmelCase : List[Any] = [int(item) for item in user_input.split(""",""")] print(circle_sort(unsorted))
291
1
"""simple docstring""" import json import os import unittest from transformers.models.xlm.tokenization_xlm import VOCAB_FILES_NAMES, XLMTokenizer from transformers.testing_utils import slow from ...test_tokenization_common import TokenizerTesterMixin class __magic_name__ ( UpperCAmelCase__ , unittest.TestCase ): '''simple docstring''' __UpperCamelCase = XLMTokenizer __UpperCamelCase = False def _lowerCAmelCase ( self ): """simple docstring""" super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt lowerCamelCase = [ """l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """w</w>""", """r</w>""", """t</w>""", """lo""", """low""", """er</w>""", """low</w>""", """lowest</w>""", """newer</w>""", """wider</w>""", """<unk>""", ] lowerCamelCase = dict(zip(_a , range(len(_a ) ) ) ) lowerCamelCase = ["""l o 123""", """lo w 1456""", """e r</w> 1789""", """"""] lowerCamelCase = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""vocab_file"""] ) lowerCamelCase = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""merges_file"""] ) with open(self.vocab_file , """w""" ) as fp: fp.write(json.dumps(_a ) ) with open(self.merges_file , """w""" ) as fp: fp.write("""\n""".join(_a ) ) def _lowerCAmelCase ( self , _a ): """simple docstring""" lowerCamelCase = """lower newer""" lowerCamelCase = """lower newer""" return input_text, output_text def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = XLMTokenizer(self.vocab_file , self.merges_file ) lowerCamelCase = """lower""" lowerCamelCase = ["""low""", """er</w>"""] lowerCamelCase = tokenizer.tokenize(_a ) self.assertListEqual(_a , _a ) lowerCamelCase = tokens + ["""<unk>"""] lowerCamelCase = [14, 15, 20] self.assertListEqual(tokenizer.convert_tokens_to_ids(_a ) , _a ) @slow def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = XLMTokenizer.from_pretrained("""xlm-mlm-en-2048""" ) lowerCamelCase = tokenizer.encode("""sequence builders""" , add_special_tokens=_a ) lowerCamelCase = tokenizer.encode("""multi-sequence build""" , add_special_tokens=_a ) lowerCamelCase = tokenizer.build_inputs_with_special_tokens(_a ) lowerCamelCase = tokenizer.build_inputs_with_special_tokens(_a , _a ) assert encoded_sentence == [0] + text + [1] assert encoded_pair == [0] + text + [1] + text_a + [1]
291
"""simple docstring""" from collections.abc import Generator def a__ ( ) -> Generator[int, None, None]: lowerCamelCase , lowerCamelCase = 0, 1 while True: lowerCamelCase , lowerCamelCase = b, a + b yield b def a__ ( snake_case__ = 10_00 ) -> int: lowerCamelCase = 1 lowerCamelCase = fibonacci_generator() while len(str(next(snake_case__ ) ) ) < n: answer += 1 return answer + 1 if __name__ == "__main__": print(solution(int(str(input()).strip())))
291
1
"""simple docstring""" def a__ ( snake_case__ ) -> bool: return number & 1 == 0 if __name__ == "__main__": import doctest doctest.testmod()
291
"""simple docstring""" from math import ceil from typing import List, Optional, Union import numpy as np from ...audio_utils import mel_filter_bank, spectrogram, window_function from ...feature_extraction_sequence_utils import BatchFeature, SequenceFeatureExtractor from ...utils import TensorType, logging lowerCAmelCase : List[str] = logging.get_logger(__name__) class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' __UpperCamelCase = ["audio_values", "audio_mask"] def __init__( self , _a=2_048 , _a=1 , _a=[16, 16] , _a=128 , _a=44_100 , _a=86 , _a=2_048 , _a=0.0 , **_a , ): """simple docstring""" super().__init__( feature_size=_a , sampling_rate=_a , padding_value=_a , **_a , ) lowerCamelCase = spectrogram_length lowerCamelCase = num_channels lowerCamelCase = patch_size lowerCamelCase = feature_size // self.patch_size[1] lowerCamelCase = n_fft lowerCamelCase = sampling_rate // hop_length_to_sampling_rate lowerCamelCase = sampling_rate lowerCamelCase = padding_value lowerCamelCase = mel_filter_bank( num_frequency_bins=1 + n_fft // 2 , num_mel_filters=_a , min_frequency=0.0 , max_frequency=22_050.0 , sampling_rate=_a , norm="""slaney""" , mel_scale="""slaney""" , ).T def _lowerCAmelCase ( self , _a ): """simple docstring""" lowerCamelCase = spectrogram( _a , window_function(self.n_fft , """hann""" ) , frame_length=self.n_fft , hop_length=self.hop_length , power=2.0 , mel_filters=self.mel_filters.T , log_mel="""dB""" , db_range=80.0 , ) lowerCamelCase = log_spec[:, :-1] lowerCamelCase = log_spec - 20.0 lowerCamelCase = np.clip(log_spec / 40.0 , -2.0 , 0.0 ) + 1.0 return log_spec def __call__( self , _a , _a = None , _a = True , _a = None , _a = False , _a = False , **_a , ): """simple docstring""" if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( """This feature extractor is set to support sampling rate""" f' of {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled' f' 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.""" ) lowerCamelCase = isinstance(_a , np.ndarray ) and len(raw_speech.shape ) > 1 if is_batched_numpy and len(raw_speech.shape ) > 2: raise ValueError(f'Only mono-channel audio is supported for input to {self}' ) lowerCamelCase = is_batched_numpy or ( isinstance(_a , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: lowerCamelCase = [np.asarray([speech] , dtype=np.floataa ).T for speech in raw_speech] elif not is_batched and not isinstance(_a , np.ndarray ): lowerCamelCase = np.asarray(_a , dtype=np.floataa ) elif isinstance(_a , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): lowerCamelCase = raw_speech.astype(np.floataa ) # always return batch if not is_batched: lowerCamelCase = [np.asarray([raw_speech] ).T] # Convert audio signals to log mel spectrograms, truncate by time axis lowerCamelCase = [ self._np_extract_fbank_features(waveform.squeeze() ).T[: self.spectrogram_length] for waveform in raw_speech ] if isinstance(audio_features[0] , _a ): lowerCamelCase = [np.asarray(_a , dtype=np.floataa ) for feature in audio_features] # Create audio attention mask lowerCamelCase = max( [ceil(feature.shape[0] / self.patch_size[0] ) * self.freq_len for feature in audio_features] ) # The maximum number of audio patches in a batch if return_attention_mask: lowerCamelCase = [ (ceil(feature.shape[0] / self.patch_size[0] ) * self.freq_len) * [1] + (max_patch_len - ceil(feature.shape[0] / self.patch_size[0] ) * self.freq_len) * [0] for feature in audio_features ] lowerCamelCase = np.array(_a ).astype(np.floataa ) # convert into correct format for padding lowerCamelCase = max_patch_len // self.freq_len * self.patch_size[0] # The maximum audio size in a batch lowerCamelCase = np.ones([len(_a ), 1, max_time_len, self.feature_size] ).astype(np.floataa ) lowerCamelCase = padded_audio_features * self.padding_value for i in range(len(_a ) ): lowerCamelCase = audio_features[i] lowerCamelCase = feature # return as BatchFeature if return_attention_mask: lowerCamelCase = {"""audio_values""": padded_audio_features, """audio_mask""": audio_mask} else: lowerCamelCase = {"""audio_values""": padded_audio_features} lowerCamelCase = BatchFeature(data=_a , tensor_type=_a ) return encoded_inputs
291
1
"""simple docstring""" from __future__ import annotations import math import numpy as np from numpy.linalg import norm def a__ ( snake_case__ , snake_case__ ) -> float: return math.sqrt(sum(pow(a - b , 2 ) for a, b in zip(snake_case__ , snake_case__ ) ) ) def a__ ( snake_case__ , snake_case__ ) -> list[list[list[float] | float]]: if dataset.ndim != value_array.ndim: lowerCamelCase = ( """Wrong input data's dimensions... """ F'dataset : {dataset.ndim}, value_array : {value_array.ndim}' ) raise ValueError(snake_case__ ) try: if dataset.shape[1] != value_array.shape[1]: lowerCamelCase = ( """Wrong input data's shape... """ F'dataset : {dataset.shape[1]}, value_array : {value_array.shape[1]}' ) raise ValueError(snake_case__ ) except IndexError: if dataset.ndim != value_array.ndim: raise TypeError("""Wrong shape""" ) if dataset.dtype != value_array.dtype: lowerCamelCase = ( """Input data have different datatype... """ F'dataset : {dataset.dtype}, value_array : {value_array.dtype}' ) raise TypeError(snake_case__ ) lowerCamelCase = [] for value in value_array: lowerCamelCase = euclidean(snake_case__ , dataset[0] ) lowerCamelCase = dataset[0].tolist() for dataset_value in dataset[1:]: lowerCamelCase = euclidean(snake_case__ , snake_case__ ) if dist > temp_dist: lowerCamelCase = temp_dist lowerCamelCase = dataset_value.tolist() answer.append([vector, dist] ) return answer def a__ ( snake_case__ , snake_case__ ) -> float: return np.dot(snake_case__ , snake_case__ ) / (norm(snake_case__ ) * norm(snake_case__ )) if __name__ == "__main__": import doctest doctest.testmod()
291
"""simple docstring""" from math import ceil def a__ ( snake_case__ , snake_case__ ) -> Optional[int]: lowerCamelCase = list(range(0 , snake_case__ ) ) lowerCamelCase = [item for sublist in list(device_map.values() ) for item in sublist] # Duplicate check lowerCamelCase = [] for i in device_map_blocks: if device_map_blocks.count(snake_case__ ) > 1 and i not in duplicate_blocks: duplicate_blocks.append(snake_case__ ) # Missing blocks lowerCamelCase = [i for i in blocks if i not in device_map_blocks] lowerCamelCase = [i for i in device_map_blocks if i not in blocks] if len(snake_case__ ) != 0: raise ValueError( """Duplicate attention blocks specified in device_map. Attention blocks must be specified to one device.""" """ These attention blocks were specified more than once: """ + str(snake_case__ ) ) if len(snake_case__ ) != 0: raise ValueError( """There are attention blocks for this model that are not specified in the device_map. Add these attention """ """blocks to a device on the device_map: """ + str(snake_case__ ) ) if len(snake_case__ ) != 0: raise ValueError( """The device_map contains more attention blocks than this model has. Remove these from the device_map:""" + str(snake_case__ ) ) def a__ ( snake_case__ , snake_case__ ) -> List[Any]: lowerCamelCase = list(range(snake_case__ ) ) lowerCamelCase = int(ceil(n_layers / len(snake_case__ ) ) ) lowerCamelCase = [layers[i : i + n_blocks] for i in range(0 , snake_case__ , snake_case__ )] return dict(zip(snake_case__ , snake_case__ ) )
291
1
"""simple docstring""" from typing import List, Optional, Union import numpy as np import PIL import torch from PIL import Image from ...models import UNetaDConditionModel, VQModel from ...pipelines import DiffusionPipeline from ...pipelines.pipeline_utils import ImagePipelineOutput from ...schedulers import DDPMScheduler from ...utils import ( is_accelerate_available, is_accelerate_version, logging, randn_tensor, replace_example_docstring, ) lowerCAmelCase : str = logging.get_logger(__name__) # pylint: disable=invalid-name lowerCAmelCase : str = """ Examples: ```py >>> from diffusers import KandinskyV22Img2ImgPipeline, KandinskyV22PriorPipeline >>> from diffusers.utils import load_image >>> import torch >>> pipe_prior = KandinskyV22PriorPipeline.from_pretrained( ... \"kandinsky-community/kandinsky-2-2-prior\", torch_dtype=torch.float16 ... ) >>> pipe_prior.to(\"cuda\") >>> prompt = \"A red cartoon frog, 4k\" >>> image_emb, zero_image_emb = pipe_prior(prompt, return_dict=False) >>> pipe = KandinskyV22Img2ImgPipeline.from_pretrained( ... \"kandinsky-community/kandinsky-2-2-decoder\", torch_dtype=torch.float16 ... ) >>> pipe.to(\"cuda\") >>> init_image = load_image( ... \"https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main\" ... \"/kandinsky/frog.png\" ... ) >>> image = pipe( ... image=init_image, ... image_embeds=image_emb, ... negative_image_embeds=zero_image_emb, ... height=768, ... width=768, ... num_inference_steps=100, ... strength=0.2, ... ).images >>> image[0].save(\"red_frog.png\") ``` """ def a__ ( snake_case__ , snake_case__ , snake_case__=8 ) -> Optional[Any]: lowerCamelCase = height // scale_factor**2 if height % scale_factor**2 != 0: new_height += 1 lowerCamelCase = width // scale_factor**2 if width % scale_factor**2 != 0: new_width += 1 return new_height * scale_factor, new_width * scale_factor def a__ ( snake_case__ , snake_case__=5_12 , snake_case__=5_12 ) -> int: lowerCamelCase = pil_image.resize((w, h) , resample=Image.BICUBIC , reducing_gap=1 ) lowerCamelCase = np.array(pil_image.convert("""RGB""" ) ) lowerCamelCase = arr.astype(np.floataa ) / 127.5 - 1 lowerCamelCase = np.transpose(snake_case__ , [2, 0, 1] ) lowerCamelCase = torch.from_numpy(snake_case__ ).unsqueeze(0 ) return image class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' def __init__( self , _a , _a , _a , ): """simple docstring""" super().__init__() self.register_modules( unet=_a , scheduler=_a , movq=_a , ) lowerCamelCase = 2 ** (len(self.movq.config.block_out_channels ) - 1) def _lowerCAmelCase ( self , _a , _a , _a ): """simple docstring""" # get the original timestep using init_timestep lowerCamelCase = min(int(num_inference_steps * strength ) , _a ) lowerCamelCase = max(num_inference_steps - init_timestep , 0 ) lowerCamelCase = self.scheduler.timesteps[t_start:] return timesteps, num_inference_steps - t_start def _lowerCAmelCase ( self , _a , _a , _a , _a , _a , _a , _a=None ): """simple docstring""" if not isinstance(_a , (torch.Tensor, PIL.Image.Image, list) ): raise ValueError( f'`image` has to be of type `torch.Tensor`, `PIL.Image.Image` or list but is {type(_a )}' ) lowerCamelCase = image.to(device=_a , dtype=_a ) lowerCamelCase = batch_size * num_images_per_prompt if image.shape[1] == 4: lowerCamelCase = image else: if isinstance(_a , _a ) and len(_a ) != batch_size: raise ValueError( f'You have passed a list of generators of length {len(_a )}, but requested an effective batch' f' size of {batch_size}. Make sure the batch size matches the length of the generators.' ) elif isinstance(_a , _a ): lowerCamelCase = [ self.movq.encode(image[i : i + 1] ).latent_dist.sample(generator[i] ) for i in range(_a ) ] lowerCamelCase = torch.cat(_a , dim=0 ) else: lowerCamelCase = self.movq.encode(_a ).latent_dist.sample(_a ) lowerCamelCase = self.movq.config.scaling_factor * init_latents lowerCamelCase = torch.cat([init_latents] , dim=0 ) lowerCamelCase = init_latents.shape lowerCamelCase = randn_tensor(_a , generator=_a , device=_a , dtype=_a ) # get latents lowerCamelCase = self.scheduler.add_noise(_a , _a , _a ) lowerCamelCase = init_latents return latents def _lowerCAmelCase ( self , _a=0 ): """simple docstring""" if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError("""Please install accelerate via `pip install accelerate`""" ) lowerCamelCase = torch.device(f'cuda:{gpu_id}' ) lowerCamelCase = [ self.unet, self.movq, ] for cpu_offloaded_model in models: if cpu_offloaded_model is not None: cpu_offload(_a , _a ) def _lowerCAmelCase ( self , _a=0 ): """simple docstring""" if is_accelerate_available() and is_accelerate_version(""">=""" , """0.17.0.dev0""" ): from accelerate import cpu_offload_with_hook else: raise ImportError("""`enable_model_cpu_offload` requires `accelerate v0.17.0` or higher.""" ) lowerCamelCase = torch.device(f'cuda:{gpu_id}' ) if self.device.type != "cpu": self.to("""cpu""" , silence_dtype_warnings=_a ) torch.cuda.empty_cache() # otherwise we don't see the memory savings (but they probably exist) lowerCamelCase = None for cpu_offloaded_model in [self.unet, self.movq]: lowerCamelCase , lowerCamelCase = cpu_offload_with_hook(_a , _a , prev_module_hook=_a ) # We'll offload the last model manually. lowerCamelCase = hook @property # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device def _lowerCAmelCase ( self ): """simple docstring""" if not hasattr(self.unet , """_hf_hook""" ): return self.device for module in self.unet.modules(): if ( hasattr(_a , """_hf_hook""" ) and hasattr(module._hf_hook , """execution_device""" ) and module._hf_hook.execution_device is not None ): return torch.device(module._hf_hook.execution_device ) return self.device @torch.no_grad() @replace_example_docstring(_a ) def __call__( self , _a , _a , _a , _a = 512 , _a = 512 , _a = 100 , _a = 4.0 , _a = 0.3 , _a = 1 , _a = None , _a = "pil" , _a = True , ): """simple docstring""" lowerCamelCase = self._execution_device lowerCamelCase = guidance_scale > 1.0 if isinstance(_a , _a ): lowerCamelCase = torch.cat(_a , dim=0 ) lowerCamelCase = image_embeds.shape[0] if isinstance(_a , _a ): lowerCamelCase = torch.cat(_a , dim=0 ) if do_classifier_free_guidance: lowerCamelCase = image_embeds.repeat_interleave(_a , dim=0 ) lowerCamelCase = negative_image_embeds.repeat_interleave(_a , dim=0 ) lowerCamelCase = torch.cat([negative_image_embeds, image_embeds] , dim=0 ).to(dtype=self.unet.dtype , device=_a ) if not isinstance(_a , _a ): lowerCamelCase = [image] if not all(isinstance(_a , (PIL.Image.Image, torch.Tensor) ) for i in image ): raise ValueError( f'Input is in incorrect format: {[type(_a ) for i in image]}. Currently, we only support PIL image and pytorch tensor' ) lowerCamelCase = torch.cat([prepare_image(_a , _a , _a ) for i in image] , dim=0 ) lowerCamelCase = image.to(dtype=image_embeds.dtype , device=_a ) lowerCamelCase = self.movq.encode(_a )["""latents"""] lowerCamelCase = latents.repeat_interleave(_a , dim=0 ) self.scheduler.set_timesteps(_a , device=_a ) lowerCamelCase , lowerCamelCase = self.get_timesteps(_a , _a , _a ) lowerCamelCase = timesteps[:1].repeat(batch_size * num_images_per_prompt ) lowerCamelCase , lowerCamelCase = downscale_height_and_width(_a , _a , self.movq_scale_factor ) lowerCamelCase = self.prepare_latents( _a , _a , _a , _a , image_embeds.dtype , _a , _a ) for i, t in enumerate(self.progress_bar(_a ) ): # expand the latents if we are doing classifier free guidance lowerCamelCase = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents lowerCamelCase = {"""image_embeds""": image_embeds} lowerCamelCase = self.unet( sample=_a , timestep=_a , encoder_hidden_states=_a , added_cond_kwargs=_a , return_dict=_a , )[0] if do_classifier_free_guidance: lowerCamelCase , lowerCamelCase = noise_pred.split(latents.shape[1] , dim=1 ) lowerCamelCase , lowerCamelCase = noise_pred.chunk(2 ) lowerCamelCase , lowerCamelCase = variance_pred.chunk(2 ) lowerCamelCase = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) lowerCamelCase = torch.cat([noise_pred, variance_pred_text] , dim=1 ) if not ( hasattr(self.scheduler.config , """variance_type""" ) and self.scheduler.config.variance_type in ["learned", "learned_range"] ): lowerCamelCase , lowerCamelCase = noise_pred.split(latents.shape[1] , dim=1 ) # compute the previous noisy sample x_t -> x_t-1 lowerCamelCase = self.scheduler.step( _a , _a , _a , generator=_a , )[0] # post-processing lowerCamelCase = self.movq.decode(_a , force_not_quantize=_a )["""sample"""] if output_type not in ["pt", "np", "pil"]: raise ValueError(f'Only the output types `pt`, `pil` and `np` are supported not output_type={output_type}' ) if output_type in ["np", "pil"]: lowerCamelCase = image * 0.5 + 0.5 lowerCamelCase = image.clamp(0 , 1 ) lowerCamelCase = image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() if output_type == "pil": lowerCamelCase = self.numpy_to_pil(_a ) if not return_dict: return (image,) return ImagePipelineOutput(images=_a )
291
"""simple docstring""" import unittest import numpy as np from transformers import RoFormerConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax.numpy as jnp from transformers.models.roformer.modeling_flax_roformer import ( FlaxRoFormerForMaskedLM, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerModel, ) class __magic_name__ ( unittest.TestCase ): '''simple docstring''' def __init__( self , _a , _a=13 , _a=7 , _a=True , _a=True , _a=True , _a=True , _a=99 , _a=32 , _a=5 , _a=4 , _a=37 , _a="gelu" , _a=0.1 , _a=0.1 , _a=512 , _a=16 , _a=2 , _a=0.02 , _a=4 , ): """simple docstring""" lowerCamelCase = parent lowerCamelCase = batch_size lowerCamelCase = seq_length lowerCamelCase = is_training lowerCamelCase = use_attention_mask lowerCamelCase = use_token_type_ids lowerCamelCase = use_labels lowerCamelCase = vocab_size lowerCamelCase = hidden_size lowerCamelCase = num_hidden_layers lowerCamelCase = num_attention_heads lowerCamelCase = intermediate_size lowerCamelCase = hidden_act lowerCamelCase = hidden_dropout_prob lowerCamelCase = attention_probs_dropout_prob lowerCamelCase = max_position_embeddings lowerCamelCase = type_vocab_size lowerCamelCase = type_sequence_label_size lowerCamelCase = initializer_range lowerCamelCase = num_choices def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) lowerCamelCase = None if self.use_attention_mask: lowerCamelCase = random_attention_mask([self.batch_size, self.seq_length] ) lowerCamelCase = None if self.use_token_type_ids: lowerCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) lowerCamelCase = RoFormerConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=_a , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.prepare_config_and_inputs() lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase = config_and_inputs lowerCamelCase = {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": attention_mask} return config, inputs_dict @require_flax class __magic_name__ ( UpperCAmelCase__ , unittest.TestCase ): '''simple docstring''' __UpperCamelCase = True __UpperCamelCase = ( ( FlaxRoFormerModel, FlaxRoFormerForMaskedLM, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, ) if is_flax_available() else () ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = FlaxRoFormerModelTester(self ) @slow def _lowerCAmelCase ( self ): """simple docstring""" for model_class_name in self.all_model_classes: lowerCamelCase = model_class_name.from_pretrained("""junnyu/roformer_chinese_small""" , from_pt=_a ) lowerCamelCase = model(np.ones((1, 1) ) ) self.assertIsNotNone(_a ) @require_flax class __magic_name__ ( unittest.TestCase ): '''simple docstring''' @slow def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = FlaxRoFormerForMaskedLM.from_pretrained("""junnyu/roformer_chinese_base""" ) lowerCamelCase = jnp.array([[0, 1, 2, 3, 4, 5]] ) lowerCamelCase = model(_a )[0] lowerCamelCase = 50_000 lowerCamelCase = (1, 6, vocab_size) self.assertEqual(output.shape , _a ) lowerCamelCase = jnp.array( [[[-0.1_205, -1.0_265, 0.2_922], [-1.5_134, 0.1_974, 0.1_519], [-5.0_135, -3.9_003, -0.8_404]]] ) self.assertTrue(jnp.allclose(output[:, :3, :3] , _a , atol=1e-4 ) )
291
1
"""simple docstring""" from typing import TYPE_CHECKING from ....utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCAmelCase : Dict = {"""configuration_mmbt""": ["""MMBTConfig"""]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : Tuple = ["""MMBTForClassification""", """MMBTModel""", """ModalEmbeddings"""] if TYPE_CHECKING: from .configuration_mmbt import MMBTConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mmbt import MMBTForClassification, MMBTModel, ModalEmbeddings else: import sys lowerCAmelCase : str = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
291
"""simple docstring""" from typing import Any def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ) -> list: _validation( snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ) # Creates data structures and fill initial step lowerCamelCase = {} lowerCamelCase = {} for state in states_space: lowerCamelCase = observations_space[0] lowerCamelCase = ( initial_probabilities[state] * emission_probabilities[state][observation] ) lowerCamelCase = None # Fills the data structure with the probabilities of # different transitions and pointers to previous states for o in range(1 , len(snake_case__ ) ): lowerCamelCase = observations_space[o] lowerCamelCase = observations_space[o - 1] for state in states_space: # Calculates the argmax for probability function lowerCamelCase = """""" lowerCamelCase = -1 for k_state in states_space: lowerCamelCase = ( probabilities[(k_state, prior_observation)] * transition_probabilities[k_state][state] * emission_probabilities[state][observation] ) if probability > max_probability: lowerCamelCase = probability lowerCamelCase = k_state # Update probabilities and pointers dicts lowerCamelCase = ( probabilities[(arg_max, prior_observation)] * transition_probabilities[arg_max][state] * emission_probabilities[state][observation] ) lowerCamelCase = arg_max # The final observation lowerCamelCase = observations_space[len(snake_case__ ) - 1] # argmax for given final observation lowerCamelCase = """""" lowerCamelCase = -1 for k_state in states_space: lowerCamelCase = probabilities[(k_state, final_observation)] if probability > max_probability: lowerCamelCase = probability lowerCamelCase = k_state lowerCamelCase = arg_max # Process pointers backwards lowerCamelCase = last_state lowerCamelCase = [] for o in range(len(snake_case__ ) - 1 , -1 , -1 ): result.append(snake_case__ ) lowerCamelCase = pointers[previous, observations_space[o]] result.reverse() return result def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ) -> None: _validate_not_empty( snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ) _validate_lists(snake_case__ , snake_case__ ) _validate_dicts( snake_case__ , snake_case__ , snake_case__ ) def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ) -> None: if not all( [ observations_space, states_space, initial_probabilities, transition_probabilities, emission_probabilities, ] ): raise ValueError("""There's an empty parameter""" ) def a__ ( snake_case__ , snake_case__ ) -> None: _validate_list(snake_case__ , """observations_space""" ) _validate_list(snake_case__ , """states_space""" ) def a__ ( snake_case__ , snake_case__ ) -> None: if not isinstance(_object , snake_case__ ): lowerCamelCase = F'{var_name} must be a list' raise ValueError(snake_case__ ) else: for x in _object: if not isinstance(snake_case__ , snake_case__ ): lowerCamelCase = F'{var_name} must be a list of strings' raise ValueError(snake_case__ ) def a__ ( snake_case__ , snake_case__ , snake_case__ , ) -> None: _validate_dict(snake_case__ , """initial_probabilities""" , snake_case__ ) _validate_nested_dict(snake_case__ , """transition_probabilities""" ) _validate_nested_dict(snake_case__ , """emission_probabilities""" ) def a__ ( snake_case__ , snake_case__ ) -> None: _validate_dict(_object , snake_case__ , snake_case__ ) for x in _object.values(): _validate_dict(snake_case__ , snake_case__ , snake_case__ , snake_case__ ) def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ = False ) -> None: if not isinstance(_object , snake_case__ ): lowerCamelCase = F'{var_name} must be a dict' raise ValueError(snake_case__ ) if not all(isinstance(snake_case__ , snake_case__ ) for x in _object ): lowerCamelCase = F'{var_name} all keys must be strings' raise ValueError(snake_case__ ) if not all(isinstance(snake_case__ , snake_case__ ) for x in _object.values() ): lowerCamelCase = """nested dictionary """ if nested else """""" lowerCamelCase = F'{var_name} {nested_text}all values must be {value_type.__name__}' raise ValueError(snake_case__ ) if __name__ == "__main__": from doctest import testmod testmod()
291
1
"""simple docstring""" from typing import Any def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ) -> list: _validation( snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ) # Creates data structures and fill initial step lowerCamelCase = {} lowerCamelCase = {} for state in states_space: lowerCamelCase = observations_space[0] lowerCamelCase = ( initial_probabilities[state] * emission_probabilities[state][observation] ) lowerCamelCase = None # Fills the data structure with the probabilities of # different transitions and pointers to previous states for o in range(1 , len(snake_case__ ) ): lowerCamelCase = observations_space[o] lowerCamelCase = observations_space[o - 1] for state in states_space: # Calculates the argmax for probability function lowerCamelCase = """""" lowerCamelCase = -1 for k_state in states_space: lowerCamelCase = ( probabilities[(k_state, prior_observation)] * transition_probabilities[k_state][state] * emission_probabilities[state][observation] ) if probability > max_probability: lowerCamelCase = probability lowerCamelCase = k_state # Update probabilities and pointers dicts lowerCamelCase = ( probabilities[(arg_max, prior_observation)] * transition_probabilities[arg_max][state] * emission_probabilities[state][observation] ) lowerCamelCase = arg_max # The final observation lowerCamelCase = observations_space[len(snake_case__ ) - 1] # argmax for given final observation lowerCamelCase = """""" lowerCamelCase = -1 for k_state in states_space: lowerCamelCase = probabilities[(k_state, final_observation)] if probability > max_probability: lowerCamelCase = probability lowerCamelCase = k_state lowerCamelCase = arg_max # Process pointers backwards lowerCamelCase = last_state lowerCamelCase = [] for o in range(len(snake_case__ ) - 1 , -1 , -1 ): result.append(snake_case__ ) lowerCamelCase = pointers[previous, observations_space[o]] result.reverse() return result def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ) -> None: _validate_not_empty( snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ) _validate_lists(snake_case__ , snake_case__ ) _validate_dicts( snake_case__ , snake_case__ , snake_case__ ) def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ) -> None: if not all( [ observations_space, states_space, initial_probabilities, transition_probabilities, emission_probabilities, ] ): raise ValueError("""There's an empty parameter""" ) def a__ ( snake_case__ , snake_case__ ) -> None: _validate_list(snake_case__ , """observations_space""" ) _validate_list(snake_case__ , """states_space""" ) def a__ ( snake_case__ , snake_case__ ) -> None: if not isinstance(_object , snake_case__ ): lowerCamelCase = F'{var_name} must be a list' raise ValueError(snake_case__ ) else: for x in _object: if not isinstance(snake_case__ , snake_case__ ): lowerCamelCase = F'{var_name} must be a list of strings' raise ValueError(snake_case__ ) def a__ ( snake_case__ , snake_case__ , snake_case__ , ) -> None: _validate_dict(snake_case__ , """initial_probabilities""" , snake_case__ ) _validate_nested_dict(snake_case__ , """transition_probabilities""" ) _validate_nested_dict(snake_case__ , """emission_probabilities""" ) def a__ ( snake_case__ , snake_case__ ) -> None: _validate_dict(_object , snake_case__ , snake_case__ ) for x in _object.values(): _validate_dict(snake_case__ , snake_case__ , snake_case__ , snake_case__ ) def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ = False ) -> None: if not isinstance(_object , snake_case__ ): lowerCamelCase = F'{var_name} must be a dict' raise ValueError(snake_case__ ) if not all(isinstance(snake_case__ , snake_case__ ) for x in _object ): lowerCamelCase = F'{var_name} all keys must be strings' raise ValueError(snake_case__ ) if not all(isinstance(snake_case__ , snake_case__ ) for x in _object.values() ): lowerCamelCase = """nested dictionary """ if nested else """""" lowerCamelCase = F'{var_name} {nested_text}all values must be {value_type.__name__}' raise ValueError(snake_case__ ) if __name__ == "__main__": from doctest import testmod testmod()
291
"""simple docstring""" import argparse from pathlib import Path import torch from transformers import OPTConfig, OPTModel from transformers.utils import logging logging.set_verbosity_info() lowerCAmelCase : Dict = logging.get_logger(__name__) def a__ ( snake_case__ ) -> Dict: lowerCamelCase = torch.load(snake_case__ , map_location="""cpu""" ) if "model" in sd.keys(): lowerCamelCase = torch.load(snake_case__ , map_location="""cpu""" )["""model"""] # pop unnecessary weights lowerCamelCase = [ """decoder.version""", """decoder.output_projection.weight""", ] for key in keys_to_delete: if key in sd: sd.pop(snake_case__ ) lowerCamelCase = { """decoder.project_in_dim.weight""": """decoder.project_in.weight""", """decoder.project_out_dim.weight""": """decoder.project_out.weight""", """decoder.layer_norm.weight""": """decoder.final_layer_norm.weight""", """decoder.layer_norm.bias""": """decoder.final_layer_norm.bias""", } for old_key, new_key in keys_to_rename.items(): if old_key in sd: lowerCamelCase = sd.pop(snake_case__ ) lowerCamelCase = list(sd.keys() ) for key in keys: if ".qkv_proj." in key: lowerCamelCase = sd[key] # We split QKV in separate Q,K,V lowerCamelCase = key.replace(""".qkv_proj.""" , """.q_proj.""" ) lowerCamelCase = key.replace(""".qkv_proj.""" , """.k_proj.""" ) lowerCamelCase = key.replace(""".qkv_proj.""" , """.v_proj.""" ) lowerCamelCase = value.shape[0] assert depth % 3 == 0 # `SequeuceParallelTransformerBlock` has QKV weight is separated in K,V,Q despite the naming: # https://cs.github.com/facebookresearch/metaseq/blob/51871bd73cd04c038f239ea2a26db1d7f6b37927/metaseq/modules/sequence_parallel_transformer_layer.py#L97 lowerCamelCase , lowerCamelCase , lowerCamelCase = torch.split(snake_case__ , depth // 3 , dim=0 ) lowerCamelCase = q lowerCamelCase = k lowerCamelCase = v del sd[key] return sd @torch.no_grad() def a__ ( snake_case__ , snake_case__ , snake_case__=None ) -> Tuple: lowerCamelCase = load_checkpoint(snake_case__ ) if config is not None: lowerCamelCase = OPTConfig.from_pretrained(snake_case__ ) else: lowerCamelCase = OPTConfig() lowerCamelCase = OPTModel(snake_case__ ).half().eval() model.load_state_dict(snake_case__ ) # Check results Path(snake_case__ ).mkdir(exist_ok=snake_case__ ) model.save_pretrained(snake_case__ ) if __name__ == "__main__": lowerCAmelCase : List[str] = argparse.ArgumentParser() # Required parameters parser.add_argument( """--fairseq_path""", type=str, help=( """path to fairseq checkpoint in correct format. You can find all checkpoints in the correct format here:""" """ https://huggingface.co/models?other=opt_metasq""" ), ) parser.add_argument("""--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""") parser.add_argument("""--hf_config""", default=None, type=str, help="""Define HF config.""") lowerCAmelCase : Optional[Any] = parser.parse_args() convert_opt_checkpoint(args.fairseq_path, args.pytorch_dump_folder_path, config=args.hf_config)
291
1
"""simple docstring""" import warnings from diffusers import StableDiffusionImgaImgPipeline # noqa F401 warnings.warn( """The `image_to_image.py` script is outdated. Please use directly `from diffusers import""" """ StableDiffusionImg2ImgPipeline` instead.""" )
291
"""simple docstring""" import json import os import shutil import tempfile import unittest import numpy as np from transformers import BertTokenizerFast from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES, BertTokenizer from transformers.testing_utils import require_tokenizers, require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import VisionTextDualEncoderProcessor, ViTImageProcessor @require_tokenizers @require_vision class __magic_name__ ( unittest.TestCase ): '''simple docstring''' def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = tempfile.mkdtemp() # fmt: off lowerCamelCase = ["""[UNK]""", """[CLS]""", """[SEP]""", """[PAD]""", """[MASK]""", """want""", """##want""", """##ed""", """wa""", """un""", """runn""", """##ing""", """,""", """low""", """lowest"""] # fmt: on lowerCamelCase = 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] ) ) lowerCamelCase = { """do_resize""": True, """size""": {"""height""": 18, """width""": 18}, """do_normalize""": True, """image_mean""": [0.5, 0.5, 0.5], """image_std""": [0.5, 0.5, 0.5], } lowerCamelCase = os.path.join(self.tmpdirname , _a ) with open(self.image_processor_file , """w""" , encoding="""utf-8""" ) as fp: json.dump(_a , _a ) def _lowerCAmelCase ( self , **_a ): """simple docstring""" return BertTokenizer.from_pretrained(self.tmpdirname , **_a ) def _lowerCAmelCase ( self , **_a ): """simple docstring""" return ViTImageProcessor.from_pretrained(self.tmpdirname , **_a ) def _lowerCAmelCase ( self ): """simple docstring""" shutil.rmtree(self.tmpdirname ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = [np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta )] lowerCamelCase = [Image.fromarray(np.moveaxis(_a , 0 , -1 ) ) for x in image_inputs] return image_inputs def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.get_tokenizer() lowerCamelCase = self.get_image_processor() lowerCamelCase = VisionTextDualEncoderProcessor(tokenizer=_a , image_processor=_a ) processor.save_pretrained(self.tmpdirname ) lowerCamelCase = VisionTextDualEncoderProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer.get_vocab() ) self.assertIsInstance(processor.tokenizer , (BertTokenizer, BertTokenizerFast) ) self.assertEqual(processor.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertIsInstance(processor.image_processor , _a ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = VisionTextDualEncoderProcessor( tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) lowerCamelCase = self.get_tokenizer(bos_token="""(BOS)""" , eos_token="""(EOS)""" ) lowerCamelCase = self.get_image_processor(do_normalize=_a , padding_value=1.0 ) lowerCamelCase = VisionTextDualEncoderProcessor.from_pretrained( self.tmpdirname , bos_token="""(BOS)""" , eos_token="""(EOS)""" , do_normalize=_a , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , (BertTokenizer, BertTokenizerFast) ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , _a ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.get_image_processor() lowerCamelCase = self.get_tokenizer() lowerCamelCase = VisionTextDualEncoderProcessor(tokenizer=_a , image_processor=_a ) lowerCamelCase = self.prepare_image_inputs() lowerCamelCase = image_processor(_a , return_tensors="""np""" ) lowerCamelCase = processor(images=_a , return_tensors="""np""" ) for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() , input_processor[key].sum() , delta=1e-2 ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.get_image_processor() lowerCamelCase = self.get_tokenizer() lowerCamelCase = VisionTextDualEncoderProcessor(tokenizer=_a , image_processor=_a ) lowerCamelCase = """lower newer""" lowerCamelCase = processor(text=_a ) lowerCamelCase = tokenizer(_a ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.get_image_processor() lowerCamelCase = self.get_tokenizer() lowerCamelCase = VisionTextDualEncoderProcessor(tokenizer=_a , image_processor=_a ) lowerCamelCase = """lower newer""" lowerCamelCase = self.prepare_image_inputs() lowerCamelCase = processor(text=_a , images=_a ) self.assertListEqual(list(inputs.keys() ) , ["""input_ids""", """token_type_ids""", """attention_mask""", """pixel_values"""] ) # test if it raises when no input is passed with self.assertRaises(_a ): processor() def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.get_image_processor() lowerCamelCase = self.get_tokenizer() lowerCamelCase = VisionTextDualEncoderProcessor(tokenizer=_a , image_processor=_a ) lowerCamelCase = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] lowerCamelCase = processor.batch_decode(_a ) lowerCamelCase = tokenizer.batch_decode(_a ) self.assertListEqual(_a , _a ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.get_image_processor() lowerCamelCase = self.get_tokenizer() lowerCamelCase = VisionTextDualEncoderProcessor(tokenizer=_a , image_processor=_a ) lowerCamelCase = """lower newer""" lowerCamelCase = self.prepare_image_inputs() lowerCamelCase = processor(text=_a , images=_a ) self.assertListEqual(list(inputs.keys() ) , processor.model_input_names )
291
1
"""simple docstring""" import os from bleurt import score # From: git+https://github.com/google-research/bleurt.git import datasets lowerCAmelCase : List[str] = datasets.logging.get_logger(__name__) lowerCAmelCase : Optional[int] = """\ @inproceedings{bleurt, title={BLEURT: Learning Robust Metrics for Text Generation}, author={Thibault Sellam and Dipanjan Das and Ankur P. Parikh}, booktitle={ACL}, year={2020}, url={https://arxiv.org/abs/2004.04696} } """ lowerCAmelCase : List[str] = """\ BLEURT a learnt evaluation metric for Natural Language Generation. It is built using multiple phases of transfer learning starting from a pretrained BERT model (Devlin et al. 2018) and then employing another pre-training phrase using synthetic data. Finally it is trained on WMT human annotations. You may run BLEURT out-of-the-box or fine-tune it for your specific application (the latter is expected to perform better). See the project's README at https://github.com/google-research/bleurt#readme for more information. """ lowerCAmelCase : Tuple = """ BLEURT score. Args: `predictions` (list of str): prediction/candidate sentences `references` (list of str): reference sentences `checkpoint` BLEURT checkpoint. Will default to BLEURT-tiny if None. Returns: 'scores': List of scores. Examples: >>> predictions = [\"hello there\", \"general kenobi\"] >>> references = [\"hello there\", \"general kenobi\"] >>> bleurt = datasets.load_metric(\"bleurt\") >>> results = bleurt.compute(predictions=predictions, references=references) >>> print([round(v, 2) for v in results[\"scores\"]]) [1.03, 1.04] """ lowerCAmelCase : List[str] = { """bleurt-tiny-128""": """https://storage.googleapis.com/bleurt-oss/bleurt-tiny-128.zip""", """bleurt-tiny-512""": """https://storage.googleapis.com/bleurt-oss/bleurt-tiny-512.zip""", """bleurt-base-128""": """https://storage.googleapis.com/bleurt-oss/bleurt-base-128.zip""", """bleurt-base-512""": """https://storage.googleapis.com/bleurt-oss/bleurt-base-512.zip""", """bleurt-large-128""": """https://storage.googleapis.com/bleurt-oss/bleurt-large-128.zip""", """bleurt-large-512""": """https://storage.googleapis.com/bleurt-oss/bleurt-large-512.zip""", """BLEURT-20-D3""": """https://storage.googleapis.com/bleurt-oss-21/BLEURT-20-D3.zip""", """BLEURT-20-D6""": """https://storage.googleapis.com/bleurt-oss-21/BLEURT-20-D6.zip""", """BLEURT-20-D12""": """https://storage.googleapis.com/bleurt-oss-21/BLEURT-20-D12.zip""", """BLEURT-20""": """https://storage.googleapis.com/bleurt-oss-21/BLEURT-20.zip""", } @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __magic_name__ ( datasets.Metric ): '''simple docstring''' def _lowerCAmelCase ( self ): """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , homepage="""https://github.com/google-research/bleurt""" , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": datasets.Value("""string""" , id="""sequence""" ), """references""": datasets.Value("""string""" , id="""sequence""" ), } ) , codebase_urls=["""https://github.com/google-research/bleurt"""] , reference_urls=["""https://github.com/google-research/bleurt""", """https://arxiv.org/abs/2004.04696"""] , ) def _lowerCAmelCase ( self , _a ): """simple docstring""" # check that config name specifies a valid BLEURT model if self.config_name == "default": logger.warning( """Using default BLEURT-Base checkpoint for sequence maximum length 128. """ """You can use a bigger model for better results with e.g.: datasets.load_metric('bleurt', 'bleurt-large-512').""" ) lowerCamelCase = """bleurt-base-128""" if self.config_name.lower() in CHECKPOINT_URLS: lowerCamelCase = self.config_name.lower() elif self.config_name.upper() in CHECKPOINT_URLS: lowerCamelCase = self.config_name.upper() else: raise KeyError( f'{self.config_name} model not found. You should supply the name of a model checkpoint for bleurt in {CHECKPOINT_URLS.keys()}' ) # download the model checkpoint specified by self.config_name and set up the scorer lowerCamelCase = dl_manager.download_and_extract(CHECKPOINT_URLS[checkpoint_name] ) lowerCamelCase = score.BleurtScorer(os.path.join(_a , _a ) ) def _lowerCAmelCase ( self , _a , _a ): """simple docstring""" lowerCamelCase = self.scorer.score(references=_a , candidates=_a ) return {"scores": scores}
291
"""simple docstring""" import importlib import sys from argparse import REMAINDER, ArgumentParser from pathlib import Path import torch_xla.distributed.xla_multiprocessing as xmp def a__ ( ) -> Union[str, Any]: lowerCamelCase = ArgumentParser( description=( """PyTorch TPU distributed training launch """ """helper utility that will spawn up """ """multiple distributed processes""" ) ) # Optional arguments for the launch helper parser.add_argument("""--num_cores""" , type=snake_case__ , default=1 , help="""Number of TPU cores to use (1 or 8).""" ) # positional parser.add_argument( """training_script""" , type=snake_case__ , help=( """The full path to the single TPU training """ """program/script to be launched in parallel, """ """followed by all the arguments for the """ """training script""" ) , ) # rest from the training program parser.add_argument("""training_script_args""" , nargs=snake_case__ ) return parser.parse_args() def a__ ( ) -> List[str]: lowerCamelCase = parse_args() # Import training_script as a module. lowerCamelCase = Path(args.training_script ) sys.path.append(str(script_fpath.parent.resolve() ) ) lowerCamelCase = script_fpath.stem lowerCamelCase = importlib.import_module(snake_case__ ) # Patch sys.argv lowerCamelCase = [args.training_script] + args.training_script_args + ["""--tpu_num_cores""", str(args.num_cores )] xmp.spawn(mod._mp_fn , args=() , nprocs=args.num_cores ) if __name__ == "__main__": main()
291
1
"""simple docstring""" import pytest import requests from datasets.utils.file_utils import http_head from .utils import OfflineSimulationMode, RequestWouldHangIndefinitelyError, offline @pytest.mark.integration def a__ ( ) -> Union[str, Any]: with offline(OfflineSimulationMode.CONNECTION_TIMES_OUT ): with pytest.raises(snake_case__ ): requests.request("""GET""" , """https://huggingface.co""" ) with pytest.raises(requests.exceptions.ConnectTimeout ): requests.request("""GET""" , """https://huggingface.co""" , timeout=1.0 ) @pytest.mark.integration def a__ ( ) -> Optional[Any]: with offline(OfflineSimulationMode.CONNECTION_FAILS ): with pytest.raises(requests.exceptions.ConnectionError ): requests.request("""GET""" , """https://huggingface.co""" ) def a__ ( ) -> Optional[Any]: with offline(OfflineSimulationMode.HF_DATASETS_OFFLINE_SET_TO_1 ): with pytest.raises(snake_case__ ): http_head("""https://huggingface.co""" )
291
"""simple docstring""" import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase : int = logging.get_logger(__name__) lowerCAmelCase : List[str] = { """asapp/sew-d-tiny-100k""": """https://huggingface.co/asapp/sew-d-tiny-100k/resolve/main/config.json""", # See all SEW-D models at https://huggingface.co/models?filter=sew-d } class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' __UpperCamelCase = "sew-d" def __init__( self , _a=32 , _a=768 , _a=12 , _a=12 , _a=3_072 , _a=2 , _a=512 , _a=256 , _a=True , _a=True , _a=("p2c", "c2p") , _a="layer_norm" , _a="gelu_python" , _a=0.1 , _a=0.1 , _a=0.1 , _a=0.0 , _a=0.1 , _a=0.02 , _a=1e-7 , _a=1e-5 , _a="group" , _a="gelu" , _a=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512) , _a=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1) , _a=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1) , _a=False , _a=128 , _a=16 , _a=True , _a=0.05 , _a=10 , _a=2 , _a=0.0 , _a=10 , _a=0 , _a="mean" , _a=False , _a=False , _a=256 , _a=0 , _a=1 , _a=2 , **_a , ): """simple docstring""" super().__init__(**_a , pad_token_id=_a , bos_token_id=_a , eos_token_id=_a ) lowerCamelCase = hidden_size lowerCamelCase = feat_extract_norm lowerCamelCase = feat_extract_activation lowerCamelCase = list(_a ) lowerCamelCase = list(_a ) lowerCamelCase = list(_a ) lowerCamelCase = conv_bias lowerCamelCase = num_conv_pos_embeddings lowerCamelCase = num_conv_pos_embedding_groups lowerCamelCase = len(self.conv_dim ) lowerCamelCase = num_hidden_layers lowerCamelCase = intermediate_size lowerCamelCase = squeeze_factor lowerCamelCase = max_position_embeddings lowerCamelCase = position_buckets lowerCamelCase = share_att_key lowerCamelCase = relative_attention lowerCamelCase = norm_rel_ebd lowerCamelCase = list(_a ) lowerCamelCase = hidden_act lowerCamelCase = num_attention_heads lowerCamelCase = hidden_dropout lowerCamelCase = attention_dropout lowerCamelCase = activation_dropout lowerCamelCase = feat_proj_dropout lowerCamelCase = final_dropout lowerCamelCase = layer_norm_eps lowerCamelCase = feature_layer_norm_eps lowerCamelCase = initializer_range lowerCamelCase = vocab_size if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( """Configuration for convolutional layers is incorrect.""" """It is required that `len(config.conv_dim)` == `len(config.conv_stride)` == `len(config.conv_kernel)`,""" f'but is `len(config.conv_dim) = {len(self.conv_dim )}`, `len(config.conv_stride)' f'= {len(self.conv_stride )}`, `len(config.conv_kernel) = {len(self.conv_kernel )}`.' ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 lowerCamelCase = apply_spec_augment lowerCamelCase = mask_time_prob lowerCamelCase = mask_time_length lowerCamelCase = mask_time_min_masks lowerCamelCase = mask_feature_prob lowerCamelCase = mask_feature_length lowerCamelCase = mask_feature_min_masks # ctc loss lowerCamelCase = ctc_loss_reduction lowerCamelCase = ctc_zero_infinity # sequence classification lowerCamelCase = use_weighted_layer_sum lowerCamelCase = classifier_proj_size @property def _lowerCAmelCase ( self ): """simple docstring""" return functools.reduce(operator.mul , self.conv_stride , 1 )
291
1
"""simple docstring""" import argparse import os import gluonnlp as nlp import mxnet as mx import numpy as np import torch from gluonnlp.base import get_home_dir from gluonnlp.model.bert import BERTEncoder from gluonnlp.model.utils import _load_vocab from gluonnlp.vocab import Vocab from packaging import version from torch import nn from transformers import BertConfig, BertForMaskedLM, BertModel, RobertaTokenizer from transformers.models.bert.modeling_bert import ( BertIntermediate, BertLayer, BertOutput, BertSelfAttention, BertSelfOutput, ) from transformers.utils import logging if version.parse(nlp.__version__) != version.parse("""0.8.3"""): raise Exception("""requires gluonnlp == 0.8.3""") if version.parse(mx.__version__) != version.parse("""1.5.0"""): raise Exception("""requires mxnet == 1.5.0""") logging.set_verbosity_info() lowerCAmelCase : Optional[Any] = logging.get_logger(__name__) lowerCAmelCase : Any = """The Nymphenburg Palace is a beautiful palace in Munich!""" def a__ ( snake_case__ , snake_case__ ) -> List[str]: lowerCamelCase = { """attention_cell""": """multi_head""", """num_layers""": 4, """units""": 10_24, """hidden_size""": 7_68, """max_length""": 5_12, """num_heads""": 8, """scaled""": True, """dropout""": 0.1, """use_residual""": True, """embed_size""": 10_24, """embed_dropout""": 0.1, """word_embed""": None, """layer_norm_eps""": 1E-5, """token_type_vocab_size""": 2, } lowerCamelCase = bort_4_8_768_1024_hparams # Let's construct the original Bort model here # Taken from official BERT implementation, see: # https://github.com/alexa/bort/blob/master/bort/bort.py lowerCamelCase = BERTEncoder( attention_cell=predefined_args["""attention_cell"""] , num_layers=predefined_args["""num_layers"""] , units=predefined_args["""units"""] , hidden_size=predefined_args["""hidden_size"""] , max_length=predefined_args["""max_length"""] , num_heads=predefined_args["""num_heads"""] , scaled=predefined_args["""scaled"""] , dropout=predefined_args["""dropout"""] , output_attention=snake_case__ , output_all_encodings=snake_case__ , use_residual=predefined_args["""use_residual"""] , activation=predefined_args.get("""activation""" , """gelu""" ) , layer_norm_eps=predefined_args.get("""layer_norm_eps""" , snake_case__ ) , ) # Vocab information needs to be fetched first # It's the same as RoBERTa, so RobertaTokenizer can be used later lowerCamelCase = """openwebtext_ccnews_stories_books_cased""" # Specify download folder to Gluonnlp's vocab lowerCamelCase = os.path.join(get_home_dir() , """models""" ) lowerCamelCase = _load_vocab(snake_case__ , snake_case__ , snake_case__ , cls=snake_case__ ) lowerCamelCase = nlp.model.BERTModel( snake_case__ , len(snake_case__ ) , units=predefined_args["""units"""] , embed_size=predefined_args["""embed_size"""] , embed_dropout=predefined_args["""embed_dropout"""] , word_embed=predefined_args["""word_embed"""] , use_pooler=snake_case__ , use_token_type_embed=snake_case__ , token_type_vocab_size=predefined_args["""token_type_vocab_size"""] , use_classifier=snake_case__ , use_decoder=snake_case__ , ) original_bort.load_parameters(snake_case__ , cast_dtype=snake_case__ , ignore_extra=snake_case__ ) lowerCamelCase = original_bort._collect_params_with_prefix() # Build our config 🤗 lowerCamelCase = { """architectures""": ["""BertForMaskedLM"""], """attention_probs_dropout_prob""": predefined_args["""dropout"""], """hidden_act""": """gelu""", """hidden_dropout_prob""": predefined_args["""dropout"""], """hidden_size""": predefined_args["""embed_size"""], """initializer_range""": 0.02, """intermediate_size""": predefined_args["""hidden_size"""], """layer_norm_eps""": predefined_args["""layer_norm_eps"""], """max_position_embeddings""": predefined_args["""max_length"""], """model_type""": """bort""", """num_attention_heads""": predefined_args["""num_heads"""], """num_hidden_layers""": predefined_args["""num_layers"""], """pad_token_id""": 1, # 2 = BERT, 1 = RoBERTa """type_vocab_size""": 1, # 2 = BERT, 1 = RoBERTa """vocab_size""": len(snake_case__ ), } lowerCamelCase = BertConfig.from_dict(snake_case__ ) lowerCamelCase = BertForMaskedLM(snake_case__ ) hf_bort_model.eval() # Parameter mapping table (Gluonnlp to Transformers) # * denotes layer index # # | Gluon Parameter | Transformers Parameter # | -------------------------------------------------------------- | ---------------------- # | `encoder.layer_norm.beta` | `bert.embeddings.LayerNorm.bias` # | `encoder.layer_norm.gamma` | `bert.embeddings.LayerNorm.weight` # | `encoder.position_weight` | `bert.embeddings.position_embeddings.weight` # | `word_embed.0.weight` | `bert.embeddings.word_embeddings.weight` # | `encoder.transformer_cells.*.attention_cell.proj_key.bias` | `bert.encoder.layer.*.attention.self.key.bias` # | `encoder.transformer_cells.*.attention_cell.proj_key.weight` | `bert.encoder.layer.*.attention.self.key.weight` # | `encoder.transformer_cells.*.attention_cell.proj_query.bias` | `bert.encoder.layer.*.attention.self.query.bias` # | `encoder.transformer_cells.*.attention_cell.proj_query.weight` | `bert.encoder.layer.*.attention.self.query.weight` # | `encoder.transformer_cells.*.attention_cell.proj_value.bias` | `bert.encoder.layer.*.attention.self.value.bias` # | `encoder.transformer_cells.*.attention_cell.proj_value.weight` | `bert.encoder.layer.*.attention.self.value.weight` # | `encoder.transformer_cells.*.ffn.ffn_2.bias` | `bert.encoder.layer.*.attention.output.dense.bias` # | `encoder.transformer_cells.*.ffn.ffn_2.weight` | `bert.encoder.layer.*.attention.output.dense.weight` # | `encoder.transformer_cells.*.layer_norm.beta` | `bert.encoder.layer.*.attention.output.LayerNorm.bias` # | `encoder.transformer_cells.*.layer_norm.gamma` | `bert.encoder.layer.*.attention.output.LayerNorm.weight` # | `encoder.transformer_cells.*.ffn.ffn_1.bias` | `bert.encoder.layer.*.intermediate.dense.bias` # | `encoder.transformer_cells.*.ffn.ffn_1.weight` | `bert.encoder.layer.*.intermediate.dense.weight` # | `encoder.transformer_cells.*.ffn.layer_norm.beta` | `bert.encoder.layer.*.output.LayerNorm.bias` # | `encoder.transformer_cells.*.ffn.layer_norm.gamma` | `bert.encoder.layer.*.output.LayerNorm.weight` # | `encoder.transformer_cells.*.proj.bias` | `bert.encoder.layer.*.output.dense.bias` # | `encoder.transformer_cells.*.proj.weight` | `bert.encoder.layer.*.output.dense.weight` # Helper function to convert MXNET Arrays to PyTorch def to_torch(snake_case__ ) -> nn.Parameter: return nn.Parameter(torch.FloatTensor(mx_array.data().asnumpy() ) ) # Check param shapes and map new HF param back def check_and_map_params(snake_case__ , snake_case__ ): lowerCamelCase = hf_param.shape lowerCamelCase = to_torch(params[gluon_param] ) lowerCamelCase = gluon_param.shape assert ( shape_hf == shape_gluon ), F'The gluon parameter {gluon_param} has shape {shape_gluon}, but expects shape {shape_hf} for Transformers' return gluon_param lowerCamelCase = check_and_map_params( hf_bort_model.bert.embeddings.word_embeddings.weight , """word_embed.0.weight""" ) lowerCamelCase = check_and_map_params( hf_bort_model.bert.embeddings.position_embeddings.weight , """encoder.position_weight""" ) lowerCamelCase = check_and_map_params( hf_bort_model.bert.embeddings.LayerNorm.bias , """encoder.layer_norm.beta""" ) lowerCamelCase = check_and_map_params( hf_bort_model.bert.embeddings.LayerNorm.weight , """encoder.layer_norm.gamma""" ) # Inspired by RoBERTa conversion script, we just zero them out (Bort does not use them) lowerCamelCase = torch.zeros_like( hf_bort_model.bert.embeddings.token_type_embeddings.weight.data ) for i in range(hf_bort_config.num_hidden_layers ): lowerCamelCase = hf_bort_model.bert.encoder.layer[i] # self attention lowerCamelCase = layer.attention.self lowerCamelCase = check_and_map_params( self_attn.key.bias.data , F'encoder.transformer_cells.{i}.attention_cell.proj_key.bias' ) lowerCamelCase = check_and_map_params( self_attn.key.weight.data , F'encoder.transformer_cells.{i}.attention_cell.proj_key.weight' ) lowerCamelCase = check_and_map_params( self_attn.query.bias.data , F'encoder.transformer_cells.{i}.attention_cell.proj_query.bias' ) lowerCamelCase = check_and_map_params( self_attn.query.weight.data , F'encoder.transformer_cells.{i}.attention_cell.proj_query.weight' ) lowerCamelCase = check_and_map_params( self_attn.value.bias.data , F'encoder.transformer_cells.{i}.attention_cell.proj_value.bias' ) lowerCamelCase = check_and_map_params( self_attn.value.weight.data , F'encoder.transformer_cells.{i}.attention_cell.proj_value.weight' ) # self attention output lowerCamelCase = layer.attention.output lowerCamelCase = check_and_map_params( self_output.dense.bias , F'encoder.transformer_cells.{i}.proj.bias' ) lowerCamelCase = check_and_map_params( self_output.dense.weight , F'encoder.transformer_cells.{i}.proj.weight' ) lowerCamelCase = check_and_map_params( self_output.LayerNorm.bias , F'encoder.transformer_cells.{i}.layer_norm.beta' ) lowerCamelCase = check_and_map_params( self_output.LayerNorm.weight , F'encoder.transformer_cells.{i}.layer_norm.gamma' ) # intermediate lowerCamelCase = layer.intermediate lowerCamelCase = check_and_map_params( intermediate.dense.bias , F'encoder.transformer_cells.{i}.ffn.ffn_1.bias' ) lowerCamelCase = check_and_map_params( intermediate.dense.weight , F'encoder.transformer_cells.{i}.ffn.ffn_1.weight' ) # output lowerCamelCase = layer.output lowerCamelCase = check_and_map_params( bert_output.dense.bias , F'encoder.transformer_cells.{i}.ffn.ffn_2.bias' ) lowerCamelCase = check_and_map_params( bert_output.dense.weight , F'encoder.transformer_cells.{i}.ffn.ffn_2.weight' ) lowerCamelCase = check_and_map_params( bert_output.LayerNorm.bias , F'encoder.transformer_cells.{i}.ffn.layer_norm.beta' ) lowerCamelCase = check_and_map_params( bert_output.LayerNorm.weight , F'encoder.transformer_cells.{i}.ffn.layer_norm.gamma' ) # Save space and energy 🎄 hf_bort_model.half() # Compare output of both models lowerCamelCase = RobertaTokenizer.from_pretrained("""roberta-base""" ) lowerCamelCase = tokenizer.encode_plus(snake_case__ )["""input_ids"""] # Get gluon output lowerCamelCase = mx.nd.array([input_ids] ) lowerCamelCase = original_bort(inputs=snake_case__ , token_types=[] ) # Get Transformer output (save and reload model again) hf_bort_model.save_pretrained(snake_case__ ) lowerCamelCase = BertModel.from_pretrained(snake_case__ ) hf_bort_model.eval() lowerCamelCase = tokenizer.encode_plus(snake_case__ , return_tensors="""pt""" ) lowerCamelCase = hf_bort_model(**snake_case__ )[0] lowerCamelCase = output_gluon[0].asnumpy() lowerCamelCase = output_hf[0].detach().numpy() lowerCamelCase = np.max(np.abs(hf_layer - gluon_layer ) ).item() lowerCamelCase = np.allclose(snake_case__ , snake_case__ , atol=1E-3 ) if success: print("""✔️ Both model do output the same tensors""" ) else: print("""❌ Both model do **NOT** output the same tensors""" ) print("""Absolute difference is:""" , snake_case__ ) if __name__ == "__main__": lowerCAmelCase : Optional[int] = argparse.ArgumentParser() # Required parameters parser.add_argument( """--bort_checkpoint_path""", default=None, type=str, required=True, help="""Path the official Bort params file.""" ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, required=True, help="""Path to the output PyTorch model.""" ) lowerCAmelCase : Optional[int] = parser.parse_args() convert_bort_checkpoint_to_pytorch(args.bort_checkpoint_path, args.pytorch_dump_folder_path)
291
"""simple docstring""" from sklearn.metrics import recall_score import datasets lowerCAmelCase : Any = """ Recall is the fraction of the positive examples that were correctly labeled by the model as positive. It can be computed with the equation: Recall = TP / (TP + FN) Where TP is the true positives and FN is the false negatives. """ lowerCAmelCase : Any = """ Args: - **predictions** (`list` of `int`): The predicted labels. - **references** (`list` of `int`): The ground truth labels. - **labels** (`list` of `int`): The set of labels to include when `average` is not set to `binary`, and their order when average is `None`. Labels present in the data can be excluded in this input, for example to calculate a multiclass average ignoring a majority negative class, while labels not present in the data will result in 0 components in a macro average. For multilabel targets, labels are column indices. By default, all labels in y_true and y_pred are used in sorted order. Defaults to None. - **pos_label** (`int`): The class label to use as the 'positive class' when calculating the recall. Defaults to `1`. - **average** (`string`): This parameter is required for multiclass/multilabel targets. If None, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data. Defaults to `'binary'`. - `'binary'`: Only report results for the class specified by `pos_label`. This is applicable only if the target labels and predictions are binary. - `'micro'`: Calculate metrics globally by counting the total true positives, false negatives, and false positives. - `'macro'`: Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account. - `'weighted'`: Calculate metrics for each label, and find their average weighted by support (the number of true instances for each label). This alters `'macro'` to account for label imbalance. Note that it can result in an F-score that is not between precision and recall. - `'samples'`: Calculate metrics for each instance, and find their average (only meaningful for multilabel classification). - **sample_weight** (`list` of `float`): Sample weights Defaults to `None`. - **zero_division** (): Sets the value to return when there is a zero division. Defaults to . - `'warn'`: If there is a zero division, the return value is `0`, but warnings are also raised. - `0`: If there is a zero division, the return value is `0`. - `1`: If there is a zero division, the return value is `1`. Returns: - **recall** (`float`, or `array` of `float`): Either the general recall score, or the recall scores for individual classes, depending on the values input to `labels` and `average`. Minimum possible value is 0. Maximum possible value is 1. A higher recall means that more of the positive examples have been labeled correctly. Therefore, a higher recall is generally considered better. Examples: Example 1-A simple example with some errors >>> recall_metric = datasets.load_metric('recall') >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1]) >>> print(results) {'recall': 0.6666666666666666} Example 2-The same example as Example 1, but with `pos_label=0` instead of the default `pos_label=1`. >>> recall_metric = datasets.load_metric('recall') >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1], pos_label=0) >>> print(results) {'recall': 0.5} Example 3-The same example as Example 1, but with `sample_weight` included. >>> recall_metric = datasets.load_metric('recall') >>> sample_weight = [0.9, 0.2, 0.9, 0.3, 0.8] >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1], sample_weight=sample_weight) >>> print(results) {'recall': 0.55} Example 4-A multiclass example, using different averages. >>> recall_metric = datasets.load_metric('recall') >>> predictions = [0, 2, 1, 0, 0, 1] >>> references = [0, 1, 2, 0, 1, 2] >>> results = recall_metric.compute(predictions=predictions, references=references, average='macro') >>> print(results) {'recall': 0.3333333333333333} >>> results = recall_metric.compute(predictions=predictions, references=references, average='micro') >>> print(results) {'recall': 0.3333333333333333} >>> results = recall_metric.compute(predictions=predictions, references=references, average='weighted') >>> print(results) {'recall': 0.3333333333333333} >>> results = recall_metric.compute(predictions=predictions, references=references, average=None) >>> print(results) {'recall': array([1., 0., 0.])} """ lowerCAmelCase : Any = """ @article{scikit-learn, title={Scikit-learn: Machine Learning in {P}ython}, author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V. and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P. and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.}, journal={Journal of Machine Learning Research}, volume={12}, pages={2825--2830}, year={2011} """ @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __magic_name__ ( datasets.Metric ): '''simple docstring''' def _lowerCAmelCase ( self ): """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": datasets.Sequence(datasets.Value("""int32""" ) ), """references""": datasets.Sequence(datasets.Value("""int32""" ) ), } if self.config_name == """multilabel""" else { """predictions""": datasets.Value("""int32""" ), """references""": datasets.Value("""int32""" ), } ) , reference_urls=["""https://scikit-learn.org/stable/modules/generated/sklearn.metrics.recall_score.html"""] , ) def _lowerCAmelCase ( self , _a , _a , _a=None , _a=1 , _a="binary" , _a=None , _a="warn" , ): """simple docstring""" lowerCamelCase = recall_score( _a , _a , labels=_a , pos_label=_a , average=_a , sample_weight=_a , zero_division=_a , ) return {"recall": float(_a ) if score.size == 1 else score}
291
1
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase : str = logging.get_logger(__name__) lowerCAmelCase : List[Any] = {"""ctrl""": """https://huggingface.co/ctrl/resolve/main/config.json"""} class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' __UpperCamelCase = "ctrl" __UpperCamelCase = ["past_key_values"] __UpperCamelCase = { "max_position_embeddings": "n_positions", "hidden_size": "n_embd", "num_attention_heads": "n_head", "num_hidden_layers": "n_layer", } def __init__( self , _a=246_534 , _a=256 , _a=1_280 , _a=8_192 , _a=48 , _a=16 , _a=0.1 , _a=0.1 , _a=1e-6 , _a=0.02 , _a=True , **_a , ): """simple docstring""" lowerCamelCase = vocab_size lowerCamelCase = n_positions lowerCamelCase = n_embd lowerCamelCase = n_layer lowerCamelCase = n_head lowerCamelCase = dff lowerCamelCase = resid_pdrop lowerCamelCase = embd_pdrop lowerCamelCase = layer_norm_epsilon lowerCamelCase = initializer_range lowerCamelCase = use_cache super().__init__(**_a )
291
"""simple docstring""" import numpy as np import torch from torch.utils.data import Dataset, IterableDataset from ..utils.generic import ModelOutput class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' def __init__( self , _a , _a , _a ): """simple docstring""" lowerCamelCase = dataset lowerCamelCase = process lowerCamelCase = params def __len__( self ): """simple docstring""" return len(self.dataset ) def __getitem__( self , _a ): """simple docstring""" lowerCamelCase = self.dataset[i] lowerCamelCase = self.process(_a , **self.params ) return processed class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' def __init__( self , _a , _a , _a , _a=None ): """simple docstring""" lowerCamelCase = loader lowerCamelCase = infer lowerCamelCase = params if loader_batch_size == 1: # Let's spare some time by deactivating altogether lowerCamelCase = None lowerCamelCase = loader_batch_size # Internal bookkeeping lowerCamelCase = None lowerCamelCase = None def __len__( self ): """simple docstring""" return len(self.loader ) def __iter__( self ): """simple docstring""" lowerCamelCase = iter(self.loader ) return self def _lowerCAmelCase ( self ): """simple docstring""" if isinstance(self._loader_batch_data , torch.Tensor ): # Batch data is simple tensor, just fetch the slice lowerCamelCase = self._loader_batch_data[self._loader_batch_index] else: # Batch data is assumed to be BaseModelOutput (or dict) lowerCamelCase = {} for k, element in self._loader_batch_data.items(): if isinstance(_a , _a ): # Convert ModelOutput to tuple first lowerCamelCase = element.to_tuple() if isinstance(element[0] , torch.Tensor ): lowerCamelCase = tuple(el[self._loader_batch_index].unsqueeze(0 ) for el in element ) elif isinstance(element[0] , np.ndarray ): lowerCamelCase = tuple(np.expand_dims(el[self._loader_batch_index] , 0 ) for el in element ) continue if k in {"hidden_states", "past_key_values", "attentions"} and isinstance(_a , _a ): # Those are stored as lists of tensors so need specific unbatching. if isinstance(element[0] , torch.Tensor ): lowerCamelCase = tuple(el[self._loader_batch_index].unsqueeze(0 ) for el in element ) elif isinstance(element[0] , np.ndarray ): lowerCamelCase = tuple(np.expand_dims(el[self._loader_batch_index] , 0 ) for el in element ) continue if element is None: # This can happen for optional data that get passed around lowerCamelCase = None elif isinstance(element[self._loader_batch_index] , torch.Tensor ): # Take correct batch data, but make it looked like batch_size=1 # For compatibility with other methods within transformers lowerCamelCase = element[self._loader_batch_index].unsqueeze(0 ) elif isinstance(element[self._loader_batch_index] , np.ndarray ): # Take correct batch data, but make it looked like batch_size=1 # For compatibility with other methods within transformers lowerCamelCase = np.expand_dims(element[self._loader_batch_index] , 0 ) else: # This is typically a list, so no need to `unsqueeze`. lowerCamelCase = element[self._loader_batch_index] # Recreate the element by reusing the original class to make it look # batch_size=1 lowerCamelCase = self._loader_batch_data.__class__(_a ) self._loader_batch_index += 1 return result def _lowerCAmelCase ( self ): """simple docstring""" if self._loader_batch_index is not None and self._loader_batch_index < self.loader_batch_size: # We are currently unrolling a batch so we just need to return # the current item within a batch return self.loader_batch_item() # We're out of items within a batch lowerCamelCase = next(self.iterator ) lowerCamelCase = self.infer(_a , **self.params ) # We now have a batch of "inferred things". if self.loader_batch_size is not None: # Try to infer the size of the batch if isinstance(_a , torch.Tensor ): lowerCamelCase = processed else: lowerCamelCase = list(processed.keys() )[0] lowerCamelCase = processed[key] if isinstance(_a , _a ): lowerCamelCase = len(_a ) else: lowerCamelCase = first_tensor.shape[0] if 0 < observed_batch_size < self.loader_batch_size: # could be last batch so we can't unroll as many # elements. lowerCamelCase = observed_batch_size # Setting internal index to unwrap the batch lowerCamelCase = processed lowerCamelCase = 0 return self.loader_batch_item() else: # We're not unrolling batches return processed class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' def __init__( self , _a , _a , _a , _a=None ): """simple docstring""" super().__init__(_a , _a , _a ) def __iter__( self ): """simple docstring""" lowerCamelCase = iter(self.loader ) lowerCamelCase = None return self def _lowerCAmelCase ( self ): """simple docstring""" if self.subiterator is None: lowerCamelCase = self.infer(next(self.iterator ) , **self.params ) try: # Try to return next item lowerCamelCase = next(self.subiterator ) except StopIteration: # When a preprocess iterator ends, we can start lookig at the next item # ChunkIterator will keep feeding until ALL elements of iterator # all have created their subiterator and have been iterating against. # # Another way to look at it, is we're basically flattening lists of lists # into a single list, but with generators lowerCamelCase = self.infer(next(self.iterator ) , **self.params ) lowerCamelCase = next(self.subiterator ) return processed class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' def __iter__( self ): """simple docstring""" lowerCamelCase = iter(self.loader ) return self def _lowerCAmelCase ( self ): """simple docstring""" # Extremely similar to PipelineIterator in its unpacking mechanism # BUT, we have an extra required item which is the presence of `is_last` # That is because everything is flattened by `PipelineChunkIterator` we # need to keep track of how to regroup here in the original `process` # boundaries so that `process` and `postprocess` see the same data. # This iterator accumulates items (possibly while unbatching) until it # its a `is_last` and then just passes it on to the caller. lowerCamelCase = False lowerCamelCase = [] if self._loader_batch_index is not None and self._loader_batch_index < self.loader_batch_size: while self._loader_batch_index < self.loader_batch_size: lowerCamelCase = self.loader_batch_item() lowerCamelCase = item.pop("""is_last""" ) accumulator.append(_a ) if is_last: return accumulator while not is_last: lowerCamelCase = self.infer(next(self.iterator ) , **self.params ) if self.loader_batch_size is not None: if isinstance(_a , torch.Tensor ): lowerCamelCase = processed else: lowerCamelCase = list(processed.keys() )[0] lowerCamelCase = processed[key] if isinstance(_a , _a ): lowerCamelCase = len(_a ) else: lowerCamelCase = first_tensor.shape[0] if 0 < observed_batch_size < self.loader_batch_size: # could be last batch so we can't unroll as many # elements. lowerCamelCase = observed_batch_size lowerCamelCase = processed lowerCamelCase = 0 while self._loader_batch_index < self.loader_batch_size: lowerCamelCase = self.loader_batch_item() lowerCamelCase = item.pop("""is_last""" ) accumulator.append(_a ) if is_last: return accumulator else: lowerCamelCase = processed lowerCamelCase = item.pop("""is_last""" ) accumulator.append(_a ) return accumulator class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' def __init__( self , _a , _a ): """simple docstring""" lowerCamelCase = dataset lowerCamelCase = key def __len__( self ): """simple docstring""" return len(self.dataset ) def __getitem__( self , _a ): """simple docstring""" return self.dataset[i][self.key] class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' def __init__( self , _a , _a , _a ): """simple docstring""" lowerCamelCase = dataset lowerCamelCase = keya lowerCamelCase = keya def __len__( self ): """simple docstring""" return len(self.dataset ) def __getitem__( self , _a ): """simple docstring""" return {"text": self.dataset[i][self.keya], "text_pair": self.dataset[i][self.keya]}
291
1
"""simple docstring""" import unittest from .lib import ( Matrix, Vector, axpy, square_zero_matrix, unit_basis_vector, zero_vector, ) class __magic_name__ ( unittest.TestCase ): '''simple docstring''' def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = Vector([1, 2, 3] ) self.assertEqual(x.component(0 ) , 1 ) self.assertEqual(x.component(2 ) , 3 ) lowerCamelCase = Vector() def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = Vector([0, 0, 0, 0, 0, 1] ) self.assertEqual(str(_a ) , """(0,0,0,0,0,1)""" ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = Vector([1, 2, 3, 4] ) self.assertEqual(len(_a ) , 4 ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = Vector([1, 2] ) lowerCamelCase = Vector([1, 2, 3, 4, 5] ) lowerCamelCase = Vector([0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ) lowerCamelCase = Vector([1, -1, 1, -1, 2, -3, 4, -5] ) self.assertAlmostEqual(x.euclidean_length() , 2.236 , 3 ) self.assertAlmostEqual(y.euclidean_length() , 7.416 , 3 ) self.assertEqual(z.euclidean_length() , 0 ) self.assertAlmostEqual(w.euclidean_length() , 7.616 , 3 ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = Vector([1, 2, 3] ) lowerCamelCase = Vector([1, 1, 1] ) self.assertEqual((x + y).component(0 ) , 2 ) self.assertEqual((x + y).component(1 ) , 3 ) self.assertEqual((x + y).component(2 ) , 4 ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = Vector([1, 2, 3] ) lowerCamelCase = Vector([1, 1, 1] ) self.assertEqual((x - y).component(0 ) , 0 ) self.assertEqual((x - y).component(1 ) , 1 ) self.assertEqual((x - y).component(2 ) , 2 ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = Vector([1, 2, 3] ) lowerCamelCase = Vector([2, -1, 4] ) # for test of dot product lowerCamelCase = Vector([1, -2, -1] ) self.assertEqual(str(x * 3.0 ) , """(3.0,6.0,9.0)""" ) self.assertEqual((a * b) , 0 ) def _lowerCAmelCase ( self ): """simple docstring""" self.assertEqual(str(zero_vector(10 ) ).count("""0""" ) , 10 ) def _lowerCAmelCase ( self ): """simple docstring""" self.assertEqual(str(unit_basis_vector(3 , 1 ) ) , """(0,1,0)""" ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = Vector([1, 2, 3] ) lowerCamelCase = Vector([1, 0, 1] ) self.assertEqual(str(axpy(2 , _a , _a ) ) , """(3,4,7)""" ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = Vector([1, 0, 0, 0, 0, 0] ) lowerCamelCase = x.copy() self.assertEqual(str(_a ) , str(_a ) ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = Vector([1, 0, 0] ) x.change_component(0 , 0 ) x.change_component(1 , 1 ) self.assertEqual(str(_a ) , """(0,1,0)""" ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]] , 3 , 3 ) self.assertEqual("""|1,2,3|\n|2,4,5|\n|6,7,8|\n""" , str(_a ) ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]] , 3 , 3 ) lowerCamelCase = [[-3, -14, -10], [-5, -10, -5], [-2, -1, 0]] for x in range(a.height() ): for y in range(a.width() ): self.assertEqual(minors[x][y] , a.minor(_a , _a ) ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]] , 3 , 3 ) lowerCamelCase = [[-3, 14, -10], [5, -10, 5], [-2, 1, 0]] for x in range(a.height() ): for y in range(a.width() ): self.assertEqual(cofactors[x][y] , a.cofactor(_a , _a ) ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]] , 3 , 3 ) self.assertEqual(-5 , a.determinant() ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]] , 3 , 3 ) lowerCamelCase = Vector([1, 2, 3] ) self.assertEqual("""(14,32,50)""" , str(a * x ) ) self.assertEqual("""|2,4,6|\n|8,10,12|\n|14,16,18|\n""" , str(a * 2 ) ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]] , 3 , 3 ) a.change_component(0 , 2 , 5 ) self.assertEqual("""|1,2,5|\n|2,4,5|\n|6,7,8|\n""" , str(_a ) ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]] , 3 , 3 ) self.assertEqual(7 , a.component(2 , 1 ) , 0.01 ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]] , 3 , 3 ) lowerCamelCase = Matrix([[1, 2, 7], [2, 4, 5], [6, 7, 10]] , 3 , 3 ) self.assertEqual("""|2,4,10|\n|4,8,10|\n|12,14,18|\n""" , str(a + b ) ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]] , 3 , 3 ) lowerCamelCase = Matrix([[1, 2, 7], [2, 4, 5], [6, 7, 10]] , 3 , 3 ) self.assertEqual("""|0,0,-4|\n|0,0,0|\n|0,0,-2|\n""" , str(a - b ) ) def _lowerCAmelCase ( self ): """simple docstring""" self.assertEqual( """|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n""" , str(square_zero_matrix(5 ) ) , ) if __name__ == "__main__": unittest.main()
291
"""simple docstring""" def a__ ( snake_case__ ) -> bool: lowerCamelCase = (1 + 24 * n) ** 0.5 return ((1 + root) / 6) % 1 == 0 def a__ ( snake_case__ = 50_00 ) -> int: lowerCamelCase = [(i * (3 * i - 1)) // 2 for i in range(1 , snake_case__ )] for i, pentagonal_i in enumerate(snake_case__ ): for j in range(snake_case__ , len(snake_case__ ) ): lowerCamelCase = pentagonal_nums[j] lowerCamelCase = pentagonal_i + pentagonal_j lowerCamelCase = pentagonal_j - pentagonal_i if is_pentagonal(snake_case__ ) and is_pentagonal(snake_case__ ): return b return -1 if __name__ == "__main__": print(F"""{solution() = }""")
291
1
"""simple docstring""" import json import os import pickle import shutil import tempfile from unittest import TestCase from unittest.mock import patch import numpy as np from datasets import Dataset from transformers import is_faiss_available from transformers.models.bart.configuration_bart import BartConfig from transformers.models.bart.tokenization_bart import BartTokenizer from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES as DPR_VOCAB_FILES_NAMES from transformers.models.dpr.configuration_dpr import DPRConfig from transformers.models.dpr.tokenization_dpr import DPRContextEncoderTokenizer, DPRQuestionEncoderTokenizer from transformers.models.rag.configuration_rag import RagConfig from transformers.models.rag.retrieval_rag import CustomHFIndex, RagRetriever from transformers.models.roberta.tokenization_roberta import VOCAB_FILES_NAMES as BART_VOCAB_FILES_NAMES from transformers.testing_utils import require_faiss, require_sentencepiece, require_tokenizers, require_torch if is_faiss_available(): import faiss @require_faiss class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = tempfile.mkdtemp() lowerCamelCase = 8 # DPR tok lowerCamelCase = [ """[UNK]""", """[CLS]""", """[SEP]""", """[PAD]""", """[MASK]""", """want""", """##want""", """##ed""", """wa""", """un""", """runn""", """##ing""", """,""", """low""", """lowest""", ] lowerCamelCase = os.path.join(self.tmpdirname , """dpr_tokenizer""" ) os.makedirs(_a , exist_ok=_a ) lowerCamelCase = os.path.join(_a , DPR_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] ) ) # BART tok lowerCamelCase = [ """l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """\u0120""", """\u0120l""", """\u0120n""", """\u0120lo""", """\u0120low""", """er""", """\u0120lowest""", """\u0120newer""", """\u0120wider""", """<unk>""", ] lowerCamelCase = dict(zip(_a , range(len(_a ) ) ) ) lowerCamelCase = ["""#version: 0.2""", """\u0120 l""", """\u0120l o""", """\u0120lo w""", """e r""", """"""] lowerCamelCase = {"""unk_token""": """<unk>"""} lowerCamelCase = os.path.join(self.tmpdirname , """bart_tokenizer""" ) os.makedirs(_a , exist_ok=_a ) lowerCamelCase = os.path.join(_a , BART_VOCAB_FILES_NAMES["""vocab_file"""] ) lowerCamelCase = os.path.join(_a , BART_VOCAB_FILES_NAMES["""merges_file"""] ) with open(self.vocab_file , """w""" , encoding="""utf-8""" ) as fp: fp.write(json.dumps(_a ) + """\n""" ) with open(self.merges_file , """w""" , encoding="""utf-8""" ) as fp: fp.write("""\n""".join(_a ) ) def _lowerCAmelCase ( self ): """simple docstring""" return DPRQuestionEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , """dpr_tokenizer""" ) ) def _lowerCAmelCase ( self ): """simple docstring""" return DPRContextEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , """dpr_tokenizer""" ) ) def _lowerCAmelCase ( self ): """simple docstring""" return BartTokenizer.from_pretrained(os.path.join(self.tmpdirname , """bart_tokenizer""" ) ) def _lowerCAmelCase ( self ): """simple docstring""" shutil.rmtree(self.tmpdirname ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = Dataset.from_dict( { """id""": ["""0""", """1"""], """text""": ["""foo""", """bar"""], """title""": ["""Foo""", """Bar"""], """embeddings""": [np.ones(self.retrieval_vector_size ), 2 * np.ones(self.retrieval_vector_size )], } ) dataset.add_faiss_index("""embeddings""" , string_factory="""Flat""" , metric_type=faiss.METRIC_INNER_PRODUCT ) return dataset def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.get_dummy_dataset() lowerCamelCase = RagConfig( retrieval_vector_size=self.retrieval_vector_size , question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() , ) with patch("""transformers.models.rag.retrieval_rag.load_dataset""" ) as mock_load_dataset: lowerCamelCase = dataset lowerCamelCase = RagRetriever( _a , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , ) return retriever def _lowerCAmelCase ( self , _a ): """simple docstring""" lowerCamelCase = self.get_dummy_dataset() lowerCamelCase = RagConfig( retrieval_vector_size=self.retrieval_vector_size , question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() , index_name="""custom""" , ) if from_disk: lowerCamelCase = os.path.join(self.tmpdirname , """dataset""" ) lowerCamelCase = os.path.join(self.tmpdirname , """index.faiss""" ) dataset.get_index("""embeddings""" ).save(os.path.join(self.tmpdirname , """index.faiss""" ) ) dataset.drop_index("""embeddings""" ) dataset.save_to_disk(os.path.join(self.tmpdirname , """dataset""" ) ) del dataset lowerCamelCase = RagRetriever( _a , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , ) else: lowerCamelCase = RagRetriever( _a , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , index=CustomHFIndex(config.retrieval_vector_size , _a ) , ) return retriever def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = Dataset.from_dict( { """id""": ["""0""", """1"""], """text""": ["""foo""", """bar"""], """title""": ["""Foo""", """Bar"""], """embeddings""": [np.ones(self.retrieval_vector_size + 1 ), 2 * np.ones(self.retrieval_vector_size + 1 )], } ) dataset.add_faiss_index("""embeddings""" , string_factory="""Flat""" , metric_type=faiss.METRIC_INNER_PRODUCT ) lowerCamelCase = os.path.join(self.tmpdirname , """hf_bert_base.hnswSQ8_correct_phi_128.c_index""" ) dataset.save_faiss_index("""embeddings""" , index_file_name + """.index.dpr""" ) pickle.dump(dataset["""id"""] , open(index_file_name + """.index_meta.dpr""" , """wb""" ) ) lowerCamelCase = os.path.join(self.tmpdirname , """psgs_w100.tsv.pkl""" ) lowerCamelCase = {sample["""id"""]: [sample["""text"""], sample["""title"""]] for sample in dataset} pickle.dump(_a , open(_a , """wb""" ) ) lowerCamelCase = RagConfig( retrieval_vector_size=self.retrieval_vector_size , question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() , index_name="""legacy""" , index_path=self.tmpdirname , ) lowerCamelCase = RagRetriever( _a , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() ) return retriever def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = 1 lowerCamelCase = self.get_dummy_canonical_hf_index_retriever() lowerCamelCase = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) lowerCamelCase , lowerCamelCase , lowerCamelCase = retriever.retrieve(_a , n_docs=_a ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(_a ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ["""embeddings""", """id""", """text""", """title"""] ) self.assertEqual(len(doc_dicts[0]["""id"""] ) , _a ) self.assertEqual(doc_dicts[0]["""id"""][0] , """1""" ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]["""id"""][0] , """0""" ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.get_dummy_canonical_hf_index_retriever() with tempfile.TemporaryDirectory() as tmp_dirname: with patch("""transformers.models.rag.retrieval_rag.load_dataset""" ) as mock_load_dataset: lowerCamelCase = self.get_dummy_dataset() retriever.save_pretrained(_a ) lowerCamelCase = RagRetriever.from_pretrained(_a ) self.assertIsInstance(_a , _a ) lowerCamelCase = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) lowerCamelCase = retriever.retrieve(_a , n_docs=1 ) self.assertTrue(out is not None ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = 1 lowerCamelCase = self.get_dummy_custom_hf_index_retriever(from_disk=_a ) lowerCamelCase = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) lowerCamelCase , lowerCamelCase , lowerCamelCase = retriever.retrieve(_a , n_docs=_a ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(_a ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ["""embeddings""", """id""", """text""", """title"""] ) self.assertEqual(len(doc_dicts[0]["""id"""] ) , _a ) self.assertEqual(doc_dicts[0]["""id"""][0] , """1""" ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]["""id"""][0] , """0""" ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.get_dummy_custom_hf_index_retriever(from_disk=_a ) with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(_a ) lowerCamelCase = RagRetriever.from_pretrained(_a ) self.assertIsInstance(_a , _a ) lowerCamelCase = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) lowerCamelCase = retriever.retrieve(_a , n_docs=1 ) self.assertTrue(out is not None ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = 1 lowerCamelCase = self.get_dummy_custom_hf_index_retriever(from_disk=_a ) lowerCamelCase = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) lowerCamelCase , lowerCamelCase , lowerCamelCase = retriever.retrieve(_a , n_docs=_a ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(_a ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ["""embeddings""", """id""", """text""", """title"""] ) self.assertEqual(len(doc_dicts[0]["""id"""] ) , _a ) self.assertEqual(doc_dicts[0]["""id"""][0] , """1""" ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]["""id"""][0] , """0""" ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.get_dummy_custom_hf_index_retriever(from_disk=_a ) with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(_a ) lowerCamelCase = RagRetriever.from_pretrained(_a ) self.assertIsInstance(_a , _a ) lowerCamelCase = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) lowerCamelCase = retriever.retrieve(_a , n_docs=1 ) self.assertTrue(out is not None ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = 1 lowerCamelCase = self.get_dummy_legacy_index_retriever() lowerCamelCase = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) lowerCamelCase , lowerCamelCase , lowerCamelCase = retriever.retrieve(_a , n_docs=_a ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(_a ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ["""text""", """title"""] ) self.assertEqual(len(doc_dicts[0]["""text"""] ) , _a ) self.assertEqual(doc_dicts[0]["""text"""][0] , """bar""" ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]["""text"""][0] , """foo""" ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.get_dummy_legacy_index_retriever() with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(_a ) lowerCamelCase = RagRetriever.from_pretrained(_a ) self.assertIsInstance(_a , _a ) lowerCamelCase = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) lowerCamelCase = retriever.retrieve(_a , n_docs=1 ) self.assertTrue(out is not None ) @require_torch @require_tokenizers @require_sentencepiece def _lowerCAmelCase ( self ): """simple docstring""" import torch lowerCamelCase = 1 lowerCamelCase = self.get_dummy_canonical_hf_index_retriever() lowerCamelCase = [[5, 7], [10, 11]] lowerCamelCase = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) lowerCamelCase = retriever(_a , _a , prefix=retriever.config.generator.prefix , n_docs=_a ) lowerCamelCase , lowerCamelCase , lowerCamelCase = ( out["""context_input_ids"""], out["""context_attention_mask"""], out["""retrieved_doc_embeds"""], ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertIsInstance(_a , _a ) self.assertIsInstance(_a , _a ) self.assertIsInstance(_a , np.ndarray ) lowerCamelCase = retriever( _a , _a , prefix=retriever.config.generator.prefix , n_docs=_a , return_tensors="""pt""" , ) lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase = ( # noqa: F841 out["""context_input_ids"""], out["""context_attention_mask"""], out["""retrieved_doc_embeds"""], out["""doc_ids"""], ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertIsInstance(_a , torch.Tensor ) self.assertIsInstance(_a , torch.Tensor ) self.assertIsInstance(_a , torch.Tensor ) @require_torch @require_tokenizers @require_sentencepiece def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.get_dpr_ctx_encoder_tokenizer() lowerCamelCase = 1 lowerCamelCase = self.get_dummy_custom_hf_index_retriever(from_disk=_a ) retriever.set_ctx_encoder_tokenizer(_a ) lowerCamelCase = [[5, 7], [10, 11]] lowerCamelCase = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) lowerCamelCase = retriever(_a , _a , prefix=retriever.config.generator.prefix , n_docs=_a ) self.assertEqual( len(_a ) , 6 ) # check whether the retriever output consist of 6 attributes including tokenized docs self.assertEqual( all(k in out for k in ("""tokenized_doc_ids""", """tokenized_doc_attention_mask""") ) , _a ) # check for doc token related keys in dictionary.
291
"""simple docstring""" from pickle import UnpicklingError import jax import jax.numpy as jnp import numpy as np from flax.serialization import from_bytes from flax.traverse_util import flatten_dict from ..utils import logging lowerCAmelCase : Tuple = logging.get_logger(__name__) def a__ ( snake_case__ , snake_case__ ) -> Tuple: try: with open(snake_case__ , """rb""" ) as flax_state_f: lowerCamelCase = from_bytes(snake_case__ , flax_state_f.read() ) except UnpicklingError as e: try: with open(snake_case__ ) as f: if f.read().startswith("""version""" ): raise OSError( """You seem to have cloned a repository without having git-lfs installed. Please""" """ install git-lfs and run `git lfs install` followed by `git lfs pull` in the""" """ folder you cloned.""" ) else: raise ValueError from e except (UnicodeDecodeError, ValueError): raise EnvironmentError(F'Unable to convert {model_file} to Flax deserializable object. ' ) return load_flax_weights_in_pytorch_model(snake_case__ , snake_case__ ) def a__ ( snake_case__ , snake_case__ ) -> Tuple: try: import torch # noqa: F401 except ImportError: logger.error( """Loading Flax weights in PyTorch requires both PyTorch and Flax to be installed. Please see""" """ https://pytorch.org/ and https://flax.readthedocs.io/en/latest/installation.html for installation""" """ instructions.""" ) raise # check if we have bf16 weights lowerCamelCase = flatten_dict(jax.tree_util.tree_map(lambda snake_case__ : x.dtype == jnp.bfloataa , snake_case__ ) ).values() if any(snake_case__ ): # convert all weights to fp32 if they are bf16 since torch.from_numpy can-not handle bf16 # and bf16 is not fully supported in PT yet. logger.warning( """Found ``bfloat16`` weights in Flax model. Casting all ``bfloat16`` weights to ``float32`` """ """before loading those in PyTorch model.""" ) lowerCamelCase = jax.tree_util.tree_map( lambda snake_case__ : params.astype(np.floataa ) if params.dtype == jnp.bfloataa else params , snake_case__ ) lowerCamelCase = """""" lowerCamelCase = flatten_dict(snake_case__ , sep=""".""" ) lowerCamelCase = pt_model.state_dict() # keep track of unexpected & missing keys lowerCamelCase = [] lowerCamelCase = set(pt_model_dict.keys() ) for flax_key_tuple, flax_tensor in flax_state_dict.items(): lowerCamelCase = flax_key_tuple.split(""".""" ) if flax_key_tuple_array[-1] == "kernel" and flax_tensor.ndim == 4: lowerCamelCase = flax_key_tuple_array[:-1] + ["""weight"""] lowerCamelCase = jnp.transpose(snake_case__ , (3, 2, 0, 1) ) elif flax_key_tuple_array[-1] == "kernel": lowerCamelCase = flax_key_tuple_array[:-1] + ["""weight"""] lowerCamelCase = flax_tensor.T elif flax_key_tuple_array[-1] == "scale": lowerCamelCase = flax_key_tuple_array[:-1] + ["""weight"""] if "time_embedding" not in flax_key_tuple_array: for i, flax_key_tuple_string in enumerate(snake_case__ ): lowerCamelCase = ( flax_key_tuple_string.replace("""_0""" , """.0""" ) .replace("""_1""" , """.1""" ) .replace("""_2""" , """.2""" ) .replace("""_3""" , """.3""" ) .replace("""_4""" , """.4""" ) .replace("""_5""" , """.5""" ) .replace("""_6""" , """.6""" ) .replace("""_7""" , """.7""" ) .replace("""_8""" , """.8""" ) .replace("""_9""" , """.9""" ) ) lowerCamelCase = """.""".join(snake_case__ ) if flax_key in pt_model_dict: if flax_tensor.shape != pt_model_dict[flax_key].shape: raise ValueError( F'Flax checkpoint seems to be incorrect. Weight {flax_key_tuple} was expected ' F'to be of shape {pt_model_dict[flax_key].shape}, but is {flax_tensor.shape}.' ) else: # add weight to pytorch dict lowerCamelCase = np.asarray(snake_case__ ) if not isinstance(snake_case__ , np.ndarray ) else flax_tensor lowerCamelCase = torch.from_numpy(snake_case__ ) # remove from missing keys missing_keys.remove(snake_case__ ) else: # weight is not expected by PyTorch model unexpected_keys.append(snake_case__ ) pt_model.load_state_dict(snake_case__ ) # re-transform missing_keys to list lowerCamelCase = list(snake_case__ ) if len(snake_case__ ) > 0: logger.warning( """Some weights of the Flax model were not used when initializing the PyTorch model""" F' {pt_model.__class__.__name__}: {unexpected_keys}\n- This IS expected if you are initializing' F' {pt_model.__class__.__name__} from a Flax model trained on another task or with another architecture' """ (e.g. initializing a BertForSequenceClassification model from a FlaxBertForPreTraining model).\n- This""" F' IS NOT expected if you are initializing {pt_model.__class__.__name__} from a Flax model that you expect' """ to be exactly identical (e.g. initializing a BertForSequenceClassification model from a""" """ FlaxBertForSequenceClassification model).""" ) if len(snake_case__ ) > 0: logger.warning( F'Some weights of {pt_model.__class__.__name__} were not initialized from the Flax model and are newly' F' initialized: {missing_keys}\nYou should probably TRAIN this model on a down-stream task to be able to' """ use it for predictions and inference.""" ) return pt_model
291
1
"""simple docstring""" from __future__ import annotations def a__ ( snake_case__ , snake_case__ ) -> list[list[int]]: lowerCamelCase = [] lowerCamelCase = [] lowerCamelCase = 0 lowerCamelCase = sum(snake_case__ ) create_state_space_tree(snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ ) return result def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ) -> None: if sum(snake_case__ ) > max_sum or (remaining_nums_sum + sum(snake_case__ )) < max_sum: return if sum(snake_case__ ) == max_sum: result.append(snake_case__ ) return for index in range(snake_case__ , len(snake_case__ ) ): create_state_space_tree( snake_case__ , snake_case__ , index + 1 , [*path, nums[index]] , snake_case__ , remaining_nums_sum - nums[index] , ) lowerCAmelCase : Any = [3, 34, 4, 12, 5, 2] lowerCAmelCase : Tuple = 9 lowerCAmelCase : Union[str, Any] = generate_sum_of_subsets_soln(nums, max_sum) print(*result)
291
"""simple docstring""" import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_xlnet import XLNetTokenizer else: lowerCAmelCase : int = None lowerCAmelCase : Tuple = logging.get_logger(__name__) lowerCAmelCase : Tuple = {"""vocab_file""": """spiece.model""", """tokenizer_file""": """tokenizer.json"""} lowerCAmelCase : Union[str, Any] = { """vocab_file""": { """xlnet-base-cased""": """https://huggingface.co/xlnet-base-cased/resolve/main/spiece.model""", """xlnet-large-cased""": """https://huggingface.co/xlnet-large-cased/resolve/main/spiece.model""", }, """tokenizer_file""": { """xlnet-base-cased""": """https://huggingface.co/xlnet-base-cased/resolve/main/tokenizer.json""", """xlnet-large-cased""": """https://huggingface.co/xlnet-large-cased/resolve/main/tokenizer.json""", }, } lowerCAmelCase : Optional[int] = { """xlnet-base-cased""": None, """xlnet-large-cased""": None, } lowerCAmelCase : Union[str, Any] = """▁""" # Segments (not really needed) lowerCAmelCase : str = 0 lowerCAmelCase : Optional[int] = 1 lowerCAmelCase : Tuple = 2 lowerCAmelCase : Optional[Any] = 3 lowerCAmelCase : List[Any] = 4 class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' __UpperCamelCase = VOCAB_FILES_NAMES __UpperCamelCase = PRETRAINED_VOCAB_FILES_MAP __UpperCamelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCamelCase = "left" __UpperCamelCase = XLNetTokenizer def __init__( self , _a=None , _a=None , _a=False , _a=True , _a=False , _a="<s>" , _a="</s>" , _a="<unk>" , _a="<sep>" , _a="<pad>" , _a="<cls>" , _a="<mask>" , _a=["<eop>", "<eod>"] , **_a , ): """simple docstring""" # Mask token behave like a normal word, i.e. include the space before it lowerCamelCase = AddedToken(_a , lstrip=_a , rstrip=_a ) if isinstance(_a , _a ) else mask_token super().__init__( vocab_file=_a , tokenizer_file=_a , do_lower_case=_a , remove_space=_a , keep_accents=_a , bos_token=_a , eos_token=_a , unk_token=_a , sep_token=_a , pad_token=_a , cls_token=_a , mask_token=_a , additional_special_tokens=_a , **_a , ) lowerCamelCase = 3 lowerCamelCase = do_lower_case lowerCamelCase = remove_space lowerCamelCase = keep_accents lowerCamelCase = vocab_file lowerCamelCase = False if not self.vocab_file else True def _lowerCAmelCase ( self , _a , _a = None ): """simple docstring""" lowerCamelCase = [self.sep_token_id] lowerCamelCase = [self.cls_token_id] if token_ids_a is None: return token_ids_a + sep + cls return token_ids_a + sep + token_ids_a + sep + cls def _lowerCAmelCase ( self , _a , _a = None ): """simple docstring""" lowerCamelCase = [self.sep_token_id] lowerCamelCase = [2] if token_ids_a is None: return len(token_ids_a + sep ) * [0] + cls_segment_id return len(token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] + cls_segment_id def _lowerCAmelCase ( self , _a , _a = 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(_a ): logger.error(f'Vocabulary path ({save_directory}) should be a directory' ) return lowerCamelCase = os.path.join( _a , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_a ): copyfile(self.vocab_file , _a ) return (out_vocab_file,)
291
1
"""simple docstring""" from __future__ import annotations def a__ ( snake_case__ ) -> int: # preprocessing the first row for i in range(1 , len(matrix[0] ) ): matrix[0][i] += matrix[0][i - 1] # preprocessing the first column for i in range(1 , len(snake_case__ ) ): matrix[i][0] += matrix[i - 1][0] # updating the path cost for current position for i in range(1 , len(snake_case__ ) ): for j in range(1 , len(matrix[0] ) ): matrix[i][j] += min(matrix[i - 1][j] , matrix[i][j - 1] ) return matrix[-1][-1] if __name__ == "__main__": import doctest doctest.testmod()
291
"""simple docstring""" import unittest from transformers import ( MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING, TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING, TextaTextGenerationPipeline, pipeline, ) from transformers.testing_utils import is_pipeline_test, require_tf, require_torch from transformers.utils import is_torch_available from .test_pipelines_common import ANY if is_torch_available(): import torch @is_pipeline_test class __magic_name__ ( unittest.TestCase ): '''simple docstring''' __UpperCamelCase = MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING __UpperCamelCase = TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING def _lowerCAmelCase ( self , _a , _a , _a ): """simple docstring""" lowerCamelCase = TextaTextGenerationPipeline(model=_a , tokenizer=_a ) return generator, ["Something to write", "Something else"] def _lowerCAmelCase ( self , _a , _a ): """simple docstring""" lowerCamelCase = generator("""Something there""" ) self.assertEqual(_a , [{"""generated_text""": ANY(_a )}] ) # These are encoder decoder, they don't just append to incoming string self.assertFalse(outputs[0]["""generated_text"""].startswith("""Something there""" ) ) lowerCamelCase = generator(["""This is great !""", """Something else"""] , num_return_sequences=2 , do_sample=_a ) self.assertEqual( _a , [ [{"""generated_text""": ANY(_a )}, {"""generated_text""": ANY(_a )}], [{"""generated_text""": ANY(_a )}, {"""generated_text""": ANY(_a )}], ] , ) lowerCamelCase = generator( ["""This is great !""", """Something else"""] , num_return_sequences=2 , batch_size=2 , do_sample=_a ) self.assertEqual( _a , [ [{"""generated_text""": ANY(_a )}, {"""generated_text""": ANY(_a )}], [{"""generated_text""": ANY(_a )}, {"""generated_text""": ANY(_a )}], ] , ) with self.assertRaises(_a ): generator(4 ) @require_torch def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = pipeline("""text2text-generation""" , model="""patrickvonplaten/t5-tiny-random""" , framework="""pt""" ) # do_sample=False necessary for reproducibility lowerCamelCase = generator("""Something there""" , do_sample=_a ) self.assertEqual(_a , [{"""generated_text""": """"""}] ) lowerCamelCase = 3 lowerCamelCase = generator( """Something there""" , num_return_sequences=_a , num_beams=_a , ) lowerCamelCase = [ {"""generated_text""": """Beide Beide Beide Beide Beide Beide Beide Beide Beide"""}, {"""generated_text""": """Beide Beide Beide Beide Beide Beide Beide Beide"""}, {"""generated_text""": """"""}, ] self.assertEqual(_a , _a ) lowerCamelCase = generator("""This is a test""" , do_sample=_a , num_return_sequences=2 , return_tensors=_a ) self.assertEqual( _a , [ {"""generated_token_ids""": ANY(torch.Tensor )}, {"""generated_token_ids""": ANY(torch.Tensor )}, ] , ) lowerCamelCase = generator.model.config.eos_token_id lowerCamelCase = """<pad>""" lowerCamelCase = generator( ["""This is a test""", """This is a second test"""] , do_sample=_a , num_return_sequences=2 , batch_size=2 , return_tensors=_a , ) self.assertEqual( _a , [ [ {"""generated_token_ids""": ANY(torch.Tensor )}, {"""generated_token_ids""": ANY(torch.Tensor )}, ], [ {"""generated_token_ids""": ANY(torch.Tensor )}, {"""generated_token_ids""": ANY(torch.Tensor )}, ], ] , ) @require_tf def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = pipeline("""text2text-generation""" , model="""patrickvonplaten/t5-tiny-random""" , framework="""tf""" ) # do_sample=False necessary for reproducibility lowerCamelCase = generator("""Something there""" , do_sample=_a ) self.assertEqual(_a , [{"""generated_text""": """"""}] )
291
1
"""simple docstring""" import argparse import json import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from torchvision import transforms from transformers import BitImageProcessor, FocalNetConfig, FocalNetForImageClassification from transformers.image_utils import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, PILImageResampling def a__ ( snake_case__ ) -> Optional[Any]: lowerCamelCase = [2, 2, 6, 2] if """tiny""" in model_name else [2, 2, 18, 2] lowerCamelCase = True if """large""" in model_name or """huge""" in model_name else False lowerCamelCase = True if """large""" in model_name or """huge""" in model_name else False lowerCamelCase = True if """large""" in model_name or """huge""" in model_name else False if "large" in model_name or "xlarge" in model_name or "huge" in model_name: if "fl3" in model_name: lowerCamelCase = [3, 3, 3, 3] lowerCamelCase = [5, 5, 5, 5] elif "fl4" in model_name: lowerCamelCase = [4, 4, 4, 4] lowerCamelCase = [3, 3, 3, 3] if "tiny" in model_name or "small" in model_name or "base" in model_name: lowerCamelCase = [3, 3, 3, 3] if "lrf" in model_name: lowerCamelCase = [3, 3, 3, 3] else: lowerCamelCase = [2, 2, 2, 2] if "tiny" in model_name: lowerCamelCase = 96 elif "small" in model_name: lowerCamelCase = 96 elif "base" in model_name: lowerCamelCase = 1_28 elif "large" in model_name: lowerCamelCase = 1_92 elif "xlarge" in model_name: lowerCamelCase = 2_56 elif "huge" in model_name: lowerCamelCase = 3_52 # set label information lowerCamelCase = """huggingface/label-files""" if "large" in model_name or "huge" in model_name: lowerCamelCase = """imagenet-22k-id2label.json""" else: lowerCamelCase = """imagenet-1k-id2label.json""" lowerCamelCase = json.load(open(hf_hub_download(snake_case__ , snake_case__ , repo_type="""dataset""" ) , """r""" ) ) lowerCamelCase = {int(snake_case__ ): v for k, v in idalabel.items()} lowerCamelCase = {v: k for k, v in idalabel.items()} lowerCamelCase = FocalNetConfig( embed_dim=snake_case__ , depths=snake_case__ , focal_levels=snake_case__ , focal_windows=snake_case__ , use_conv_embed=snake_case__ , idalabel=snake_case__ , labelaid=snake_case__ , use_post_layernorm=snake_case__ , use_layerscale=snake_case__ , ) return config def a__ ( snake_case__ ) -> Union[str, Any]: if "patch_embed.proj" in name: lowerCamelCase = name.replace("""patch_embed.proj""" , """embeddings.patch_embeddings.projection""" ) if "patch_embed.norm" in name: lowerCamelCase = name.replace("""patch_embed.norm""" , """embeddings.norm""" ) if "layers" in name: lowerCamelCase = """encoder.""" + name if "encoder.layers" in name: lowerCamelCase = name.replace("""encoder.layers""" , """encoder.stages""" ) if "downsample.proj" in name: lowerCamelCase = name.replace("""downsample.proj""" , """downsample.projection""" ) if "blocks" in name: lowerCamelCase = name.replace("""blocks""" , """layers""" ) if "modulation.f.weight" in name or "modulation.f.bias" in name: lowerCamelCase = name.replace("""modulation.f""" , """modulation.projection_in""" ) if "modulation.h.weight" in name or "modulation.h.bias" in name: lowerCamelCase = name.replace("""modulation.h""" , """modulation.projection_context""" ) if "modulation.proj.weight" in name or "modulation.proj.bias" in name: lowerCamelCase = name.replace("""modulation.proj""" , """modulation.projection_out""" ) if name == "norm.weight": lowerCamelCase = """layernorm.weight""" if name == "norm.bias": lowerCamelCase = """layernorm.bias""" if "head" in name: lowerCamelCase = name.replace("""head""" , """classifier""" ) else: lowerCamelCase = """focalnet.""" + name return name def a__ ( snake_case__ , snake_case__ , snake_case__=False ) -> Any: # fmt: off lowerCamelCase = { """focalnet-tiny""": """https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_tiny_srf.pth""", """focalnet-tiny-lrf""": """https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_tiny_lrf.pth""", """focalnet-small""": """https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_small_srf.pth""", """focalnet-small-lrf""": """https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_small_lrf.pth""", """focalnet-base""": """https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_base_srf.pth""", """focalnet-base-lrf""": """https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_base_lrf.pth""", """focalnet-large-lrf-fl3""": """https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_large_lrf_384.pth""", """focalnet-large-lrf-fl4""": """https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_large_lrf_384_fl4.pth""", """focalnet-xlarge-lrf-fl3""": """https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_xlarge_lrf_384.pth""", """focalnet-xlarge-lrf-fl4""": """https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_xlarge_lrf_384_fl4.pth""", } # fmt: on lowerCamelCase = model_name_to_url[model_name] print("""Checkpoint URL: """ , snake_case__ ) lowerCamelCase = torch.hub.load_state_dict_from_url(snake_case__ , map_location="""cpu""" )["""model"""] # rename keys for key in state_dict.copy().keys(): lowerCamelCase = state_dict.pop(snake_case__ ) lowerCamelCase = val lowerCamelCase = get_focalnet_config(snake_case__ ) lowerCamelCase = FocalNetForImageClassification(snake_case__ ) model.eval() # load state dict model.load_state_dict(snake_case__ ) # verify conversion lowerCamelCase = """http://images.cocodataset.org/val2017/000000039769.jpg""" lowerCamelCase = BitImageProcessor( do_resize=snake_case__ , size={"""shortest_edge""": 2_56} , resample=PILImageResampling.BILINEAR , do_center_crop=snake_case__ , crop_size=2_24 , do_normalize=snake_case__ , image_mean=snake_case__ , image_std=snake_case__ , ) lowerCamelCase = Image.open(requests.get(snake_case__ , stream=snake_case__ ).raw ) lowerCamelCase = processor(images=snake_case__ , return_tensors="""pt""" ) lowerCamelCase = transforms.Compose( [ transforms.Resize(2_56 ), transforms.CenterCrop(2_24 ), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406] , std=[0.229, 0.224, 0.225] ), ] ) lowerCamelCase = image_transforms(snake_case__ ).unsqueeze(0 ) # verify pixel_values assert torch.allclose(inputs.pixel_values , snake_case__ , atol=1E-4 ) lowerCamelCase = model(**snake_case__ ) lowerCamelCase = outputs.logits.argmax(-1 ).item() print("""Predicted class:""" , model.config.idalabel[predicted_class_idx] ) print("""First values of logits:""" , outputs.logits[0, :3] ) if model_name == "focalnet-tiny": lowerCamelCase = torch.tensor([0.2166, -0.4368, 0.2191] ) elif model_name == "focalnet-tiny-lrf": lowerCamelCase = torch.tensor([1.1669, 0.0125, -0.1695] ) elif model_name == "focalnet-small": lowerCamelCase = torch.tensor([0.4917, -0.0430, 0.1341] ) elif model_name == "focalnet-small-lrf": lowerCamelCase = torch.tensor([-0.2588, -0.5342, -0.2331] ) elif model_name == "focalnet-base": lowerCamelCase = torch.tensor([-0.1655, -0.4090, -0.1730] ) elif model_name == "focalnet-base-lrf": lowerCamelCase = torch.tensor([0.5306, -0.0483, -0.3928] ) assert torch.allclose(outputs.logits[0, :3] , snake_case__ , atol=1E-4 ) print("""Looks ok!""" ) if pytorch_dump_folder_path is not None: print(F'Saving model and processor of {model_name} to {pytorch_dump_folder_path}' ) model.save_pretrained(snake_case__ ) processor.save_pretrained(snake_case__ ) if push_to_hub: print(F'Pushing model and processor of {model_name} to the hub...' ) model.push_to_hub(F'{model_name}' ) processor.push_to_hub(F'{model_name}' ) if __name__ == "__main__": lowerCAmelCase : Dict = argparse.ArgumentParser() # Required parameters parser.add_argument( """--model_name""", default="""focalnet-tiny""", type=str, help="""Name of the FocalNet model you'd like to convert.""", ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model directory.""" ) parser.add_argument( """--push_to_hub""", action="""store_true""", help="""Whether to push the model and processor to the hub.""", ) lowerCAmelCase : Union[str, Any] = parser.parse_args() convert_focalnet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
291
"""simple docstring""" def a__ ( snake_case__ , snake_case__ = False ) -> str: if not isinstance(snake_case__ , snake_case__ ): lowerCamelCase = F'Expected string as input, found {type(snake_case__ )}' raise ValueError(snake_case__ ) if not isinstance(snake_case__ , snake_case__ ): lowerCamelCase = F'Expected boolean as use_pascal parameter, found {type(snake_case__ )}' raise ValueError(snake_case__ ) lowerCamelCase = input_str.split("""_""" ) lowerCamelCase = 0 if use_pascal else 1 lowerCamelCase = words[start_index:] lowerCamelCase = [word[0].upper() + word[1:] for word in words_to_capitalize] lowerCamelCase = """""" if use_pascal else words[0] return "".join([initial_word, *capitalized_words] ) if __name__ == "__main__": from doctest import testmod testmod()
291
1
"""simple docstring""" import argparse import json import os import re import shutil import torch from transformers import BioGptConfig, BioGptForCausalLM from transformers.models.biogpt.tokenization_biogpt import VOCAB_FILES_NAMES from transformers.tokenization_utils_base import TOKENIZER_CONFIG_FILE from transformers.utils import WEIGHTS_NAME, logging logging.set_verbosity_warning() lowerCAmelCase : Optional[int] = 2 class __magic_name__ : '''simple docstring''' def __init__( self , *, # begin keyword-only arguments _a="<s>" , _a="<pad>" , _a="</s>" , _a="<unk>" , _a=None , ): """simple docstring""" lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase = bos, unk, pad, eos lowerCamelCase = [] lowerCamelCase = [] lowerCamelCase = {} lowerCamelCase = self.add_symbol(_a ) lowerCamelCase = self.add_symbol(_a ) lowerCamelCase = self.add_symbol(_a ) lowerCamelCase = self.add_symbol(_a ) if extra_special_symbols: for s in extra_special_symbols: self.add_symbol(_a ) lowerCamelCase = len(self.symbols ) def __eq__( self , _a ): """simple docstring""" return self.indices == other.indices def __getitem__( self , _a ): """simple docstring""" if idx < len(self.symbols ): return self.symbols[idx] return self.unk_word def __len__( self ): """simple docstring""" return len(self.symbols ) def __contains__( self , _a ): """simple docstring""" return sym in self.indices @classmethod def _lowerCAmelCase ( cls , _a ): """simple docstring""" lowerCamelCase = cls() d.add_from_file(_a ) return d def _lowerCAmelCase ( self , _a , _a=1 , _a=False ): """simple docstring""" if word in self.indices and not overwrite: lowerCamelCase = self.indices[word] lowerCamelCase = self.count[idx] + n return idx else: lowerCamelCase = len(self.symbols ) lowerCamelCase = idx self.symbols.append(_a ) self.count.append(_a ) return idx def _lowerCAmelCase ( self , _a ): """simple docstring""" return 0 def _lowerCAmelCase ( self , _a ): """simple docstring""" if isinstance(_a , _a ): try: with open(_a , """r""" , encoding="""utf-8""" ) as fd: self.add_from_file(_a ) except FileNotFoundError as fnfe: raise fnfe except UnicodeError: raise Exception("""Incorrect encoding detected in {}, please rebuild the dataset""".format(_a ) ) return lowerCamelCase = f.readlines() lowerCamelCase = self._load_meta(_a ) for line in lines[indices_start_line:]: try: lowerCamelCase , lowerCamelCase = line.rstrip().rsplit(""" """ , 1 ) if field == "#fairseq:overwrite": lowerCamelCase = True lowerCamelCase , lowerCamelCase = line.rsplit(""" """ , 1 ) else: lowerCamelCase = False lowerCamelCase = int(_a ) lowerCamelCase = line if word in self and not overwrite: raise RuntimeError( """Duplicate word found when loading Dictionary: '{}'. """ """Duplicate words can overwrite earlier ones by adding the """ """#fairseq:overwrite flag at the end of the corresponding row """ """in the dictionary file. If using the Camembert model, please """ """download an updated copy of the model file.""".format(_a ) ) self.add_symbol(_a , n=_a , overwrite=_a ) except ValueError: raise ValueError("""Incorrect dictionary format, expected '<token> <cnt> [flags]'""" ) def a__ ( snake_case__ ) -> Tuple: # (1) remove word breaking symbol, (2) add word ending symbol where the word is not broken up, # e.g.: d = {'le@@': 5, 'tt@@': 6, 'er': 7} => {'le': 5, 'tt': 6, 'er</w>': 7} lowerCamelCase = dict((re.sub(R"""@@$""" , """""" , snake_case__ ), v) if k.endswith("""@@""" ) else (re.sub(R"""$""" , """</w>""" , snake_case__ ), v) for k, v in d.items() ) lowerCamelCase = """<s> <pad> </s> <unk>""".split() # restore the special tokens for k in keep_keys: del da[F'{k}</w>'] lowerCamelCase = d[k] # restore return da def a__ ( snake_case__ , snake_case__ ) -> Dict: # prep if not os.path.exists(snake_case__ ): raise ValueError(F'path {biogpt_checkpoint_path} does not exist!' ) os.makedirs(snake_case__ , exist_ok=snake_case__ ) print(F'Writing results to {pytorch_dump_folder_path}' ) # handle various types of models lowerCamelCase = os.path.join(snake_case__ , """checkpoint.pt""" ) if not os.path.isfile(snake_case__ ): raise ValueError(F'path to the file {checkpoint_file} does not exist!' ) lowerCamelCase = torch.load(snake_case__ , map_location="""cpu""" ) lowerCamelCase = chkpt["""cfg"""]["""model"""] # dicts lowerCamelCase = os.path.join(snake_case__ , """dict.txt""" ) if not os.path.isfile(snake_case__ ): raise ValueError(F'path to the file {dict_file} does not exist!' ) lowerCamelCase = Dictionary.load(snake_case__ ) lowerCamelCase = rewrite_dict_keys(src_dict.indices ) lowerCamelCase = len(snake_case__ ) lowerCamelCase = os.path.join(snake_case__ , VOCAB_FILES_NAMES["""vocab_file"""] ) print(F'Generating {src_vocab_file} of {src_vocab_size} records' ) with open(snake_case__ , """w""" , encoding="""utf-8""" ) as f: f.write(json.dumps(snake_case__ , ensure_ascii=snake_case__ , indent=snake_case__ ) ) # merges_file (bpecodes) lowerCamelCase = os.path.join(snake_case__ , """bpecodes""" ) if not os.path.isfile(snake_case__ ): raise ValueError(F'path to the file {bpecodes_file} does not exist!' ) lowerCamelCase = os.path.join(snake_case__ , VOCAB_FILES_NAMES["""merges_file"""] ) shutil.copyfile(snake_case__ , snake_case__ ) # model config lowerCamelCase = os.path.join(snake_case__ , """config.json""" ) lowerCamelCase = { """activation_dropout""": args["""activation_dropout"""], """architectures""": ["""BioGptForCausalLM"""], """attention_probs_dropout_prob""": args["""attention_dropout"""], """bos_token_id""": 0, """eos_token_id""": 2, """hidden_act""": args["""activation_fn"""], """hidden_dropout_prob""": args["""dropout"""], """hidden_size""": args["""decoder_embed_dim"""], """initializer_range""": 0.02, """intermediate_size""": args["""decoder_ffn_embed_dim"""], """layer_norm_eps""": 1E-12, """layerdrop""": args["""decoder_layerdrop"""], """max_position_embeddings""": args["""max_target_positions"""], """model_type""": """biogpt""", """num_attention_heads""": args["""decoder_attention_heads"""], """num_hidden_layers""": args["""decoder_layers"""], """pad_token_id""": 1, """scale_embedding""": not args["""no_scale_embedding"""], """tie_word_embeddings""": args["""share_decoder_input_output_embed"""], """vocab_size""": src_vocab_size, } # good hparam defaults to start with print(F'Generating {biogpt_model_config_file}' ) with open(snake_case__ , """w""" , encoding="""utf-8""" ) as f: f.write(json.dumps(snake_case__ , ensure_ascii=snake_case__ , indent=snake_case__ ) ) # tokenizer config lowerCamelCase = os.path.join(snake_case__ , snake_case__ ) lowerCamelCase = { """bos_token""": """<s>""", """eos_token""": """</s>""", """model_max_length""": 10_24, """pad_token""": """<pad>""", """special_tokens_map_file""": None, """tokenizer_class""": """BioGptTokenizer""", """unk_token""": """<unk>""", } print(F'Generating {biogpt_tokenizer_config_file}' ) with open(snake_case__ , """w""" , encoding="""utf-8""" ) as f: f.write(json.dumps(snake_case__ , ensure_ascii=snake_case__ , indent=snake_case__ ) ) # model lowerCamelCase = chkpt["""model"""] # remove unneeded keys lowerCamelCase = [ """decoder.version""", ] for k in ignore_keys: model_state_dict.pop(snake_case__ , snake_case__ ) lowerCamelCase = list(model_state_dict.keys() ) for layer_name in layer_names: if layer_name.endswith("""output_projection.weight""" ): lowerCamelCase = model_state_dict.pop(snake_case__ ) else: lowerCamelCase = model_state_dict.pop(snake_case__ ) lowerCamelCase = BioGptConfig.from_pretrained(snake_case__ ) lowerCamelCase = BioGptForCausalLM(snake_case__ ) # check that it loads ok model_new.load_state_dict(snake_case__ ) # save lowerCamelCase = os.path.join(snake_case__ , snake_case__ ) print(F'Generating {pytorch_weights_dump_path}' ) torch.save(snake_case__ , snake_case__ ) print("""Conversion is done!""" ) if __name__ == "__main__": lowerCAmelCase : Optional[int] = argparse.ArgumentParser() # Required parameters parser.add_argument( """--biogpt_checkpoint_path""", default=None, type=str, required=True, help=( """Path to the official PyTorch checkpoint file which is expected to reside in the dump dir with dicts,""" """ bpecodes, etc.""" ), ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, required=True, help="""Path to the output PyTorch model.""" ) lowerCAmelCase : Tuple = parser.parse_args() convert_biogpt_checkpoint_to_pytorch(args.biogpt_checkpoint_path, args.pytorch_dump_folder_path)
291
"""simple docstring""" from typing import Dict, List, Optional, Tuple, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_torch_available, is_torch_tensor, logging if is_torch_available(): import torch lowerCAmelCase : int = logging.get_logger(__name__) class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' __UpperCamelCase = ["pixel_values"] def __init__( self , _a = True , _a = None , _a = PILImageResampling.BILINEAR , _a = True , _a = None , _a = True , _a = 1 / 255 , _a = True , _a = None , _a = None , **_a , ): """simple docstring""" super().__init__(**_a ) lowerCamelCase = size if size is not None else {"""shortest_edge""": 256} lowerCamelCase = get_size_dict(_a , default_to_square=_a ) lowerCamelCase = crop_size if crop_size is not None else {"""height""": 224, """width""": 224} lowerCamelCase = get_size_dict(_a , param_name="""crop_size""" ) lowerCamelCase = do_resize lowerCamelCase = size lowerCamelCase = resample lowerCamelCase = do_center_crop lowerCamelCase = crop_size lowerCamelCase = do_rescale lowerCamelCase = rescale_factor lowerCamelCase = do_normalize lowerCamelCase = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN lowerCamelCase = image_std if image_std is not None else IMAGENET_STANDARD_STD def _lowerCAmelCase ( self , _a , _a , _a = PILImageResampling.BICUBIC , _a = None , **_a , ): """simple docstring""" lowerCamelCase = get_size_dict(_a , default_to_square=_a ) if "shortest_edge" not in size: raise ValueError(f'The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}' ) lowerCamelCase = get_resize_output_image_size(_a , size=size["""shortest_edge"""] , default_to_square=_a ) return resize(_a , size=_a , resample=_a , data_format=_a , **_a ) def _lowerCAmelCase ( self , _a , _a , _a = None , **_a , ): """simple docstring""" lowerCamelCase = get_size_dict(_a ) if "height" not in size or "width" not in size: raise ValueError(f'The `size` parameter must contain the keys `height` and `width`. Got {size.keys()}' ) return center_crop(_a , size=(size["""height"""], size["""width"""]) , data_format=_a , **_a ) def _lowerCAmelCase ( self , _a , _a , _a = None , **_a ): """simple docstring""" return rescale(_a , scale=_a , data_format=_a , **_a ) def _lowerCAmelCase ( self , _a , _a , _a , _a = None , **_a , ): """simple docstring""" return normalize(_a , mean=_a , std=_a , data_format=_a , **_a ) def _lowerCAmelCase ( self , _a , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = ChannelDimension.FIRST , **_a , ): """simple docstring""" lowerCamelCase = do_resize if do_resize is not None else self.do_resize lowerCamelCase = size if size is not None else self.size lowerCamelCase = get_size_dict(_a , default_to_square=_a ) lowerCamelCase = resample if resample is not None else self.resample lowerCamelCase = do_center_crop if do_center_crop is not None else self.do_center_crop lowerCamelCase = crop_size if crop_size is not None else self.crop_size lowerCamelCase = get_size_dict(_a , param_name="""crop_size""" ) lowerCamelCase = do_rescale if do_rescale is not None else self.do_rescale lowerCamelCase = rescale_factor if rescale_factor is not None else self.rescale_factor lowerCamelCase = do_normalize if do_normalize is not None else self.do_normalize lowerCamelCase = image_mean if image_mean is not None else self.image_mean lowerCamelCase = image_std if image_std is not None else self.image_std lowerCamelCase = make_list_of_images(_a ) if not valid_images(_a ): raise ValueError( """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """ """torch.Tensor, tf.Tensor or jax.ndarray.""" ) if do_resize and size is None: raise ValueError("""Size must be specified if do_resize is True.""" ) if do_center_crop and crop_size is None: raise ValueError("""Crop size must be specified if do_center_crop is True.""" ) if do_rescale and rescale_factor is None: raise ValueError("""Rescale factor must be specified if do_rescale is True.""" ) if do_normalize and (image_mean is None or image_std is None): raise ValueError("""Image mean and std must be specified if do_normalize is True.""" ) # All transformations expect numpy arrays. lowerCamelCase = [to_numpy_array(_a ) for image in images] if do_resize: lowerCamelCase = [self.resize(image=_a , size=_a , resample=_a ) for image in images] if do_center_crop: lowerCamelCase = [self.center_crop(image=_a , size=_a ) for image in images] if do_rescale: lowerCamelCase = [self.rescale(image=_a , scale=_a ) for image in images] if do_normalize: lowerCamelCase = [self.normalize(image=_a , mean=_a , std=_a ) for image in images] lowerCamelCase = [to_channel_dimension_format(_a , _a ) for image in images] lowerCamelCase = {"""pixel_values""": images} return BatchFeature(data=_a , tensor_type=_a ) def _lowerCAmelCase ( self , _a , _a = None ): """simple docstring""" lowerCamelCase = outputs.logits # Resize logits and compute semantic segmentation maps if target_sizes is not None: if len(_a ) != len(_a ): raise ValueError( """Make sure that you pass in as many target sizes as the batch dimension of the logits""" ) if is_torch_tensor(_a ): lowerCamelCase = target_sizes.numpy() lowerCamelCase = [] for idx in range(len(_a ) ): lowerCamelCase = torch.nn.functional.interpolate( logits[idx].unsqueeze(dim=0 ) , size=target_sizes[idx] , mode="""bilinear""" , align_corners=_a ) lowerCamelCase = resized_logits[0].argmax(dim=0 ) semantic_segmentation.append(_a ) else: lowerCamelCase = logits.argmax(dim=1 ) lowerCamelCase = [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0] )] return semantic_segmentation
291
1
"""simple docstring""" import math from typing import Any, Callable, List, Optional, Tuple, Union import numpy as np import torch from ...models import TaFilmDecoder from ...schedulers import DDPMScheduler from ...utils import is_onnx_available, logging, randn_tensor if is_onnx_available(): from ..onnx_utils import OnnxRuntimeModel from ..pipeline_utils import AudioPipelineOutput, DiffusionPipeline from .continous_encoder import SpectrogramContEncoder from .notes_encoder import SpectrogramNotesEncoder lowerCAmelCase : str = logging.get_logger(__name__) # pylint: disable=invalid-name lowerCAmelCase : Tuple = 256 class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' __UpperCamelCase = ["melgan"] def __init__( self , _a , _a , _a , _a , _a , ): """simple docstring""" super().__init__() # From MELGAN lowerCamelCase = math.log(1e-5 ) # Matches MelGAN training. lowerCamelCase = 4.0 # Largest value for most examples lowerCamelCase = 128 self.register_modules( notes_encoder=_a , continuous_encoder=_a , decoder=_a , scheduler=_a , melgan=_a , ) def _lowerCAmelCase ( self , _a , _a=(-1.0, 1.0) , _a=False ): """simple docstring""" lowerCamelCase , lowerCamelCase = output_range if clip: lowerCamelCase = torch.clip(_a , self.min_value , self.max_value ) # Scale to [0, 1]. lowerCamelCase = (features - self.min_value) / (self.max_value - self.min_value) # Scale to [min_out, max_out]. return zero_one * (max_out - min_out) + min_out def _lowerCAmelCase ( self , _a , _a=(-1.0, 1.0) , _a=False ): """simple docstring""" lowerCamelCase , lowerCamelCase = input_range lowerCamelCase = torch.clip(_a , _a , _a ) if clip else outputs # Scale to [0, 1]. lowerCamelCase = (outputs - min_out) / (max_out - min_out) # Scale to [self.min_value, self.max_value]. return zero_one * (self.max_value - self.min_value) + self.min_value def _lowerCAmelCase ( self , _a , _a , _a ): """simple docstring""" lowerCamelCase = input_tokens > 0 lowerCamelCase , lowerCamelCase = self.notes_encoder( encoder_input_tokens=_a , encoder_inputs_mask=_a ) lowerCamelCase , lowerCamelCase = self.continuous_encoder( encoder_inputs=_a , encoder_inputs_mask=_a ) return [(tokens_encoded, tokens_mask), (continuous_encoded, continuous_mask)] def _lowerCAmelCase ( self , _a , _a , _a ): """simple docstring""" lowerCamelCase = noise_time if not torch.is_tensor(_a ): lowerCamelCase = torch.tensor([timesteps] , dtype=torch.long , device=input_tokens.device ) elif torch.is_tensor(_a ) and len(timesteps.shape ) == 0: lowerCamelCase = timesteps[None].to(input_tokens.device ) # broadcast to batch dimension in a way that's compatible with ONNX/Core ML lowerCamelCase = timesteps * torch.ones(input_tokens.shape[0] , dtype=timesteps.dtype , device=timesteps.device ) lowerCamelCase = self.decoder( encodings_and_masks=_a , decoder_input_tokens=_a , decoder_noise_time=_a ) return logits @torch.no_grad() def __call__( self , _a , _a = None , _a = 100 , _a = True , _a = "numpy" , _a = None , _a = 1 , ): """simple docstring""" if (callback_steps is None) or ( callback_steps is not None and (not isinstance(_a , _a ) or callback_steps <= 0) ): raise ValueError( f'`callback_steps` has to be a positive integer but is {callback_steps} of type' f' {type(_a )}.' ) lowerCamelCase = np.zeros([1, TARGET_FEATURE_LENGTH, self.n_dims] , dtype=np.floataa ) lowerCamelCase = np.zeros([1, 0, self.n_dims] , np.floataa ) lowerCamelCase = torch.ones((1, TARGET_FEATURE_LENGTH) , dtype=_a , device=self.device ) for i, encoder_input_tokens in enumerate(_a ): if i == 0: lowerCamelCase = torch.from_numpy(pred_mel[:1].copy() ).to( device=self.device , dtype=self.decoder.dtype ) # The first chunk has no previous context. lowerCamelCase = torch.zeros((1, TARGET_FEATURE_LENGTH) , dtype=_a , device=self.device ) else: # The full song pipeline does not feed in a context feature, so the mask # will be all 0s after the feature converter. Because we know we're # feeding in a full context chunk from the previous prediction, set it # to all 1s. lowerCamelCase = ones lowerCamelCase = self.scale_features( _a , output_range=[-1.0, 1.0] , clip=_a ) lowerCamelCase = self.encode( input_tokens=torch.IntTensor([encoder_input_tokens] ).to(device=self.device ) , continuous_inputs=_a , continuous_mask=_a , ) # Sample encoder_continuous_inputs shaped gaussian noise to begin loop lowerCamelCase = randn_tensor( shape=encoder_continuous_inputs.shape , generator=_a , device=self.device , dtype=self.decoder.dtype , ) # set step values self.scheduler.set_timesteps(_a ) # Denoising diffusion loop for j, t in enumerate(self.progress_bar(self.scheduler.timesteps ) ): lowerCamelCase = self.decode( encodings_and_masks=_a , input_tokens=_a , noise_time=t / self.scheduler.config.num_train_timesteps , ) # Compute previous output: x_t -> x_t-1 lowerCamelCase = self.scheduler.step(_a , _a , _a , generator=_a ).prev_sample lowerCamelCase = self.scale_to_features(_a , input_range=[-1.0, 1.0] ) lowerCamelCase = mel[:1] lowerCamelCase = mel.cpu().float().numpy() lowerCamelCase = np.concatenate([full_pred_mel, pred_mel[:1]] , axis=1 ) # call the callback, if provided if callback is not None and i % callback_steps == 0: callback(_a , _a ) logger.info("""Generated segment""" , _a ) if output_type == "numpy" and not is_onnx_available(): raise ValueError( """Cannot return output in 'np' format if ONNX is not available. Make sure to have ONNX installed or set 'output_type' to 'mel'.""" ) elif output_type == "numpy" and self.melgan is None: raise ValueError( """Cannot return output in 'np' format if melgan component is not defined. Make sure to define `self.melgan` or set 'output_type' to 'mel'.""" ) if output_type == "numpy": lowerCamelCase = self.melgan(input_features=full_pred_mel.astype(np.floataa ) ) else: lowerCamelCase = full_pred_mel if not return_dict: return (output,) return AudioPipelineOutput(audios=_a )
291
"""simple docstring""" import operator as op lowerCAmelCase : Dict = """scaler.pt""" lowerCAmelCase : Tuple = """pytorch_model""" lowerCAmelCase : Union[str, Any] = """random_states""" lowerCAmelCase : Union[str, Any] = """optimizer""" lowerCAmelCase : Dict = """scheduler""" lowerCAmelCase : int = """pytorch_model.bin""" lowerCAmelCase : str = """pytorch_model.bin.index.json""" lowerCAmelCase : Union[str, Any] = """model.safetensors""" lowerCAmelCase : List[Any] = """model.safetensors.index.json""" lowerCAmelCase : List[Any] = """1.10.2""" lowerCAmelCase : Any = """py38""" lowerCAmelCase : Optional[int] = """4.17.0""" lowerCAmelCase : str = ["""ml.p3.16xlarge""", """ml.p3dn.24xlarge""", """ml.p4dn.24xlarge"""] lowerCAmelCase : Tuple = ["""FULL_SHARD""", """SHARD_GRAD_OP""", """NO_SHARD""", """HYBRID_SHARD""", """HYBRID_SHARD_ZERO2"""] lowerCAmelCase : List[Any] = ["""TRANSFORMER_BASED_WRAP""", """SIZE_BASED_WRAP""", """NO_WRAP"""] lowerCAmelCase : List[str] = ["""BACKWARD_PRE""", """BACKWARD_POST""", """NO_PREFETCH"""] lowerCAmelCase : List[str] = ["""FULL_STATE_DICT""", """LOCAL_STATE_DICT""", """SHARDED_STATE_DICT"""] lowerCAmelCase : Any = """2.0.1""" lowerCAmelCase : List[Any] = ["""pdsh""", """standard""", """openmpi""", """mvapich"""] lowerCAmelCase : Union[str, Any] = ["""default""", """reduce-overhead""", """max-autotune"""] lowerCAmelCase : Optional[int] = {""">""": op.gt, """>=""": op.ge, """==""": op.eq, """!=""": op.ne, """<=""": op.le, """<""": op.lt} # These are the args for `torch.distributed.launch` for pytorch < 1.9 lowerCAmelCase : Union[str, Any] = [ """nnodes""", """nproc_per_node""", """rdzv_backend""", """rdzv_endpoint""", """rdzv_id""", """rdzv_conf""", """standalone""", """max_restarts""", """monitor_interval""", """start_method""", """role""", """module""", """m""", """no_python""", """run_path""", """log_dir""", """r""", """redirects""", """t""", """tee""", """node_rank""", """master_addr""", """master_port""", ] lowerCAmelCase : List[str] = ["""DEEPSPEED""", """MULTI_GPU""", """FSDP""", """MEGATRON_LM"""] lowerCAmelCase : Optional[Any] = ["""DEEPSPEED""", """MULTI_XPU""", """FSDP"""]
291
1
"""simple docstring""" import numpy as np def a__ ( snake_case__ ) -> np.ndarray: return 1 / (1 + np.exp(-vector )) def a__ ( snake_case__ ) -> np.ndarray: return vector * sigmoid(snake_case__ ) if __name__ == "__main__": import doctest doctest.testmod()
291
"""simple docstring""" import inspect import unittest from transformers import ViTMSNConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTMSNForImageClassification, ViTMSNModel from transformers.models.vit_msn.modeling_vit_msn import VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class __magic_name__ : '''simple docstring''' def __init__( self , _a , _a=13 , _a=30 , _a=2 , _a=3 , _a=True , _a=True , _a=32 , _a=5 , _a=4 , _a=37 , _a="gelu" , _a=0.1 , _a=0.1 , _a=10 , _a=0.02 , _a=None , ): """simple docstring""" lowerCamelCase = parent lowerCamelCase = batch_size lowerCamelCase = image_size lowerCamelCase = patch_size lowerCamelCase = num_channels lowerCamelCase = is_training lowerCamelCase = use_labels lowerCamelCase = hidden_size lowerCamelCase = num_hidden_layers lowerCamelCase = num_attention_heads lowerCamelCase = intermediate_size lowerCamelCase = hidden_act lowerCamelCase = hidden_dropout_prob lowerCamelCase = attention_probs_dropout_prob lowerCamelCase = type_sequence_label_size lowerCamelCase = initializer_range lowerCamelCase = scope # in ViT MSN, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) lowerCamelCase = (image_size // patch_size) ** 2 lowerCamelCase = num_patches + 1 def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) lowerCamelCase = None if self.use_labels: lowerCamelCase = ids_tensor([self.batch_size] , self.type_sequence_label_size ) lowerCamelCase = self.get_config() return config, pixel_values, labels def _lowerCAmelCase ( self ): """simple docstring""" return ViTMSNConfig( 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 , initializer_range=self.initializer_range , ) def _lowerCAmelCase ( self , _a , _a , _a ): """simple docstring""" lowerCamelCase = ViTMSNModel(config=_a ) model.to(_a ) model.eval() lowerCamelCase = model(_a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _lowerCAmelCase ( self , _a , _a , _a ): """simple docstring""" lowerCamelCase = self.type_sequence_label_size lowerCamelCase = ViTMSNForImageClassification(_a ) model.to(_a ) model.eval() lowerCamelCase = model(_a , labels=_a ) print("""Pixel and labels shape: {pixel_values.shape}, {labels.shape}""" ) print("""Labels: {labels}""" ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images lowerCamelCase = 1 lowerCamelCase = ViTMSNForImageClassification(_a ) model.to(_a ) model.eval() lowerCamelCase = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) lowerCamelCase = model(_a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.prepare_config_and_inputs() lowerCamelCase , lowerCamelCase , lowerCamelCase = config_and_inputs lowerCamelCase = {"""pixel_values""": pixel_values} return config, inputs_dict @require_torch class __magic_name__ ( UpperCAmelCase__ , UpperCAmelCase__ , unittest.TestCase ): '''simple docstring''' __UpperCamelCase = (ViTMSNModel, ViTMSNForImageClassification) if is_torch_available() else () __UpperCamelCase = ( {"feature-extraction": ViTMSNModel, "image-classification": ViTMSNForImageClassification} if is_torch_available() else {} ) __UpperCamelCase = False __UpperCamelCase = False __UpperCamelCase = False __UpperCamelCase = False def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = ViTMSNModelTester(self ) lowerCamelCase = ConfigTester(self , config_class=_a , has_text_modality=_a , hidden_size=37 ) def _lowerCAmelCase ( self ): """simple docstring""" self.config_tester.run_common_tests() @unittest.skip(reason="""ViTMSN does not use inputs_embeds""" ) def _lowerCAmelCase ( self ): """simple docstring""" pass def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase , lowerCamelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCamelCase = model_class(_a ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) lowerCamelCase = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_a , nn.Linear ) ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase , lowerCamelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCamelCase = model_class(_a ) lowerCamelCase = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic lowerCamelCase = [*signature.parameters.keys()] lowerCamelCase = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , _a ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_a ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_a ) @slow def _lowerCAmelCase ( self ): """simple docstring""" for model_name in VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowerCamelCase = ViTMSNModel.from_pretrained(_a ) self.assertIsNotNone(_a ) def a__ ( ) -> Any: lowerCamelCase = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) return image @require_torch @require_vision class __magic_name__ ( unittest.TestCase ): '''simple docstring''' @cached_property def _lowerCAmelCase ( self ): """simple docstring""" return ViTImageProcessor.from_pretrained("""facebook/vit-msn-small""" ) if is_vision_available() else None @slow def _lowerCAmelCase ( self ): """simple docstring""" torch.manual_seed(2 ) lowerCamelCase = ViTMSNForImageClassification.from_pretrained("""facebook/vit-msn-small""" ).to(_a ) lowerCamelCase = self.default_image_processor lowerCamelCase = prepare_img() lowerCamelCase = image_processor(images=_a , return_tensors="""pt""" ).to(_a ) # forward pass with torch.no_grad(): lowerCamelCase = model(**_a ) # verify the logits lowerCamelCase = torch.Size((1, 1_000) ) self.assertEqual(outputs.logits.shape , _a ) lowerCamelCase = torch.tensor([-0.0_803, -0.4_454, -0.2_375] ).to(_a ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , _a , atol=1e-4 ) )
291
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available lowerCAmelCase : Optional[int] = { """configuration_ctrl""": ["""CTRL_PRETRAINED_CONFIG_ARCHIVE_MAP""", """CTRLConfig"""], """tokenization_ctrl""": ["""CTRLTokenizer"""], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : Dict = [ """CTRL_PRETRAINED_MODEL_ARCHIVE_LIST""", """CTRLForSequenceClassification""", """CTRLLMHeadModel""", """CTRLModel""", """CTRLPreTrainedModel""", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : Union[str, Any] = [ """TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST""", """TFCTRLForSequenceClassification""", """TFCTRLLMHeadModel""", """TFCTRLModel""", """TFCTRLPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_ctrl import CTRL_PRETRAINED_CONFIG_ARCHIVE_MAP, CTRLConfig from .tokenization_ctrl import CTRLTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_ctrl import ( CTRL_PRETRAINED_MODEL_ARCHIVE_LIST, CTRLForSequenceClassification, CTRLLMHeadModel, CTRLModel, CTRLPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_ctrl import ( TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST, TFCTRLForSequenceClassification, TFCTRLLMHeadModel, TFCTRLModel, TFCTRLPreTrainedModel, ) else: import sys lowerCAmelCase : List[str] = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
291
"""simple docstring""" 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__ ( snake_case__ , snake_case__ , snake_case__ ) -> List[Any]: return params[F'{prefix}/{prefix}/relpos_bias/rel_embedding'][:, i, :] def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__="attention" ) -> List[Any]: lowerCamelCase = lowerCamelCase = np.ascontiguousarray(params[F'{prefix}/{prefix}/{layer_name}/key/kernel'][:, i, :, :] ) lowerCamelCase = k_tmp.reshape(k_tmp.shape[0] , k_tmp.shape[1] * k_tmp.shape[2] ) lowerCamelCase = np.ascontiguousarray(params[F'{prefix}/{prefix}/{layer_name}/out/kernel'][:, i, :, :] ) lowerCamelCase = o_tmp.reshape(o_tmp.shape[0] * o_tmp.shape[1] , o_tmp.shape[2] ) lowerCamelCase = np.ascontiguousarray(params[F'{prefix}/{prefix}/{layer_name}/query/kernel'][:, i, :, :] ) lowerCamelCase = q_tmp.reshape(q_tmp.shape[0] , q_tmp.shape[1] * q_tmp.shape[2] ) lowerCamelCase = np.ascontiguousarray(params[F'{prefix}/{prefix}/{layer_name}/value/kernel'][:, i, :, :] ) lowerCamelCase = v_tmp.reshape(v_tmp.shape[0] , v_tmp.shape[1] * v_tmp.shape[2] ) return k, o, q, v def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__=False ) -> List[str]: if split_mlp_wi: lowerCamelCase = params[F'{prefix}/{prefix}/mlp/wi_0/kernel'][:, i, :] lowerCamelCase = params[F'{prefix}/{prefix}/mlp/wi_1/kernel'][:, i, :] lowerCamelCase = (wi_a, wi_a) else: lowerCamelCase = params[F'{prefix}/{prefix}/mlp/wi/kernel'][:, i, :] lowerCamelCase = params[F'{prefix}/{prefix}/mlp/wo/kernel'][:, i, :] return wi, wo def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ ) -> Tuple: return params[F'{prefix}/{prefix}/{layer_name}/scale'][:, i] def a__ ( snake_case__ , *, snake_case__ , snake_case__ , snake_case__ = False ) -> Dict: lowerCamelCase = traverse_util.flatten_dict(variables["""target"""] ) lowerCamelCase = {"""/""".join(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 lowerCamelCase = """encoder/encoder/mlp/wi_0/kernel""" in old print("""Split MLP:""" , snake_case__ ) lowerCamelCase = collections.OrderedDict() # Shared embeddings. lowerCamelCase = old["""token_embedder/embedding"""] # Encoder. for i in range(snake_case__ ): # Block i, layer 0 (Self Attention). lowerCamelCase = tax_layer_norm_lookup(snake_case__ , snake_case__ , """encoder""" , """pre_attention_layer_norm""" ) lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase = tax_attention_lookup(snake_case__ , snake_case__ , """encoder""" , """attention""" ) lowerCamelCase = layer_norm lowerCamelCase = k.T lowerCamelCase = o.T lowerCamelCase = q.T lowerCamelCase = v.T # Block i, layer 1 (MLP). lowerCamelCase = tax_layer_norm_lookup(snake_case__ , snake_case__ , """encoder""" , """pre_mlp_layer_norm""" ) lowerCamelCase , lowerCamelCase = tax_mlp_lookup(snake_case__ , snake_case__ , """encoder""" , snake_case__ ) lowerCamelCase = layer_norm if split_mlp_wi: lowerCamelCase = wi[0].T lowerCamelCase = wi[1].T else: lowerCamelCase = wi.T lowerCamelCase = wo.T if scalable_attention: # convert the rel_embedding of each layer lowerCamelCase = tax_relpos_bias_lookup( snake_case__ , snake_case__ , """encoder""" ).T lowerCamelCase = old["""encoder/encoder_norm/scale"""] if not scalable_attention: lowerCamelCase = tax_relpos_bias_lookup( snake_case__ , 0 , """encoder""" ).T lowerCamelCase = tax_relpos_bias_lookup( snake_case__ , 0 , """decoder""" ).T if not is_encoder_only: # Decoder. for i in range(snake_case__ ): # Block i, layer 0 (Self Attention). lowerCamelCase = tax_layer_norm_lookup(snake_case__ , snake_case__ , """decoder""" , """pre_self_attention_layer_norm""" ) lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase = tax_attention_lookup(snake_case__ , snake_case__ , """decoder""" , """self_attention""" ) lowerCamelCase = layer_norm lowerCamelCase = k.T lowerCamelCase = o.T lowerCamelCase = q.T lowerCamelCase = v.T # Block i, layer 1 (Cross Attention). lowerCamelCase = tax_layer_norm_lookup(snake_case__ , snake_case__ , """decoder""" , """pre_cross_attention_layer_norm""" ) lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase = tax_attention_lookup(snake_case__ , snake_case__ , """decoder""" , """encoder_decoder_attention""" ) lowerCamelCase = layer_norm lowerCamelCase = k.T lowerCamelCase = o.T lowerCamelCase = q.T lowerCamelCase = v.T # Block i, layer 2 (MLP). lowerCamelCase = tax_layer_norm_lookup(snake_case__ , snake_case__ , """decoder""" , """pre_mlp_layer_norm""" ) lowerCamelCase , lowerCamelCase = tax_mlp_lookup(snake_case__ , snake_case__ , """decoder""" , snake_case__ ) lowerCamelCase = layer_norm if split_mlp_wi: lowerCamelCase = wi[0].T lowerCamelCase = wi[1].T else: lowerCamelCase = wi.T lowerCamelCase = wo.T if scalable_attention: # convert the rel_embedding of each layer lowerCamelCase = tax_relpos_bias_lookup(snake_case__ , snake_case__ , """decoder""" ).T lowerCamelCase = 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: lowerCamelCase = old["""decoder/logits_dense/kernel"""].T return new def a__ ( snake_case__ , snake_case__ ) -> Optional[int]: lowerCamelCase = 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: lowerCamelCase = state_dict["""shared.weight"""] if not is_encoder_only: if "decoder.embed_tokens.weight" not in state_dict: lowerCamelCase = 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.""" ) lowerCamelCase = state_dict["""shared.weight"""] return state_dict def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ ) -> List[Any]: lowerCamelCase = checkpoints.load_tax_checkpoint(snake_case__ ) lowerCamelCase = convert_tax_to_pytorch( snake_case__ , num_layers=config.num_layers , is_encoder_only=snake_case__ , scalable_attention=snake_case__ ) lowerCamelCase = make_state_dict(snake_case__ , snake_case__ ) model.load_state_dict(snake_case__ , strict=snake_case__ ) def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ = False , snake_case__ = False , ) -> str: lowerCamelCase = MTaConfig.from_json_file(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: lowerCamelCase = UMTaEncoderModel(snake_case__ ) else: lowerCamelCase = UMTaForConditionalGeneration(snake_case__ ) # Load weights from tf checkpoint load_tax_weights_in_ta(snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ ) # Save pytorch-model print(F'Save PyTorch model to {pytorch_dump_path}' ) model.save_pretrained(snake_case__ ) # Verify that we can load the checkpoint. model.from_pretrained(snake_case__ ) print("""Done""" ) if __name__ == "__main__": lowerCAmelCase : 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, ) lowerCAmelCase : 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, )
291
1
"""simple docstring""" def a__ ( snake_case__ , snake_case__ , snake_case__ ) -> int: if len(snake_case__ ) != len(snake_case__ ): raise ValueError("""The length of profit and weight must be same.""" ) if max_weight <= 0: raise ValueError("""max_weight must greater than zero.""" ) if any(p < 0 for p in profit ): raise ValueError("""Profit can not be negative.""" ) if any(w < 0 for w in weight ): raise ValueError("""Weight can not be negative.""" ) # List created to store profit gained for the 1kg in case of each weight # respectively. Calculate and append profit/weight for each element. lowerCamelCase = [p / w for p, w in zip(snake_case__ , snake_case__ )] # Creating a copy of the list and sorting profit/weight in ascending order lowerCamelCase = sorted(snake_case__ ) # declaring useful variables lowerCamelCase = len(snake_case__ ) lowerCamelCase = 0 lowerCamelCase = 0 lowerCamelCase = 0 # loop till the total weight do not reach max limit e.g. 15 kg and till i<length while limit <= max_weight and i < length: # flag value for encountered greatest element in sorted_profit_by_weight lowerCamelCase = sorted_profit_by_weight[length - i - 1] lowerCamelCase = profit_by_weight.index(snake_case__ ) lowerCamelCase = -1 # check if the weight encountered is less than the total weight # encountered before. if max_weight - limit >= weight[index]: limit += weight[index] # Adding profit gained for the given weight 1 === # weight[index]/weight[index] gain += 1 * profit[index] else: # Since the weight encountered is greater than limit, therefore take the # required number of remaining kgs and calculate profit for it. # weight remaining / weight[index] gain += (max_weight - limit) / weight[index] * profit[index] break i += 1 return gain if __name__ == "__main__": print( """Input profits, weights, and then max_weight (all positive ints) separated by """ """spaces.""" ) lowerCAmelCase : List[Any] = [int(x) for x in input("""Input profits separated by spaces: """).split()] lowerCAmelCase : int = [int(x) for x in input("""Input weights separated by spaces: """).split()] lowerCAmelCase : Any = int(input("""Max weight allowed: """)) # Function Call calc_profit(profit, weight, max_weight)
291
"""simple docstring""" from __future__ import annotations def a__ ( snake_case__ , snake_case__ ) -> bool: if len(snake_case__ ) == 0: return False lowerCamelCase = len(snake_case__ ) // 2 if a_list[midpoint] == item: return True if item < a_list[midpoint]: return binary_search(a_list[:midpoint] , snake_case__ ) else: return binary_search(a_list[midpoint + 1 :] , snake_case__ ) if __name__ == "__main__": lowerCAmelCase : List[Any] = input("""Enter numbers separated by comma:\n""").strip() lowerCAmelCase : Optional[Any] = [int(item.strip()) for item in user_input.split(""",""")] lowerCAmelCase : Optional[int] = int(input("""Enter the number to be found in the list:\n""").strip()) lowerCAmelCase : Union[str, Any] = """""" if binary_search(sequence, target) else """not """ print(F"""{target} was {not_str}found in {sequence}""")
291
1
"""simple docstring""" from sklearn.metrics import fa_score, matthews_corrcoef import datasets from .record_evaluation import evaluate as evaluate_record lowerCAmelCase : Optional[Any] = """\ @article{wang2019superglue, title={SuperGLUE: A Stickier Benchmark for General-Purpose Language Understanding Systems}, author={Wang, Alex and Pruksachatkun, Yada and Nangia, Nikita and Singh, Amanpreet and Michael, Julian and Hill, Felix and Levy, Omer and Bowman, Samuel R}, journal={arXiv preprint arXiv:1905.00537}, year={2019} } """ lowerCAmelCase : Optional[int] = """\ SuperGLUE (https://super.gluebenchmark.com/) is a new benchmark styled after GLUE with a new set of more difficult language understanding tasks, improved resources, and a new public leaderboard. """ lowerCAmelCase : List[Any] = """ Compute SuperGLUE evaluation metric associated to each SuperGLUE dataset. Args: predictions: list of predictions to score. Depending on the SuperGlUE subset: - for 'record': list of question-answer dictionaries with the following keys: - 'idx': index of the question as specified by the dataset - 'prediction_text': the predicted answer text - for 'multirc': list of question-answer dictionaries with the following keys: - 'idx': index of the question-answer pair as specified by the dataset - 'prediction': the predicted answer label - otherwise: list of predicted labels references: list of reference labels. Depending on the SuperGLUE subset: - for 'record': list of question-answers dictionaries with the following keys: - 'idx': index of the question as specified by the dataset - 'answers': list of possible answers - otherwise: list of reference labels Returns: depending on the SuperGLUE subset: - for 'record': - 'exact_match': Exact match between answer and gold answer - 'f1': F1 score - for 'multirc': - 'exact_match': Exact match between answer and gold answer - 'f1_m': Per-question macro-F1 score - 'f1_a': Average F1 score over all answers - for 'axb': 'matthews_correlation': Matthew Correlation - for 'cb': - 'accuracy': Accuracy - 'f1': F1 score - for all others: - 'accuracy': Accuracy Examples: >>> super_glue_metric = datasets.load_metric('super_glue', 'copa') # any of [\"copa\", \"rte\", \"wic\", \"wsc\", \"wsc.fixed\", \"boolq\", \"axg\"] >>> predictions = [0, 1] >>> references = [0, 1] >>> results = super_glue_metric.compute(predictions=predictions, references=references) >>> print(results) {'accuracy': 1.0} >>> super_glue_metric = datasets.load_metric('super_glue', 'cb') >>> predictions = [0, 1] >>> references = [0, 1] >>> results = super_glue_metric.compute(predictions=predictions, references=references) >>> print(results) {'accuracy': 1.0, 'f1': 1.0} >>> super_glue_metric = datasets.load_metric('super_glue', 'record') >>> predictions = [{'idx': {'passage': 0, 'query': 0}, 'prediction_text': 'answer'}] >>> references = [{'idx': {'passage': 0, 'query': 0}, 'answers': ['answer', 'another_answer']}] >>> results = super_glue_metric.compute(predictions=predictions, references=references) >>> print(results) {'exact_match': 1.0, 'f1': 1.0} >>> super_glue_metric = datasets.load_metric('super_glue', 'multirc') >>> predictions = [{'idx': {'answer': 0, 'paragraph': 0, 'question': 0}, 'prediction': 0}, {'idx': {'answer': 1, 'paragraph': 2, 'question': 3}, 'prediction': 1}] >>> references = [0, 1] >>> results = super_glue_metric.compute(predictions=predictions, references=references) >>> print(results) {'exact_match': 1.0, 'f1_m': 1.0, 'f1_a': 1.0} >>> super_glue_metric = datasets.load_metric('super_glue', 'axb') >>> references = [0, 1] >>> predictions = [0, 1] >>> results = super_glue_metric.compute(predictions=predictions, references=references) >>> print(results) {'matthews_correlation': 1.0} """ def a__ ( snake_case__ , snake_case__ ) -> str: return float((preds == labels).mean() ) def a__ ( snake_case__ , snake_case__ , snake_case__="binary" ) -> Optional[Any]: lowerCamelCase = simple_accuracy(snake_case__ , snake_case__ ) lowerCamelCase = float(fa_score(y_true=snake_case__ , y_pred=snake_case__ , average=snake_case__ ) ) return { "accuracy": acc, "f1": fa, } def a__ ( snake_case__ , snake_case__ ) -> int: lowerCamelCase = {} for id_pred, label in zip(snake_case__ , snake_case__ ): lowerCamelCase = F'{id_pred["idx"]["paragraph"]}-{id_pred["idx"]["question"]}' lowerCamelCase = id_pred["""prediction"""] if question_id in question_map: question_map[question_id].append((pred, label) ) else: lowerCamelCase = [(pred, label)] lowerCamelCase , lowerCamelCase = [], [] for question, preds_labels in question_map.items(): lowerCamelCase , lowerCamelCase = zip(*snake_case__ ) lowerCamelCase = fa_score(y_true=snake_case__ , y_pred=snake_case__ , average="""macro""" ) fas.append(snake_case__ ) lowerCamelCase = int(sum(pred == label for pred, label in preds_labels ) == len(snake_case__ ) ) ems.append(snake_case__ ) lowerCamelCase = float(sum(snake_case__ ) / len(snake_case__ ) ) lowerCamelCase = sum(snake_case__ ) / len(snake_case__ ) lowerCamelCase = float(fa_score(y_true=snake_case__ , y_pred=[id_pred["""prediction"""] for id_pred in ids_preds] ) ) return {"exact_match": em, "f1_m": fa_m, "f1_a": fa_a} @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __magic_name__ ( datasets.Metric ): '''simple docstring''' def _lowerCAmelCase ( self ): """simple docstring""" if self.config_name not in [ "boolq", "cb", "copa", "multirc", "record", "rte", "wic", "wsc", "wsc.fixed", "axb", "axg", ]: raise KeyError( """You should supply a configuration name selected in """ """[\"boolq\", \"cb\", \"copa\", \"multirc\", \"record\", \"rte\", \"wic\", \"wsc\", \"wsc.fixed\", \"axb\", \"axg\",]""" ) return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features(self._get_feature_types() ) , codebase_urls=[] , reference_urls=[] , format="""numpy""" if not self.config_name == """record""" and not self.config_name == """multirc""" else None , ) def _lowerCAmelCase ( self ): """simple docstring""" if self.config_name == "record": return { "predictions": { "idx": { "passage": datasets.Value("""int64""" ), "query": datasets.Value("""int64""" ), }, "prediction_text": datasets.Value("""string""" ), }, "references": { "idx": { "passage": datasets.Value("""int64""" ), "query": datasets.Value("""int64""" ), }, "answers": datasets.Sequence(datasets.Value("""string""" ) ), }, } elif self.config_name == "multirc": return { "predictions": { "idx": { "answer": datasets.Value("""int64""" ), "paragraph": datasets.Value("""int64""" ), "question": datasets.Value("""int64""" ), }, "prediction": datasets.Value("""int64""" ), }, "references": datasets.Value("""int64""" ), } else: return { "predictions": datasets.Value("""int64""" ), "references": datasets.Value("""int64""" ), } def _lowerCAmelCase ( self , _a , _a ): """simple docstring""" if self.config_name == "axb": return {"matthews_correlation": matthews_corrcoef(_a , _a )} elif self.config_name == "cb": return acc_and_fa(_a , _a , fa_avg="""macro""" ) elif self.config_name == "record": lowerCamelCase = [ { """qas""": [ {"""id""": ref["""idx"""]["""query"""], """answers""": [{"""text""": ans} for ans in ref["""answers"""]]} for ref in references ] } ] lowerCamelCase = {pred["""idx"""]["""query"""]: pred["""prediction_text"""] for pred in predictions} return evaluate_record(_a , _a )[0] elif self.config_name == "multirc": return evaluate_multirc(_a , _a ) elif self.config_name in ["copa", "rte", "wic", "wsc", "wsc.fixed", "boolq", "axg"]: return {"accuracy": simple_accuracy(_a , _a )} else: raise KeyError( """You should supply a configuration name selected in """ """[\"boolq\", \"cb\", \"copa\", \"multirc\", \"record\", \"rte\", \"wic\", \"wsc\", \"wsc.fixed\", \"axb\", \"axg\",]""" )
291
"""simple docstring""" def a__ ( snake_case__ ) -> list: if len(snake_case__ ) < 2: return collection def circle_sort_util(snake_case__ , snake_case__ , snake_case__ ) -> bool: lowerCamelCase = False if low == high: return swapped lowerCamelCase = low lowerCamelCase = high while left < right: if collection[left] > collection[right]: lowerCamelCase , lowerCamelCase = ( collection[right], collection[left], ) lowerCamelCase = True left += 1 right -= 1 if left == right and collection[left] > collection[right + 1]: lowerCamelCase , lowerCamelCase = ( collection[right + 1], collection[left], ) lowerCamelCase = True lowerCamelCase = low + int((high - low) / 2 ) lowerCamelCase = circle_sort_util(snake_case__ , snake_case__ , snake_case__ ) lowerCamelCase = circle_sort_util(snake_case__ , mid + 1 , snake_case__ ) return swapped or left_swap or right_swap lowerCamelCase = True while is_not_sorted is True: lowerCamelCase = circle_sort_util(snake_case__ , 0 , len(snake_case__ ) - 1 ) return collection if __name__ == "__main__": lowerCAmelCase : Tuple = input("""Enter numbers separated by a comma:\n""").strip() lowerCAmelCase : List[Any] = [int(item) for item in user_input.split(""",""")] print(circle_sort(unsorted))
291
1
"""simple docstring""" import os import string import sys lowerCAmelCase : Tuple = 1 << 8 lowerCAmelCase : str = { """tab""": ord("""\t"""), """newline""": ord("""\r"""), """esc""": 27, """up""": 65 + ARROW_KEY_FLAG, """down""": 66 + ARROW_KEY_FLAG, """right""": 67 + ARROW_KEY_FLAG, """left""": 68 + ARROW_KEY_FLAG, """mod_int""": 91, """undefined""": sys.maxsize, """interrupt""": 3, """insert""": 50, """delete""": 51, """pg_up""": 53, """pg_down""": 54, } lowerCAmelCase : int = KEYMAP["""up"""] lowerCAmelCase : Dict = KEYMAP["""left"""] if sys.platform == "win32": lowerCAmelCase : Union[str, Any] = [] lowerCAmelCase : List[str] = { b"""\xe0H""": KEYMAP["""up"""] - ARROW_KEY_FLAG, b"""\x00H""": KEYMAP["""up"""] - ARROW_KEY_FLAG, b"""\xe0P""": KEYMAP["""down"""] - ARROW_KEY_FLAG, b"""\x00P""": KEYMAP["""down"""] - ARROW_KEY_FLAG, b"""\xe0M""": KEYMAP["""right"""] - ARROW_KEY_FLAG, b"""\x00M""": KEYMAP["""right"""] - ARROW_KEY_FLAG, b"""\xe0K""": KEYMAP["""left"""] - ARROW_KEY_FLAG, b"""\x00K""": KEYMAP["""left"""] - ARROW_KEY_FLAG, } for i in range(10): lowerCAmelCase : Dict = ord(str(i)) def a__ ( ) -> Tuple: if os.name == "nt": import msvcrt lowerCamelCase = """mbcs""" # Flush the keyboard buffer while msvcrt.kbhit(): msvcrt.getch() if len(snake_case__ ) == 0: # Read the keystroke lowerCamelCase = msvcrt.getch() # If it is a prefix char, get second part if ch in (b"\x00", b"\xe0"): lowerCamelCase = ch + msvcrt.getch() # Translate actual Win chars to bullet char types try: lowerCamelCase = chr(WIN_KEYMAP[cha] ) WIN_CH_BUFFER.append(chr(KEYMAP["""mod_int"""] ) ) WIN_CH_BUFFER.append(snake_case__ ) if ord(snake_case__ ) in ( KEYMAP["insert"] - 1 << 9, KEYMAP["delete"] - 1 << 9, KEYMAP["pg_up"] - 1 << 9, KEYMAP["pg_down"] - 1 << 9, ): WIN_CH_BUFFER.append(chr(1_26 ) ) lowerCamelCase = chr(KEYMAP["""esc"""] ) except KeyError: lowerCamelCase = cha[1] else: lowerCamelCase = ch.decode(snake_case__ ) else: lowerCamelCase = WIN_CH_BUFFER.pop(0 ) elif os.name == "posix": import termios import tty lowerCamelCase = sys.stdin.fileno() lowerCamelCase = termios.tcgetattr(snake_case__ ) try: tty.setraw(snake_case__ ) lowerCamelCase = sys.stdin.read(1 ) finally: termios.tcsetattr(snake_case__ , termios.TCSADRAIN , snake_case__ ) return ch def a__ ( ) -> List[str]: lowerCamelCase = get_raw_chars() if ord(snake_case__ ) in [KEYMAP["interrupt"], KEYMAP["newline"]]: return char elif ord(snake_case__ ) == KEYMAP["esc"]: lowerCamelCase = get_raw_chars() if ord(snake_case__ ) == KEYMAP["mod_int"]: lowerCamelCase = get_raw_chars() if ord(snake_case__ ) >= KEYMAP["arrow_begin"] - ARROW_KEY_FLAG and ord(snake_case__ ) <= KEYMAP["arrow_end"] - ARROW_KEY_FLAG: return chr(ord(snake_case__ ) + ARROW_KEY_FLAG ) else: return KEYMAP["undefined"] else: return get_raw_chars() else: if char in string.printable: return char else: return KEYMAP["undefined"]
291
"""simple docstring""" from collections.abc import Generator def a__ ( ) -> Generator[int, None, None]: lowerCamelCase , lowerCamelCase = 0, 1 while True: lowerCamelCase , lowerCamelCase = b, a + b yield b def a__ ( snake_case__ = 10_00 ) -> int: lowerCamelCase = 1 lowerCamelCase = fibonacci_generator() while len(str(next(snake_case__ ) ) ) < n: answer += 1 return answer + 1 if __name__ == "__main__": print(solution(int(str(input()).strip())))
291
1
"""simple docstring""" import json import os import tempfile import datasets from utils import generate_example_dataset, get_duration lowerCAmelCase : int = 5_0000 lowerCAmelCase : Optional[int] = 5000 lowerCAmelCase , lowerCAmelCase : Dict = os.path.split(__file__) lowerCAmelCase : List[str] = os.path.join(RESULTS_BASEPATH, """results""", RESULTS_FILENAME.replace(""".py""", """.json""")) @get_duration def a__ ( snake_case__ , snake_case__ ) -> Tuple: for i in range(snake_case__ ): lowerCamelCase = dataset[i] @get_duration def a__ ( snake_case__ , snake_case__ , snake_case__ ) -> Any: for i in range(0 , len(snake_case__ ) , snake_case__ ): lowerCamelCase = dataset[i : i + batch_size] @get_duration def a__ ( snake_case__ , snake_case__ , snake_case__ ) -> List[str]: with dataset.formatted_as(type=snake_case__ ): for i in range(snake_case__ ): lowerCamelCase = dataset[i] @get_duration def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ ) -> List[str]: with dataset.formatted_as(type=snake_case__ ): for i in range(0 , snake_case__ , snake_case__ ): lowerCamelCase = dataset[i : i + batch_size] def a__ ( ) -> Any: lowerCamelCase = {"""num examples""": SPEED_TEST_N_EXAMPLES} lowerCamelCase = [ (read, {"""length""": SMALL_TEST}), (read, {"""length""": SPEED_TEST_N_EXAMPLES}), (read_batch, {"""length""": SPEED_TEST_N_EXAMPLES, """batch_size""": 10}), (read_batch, {"""length""": SPEED_TEST_N_EXAMPLES, """batch_size""": 1_00}), (read_batch, {"""length""": SPEED_TEST_N_EXAMPLES, """batch_size""": 10_00}), (read_formatted, {"""type""": """numpy""", """length""": SMALL_TEST}), (read_formatted, {"""type""": """pandas""", """length""": SMALL_TEST}), (read_formatted, {"""type""": """torch""", """length""": SMALL_TEST}), (read_formatted, {"""type""": """tensorflow""", """length""": SMALL_TEST}), (read_formatted_batch, {"""type""": """numpy""", """length""": SMALL_TEST, """batch_size""": 10}), (read_formatted_batch, {"""type""": """numpy""", """length""": SMALL_TEST, """batch_size""": 10_00}), ] lowerCamelCase = [ (read, {"""length""": SMALL_TEST}), (read, {"""length""": SPEED_TEST_N_EXAMPLES}), (read_batch, {"""length""": SPEED_TEST_N_EXAMPLES, """batch_size""": 10}), (read_batch, {"""length""": SPEED_TEST_N_EXAMPLES, """batch_size""": 1_00}), (read_batch, {"""length""": SPEED_TEST_N_EXAMPLES, """batch_size""": 10_00}), (read_formatted, {"""type""": """numpy""", """length""": SMALL_TEST}), (read_formatted_batch, {"""type""": """numpy""", """length""": SMALL_TEST, """batch_size""": 10}), (read_formatted_batch, {"""type""": """numpy""", """length""": SMALL_TEST, """batch_size""": 10_00}), ] with tempfile.TemporaryDirectory() as tmp_dir: print("""generating dataset""" ) lowerCamelCase = datasets.Features( {"""list""": datasets.Sequence(datasets.Value("""float32""" ) ), """numbers""": datasets.Value("""float32""" )} ) lowerCamelCase = generate_example_dataset( os.path.join(snake_case__ , """dataset.arrow""" ) , snake_case__ , num_examples=snake_case__ , seq_shapes={"""list""": (1_00,)} , ) print("""first set of iterations""" ) for func, kwargs in functions: print(func.__name__ , str(snake_case__ ) ) lowerCamelCase = func(snake_case__ , **snake_case__ ) print("""shuffling dataset""" ) lowerCamelCase = dataset.shuffle() print("""Second set of iterations (after shuffling""" ) for func, kwargs in functions_shuffled: print("""shuffled """ , func.__name__ , str(snake_case__ ) ) lowerCamelCase = func( snake_case__ , **snake_case__ ) with open(snake_case__ , """wb""" ) as f: f.write(json.dumps(snake_case__ ).encode("""utf-8""" ) ) if __name__ == "__main__": # useful to run the profiler benchmark_iterating()
291
"""simple docstring""" from math import ceil from typing import List, Optional, Union import numpy as np from ...audio_utils import mel_filter_bank, spectrogram, window_function from ...feature_extraction_sequence_utils import BatchFeature, SequenceFeatureExtractor from ...utils import TensorType, logging lowerCAmelCase : List[str] = logging.get_logger(__name__) class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' __UpperCamelCase = ["audio_values", "audio_mask"] def __init__( self , _a=2_048 , _a=1 , _a=[16, 16] , _a=128 , _a=44_100 , _a=86 , _a=2_048 , _a=0.0 , **_a , ): """simple docstring""" super().__init__( feature_size=_a , sampling_rate=_a , padding_value=_a , **_a , ) lowerCamelCase = spectrogram_length lowerCamelCase = num_channels lowerCamelCase = patch_size lowerCamelCase = feature_size // self.patch_size[1] lowerCamelCase = n_fft lowerCamelCase = sampling_rate // hop_length_to_sampling_rate lowerCamelCase = sampling_rate lowerCamelCase = padding_value lowerCamelCase = mel_filter_bank( num_frequency_bins=1 + n_fft // 2 , num_mel_filters=_a , min_frequency=0.0 , max_frequency=22_050.0 , sampling_rate=_a , norm="""slaney""" , mel_scale="""slaney""" , ).T def _lowerCAmelCase ( self , _a ): """simple docstring""" lowerCamelCase = spectrogram( _a , window_function(self.n_fft , """hann""" ) , frame_length=self.n_fft , hop_length=self.hop_length , power=2.0 , mel_filters=self.mel_filters.T , log_mel="""dB""" , db_range=80.0 , ) lowerCamelCase = log_spec[:, :-1] lowerCamelCase = log_spec - 20.0 lowerCamelCase = np.clip(log_spec / 40.0 , -2.0 , 0.0 ) + 1.0 return log_spec def __call__( self , _a , _a = None , _a = True , _a = None , _a = False , _a = False , **_a , ): """simple docstring""" if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( """This feature extractor is set to support sampling rate""" f' of {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled' f' 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.""" ) lowerCamelCase = isinstance(_a , np.ndarray ) and len(raw_speech.shape ) > 1 if is_batched_numpy and len(raw_speech.shape ) > 2: raise ValueError(f'Only mono-channel audio is supported for input to {self}' ) lowerCamelCase = is_batched_numpy or ( isinstance(_a , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: lowerCamelCase = [np.asarray([speech] , dtype=np.floataa ).T for speech in raw_speech] elif not is_batched and not isinstance(_a , np.ndarray ): lowerCamelCase = np.asarray(_a , dtype=np.floataa ) elif isinstance(_a , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): lowerCamelCase = raw_speech.astype(np.floataa ) # always return batch if not is_batched: lowerCamelCase = [np.asarray([raw_speech] ).T] # Convert audio signals to log mel spectrograms, truncate by time axis lowerCamelCase = [ self._np_extract_fbank_features(waveform.squeeze() ).T[: self.spectrogram_length] for waveform in raw_speech ] if isinstance(audio_features[0] , _a ): lowerCamelCase = [np.asarray(_a , dtype=np.floataa ) for feature in audio_features] # Create audio attention mask lowerCamelCase = max( [ceil(feature.shape[0] / self.patch_size[0] ) * self.freq_len for feature in audio_features] ) # The maximum number of audio patches in a batch if return_attention_mask: lowerCamelCase = [ (ceil(feature.shape[0] / self.patch_size[0] ) * self.freq_len) * [1] + (max_patch_len - ceil(feature.shape[0] / self.patch_size[0] ) * self.freq_len) * [0] for feature in audio_features ] lowerCamelCase = np.array(_a ).astype(np.floataa ) # convert into correct format for padding lowerCamelCase = max_patch_len // self.freq_len * self.patch_size[0] # The maximum audio size in a batch lowerCamelCase = np.ones([len(_a ), 1, max_time_len, self.feature_size] ).astype(np.floataa ) lowerCamelCase = padded_audio_features * self.padding_value for i in range(len(_a ) ): lowerCamelCase = audio_features[i] lowerCamelCase = feature # return as BatchFeature if return_attention_mask: lowerCamelCase = {"""audio_values""": padded_audio_features, """audio_mask""": audio_mask} else: lowerCamelCase = {"""audio_values""": padded_audio_features} lowerCamelCase = BatchFeature(data=_a , tensor_type=_a ) return encoded_inputs
291
1
"""simple docstring""" import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from .tokenization_electra import ElectraTokenizer lowerCAmelCase : Tuple = {"""vocab_file""": """vocab.txt""", """tokenizer_file""": """tokenizer.json"""} lowerCAmelCase : Optional[Any] = { """vocab_file""": { """google/electra-small-generator""": ( """https://huggingface.co/google/electra-small-generator/resolve/main/vocab.txt""" ), """google/electra-base-generator""": """https://huggingface.co/google/electra-base-generator/resolve/main/vocab.txt""", """google/electra-large-generator""": ( """https://huggingface.co/google/electra-large-generator/resolve/main/vocab.txt""" ), """google/electra-small-discriminator""": ( """https://huggingface.co/google/electra-small-discriminator/resolve/main/vocab.txt""" ), """google/electra-base-discriminator""": ( """https://huggingface.co/google/electra-base-discriminator/resolve/main/vocab.txt""" ), """google/electra-large-discriminator""": ( """https://huggingface.co/google/electra-large-discriminator/resolve/main/vocab.txt""" ), }, """tokenizer_file""": { """google/electra-small-generator""": ( """https://huggingface.co/google/electra-small-generator/resolve/main/tokenizer.json""" ), """google/electra-base-generator""": ( """https://huggingface.co/google/electra-base-generator/resolve/main/tokenizer.json""" ), """google/electra-large-generator""": ( """https://huggingface.co/google/electra-large-generator/resolve/main/tokenizer.json""" ), """google/electra-small-discriminator""": ( """https://huggingface.co/google/electra-small-discriminator/resolve/main/tokenizer.json""" ), """google/electra-base-discriminator""": ( """https://huggingface.co/google/electra-base-discriminator/resolve/main/tokenizer.json""" ), """google/electra-large-discriminator""": ( """https://huggingface.co/google/electra-large-discriminator/resolve/main/tokenizer.json""" ), }, } lowerCAmelCase : List[str] = { """google/electra-small-generator""": 512, """google/electra-base-generator""": 512, """google/electra-large-generator""": 512, """google/electra-small-discriminator""": 512, """google/electra-base-discriminator""": 512, """google/electra-large-discriminator""": 512, } lowerCAmelCase : Optional[Any] = { """google/electra-small-generator""": {"""do_lower_case""": True}, """google/electra-base-generator""": {"""do_lower_case""": True}, """google/electra-large-generator""": {"""do_lower_case""": True}, """google/electra-small-discriminator""": {"""do_lower_case""": True}, """google/electra-base-discriminator""": {"""do_lower_case""": True}, """google/electra-large-discriminator""": {"""do_lower_case""": True}, } class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' __UpperCamelCase = VOCAB_FILES_NAMES __UpperCamelCase = PRETRAINED_VOCAB_FILES_MAP __UpperCamelCase = PRETRAINED_INIT_CONFIGURATION __UpperCamelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCamelCase = ElectraTokenizer def __init__( self , _a=None , _a=None , _a=True , _a="[UNK]" , _a="[SEP]" , _a="[PAD]" , _a="[CLS]" , _a="[MASK]" , _a=True , _a=None , **_a , ): """simple docstring""" super().__init__( _a , tokenizer_file=_a , do_lower_case=_a , unk_token=_a , sep_token=_a , pad_token=_a , cls_token=_a , mask_token=_a , tokenize_chinese_chars=_a , strip_accents=_a , **_a , ) lowerCamelCase = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get("""lowercase""" , _a ) != do_lower_case or normalizer_state.get("""strip_accents""" , _a ) != strip_accents or normalizer_state.get("""handle_chinese_chars""" , _a ) != tokenize_chinese_chars ): lowerCamelCase = getattr(_a , normalizer_state.pop("""type""" ) ) lowerCamelCase = do_lower_case lowerCamelCase = strip_accents lowerCamelCase = tokenize_chinese_chars lowerCamelCase = normalizer_class(**_a ) lowerCamelCase = do_lower_case def _lowerCAmelCase ( self , _a , _a=None ): """simple docstring""" lowerCamelCase = [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 _lowerCAmelCase ( self , _a , _a = None ): """simple docstring""" lowerCamelCase = [self.sep_token_id] lowerCamelCase = [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 _lowerCAmelCase ( self , _a , _a = None ): """simple docstring""" lowerCamelCase = self._tokenizer.model.save(_a , name=_a ) return tuple(_a )
291
"""simple docstring""" from math import ceil def a__ ( snake_case__ , snake_case__ ) -> Optional[int]: lowerCamelCase = list(range(0 , snake_case__ ) ) lowerCamelCase = [item for sublist in list(device_map.values() ) for item in sublist] # Duplicate check lowerCamelCase = [] for i in device_map_blocks: if device_map_blocks.count(snake_case__ ) > 1 and i not in duplicate_blocks: duplicate_blocks.append(snake_case__ ) # Missing blocks lowerCamelCase = [i for i in blocks if i not in device_map_blocks] lowerCamelCase = [i for i in device_map_blocks if i not in blocks] if len(snake_case__ ) != 0: raise ValueError( """Duplicate attention blocks specified in device_map. Attention blocks must be specified to one device.""" """ These attention blocks were specified more than once: """ + str(snake_case__ ) ) if len(snake_case__ ) != 0: raise ValueError( """There are attention blocks for this model that are not specified in the device_map. Add these attention """ """blocks to a device on the device_map: """ + str(snake_case__ ) ) if len(snake_case__ ) != 0: raise ValueError( """The device_map contains more attention blocks than this model has. Remove these from the device_map:""" + str(snake_case__ ) ) def a__ ( snake_case__ , snake_case__ ) -> List[Any]: lowerCamelCase = list(range(snake_case__ ) ) lowerCamelCase = int(ceil(n_layers / len(snake_case__ ) ) ) lowerCamelCase = [layers[i : i + n_blocks] for i in range(0 , snake_case__ , snake_case__ )] return dict(zip(snake_case__ , snake_case__ ) )
291
1
"""simple docstring""" from pickle import UnpicklingError import jax import jax.numpy as jnp import numpy as np from flax.serialization import from_bytes from flax.traverse_util import flatten_dict from ..utils import logging lowerCAmelCase : Tuple = logging.get_logger(__name__) def a__ ( snake_case__ , snake_case__ ) -> Tuple: try: with open(snake_case__ , """rb""" ) as flax_state_f: lowerCamelCase = from_bytes(snake_case__ , flax_state_f.read() ) except UnpicklingError as e: try: with open(snake_case__ ) as f: if f.read().startswith("""version""" ): raise OSError( """You seem to have cloned a repository without having git-lfs installed. Please""" """ install git-lfs and run `git lfs install` followed by `git lfs pull` in the""" """ folder you cloned.""" ) else: raise ValueError from e except (UnicodeDecodeError, ValueError): raise EnvironmentError(F'Unable to convert {model_file} to Flax deserializable object. ' ) return load_flax_weights_in_pytorch_model(snake_case__ , snake_case__ ) def a__ ( snake_case__ , snake_case__ ) -> Tuple: try: import torch # noqa: F401 except ImportError: logger.error( """Loading Flax weights in PyTorch requires both PyTorch and Flax to be installed. Please see""" """ https://pytorch.org/ and https://flax.readthedocs.io/en/latest/installation.html for installation""" """ instructions.""" ) raise # check if we have bf16 weights lowerCamelCase = flatten_dict(jax.tree_util.tree_map(lambda snake_case__ : x.dtype == jnp.bfloataa , snake_case__ ) ).values() if any(snake_case__ ): # convert all weights to fp32 if they are bf16 since torch.from_numpy can-not handle bf16 # and bf16 is not fully supported in PT yet. logger.warning( """Found ``bfloat16`` weights in Flax model. Casting all ``bfloat16`` weights to ``float32`` """ """before loading those in PyTorch model.""" ) lowerCamelCase = jax.tree_util.tree_map( lambda snake_case__ : params.astype(np.floataa ) if params.dtype == jnp.bfloataa else params , snake_case__ ) lowerCamelCase = """""" lowerCamelCase = flatten_dict(snake_case__ , sep=""".""" ) lowerCamelCase = pt_model.state_dict() # keep track of unexpected & missing keys lowerCamelCase = [] lowerCamelCase = set(pt_model_dict.keys() ) for flax_key_tuple, flax_tensor in flax_state_dict.items(): lowerCamelCase = flax_key_tuple.split(""".""" ) if flax_key_tuple_array[-1] == "kernel" and flax_tensor.ndim == 4: lowerCamelCase = flax_key_tuple_array[:-1] + ["""weight"""] lowerCamelCase = jnp.transpose(snake_case__ , (3, 2, 0, 1) ) elif flax_key_tuple_array[-1] == "kernel": lowerCamelCase = flax_key_tuple_array[:-1] + ["""weight"""] lowerCamelCase = flax_tensor.T elif flax_key_tuple_array[-1] == "scale": lowerCamelCase = flax_key_tuple_array[:-1] + ["""weight"""] if "time_embedding" not in flax_key_tuple_array: for i, flax_key_tuple_string in enumerate(snake_case__ ): lowerCamelCase = ( flax_key_tuple_string.replace("""_0""" , """.0""" ) .replace("""_1""" , """.1""" ) .replace("""_2""" , """.2""" ) .replace("""_3""" , """.3""" ) .replace("""_4""" , """.4""" ) .replace("""_5""" , """.5""" ) .replace("""_6""" , """.6""" ) .replace("""_7""" , """.7""" ) .replace("""_8""" , """.8""" ) .replace("""_9""" , """.9""" ) ) lowerCamelCase = """.""".join(snake_case__ ) if flax_key in pt_model_dict: if flax_tensor.shape != pt_model_dict[flax_key].shape: raise ValueError( F'Flax checkpoint seems to be incorrect. Weight {flax_key_tuple} was expected ' F'to be of shape {pt_model_dict[flax_key].shape}, but is {flax_tensor.shape}.' ) else: # add weight to pytorch dict lowerCamelCase = np.asarray(snake_case__ ) if not isinstance(snake_case__ , np.ndarray ) else flax_tensor lowerCamelCase = torch.from_numpy(snake_case__ ) # remove from missing keys missing_keys.remove(snake_case__ ) else: # weight is not expected by PyTorch model unexpected_keys.append(snake_case__ ) pt_model.load_state_dict(snake_case__ ) # re-transform missing_keys to list lowerCamelCase = list(snake_case__ ) if len(snake_case__ ) > 0: logger.warning( """Some weights of the Flax model were not used when initializing the PyTorch model""" F' {pt_model.__class__.__name__}: {unexpected_keys}\n- This IS expected if you are initializing' F' {pt_model.__class__.__name__} from a Flax model trained on another task or with another architecture' """ (e.g. initializing a BertForSequenceClassification model from a FlaxBertForPreTraining model).\n- This""" F' IS NOT expected if you are initializing {pt_model.__class__.__name__} from a Flax model that you expect' """ to be exactly identical (e.g. initializing a BertForSequenceClassification model from a""" """ FlaxBertForSequenceClassification model).""" ) if len(snake_case__ ) > 0: logger.warning( F'Some weights of {pt_model.__class__.__name__} were not initialized from the Flax model and are newly' F' initialized: {missing_keys}\nYou should probably TRAIN this model on a down-stream task to be able to' """ use it for predictions and inference.""" ) return pt_model
291
"""simple docstring""" import unittest import numpy as np from transformers import RoFormerConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax.numpy as jnp from transformers.models.roformer.modeling_flax_roformer import ( FlaxRoFormerForMaskedLM, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerModel, ) class __magic_name__ ( unittest.TestCase ): '''simple docstring''' def __init__( self , _a , _a=13 , _a=7 , _a=True , _a=True , _a=True , _a=True , _a=99 , _a=32 , _a=5 , _a=4 , _a=37 , _a="gelu" , _a=0.1 , _a=0.1 , _a=512 , _a=16 , _a=2 , _a=0.02 , _a=4 , ): """simple docstring""" lowerCamelCase = parent lowerCamelCase = batch_size lowerCamelCase = seq_length lowerCamelCase = is_training lowerCamelCase = use_attention_mask lowerCamelCase = use_token_type_ids lowerCamelCase = use_labels lowerCamelCase = vocab_size lowerCamelCase = hidden_size lowerCamelCase = num_hidden_layers lowerCamelCase = num_attention_heads lowerCamelCase = intermediate_size lowerCamelCase = hidden_act lowerCamelCase = hidden_dropout_prob lowerCamelCase = attention_probs_dropout_prob lowerCamelCase = max_position_embeddings lowerCamelCase = type_vocab_size lowerCamelCase = type_sequence_label_size lowerCamelCase = initializer_range lowerCamelCase = num_choices def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) lowerCamelCase = None if self.use_attention_mask: lowerCamelCase = random_attention_mask([self.batch_size, self.seq_length] ) lowerCamelCase = None if self.use_token_type_ids: lowerCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) lowerCamelCase = RoFormerConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=_a , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.prepare_config_and_inputs() lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase = config_and_inputs lowerCamelCase = {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": attention_mask} return config, inputs_dict @require_flax class __magic_name__ ( UpperCAmelCase__ , unittest.TestCase ): '''simple docstring''' __UpperCamelCase = True __UpperCamelCase = ( ( FlaxRoFormerModel, FlaxRoFormerForMaskedLM, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, ) if is_flax_available() else () ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = FlaxRoFormerModelTester(self ) @slow def _lowerCAmelCase ( self ): """simple docstring""" for model_class_name in self.all_model_classes: lowerCamelCase = model_class_name.from_pretrained("""junnyu/roformer_chinese_small""" , from_pt=_a ) lowerCamelCase = model(np.ones((1, 1) ) ) self.assertIsNotNone(_a ) @require_flax class __magic_name__ ( unittest.TestCase ): '''simple docstring''' @slow def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = FlaxRoFormerForMaskedLM.from_pretrained("""junnyu/roformer_chinese_base""" ) lowerCamelCase = jnp.array([[0, 1, 2, 3, 4, 5]] ) lowerCamelCase = model(_a )[0] lowerCamelCase = 50_000 lowerCamelCase = (1, 6, vocab_size) self.assertEqual(output.shape , _a ) lowerCamelCase = jnp.array( [[[-0.1_205, -1.0_265, 0.2_922], [-1.5_134, 0.1_974, 0.1_519], [-5.0_135, -3.9_003, -0.8_404]]] ) self.assertTrue(jnp.allclose(output[:, :3, :3] , _a , atol=1e-4 ) )
291
1
"""simple docstring""" import json import os import shutil import tempfile import unittest import numpy as np from transformers import BertTokenizerFast from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES, BertTokenizer from transformers.testing_utils import require_tokenizers, require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import VisionTextDualEncoderProcessor, ViTImageProcessor @require_tokenizers @require_vision class __magic_name__ ( unittest.TestCase ): '''simple docstring''' def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = tempfile.mkdtemp() # fmt: off lowerCamelCase = ["""[UNK]""", """[CLS]""", """[SEP]""", """[PAD]""", """[MASK]""", """want""", """##want""", """##ed""", """wa""", """un""", """runn""", """##ing""", """,""", """low""", """lowest"""] # fmt: on lowerCamelCase = 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] ) ) lowerCamelCase = { """do_resize""": True, """size""": {"""height""": 18, """width""": 18}, """do_normalize""": True, """image_mean""": [0.5, 0.5, 0.5], """image_std""": [0.5, 0.5, 0.5], } lowerCamelCase = os.path.join(self.tmpdirname , _a ) with open(self.image_processor_file , """w""" , encoding="""utf-8""" ) as fp: json.dump(_a , _a ) def _lowerCAmelCase ( self , **_a ): """simple docstring""" return BertTokenizer.from_pretrained(self.tmpdirname , **_a ) def _lowerCAmelCase ( self , **_a ): """simple docstring""" return ViTImageProcessor.from_pretrained(self.tmpdirname , **_a ) def _lowerCAmelCase ( self ): """simple docstring""" shutil.rmtree(self.tmpdirname ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = [np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta )] lowerCamelCase = [Image.fromarray(np.moveaxis(_a , 0 , -1 ) ) for x in image_inputs] return image_inputs def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.get_tokenizer() lowerCamelCase = self.get_image_processor() lowerCamelCase = VisionTextDualEncoderProcessor(tokenizer=_a , image_processor=_a ) processor.save_pretrained(self.tmpdirname ) lowerCamelCase = VisionTextDualEncoderProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer.get_vocab() ) self.assertIsInstance(processor.tokenizer , (BertTokenizer, BertTokenizerFast) ) self.assertEqual(processor.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertIsInstance(processor.image_processor , _a ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = VisionTextDualEncoderProcessor( tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) lowerCamelCase = self.get_tokenizer(bos_token="""(BOS)""" , eos_token="""(EOS)""" ) lowerCamelCase = self.get_image_processor(do_normalize=_a , padding_value=1.0 ) lowerCamelCase = VisionTextDualEncoderProcessor.from_pretrained( self.tmpdirname , bos_token="""(BOS)""" , eos_token="""(EOS)""" , do_normalize=_a , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , (BertTokenizer, BertTokenizerFast) ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , _a ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.get_image_processor() lowerCamelCase = self.get_tokenizer() lowerCamelCase = VisionTextDualEncoderProcessor(tokenizer=_a , image_processor=_a ) lowerCamelCase = self.prepare_image_inputs() lowerCamelCase = image_processor(_a , return_tensors="""np""" ) lowerCamelCase = processor(images=_a , return_tensors="""np""" ) for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() , input_processor[key].sum() , delta=1e-2 ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.get_image_processor() lowerCamelCase = self.get_tokenizer() lowerCamelCase = VisionTextDualEncoderProcessor(tokenizer=_a , image_processor=_a ) lowerCamelCase = """lower newer""" lowerCamelCase = processor(text=_a ) lowerCamelCase = tokenizer(_a ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.get_image_processor() lowerCamelCase = self.get_tokenizer() lowerCamelCase = VisionTextDualEncoderProcessor(tokenizer=_a , image_processor=_a ) lowerCamelCase = """lower newer""" lowerCamelCase = self.prepare_image_inputs() lowerCamelCase = processor(text=_a , images=_a ) self.assertListEqual(list(inputs.keys() ) , ["""input_ids""", """token_type_ids""", """attention_mask""", """pixel_values"""] ) # test if it raises when no input is passed with self.assertRaises(_a ): processor() def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.get_image_processor() lowerCamelCase = self.get_tokenizer() lowerCamelCase = VisionTextDualEncoderProcessor(tokenizer=_a , image_processor=_a ) lowerCamelCase = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] lowerCamelCase = processor.batch_decode(_a ) lowerCamelCase = tokenizer.batch_decode(_a ) self.assertListEqual(_a , _a ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.get_image_processor() lowerCamelCase = self.get_tokenizer() lowerCamelCase = VisionTextDualEncoderProcessor(tokenizer=_a , image_processor=_a ) lowerCamelCase = """lower newer""" lowerCamelCase = self.prepare_image_inputs() lowerCamelCase = processor(text=_a , images=_a ) self.assertListEqual(list(inputs.keys() ) , processor.model_input_names )
291
"""simple docstring""" from typing import Any def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ) -> list: _validation( snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ) # Creates data structures and fill initial step lowerCamelCase = {} lowerCamelCase = {} for state in states_space: lowerCamelCase = observations_space[0] lowerCamelCase = ( initial_probabilities[state] * emission_probabilities[state][observation] ) lowerCamelCase = None # Fills the data structure with the probabilities of # different transitions and pointers to previous states for o in range(1 , len(snake_case__ ) ): lowerCamelCase = observations_space[o] lowerCamelCase = observations_space[o - 1] for state in states_space: # Calculates the argmax for probability function lowerCamelCase = """""" lowerCamelCase = -1 for k_state in states_space: lowerCamelCase = ( probabilities[(k_state, prior_observation)] * transition_probabilities[k_state][state] * emission_probabilities[state][observation] ) if probability > max_probability: lowerCamelCase = probability lowerCamelCase = k_state # Update probabilities and pointers dicts lowerCamelCase = ( probabilities[(arg_max, prior_observation)] * transition_probabilities[arg_max][state] * emission_probabilities[state][observation] ) lowerCamelCase = arg_max # The final observation lowerCamelCase = observations_space[len(snake_case__ ) - 1] # argmax for given final observation lowerCamelCase = """""" lowerCamelCase = -1 for k_state in states_space: lowerCamelCase = probabilities[(k_state, final_observation)] if probability > max_probability: lowerCamelCase = probability lowerCamelCase = k_state lowerCamelCase = arg_max # Process pointers backwards lowerCamelCase = last_state lowerCamelCase = [] for o in range(len(snake_case__ ) - 1 , -1 , -1 ): result.append(snake_case__ ) lowerCamelCase = pointers[previous, observations_space[o]] result.reverse() return result def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ) -> None: _validate_not_empty( snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ) _validate_lists(snake_case__ , snake_case__ ) _validate_dicts( snake_case__ , snake_case__ , snake_case__ ) def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ) -> None: if not all( [ observations_space, states_space, initial_probabilities, transition_probabilities, emission_probabilities, ] ): raise ValueError("""There's an empty parameter""" ) def a__ ( snake_case__ , snake_case__ ) -> None: _validate_list(snake_case__ , """observations_space""" ) _validate_list(snake_case__ , """states_space""" ) def a__ ( snake_case__ , snake_case__ ) -> None: if not isinstance(_object , snake_case__ ): lowerCamelCase = F'{var_name} must be a list' raise ValueError(snake_case__ ) else: for x in _object: if not isinstance(snake_case__ , snake_case__ ): lowerCamelCase = F'{var_name} must be a list of strings' raise ValueError(snake_case__ ) def a__ ( snake_case__ , snake_case__ , snake_case__ , ) -> None: _validate_dict(snake_case__ , """initial_probabilities""" , snake_case__ ) _validate_nested_dict(snake_case__ , """transition_probabilities""" ) _validate_nested_dict(snake_case__ , """emission_probabilities""" ) def a__ ( snake_case__ , snake_case__ ) -> None: _validate_dict(_object , snake_case__ , snake_case__ ) for x in _object.values(): _validate_dict(snake_case__ , snake_case__ , snake_case__ , snake_case__ ) def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ = False ) -> None: if not isinstance(_object , snake_case__ ): lowerCamelCase = F'{var_name} must be a dict' raise ValueError(snake_case__ ) if not all(isinstance(snake_case__ , snake_case__ ) for x in _object ): lowerCamelCase = F'{var_name} all keys must be strings' raise ValueError(snake_case__ ) if not all(isinstance(snake_case__ , snake_case__ ) for x in _object.values() ): lowerCamelCase = """nested dictionary """ if nested else """""" lowerCamelCase = F'{var_name} {nested_text}all values must be {value_type.__name__}' raise ValueError(snake_case__ ) if __name__ == "__main__": from doctest import testmod testmod()
291
1
"""simple docstring""" from pathlib import Path from typing import List from transformers import is_torch_available, is_vision_available from transformers.testing_utils import get_tests_dir, is_tool_test from transformers.tools.agent_types import AGENT_TYPE_MAPPING, AgentAudio, AgentImage, AgentText if is_torch_available(): import torch if is_vision_available(): from PIL import Image lowerCAmelCase : List[str] = ["""text""", """image""", """audio"""] def a__ ( snake_case__ ) -> str: lowerCamelCase = [] for input_type in input_types: if input_type == "text": inputs.append("""Text input""" ) elif input_type == "image": inputs.append( Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO""" ) ) / """000000039769.png""" ).resize((5_12, 5_12) ) ) elif input_type == "audio": inputs.append(torch.ones(30_00 ) ) elif isinstance(snake_case__ , snake_case__ ): inputs.append(create_inputs(snake_case__ ) ) else: raise ValueError(F'Invalid type requested: {input_type}' ) return inputs def a__ ( snake_case__ ) -> str: lowerCamelCase = [] for output in outputs: if isinstance(snake_case__ , (str, AgentText) ): output_types.append("""text""" ) elif isinstance(snake_case__ , (Image.Image, AgentImage) ): output_types.append("""image""" ) elif isinstance(snake_case__ , (torch.Tensor, AgentAudio) ): output_types.append("""audio""" ) else: raise ValueError(F'Invalid output: {output}' ) return output_types @is_tool_test class __magic_name__ : '''simple docstring''' def _lowerCAmelCase ( self ): """simple docstring""" self.assertTrue(hasattr(self.tool , """inputs""" ) ) self.assertTrue(hasattr(self.tool , """outputs""" ) ) lowerCamelCase = self.tool.inputs for _input in inputs: if isinstance(_input , _a ): for __input in _input: self.assertTrue(__input in authorized_types ) else: self.assertTrue(_input in authorized_types ) lowerCamelCase = self.tool.outputs for _output in outputs: self.assertTrue(_output in authorized_types ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = create_inputs(self.tool.inputs ) lowerCamelCase = self.tool(*_a ) # There is a single output if len(self.tool.outputs ) == 1: lowerCamelCase = [outputs] self.assertListEqual(output_types(_a ) , self.tool.outputs ) def _lowerCAmelCase ( self ): """simple docstring""" self.assertTrue(hasattr(self.tool , """description""" ) ) self.assertTrue(hasattr(self.tool , """default_checkpoint""" ) ) self.assertTrue(self.tool.description.startswith("""This is a tool that""" ) ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = create_inputs(self.tool.inputs ) lowerCamelCase = self.tool(*_a ) if not isinstance(_a , _a ): lowerCamelCase = [outputs] self.assertEqual(len(_a ) , len(self.tool.outputs ) ) for output, output_type in zip(_a , self.tool.outputs ): lowerCamelCase = AGENT_TYPE_MAPPING[output_type] self.assertTrue(isinstance(_a , _a ) ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = create_inputs(self.tool.inputs ) lowerCamelCase = [] for _input, input_type in zip(_a , self.tool.inputs ): if isinstance(_a , _a ): _inputs.append([AGENT_TYPE_MAPPING[_input_type](_input ) for _input_type in input_type] ) else: _inputs.append(AGENT_TYPE_MAPPING[input_type](_input ) ) # Should not raise an error lowerCamelCase = self.tool(*_a ) if not isinstance(_a , _a ): lowerCamelCase = [outputs] self.assertEqual(len(_a ) , len(self.tool.outputs ) )
291
"""simple docstring""" import argparse from pathlib import Path import torch from transformers import OPTConfig, OPTModel from transformers.utils import logging logging.set_verbosity_info() lowerCAmelCase : Dict = logging.get_logger(__name__) def a__ ( snake_case__ ) -> Dict: lowerCamelCase = torch.load(snake_case__ , map_location="""cpu""" ) if "model" in sd.keys(): lowerCamelCase = torch.load(snake_case__ , map_location="""cpu""" )["""model"""] # pop unnecessary weights lowerCamelCase = [ """decoder.version""", """decoder.output_projection.weight""", ] for key in keys_to_delete: if key in sd: sd.pop(snake_case__ ) lowerCamelCase = { """decoder.project_in_dim.weight""": """decoder.project_in.weight""", """decoder.project_out_dim.weight""": """decoder.project_out.weight""", """decoder.layer_norm.weight""": """decoder.final_layer_norm.weight""", """decoder.layer_norm.bias""": """decoder.final_layer_norm.bias""", } for old_key, new_key in keys_to_rename.items(): if old_key in sd: lowerCamelCase = sd.pop(snake_case__ ) lowerCamelCase = list(sd.keys() ) for key in keys: if ".qkv_proj." in key: lowerCamelCase = sd[key] # We split QKV in separate Q,K,V lowerCamelCase = key.replace(""".qkv_proj.""" , """.q_proj.""" ) lowerCamelCase = key.replace(""".qkv_proj.""" , """.k_proj.""" ) lowerCamelCase = key.replace(""".qkv_proj.""" , """.v_proj.""" ) lowerCamelCase = value.shape[0] assert depth % 3 == 0 # `SequeuceParallelTransformerBlock` has QKV weight is separated in K,V,Q despite the naming: # https://cs.github.com/facebookresearch/metaseq/blob/51871bd73cd04c038f239ea2a26db1d7f6b37927/metaseq/modules/sequence_parallel_transformer_layer.py#L97 lowerCamelCase , lowerCamelCase , lowerCamelCase = torch.split(snake_case__ , depth // 3 , dim=0 ) lowerCamelCase = q lowerCamelCase = k lowerCamelCase = v del sd[key] return sd @torch.no_grad() def a__ ( snake_case__ , snake_case__ , snake_case__=None ) -> Tuple: lowerCamelCase = load_checkpoint(snake_case__ ) if config is not None: lowerCamelCase = OPTConfig.from_pretrained(snake_case__ ) else: lowerCamelCase = OPTConfig() lowerCamelCase = OPTModel(snake_case__ ).half().eval() model.load_state_dict(snake_case__ ) # Check results Path(snake_case__ ).mkdir(exist_ok=snake_case__ ) model.save_pretrained(snake_case__ ) if __name__ == "__main__": lowerCAmelCase : List[str] = argparse.ArgumentParser() # Required parameters parser.add_argument( """--fairseq_path""", type=str, help=( """path to fairseq checkpoint in correct format. You can find all checkpoints in the correct format here:""" """ https://huggingface.co/models?other=opt_metasq""" ), ) parser.add_argument("""--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""") parser.add_argument("""--hf_config""", default=None, type=str, help="""Define HF config.""") lowerCAmelCase : Optional[Any] = parser.parse_args() convert_opt_checkpoint(args.fairseq_path, args.pytorch_dump_folder_path, config=args.hf_config)
291
1
"""simple docstring""" import itertools import os import random import tempfile import unittest import numpy as np from transformers import TvltFeatureExtractor, is_datasets_available from transformers.testing_utils import check_json_file_has_correct_format, require_torch, require_torchaudio from transformers.utils.import_utils import is_torch_available from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin if is_torch_available(): import torch if is_datasets_available(): from datasets import load_dataset lowerCAmelCase : int = random.Random() def a__ ( snake_case__ , snake_case__=1.0 , snake_case__=None , snake_case__=None ) -> List[Any]: if rng is None: lowerCamelCase = global_rng lowerCamelCase = [] for batch_idx in range(shape[0] ): values.append([] ) for _ in range(shape[1] ): values[-1].append(rng.random() * scale ) return values class __magic_name__ ( unittest.TestCase ): '''simple docstring''' def __init__( self , _a , _a=7 , _a=400 , _a=2_000 , _a=2_048 , _a=128 , _a=1 , _a=512 , _a=30 , _a=44_100 , ): """simple docstring""" lowerCamelCase = parent lowerCamelCase = batch_size lowerCamelCase = min_seq_length lowerCamelCase = max_seq_length lowerCamelCase = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) lowerCamelCase = spectrogram_length lowerCamelCase = feature_size lowerCamelCase = num_audio_channels lowerCamelCase = hop_length lowerCamelCase = chunk_length lowerCamelCase = sampling_rate def _lowerCAmelCase ( self ): """simple docstring""" return { "spectrogram_length": self.spectrogram_length, "feature_size": self.feature_size, "num_audio_channels": self.num_audio_channels, "hop_length": self.hop_length, "chunk_length": self.chunk_length, "sampling_rate": self.sampling_rate, } def _lowerCAmelCase ( self , _a=False , _a=False ): """simple docstring""" def _flatten(_a ): return list(itertools.chain(*_a ) ) if equal_length: lowerCamelCase = [floats_list((self.max_seq_length, self.feature_size) ) for _ in range(self.batch_size )] else: # make sure that inputs increase in size lowerCamelCase = [ floats_list((x, self.feature_size) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: lowerCamelCase = [np.asarray(_a ) for x in speech_inputs] return speech_inputs @require_torch @require_torchaudio class __magic_name__ ( UpperCAmelCase__ , unittest.TestCase ): '''simple docstring''' __UpperCamelCase = TvltFeatureExtractor def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = TvltFeatureExtractionTester(self ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.feature_extraction_class(**self.feat_extract_dict ) self.assertTrue(hasattr(_a , """spectrogram_length""" ) ) self.assertTrue(hasattr(_a , """feature_size""" ) ) self.assertTrue(hasattr(_a , """num_audio_channels""" ) ) self.assertTrue(hasattr(_a , """hop_length""" ) ) self.assertTrue(hasattr(_a , """chunk_length""" ) ) self.assertTrue(hasattr(_a , """sampling_rate""" ) ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: lowerCamelCase = feat_extract_first.save_pretrained(_a )[0] check_json_file_has_correct_format(_a ) lowerCamelCase = self.feature_extraction_class.from_pretrained(_a ) lowerCamelCase = feat_extract_first.to_dict() lowerCamelCase = feat_extract_second.to_dict() lowerCamelCase = dict_first.pop("""mel_filters""" ) lowerCamelCase = dict_second.pop("""mel_filters""" ) self.assertTrue(np.allclose(_a , _a ) ) self.assertEqual(_a , _a ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: lowerCamelCase = os.path.join(_a , """feat_extract.json""" ) feat_extract_first.to_json_file(_a ) lowerCamelCase = self.feature_extraction_class.from_json_file(_a ) lowerCamelCase = feat_extract_first.to_dict() lowerCamelCase = feat_extract_second.to_dict() lowerCamelCase = dict_first.pop("""mel_filters""" ) lowerCamelCase = dict_second.pop("""mel_filters""" ) self.assertTrue(np.allclose(_a , _a ) ) self.assertEqual(_a , _a ) def _lowerCAmelCase ( self ): """simple docstring""" # Initialize feature_extractor lowerCamelCase = self.feature_extraction_class(**self.feat_extract_dict ) # create three inputs of length 800, 1000, and 1200 lowerCamelCase = [floats_list((1, x) )[0] for x in range(800 , 1_400 , 200 )] lowerCamelCase = [np.asarray(_a ) for speech_input in speech_inputs] # Test not batched input lowerCamelCase = feature_extractor(np_speech_inputs[0] , return_tensors="""np""" , sampling_rate=44_100 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test batched lowerCamelCase = feature_extractor(_a , return_tensors="""np""" , sampling_rate=44_100 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test audio masking lowerCamelCase = feature_extractor( _a , return_tensors="""np""" , sampling_rate=44_100 , mask_audio=_a ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test 2-D numpy arrays are batched. lowerCamelCase = [floats_list((1, x) )[0] for x in (800, 800, 800)] lowerCamelCase = np.asarray(_a ) lowerCamelCase = feature_extractor(_a , return_tensors="""np""" , sampling_rate=44_100 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) def _lowerCAmelCase ( self , _a ): """simple docstring""" lowerCamelCase = load_dataset("""hf-internal-testing/librispeech_asr_dummy""" , """clean""" , split="""validation""" ) # automatic decoding with librispeech lowerCamelCase = ds.sort("""id""" ).select(range(_a ) )[:num_samples]["""audio"""] return [x["array"] for x in speech_samples] def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self._load_datasamples(1 ) lowerCamelCase = TvltFeatureExtractor() lowerCamelCase = feature_extractor(_a , return_tensors="""pt""" ).audio_values self.assertEquals(audio_values.shape , (1, 1, 192, 128) ) lowerCamelCase = torch.tensor([[-0.3_032, -0.2_708], [-0.4_434, -0.4_007]] ) self.assertTrue(torch.allclose(audio_values[0, 0, :2, :2] , _a , atol=1e-4 ) )
291
"""simple docstring""" import json import os import shutil import tempfile import unittest import numpy as np from transformers import BertTokenizerFast from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES, BertTokenizer from transformers.testing_utils import require_tokenizers, require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import VisionTextDualEncoderProcessor, ViTImageProcessor @require_tokenizers @require_vision class __magic_name__ ( unittest.TestCase ): '''simple docstring''' def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = tempfile.mkdtemp() # fmt: off lowerCamelCase = ["""[UNK]""", """[CLS]""", """[SEP]""", """[PAD]""", """[MASK]""", """want""", """##want""", """##ed""", """wa""", """un""", """runn""", """##ing""", """,""", """low""", """lowest"""] # fmt: on lowerCamelCase = 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] ) ) lowerCamelCase = { """do_resize""": True, """size""": {"""height""": 18, """width""": 18}, """do_normalize""": True, """image_mean""": [0.5, 0.5, 0.5], """image_std""": [0.5, 0.5, 0.5], } lowerCamelCase = os.path.join(self.tmpdirname , _a ) with open(self.image_processor_file , """w""" , encoding="""utf-8""" ) as fp: json.dump(_a , _a ) def _lowerCAmelCase ( self , **_a ): """simple docstring""" return BertTokenizer.from_pretrained(self.tmpdirname , **_a ) def _lowerCAmelCase ( self , **_a ): """simple docstring""" return ViTImageProcessor.from_pretrained(self.tmpdirname , **_a ) def _lowerCAmelCase ( self ): """simple docstring""" shutil.rmtree(self.tmpdirname ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = [np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta )] lowerCamelCase = [Image.fromarray(np.moveaxis(_a , 0 , -1 ) ) for x in image_inputs] return image_inputs def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.get_tokenizer() lowerCamelCase = self.get_image_processor() lowerCamelCase = VisionTextDualEncoderProcessor(tokenizer=_a , image_processor=_a ) processor.save_pretrained(self.tmpdirname ) lowerCamelCase = VisionTextDualEncoderProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer.get_vocab() ) self.assertIsInstance(processor.tokenizer , (BertTokenizer, BertTokenizerFast) ) self.assertEqual(processor.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertIsInstance(processor.image_processor , _a ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = VisionTextDualEncoderProcessor( tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) lowerCamelCase = self.get_tokenizer(bos_token="""(BOS)""" , eos_token="""(EOS)""" ) lowerCamelCase = self.get_image_processor(do_normalize=_a , padding_value=1.0 ) lowerCamelCase = VisionTextDualEncoderProcessor.from_pretrained( self.tmpdirname , bos_token="""(BOS)""" , eos_token="""(EOS)""" , do_normalize=_a , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , (BertTokenizer, BertTokenizerFast) ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , _a ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.get_image_processor() lowerCamelCase = self.get_tokenizer() lowerCamelCase = VisionTextDualEncoderProcessor(tokenizer=_a , image_processor=_a ) lowerCamelCase = self.prepare_image_inputs() lowerCamelCase = image_processor(_a , return_tensors="""np""" ) lowerCamelCase = processor(images=_a , return_tensors="""np""" ) for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() , input_processor[key].sum() , delta=1e-2 ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.get_image_processor() lowerCamelCase = self.get_tokenizer() lowerCamelCase = VisionTextDualEncoderProcessor(tokenizer=_a , image_processor=_a ) lowerCamelCase = """lower newer""" lowerCamelCase = processor(text=_a ) lowerCamelCase = tokenizer(_a ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.get_image_processor() lowerCamelCase = self.get_tokenizer() lowerCamelCase = VisionTextDualEncoderProcessor(tokenizer=_a , image_processor=_a ) lowerCamelCase = """lower newer""" lowerCamelCase = self.prepare_image_inputs() lowerCamelCase = processor(text=_a , images=_a ) self.assertListEqual(list(inputs.keys() ) , ["""input_ids""", """token_type_ids""", """attention_mask""", """pixel_values"""] ) # test if it raises when no input is passed with self.assertRaises(_a ): processor() def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.get_image_processor() lowerCamelCase = self.get_tokenizer() lowerCamelCase = VisionTextDualEncoderProcessor(tokenizer=_a , image_processor=_a ) lowerCamelCase = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] lowerCamelCase = processor.batch_decode(_a ) lowerCamelCase = tokenizer.batch_decode(_a ) self.assertListEqual(_a , _a ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.get_image_processor() lowerCamelCase = self.get_tokenizer() lowerCamelCase = VisionTextDualEncoderProcessor(tokenizer=_a , image_processor=_a ) lowerCamelCase = """lower newer""" lowerCamelCase = self.prepare_image_inputs() lowerCamelCase = processor(text=_a , images=_a ) self.assertListEqual(list(inputs.keys() ) , processor.model_input_names )
291
1
"""simple docstring""" from typing import TYPE_CHECKING from ..utils import _LazyModule lowerCAmelCase : Any = { """config""": [ """EXTERNAL_DATA_FORMAT_SIZE_LIMIT""", """OnnxConfig""", """OnnxConfigWithPast""", """OnnxSeq2SeqConfigWithPast""", """PatchingSpec""", ], """convert""": ["""export""", """validate_model_outputs"""], """features""": ["""FeaturesManager"""], """utils""": ["""ParameterFormat""", """compute_serialized_parameters_size"""], } if TYPE_CHECKING: from .config import ( EXTERNAL_DATA_FORMAT_SIZE_LIMIT, OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast, PatchingSpec, ) from .convert import export, validate_model_outputs from .features import FeaturesManager from .utils import ParameterFormat, compute_serialized_parameters_size else: import sys lowerCAmelCase : str = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
291
"""simple docstring""" import importlib import sys from argparse import REMAINDER, ArgumentParser from pathlib import Path import torch_xla.distributed.xla_multiprocessing as xmp def a__ ( ) -> Union[str, Any]: lowerCamelCase = ArgumentParser( description=( """PyTorch TPU distributed training launch """ """helper utility that will spawn up """ """multiple distributed processes""" ) ) # Optional arguments for the launch helper parser.add_argument("""--num_cores""" , type=snake_case__ , default=1 , help="""Number of TPU cores to use (1 or 8).""" ) # positional parser.add_argument( """training_script""" , type=snake_case__ , help=( """The full path to the single TPU training """ """program/script to be launched in parallel, """ """followed by all the arguments for the """ """training script""" ) , ) # rest from the training program parser.add_argument("""training_script_args""" , nargs=snake_case__ ) return parser.parse_args() def a__ ( ) -> List[str]: lowerCamelCase = parse_args() # Import training_script as a module. lowerCamelCase = Path(args.training_script ) sys.path.append(str(script_fpath.parent.resolve() ) ) lowerCamelCase = script_fpath.stem lowerCamelCase = importlib.import_module(snake_case__ ) # Patch sys.argv lowerCamelCase = [args.training_script] + args.training_script_args + ["""--tpu_num_cores""", str(args.num_cores )] xmp.spawn(mod._mp_fn , args=() , nprocs=args.num_cores ) if __name__ == "__main__": main()
291
1
"""simple docstring""" import warnings from typing import List, Optional, Union from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' __UpperCamelCase = ["image_processor", "tokenizer"] __UpperCamelCase = "LayoutLMv2ImageProcessor" __UpperCamelCase = ("LayoutXLMTokenizer", "LayoutXLMTokenizerFast") def __init__( self , _a=None , _a=None , **_a ): """simple docstring""" if "feature_extractor" in kwargs: warnings.warn( """The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`""" """ instead.""" , _a , ) lowerCamelCase = kwargs.pop("""feature_extractor""" ) lowerCamelCase = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError("""You need to specify an `image_processor`.""" ) if tokenizer is None: raise ValueError("""You need to specify a `tokenizer`.""" ) super().__init__(_a , _a ) def __call__( self , _a , _a = None , _a = None , _a = None , _a = None , _a = True , _a = False , _a = None , _a = None , _a = 0 , _a = None , _a = None , _a = None , _a = False , _a = False , _a = False , _a = False , _a = True , _a = None , **_a , ): """simple docstring""" # verify input if self.image_processor.apply_ocr and (boxes is not None): raise ValueError( """You cannot provide bounding boxes """ """if you initialized the image processor with apply_ocr set to True.""" ) if self.image_processor.apply_ocr and (word_labels is not None): raise ValueError( """You cannot provide word labels if you initialized the image processor with apply_ocr set to True.""" ) if return_overflowing_tokens is True and return_offsets_mapping is False: raise ValueError("""You cannot return overflowing tokens without returning the offsets mapping.""" ) # first, apply the image processor lowerCamelCase = self.image_processor(images=_a , return_tensors=_a ) # second, apply the tokenizer if text is not None and self.image_processor.apply_ocr and text_pair is None: if isinstance(_a , _a ): lowerCamelCase = [text] # add batch dimension (as the image processor always adds a batch dimension) lowerCamelCase = features["""words"""] lowerCamelCase = self.tokenizer( text=text if text is not None else features["""words"""] , text_pair=text_pair if text_pair is not None else None , boxes=boxes if boxes is not None else features["""boxes"""] , word_labels=_a , add_special_tokens=_a , padding=_a , truncation=_a , max_length=_a , stride=_a , pad_to_multiple_of=_a , return_token_type_ids=_a , return_attention_mask=_a , return_overflowing_tokens=_a , return_special_tokens_mask=_a , return_offsets_mapping=_a , return_length=_a , verbose=_a , return_tensors=_a , **_a , ) # add pixel values lowerCamelCase = features.pop("""pixel_values""" ) if return_overflowing_tokens is True: lowerCamelCase = self.get_overflowing_images(_a , encoded_inputs["""overflow_to_sample_mapping"""] ) lowerCamelCase = images return encoded_inputs def _lowerCAmelCase ( self , _a , _a ): """simple docstring""" # in case there's an overflow, ensure each `input_ids` sample is mapped to its corresponding image lowerCamelCase = [] for sample_idx in overflow_to_sample_mapping: images_with_overflow.append(images[sample_idx] ) if len(_a ) != len(_a ): raise ValueError( """Expected length of images to be the same as the length of `overflow_to_sample_mapping`, but got""" f' {len(_a )} and {len(_a )}' ) return images_with_overflow def _lowerCAmelCase ( self , *_a , **_a ): """simple docstring""" return self.tokenizer.batch_decode(*_a , **_a ) def _lowerCAmelCase ( self , *_a , **_a ): """simple docstring""" return self.tokenizer.decode(*_a , **_a ) @property def _lowerCAmelCase ( self ): """simple docstring""" return ["input_ids", "bbox", "attention_mask", "image"] @property def _lowerCAmelCase ( self ): """simple docstring""" warnings.warn( """`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.""" , _a , ) return self.image_processor_class @property def _lowerCAmelCase ( self ): """simple docstring""" warnings.warn( """`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.""" , _a , ) return self.image_processor
291
"""simple docstring""" import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase : int = logging.get_logger(__name__) lowerCAmelCase : List[str] = { """asapp/sew-d-tiny-100k""": """https://huggingface.co/asapp/sew-d-tiny-100k/resolve/main/config.json""", # See all SEW-D models at https://huggingface.co/models?filter=sew-d } class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' __UpperCamelCase = "sew-d" def __init__( self , _a=32 , _a=768 , _a=12 , _a=12 , _a=3_072 , _a=2 , _a=512 , _a=256 , _a=True , _a=True , _a=("p2c", "c2p") , _a="layer_norm" , _a="gelu_python" , _a=0.1 , _a=0.1 , _a=0.1 , _a=0.0 , _a=0.1 , _a=0.02 , _a=1e-7 , _a=1e-5 , _a="group" , _a="gelu" , _a=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512) , _a=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1) , _a=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1) , _a=False , _a=128 , _a=16 , _a=True , _a=0.05 , _a=10 , _a=2 , _a=0.0 , _a=10 , _a=0 , _a="mean" , _a=False , _a=False , _a=256 , _a=0 , _a=1 , _a=2 , **_a , ): """simple docstring""" super().__init__(**_a , pad_token_id=_a , bos_token_id=_a , eos_token_id=_a ) lowerCamelCase = hidden_size lowerCamelCase = feat_extract_norm lowerCamelCase = feat_extract_activation lowerCamelCase = list(_a ) lowerCamelCase = list(_a ) lowerCamelCase = list(_a ) lowerCamelCase = conv_bias lowerCamelCase = num_conv_pos_embeddings lowerCamelCase = num_conv_pos_embedding_groups lowerCamelCase = len(self.conv_dim ) lowerCamelCase = num_hidden_layers lowerCamelCase = intermediate_size lowerCamelCase = squeeze_factor lowerCamelCase = max_position_embeddings lowerCamelCase = position_buckets lowerCamelCase = share_att_key lowerCamelCase = relative_attention lowerCamelCase = norm_rel_ebd lowerCamelCase = list(_a ) lowerCamelCase = hidden_act lowerCamelCase = num_attention_heads lowerCamelCase = hidden_dropout lowerCamelCase = attention_dropout lowerCamelCase = activation_dropout lowerCamelCase = feat_proj_dropout lowerCamelCase = final_dropout lowerCamelCase = layer_norm_eps lowerCamelCase = feature_layer_norm_eps lowerCamelCase = initializer_range lowerCamelCase = vocab_size if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( """Configuration for convolutional layers is incorrect.""" """It is required that `len(config.conv_dim)` == `len(config.conv_stride)` == `len(config.conv_kernel)`,""" f'but is `len(config.conv_dim) = {len(self.conv_dim )}`, `len(config.conv_stride)' f'= {len(self.conv_stride )}`, `len(config.conv_kernel) = {len(self.conv_kernel )}`.' ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 lowerCamelCase = apply_spec_augment lowerCamelCase = mask_time_prob lowerCamelCase = mask_time_length lowerCamelCase = mask_time_min_masks lowerCamelCase = mask_feature_prob lowerCamelCase = mask_feature_length lowerCamelCase = mask_feature_min_masks # ctc loss lowerCamelCase = ctc_loss_reduction lowerCamelCase = ctc_zero_infinity # sequence classification lowerCamelCase = use_weighted_layer_sum lowerCamelCase = classifier_proj_size @property def _lowerCAmelCase ( self ): """simple docstring""" return functools.reduce(operator.mul , self.conv_stride , 1 )
291
1
"""simple docstring""" import argparse import datetime import json import time import warnings from logging import getLogger from pathlib import Path from typing import Dict, List import torch from tqdm import tqdm from transformers import AutoModelForSeqaSeqLM, AutoTokenizer from utils import calculate_bleu, calculate_rouge, chunks, parse_numeric_n_bool_cl_kwargs, use_task_specific_params lowerCAmelCase : str = getLogger(__name__) lowerCAmelCase : Union[str, Any] = """cuda""" if torch.cuda.is_available() else """cpu""" def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ = 8 , snake_case__ = DEFAULT_DEVICE , snake_case__=False , snake_case__="summarization" , snake_case__=None , **snake_case__ , ) -> Dict: lowerCamelCase = Path(snake_case__ ).open("""w""" , encoding="""utf-8""" ) lowerCamelCase = str(snake_case__ ) lowerCamelCase = AutoModelForSeqaSeqLM.from_pretrained(snake_case__ ).to(snake_case__ ) if fpaa: lowerCamelCase = model.half() lowerCamelCase = AutoTokenizer.from_pretrained(snake_case__ ) logger.info(F'Inferred tokenizer type: {tokenizer.__class__}' ) # if this is wrong, check config.model_type. lowerCamelCase = time.time() # update config with task specific params use_task_specific_params(snake_case__ , snake_case__ ) if prefix is None: lowerCamelCase = prefix or getattr(model.config , """prefix""" , """""" ) or """""" for examples_chunk in tqdm(list(chunks(snake_case__ , snake_case__ ) ) ): lowerCamelCase = [prefix + text for text in examples_chunk] lowerCamelCase = tokenizer(snake_case__ , return_tensors="""pt""" , truncation=snake_case__ , padding="""longest""" ).to(snake_case__ ) lowerCamelCase = model.generate( input_ids=batch.input_ids , attention_mask=batch.attention_mask , **snake_case__ , ) lowerCamelCase = tokenizer.batch_decode(snake_case__ , skip_special_tokens=snake_case__ , clean_up_tokenization_spaces=snake_case__ ) for hypothesis in dec: fout.write(hypothesis + """\n""" ) fout.flush() fout.close() lowerCamelCase = int(time.time() - start_time ) # seconds lowerCamelCase = len(snake_case__ ) return {"n_obs": n_obs, "runtime": runtime, "seconds_per_sample": round(runtime / n_obs , 4 )} def a__ ( ) -> List[str]: return datetime.datetime.now().strftime("""%Y-%m-%d %H:%M:%S""" ) def a__ ( snake_case__=True ) -> List[str]: lowerCamelCase = argparse.ArgumentParser() parser.add_argument("""model_name""" , type=snake_case__ , help="""like facebook/bart-large-cnn,t5-base, etc.""" ) parser.add_argument("""input_path""" , type=snake_case__ , help="""like cnn_dm/test.source""" ) parser.add_argument("""save_path""" , type=snake_case__ , help="""where to save summaries""" ) parser.add_argument("""--reference_path""" , type=snake_case__ , required=snake_case__ , help="""like cnn_dm/test.target""" ) parser.add_argument("""--score_path""" , type=snake_case__ , required=snake_case__ , default="""metrics.json""" , help="""where to save metrics""" ) parser.add_argument("""--device""" , type=snake_case__ , required=snake_case__ , default=snake_case__ , help="""cuda, cuda:1, cpu etc.""" ) parser.add_argument( """--prefix""" , type=snake_case__ , required=snake_case__ , default=snake_case__ , help="""will be added to the begininng of src examples""" ) parser.add_argument("""--task""" , type=snake_case__ , default="""summarization""" , help="""used for task_specific_params + metrics""" ) parser.add_argument("""--bs""" , type=snake_case__ , default=8 , required=snake_case__ , help="""batch size""" ) parser.add_argument( """--n_obs""" , type=snake_case__ , default=-1 , required=snake_case__ , help="""How many observations. Defaults to all.""" ) parser.add_argument("""--fp16""" , action="""store_true""" ) parser.add_argument("""--dump-args""" , action="""store_true""" , help="""print the custom hparams with the results""" ) parser.add_argument( """--info""" , nargs="""?""" , type=snake_case__ , const=datetime_now() , help=( """use in conjunction w/ --dump-args to print with the results whatever other info you'd like, e.g.""" """ lang=en-ru. If no value is passed, the current datetime string will be used.""" ) , ) # Unspecified args like --num_beams=2 --decoder_start_token_id=4 are passed to model.generate lowerCamelCase , lowerCamelCase = parser.parse_known_args() lowerCamelCase = parse_numeric_n_bool_cl_kwargs(snake_case__ ) if parsed_args and verbose: print(F'parsed the following generate kwargs: {parsed_args}' ) lowerCamelCase = [""" """ + x.rstrip() if """t5""" in args.model_name else x.rstrip() for x in open(args.input_path ).readlines()] if args.n_obs > 0: lowerCamelCase = examples[: args.n_obs] Path(args.save_path ).parent.mkdir(exist_ok=snake_case__ ) if args.reference_path is None and Path(args.score_path ).exists(): warnings.warn(F'score_path {args.score_path} will be overwritten unless you type ctrl-c.' ) if args.device == "cpu" and args.fpaa: # this mix leads to RuntimeError: "threshold_cpu" not implemented for 'Half' raise ValueError("""Can't mix --fp16 and --device cpu""" ) lowerCamelCase = generate_summaries_or_translations( snake_case__ , args.save_path , args.model_name , batch_size=args.bs , device=args.device , fpaa=args.fpaa , task=args.task , prefix=args.prefix , **snake_case__ , ) if args.reference_path is None: return {} # Compute scores lowerCamelCase = calculate_bleu if """translation""" in args.task else calculate_rouge lowerCamelCase = [x.rstrip() for x in open(args.save_path ).readlines()] lowerCamelCase = [x.rstrip() for x in open(args.reference_path ).readlines()][: len(snake_case__ )] lowerCamelCase = score_fn(snake_case__ , snake_case__ ) scores.update(snake_case__ ) if args.dump_args: scores.update(snake_case__ ) if args.info: lowerCamelCase = args.info if verbose: print(snake_case__ ) if args.score_path is not None: json.dump(snake_case__ , open(args.score_path , """w""" ) ) return scores if __name__ == "__main__": # Usage for MT: # python run_eval.py MODEL_NAME $DATA_DIR/test.source $save_dir/test_translations.txt --reference_path $DATA_DIR/test.target --score_path $save_dir/test_bleu.json --task translation $@ run_generate(verbose=True)
291
"""simple docstring""" from sklearn.metrics import recall_score import datasets lowerCAmelCase : Any = """ Recall is the fraction of the positive examples that were correctly labeled by the model as positive. It can be computed with the equation: Recall = TP / (TP + FN) Where TP is the true positives and FN is the false negatives. """ lowerCAmelCase : Any = """ Args: - **predictions** (`list` of `int`): The predicted labels. - **references** (`list` of `int`): The ground truth labels. - **labels** (`list` of `int`): The set of labels to include when `average` is not set to `binary`, and their order when average is `None`. Labels present in the data can be excluded in this input, for example to calculate a multiclass average ignoring a majority negative class, while labels not present in the data will result in 0 components in a macro average. For multilabel targets, labels are column indices. By default, all labels in y_true and y_pred are used in sorted order. Defaults to None. - **pos_label** (`int`): The class label to use as the 'positive class' when calculating the recall. Defaults to `1`. - **average** (`string`): This parameter is required for multiclass/multilabel targets. If None, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data. Defaults to `'binary'`. - `'binary'`: Only report results for the class specified by `pos_label`. This is applicable only if the target labels and predictions are binary. - `'micro'`: Calculate metrics globally by counting the total true positives, false negatives, and false positives. - `'macro'`: Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account. - `'weighted'`: Calculate metrics for each label, and find their average weighted by support (the number of true instances for each label). This alters `'macro'` to account for label imbalance. Note that it can result in an F-score that is not between precision and recall. - `'samples'`: Calculate metrics for each instance, and find their average (only meaningful for multilabel classification). - **sample_weight** (`list` of `float`): Sample weights Defaults to `None`. - **zero_division** (): Sets the value to return when there is a zero division. Defaults to . - `'warn'`: If there is a zero division, the return value is `0`, but warnings are also raised. - `0`: If there is a zero division, the return value is `0`. - `1`: If there is a zero division, the return value is `1`. Returns: - **recall** (`float`, or `array` of `float`): Either the general recall score, or the recall scores for individual classes, depending on the values input to `labels` and `average`. Minimum possible value is 0. Maximum possible value is 1. A higher recall means that more of the positive examples have been labeled correctly. Therefore, a higher recall is generally considered better. Examples: Example 1-A simple example with some errors >>> recall_metric = datasets.load_metric('recall') >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1]) >>> print(results) {'recall': 0.6666666666666666} Example 2-The same example as Example 1, but with `pos_label=0` instead of the default `pos_label=1`. >>> recall_metric = datasets.load_metric('recall') >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1], pos_label=0) >>> print(results) {'recall': 0.5} Example 3-The same example as Example 1, but with `sample_weight` included. >>> recall_metric = datasets.load_metric('recall') >>> sample_weight = [0.9, 0.2, 0.9, 0.3, 0.8] >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1], sample_weight=sample_weight) >>> print(results) {'recall': 0.55} Example 4-A multiclass example, using different averages. >>> recall_metric = datasets.load_metric('recall') >>> predictions = [0, 2, 1, 0, 0, 1] >>> references = [0, 1, 2, 0, 1, 2] >>> results = recall_metric.compute(predictions=predictions, references=references, average='macro') >>> print(results) {'recall': 0.3333333333333333} >>> results = recall_metric.compute(predictions=predictions, references=references, average='micro') >>> print(results) {'recall': 0.3333333333333333} >>> results = recall_metric.compute(predictions=predictions, references=references, average='weighted') >>> print(results) {'recall': 0.3333333333333333} >>> results = recall_metric.compute(predictions=predictions, references=references, average=None) >>> print(results) {'recall': array([1., 0., 0.])} """ lowerCAmelCase : Any = """ @article{scikit-learn, title={Scikit-learn: Machine Learning in {P}ython}, author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V. and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P. and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.}, journal={Journal of Machine Learning Research}, volume={12}, pages={2825--2830}, year={2011} """ @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __magic_name__ ( datasets.Metric ): '''simple docstring''' def _lowerCAmelCase ( self ): """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": datasets.Sequence(datasets.Value("""int32""" ) ), """references""": datasets.Sequence(datasets.Value("""int32""" ) ), } if self.config_name == """multilabel""" else { """predictions""": datasets.Value("""int32""" ), """references""": datasets.Value("""int32""" ), } ) , reference_urls=["""https://scikit-learn.org/stable/modules/generated/sklearn.metrics.recall_score.html"""] , ) def _lowerCAmelCase ( self , _a , _a , _a=None , _a=1 , _a="binary" , _a=None , _a="warn" , ): """simple docstring""" lowerCamelCase = recall_score( _a , _a , labels=_a , pos_label=_a , average=_a , sample_weight=_a , zero_division=_a , ) return {"recall": float(_a ) if score.size == 1 else score}
291
1
"""simple docstring""" from pathlib import PurePosixPath from typing import Optional import fsspec from fsspec import AbstractFileSystem from huggingface_hub.hf_api import DatasetInfo from ..utils.file_utils import get_authentication_headers_for_url from ..utils.hub import hf_hub_url class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' __UpperCamelCase = "" __UpperCamelCase = "hf-legacy" # "hf://"" is reserved for hffs def __init__( self , _a = None , _a = None , **_a , ): """simple docstring""" super().__init__(self , **_a ) lowerCamelCase = repo_info lowerCamelCase = token lowerCamelCase = None def _lowerCAmelCase ( self ): """simple docstring""" if self.dir_cache is None: lowerCamelCase = {} for hf_file in self.repo_info.siblings: # TODO(QL): add sizes lowerCamelCase = { """name""": hf_file.rfilename, """size""": None, """type""": """file""", } self.dir_cache.update( { str(_a ): {"""name""": str(_a ), """size""": None, """type""": """directory"""} for d in list(PurePosixPath(hf_file.rfilename ).parents )[:-1] } ) def _lowerCAmelCase ( self , _a , _a = "rb" , **_a , ): """simple docstring""" if not isinstance(self.repo_info , _a ): raise NotImplementedError(f'Open is only implemented for dataset repositories, but got {self.repo_info}' ) lowerCamelCase = hf_hub_url(self.repo_info.id , _a , revision=self.repo_info.sha ) return fsspec.open( _a , mode=_a , headers=get_authentication_headers_for_url(_a , use_auth_token=self.token ) , client_kwargs={"""trust_env""": True} , ).open() def _lowerCAmelCase ( self , _a , **_a ): """simple docstring""" self._get_dirs() lowerCamelCase = self._strip_protocol(_a ) if path in self.dir_cache: return self.dir_cache[path] else: raise FileNotFoundError(_a ) def _lowerCAmelCase ( self , _a , _a=False , **_a ): """simple docstring""" self._get_dirs() lowerCamelCase = PurePosixPath(path.strip("""/""" ) ) lowerCamelCase = {} for p, f in self.dir_cache.items(): lowerCamelCase = PurePosixPath(p.strip("""/""" ) ) lowerCamelCase = p.parent if root == path: lowerCamelCase = f lowerCamelCase = list(paths.values() ) if detail: return out else: return sorted(f["""name"""] for f in out )
291
"""simple docstring""" import numpy as np import torch from torch.utils.data import Dataset, IterableDataset from ..utils.generic import ModelOutput class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' def __init__( self , _a , _a , _a ): """simple docstring""" lowerCamelCase = dataset lowerCamelCase = process lowerCamelCase = params def __len__( self ): """simple docstring""" return len(self.dataset ) def __getitem__( self , _a ): """simple docstring""" lowerCamelCase = self.dataset[i] lowerCamelCase = self.process(_a , **self.params ) return processed class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' def __init__( self , _a , _a , _a , _a=None ): """simple docstring""" lowerCamelCase = loader lowerCamelCase = infer lowerCamelCase = params if loader_batch_size == 1: # Let's spare some time by deactivating altogether lowerCamelCase = None lowerCamelCase = loader_batch_size # Internal bookkeeping lowerCamelCase = None lowerCamelCase = None def __len__( self ): """simple docstring""" return len(self.loader ) def __iter__( self ): """simple docstring""" lowerCamelCase = iter(self.loader ) return self def _lowerCAmelCase ( self ): """simple docstring""" if isinstance(self._loader_batch_data , torch.Tensor ): # Batch data is simple tensor, just fetch the slice lowerCamelCase = self._loader_batch_data[self._loader_batch_index] else: # Batch data is assumed to be BaseModelOutput (or dict) lowerCamelCase = {} for k, element in self._loader_batch_data.items(): if isinstance(_a , _a ): # Convert ModelOutput to tuple first lowerCamelCase = element.to_tuple() if isinstance(element[0] , torch.Tensor ): lowerCamelCase = tuple(el[self._loader_batch_index].unsqueeze(0 ) for el in element ) elif isinstance(element[0] , np.ndarray ): lowerCamelCase = tuple(np.expand_dims(el[self._loader_batch_index] , 0 ) for el in element ) continue if k in {"hidden_states", "past_key_values", "attentions"} and isinstance(_a , _a ): # Those are stored as lists of tensors so need specific unbatching. if isinstance(element[0] , torch.Tensor ): lowerCamelCase = tuple(el[self._loader_batch_index].unsqueeze(0 ) for el in element ) elif isinstance(element[0] , np.ndarray ): lowerCamelCase = tuple(np.expand_dims(el[self._loader_batch_index] , 0 ) for el in element ) continue if element is None: # This can happen for optional data that get passed around lowerCamelCase = None elif isinstance(element[self._loader_batch_index] , torch.Tensor ): # Take correct batch data, but make it looked like batch_size=1 # For compatibility with other methods within transformers lowerCamelCase = element[self._loader_batch_index].unsqueeze(0 ) elif isinstance(element[self._loader_batch_index] , np.ndarray ): # Take correct batch data, but make it looked like batch_size=1 # For compatibility with other methods within transformers lowerCamelCase = np.expand_dims(element[self._loader_batch_index] , 0 ) else: # This is typically a list, so no need to `unsqueeze`. lowerCamelCase = element[self._loader_batch_index] # Recreate the element by reusing the original class to make it look # batch_size=1 lowerCamelCase = self._loader_batch_data.__class__(_a ) self._loader_batch_index += 1 return result def _lowerCAmelCase ( self ): """simple docstring""" if self._loader_batch_index is not None and self._loader_batch_index < self.loader_batch_size: # We are currently unrolling a batch so we just need to return # the current item within a batch return self.loader_batch_item() # We're out of items within a batch lowerCamelCase = next(self.iterator ) lowerCamelCase = self.infer(_a , **self.params ) # We now have a batch of "inferred things". if self.loader_batch_size is not None: # Try to infer the size of the batch if isinstance(_a , torch.Tensor ): lowerCamelCase = processed else: lowerCamelCase = list(processed.keys() )[0] lowerCamelCase = processed[key] if isinstance(_a , _a ): lowerCamelCase = len(_a ) else: lowerCamelCase = first_tensor.shape[0] if 0 < observed_batch_size < self.loader_batch_size: # could be last batch so we can't unroll as many # elements. lowerCamelCase = observed_batch_size # Setting internal index to unwrap the batch lowerCamelCase = processed lowerCamelCase = 0 return self.loader_batch_item() else: # We're not unrolling batches return processed class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' def __init__( self , _a , _a , _a , _a=None ): """simple docstring""" super().__init__(_a , _a , _a ) def __iter__( self ): """simple docstring""" lowerCamelCase = iter(self.loader ) lowerCamelCase = None return self def _lowerCAmelCase ( self ): """simple docstring""" if self.subiterator is None: lowerCamelCase = self.infer(next(self.iterator ) , **self.params ) try: # Try to return next item lowerCamelCase = next(self.subiterator ) except StopIteration: # When a preprocess iterator ends, we can start lookig at the next item # ChunkIterator will keep feeding until ALL elements of iterator # all have created their subiterator and have been iterating against. # # Another way to look at it, is we're basically flattening lists of lists # into a single list, but with generators lowerCamelCase = self.infer(next(self.iterator ) , **self.params ) lowerCamelCase = next(self.subiterator ) return processed class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' def __iter__( self ): """simple docstring""" lowerCamelCase = iter(self.loader ) return self def _lowerCAmelCase ( self ): """simple docstring""" # Extremely similar to PipelineIterator in its unpacking mechanism # BUT, we have an extra required item which is the presence of `is_last` # That is because everything is flattened by `PipelineChunkIterator` we # need to keep track of how to regroup here in the original `process` # boundaries so that `process` and `postprocess` see the same data. # This iterator accumulates items (possibly while unbatching) until it # its a `is_last` and then just passes it on to the caller. lowerCamelCase = False lowerCamelCase = [] if self._loader_batch_index is not None and self._loader_batch_index < self.loader_batch_size: while self._loader_batch_index < self.loader_batch_size: lowerCamelCase = self.loader_batch_item() lowerCamelCase = item.pop("""is_last""" ) accumulator.append(_a ) if is_last: return accumulator while not is_last: lowerCamelCase = self.infer(next(self.iterator ) , **self.params ) if self.loader_batch_size is not None: if isinstance(_a , torch.Tensor ): lowerCamelCase = processed else: lowerCamelCase = list(processed.keys() )[0] lowerCamelCase = processed[key] if isinstance(_a , _a ): lowerCamelCase = len(_a ) else: lowerCamelCase = first_tensor.shape[0] if 0 < observed_batch_size < self.loader_batch_size: # could be last batch so we can't unroll as many # elements. lowerCamelCase = observed_batch_size lowerCamelCase = processed lowerCamelCase = 0 while self._loader_batch_index < self.loader_batch_size: lowerCamelCase = self.loader_batch_item() lowerCamelCase = item.pop("""is_last""" ) accumulator.append(_a ) if is_last: return accumulator else: lowerCamelCase = processed lowerCamelCase = item.pop("""is_last""" ) accumulator.append(_a ) return accumulator class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' def __init__( self , _a , _a ): """simple docstring""" lowerCamelCase = dataset lowerCamelCase = key def __len__( self ): """simple docstring""" return len(self.dataset ) def __getitem__( self , _a ): """simple docstring""" return self.dataset[i][self.key] class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' def __init__( self , _a , _a , _a ): """simple docstring""" lowerCamelCase = dataset lowerCamelCase = keya lowerCamelCase = keya def __len__( self ): """simple docstring""" return len(self.dataset ) def __getitem__( self , _a ): """simple docstring""" return {"text": self.dataset[i][self.keya], "text_pair": self.dataset[i][self.keya]}
291
1
"""simple docstring""" import unittest from transformers import ( MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING, TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING, TextaTextGenerationPipeline, pipeline, ) from transformers.testing_utils import is_pipeline_test, require_tf, require_torch from transformers.utils import is_torch_available from .test_pipelines_common import ANY if is_torch_available(): import torch @is_pipeline_test class __magic_name__ ( unittest.TestCase ): '''simple docstring''' __UpperCamelCase = MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING __UpperCamelCase = TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING def _lowerCAmelCase ( self , _a , _a , _a ): """simple docstring""" lowerCamelCase = TextaTextGenerationPipeline(model=_a , tokenizer=_a ) return generator, ["Something to write", "Something else"] def _lowerCAmelCase ( self , _a , _a ): """simple docstring""" lowerCamelCase = generator("""Something there""" ) self.assertEqual(_a , [{"""generated_text""": ANY(_a )}] ) # These are encoder decoder, they don't just append to incoming string self.assertFalse(outputs[0]["""generated_text"""].startswith("""Something there""" ) ) lowerCamelCase = generator(["""This is great !""", """Something else"""] , num_return_sequences=2 , do_sample=_a ) self.assertEqual( _a , [ [{"""generated_text""": ANY(_a )}, {"""generated_text""": ANY(_a )}], [{"""generated_text""": ANY(_a )}, {"""generated_text""": ANY(_a )}], ] , ) lowerCamelCase = generator( ["""This is great !""", """Something else"""] , num_return_sequences=2 , batch_size=2 , do_sample=_a ) self.assertEqual( _a , [ [{"""generated_text""": ANY(_a )}, {"""generated_text""": ANY(_a )}], [{"""generated_text""": ANY(_a )}, {"""generated_text""": ANY(_a )}], ] , ) with self.assertRaises(_a ): generator(4 ) @require_torch def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = pipeline("""text2text-generation""" , model="""patrickvonplaten/t5-tiny-random""" , framework="""pt""" ) # do_sample=False necessary for reproducibility lowerCamelCase = generator("""Something there""" , do_sample=_a ) self.assertEqual(_a , [{"""generated_text""": """"""}] ) lowerCamelCase = 3 lowerCamelCase = generator( """Something there""" , num_return_sequences=_a , num_beams=_a , ) lowerCamelCase = [ {"""generated_text""": """Beide Beide Beide Beide Beide Beide Beide Beide Beide"""}, {"""generated_text""": """Beide Beide Beide Beide Beide Beide Beide Beide"""}, {"""generated_text""": """"""}, ] self.assertEqual(_a , _a ) lowerCamelCase = generator("""This is a test""" , do_sample=_a , num_return_sequences=2 , return_tensors=_a ) self.assertEqual( _a , [ {"""generated_token_ids""": ANY(torch.Tensor )}, {"""generated_token_ids""": ANY(torch.Tensor )}, ] , ) lowerCamelCase = generator.model.config.eos_token_id lowerCamelCase = """<pad>""" lowerCamelCase = generator( ["""This is a test""", """This is a second test"""] , do_sample=_a , num_return_sequences=2 , batch_size=2 , return_tensors=_a , ) self.assertEqual( _a , [ [ {"""generated_token_ids""": ANY(torch.Tensor )}, {"""generated_token_ids""": ANY(torch.Tensor )}, ], [ {"""generated_token_ids""": ANY(torch.Tensor )}, {"""generated_token_ids""": ANY(torch.Tensor )}, ], ] , ) @require_tf def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = pipeline("""text2text-generation""" , model="""patrickvonplaten/t5-tiny-random""" , framework="""tf""" ) # do_sample=False necessary for reproducibility lowerCamelCase = generator("""Something there""" , do_sample=_a ) self.assertEqual(_a , [{"""generated_text""": """"""}] )
291
"""simple docstring""" def a__ ( snake_case__ ) -> bool: lowerCamelCase = (1 + 24 * n) ** 0.5 return ((1 + root) / 6) % 1 == 0 def a__ ( snake_case__ = 50_00 ) -> int: lowerCamelCase = [(i * (3 * i - 1)) // 2 for i in range(1 , snake_case__ )] for i, pentagonal_i in enumerate(snake_case__ ): for j in range(snake_case__ , len(snake_case__ ) ): lowerCamelCase = pentagonal_nums[j] lowerCamelCase = pentagonal_i + pentagonal_j lowerCamelCase = pentagonal_j - pentagonal_i if is_pentagonal(snake_case__ ) and is_pentagonal(snake_case__ ): return b return -1 if __name__ == "__main__": print(F"""{solution() = }""")
291
1
"""simple docstring""" from typing import List, Optional from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase : Optional[int] = logging.get_logger(__name__) lowerCAmelCase : int = { """huggingface/autoformer-tourism-monthly""": """https://huggingface.co/huggingface/autoformer-tourism-monthly/resolve/main/config.json""", } class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' __UpperCamelCase = "autoformer" __UpperCamelCase = { "hidden_size": "d_model", "num_attention_heads": "encoder_attention_heads", "num_hidden_layers": "encoder_layers", } def __init__( self , _a = None , _a = None , _a = "student_t" , _a = "nll" , _a = 1 , _a = [1, 2, 3, 4, 5, 6, 7] , _a = True , _a = 0 , _a = 0 , _a = 0 , _a = 0 , _a = None , _a = None , _a = 64 , _a = 2 , _a = 2 , _a = 2 , _a = 2 , _a = 32 , _a = 32 , _a = "gelu" , _a = 0.1 , _a = 0.1 , _a = 0.1 , _a = 0.1 , _a = 0.1 , _a = 100 , _a = 0.02 , _a = True , _a=True , _a = 10 , _a = 25 , _a = 3 , **_a , ): """simple docstring""" # time series specific configuration lowerCamelCase = prediction_length lowerCamelCase = context_length if context_length is not None else prediction_length lowerCamelCase = distribution_output lowerCamelCase = loss lowerCamelCase = input_size lowerCamelCase = num_time_features lowerCamelCase = lags_sequence lowerCamelCase = scaling lowerCamelCase = num_dynamic_real_features lowerCamelCase = num_static_real_features lowerCamelCase = num_static_categorical_features if cardinality is not None and num_static_categorical_features > 0: if len(_a ) != num_static_categorical_features: raise ValueError( """The cardinality should be a list of the same length as `num_static_categorical_features`""" ) lowerCamelCase = cardinality else: lowerCamelCase = [0] if embedding_dimension is not None and num_static_categorical_features > 0: if len(_a ) != num_static_categorical_features: raise ValueError( """The embedding dimension should be a list of the same length as `num_static_categorical_features`""" ) lowerCamelCase = embedding_dimension else: lowerCamelCase = [min(50 , (cat + 1) // 2 ) for cat in self.cardinality] lowerCamelCase = num_parallel_samples # Transformer architecture configuration lowerCamelCase = input_size * len(self.lags_sequence ) + self._number_of_features lowerCamelCase = d_model lowerCamelCase = encoder_attention_heads lowerCamelCase = decoder_attention_heads lowerCamelCase = encoder_ffn_dim lowerCamelCase = decoder_ffn_dim lowerCamelCase = encoder_layers lowerCamelCase = decoder_layers lowerCamelCase = dropout lowerCamelCase = attention_dropout lowerCamelCase = activation_dropout lowerCamelCase = encoder_layerdrop lowerCamelCase = decoder_layerdrop lowerCamelCase = activation_function lowerCamelCase = init_std lowerCamelCase = use_cache # Autoformer lowerCamelCase = label_length lowerCamelCase = moving_average lowerCamelCase = autocorrelation_factor super().__init__(is_encoder_decoder=_a , **_a ) @property def _lowerCAmelCase ( self ): """simple docstring""" return ( sum(self.embedding_dimension ) + self.num_dynamic_real_features + self.num_time_features + self.num_static_real_features + self.input_size * 2 # the log1p(abs(loc)) and log(scale) features )
291
"""simple docstring""" from pickle import UnpicklingError import jax import jax.numpy as jnp import numpy as np from flax.serialization import from_bytes from flax.traverse_util import flatten_dict from ..utils import logging lowerCAmelCase : Tuple = logging.get_logger(__name__) def a__ ( snake_case__ , snake_case__ ) -> Tuple: try: with open(snake_case__ , """rb""" ) as flax_state_f: lowerCamelCase = from_bytes(snake_case__ , flax_state_f.read() ) except UnpicklingError as e: try: with open(snake_case__ ) as f: if f.read().startswith("""version""" ): raise OSError( """You seem to have cloned a repository without having git-lfs installed. Please""" """ install git-lfs and run `git lfs install` followed by `git lfs pull` in the""" """ folder you cloned.""" ) else: raise ValueError from e except (UnicodeDecodeError, ValueError): raise EnvironmentError(F'Unable to convert {model_file} to Flax deserializable object. ' ) return load_flax_weights_in_pytorch_model(snake_case__ , snake_case__ ) def a__ ( snake_case__ , snake_case__ ) -> Tuple: try: import torch # noqa: F401 except ImportError: logger.error( """Loading Flax weights in PyTorch requires both PyTorch and Flax to be installed. Please see""" """ https://pytorch.org/ and https://flax.readthedocs.io/en/latest/installation.html for installation""" """ instructions.""" ) raise # check if we have bf16 weights lowerCamelCase = flatten_dict(jax.tree_util.tree_map(lambda snake_case__ : x.dtype == jnp.bfloataa , snake_case__ ) ).values() if any(snake_case__ ): # convert all weights to fp32 if they are bf16 since torch.from_numpy can-not handle bf16 # and bf16 is not fully supported in PT yet. logger.warning( """Found ``bfloat16`` weights in Flax model. Casting all ``bfloat16`` weights to ``float32`` """ """before loading those in PyTorch model.""" ) lowerCamelCase = jax.tree_util.tree_map( lambda snake_case__ : params.astype(np.floataa ) if params.dtype == jnp.bfloataa else params , snake_case__ ) lowerCamelCase = """""" lowerCamelCase = flatten_dict(snake_case__ , sep=""".""" ) lowerCamelCase = pt_model.state_dict() # keep track of unexpected & missing keys lowerCamelCase = [] lowerCamelCase = set(pt_model_dict.keys() ) for flax_key_tuple, flax_tensor in flax_state_dict.items(): lowerCamelCase = flax_key_tuple.split(""".""" ) if flax_key_tuple_array[-1] == "kernel" and flax_tensor.ndim == 4: lowerCamelCase = flax_key_tuple_array[:-1] + ["""weight"""] lowerCamelCase = jnp.transpose(snake_case__ , (3, 2, 0, 1) ) elif flax_key_tuple_array[-1] == "kernel": lowerCamelCase = flax_key_tuple_array[:-1] + ["""weight"""] lowerCamelCase = flax_tensor.T elif flax_key_tuple_array[-1] == "scale": lowerCamelCase = flax_key_tuple_array[:-1] + ["""weight"""] if "time_embedding" not in flax_key_tuple_array: for i, flax_key_tuple_string in enumerate(snake_case__ ): lowerCamelCase = ( flax_key_tuple_string.replace("""_0""" , """.0""" ) .replace("""_1""" , """.1""" ) .replace("""_2""" , """.2""" ) .replace("""_3""" , """.3""" ) .replace("""_4""" , """.4""" ) .replace("""_5""" , """.5""" ) .replace("""_6""" , """.6""" ) .replace("""_7""" , """.7""" ) .replace("""_8""" , """.8""" ) .replace("""_9""" , """.9""" ) ) lowerCamelCase = """.""".join(snake_case__ ) if flax_key in pt_model_dict: if flax_tensor.shape != pt_model_dict[flax_key].shape: raise ValueError( F'Flax checkpoint seems to be incorrect. Weight {flax_key_tuple} was expected ' F'to be of shape {pt_model_dict[flax_key].shape}, but is {flax_tensor.shape}.' ) else: # add weight to pytorch dict lowerCamelCase = np.asarray(snake_case__ ) if not isinstance(snake_case__ , np.ndarray ) else flax_tensor lowerCamelCase = torch.from_numpy(snake_case__ ) # remove from missing keys missing_keys.remove(snake_case__ ) else: # weight is not expected by PyTorch model unexpected_keys.append(snake_case__ ) pt_model.load_state_dict(snake_case__ ) # re-transform missing_keys to list lowerCamelCase = list(snake_case__ ) if len(snake_case__ ) > 0: logger.warning( """Some weights of the Flax model were not used when initializing the PyTorch model""" F' {pt_model.__class__.__name__}: {unexpected_keys}\n- This IS expected if you are initializing' F' {pt_model.__class__.__name__} from a Flax model trained on another task or with another architecture' """ (e.g. initializing a BertForSequenceClassification model from a FlaxBertForPreTraining model).\n- This""" F' IS NOT expected if you are initializing {pt_model.__class__.__name__} from a Flax model that you expect' """ to be exactly identical (e.g. initializing a BertForSequenceClassification model from a""" """ FlaxBertForSequenceClassification model).""" ) if len(snake_case__ ) > 0: logger.warning( F'Some weights of {pt_model.__class__.__name__} were not initialized from the Flax model and are newly' F' initialized: {missing_keys}\nYou should probably TRAIN this model on a down-stream task to be able to' """ use it for predictions and inference.""" ) return pt_model
291
1
"""simple docstring""" from __future__ import annotations def a__ ( snake_case__ ) -> list[int]: lowerCamelCase = 2 lowerCamelCase = [] while i * i <= n: if n % i: i += 1 else: n //= i factors.append(snake_case__ ) if n > 1: factors.append(snake_case__ ) return factors if __name__ == "__main__": import doctest doctest.testmod()
291
"""simple docstring""" import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_xlnet import XLNetTokenizer else: lowerCAmelCase : int = None lowerCAmelCase : Tuple = logging.get_logger(__name__) lowerCAmelCase : Tuple = {"""vocab_file""": """spiece.model""", """tokenizer_file""": """tokenizer.json"""} lowerCAmelCase : Union[str, Any] = { """vocab_file""": { """xlnet-base-cased""": """https://huggingface.co/xlnet-base-cased/resolve/main/spiece.model""", """xlnet-large-cased""": """https://huggingface.co/xlnet-large-cased/resolve/main/spiece.model""", }, """tokenizer_file""": { """xlnet-base-cased""": """https://huggingface.co/xlnet-base-cased/resolve/main/tokenizer.json""", """xlnet-large-cased""": """https://huggingface.co/xlnet-large-cased/resolve/main/tokenizer.json""", }, } lowerCAmelCase : Optional[int] = { """xlnet-base-cased""": None, """xlnet-large-cased""": None, } lowerCAmelCase : Union[str, Any] = """▁""" # Segments (not really needed) lowerCAmelCase : str = 0 lowerCAmelCase : Optional[int] = 1 lowerCAmelCase : Tuple = 2 lowerCAmelCase : Optional[Any] = 3 lowerCAmelCase : List[Any] = 4 class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' __UpperCamelCase = VOCAB_FILES_NAMES __UpperCamelCase = PRETRAINED_VOCAB_FILES_MAP __UpperCamelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCamelCase = "left" __UpperCamelCase = XLNetTokenizer def __init__( self , _a=None , _a=None , _a=False , _a=True , _a=False , _a="<s>" , _a="</s>" , _a="<unk>" , _a="<sep>" , _a="<pad>" , _a="<cls>" , _a="<mask>" , _a=["<eop>", "<eod>"] , **_a , ): """simple docstring""" # Mask token behave like a normal word, i.e. include the space before it lowerCamelCase = AddedToken(_a , lstrip=_a , rstrip=_a ) if isinstance(_a , _a ) else mask_token super().__init__( vocab_file=_a , tokenizer_file=_a , do_lower_case=_a , remove_space=_a , keep_accents=_a , bos_token=_a , eos_token=_a , unk_token=_a , sep_token=_a , pad_token=_a , cls_token=_a , mask_token=_a , additional_special_tokens=_a , **_a , ) lowerCamelCase = 3 lowerCamelCase = do_lower_case lowerCamelCase = remove_space lowerCamelCase = keep_accents lowerCamelCase = vocab_file lowerCamelCase = False if not self.vocab_file else True def _lowerCAmelCase ( self , _a , _a = None ): """simple docstring""" lowerCamelCase = [self.sep_token_id] lowerCamelCase = [self.cls_token_id] if token_ids_a is None: return token_ids_a + sep + cls return token_ids_a + sep + token_ids_a + sep + cls def _lowerCAmelCase ( self , _a , _a = None ): """simple docstring""" lowerCamelCase = [self.sep_token_id] lowerCamelCase = [2] if token_ids_a is None: return len(token_ids_a + sep ) * [0] + cls_segment_id return len(token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] + cls_segment_id def _lowerCAmelCase ( self , _a , _a = 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(_a ): logger.error(f'Vocabulary path ({save_directory}) should be a directory' ) return lowerCamelCase = os.path.join( _a , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_a ): copyfile(self.vocab_file , _a ) return (out_vocab_file,)
291
1
"""simple docstring""" def a__ ( ) -> Union[str, Any]: lowerCamelCase = [] lowerCamelCase = 1 while len(snake_case__ ) < 1E6: constant.append(str(snake_case__ ) ) i += 1 lowerCamelCase = """""".join(snake_case__ ) return ( int(constant[0] ) * int(constant[9] ) * int(constant[99] ) * int(constant[9_99] ) * int(constant[99_99] ) * int(constant[9_99_99] ) * int(constant[99_99_99] ) ) if __name__ == "__main__": print(solution())
291
"""simple docstring""" import unittest from transformers import ( MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING, TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING, TextaTextGenerationPipeline, pipeline, ) from transformers.testing_utils import is_pipeline_test, require_tf, require_torch from transformers.utils import is_torch_available from .test_pipelines_common import ANY if is_torch_available(): import torch @is_pipeline_test class __magic_name__ ( unittest.TestCase ): '''simple docstring''' __UpperCamelCase = MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING __UpperCamelCase = TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING def _lowerCAmelCase ( self , _a , _a , _a ): """simple docstring""" lowerCamelCase = TextaTextGenerationPipeline(model=_a , tokenizer=_a ) return generator, ["Something to write", "Something else"] def _lowerCAmelCase ( self , _a , _a ): """simple docstring""" lowerCamelCase = generator("""Something there""" ) self.assertEqual(_a , [{"""generated_text""": ANY(_a )}] ) # These are encoder decoder, they don't just append to incoming string self.assertFalse(outputs[0]["""generated_text"""].startswith("""Something there""" ) ) lowerCamelCase = generator(["""This is great !""", """Something else"""] , num_return_sequences=2 , do_sample=_a ) self.assertEqual( _a , [ [{"""generated_text""": ANY(_a )}, {"""generated_text""": ANY(_a )}], [{"""generated_text""": ANY(_a )}, {"""generated_text""": ANY(_a )}], ] , ) lowerCamelCase = generator( ["""This is great !""", """Something else"""] , num_return_sequences=2 , batch_size=2 , do_sample=_a ) self.assertEqual( _a , [ [{"""generated_text""": ANY(_a )}, {"""generated_text""": ANY(_a )}], [{"""generated_text""": ANY(_a )}, {"""generated_text""": ANY(_a )}], ] , ) with self.assertRaises(_a ): generator(4 ) @require_torch def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = pipeline("""text2text-generation""" , model="""patrickvonplaten/t5-tiny-random""" , framework="""pt""" ) # do_sample=False necessary for reproducibility lowerCamelCase = generator("""Something there""" , do_sample=_a ) self.assertEqual(_a , [{"""generated_text""": """"""}] ) lowerCamelCase = 3 lowerCamelCase = generator( """Something there""" , num_return_sequences=_a , num_beams=_a , ) lowerCamelCase = [ {"""generated_text""": """Beide Beide Beide Beide Beide Beide Beide Beide Beide"""}, {"""generated_text""": """Beide Beide Beide Beide Beide Beide Beide Beide"""}, {"""generated_text""": """"""}, ] self.assertEqual(_a , _a ) lowerCamelCase = generator("""This is a test""" , do_sample=_a , num_return_sequences=2 , return_tensors=_a ) self.assertEqual( _a , [ {"""generated_token_ids""": ANY(torch.Tensor )}, {"""generated_token_ids""": ANY(torch.Tensor )}, ] , ) lowerCamelCase = generator.model.config.eos_token_id lowerCamelCase = """<pad>""" lowerCamelCase = generator( ["""This is a test""", """This is a second test"""] , do_sample=_a , num_return_sequences=2 , batch_size=2 , return_tensors=_a , ) self.assertEqual( _a , [ [ {"""generated_token_ids""": ANY(torch.Tensor )}, {"""generated_token_ids""": ANY(torch.Tensor )}, ], [ {"""generated_token_ids""": ANY(torch.Tensor )}, {"""generated_token_ids""": ANY(torch.Tensor )}, ], ] , ) @require_tf def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = pipeline("""text2text-generation""" , model="""patrickvonplaten/t5-tiny-random""" , framework="""tf""" ) # do_sample=False necessary for reproducibility lowerCamelCase = generator("""Something there""" , do_sample=_a ) self.assertEqual(_a , [{"""generated_text""": """"""}] )
291
1
"""simple docstring""" import json import os from typing import Optional import numpy as np from ...feature_extraction_utils import BatchFeature from ...processing_utils import ProcessorMixin from ...utils import logging from ...utils.hub import get_file_from_repo from ..auto import AutoTokenizer lowerCAmelCase : Union[str, Any] = logging.get_logger(__name__) class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' __UpperCamelCase = "AutoTokenizer" __UpperCamelCase = ["tokenizer"] __UpperCamelCase = { "semantic_prompt": 1, "coarse_prompt": 2, "fine_prompt": 2, } def __init__( self , _a , _a=None ): """simple docstring""" super().__init__(_a ) lowerCamelCase = speaker_embeddings @classmethod def _lowerCAmelCase ( cls , _a , _a="speaker_embeddings_path.json" , **_a ): """simple docstring""" if speaker_embeddings_dict_path is not None: lowerCamelCase = get_file_from_repo( _a , _a , subfolder=kwargs.pop("""subfolder""" , _a ) , cache_dir=kwargs.pop("""cache_dir""" , _a ) , force_download=kwargs.pop("""force_download""" , _a ) , proxies=kwargs.pop("""proxies""" , _a ) , resume_download=kwargs.pop("""resume_download""" , _a ) , local_files_only=kwargs.pop("""local_files_only""" , _a ) , use_auth_token=kwargs.pop("""use_auth_token""" , _a ) , revision=kwargs.pop("""revision""" , _a ) , ) if speaker_embeddings_path is None: logger.warning( f'`{os.path.join(_a , _a )}` does not exists\n , no preloaded speaker embeddings will be used - Make sure to provide a correct path to the json\n dictionnary if wanted, otherwise set `speaker_embeddings_dict_path=None`.' ) lowerCamelCase = None else: with open(_a ) as speaker_embeddings_json: lowerCamelCase = json.load(_a ) else: lowerCamelCase = None lowerCamelCase = AutoTokenizer.from_pretrained(_a , **_a ) return cls(tokenizer=_a , speaker_embeddings=_a ) def _lowerCAmelCase ( self , _a , _a="speaker_embeddings_path.json" , _a="speaker_embeddings" , _a = False , **_a , ): """simple docstring""" if self.speaker_embeddings is not None: os.makedirs(os.path.join(_a , _a , """v2""" ) , exist_ok=_a ) lowerCamelCase = {} lowerCamelCase = save_directory for prompt_key in self.speaker_embeddings: if prompt_key != "repo_or_path": lowerCamelCase = self._load_voice_preset(_a ) lowerCamelCase = {} for key in self.speaker_embeddings[prompt_key]: np.save( os.path.join( embeddings_dict["""repo_or_path"""] , _a , f'{prompt_key}_{key}' ) , voice_preset[key] , allow_pickle=_a , ) lowerCamelCase = os.path.join(_a , f'{prompt_key}_{key}.npy' ) lowerCamelCase = tmp_dict with open(os.path.join(_a , _a ) , """w""" ) as fp: json.dump(_a , _a ) super().save_pretrained(_a , _a , **_a ) def _lowerCAmelCase ( self , _a = None , **_a ): """simple docstring""" lowerCamelCase = self.speaker_embeddings[voice_preset] lowerCamelCase = {} for key in ["semantic_prompt", "coarse_prompt", "fine_prompt"]: if key not in voice_preset_paths: raise ValueError( f'Voice preset unrecognized, missing {key} as a key in self.speaker_embeddings[{voice_preset}].' ) lowerCamelCase = get_file_from_repo( self.speaker_embeddings.get("""repo_or_path""" , """/""" ) , voice_preset_paths[key] , subfolder=kwargs.pop("""subfolder""" , _a ) , cache_dir=kwargs.pop("""cache_dir""" , _a ) , force_download=kwargs.pop("""force_download""" , _a ) , proxies=kwargs.pop("""proxies""" , _a ) , resume_download=kwargs.pop("""resume_download""" , _a ) , local_files_only=kwargs.pop("""local_files_only""" , _a ) , use_auth_token=kwargs.pop("""use_auth_token""" , _a ) , revision=kwargs.pop("""revision""" , _a ) , ) if path is None: raise ValueError( f'`{os.path.join(self.speaker_embeddings.get("repo_or_path" , "/" ) , voice_preset_paths[key] )}` does not exists\n , no preloaded voice preset will be used - Make sure to provide correct paths to the {voice_preset}\n embeddings.' ) lowerCamelCase = np.load(_a ) return voice_preset_dict def _lowerCAmelCase ( self , _a = None ): """simple docstring""" for key in ["semantic_prompt", "coarse_prompt", "fine_prompt"]: if key not in voice_preset: raise ValueError(f'Voice preset unrecognized, missing {key} as a key.' ) if not isinstance(voice_preset[key] , np.ndarray ): raise ValueError(f'{key} voice preset must be a {str(self.preset_shape[key] )}D ndarray.' ) if len(voice_preset[key].shape ) != self.preset_shape[key]: raise ValueError(f'{key} voice preset must be a {str(self.preset_shape[key] )}D ndarray.' ) def __call__( self , _a=None , _a=None , _a="pt" , _a=256 , _a=False , _a=True , _a=False , **_a , ): """simple docstring""" if voice_preset is not None and not isinstance(_a , _a ): if ( isinstance(_a , _a ) and self.speaker_embeddings is not None and voice_preset in self.speaker_embeddings ): lowerCamelCase = self._load_voice_preset(_a ) else: if isinstance(_a , _a ) and not voice_preset.endswith(""".npz""" ): lowerCamelCase = voice_preset + """.npz""" lowerCamelCase = np.load(_a ) if voice_preset is not None: self._validate_voice_preset_dict(_a , **_a ) lowerCamelCase = BatchFeature(data=_a , tensor_type=_a ) lowerCamelCase = self.tokenizer( _a , return_tensors=_a , padding="""max_length""" , max_length=_a , return_attention_mask=_a , return_token_type_ids=_a , add_special_tokens=_a , **_a , ) if voice_preset is not None: lowerCamelCase = voice_preset return encoded_text
291
"""simple docstring""" def a__ ( snake_case__ , snake_case__ = False ) -> str: if not isinstance(snake_case__ , snake_case__ ): lowerCamelCase = F'Expected string as input, found {type(snake_case__ )}' raise ValueError(snake_case__ ) if not isinstance(snake_case__ , snake_case__ ): lowerCamelCase = F'Expected boolean as use_pascal parameter, found {type(snake_case__ )}' raise ValueError(snake_case__ ) lowerCamelCase = input_str.split("""_""" ) lowerCamelCase = 0 if use_pascal else 1 lowerCamelCase = words[start_index:] lowerCamelCase = [word[0].upper() + word[1:] for word in words_to_capitalize] lowerCamelCase = """""" if use_pascal else words[0] return "".join([initial_word, *capitalized_words] ) if __name__ == "__main__": from doctest import testmod testmod()
291
1
"""simple docstring""" import unittest import numpy as np from transformers.testing_utils import is_flaky, require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import DonutImageProcessor class __magic_name__ ( unittest.TestCase ): '''simple docstring''' def __init__( self , _a , _a=7 , _a=3 , _a=18 , _a=30 , _a=400 , _a=True , _a=None , _a=True , _a=False , _a=True , _a=True , _a=[0.5, 0.5, 0.5] , _a=[0.5, 0.5, 0.5] , ): """simple docstring""" lowerCamelCase = parent lowerCamelCase = batch_size lowerCamelCase = num_channels lowerCamelCase = image_size lowerCamelCase = min_resolution lowerCamelCase = max_resolution lowerCamelCase = do_resize lowerCamelCase = size if size is not None else {"""height""": 18, """width""": 20} lowerCamelCase = do_thumbnail lowerCamelCase = do_align_axis lowerCamelCase = do_pad lowerCamelCase = do_normalize lowerCamelCase = image_mean lowerCamelCase = image_std def _lowerCAmelCase ( self ): """simple docstring""" return { "do_resize": self.do_resize, "size": self.size, "do_thumbnail": self.do_thumbnail, "do_align_long_axis": self.do_align_axis, "do_pad": self.do_pad, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, } @require_torch @require_vision class __magic_name__ ( UpperCAmelCase__ , unittest.TestCase ): '''simple docstring''' __UpperCamelCase = DonutImageProcessor if is_vision_available() else None def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = DonutImageProcessingTester(self ) @property def _lowerCAmelCase ( self ): """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_a , """do_resize""" ) ) self.assertTrue(hasattr(_a , """size""" ) ) self.assertTrue(hasattr(_a , """do_thumbnail""" ) ) self.assertTrue(hasattr(_a , """do_align_long_axis""" ) ) self.assertTrue(hasattr(_a , """do_pad""" ) ) self.assertTrue(hasattr(_a , """do_normalize""" ) ) self.assertTrue(hasattr(_a , """image_mean""" ) ) self.assertTrue(hasattr(_a , """image_std""" ) ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"""height""": 18, """width""": 20} ) lowerCamelCase = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {"""height""": 42, """width""": 42} ) # Previous config had dimensions in (width, height) order lowerCamelCase = self.image_processing_class.from_dict(self.image_processor_dict , size=(42, 84) ) self.assertEqual(image_processor.size , {"""height""": 84, """width""": 42} ) def _lowerCAmelCase ( self ): """simple docstring""" pass @is_flaky() def _lowerCAmelCase ( self ): """simple docstring""" # Initialize image_processing lowerCamelCase = self.image_processing_class(**self.image_processor_dict ) # create random PIL images lowerCamelCase = prepare_image_inputs(self.image_processor_tester , equal_resolution=_a ) for image in image_inputs: self.assertIsInstance(_a , Image.Image ) # Test not batched input lowerCamelCase = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) , ) # Test batched lowerCamelCase = image_processing(_a , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) , ) @is_flaky() def _lowerCAmelCase ( self ): """simple docstring""" # Initialize image_processing lowerCamelCase = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors lowerCamelCase = prepare_image_inputs(self.image_processor_tester , equal_resolution=_a , numpify=_a ) for image in image_inputs: self.assertIsInstance(_a , np.ndarray ) # Test not batched input lowerCamelCase = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) , ) # Test batched lowerCamelCase = image_processing(_a , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) , ) @is_flaky() def _lowerCAmelCase ( self ): """simple docstring""" # Initialize image_processing lowerCamelCase = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors lowerCamelCase = prepare_image_inputs(self.image_processor_tester , equal_resolution=_a , torchify=_a ) for image in image_inputs: self.assertIsInstance(_a , torch.Tensor ) # Test not batched input lowerCamelCase = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) , ) # Test batched lowerCamelCase = image_processing(_a , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) , )
291
"""simple docstring""" from typing import Dict, List, Optional, Tuple, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_torch_available, is_torch_tensor, logging if is_torch_available(): import torch lowerCAmelCase : int = logging.get_logger(__name__) class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' __UpperCamelCase = ["pixel_values"] def __init__( self , _a = True , _a = None , _a = PILImageResampling.BILINEAR , _a = True , _a = None , _a = True , _a = 1 / 255 , _a = True , _a = None , _a = None , **_a , ): """simple docstring""" super().__init__(**_a ) lowerCamelCase = size if size is not None else {"""shortest_edge""": 256} lowerCamelCase = get_size_dict(_a , default_to_square=_a ) lowerCamelCase = crop_size if crop_size is not None else {"""height""": 224, """width""": 224} lowerCamelCase = get_size_dict(_a , param_name="""crop_size""" ) lowerCamelCase = do_resize lowerCamelCase = size lowerCamelCase = resample lowerCamelCase = do_center_crop lowerCamelCase = crop_size lowerCamelCase = do_rescale lowerCamelCase = rescale_factor lowerCamelCase = do_normalize lowerCamelCase = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN lowerCamelCase = image_std if image_std is not None else IMAGENET_STANDARD_STD def _lowerCAmelCase ( self , _a , _a , _a = PILImageResampling.BICUBIC , _a = None , **_a , ): """simple docstring""" lowerCamelCase = get_size_dict(_a , default_to_square=_a ) if "shortest_edge" not in size: raise ValueError(f'The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}' ) lowerCamelCase = get_resize_output_image_size(_a , size=size["""shortest_edge"""] , default_to_square=_a ) return resize(_a , size=_a , resample=_a , data_format=_a , **_a ) def _lowerCAmelCase ( self , _a , _a , _a = None , **_a , ): """simple docstring""" lowerCamelCase = get_size_dict(_a ) if "height" not in size or "width" not in size: raise ValueError(f'The `size` parameter must contain the keys `height` and `width`. Got {size.keys()}' ) return center_crop(_a , size=(size["""height"""], size["""width"""]) , data_format=_a , **_a ) def _lowerCAmelCase ( self , _a , _a , _a = None , **_a ): """simple docstring""" return rescale(_a , scale=_a , data_format=_a , **_a ) def _lowerCAmelCase ( self , _a , _a , _a , _a = None , **_a , ): """simple docstring""" return normalize(_a , mean=_a , std=_a , data_format=_a , **_a ) def _lowerCAmelCase ( self , _a , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = ChannelDimension.FIRST , **_a , ): """simple docstring""" lowerCamelCase = do_resize if do_resize is not None else self.do_resize lowerCamelCase = size if size is not None else self.size lowerCamelCase = get_size_dict(_a , default_to_square=_a ) lowerCamelCase = resample if resample is not None else self.resample lowerCamelCase = do_center_crop if do_center_crop is not None else self.do_center_crop lowerCamelCase = crop_size if crop_size is not None else self.crop_size lowerCamelCase = get_size_dict(_a , param_name="""crop_size""" ) lowerCamelCase = do_rescale if do_rescale is not None else self.do_rescale lowerCamelCase = rescale_factor if rescale_factor is not None else self.rescale_factor lowerCamelCase = do_normalize if do_normalize is not None else self.do_normalize lowerCamelCase = image_mean if image_mean is not None else self.image_mean lowerCamelCase = image_std if image_std is not None else self.image_std lowerCamelCase = make_list_of_images(_a ) if not valid_images(_a ): raise ValueError( """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """ """torch.Tensor, tf.Tensor or jax.ndarray.""" ) if do_resize and size is None: raise ValueError("""Size must be specified if do_resize is True.""" ) if do_center_crop and crop_size is None: raise ValueError("""Crop size must be specified if do_center_crop is True.""" ) if do_rescale and rescale_factor is None: raise ValueError("""Rescale factor must be specified if do_rescale is True.""" ) if do_normalize and (image_mean is None or image_std is None): raise ValueError("""Image mean and std must be specified if do_normalize is True.""" ) # All transformations expect numpy arrays. lowerCamelCase = [to_numpy_array(_a ) for image in images] if do_resize: lowerCamelCase = [self.resize(image=_a , size=_a , resample=_a ) for image in images] if do_center_crop: lowerCamelCase = [self.center_crop(image=_a , size=_a ) for image in images] if do_rescale: lowerCamelCase = [self.rescale(image=_a , scale=_a ) for image in images] if do_normalize: lowerCamelCase = [self.normalize(image=_a , mean=_a , std=_a ) for image in images] lowerCamelCase = [to_channel_dimension_format(_a , _a ) for image in images] lowerCamelCase = {"""pixel_values""": images} return BatchFeature(data=_a , tensor_type=_a ) def _lowerCAmelCase ( self , _a , _a = None ): """simple docstring""" lowerCamelCase = outputs.logits # Resize logits and compute semantic segmentation maps if target_sizes is not None: if len(_a ) != len(_a ): raise ValueError( """Make sure that you pass in as many target sizes as the batch dimension of the logits""" ) if is_torch_tensor(_a ): lowerCamelCase = target_sizes.numpy() lowerCamelCase = [] for idx in range(len(_a ) ): lowerCamelCase = torch.nn.functional.interpolate( logits[idx].unsqueeze(dim=0 ) , size=target_sizes[idx] , mode="""bilinear""" , align_corners=_a ) lowerCamelCase = resized_logits[0].argmax(dim=0 ) semantic_segmentation.append(_a ) else: lowerCamelCase = logits.argmax(dim=1 ) lowerCamelCase = [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0] )] return semantic_segmentation
291
1
"""simple docstring""" import importlib import sys from argparse import REMAINDER, ArgumentParser from pathlib import Path import torch_xla.distributed.xla_multiprocessing as xmp def a__ ( ) -> Union[str, Any]: lowerCamelCase = ArgumentParser( description=( """PyTorch TPU distributed training launch """ """helper utility that will spawn up """ """multiple distributed processes""" ) ) # Optional arguments for the launch helper parser.add_argument("""--num_cores""" , type=snake_case__ , default=1 , help="""Number of TPU cores to use (1 or 8).""" ) # positional parser.add_argument( """training_script""" , type=snake_case__ , help=( """The full path to the single TPU training """ """program/script to be launched in parallel, """ """followed by all the arguments for the """ """training script""" ) , ) # rest from the training program parser.add_argument("""training_script_args""" , nargs=snake_case__ ) return parser.parse_args() def a__ ( ) -> List[str]: lowerCamelCase = parse_args() # Import training_script as a module. lowerCamelCase = Path(args.training_script ) sys.path.append(str(script_fpath.parent.resolve() ) ) lowerCamelCase = script_fpath.stem lowerCamelCase = importlib.import_module(snake_case__ ) # Patch sys.argv lowerCamelCase = [args.training_script] + args.training_script_args + ["""--tpu_num_cores""", str(args.num_cores )] xmp.spawn(mod._mp_fn , args=() , nprocs=args.num_cores ) if __name__ == "__main__": main()
291
"""simple docstring""" import operator as op lowerCAmelCase : Dict = """scaler.pt""" lowerCAmelCase : Tuple = """pytorch_model""" lowerCAmelCase : Union[str, Any] = """random_states""" lowerCAmelCase : Union[str, Any] = """optimizer""" lowerCAmelCase : Dict = """scheduler""" lowerCAmelCase : int = """pytorch_model.bin""" lowerCAmelCase : str = """pytorch_model.bin.index.json""" lowerCAmelCase : Union[str, Any] = """model.safetensors""" lowerCAmelCase : List[Any] = """model.safetensors.index.json""" lowerCAmelCase : List[Any] = """1.10.2""" lowerCAmelCase : Any = """py38""" lowerCAmelCase : Optional[int] = """4.17.0""" lowerCAmelCase : str = ["""ml.p3.16xlarge""", """ml.p3dn.24xlarge""", """ml.p4dn.24xlarge"""] lowerCAmelCase : Tuple = ["""FULL_SHARD""", """SHARD_GRAD_OP""", """NO_SHARD""", """HYBRID_SHARD""", """HYBRID_SHARD_ZERO2"""] lowerCAmelCase : List[Any] = ["""TRANSFORMER_BASED_WRAP""", """SIZE_BASED_WRAP""", """NO_WRAP"""] lowerCAmelCase : List[str] = ["""BACKWARD_PRE""", """BACKWARD_POST""", """NO_PREFETCH"""] lowerCAmelCase : List[str] = ["""FULL_STATE_DICT""", """LOCAL_STATE_DICT""", """SHARDED_STATE_DICT"""] lowerCAmelCase : Any = """2.0.1""" lowerCAmelCase : List[Any] = ["""pdsh""", """standard""", """openmpi""", """mvapich"""] lowerCAmelCase : Union[str, Any] = ["""default""", """reduce-overhead""", """max-autotune"""] lowerCAmelCase : Optional[int] = {""">""": op.gt, """>=""": op.ge, """==""": op.eq, """!=""": op.ne, """<=""": op.le, """<""": op.lt} # These are the args for `torch.distributed.launch` for pytorch < 1.9 lowerCAmelCase : Union[str, Any] = [ """nnodes""", """nproc_per_node""", """rdzv_backend""", """rdzv_endpoint""", """rdzv_id""", """rdzv_conf""", """standalone""", """max_restarts""", """monitor_interval""", """start_method""", """role""", """module""", """m""", """no_python""", """run_path""", """log_dir""", """r""", """redirects""", """t""", """tee""", """node_rank""", """master_addr""", """master_port""", ] lowerCAmelCase : List[str] = ["""DEEPSPEED""", """MULTI_GPU""", """FSDP""", """MEGATRON_LM"""] lowerCAmelCase : Optional[Any] = ["""DEEPSPEED""", """MULTI_XPU""", """FSDP"""]
291
1
"""simple docstring""" import cmath import math def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ ) -> complex: lowerCamelCase = math.radians(snake_case__ ) lowerCamelCase = math.radians(snake_case__ ) # Convert voltage and current to rectangular form lowerCamelCase = cmath.rect(snake_case__ , snake_case__ ) lowerCamelCase = cmath.rect(snake_case__ , snake_case__ ) # Calculate apparent power return voltage_rect * current_rect if __name__ == "__main__": import doctest doctest.testmod()
291
"""simple docstring""" import inspect import unittest from transformers import ViTMSNConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTMSNForImageClassification, ViTMSNModel from transformers.models.vit_msn.modeling_vit_msn import VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class __magic_name__ : '''simple docstring''' def __init__( self , _a , _a=13 , _a=30 , _a=2 , _a=3 , _a=True , _a=True , _a=32 , _a=5 , _a=4 , _a=37 , _a="gelu" , _a=0.1 , _a=0.1 , _a=10 , _a=0.02 , _a=None , ): """simple docstring""" lowerCamelCase = parent lowerCamelCase = batch_size lowerCamelCase = image_size lowerCamelCase = patch_size lowerCamelCase = num_channels lowerCamelCase = is_training lowerCamelCase = use_labels lowerCamelCase = hidden_size lowerCamelCase = num_hidden_layers lowerCamelCase = num_attention_heads lowerCamelCase = intermediate_size lowerCamelCase = hidden_act lowerCamelCase = hidden_dropout_prob lowerCamelCase = attention_probs_dropout_prob lowerCamelCase = type_sequence_label_size lowerCamelCase = initializer_range lowerCamelCase = scope # in ViT MSN, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) lowerCamelCase = (image_size // patch_size) ** 2 lowerCamelCase = num_patches + 1 def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) lowerCamelCase = None if self.use_labels: lowerCamelCase = ids_tensor([self.batch_size] , self.type_sequence_label_size ) lowerCamelCase = self.get_config() return config, pixel_values, labels def _lowerCAmelCase ( self ): """simple docstring""" return ViTMSNConfig( 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 , initializer_range=self.initializer_range , ) def _lowerCAmelCase ( self , _a , _a , _a ): """simple docstring""" lowerCamelCase = ViTMSNModel(config=_a ) model.to(_a ) model.eval() lowerCamelCase = model(_a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _lowerCAmelCase ( self , _a , _a , _a ): """simple docstring""" lowerCamelCase = self.type_sequence_label_size lowerCamelCase = ViTMSNForImageClassification(_a ) model.to(_a ) model.eval() lowerCamelCase = model(_a , labels=_a ) print("""Pixel and labels shape: {pixel_values.shape}, {labels.shape}""" ) print("""Labels: {labels}""" ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images lowerCamelCase = 1 lowerCamelCase = ViTMSNForImageClassification(_a ) model.to(_a ) model.eval() lowerCamelCase = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) lowerCamelCase = model(_a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.prepare_config_and_inputs() lowerCamelCase , lowerCamelCase , lowerCamelCase = config_and_inputs lowerCamelCase = {"""pixel_values""": pixel_values} return config, inputs_dict @require_torch class __magic_name__ ( UpperCAmelCase__ , UpperCAmelCase__ , unittest.TestCase ): '''simple docstring''' __UpperCamelCase = (ViTMSNModel, ViTMSNForImageClassification) if is_torch_available() else () __UpperCamelCase = ( {"feature-extraction": ViTMSNModel, "image-classification": ViTMSNForImageClassification} if is_torch_available() else {} ) __UpperCamelCase = False __UpperCamelCase = False __UpperCamelCase = False __UpperCamelCase = False def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = ViTMSNModelTester(self ) lowerCamelCase = ConfigTester(self , config_class=_a , has_text_modality=_a , hidden_size=37 ) def _lowerCAmelCase ( self ): """simple docstring""" self.config_tester.run_common_tests() @unittest.skip(reason="""ViTMSN does not use inputs_embeds""" ) def _lowerCAmelCase ( self ): """simple docstring""" pass def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase , lowerCamelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCamelCase = model_class(_a ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) lowerCamelCase = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_a , nn.Linear ) ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase , lowerCamelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCamelCase = model_class(_a ) lowerCamelCase = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic lowerCamelCase = [*signature.parameters.keys()] lowerCamelCase = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , _a ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_a ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_a ) @slow def _lowerCAmelCase ( self ): """simple docstring""" for model_name in VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowerCamelCase = ViTMSNModel.from_pretrained(_a ) self.assertIsNotNone(_a ) def a__ ( ) -> Any: lowerCamelCase = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) return image @require_torch @require_vision class __magic_name__ ( unittest.TestCase ): '''simple docstring''' @cached_property def _lowerCAmelCase ( self ): """simple docstring""" return ViTImageProcessor.from_pretrained("""facebook/vit-msn-small""" ) if is_vision_available() else None @slow def _lowerCAmelCase ( self ): """simple docstring""" torch.manual_seed(2 ) lowerCamelCase = ViTMSNForImageClassification.from_pretrained("""facebook/vit-msn-small""" ).to(_a ) lowerCamelCase = self.default_image_processor lowerCamelCase = prepare_img() lowerCamelCase = image_processor(images=_a , return_tensors="""pt""" ).to(_a ) # forward pass with torch.no_grad(): lowerCamelCase = model(**_a ) # verify the logits lowerCamelCase = torch.Size((1, 1_000) ) self.assertEqual(outputs.logits.shape , _a ) lowerCamelCase = torch.tensor([-0.0_803, -0.4_454, -0.2_375] ).to(_a ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , _a , atol=1e-4 ) )
291
1
"""simple docstring""" from typing import Dict, List, Optional, Tuple, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_torch_available, is_torch_tensor, logging if is_torch_available(): import torch lowerCAmelCase : int = logging.get_logger(__name__) class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' __UpperCamelCase = ["pixel_values"] def __init__( self , _a = True , _a = None , _a = PILImageResampling.BILINEAR , _a = True , _a = None , _a = True , _a = 1 / 255 , _a = True , _a = None , _a = None , **_a , ): """simple docstring""" super().__init__(**_a ) lowerCamelCase = size if size is not None else {"""shortest_edge""": 256} lowerCamelCase = get_size_dict(_a , default_to_square=_a ) lowerCamelCase = crop_size if crop_size is not None else {"""height""": 224, """width""": 224} lowerCamelCase = get_size_dict(_a , param_name="""crop_size""" ) lowerCamelCase = do_resize lowerCamelCase = size lowerCamelCase = resample lowerCamelCase = do_center_crop lowerCamelCase = crop_size lowerCamelCase = do_rescale lowerCamelCase = rescale_factor lowerCamelCase = do_normalize lowerCamelCase = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN lowerCamelCase = image_std if image_std is not None else IMAGENET_STANDARD_STD def _lowerCAmelCase ( self , _a , _a , _a = PILImageResampling.BICUBIC , _a = None , **_a , ): """simple docstring""" lowerCamelCase = get_size_dict(_a , default_to_square=_a ) if "shortest_edge" not in size: raise ValueError(f'The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}' ) lowerCamelCase = get_resize_output_image_size(_a , size=size["""shortest_edge"""] , default_to_square=_a ) return resize(_a , size=_a , resample=_a , data_format=_a , **_a ) def _lowerCAmelCase ( self , _a , _a , _a = None , **_a , ): """simple docstring""" lowerCamelCase = get_size_dict(_a ) if "height" not in size or "width" not in size: raise ValueError(f'The `size` parameter must contain the keys `height` and `width`. Got {size.keys()}' ) return center_crop(_a , size=(size["""height"""], size["""width"""]) , data_format=_a , **_a ) def _lowerCAmelCase ( self , _a , _a , _a = None , **_a ): """simple docstring""" return rescale(_a , scale=_a , data_format=_a , **_a ) def _lowerCAmelCase ( self , _a , _a , _a , _a = None , **_a , ): """simple docstring""" return normalize(_a , mean=_a , std=_a , data_format=_a , **_a ) def _lowerCAmelCase ( self , _a , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = ChannelDimension.FIRST , **_a , ): """simple docstring""" lowerCamelCase = do_resize if do_resize is not None else self.do_resize lowerCamelCase = size if size is not None else self.size lowerCamelCase = get_size_dict(_a , default_to_square=_a ) lowerCamelCase = resample if resample is not None else self.resample lowerCamelCase = do_center_crop if do_center_crop is not None else self.do_center_crop lowerCamelCase = crop_size if crop_size is not None else self.crop_size lowerCamelCase = get_size_dict(_a , param_name="""crop_size""" ) lowerCamelCase = do_rescale if do_rescale is not None else self.do_rescale lowerCamelCase = rescale_factor if rescale_factor is not None else self.rescale_factor lowerCamelCase = do_normalize if do_normalize is not None else self.do_normalize lowerCamelCase = image_mean if image_mean is not None else self.image_mean lowerCamelCase = image_std if image_std is not None else self.image_std lowerCamelCase = make_list_of_images(_a ) if not valid_images(_a ): raise ValueError( """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """ """torch.Tensor, tf.Tensor or jax.ndarray.""" ) if do_resize and size is None: raise ValueError("""Size must be specified if do_resize is True.""" ) if do_center_crop and crop_size is None: raise ValueError("""Crop size must be specified if do_center_crop is True.""" ) if do_rescale and rescale_factor is None: raise ValueError("""Rescale factor must be specified if do_rescale is True.""" ) if do_normalize and (image_mean is None or image_std is None): raise ValueError("""Image mean and std must be specified if do_normalize is True.""" ) # All transformations expect numpy arrays. lowerCamelCase = [to_numpy_array(_a ) for image in images] if do_resize: lowerCamelCase = [self.resize(image=_a , size=_a , resample=_a ) for image in images] if do_center_crop: lowerCamelCase = [self.center_crop(image=_a , size=_a ) for image in images] if do_rescale: lowerCamelCase = [self.rescale(image=_a , scale=_a ) for image in images] if do_normalize: lowerCamelCase = [self.normalize(image=_a , mean=_a , std=_a ) for image in images] lowerCamelCase = [to_channel_dimension_format(_a , _a ) for image in images] lowerCamelCase = {"""pixel_values""": images} return BatchFeature(data=_a , tensor_type=_a ) def _lowerCAmelCase ( self , _a , _a = None ): """simple docstring""" lowerCamelCase = outputs.logits # Resize logits and compute semantic segmentation maps if target_sizes is not None: if len(_a ) != len(_a ): raise ValueError( """Make sure that you pass in as many target sizes as the batch dimension of the logits""" ) if is_torch_tensor(_a ): lowerCamelCase = target_sizes.numpy() lowerCamelCase = [] for idx in range(len(_a ) ): lowerCamelCase = torch.nn.functional.interpolate( logits[idx].unsqueeze(dim=0 ) , size=target_sizes[idx] , mode="""bilinear""" , align_corners=_a ) lowerCamelCase = resized_logits[0].argmax(dim=0 ) semantic_segmentation.append(_a ) else: lowerCamelCase = logits.argmax(dim=1 ) lowerCamelCase = [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0] )] return semantic_segmentation
291
"""simple docstring""" 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__ ( snake_case__ , snake_case__ , snake_case__ ) -> List[Any]: return params[F'{prefix}/{prefix}/relpos_bias/rel_embedding'][:, i, :] def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__="attention" ) -> List[Any]: lowerCamelCase = lowerCamelCase = np.ascontiguousarray(params[F'{prefix}/{prefix}/{layer_name}/key/kernel'][:, i, :, :] ) lowerCamelCase = k_tmp.reshape(k_tmp.shape[0] , k_tmp.shape[1] * k_tmp.shape[2] ) lowerCamelCase = np.ascontiguousarray(params[F'{prefix}/{prefix}/{layer_name}/out/kernel'][:, i, :, :] ) lowerCamelCase = o_tmp.reshape(o_tmp.shape[0] * o_tmp.shape[1] , o_tmp.shape[2] ) lowerCamelCase = np.ascontiguousarray(params[F'{prefix}/{prefix}/{layer_name}/query/kernel'][:, i, :, :] ) lowerCamelCase = q_tmp.reshape(q_tmp.shape[0] , q_tmp.shape[1] * q_tmp.shape[2] ) lowerCamelCase = np.ascontiguousarray(params[F'{prefix}/{prefix}/{layer_name}/value/kernel'][:, i, :, :] ) lowerCamelCase = v_tmp.reshape(v_tmp.shape[0] , v_tmp.shape[1] * v_tmp.shape[2] ) return k, o, q, v def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__=False ) -> List[str]: if split_mlp_wi: lowerCamelCase = params[F'{prefix}/{prefix}/mlp/wi_0/kernel'][:, i, :] lowerCamelCase = params[F'{prefix}/{prefix}/mlp/wi_1/kernel'][:, i, :] lowerCamelCase = (wi_a, wi_a) else: lowerCamelCase = params[F'{prefix}/{prefix}/mlp/wi/kernel'][:, i, :] lowerCamelCase = params[F'{prefix}/{prefix}/mlp/wo/kernel'][:, i, :] return wi, wo def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ ) -> Tuple: return params[F'{prefix}/{prefix}/{layer_name}/scale'][:, i] def a__ ( snake_case__ , *, snake_case__ , snake_case__ , snake_case__ = False ) -> Dict: lowerCamelCase = traverse_util.flatten_dict(variables["""target"""] ) lowerCamelCase = {"""/""".join(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 lowerCamelCase = """encoder/encoder/mlp/wi_0/kernel""" in old print("""Split MLP:""" , snake_case__ ) lowerCamelCase = collections.OrderedDict() # Shared embeddings. lowerCamelCase = old["""token_embedder/embedding"""] # Encoder. for i in range(snake_case__ ): # Block i, layer 0 (Self Attention). lowerCamelCase = tax_layer_norm_lookup(snake_case__ , snake_case__ , """encoder""" , """pre_attention_layer_norm""" ) lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase = tax_attention_lookup(snake_case__ , snake_case__ , """encoder""" , """attention""" ) lowerCamelCase = layer_norm lowerCamelCase = k.T lowerCamelCase = o.T lowerCamelCase = q.T lowerCamelCase = v.T # Block i, layer 1 (MLP). lowerCamelCase = tax_layer_norm_lookup(snake_case__ , snake_case__ , """encoder""" , """pre_mlp_layer_norm""" ) lowerCamelCase , lowerCamelCase = tax_mlp_lookup(snake_case__ , snake_case__ , """encoder""" , snake_case__ ) lowerCamelCase = layer_norm if split_mlp_wi: lowerCamelCase = wi[0].T lowerCamelCase = wi[1].T else: lowerCamelCase = wi.T lowerCamelCase = wo.T if scalable_attention: # convert the rel_embedding of each layer lowerCamelCase = tax_relpos_bias_lookup( snake_case__ , snake_case__ , """encoder""" ).T lowerCamelCase = old["""encoder/encoder_norm/scale"""] if not scalable_attention: lowerCamelCase = tax_relpos_bias_lookup( snake_case__ , 0 , """encoder""" ).T lowerCamelCase = tax_relpos_bias_lookup( snake_case__ , 0 , """decoder""" ).T if not is_encoder_only: # Decoder. for i in range(snake_case__ ): # Block i, layer 0 (Self Attention). lowerCamelCase = tax_layer_norm_lookup(snake_case__ , snake_case__ , """decoder""" , """pre_self_attention_layer_norm""" ) lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase = tax_attention_lookup(snake_case__ , snake_case__ , """decoder""" , """self_attention""" ) lowerCamelCase = layer_norm lowerCamelCase = k.T lowerCamelCase = o.T lowerCamelCase = q.T lowerCamelCase = v.T # Block i, layer 1 (Cross Attention). lowerCamelCase = tax_layer_norm_lookup(snake_case__ , snake_case__ , """decoder""" , """pre_cross_attention_layer_norm""" ) lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase = tax_attention_lookup(snake_case__ , snake_case__ , """decoder""" , """encoder_decoder_attention""" ) lowerCamelCase = layer_norm lowerCamelCase = k.T lowerCamelCase = o.T lowerCamelCase = q.T lowerCamelCase = v.T # Block i, layer 2 (MLP). lowerCamelCase = tax_layer_norm_lookup(snake_case__ , snake_case__ , """decoder""" , """pre_mlp_layer_norm""" ) lowerCamelCase , lowerCamelCase = tax_mlp_lookup(snake_case__ , snake_case__ , """decoder""" , snake_case__ ) lowerCamelCase = layer_norm if split_mlp_wi: lowerCamelCase = wi[0].T lowerCamelCase = wi[1].T else: lowerCamelCase = wi.T lowerCamelCase = wo.T if scalable_attention: # convert the rel_embedding of each layer lowerCamelCase = tax_relpos_bias_lookup(snake_case__ , snake_case__ , """decoder""" ).T lowerCamelCase = 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: lowerCamelCase = old["""decoder/logits_dense/kernel"""].T return new def a__ ( snake_case__ , snake_case__ ) -> Optional[int]: lowerCamelCase = 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: lowerCamelCase = state_dict["""shared.weight"""] if not is_encoder_only: if "decoder.embed_tokens.weight" not in state_dict: lowerCamelCase = 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.""" ) lowerCamelCase = state_dict["""shared.weight"""] return state_dict def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ ) -> List[Any]: lowerCamelCase = checkpoints.load_tax_checkpoint(snake_case__ ) lowerCamelCase = convert_tax_to_pytorch( snake_case__ , num_layers=config.num_layers , is_encoder_only=snake_case__ , scalable_attention=snake_case__ ) lowerCamelCase = make_state_dict(snake_case__ , snake_case__ ) model.load_state_dict(snake_case__ , strict=snake_case__ ) def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ = False , snake_case__ = False , ) -> str: lowerCamelCase = MTaConfig.from_json_file(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: lowerCamelCase = UMTaEncoderModel(snake_case__ ) else: lowerCamelCase = UMTaForConditionalGeneration(snake_case__ ) # Load weights from tf checkpoint load_tax_weights_in_ta(snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ ) # Save pytorch-model print(F'Save PyTorch model to {pytorch_dump_path}' ) model.save_pretrained(snake_case__ ) # Verify that we can load the checkpoint. model.from_pretrained(snake_case__ ) print("""Done""" ) if __name__ == "__main__": lowerCAmelCase : 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, ) lowerCAmelCase : 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, )
291
1
"""simple docstring""" import argparse lowerCAmelCase : str = """docs/source/_static/js/custom.js""" def a__ ( snake_case__ ) -> List[str]: with open(snake_case__ , encoding="""utf-8""" , newline="""\n""" ) as f: lowerCamelCase = f.readlines() lowerCamelCase = 0 # First let's put the right version while not lines[index].startswith("""const stableVersion =""" ): index += 1 lowerCamelCase = F'const stableVersion = "v{version}"\n' # Then update the dictionary while not lines[index].startswith("""const versionMapping = {""" ): index += 1 # We go until the end while not lines[index].startswith("""}""" ): index += 1 # We add the new version at the end lines[index - 1] += F' "v{version}": "v{version}",\n' with open(snake_case__ , """w""" , encoding="""utf-8""" , newline="""\n""" ) as f: f.writelines(snake_case__ ) if __name__ == "__main__": lowerCAmelCase : Tuple = argparse.ArgumentParser() parser.add_argument("""--version""", help="""Release version.""") lowerCAmelCase : Optional[Any] = parser.parse_args() update_custom_js(args.version)
291
"""simple docstring""" from __future__ import annotations def a__ ( snake_case__ , snake_case__ ) -> bool: if len(snake_case__ ) == 0: return False lowerCamelCase = len(snake_case__ ) // 2 if a_list[midpoint] == item: return True if item < a_list[midpoint]: return binary_search(a_list[:midpoint] , snake_case__ ) else: return binary_search(a_list[midpoint + 1 :] , snake_case__ ) if __name__ == "__main__": lowerCAmelCase : List[Any] = input("""Enter numbers separated by comma:\n""").strip() lowerCAmelCase : Optional[Any] = [int(item.strip()) for item in user_input.split(""",""")] lowerCAmelCase : Optional[int] = int(input("""Enter the number to be found in the list:\n""").strip()) lowerCAmelCase : Union[str, Any] = """""" if binary_search(sequence, target) else """not """ print(F"""{target} was {not_str}found in {sequence}""")
291
1
"""simple docstring""" import unittest from transformers import MraConfig, is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask if is_torch_available(): import torch from transformers import ( MraForMaskedLM, MraForMultipleChoice, MraForQuestionAnswering, MraForSequenceClassification, MraForTokenClassification, MraModel, ) from transformers.models.mra.modeling_mra import MRA_PRETRAINED_MODEL_ARCHIVE_LIST class __magic_name__ : '''simple docstring''' def __init__( self , _a , _a=2 , _a=8 , _a=True , _a=True , _a=True , _a=True , _a=99 , _a=16 , _a=5 , _a=2 , _a=36 , _a="gelu" , _a=0.0 , _a=0.0 , _a=512 , _a=16 , _a=2 , _a=0.02 , _a=3 , _a=4 , _a=None , ): """simple docstring""" lowerCamelCase = parent lowerCamelCase = batch_size lowerCamelCase = seq_length lowerCamelCase = is_training lowerCamelCase = use_input_mask lowerCamelCase = use_token_type_ids lowerCamelCase = use_labels lowerCamelCase = vocab_size lowerCamelCase = hidden_size lowerCamelCase = num_hidden_layers lowerCamelCase = num_attention_heads lowerCamelCase = intermediate_size lowerCamelCase = hidden_act lowerCamelCase = hidden_dropout_prob lowerCamelCase = attention_probs_dropout_prob lowerCamelCase = max_position_embeddings lowerCamelCase = type_vocab_size lowerCamelCase = type_sequence_label_size lowerCamelCase = initializer_range lowerCamelCase = num_labels lowerCamelCase = num_choices lowerCamelCase = scope def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) lowerCamelCase = None if self.use_input_mask: lowerCamelCase = random_attention_mask([self.batch_size, self.seq_length] ) lowerCamelCase = None if self.use_token_type_ids: lowerCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) lowerCamelCase = None lowerCamelCase = None lowerCamelCase = None if self.use_labels: lowerCamelCase = ids_tensor([self.batch_size] , self.type_sequence_label_size ) lowerCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) lowerCamelCase = ids_tensor([self.batch_size] , self.num_choices ) lowerCamelCase = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def _lowerCAmelCase ( self ): """simple docstring""" return MraConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=_a , initializer_range=self.initializer_range , ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.get_config() lowerCamelCase = 300 return config def _lowerCAmelCase ( self ): """simple docstring""" ( ( lowerCamelCase ) , ( lowerCamelCase ) , ( lowerCamelCase ) , ( lowerCamelCase ) , ( lowerCamelCase ) , ( lowerCamelCase ) , ( lowerCamelCase ) , ) = self.prepare_config_and_inputs() lowerCamelCase = True lowerCamelCase = floats_tensor([self.batch_size, self.seq_length, self.hidden_size] ) lowerCamelCase = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) return ( config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels, encoder_hidden_states, encoder_attention_mask, ) def _lowerCAmelCase ( self , _a , _a , _a , _a , _a , _a , _a ): """simple docstring""" lowerCamelCase = MraModel(config=_a ) model.to(_a ) model.eval() lowerCamelCase = model(_a , attention_mask=_a , token_type_ids=_a ) lowerCamelCase = model(_a , token_type_ids=_a ) lowerCamelCase = model(_a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _lowerCAmelCase ( self , _a , _a , _a , _a , _a , _a , _a , _a , _a , ): """simple docstring""" lowerCamelCase = True lowerCamelCase = MraModel(_a ) model.to(_a ) model.eval() lowerCamelCase = model( _a , attention_mask=_a , token_type_ids=_a , encoder_hidden_states=_a , encoder_attention_mask=_a , ) lowerCamelCase = model( _a , attention_mask=_a , token_type_ids=_a , encoder_hidden_states=_a , ) lowerCamelCase = model(_a , attention_mask=_a , token_type_ids=_a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _lowerCAmelCase ( self , _a , _a , _a , _a , _a , _a , _a ): """simple docstring""" lowerCamelCase = MraForMaskedLM(config=_a ) model.to(_a ) model.eval() lowerCamelCase = model(_a , attention_mask=_a , token_type_ids=_a , labels=_a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def _lowerCAmelCase ( self , _a , _a , _a , _a , _a , _a , _a ): """simple docstring""" lowerCamelCase = MraForQuestionAnswering(config=_a ) model.to(_a ) model.eval() lowerCamelCase = model( _a , attention_mask=_a , token_type_ids=_a , start_positions=_a , end_positions=_a , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def _lowerCAmelCase ( self , _a , _a , _a , _a , _a , _a , _a ): """simple docstring""" lowerCamelCase = self.num_labels lowerCamelCase = MraForSequenceClassification(_a ) model.to(_a ) model.eval() lowerCamelCase = model(_a , attention_mask=_a , token_type_ids=_a , labels=_a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def _lowerCAmelCase ( self , _a , _a , _a , _a , _a , _a , _a ): """simple docstring""" lowerCamelCase = self.num_labels lowerCamelCase = MraForTokenClassification(config=_a ) model.to(_a ) model.eval() lowerCamelCase = model(_a , attention_mask=_a , token_type_ids=_a , labels=_a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def _lowerCAmelCase ( self , _a , _a , _a , _a , _a , _a , _a ): """simple docstring""" lowerCamelCase = self.num_choices lowerCamelCase = MraForMultipleChoice(config=_a ) model.to(_a ) model.eval() lowerCamelCase = input_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() lowerCamelCase = token_type_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() lowerCamelCase = input_mask.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() lowerCamelCase = model( _a , attention_mask=_a , token_type_ids=_a , labels=_a , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.prepare_config_and_inputs() ( ( lowerCamelCase ) , ( lowerCamelCase ) , ( lowerCamelCase ) , ( lowerCamelCase ) , ( lowerCamelCase ) , ( lowerCamelCase ) , ( lowerCamelCase ) , ) = config_and_inputs lowerCamelCase = {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask} return config, inputs_dict @require_torch class __magic_name__ ( UpperCAmelCase__ , unittest.TestCase ): '''simple docstring''' __UpperCamelCase = ( ( MraModel, MraForMaskedLM, MraForMultipleChoice, MraForQuestionAnswering, MraForSequenceClassification, MraForTokenClassification, ) if is_torch_available() else () ) __UpperCamelCase = False __UpperCamelCase = False __UpperCamelCase = False __UpperCamelCase = False __UpperCamelCase = () def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = MraModelTester(self ) lowerCamelCase = ConfigTester(self , config_class=_a , hidden_size=37 ) def _lowerCAmelCase ( self ): """simple docstring""" self.config_tester.run_common_tests() def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_a ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: lowerCamelCase = type self.model_tester.create_and_check_model(*_a ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*_a ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_multiple_choice(*_a ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*_a ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*_a ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*_a ) @slow def _lowerCAmelCase ( self ): """simple docstring""" for model_name in MRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowerCamelCase = MraModel.from_pretrained(_a ) self.assertIsNotNone(_a ) @unittest.skip(reason="""MRA does not output attentions""" ) def _lowerCAmelCase ( self ): """simple docstring""" return @require_torch class __magic_name__ ( unittest.TestCase ): '''simple docstring''' @slow def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = MraModel.from_pretrained("""uw-madison/mra-base-512-4""" ) lowerCamelCase = torch.arange(256 ).unsqueeze(0 ) with torch.no_grad(): lowerCamelCase = model(_a )[0] lowerCamelCase = torch.Size((1, 256, 768) ) self.assertEqual(output.shape , _a ) lowerCamelCase = torch.tensor( [[[-0.0_140, 0.0_830, -0.0_381], [0.1_546, 0.1_402, 0.0_220], [0.1_162, 0.0_851, 0.0_165]]] ) self.assertTrue(torch.allclose(output[:, :3, :3] , _a , atol=1e-4 ) ) @slow def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = MraForMaskedLM.from_pretrained("""uw-madison/mra-base-512-4""" ) lowerCamelCase = torch.arange(256 ).unsqueeze(0 ) with torch.no_grad(): lowerCamelCase = model(_a )[0] lowerCamelCase = 50_265 lowerCamelCase = torch.Size((1, 256, vocab_size) ) self.assertEqual(output.shape , _a ) lowerCamelCase = torch.tensor( [[[9.2_595, -3.6_038, 11.8_819], [9.3_869, -3.2_693, 11.0_956], [11.8_524, -3.4_938, 13.1_210]]] ) self.assertTrue(torch.allclose(output[:, :3, :3] , _a , atol=1e-4 ) ) @slow def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = MraForMaskedLM.from_pretrained("""uw-madison/mra-base-4096-8-d3""" ) lowerCamelCase = torch.arange(4_096 ).unsqueeze(0 ) with torch.no_grad(): lowerCamelCase = model(_a )[0] lowerCamelCase = 50_265 lowerCamelCase = torch.Size((1, 4_096, vocab_size) ) self.assertEqual(output.shape , _a ) lowerCamelCase = torch.tensor( [[[5.4_789, -2.3_564, 7.5_064], [7.9_067, -1.3_369, 9.9_668], [9.0_712, -1.8_106, 7.0_380]]] ) self.assertTrue(torch.allclose(output[:, :3, :3] , _a , atol=1e-4 ) )
291
"""simple docstring""" def a__ ( snake_case__ ) -> list: if len(snake_case__ ) < 2: return collection def circle_sort_util(snake_case__ , snake_case__ , snake_case__ ) -> bool: lowerCamelCase = False if low == high: return swapped lowerCamelCase = low lowerCamelCase = high while left < right: if collection[left] > collection[right]: lowerCamelCase , lowerCamelCase = ( collection[right], collection[left], ) lowerCamelCase = True left += 1 right -= 1 if left == right and collection[left] > collection[right + 1]: lowerCamelCase , lowerCamelCase = ( collection[right + 1], collection[left], ) lowerCamelCase = True lowerCamelCase = low + int((high - low) / 2 ) lowerCamelCase = circle_sort_util(snake_case__ , snake_case__ , snake_case__ ) lowerCamelCase = circle_sort_util(snake_case__ , mid + 1 , snake_case__ ) return swapped or left_swap or right_swap lowerCamelCase = True while is_not_sorted is True: lowerCamelCase = circle_sort_util(snake_case__ , 0 , len(snake_case__ ) - 1 ) return collection if __name__ == "__main__": lowerCAmelCase : Tuple = input("""Enter numbers separated by a comma:\n""").strip() lowerCAmelCase : List[Any] = [int(item) for item in user_input.split(""",""")] print(circle_sort(unsorted))
291
1
"""simple docstring""" def a__ ( snake_case__ ) -> float: if edge <= 0 or not isinstance(snake_case__ , snake_case__ ): raise ValueError("""Length must be a positive.""" ) return 3 * ((25 + 10 * (5 ** (1 / 2))) ** (1 / 2)) * (edge**2) def a__ ( snake_case__ ) -> float: if edge <= 0 or not isinstance(snake_case__ , snake_case__ ): raise ValueError("""Length must be a positive.""" ) return ((15 + (7 * (5 ** (1 / 2)))) / 4) * (edge**3) if __name__ == "__main__": import doctest doctest.testmod()
291
"""simple docstring""" from collections.abc import Generator def a__ ( ) -> Generator[int, None, None]: lowerCamelCase , lowerCamelCase = 0, 1 while True: lowerCamelCase , lowerCamelCase = b, a + b yield b def a__ ( snake_case__ = 10_00 ) -> int: lowerCamelCase = 1 lowerCamelCase = fibonacci_generator() while len(str(next(snake_case__ ) ) ) < n: answer += 1 return answer + 1 if __name__ == "__main__": print(solution(int(str(input()).strip())))
291
1
"""simple docstring""" import inspect import unittest from typing import List import numpy as np from transformers import EfficientFormerConfig 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 ( TFEfficientFormerForImageClassification, TFEfficientFormerForImageClassificationWithTeacher, TFEfficientFormerModel, ) from transformers.models.efficientformer.modeling_tf_efficientformer import ( TF_EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, ) if is_vision_available(): from PIL import Image from transformers import EfficientFormerImageProcessor class __magic_name__ : '''simple docstring''' def __init__( self , _a , _a = 13 , _a = 64 , _a = 2 , _a = 3 , _a = 3 , _a = True , _a = True , _a = 128 , _a=[16, 32, 64, 128] , _a = 7 , _a = 4 , _a = 37 , _a = "gelu" , _a = 0.1 , _a = 0.1 , _a = 10 , _a = 0.02 , _a = 2 , _a = 1 , _a = 128 , _a = [2, 2, 2, 2] , _a = 2 , _a = 2 , ): """simple docstring""" lowerCamelCase = parent lowerCamelCase = batch_size lowerCamelCase = image_size lowerCamelCase = patch_size lowerCamelCase = num_channels lowerCamelCase = is_training lowerCamelCase = use_labels lowerCamelCase = hidden_size lowerCamelCase = num_hidden_layers lowerCamelCase = num_attention_heads lowerCamelCase = intermediate_size lowerCamelCase = hidden_act lowerCamelCase = hidden_dropout_prob lowerCamelCase = attention_probs_dropout_prob lowerCamelCase = type_sequence_label_size lowerCamelCase = initializer_range lowerCamelCase = encoder_stride lowerCamelCase = num_attention_outputs lowerCamelCase = embed_dim lowerCamelCase = embed_dim + 1 lowerCamelCase = resolution lowerCamelCase = depths lowerCamelCase = hidden_sizes lowerCamelCase = dim lowerCamelCase = mlp_expansion_ratio def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) lowerCamelCase = None if self.use_labels: lowerCamelCase = ids_tensor([self.batch_size] , self.type_sequence_label_size ) lowerCamelCase = self.get_config() return config, pixel_values, labels def _lowerCAmelCase ( self ): """simple docstring""" return EfficientFormerConfig( 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=_a , initializer_range=self.initializer_range , encoder_stride=self.encoder_stride , resolution=self.resolution , depths=self.depths , hidden_sizes=self.hidden_sizes , dim=self.dim , mlp_expansion_ratio=self.mlp_expansion_ratio , ) def _lowerCAmelCase ( self , _a , _a , _a ): """simple docstring""" lowerCamelCase = TFEfficientFormerModel(config=_a ) lowerCamelCase = model(_a , training=_a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _lowerCAmelCase ( self , _a , _a , _a ): """simple docstring""" lowerCamelCase = self.type_sequence_label_size lowerCamelCase = TFEfficientFormerForImageClassification(_a ) lowerCamelCase = model(_a , labels=_a , training=_a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images lowerCamelCase = 1 lowerCamelCase = TFEfficientFormerForImageClassification(_a ) lowerCamelCase = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) lowerCamelCase = model(_a , labels=_a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.prepare_config_and_inputs() lowerCamelCase , lowerCamelCase , lowerCamelCase = config_and_inputs lowerCamelCase = {"""pixel_values""": pixel_values} return config, inputs_dict @require_tf class __magic_name__ ( UpperCAmelCase__ , UpperCAmelCase__ , unittest.TestCase ): '''simple docstring''' __UpperCamelCase = ( ( TFEfficientFormerModel, TFEfficientFormerForImageClassificationWithTeacher, TFEfficientFormerForImageClassification, ) if is_tf_available() else () ) __UpperCamelCase = ( { "feature-extraction": TFEfficientFormerModel, "image-classification": ( TFEfficientFormerForImageClassification, TFEfficientFormerForImageClassificationWithTeacher, ), } if is_tf_available() else {} ) __UpperCamelCase = False __UpperCamelCase = False __UpperCamelCase = False __UpperCamelCase = False __UpperCamelCase = False def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = TFEfficientFormerModelTester(self ) lowerCamelCase = ConfigTester( self , config_class=_a , has_text_modality=_a , hidden_size=37 ) def _lowerCAmelCase ( self ): """simple docstring""" self.config_tester.run_common_tests() @unittest.skip(reason="""EfficientFormer does not use inputs_embeds""" ) def _lowerCAmelCase ( self ): """simple docstring""" pass @unittest.skip(reason="""EfficientFormer does not support input and output embeddings""" ) def _lowerCAmelCase ( self ): """simple docstring""" pass def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase , lowerCamelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCamelCase = model_class(_a ) lowerCamelCase = inspect.signature(model.call ) # signature.parameters is an OrderedDict => so arg_names order is deterministic lowerCamelCase = [*signature.parameters.keys()] lowerCamelCase = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , _a ) def _lowerCAmelCase ( self ): """simple docstring""" def check_hidden_states_output(_a , _a , _a ): lowerCamelCase = model_class(_a ) lowerCamelCase = model(**self._prepare_for_class(_a , _a ) , training=_a ) lowerCamelCase = outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states lowerCamelCase = getattr( self.model_tester , """expected_num_hidden_layers""" , self.model_tester.num_hidden_layers + 1 ) self.assertEqual(len(_a ) , _a ) if hasattr(self.model_tester , """encoder_seq_length""" ): lowerCamelCase = self.model_tester.encoder_seq_length if hasattr(self.model_tester , """chunk_length""" ) and self.model_tester.chunk_length > 1: lowerCamelCase = seq_length * self.model_tester.chunk_length else: lowerCamelCase = self.model_tester.seq_length self.assertListEqual( list(hidden_states[-1].shape[-2:] ) , [seq_length, self.model_tester.hidden_size] , ) if config.is_encoder_decoder: lowerCamelCase = outputs.decoder_hidden_states self.asseretIsInstance(_a , (list, tuple) ) self.assertEqual(len(_a ) , _a ) lowerCamelCase = getattr(self.model_tester , """seq_length""" , _a ) lowerCamelCase = getattr(self.model_tester , """decoder_seq_length""" , _a ) self.assertListEqual( list(hidden_states[-1].shape[-2:] ) , [decoder_seq_length, self.model_tester.hidden_size] , ) lowerCamelCase , lowerCamelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCamelCase = True check_hidden_states_output(_a , _a , _a ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] lowerCamelCase = True check_hidden_states_output(_a , _a , _a ) def _lowerCAmelCase ( self , _a , _a , _a=False ): """simple docstring""" lowerCamelCase = super()._prepare_for_class(_a , _a , return_labels=_a ) if return_labels: if model_class.__name__ == "TFEfficientFormerForImageClassificationWithTeacher": del inputs_dict["labels"] return inputs_dict def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_a ) @unittest.skip(reason="""EfficientFormer does not implement masked image modeling yet""" ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_image_modeling(*_a ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_a ) @slow def _lowerCAmelCase ( self ): """simple docstring""" for model_name in TF_EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowerCamelCase = TFEfficientFormerModel.from_pretrained(_a ) self.assertIsNotNone(_a ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase , lowerCamelCase = self.model_tester.prepare_config_and_inputs_for_common() lowerCamelCase = True lowerCamelCase = getattr(self.model_tester , """seq_length""" , _a ) lowerCamelCase = getattr(self.model_tester , """encoder_seq_length""" , _a ) lowerCamelCase = getattr(self.model_tester , """key_length""" , _a ) lowerCamelCase = getattr(self.model_tester , """chunk_length""" , _a ) if chunk_length is not None and hasattr(self.model_tester , """num_hashes""" ): lowerCamelCase = encoder_seq_length * self.model_tester.num_hashes for model_class in self.all_model_classes: lowerCamelCase = True lowerCamelCase = False lowerCamelCase = True lowerCamelCase = model_class(_a ) lowerCamelCase = model(**self._prepare_for_class(_a , _a ) , training=_a ) lowerCamelCase = outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions self.assertEqual(len(_a ) , self.model_tester.num_attention_outputs ) # check that output_attentions also work using config del inputs_dict["output_attentions"] lowerCamelCase = True lowerCamelCase = model_class(_a ) lowerCamelCase = model(**self._prepare_for_class(_a , _a ) , training=_a ) lowerCamelCase = outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions self.assertEqual(len(_a ) , self.model_tester.num_attention_outputs ) if chunk_length is not None: self.assertListEqual( list(attentions[0].shape[-4:] ) , [self.model_tester.num_attention_heads, encoder_seq_length, chunk_length, encoder_key_length] , ) else: self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, encoder_seq_length, encoder_key_length] , ) def _lowerCAmelCase ( self ): """simple docstring""" # We use a simplified version of this test for EfficientFormer because it requires training=False # and Keras refuses to let us force that during functional construction lowerCamelCase , lowerCamelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: # Prepare our model lowerCamelCase = model_class(_a ) # These are maximally general inputs for the model, with multiple None dimensions # Hopefully this will catch any conditionals that fail for flexible shapes lowerCamelCase = { key: tf.keras.Input(shape=val.shape[1:] , dtype=val.dtype , name=_a ) for key, val in model.input_signature.items() if key in model.dummy_inputs } lowerCamelCase = model(_a ) self.assertTrue(outputs_dict is not None ) def a__ ( ) -> Tuple: lowerCamelCase = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) return image @require_tf @require_vision class __magic_name__ ( unittest.TestCase ): '''simple docstring''' @cached_property def _lowerCAmelCase ( self ): """simple docstring""" return ( EfficientFormerImageProcessor.from_pretrained("""snap-research/efficientformer-l1-300""" ) if is_vision_available() else None ) @slow def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = TFEfficientFormerForImageClassification.from_pretrained("""snap-research/efficientformer-l1-300""" ) lowerCamelCase = self.default_image_processor lowerCamelCase = prepare_img() lowerCamelCase = image_processor(images=_a , return_tensors="""tf""" ) # forward pass lowerCamelCase = model(**_a , training=_a ) # verify the logits lowerCamelCase = tf.TensorShape((1, 1_000) ) self.assertEqual(outputs.logits.shape , _a ) lowerCamelCase = tf.constant([-0.0_555, 0.4_825, -0.0_852] ) self.assertTrue(np.allclose(outputs.logits[0, :3] , _a , atol=1e-4 ) ) @slow def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = TFEfficientFormerForImageClassificationWithTeacher.from_pretrained( """snap-research/efficientformer-l1-300""" ) lowerCamelCase = self.default_image_processor lowerCamelCase = prepare_img() lowerCamelCase = image_processor(images=_a , return_tensors="""tf""" ) # forward pass lowerCamelCase = model(**_a , training=_a ) # verify the logits lowerCamelCase = tf.TensorShape((1, 1_000) ) self.assertEqual(outputs.logits.shape , _a ) lowerCamelCase = tf.constant([-0.1_312, 0.4_353, -1.0_499] ) self.assertTrue(np.allclose(outputs.logits[0, :3] , _a , atol=1e-4 ) )
291
"""simple docstring""" from math import ceil from typing import List, Optional, Union import numpy as np from ...audio_utils import mel_filter_bank, spectrogram, window_function from ...feature_extraction_sequence_utils import BatchFeature, SequenceFeatureExtractor from ...utils import TensorType, logging lowerCAmelCase : List[str] = logging.get_logger(__name__) class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' __UpperCamelCase = ["audio_values", "audio_mask"] def __init__( self , _a=2_048 , _a=1 , _a=[16, 16] , _a=128 , _a=44_100 , _a=86 , _a=2_048 , _a=0.0 , **_a , ): """simple docstring""" super().__init__( feature_size=_a , sampling_rate=_a , padding_value=_a , **_a , ) lowerCamelCase = spectrogram_length lowerCamelCase = num_channels lowerCamelCase = patch_size lowerCamelCase = feature_size // self.patch_size[1] lowerCamelCase = n_fft lowerCamelCase = sampling_rate // hop_length_to_sampling_rate lowerCamelCase = sampling_rate lowerCamelCase = padding_value lowerCamelCase = mel_filter_bank( num_frequency_bins=1 + n_fft // 2 , num_mel_filters=_a , min_frequency=0.0 , max_frequency=22_050.0 , sampling_rate=_a , norm="""slaney""" , mel_scale="""slaney""" , ).T def _lowerCAmelCase ( self , _a ): """simple docstring""" lowerCamelCase = spectrogram( _a , window_function(self.n_fft , """hann""" ) , frame_length=self.n_fft , hop_length=self.hop_length , power=2.0 , mel_filters=self.mel_filters.T , log_mel="""dB""" , db_range=80.0 , ) lowerCamelCase = log_spec[:, :-1] lowerCamelCase = log_spec - 20.0 lowerCamelCase = np.clip(log_spec / 40.0 , -2.0 , 0.0 ) + 1.0 return log_spec def __call__( self , _a , _a = None , _a = True , _a = None , _a = False , _a = False , **_a , ): """simple docstring""" if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( """This feature extractor is set to support sampling rate""" f' of {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled' f' 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.""" ) lowerCamelCase = isinstance(_a , np.ndarray ) and len(raw_speech.shape ) > 1 if is_batched_numpy and len(raw_speech.shape ) > 2: raise ValueError(f'Only mono-channel audio is supported for input to {self}' ) lowerCamelCase = is_batched_numpy or ( isinstance(_a , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: lowerCamelCase = [np.asarray([speech] , dtype=np.floataa ).T for speech in raw_speech] elif not is_batched and not isinstance(_a , np.ndarray ): lowerCamelCase = np.asarray(_a , dtype=np.floataa ) elif isinstance(_a , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): lowerCamelCase = raw_speech.astype(np.floataa ) # always return batch if not is_batched: lowerCamelCase = [np.asarray([raw_speech] ).T] # Convert audio signals to log mel spectrograms, truncate by time axis lowerCamelCase = [ self._np_extract_fbank_features(waveform.squeeze() ).T[: self.spectrogram_length] for waveform in raw_speech ] if isinstance(audio_features[0] , _a ): lowerCamelCase = [np.asarray(_a , dtype=np.floataa ) for feature in audio_features] # Create audio attention mask lowerCamelCase = max( [ceil(feature.shape[0] / self.patch_size[0] ) * self.freq_len for feature in audio_features] ) # The maximum number of audio patches in a batch if return_attention_mask: lowerCamelCase = [ (ceil(feature.shape[0] / self.patch_size[0] ) * self.freq_len) * [1] + (max_patch_len - ceil(feature.shape[0] / self.patch_size[0] ) * self.freq_len) * [0] for feature in audio_features ] lowerCamelCase = np.array(_a ).astype(np.floataa ) # convert into correct format for padding lowerCamelCase = max_patch_len // self.freq_len * self.patch_size[0] # The maximum audio size in a batch lowerCamelCase = np.ones([len(_a ), 1, max_time_len, self.feature_size] ).astype(np.floataa ) lowerCamelCase = padded_audio_features * self.padding_value for i in range(len(_a ) ): lowerCamelCase = audio_features[i] lowerCamelCase = feature # return as BatchFeature if return_attention_mask: lowerCamelCase = {"""audio_values""": padded_audio_features, """audio_mask""": audio_mask} else: lowerCamelCase = {"""audio_values""": padded_audio_features} lowerCamelCase = BatchFeature(data=_a , tensor_type=_a ) return encoded_inputs
291
1
"""simple docstring""" import argparse import os import shutil import torch from emmental.modules import MagnitudeBinarizer, ThresholdBinarizer, TopKBinarizer def a__ ( snake_case__ ) -> List[str]: lowerCamelCase = args.pruning_method lowerCamelCase = args.threshold lowerCamelCase = args.model_name_or_path.rstrip("""/""" ) lowerCamelCase = args.target_model_path print(F'Load fine-pruned model from {model_name_or_path}' ) lowerCamelCase = torch.load(os.path.join(snake_case__ , """pytorch_model.bin""" ) ) lowerCamelCase = {} for name, tensor in model.items(): if "embeddings" in name or "LayerNorm" in name or "pooler" in name: lowerCamelCase = tensor print(F'Copied layer {name}' ) elif "classifier" in name or "qa_output" in name: lowerCamelCase = tensor print(F'Copied layer {name}' ) elif "bias" in name: lowerCamelCase = tensor print(F'Copied layer {name}' ) else: if pruning_method == "magnitude": lowerCamelCase = MagnitudeBinarizer.apply(inputs=snake_case__ , threshold=snake_case__ ) lowerCamelCase = tensor * mask print(F'Pruned layer {name}' ) elif pruning_method == "topK": if "mask_scores" in name: continue lowerCamelCase = name[:-6] lowerCamelCase = model[F'{prefix_}mask_scores'] lowerCamelCase = TopKBinarizer.apply(snake_case__ , snake_case__ ) lowerCamelCase = tensor * mask print(F'Pruned layer {name}' ) elif pruning_method == "sigmoied_threshold": if "mask_scores" in name: continue lowerCamelCase = name[:-6] lowerCamelCase = model[F'{prefix_}mask_scores'] lowerCamelCase = ThresholdBinarizer.apply(snake_case__ , snake_case__ , snake_case__ ) lowerCamelCase = tensor * mask print(F'Pruned layer {name}' ) elif pruning_method == "l0": if "mask_scores" in name: continue lowerCamelCase = name[:-6] lowerCamelCase = model[F'{prefix_}mask_scores'] lowerCamelCase , lowerCamelCase = -0.1, 1.1 lowerCamelCase = torch.sigmoid(snake_case__ ) lowerCamelCase = s * (r - l) + l lowerCamelCase = s_bar.clamp(min=0.0 , max=1.0 ) lowerCamelCase = tensor * mask print(F'Pruned layer {name}' ) else: raise ValueError("""Unknown pruning method""" ) if target_model_path is None: lowerCamelCase = os.path.join( os.path.dirname(snake_case__ ) , F'bertarized_{os.path.basename(snake_case__ )}' ) if not os.path.isdir(snake_case__ ): shutil.copytree(snake_case__ , snake_case__ ) print(F'\nCreated folder {target_model_path}' ) torch.save(snake_case__ , os.path.join(snake_case__ , """pytorch_model.bin""" ) ) print("""\nPruned model saved! See you later!""" ) if __name__ == "__main__": lowerCAmelCase : str = argparse.ArgumentParser() parser.add_argument( """--pruning_method""", choices=["""l0""", """magnitude""", """topK""", """sigmoied_threshold"""], type=str, required=True, help=( """Pruning Method (l0 = L0 regularization, magnitude = Magnitude pruning, topK = Movement pruning,""" """ sigmoied_threshold = Soft movement pruning)""" ), ) parser.add_argument( """--threshold""", type=float, required=False, help=( """For `magnitude` and `topK`, it is the level of remaining weights (in %) in the fine-pruned model.""" """For `sigmoied_threshold`, it is the threshold \tau against which the (sigmoied) scores are compared.""" """Not needed for `l0`""" ), ) parser.add_argument( """--model_name_or_path""", type=str, required=True, help="""Folder containing the model that was previously fine-pruned""", ) parser.add_argument( """--target_model_path""", default=None, type=str, required=False, help="""Folder containing the model that was previously fine-pruned""", ) lowerCAmelCase : Dict = parser.parse_args() main(args)
291
"""simple docstring""" from math import ceil def a__ ( snake_case__ , snake_case__ ) -> Optional[int]: lowerCamelCase = list(range(0 , snake_case__ ) ) lowerCamelCase = [item for sublist in list(device_map.values() ) for item in sublist] # Duplicate check lowerCamelCase = [] for i in device_map_blocks: if device_map_blocks.count(snake_case__ ) > 1 and i not in duplicate_blocks: duplicate_blocks.append(snake_case__ ) # Missing blocks lowerCamelCase = [i for i in blocks if i not in device_map_blocks] lowerCamelCase = [i for i in device_map_blocks if i not in blocks] if len(snake_case__ ) != 0: raise ValueError( """Duplicate attention blocks specified in device_map. Attention blocks must be specified to one device.""" """ These attention blocks were specified more than once: """ + str(snake_case__ ) ) if len(snake_case__ ) != 0: raise ValueError( """There are attention blocks for this model that are not specified in the device_map. Add these attention """ """blocks to a device on the device_map: """ + str(snake_case__ ) ) if len(snake_case__ ) != 0: raise ValueError( """The device_map contains more attention blocks than this model has. Remove these from the device_map:""" + str(snake_case__ ) ) def a__ ( snake_case__ , snake_case__ ) -> List[Any]: lowerCamelCase = list(range(snake_case__ ) ) lowerCamelCase = int(ceil(n_layers / len(snake_case__ ) ) ) lowerCamelCase = [layers[i : i + n_blocks] for i in range(0 , snake_case__ , snake_case__ )] return dict(zip(snake_case__ , snake_case__ ) )
291
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available lowerCAmelCase : int = { """configuration_bloom""": ["""BLOOM_PRETRAINED_CONFIG_ARCHIVE_MAP""", """BloomConfig""", """BloomOnnxConfig"""], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : str = ["""BloomTokenizerFast"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : Dict = [ """BLOOM_PRETRAINED_MODEL_ARCHIVE_LIST""", """BloomForCausalLM""", """BloomModel""", """BloomPreTrainedModel""", """BloomForSequenceClassification""", """BloomForTokenClassification""", """BloomForQuestionAnswering""", ] if TYPE_CHECKING: from .configuration_bloom import BLOOM_PRETRAINED_CONFIG_ARCHIVE_MAP, BloomConfig, BloomOnnxConfig try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_bloom_fast import BloomTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_bloom import ( BLOOM_PRETRAINED_MODEL_ARCHIVE_LIST, BloomForCausalLM, BloomForQuestionAnswering, BloomForSequenceClassification, BloomForTokenClassification, BloomModel, BloomPreTrainedModel, ) else: import sys lowerCAmelCase : str = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
291
"""simple docstring""" import unittest import numpy as np from transformers import RoFormerConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax.numpy as jnp from transformers.models.roformer.modeling_flax_roformer import ( FlaxRoFormerForMaskedLM, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerModel, ) class __magic_name__ ( unittest.TestCase ): '''simple docstring''' def __init__( self , _a , _a=13 , _a=7 , _a=True , _a=True , _a=True , _a=True , _a=99 , _a=32 , _a=5 , _a=4 , _a=37 , _a="gelu" , _a=0.1 , _a=0.1 , _a=512 , _a=16 , _a=2 , _a=0.02 , _a=4 , ): """simple docstring""" lowerCamelCase = parent lowerCamelCase = batch_size lowerCamelCase = seq_length lowerCamelCase = is_training lowerCamelCase = use_attention_mask lowerCamelCase = use_token_type_ids lowerCamelCase = use_labels lowerCamelCase = vocab_size lowerCamelCase = hidden_size lowerCamelCase = num_hidden_layers lowerCamelCase = num_attention_heads lowerCamelCase = intermediate_size lowerCamelCase = hidden_act lowerCamelCase = hidden_dropout_prob lowerCamelCase = attention_probs_dropout_prob lowerCamelCase = max_position_embeddings lowerCamelCase = type_vocab_size lowerCamelCase = type_sequence_label_size lowerCamelCase = initializer_range lowerCamelCase = num_choices def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) lowerCamelCase = None if self.use_attention_mask: lowerCamelCase = random_attention_mask([self.batch_size, self.seq_length] ) lowerCamelCase = None if self.use_token_type_ids: lowerCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) lowerCamelCase = RoFormerConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=_a , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.prepare_config_and_inputs() lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase = config_and_inputs lowerCamelCase = {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": attention_mask} return config, inputs_dict @require_flax class __magic_name__ ( UpperCAmelCase__ , unittest.TestCase ): '''simple docstring''' __UpperCamelCase = True __UpperCamelCase = ( ( FlaxRoFormerModel, FlaxRoFormerForMaskedLM, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, ) if is_flax_available() else () ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = FlaxRoFormerModelTester(self ) @slow def _lowerCAmelCase ( self ): """simple docstring""" for model_class_name in self.all_model_classes: lowerCamelCase = model_class_name.from_pretrained("""junnyu/roformer_chinese_small""" , from_pt=_a ) lowerCamelCase = model(np.ones((1, 1) ) ) self.assertIsNotNone(_a ) @require_flax class __magic_name__ ( unittest.TestCase ): '''simple docstring''' @slow def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = FlaxRoFormerForMaskedLM.from_pretrained("""junnyu/roformer_chinese_base""" ) lowerCamelCase = jnp.array([[0, 1, 2, 3, 4, 5]] ) lowerCamelCase = model(_a )[0] lowerCamelCase = 50_000 lowerCamelCase = (1, 6, vocab_size) self.assertEqual(output.shape , _a ) lowerCamelCase = jnp.array( [[[-0.1_205, -1.0_265, 0.2_922], [-1.5_134, 0.1_974, 0.1_519], [-5.0_135, -3.9_003, -0.8_404]]] ) self.assertTrue(jnp.allclose(output[:, :3, :3] , _a , atol=1e-4 ) )
291
1
"""simple docstring""" from math import ceil def a__ ( snake_case__ , snake_case__ ) -> Optional[int]: lowerCamelCase = list(range(0 , snake_case__ ) ) lowerCamelCase = [item for sublist in list(device_map.values() ) for item in sublist] # Duplicate check lowerCamelCase = [] for i in device_map_blocks: if device_map_blocks.count(snake_case__ ) > 1 and i not in duplicate_blocks: duplicate_blocks.append(snake_case__ ) # Missing blocks lowerCamelCase = [i for i in blocks if i not in device_map_blocks] lowerCamelCase = [i for i in device_map_blocks if i not in blocks] if len(snake_case__ ) != 0: raise ValueError( """Duplicate attention blocks specified in device_map. Attention blocks must be specified to one device.""" """ These attention blocks were specified more than once: """ + str(snake_case__ ) ) if len(snake_case__ ) != 0: raise ValueError( """There are attention blocks for this model that are not specified in the device_map. Add these attention """ """blocks to a device on the device_map: """ + str(snake_case__ ) ) if len(snake_case__ ) != 0: raise ValueError( """The device_map contains more attention blocks than this model has. Remove these from the device_map:""" + str(snake_case__ ) ) def a__ ( snake_case__ , snake_case__ ) -> List[Any]: lowerCamelCase = list(range(snake_case__ ) ) lowerCamelCase = int(ceil(n_layers / len(snake_case__ ) ) ) lowerCamelCase = [layers[i : i + n_blocks] for i in range(0 , snake_case__ , snake_case__ )] return dict(zip(snake_case__ , snake_case__ ) )
291
"""simple docstring""" from typing import Any def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ) -> list: _validation( snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ) # Creates data structures and fill initial step lowerCamelCase = {} lowerCamelCase = {} for state in states_space: lowerCamelCase = observations_space[0] lowerCamelCase = ( initial_probabilities[state] * emission_probabilities[state][observation] ) lowerCamelCase = None # Fills the data structure with the probabilities of # different transitions and pointers to previous states for o in range(1 , len(snake_case__ ) ): lowerCamelCase = observations_space[o] lowerCamelCase = observations_space[o - 1] for state in states_space: # Calculates the argmax for probability function lowerCamelCase = """""" lowerCamelCase = -1 for k_state in states_space: lowerCamelCase = ( probabilities[(k_state, prior_observation)] * transition_probabilities[k_state][state] * emission_probabilities[state][observation] ) if probability > max_probability: lowerCamelCase = probability lowerCamelCase = k_state # Update probabilities and pointers dicts lowerCamelCase = ( probabilities[(arg_max, prior_observation)] * transition_probabilities[arg_max][state] * emission_probabilities[state][observation] ) lowerCamelCase = arg_max # The final observation lowerCamelCase = observations_space[len(snake_case__ ) - 1] # argmax for given final observation lowerCamelCase = """""" lowerCamelCase = -1 for k_state in states_space: lowerCamelCase = probabilities[(k_state, final_observation)] if probability > max_probability: lowerCamelCase = probability lowerCamelCase = k_state lowerCamelCase = arg_max # Process pointers backwards lowerCamelCase = last_state lowerCamelCase = [] for o in range(len(snake_case__ ) - 1 , -1 , -1 ): result.append(snake_case__ ) lowerCamelCase = pointers[previous, observations_space[o]] result.reverse() return result def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ) -> None: _validate_not_empty( snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ) _validate_lists(snake_case__ , snake_case__ ) _validate_dicts( snake_case__ , snake_case__ , snake_case__ ) def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ) -> None: if not all( [ observations_space, states_space, initial_probabilities, transition_probabilities, emission_probabilities, ] ): raise ValueError("""There's an empty parameter""" ) def a__ ( snake_case__ , snake_case__ ) -> None: _validate_list(snake_case__ , """observations_space""" ) _validate_list(snake_case__ , """states_space""" ) def a__ ( snake_case__ , snake_case__ ) -> None: if not isinstance(_object , snake_case__ ): lowerCamelCase = F'{var_name} must be a list' raise ValueError(snake_case__ ) else: for x in _object: if not isinstance(snake_case__ , snake_case__ ): lowerCamelCase = F'{var_name} must be a list of strings' raise ValueError(snake_case__ ) def a__ ( snake_case__ , snake_case__ , snake_case__ , ) -> None: _validate_dict(snake_case__ , """initial_probabilities""" , snake_case__ ) _validate_nested_dict(snake_case__ , """transition_probabilities""" ) _validate_nested_dict(snake_case__ , """emission_probabilities""" ) def a__ ( snake_case__ , snake_case__ ) -> None: _validate_dict(_object , snake_case__ , snake_case__ ) for x in _object.values(): _validate_dict(snake_case__ , snake_case__ , snake_case__ , snake_case__ ) def a__ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ = False ) -> None: if not isinstance(_object , snake_case__ ): lowerCamelCase = F'{var_name} must be a dict' raise ValueError(snake_case__ ) if not all(isinstance(snake_case__ , snake_case__ ) for x in _object ): lowerCamelCase = F'{var_name} all keys must be strings' raise ValueError(snake_case__ ) if not all(isinstance(snake_case__ , snake_case__ ) for x in _object.values() ): lowerCamelCase = """nested dictionary """ if nested else """""" lowerCamelCase = F'{var_name} {nested_text}all values must be {value_type.__name__}' raise ValueError(snake_case__ ) if __name__ == "__main__": from doctest import testmod testmod()
291
1
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices lowerCAmelCase : int = logging.get_logger(__name__) lowerCAmelCase : str = { """shi-labs/dinat-mini-in1k-224""": """https://huggingface.co/shi-labs/dinat-mini-in1k-224/resolve/main/config.json""", # See all Dinat models at https://huggingface.co/models?filter=dinat } class __magic_name__ ( UpperCAmelCase__ , UpperCAmelCase__ ): '''simple docstring''' __UpperCamelCase = "dinat" __UpperCamelCase = { "num_attention_heads": "num_heads", "num_hidden_layers": "num_layers", } def __init__( self , _a=4 , _a=3 , _a=64 , _a=[3, 4, 6, 5] , _a=[2, 4, 8, 16] , _a=7 , _a=[[1, 8, 1], [1, 4, 1, 4], [1, 2, 1, 2, 1, 2], [1, 1, 1, 1, 1]] , _a=3.0 , _a=True , _a=0.0 , _a=0.0 , _a=0.1 , _a="gelu" , _a=0.02 , _a=1e-5 , _a=0.0 , _a=None , _a=None , **_a , ): """simple docstring""" super().__init__(**_a ) lowerCamelCase = patch_size lowerCamelCase = num_channels lowerCamelCase = embed_dim lowerCamelCase = depths lowerCamelCase = len(_a ) lowerCamelCase = num_heads lowerCamelCase = kernel_size lowerCamelCase = dilations lowerCamelCase = mlp_ratio lowerCamelCase = qkv_bias lowerCamelCase = hidden_dropout_prob lowerCamelCase = attention_probs_dropout_prob lowerCamelCase = drop_path_rate lowerCamelCase = hidden_act lowerCamelCase = layer_norm_eps lowerCamelCase = initializer_range # we set the hidden_size attribute in order to make Dinat work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model lowerCamelCase = int(embed_dim * 2 ** (len(_a ) - 1) ) lowerCamelCase = layer_scale_init_value lowerCamelCase = ["""stem"""] + [f'stage{idx}' for idx in range(1 , len(_a ) + 1 )] lowerCamelCase , lowerCamelCase = get_aligned_output_features_output_indices( out_features=_a , out_indices=_a , stage_names=self.stage_names )
291
"""simple docstring""" import argparse from pathlib import Path import torch from transformers import OPTConfig, OPTModel from transformers.utils import logging logging.set_verbosity_info() lowerCAmelCase : Dict = logging.get_logger(__name__) def a__ ( snake_case__ ) -> Dict: lowerCamelCase = torch.load(snake_case__ , map_location="""cpu""" ) if "model" in sd.keys(): lowerCamelCase = torch.load(snake_case__ , map_location="""cpu""" )["""model"""] # pop unnecessary weights lowerCamelCase = [ """decoder.version""", """decoder.output_projection.weight""", ] for key in keys_to_delete: if key in sd: sd.pop(snake_case__ ) lowerCamelCase = { """decoder.project_in_dim.weight""": """decoder.project_in.weight""", """decoder.project_out_dim.weight""": """decoder.project_out.weight""", """decoder.layer_norm.weight""": """decoder.final_layer_norm.weight""", """decoder.layer_norm.bias""": """decoder.final_layer_norm.bias""", } for old_key, new_key in keys_to_rename.items(): if old_key in sd: lowerCamelCase = sd.pop(snake_case__ ) lowerCamelCase = list(sd.keys() ) for key in keys: if ".qkv_proj." in key: lowerCamelCase = sd[key] # We split QKV in separate Q,K,V lowerCamelCase = key.replace(""".qkv_proj.""" , """.q_proj.""" ) lowerCamelCase = key.replace(""".qkv_proj.""" , """.k_proj.""" ) lowerCamelCase = key.replace(""".qkv_proj.""" , """.v_proj.""" ) lowerCamelCase = value.shape[0] assert depth % 3 == 0 # `SequeuceParallelTransformerBlock` has QKV weight is separated in K,V,Q despite the naming: # https://cs.github.com/facebookresearch/metaseq/blob/51871bd73cd04c038f239ea2a26db1d7f6b37927/metaseq/modules/sequence_parallel_transformer_layer.py#L97 lowerCamelCase , lowerCamelCase , lowerCamelCase = torch.split(snake_case__ , depth // 3 , dim=0 ) lowerCamelCase = q lowerCamelCase = k lowerCamelCase = v del sd[key] return sd @torch.no_grad() def a__ ( snake_case__ , snake_case__ , snake_case__=None ) -> Tuple: lowerCamelCase = load_checkpoint(snake_case__ ) if config is not None: lowerCamelCase = OPTConfig.from_pretrained(snake_case__ ) else: lowerCamelCase = OPTConfig() lowerCamelCase = OPTModel(snake_case__ ).half().eval() model.load_state_dict(snake_case__ ) # Check results Path(snake_case__ ).mkdir(exist_ok=snake_case__ ) model.save_pretrained(snake_case__ ) if __name__ == "__main__": lowerCAmelCase : List[str] = argparse.ArgumentParser() # Required parameters parser.add_argument( """--fairseq_path""", type=str, help=( """path to fairseq checkpoint in correct format. You can find all checkpoints in the correct format here:""" """ https://huggingface.co/models?other=opt_metasq""" ), ) parser.add_argument("""--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""") parser.add_argument("""--hf_config""", default=None, type=str, help="""Define HF config.""") lowerCAmelCase : Optional[Any] = parser.parse_args() convert_opt_checkpoint(args.fairseq_path, args.pytorch_dump_folder_path, config=args.hf_config)
291
1
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase : Tuple = logging.get_logger(__name__) lowerCAmelCase : List[Any] = { """MIT/ast-finetuned-audioset-10-10-0.4593""": ( """https://huggingface.co/MIT/ast-finetuned-audioset-10-10-0.4593/resolve/main/config.json""" ), } class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' __UpperCamelCase = "audio-spectrogram-transformer" def __init__( self , _a=768 , _a=12 , _a=12 , _a=3_072 , _a="gelu" , _a=0.0 , _a=0.0 , _a=0.02 , _a=1e-1_2 , _a=16 , _a=True , _a=10 , _a=10 , _a=1_024 , _a=128 , **_a , ): """simple docstring""" super().__init__(**_a ) lowerCamelCase = hidden_size lowerCamelCase = num_hidden_layers lowerCamelCase = num_attention_heads lowerCamelCase = intermediate_size lowerCamelCase = hidden_act lowerCamelCase = hidden_dropout_prob lowerCamelCase = attention_probs_dropout_prob lowerCamelCase = initializer_range lowerCamelCase = layer_norm_eps lowerCamelCase = patch_size lowerCamelCase = qkv_bias lowerCamelCase = frequency_stride lowerCamelCase = time_stride lowerCamelCase = max_length lowerCamelCase = num_mel_bins
291
"""simple docstring""" import json import os import shutil import tempfile import unittest import numpy as np from transformers import BertTokenizerFast from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES, BertTokenizer from transformers.testing_utils import require_tokenizers, require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import VisionTextDualEncoderProcessor, ViTImageProcessor @require_tokenizers @require_vision class __magic_name__ ( unittest.TestCase ): '''simple docstring''' def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = tempfile.mkdtemp() # fmt: off lowerCamelCase = ["""[UNK]""", """[CLS]""", """[SEP]""", """[PAD]""", """[MASK]""", """want""", """##want""", """##ed""", """wa""", """un""", """runn""", """##ing""", """,""", """low""", """lowest"""] # fmt: on lowerCamelCase = 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] ) ) lowerCamelCase = { """do_resize""": True, """size""": {"""height""": 18, """width""": 18}, """do_normalize""": True, """image_mean""": [0.5, 0.5, 0.5], """image_std""": [0.5, 0.5, 0.5], } lowerCamelCase = os.path.join(self.tmpdirname , _a ) with open(self.image_processor_file , """w""" , encoding="""utf-8""" ) as fp: json.dump(_a , _a ) def _lowerCAmelCase ( self , **_a ): """simple docstring""" return BertTokenizer.from_pretrained(self.tmpdirname , **_a ) def _lowerCAmelCase ( self , **_a ): """simple docstring""" return ViTImageProcessor.from_pretrained(self.tmpdirname , **_a ) def _lowerCAmelCase ( self ): """simple docstring""" shutil.rmtree(self.tmpdirname ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = [np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta )] lowerCamelCase = [Image.fromarray(np.moveaxis(_a , 0 , -1 ) ) for x in image_inputs] return image_inputs def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.get_tokenizer() lowerCamelCase = self.get_image_processor() lowerCamelCase = VisionTextDualEncoderProcessor(tokenizer=_a , image_processor=_a ) processor.save_pretrained(self.tmpdirname ) lowerCamelCase = VisionTextDualEncoderProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer.get_vocab() ) self.assertIsInstance(processor.tokenizer , (BertTokenizer, BertTokenizerFast) ) self.assertEqual(processor.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertIsInstance(processor.image_processor , _a ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = VisionTextDualEncoderProcessor( tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) lowerCamelCase = self.get_tokenizer(bos_token="""(BOS)""" , eos_token="""(EOS)""" ) lowerCamelCase = self.get_image_processor(do_normalize=_a , padding_value=1.0 ) lowerCamelCase = VisionTextDualEncoderProcessor.from_pretrained( self.tmpdirname , bos_token="""(BOS)""" , eos_token="""(EOS)""" , do_normalize=_a , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , (BertTokenizer, BertTokenizerFast) ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , _a ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.get_image_processor() lowerCamelCase = self.get_tokenizer() lowerCamelCase = VisionTextDualEncoderProcessor(tokenizer=_a , image_processor=_a ) lowerCamelCase = self.prepare_image_inputs() lowerCamelCase = image_processor(_a , return_tensors="""np""" ) lowerCamelCase = processor(images=_a , return_tensors="""np""" ) for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() , input_processor[key].sum() , delta=1e-2 ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.get_image_processor() lowerCamelCase = self.get_tokenizer() lowerCamelCase = VisionTextDualEncoderProcessor(tokenizer=_a , image_processor=_a ) lowerCamelCase = """lower newer""" lowerCamelCase = processor(text=_a ) lowerCamelCase = tokenizer(_a ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.get_image_processor() lowerCamelCase = self.get_tokenizer() lowerCamelCase = VisionTextDualEncoderProcessor(tokenizer=_a , image_processor=_a ) lowerCamelCase = """lower newer""" lowerCamelCase = self.prepare_image_inputs() lowerCamelCase = processor(text=_a , images=_a ) self.assertListEqual(list(inputs.keys() ) , ["""input_ids""", """token_type_ids""", """attention_mask""", """pixel_values"""] ) # test if it raises when no input is passed with self.assertRaises(_a ): processor() def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.get_image_processor() lowerCamelCase = self.get_tokenizer() lowerCamelCase = VisionTextDualEncoderProcessor(tokenizer=_a , image_processor=_a ) lowerCamelCase = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] lowerCamelCase = processor.batch_decode(_a ) lowerCamelCase = tokenizer.batch_decode(_a ) self.assertListEqual(_a , _a ) def _lowerCAmelCase ( self ): """simple docstring""" lowerCamelCase = self.get_image_processor() lowerCamelCase = self.get_tokenizer() lowerCamelCase = VisionTextDualEncoderProcessor(tokenizer=_a , image_processor=_a ) lowerCamelCase = """lower newer""" lowerCamelCase = self.prepare_image_inputs() lowerCamelCase = processor(text=_a , images=_a ) self.assertListEqual(list(inputs.keys() ) , processor.model_input_names )
291
1
"""simple docstring""" from __future__ import annotations import math def a__ ( snake_case__ , snake_case__ ) -> list: if len(snake_case__ ) != 2 or len(a[0] ) != 2 or len(snake_case__ ) != 2 or len(b[0] ) != 2: raise Exception("""Matrices are not 2x2""" ) lowerCamelCase = [ [a[0][0] * b[0][0] + a[0][1] * b[1][0], a[0][0] * b[0][1] + a[0][1] * b[1][1]], [a[1][0] * b[0][0] + a[1][1] * b[1][0], a[1][0] * b[0][1] + a[1][1] * b[1][1]], ] return new_matrix def a__ ( snake_case__ , snake_case__ ) -> Union[str, Any]: return [ [matrix_a[row][col] + matrix_b[row][col] for col in range(len(matrix_a[row] ) )] for row in range(len(snake_case__ ) ) ] def a__ ( snake_case__ , snake_case__ ) -> List[str]: return [ [matrix_a[row][col] - matrix_b[row][col] for col in range(len(matrix_a[row] ) )] for row in range(len(snake_case__ ) ) ] def a__ ( snake_case__ ) -> tuple[list, list, list, list]: if len(snake_case__ ) % 2 != 0 or len(a[0] ) % 2 != 0: raise Exception("""Odd matrices are not supported!""" ) lowerCamelCase = len(snake_case__ ) lowerCamelCase = matrix_length // 2 lowerCamelCase = [[a[i][j] for j in range(snake_case__ , snake_case__ )] for i in range(snake_case__ )] lowerCamelCase = [ [a[i][j] for j in range(snake_case__ , snake_case__ )] for i in range(snake_case__ , snake_case__ ) ] lowerCamelCase = [[a[i][j] for j in range(snake_case__ )] for i in range(snake_case__ )] lowerCamelCase = [[a[i][j] for j in range(snake_case__ )] for i in range(snake_case__ , snake_case__ )] return top_left, top_right, bot_left, bot_right def a__ ( snake_case__ ) -> tuple[int, int]: return len(snake_case__ ), len(matrix[0] ) def a__ ( snake_case__ ) -> None: print("""\n""".join(str(snake_case__ ) for line in matrix ) ) def a__ ( snake_case__ , snake_case__ ) -> list: if matrix_dimensions(snake_case__ ) == (2, 2): return default_matrix_multiplication(snake_case__ , snake_case__ ) lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase = split_matrix(snake_case__ ) lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase = split_matrix(snake_case__ ) lowerCamelCase = actual_strassen(snake_case__ , matrix_subtraction(snake_case__ , snake_case__ ) ) lowerCamelCase = actual_strassen(matrix_addition(snake_case__ , snake_case__ ) , snake_case__ ) lowerCamelCase = actual_strassen(matrix_addition(snake_case__ , snake_case__ ) , snake_case__ ) lowerCamelCase = actual_strassen(snake_case__ , matrix_subtraction(snake_case__ , snake_case__ ) ) lowerCamelCase = actual_strassen(matrix_addition(snake_case__ , snake_case__ ) , matrix_addition(snake_case__ , snake_case__ ) ) lowerCamelCase = actual_strassen(matrix_subtraction(snake_case__ , snake_case__ ) , matrix_addition(snake_case__ , snake_case__ ) ) lowerCamelCase = actual_strassen(matrix_subtraction(snake_case__ , snake_case__ ) , matrix_addition(snake_case__ , snake_case__ ) ) lowerCamelCase = matrix_addition(matrix_subtraction(matrix_addition(snake_case__ , snake_case__ ) , snake_case__ ) , snake_case__ ) lowerCamelCase = matrix_addition(snake_case__ , snake_case__ ) lowerCamelCase = matrix_addition(snake_case__ , snake_case__ ) lowerCamelCase = matrix_subtraction(matrix_subtraction(matrix_addition(snake_case__ , snake_case__ ) , snake_case__ ) , snake_case__ ) # construct the new matrix from our 4 quadrants lowerCamelCase = [] for i in range(len(snake_case__ ) ): new_matrix.append(top_left[i] + top_right[i] ) for i in range(len(snake_case__ ) ): new_matrix.append(bot_left[i] + bot_right[i] ) return new_matrix def a__ ( snake_case__ , snake_case__ ) -> list: if matrix_dimensions(snake_case__ )[1] != matrix_dimensions(snake_case__ )[0]: lowerCamelCase = ( """Unable to multiply these matrices, please check the dimensions.\n""" F'Matrix A: {matrixa}\n' F'Matrix B: {matrixa}' ) raise Exception(snake_case__ ) lowerCamelCase = matrix_dimensions(snake_case__ ) lowerCamelCase = matrix_dimensions(snake_case__ ) if dimensiona[0] == dimensiona[1] and dimensiona[0] == dimensiona[1]: return [matrixa, matrixa] lowerCamelCase = max(*snake_case__ , *snake_case__ ) lowerCamelCase = int(math.pow(2 , math.ceil(math.loga(snake_case__ ) ) ) ) lowerCamelCase = matrixa lowerCamelCase = matrixa # Adding zeros to the matrices so that the arrays dimensions are the same and also # power of 2 for i in range(0 , snake_case__ ): if i < dimensiona[0]: for _ in range(dimensiona[1] , snake_case__ ): new_matrixa[i].append(0 ) else: new_matrixa.append([0] * maxim ) if i < dimensiona[0]: for _ in range(dimensiona[1] , snake_case__ ): new_matrixa[i].append(0 ) else: new_matrixa.append([0] * maxim ) lowerCamelCase = actual_strassen(snake_case__ , snake_case__ ) # Removing the additional zeros for i in range(0 , snake_case__ ): if i < dimensiona[0]: for _ in range(dimensiona[1] , snake_case__ ): final_matrix[i].pop() else: final_matrix.pop() return final_matrix if __name__ == "__main__": lowerCAmelCase : Optional[int] = [ [2, 3, 4, 5], [6, 4, 3, 1], [2, 3, 6, 7], [3, 1, 2, 4], [2, 3, 4, 5], [6, 4, 3, 1], [2, 3, 6, 7], [3, 1, 2, 4], [2, 3, 4, 5], [6, 2, 3, 1], ] lowerCAmelCase : Optional[Any] = [[0, 2, 1, 1], [16, 2, 3, 3], [2, 2, 7, 7], [13, 11, 22, 4]] print(strassen(matrixa, matrixa))
291
"""simple docstring""" import importlib import sys from argparse import REMAINDER, ArgumentParser from pathlib import Path import torch_xla.distributed.xla_multiprocessing as xmp def a__ ( ) -> Union[str, Any]: lowerCamelCase = ArgumentParser( description=( """PyTorch TPU distributed training launch """ """helper utility that will spawn up """ """multiple distributed processes""" ) ) # Optional arguments for the launch helper parser.add_argument("""--num_cores""" , type=snake_case__ , default=1 , help="""Number of TPU cores to use (1 or 8).""" ) # positional parser.add_argument( """training_script""" , type=snake_case__ , help=( """The full path to the single TPU training """ """program/script to be launched in parallel, """ """followed by all the arguments for the """ """training script""" ) , ) # rest from the training program parser.add_argument("""training_script_args""" , nargs=snake_case__ ) return parser.parse_args() def a__ ( ) -> List[str]: lowerCamelCase = parse_args() # Import training_script as a module. lowerCamelCase = Path(args.training_script ) sys.path.append(str(script_fpath.parent.resolve() ) ) lowerCamelCase = script_fpath.stem lowerCamelCase = importlib.import_module(snake_case__ ) # Patch sys.argv lowerCamelCase = [args.training_script] + args.training_script_args + ["""--tpu_num_cores""", str(args.num_cores )] xmp.spawn(mod._mp_fn , args=() , nprocs=args.num_cores ) if __name__ == "__main__": main()
291
1
"""simple docstring""" 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__ ( snake_case__ ) -> Union[str, Any]: lowerCamelCase = filter(lambda snake_case__ : p.requires_grad , model.parameters() ) lowerCamelCase = sum([np.prod(p.size() ) for p in model_parameters] ) return params lowerCAmelCase : Union[str, Any] = logging.getLogger(__name__) def a__ ( snake_case__ , snake_case__ ) -> Optional[int]: if metric == "rouge2": lowerCamelCase = """{val_avg_rouge2:.4f}-{step_count}""" elif metric == "bleu": lowerCamelCase = """{val_avg_bleu:.4f}-{step_count}""" elif metric == "em": lowerCamelCase = """{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.""" ) lowerCamelCase = ModelCheckpoint( dirpath=snake_case__ , filename=snake_case__ , monitor=F'val_{metric}' , mode="""max""" , save_top_k=3 , every_n_epochs=1 , ) return checkpoint_callback def a__ ( snake_case__ , snake_case__ ) -> List[str]: return EarlyStopping( monitor=F'val_{metric}' , mode="""min""" if """loss""" in metric else """max""" , patience=snake_case__ , verbose=snake_case__ , ) class __magic_name__ ( pl.Callback ): '''simple docstring''' def _lowerCAmelCase ( self , _a , _a ): """simple docstring""" lowerCamelCase = {f'lr_group_{i}': param["""lr"""] for i, param in enumerate(pl_module.trainer.optimizers[0].param_groups )} pl_module.logger.log_metrics(_a ) @rank_zero_only def _lowerCAmelCase ( self , _a , _a , _a , _a=True ): """simple docstring""" logger.info(f'***** {type_path} results at step {trainer.global_step:05d} *****' ) lowerCamelCase = 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 lowerCamelCase = Path(pl_module.hparams.output_dir ) if type_path == "test": lowerCamelCase = od / """test_results.txt""" lowerCamelCase = 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. lowerCamelCase = od / f'{type_path}_results/{trainer.global_step:05d}.txt' lowerCamelCase = od / f'{type_path}_generations/{trainer.global_step:05d}.txt' results_file.parent.mkdir(exist_ok=_a ) generations_file.parent.mkdir(exist_ok=_a ) with open(_a , """a+""" ) as writer: for key in sorted(_a ): if key in ["log", "progress_bar", "preds"]: continue lowerCamelCase = metrics[key] if isinstance(_a , torch.Tensor ): lowerCamelCase = val.item() lowerCamelCase = f'{key}: {val:.6f}\n' writer.write(_a ) if not save_generations: return if "preds" in metrics: lowerCamelCase = """\n""".join(metrics["""preds"""] ) generations_file.open("""w+""" ).write(_a ) @rank_zero_only def _lowerCAmelCase ( self , _a , _a ): """simple docstring""" try: lowerCamelCase = pl_module.model.model.num_parameters() except AttributeError: lowerCamelCase = pl_module.model.num_parameters() lowerCamelCase = count_trainable_parameters(_a ) # 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 _lowerCAmelCase ( self , _a , _a ): """simple docstring""" save_json(pl_module.metrics , pl_module.metrics_save_path ) return self._write_logs(_a , _a , """test""" ) @rank_zero_only def _lowerCAmelCase ( self , _a , _a ): """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")
291
"""simple docstring""" import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase : int = logging.get_logger(__name__) lowerCAmelCase : List[str] = { """asapp/sew-d-tiny-100k""": """https://huggingface.co/asapp/sew-d-tiny-100k/resolve/main/config.json""", # See all SEW-D models at https://huggingface.co/models?filter=sew-d } class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' __UpperCamelCase = "sew-d" def __init__( self , _a=32 , _a=768 , _a=12 , _a=12 , _a=3_072 , _a=2 , _a=512 , _a=256 , _a=True , _a=True , _a=("p2c", "c2p") , _a="layer_norm" , _a="gelu_python" , _a=0.1 , _a=0.1 , _a=0.1 , _a=0.0 , _a=0.1 , _a=0.02 , _a=1e-7 , _a=1e-5 , _a="group" , _a="gelu" , _a=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512) , _a=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1) , _a=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1) , _a=False , _a=128 , _a=16 , _a=True , _a=0.05 , _a=10 , _a=2 , _a=0.0 , _a=10 , _a=0 , _a="mean" , _a=False , _a=False , _a=256 , _a=0 , _a=1 , _a=2 , **_a , ): """simple docstring""" super().__init__(**_a , pad_token_id=_a , bos_token_id=_a , eos_token_id=_a ) lowerCamelCase = hidden_size lowerCamelCase = feat_extract_norm lowerCamelCase = feat_extract_activation lowerCamelCase = list(_a ) lowerCamelCase = list(_a ) lowerCamelCase = list(_a ) lowerCamelCase = conv_bias lowerCamelCase = num_conv_pos_embeddings lowerCamelCase = num_conv_pos_embedding_groups lowerCamelCase = len(self.conv_dim ) lowerCamelCase = num_hidden_layers lowerCamelCase = intermediate_size lowerCamelCase = squeeze_factor lowerCamelCase = max_position_embeddings lowerCamelCase = position_buckets lowerCamelCase = share_att_key lowerCamelCase = relative_attention lowerCamelCase = norm_rel_ebd lowerCamelCase = list(_a ) lowerCamelCase = hidden_act lowerCamelCase = num_attention_heads lowerCamelCase = hidden_dropout lowerCamelCase = attention_dropout lowerCamelCase = activation_dropout lowerCamelCase = feat_proj_dropout lowerCamelCase = final_dropout lowerCamelCase = layer_norm_eps lowerCamelCase = feature_layer_norm_eps lowerCamelCase = initializer_range lowerCamelCase = vocab_size if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( """Configuration for convolutional layers is incorrect.""" """It is required that `len(config.conv_dim)` == `len(config.conv_stride)` == `len(config.conv_kernel)`,""" f'but is `len(config.conv_dim) = {len(self.conv_dim )}`, `len(config.conv_stride)' f'= {len(self.conv_stride )}`, `len(config.conv_kernel) = {len(self.conv_kernel )}`.' ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 lowerCamelCase = apply_spec_augment lowerCamelCase = mask_time_prob lowerCamelCase = mask_time_length lowerCamelCase = mask_time_min_masks lowerCamelCase = mask_feature_prob lowerCamelCase = mask_feature_length lowerCamelCase = mask_feature_min_masks # ctc loss lowerCamelCase = ctc_loss_reduction lowerCamelCase = ctc_zero_infinity # sequence classification lowerCamelCase = use_weighted_layer_sum lowerCamelCase = classifier_proj_size @property def _lowerCAmelCase ( self ): """simple docstring""" return functools.reduce(operator.mul , self.conv_stride , 1 )
291
1
"""simple docstring""" import numpy as np import torch from imwatermark import WatermarkEncoder # Copied from https://github.com/Stability-AI/generative-models/blob/613af104c6b85184091d42d374fef420eddb356d/scripts/demo/streamlit_helpers.py#L66 lowerCAmelCase : List[Any] = 0b101100111110110010010000011110111011000110011110 # bin(x)[2:] gives bits of x as str, use int to convert them to 0/1 lowerCAmelCase : List[str] = [int(bit) for bit in bin(WATERMARK_MESSAGE)[2:]] class __magic_name__ : '''simple docstring''' def __init__( self ): """simple docstring""" lowerCamelCase = WATERMARK_BITS lowerCamelCase = WatermarkEncoder() self.encoder.set_watermark("""bits""" , self.watermark ) def _lowerCAmelCase ( self , _a ): """simple docstring""" # can't encode images that are smaller than 256 if images.shape[-1] < 256: return images lowerCamelCase = (255 * (images / 2 + 0.5)).cpu().permute(0 , 2 , 3 , 1 ).float().numpy() lowerCamelCase = [self.encoder.encode(_a , """dwtDct""" ) for image in images] lowerCamelCase = torch.from_numpy(np.array(_a ) ).permute(0 , 3 , 1 , 2 ) lowerCamelCase = torch.clamp(2 * (images / 255 - 0.5) , min=-1.0 , max=1.0 ) return images
291
"""simple docstring""" from sklearn.metrics import recall_score import datasets lowerCAmelCase : Any = """ Recall is the fraction of the positive examples that were correctly labeled by the model as positive. It can be computed with the equation: Recall = TP / (TP + FN) Where TP is the true positives and FN is the false negatives. """ lowerCAmelCase : Any = """ Args: - **predictions** (`list` of `int`): The predicted labels. - **references** (`list` of `int`): The ground truth labels. - **labels** (`list` of `int`): The set of labels to include when `average` is not set to `binary`, and their order when average is `None`. Labels present in the data can be excluded in this input, for example to calculate a multiclass average ignoring a majority negative class, while labels not present in the data will result in 0 components in a macro average. For multilabel targets, labels are column indices. By default, all labels in y_true and y_pred are used in sorted order. Defaults to None. - **pos_label** (`int`): The class label to use as the 'positive class' when calculating the recall. Defaults to `1`. - **average** (`string`): This parameter is required for multiclass/multilabel targets. If None, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data. Defaults to `'binary'`. - `'binary'`: Only report results for the class specified by `pos_label`. This is applicable only if the target labels and predictions are binary. - `'micro'`: Calculate metrics globally by counting the total true positives, false negatives, and false positives. - `'macro'`: Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account. - `'weighted'`: Calculate metrics for each label, and find their average weighted by support (the number of true instances for each label). This alters `'macro'` to account for label imbalance. Note that it can result in an F-score that is not between precision and recall. - `'samples'`: Calculate metrics for each instance, and find their average (only meaningful for multilabel classification). - **sample_weight** (`list` of `float`): Sample weights Defaults to `None`. - **zero_division** (): Sets the value to return when there is a zero division. Defaults to . - `'warn'`: If there is a zero division, the return value is `0`, but warnings are also raised. - `0`: If there is a zero division, the return value is `0`. - `1`: If there is a zero division, the return value is `1`. Returns: - **recall** (`float`, or `array` of `float`): Either the general recall score, or the recall scores for individual classes, depending on the values input to `labels` and `average`. Minimum possible value is 0. Maximum possible value is 1. A higher recall means that more of the positive examples have been labeled correctly. Therefore, a higher recall is generally considered better. Examples: Example 1-A simple example with some errors >>> recall_metric = datasets.load_metric('recall') >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1]) >>> print(results) {'recall': 0.6666666666666666} Example 2-The same example as Example 1, but with `pos_label=0` instead of the default `pos_label=1`. >>> recall_metric = datasets.load_metric('recall') >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1], pos_label=0) >>> print(results) {'recall': 0.5} Example 3-The same example as Example 1, but with `sample_weight` included. >>> recall_metric = datasets.load_metric('recall') >>> sample_weight = [0.9, 0.2, 0.9, 0.3, 0.8] >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1], sample_weight=sample_weight) >>> print(results) {'recall': 0.55} Example 4-A multiclass example, using different averages. >>> recall_metric = datasets.load_metric('recall') >>> predictions = [0, 2, 1, 0, 0, 1] >>> references = [0, 1, 2, 0, 1, 2] >>> results = recall_metric.compute(predictions=predictions, references=references, average='macro') >>> print(results) {'recall': 0.3333333333333333} >>> results = recall_metric.compute(predictions=predictions, references=references, average='micro') >>> print(results) {'recall': 0.3333333333333333} >>> results = recall_metric.compute(predictions=predictions, references=references, average='weighted') >>> print(results) {'recall': 0.3333333333333333} >>> results = recall_metric.compute(predictions=predictions, references=references, average=None) >>> print(results) {'recall': array([1., 0., 0.])} """ lowerCAmelCase : Any = """ @article{scikit-learn, title={Scikit-learn: Machine Learning in {P}ython}, author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V. and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P. and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.}, journal={Journal of Machine Learning Research}, volume={12}, pages={2825--2830}, year={2011} """ @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __magic_name__ ( datasets.Metric ): '''simple docstring''' def _lowerCAmelCase ( self ): """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": datasets.Sequence(datasets.Value("""int32""" ) ), """references""": datasets.Sequence(datasets.Value("""int32""" ) ), } if self.config_name == """multilabel""" else { """predictions""": datasets.Value("""int32""" ), """references""": datasets.Value("""int32""" ), } ) , reference_urls=["""https://scikit-learn.org/stable/modules/generated/sklearn.metrics.recall_score.html"""] , ) def _lowerCAmelCase ( self , _a , _a , _a=None , _a=1 , _a="binary" , _a=None , _a="warn" , ): """simple docstring""" lowerCamelCase = recall_score( _a , _a , labels=_a , pos_label=_a , average=_a , sample_weight=_a , zero_division=_a , ) return {"recall": float(_a ) if score.size == 1 else score}
291
1
"""simple docstring""" import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_xlnet import XLNetTokenizer else: lowerCAmelCase : int = None lowerCAmelCase : Tuple = logging.get_logger(__name__) lowerCAmelCase : Tuple = {"""vocab_file""": """spiece.model""", """tokenizer_file""": """tokenizer.json"""} lowerCAmelCase : Union[str, Any] = { """vocab_file""": { """xlnet-base-cased""": """https://huggingface.co/xlnet-base-cased/resolve/main/spiece.model""", """xlnet-large-cased""": """https://huggingface.co/xlnet-large-cased/resolve/main/spiece.model""", }, """tokenizer_file""": { """xlnet-base-cased""": """https://huggingface.co/xlnet-base-cased/resolve/main/tokenizer.json""", """xlnet-large-cased""": """https://huggingface.co/xlnet-large-cased/resolve/main/tokenizer.json""", }, } lowerCAmelCase : Optional[int] = { """xlnet-base-cased""": None, """xlnet-large-cased""": None, } lowerCAmelCase : Union[str, Any] = """▁""" # Segments (not really needed) lowerCAmelCase : str = 0 lowerCAmelCase : Optional[int] = 1 lowerCAmelCase : Tuple = 2 lowerCAmelCase : Optional[Any] = 3 lowerCAmelCase : List[Any] = 4 class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' __UpperCamelCase = VOCAB_FILES_NAMES __UpperCamelCase = PRETRAINED_VOCAB_FILES_MAP __UpperCamelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCamelCase = "left" __UpperCamelCase = XLNetTokenizer def __init__( self , _a=None , _a=None , _a=False , _a=True , _a=False , _a="<s>" , _a="</s>" , _a="<unk>" , _a="<sep>" , _a="<pad>" , _a="<cls>" , _a="<mask>" , _a=["<eop>", "<eod>"] , **_a , ): """simple docstring""" # Mask token behave like a normal word, i.e. include the space before it lowerCamelCase = AddedToken(_a , lstrip=_a , rstrip=_a ) if isinstance(_a , _a ) else mask_token super().__init__( vocab_file=_a , tokenizer_file=_a , do_lower_case=_a , remove_space=_a , keep_accents=_a , bos_token=_a , eos_token=_a , unk_token=_a , sep_token=_a , pad_token=_a , cls_token=_a , mask_token=_a , additional_special_tokens=_a , **_a , ) lowerCamelCase = 3 lowerCamelCase = do_lower_case lowerCamelCase = remove_space lowerCamelCase = keep_accents lowerCamelCase = vocab_file lowerCamelCase = False if not self.vocab_file else True def _lowerCAmelCase ( self , _a , _a = None ): """simple docstring""" lowerCamelCase = [self.sep_token_id] lowerCamelCase = [self.cls_token_id] if token_ids_a is None: return token_ids_a + sep + cls return token_ids_a + sep + token_ids_a + sep + cls def _lowerCAmelCase ( self , _a , _a = None ): """simple docstring""" lowerCamelCase = [self.sep_token_id] lowerCamelCase = [2] if token_ids_a is None: return len(token_ids_a + sep ) * [0] + cls_segment_id return len(token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] + cls_segment_id def _lowerCAmelCase ( self , _a , _a = 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(_a ): logger.error(f'Vocabulary path ({save_directory}) should be a directory' ) return lowerCamelCase = os.path.join( _a , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_a ): copyfile(self.vocab_file , _a ) return (out_vocab_file,)
291
"""simple docstring""" import numpy as np import torch from torch.utils.data import Dataset, IterableDataset from ..utils.generic import ModelOutput class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' def __init__( self , _a , _a , _a ): """simple docstring""" lowerCamelCase = dataset lowerCamelCase = process lowerCamelCase = params def __len__( self ): """simple docstring""" return len(self.dataset ) def __getitem__( self , _a ): """simple docstring""" lowerCamelCase = self.dataset[i] lowerCamelCase = self.process(_a , **self.params ) return processed class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' def __init__( self , _a , _a , _a , _a=None ): """simple docstring""" lowerCamelCase = loader lowerCamelCase = infer lowerCamelCase = params if loader_batch_size == 1: # Let's spare some time by deactivating altogether lowerCamelCase = None lowerCamelCase = loader_batch_size # Internal bookkeeping lowerCamelCase = None lowerCamelCase = None def __len__( self ): """simple docstring""" return len(self.loader ) def __iter__( self ): """simple docstring""" lowerCamelCase = iter(self.loader ) return self def _lowerCAmelCase ( self ): """simple docstring""" if isinstance(self._loader_batch_data , torch.Tensor ): # Batch data is simple tensor, just fetch the slice lowerCamelCase = self._loader_batch_data[self._loader_batch_index] else: # Batch data is assumed to be BaseModelOutput (or dict) lowerCamelCase = {} for k, element in self._loader_batch_data.items(): if isinstance(_a , _a ): # Convert ModelOutput to tuple first lowerCamelCase = element.to_tuple() if isinstance(element[0] , torch.Tensor ): lowerCamelCase = tuple(el[self._loader_batch_index].unsqueeze(0 ) for el in element ) elif isinstance(element[0] , np.ndarray ): lowerCamelCase = tuple(np.expand_dims(el[self._loader_batch_index] , 0 ) for el in element ) continue if k in {"hidden_states", "past_key_values", "attentions"} and isinstance(_a , _a ): # Those are stored as lists of tensors so need specific unbatching. if isinstance(element[0] , torch.Tensor ): lowerCamelCase = tuple(el[self._loader_batch_index].unsqueeze(0 ) for el in element ) elif isinstance(element[0] , np.ndarray ): lowerCamelCase = tuple(np.expand_dims(el[self._loader_batch_index] , 0 ) for el in element ) continue if element is None: # This can happen for optional data that get passed around lowerCamelCase = None elif isinstance(element[self._loader_batch_index] , torch.Tensor ): # Take correct batch data, but make it looked like batch_size=1 # For compatibility with other methods within transformers lowerCamelCase = element[self._loader_batch_index].unsqueeze(0 ) elif isinstance(element[self._loader_batch_index] , np.ndarray ): # Take correct batch data, but make it looked like batch_size=1 # For compatibility with other methods within transformers lowerCamelCase = np.expand_dims(element[self._loader_batch_index] , 0 ) else: # This is typically a list, so no need to `unsqueeze`. lowerCamelCase = element[self._loader_batch_index] # Recreate the element by reusing the original class to make it look # batch_size=1 lowerCamelCase = self._loader_batch_data.__class__(_a ) self._loader_batch_index += 1 return result def _lowerCAmelCase ( self ): """simple docstring""" if self._loader_batch_index is not None and self._loader_batch_index < self.loader_batch_size: # We are currently unrolling a batch so we just need to return # the current item within a batch return self.loader_batch_item() # We're out of items within a batch lowerCamelCase = next(self.iterator ) lowerCamelCase = self.infer(_a , **self.params ) # We now have a batch of "inferred things". if self.loader_batch_size is not None: # Try to infer the size of the batch if isinstance(_a , torch.Tensor ): lowerCamelCase = processed else: lowerCamelCase = list(processed.keys() )[0] lowerCamelCase = processed[key] if isinstance(_a , _a ): lowerCamelCase = len(_a ) else: lowerCamelCase = first_tensor.shape[0] if 0 < observed_batch_size < self.loader_batch_size: # could be last batch so we can't unroll as many # elements. lowerCamelCase = observed_batch_size # Setting internal index to unwrap the batch lowerCamelCase = processed lowerCamelCase = 0 return self.loader_batch_item() else: # We're not unrolling batches return processed class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' def __init__( self , _a , _a , _a , _a=None ): """simple docstring""" super().__init__(_a , _a , _a ) def __iter__( self ): """simple docstring""" lowerCamelCase = iter(self.loader ) lowerCamelCase = None return self def _lowerCAmelCase ( self ): """simple docstring""" if self.subiterator is None: lowerCamelCase = self.infer(next(self.iterator ) , **self.params ) try: # Try to return next item lowerCamelCase = next(self.subiterator ) except StopIteration: # When a preprocess iterator ends, we can start lookig at the next item # ChunkIterator will keep feeding until ALL elements of iterator # all have created their subiterator and have been iterating against. # # Another way to look at it, is we're basically flattening lists of lists # into a single list, but with generators lowerCamelCase = self.infer(next(self.iterator ) , **self.params ) lowerCamelCase = next(self.subiterator ) return processed class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' def __iter__( self ): """simple docstring""" lowerCamelCase = iter(self.loader ) return self def _lowerCAmelCase ( self ): """simple docstring""" # Extremely similar to PipelineIterator in its unpacking mechanism # BUT, we have an extra required item which is the presence of `is_last` # That is because everything is flattened by `PipelineChunkIterator` we # need to keep track of how to regroup here in the original `process` # boundaries so that `process` and `postprocess` see the same data. # This iterator accumulates items (possibly while unbatching) until it # its a `is_last` and then just passes it on to the caller. lowerCamelCase = False lowerCamelCase = [] if self._loader_batch_index is not None and self._loader_batch_index < self.loader_batch_size: while self._loader_batch_index < self.loader_batch_size: lowerCamelCase = self.loader_batch_item() lowerCamelCase = item.pop("""is_last""" ) accumulator.append(_a ) if is_last: return accumulator while not is_last: lowerCamelCase = self.infer(next(self.iterator ) , **self.params ) if self.loader_batch_size is not None: if isinstance(_a , torch.Tensor ): lowerCamelCase = processed else: lowerCamelCase = list(processed.keys() )[0] lowerCamelCase = processed[key] if isinstance(_a , _a ): lowerCamelCase = len(_a ) else: lowerCamelCase = first_tensor.shape[0] if 0 < observed_batch_size < self.loader_batch_size: # could be last batch so we can't unroll as many # elements. lowerCamelCase = observed_batch_size lowerCamelCase = processed lowerCamelCase = 0 while self._loader_batch_index < self.loader_batch_size: lowerCamelCase = self.loader_batch_item() lowerCamelCase = item.pop("""is_last""" ) accumulator.append(_a ) if is_last: return accumulator else: lowerCamelCase = processed lowerCamelCase = item.pop("""is_last""" ) accumulator.append(_a ) return accumulator class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' def __init__( self , _a , _a ): """simple docstring""" lowerCamelCase = dataset lowerCamelCase = key def __len__( self ): """simple docstring""" return len(self.dataset ) def __getitem__( self , _a ): """simple docstring""" return self.dataset[i][self.key] class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' def __init__( self , _a , _a , _a ): """simple docstring""" lowerCamelCase = dataset lowerCamelCase = keya lowerCamelCase = keya def __len__( self ): """simple docstring""" return len(self.dataset ) def __getitem__( self , _a ): """simple docstring""" return {"text": self.dataset[i][self.keya], "text_pair": self.dataset[i][self.keya]}
291
1