code
stringlengths
82
54.1k
code_codestyle
int64
0
699
style_context
stringlengths
111
35.6k
style_context_codestyle
int64
0
699
label
int64
0
1
'''simple docstring''' from __future__ import annotations lowerCAmelCase : List[str] = { 'A': ['B', 'C', 'E'], 'B': ['A', 'D', 'E'], 'C': ['A', 'F', 'G'], 'D': ['B'], 'E': ['A', 'B', 'D'], 'F': ['C'], 'G': ['C'], } class SCREAMING_SNAKE_CASE__ : def __init__( self , A_ , A_ )-> None: '''simple docstring''' UpperCamelCase = graph # mapping node to its parent in resulting breadth first tree UpperCamelCase = {} UpperCamelCase = source_vertex def UpperCAmelCase_ ( self )-> None: '''simple docstring''' UpperCamelCase = {self.source_vertex} UpperCamelCase = None UpperCamelCase = [self.source_vertex] # first in first out queue while queue: UpperCamelCase = queue.pop(0 ) for adjacent_vertex in self.graph[vertex]: if adjacent_vertex not in visited: visited.add(A_ ) UpperCamelCase = vertex queue.append(A_ ) def UpperCAmelCase_ ( self , A_ )-> str: '''simple docstring''' if target_vertex == self.source_vertex: return self.source_vertex UpperCamelCase = self.parent.get(A_ ) if target_vertex_parent is None: UpperCamelCase = ( F'''No path from vertex: {self.source_vertex} to vertex: {target_vertex}''' ) raise ValueError(A_ ) return self.shortest_path(A_ ) + F'''->{target_vertex}''' if __name__ == "__main__": lowerCAmelCase : Any = Graph(graph, 'G') g.breath_first_search() print(g.shortest_path('D')) print(g.shortest_path('G')) print(g.shortest_path('Foo'))
3
'''simple docstring''' import inspect import os import unittest from dataclasses import dataclass import torch from accelerate import Accelerator, DistributedDataParallelKwargs, GradScalerKwargs from accelerate.state import AcceleratorState from accelerate.test_utils import execute_subprocess_async, require_cuda, require_multi_gpu from accelerate.utils import KwargsHandler @dataclass class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = 0 lowerCAmelCase_ = False lowerCAmelCase_ = 3.0 class SCREAMING_SNAKE_CASE__ ( unittest.TestCase): def UpperCAmelCase_ ( self )-> int: '''simple docstring''' self.assertDictEqual(MockClass().to_kwargs() , {} ) self.assertDictEqual(MockClass(a=2 ).to_kwargs() , {'a': 2} ) self.assertDictEqual(MockClass(a=2 , b=A_ ).to_kwargs() , {'a': 2, 'b': True} ) self.assertDictEqual(MockClass(a=2 , c=2.25 ).to_kwargs() , {'a': 2, 'c': 2.25} ) @require_cuda def UpperCAmelCase_ ( self )-> Dict: '''simple docstring''' UpperCamelCase = GradScalerKwargs(init_scale=1024 , growth_factor=2 ) AcceleratorState._reset_state() UpperCamelCase = Accelerator(mixed_precision='fp16' , kwargs_handlers=[scaler_handler] ) print(accelerator.use_fpaa ) UpperCamelCase = accelerator.scaler # Check the kwargs have been applied self.assertEqual(scaler._init_scale , 1_024.0 ) self.assertEqual(scaler._growth_factor , 2.0 ) # Check the other values are at the default self.assertEqual(scaler._backoff_factor , 0.5 ) self.assertEqual(scaler._growth_interval , 2000 ) self.assertEqual(scaler._enabled , A_ ) @require_multi_gpu def UpperCAmelCase_ ( self )-> Dict: '''simple docstring''' UpperCamelCase = ['torchrun', F'''--nproc_per_node={torch.cuda.device_count()}''', inspect.getfile(self.__class__ )] execute_subprocess_async(A_ , env=os.environ.copy() ) if __name__ == "__main__": lowerCAmelCase : Tuple = DistributedDataParallelKwargs(bucket_cap_mb=15, find_unused_parameters=True) lowerCAmelCase : List[str] = Accelerator(kwargs_handlers=[ddp_scaler]) lowerCAmelCase : List[Any] = torch.nn.Linear(1_00, 2_00) lowerCAmelCase : int = accelerator.prepare(model) # Check the values changed in kwargs lowerCAmelCase : Dict = '' lowerCAmelCase : Dict = model.bucket_bytes_cap // (10_24 * 10_24) if observed_bucket_cap_map != 15: error_msg += f"Kwargs badly passed, should have `15` but found {observed_bucket_cap_map}.\n" if model.find_unused_parameters is not True: error_msg += f"Kwargs badly passed, should have `True` but found {model.find_unused_parameters}.\n" # Check the values of the defaults if model.dim != 0: error_msg += f"Default value not respected, should have `0` but found {model.dim}.\n" if model.broadcast_buffers is not True: error_msg += f"Default value not respected, should have `True` but found {model.broadcast_buffers}.\n" if model.gradient_as_bucket_view is not False: error_msg += f"Default value not respected, should have `False` but found {model.gradient_as_bucket_view}.\n" # Raise error at the end to make sure we don't stop at the first failure. if len(error_msg) > 0: raise ValueError(error_msg)
3
1
'''simple docstring''' import math import sys def A_( A : str): UpperCamelCase = '' try: with open(A , 'rb') as binary_file: UpperCamelCase = binary_file.read() for dat in data: UpperCamelCase = f'''{dat:08b}''' result += curr_byte return result except OSError: print('File not accessible') sys.exit() def A_( A : str): UpperCamelCase = {'0': '0', '1': '1'} UpperCamelCase , UpperCamelCase = '', '' UpperCamelCase = len(A) for i in range(len(A)): curr_string += data_bits[i] if curr_string not in lexicon: continue UpperCamelCase = lexicon[curr_string] result += last_match_id UpperCamelCase = last_match_id + '0' if math.loga(A).is_integer(): UpperCamelCase = {} for curr_key in list(A): UpperCamelCase = lexicon.pop(A) UpperCamelCase = new_lex UpperCamelCase = last_match_id + '1' index += 1 UpperCamelCase = '' return result def A_( A : str , A : str): UpperCamelCase = 8 try: with open(A , 'wb') as opened_file: UpperCamelCase = [ to_write[i : i + byte_length] for i in range(0 , len(A) , A) ] if len(result_byte_array[-1]) % byte_length == 0: result_byte_array.append('10000000') else: result_byte_array[-1] += "1" + "0" * ( byte_length - len(result_byte_array[-1]) - 1 ) for elem in result_byte_array[:-1]: opened_file.write(int(A , 2).to_bytes(1 , byteorder='big')) except OSError: print('File not accessible') sys.exit() def A_( A : str): UpperCamelCase = 0 for letter in data_bits: if letter == "1": break counter += 1 UpperCamelCase = data_bits[counter:] UpperCamelCase = data_bits[counter + 1 :] return data_bits def A_( A : str , A : str): UpperCamelCase = read_file_binary(A) UpperCamelCase = remove_prefix(A) UpperCamelCase = decompress_data(A) write_file_binary(A , A) if __name__ == "__main__": compress(sys.argv[1], sys.argv[2])
3
'''simple docstring''' from typing import Callable, List, Optional, Tuple, Union import torch from transformers import CLIPTextModel, CLIPTokenizer from ...configuration_utils import ConfigMixin, register_to_config from ...models import ModelMixin, TransformeraDModel, VQModel from ...schedulers import VQDiffusionScheduler from ...utils import logging from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput lowerCAmelCase : Optional[Any] = logging.get_logger(__name__) # pylint: disable=invalid-name class SCREAMING_SNAKE_CASE__ ( snake_case_ , snake_case_): @register_to_config def __init__( self , A_ , A_ = None , A_ = None )-> Tuple: '''simple docstring''' super().__init__() UpperCamelCase = learnable if self.learnable: assert hidden_size is not None, "learnable=True requires `hidden_size` to be set" assert length is not None, "learnable=True requires `length` to be set" UpperCamelCase = torch.zeros(A_ , A_ ) else: UpperCamelCase = None UpperCamelCase = torch.nn.Parameter(A_ ) class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = 42 lowerCAmelCase_ = 42 lowerCAmelCase_ = 42 lowerCAmelCase_ = 42 lowerCAmelCase_ = 42 lowerCAmelCase_ = 42 def __init__( self , A_ , A_ , A_ , A_ , A_ , A_ , )-> Union[str, Any]: '''simple docstring''' super().__init__() self.register_modules( vqvae=A_ , transformer=A_ , text_encoder=A_ , tokenizer=A_ , scheduler=A_ , learned_classifier_free_sampling_embeddings=A_ , ) def UpperCAmelCase_ ( self , A_ , A_ , A_ )-> Tuple: '''simple docstring''' UpperCamelCase = len(A_ ) if isinstance(A_ , A_ ) else 1 # get prompt text embeddings UpperCamelCase = self.tokenizer( A_ , padding='max_length' , max_length=self.tokenizer.model_max_length , return_tensors='pt' , ) UpperCamelCase = text_inputs.input_ids if text_input_ids.shape[-1] > self.tokenizer.model_max_length: UpperCamelCase = self.tokenizer.batch_decode(text_input_ids[:, self.tokenizer.model_max_length :] ) logger.warning( 'The following part of your input was truncated because CLIP can only handle sequences up to' F''' {self.tokenizer.model_max_length} tokens: {removed_text}''' ) UpperCamelCase = text_input_ids[:, : self.tokenizer.model_max_length] UpperCamelCase = self.text_encoder(text_input_ids.to(self.device ) )[0] # NOTE: This additional step of normalizing the text embeddings is from VQ-Diffusion. # While CLIP does normalize the pooled output of the text transformer when combining # the image and text embeddings, CLIP does not directly normalize the last hidden state. # # CLIP normalizing the pooled output. # https://github.com/huggingface/transformers/blob/d92e22d1f28324f513f3080e5c47c071a3916721/src/transformers/models/clip/modeling_clip.py#L1052-L1053 UpperCamelCase = prompt_embeds / prompt_embeds.norm(dim=-1 , keepdim=A_ ) # duplicate text embeddings for each generation per prompt UpperCamelCase = prompt_embeds.repeat_interleave(A_ , dim=0 ) if do_classifier_free_guidance: if self.learned_classifier_free_sampling_embeddings.learnable: UpperCamelCase = self.learned_classifier_free_sampling_embeddings.embeddings UpperCamelCase = negative_prompt_embeds.unsqueeze(0 ).repeat(A_ , 1 , 1 ) else: UpperCamelCase = [''] * batch_size UpperCamelCase = text_input_ids.shape[-1] UpperCamelCase = self.tokenizer( A_ , padding='max_length' , max_length=A_ , truncation=A_ , return_tensors='pt' , ) UpperCamelCase = self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # See comment for normalizing text embeddings UpperCamelCase = negative_prompt_embeds / negative_prompt_embeds.norm(dim=-1 , keepdim=A_ ) # duplicate unconditional embeddings for each generation per prompt, using mps friendly method UpperCamelCase = negative_prompt_embeds.shape[1] UpperCamelCase = negative_prompt_embeds.repeat(1 , A_ , 1 ) UpperCamelCase = negative_prompt_embeds.view(batch_size * num_images_per_prompt , A_ , -1 ) # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes UpperCamelCase = torch.cat([negative_prompt_embeds, prompt_embeds] ) return prompt_embeds @torch.no_grad() def __call__( self , A_ , A_ = 100 , A_ = 5.0 , A_ = 1.0 , A_ = 1 , A_ = None , A_ = None , A_ = "pil" , A_ = True , A_ = None , A_ = 1 , )-> Union[ImagePipelineOutput, Tuple]: '''simple docstring''' if isinstance(A_ , A_ ): UpperCamelCase = 1 elif isinstance(A_ , A_ ): UpperCamelCase = len(A_ ) else: raise ValueError(F'''`prompt` has to be of type `str` or `list` but is {type(A_ )}''' ) UpperCamelCase = batch_size * num_images_per_prompt UpperCamelCase = guidance_scale > 1.0 UpperCamelCase = self._encode_prompt(A_ , A_ , A_ ) 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_ )}.''' ) # get the initial completely masked latents unless the user supplied it UpperCamelCase = (batch_size, self.transformer.num_latent_pixels) if latents is None: UpperCamelCase = self.transformer.num_vector_embeds - 1 UpperCamelCase = torch.full(A_ , A_ ).to(self.device ) else: if latents.shape != latents_shape: raise ValueError(F'''Unexpected latents shape, got {latents.shape}, expected {latents_shape}''' ) if (latents < 0).any() or (latents >= self.transformer.num_vector_embeds).any(): raise ValueError( 'Unexpected latents value(s). All latents be valid embedding indices i.e. in the range 0,' F''' {self.transformer.num_vector_embeds - 1} (inclusive).''' ) UpperCamelCase = latents.to(self.device ) # set timesteps self.scheduler.set_timesteps(A_ , device=self.device ) UpperCamelCase = self.scheduler.timesteps.to(self.device ) UpperCamelCase = latents for i, t in enumerate(self.progress_bar(A_ ) ): # expand the sample if we are doing classifier free guidance UpperCamelCase = torch.cat([sample] * 2 ) if do_classifier_free_guidance else sample # predict the un-noised image # model_output == `log_p_x_0` UpperCamelCase = self.transformer(A_ , encoder_hidden_states=A_ , timestep=A_ ).sample if do_classifier_free_guidance: UpperCamelCase , UpperCamelCase = model_output.chunk(2 ) UpperCamelCase = model_output_uncond + guidance_scale * (model_output_text - model_output_uncond) model_output -= torch.logsumexp(A_ , dim=1 , keepdim=A_ ) UpperCamelCase = self.truncate(A_ , A_ ) # remove `log(0)`'s (`-inf`s) UpperCamelCase = model_output.clamp(-70 ) # compute the previous noisy sample x_t -> x_t-1 UpperCamelCase = self.scheduler.step(A_ , timestep=A_ , sample=A_ , generator=A_ ).prev_sample # call the callback, if provided if callback is not None and i % callback_steps == 0: callback(A_ , A_ , A_ ) UpperCamelCase = self.vqvae.config.vq_embed_dim UpperCamelCase = (batch_size, self.transformer.height, self.transformer.width, embedding_channels) UpperCamelCase = self.vqvae.quantize.get_codebook_entry(A_ , shape=A_ ) UpperCamelCase = self.vqvae.decode(A_ , force_not_quantize=A_ ).sample UpperCamelCase = (image / 2 + 0.5).clamp(0 , 1 ) UpperCamelCase = image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": UpperCamelCase = self.numpy_to_pil(A_ ) if not return_dict: return (image,) return ImagePipelineOutput(images=A_ ) def UpperCAmelCase_ ( self , A_ , A_ )-> torch.FloatTensor: '''simple docstring''' UpperCamelCase , UpperCamelCase = torch.sort(A_ , 1 , descending=A_ ) UpperCamelCase = torch.exp(A_ ) UpperCamelCase = sorted_p_x_0.cumsum(dim=1 ) < truncation_rate # Ensure that at least the largest probability is not zeroed out UpperCamelCase = torch.full_like(keep_mask[:, 0:1, :] , A_ ) UpperCamelCase = torch.cat((all_true, keep_mask) , dim=1 ) UpperCamelCase = keep_mask[:, :-1, :] UpperCamelCase = keep_mask.gather(1 , indices.argsort(1 ) ) UpperCamelCase = log_p_x_0.clone() UpperCamelCase = -torch.inf # -inf = log(0) return rv
3
1
'''simple docstring''' import sys import webbrowser import requests from bsa import BeautifulSoup from fake_useragent import UserAgent if __name__ == "__main__": print('Googling.....') lowerCAmelCase : List[Any] = 'https://www.google.com/search?q=' + ' '.join(sys.argv[1:]) lowerCAmelCase : List[Any] = requests.get(url, headers={'UserAgent': UserAgent().random}) # res.raise_for_status() with open('project1a.html', 'wb') as out_file: # only for knowing the class for data in res.iter_content(1_00_00): out_file.write(data) lowerCAmelCase : Tuple = BeautifulSoup(res.text, 'html.parser') lowerCAmelCase : List[Any] = list(soup.select('.eZt8xd'))[:5] print(len(links)) for link in links: if link.text == "Maps": webbrowser.open(link.get('href')) else: webbrowser.open(f"""https://google.com{link.get('href')}""")
3
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCAmelCase : Union[str, Any] = { 'configuration_git': ['GIT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'GitConfig', 'GitVisionConfig'], 'processing_git': ['GitProcessor'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : List[Any] = [ 'GIT_PRETRAINED_MODEL_ARCHIVE_LIST', 'GitForCausalLM', 'GitModel', 'GitPreTrainedModel', 'GitVisionModel', ] if TYPE_CHECKING: from .configuration_git import GIT_PRETRAINED_CONFIG_ARCHIVE_MAP, GitConfig, GitVisionConfig from .processing_git import GitProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_git import ( GIT_PRETRAINED_MODEL_ARCHIVE_LIST, GitForCausalLM, GitModel, GitPreTrainedModel, GitVisionModel, ) else: import sys lowerCAmelCase : Optional[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
3
1
'''simple docstring''' import json import logging import os import sys from time import time from unittest.mock import patch from transformers.testing_utils import TestCasePlus, require_torch_tpu logging.basicConfig(level=logging.DEBUG) lowerCAmelCase : Optional[Any] = logging.getLogger() def A_( A : Dict): UpperCamelCase = {} UpperCamelCase = os.path.join(A , 'all_results.json') if os.path.exists(A): with open(A , 'r') as f: UpperCamelCase = json.load(A) else: raise ValueError(f'''can\'t find {path}''') return results lowerCAmelCase : Optional[Any] = logging.StreamHandler(sys.stdout) logger.addHandler(stream_handler) @require_torch_tpu class SCREAMING_SNAKE_CASE__ ( snake_case_): def UpperCAmelCase_ ( self )-> Any: '''simple docstring''' import xla_spawn UpperCamelCase = self.get_auto_remove_tmp_dir() UpperCamelCase = F''' ./examples/pytorch/text-classification/run_glue.py --num_cores=8 ./examples/pytorch/text-classification/run_glue.py --model_name_or_path distilbert-base-uncased --output_dir {tmp_dir} --overwrite_output_dir --train_file ./tests/fixtures/tests_samples/MRPC/train.csv --validation_file ./tests/fixtures/tests_samples/MRPC/dev.csv --do_train --do_eval --debug tpu_metrics_debug --per_device_train_batch_size=2 --per_device_eval_batch_size=1 --learning_rate=1e-4 --max_steps=10 --warmup_steps=2 --seed=42 --max_seq_length=128 '''.split() with patch.object(A_ , 'argv' , A_ ): UpperCamelCase = time() xla_spawn.main() UpperCamelCase = time() UpperCamelCase = get_results(A_ ) self.assertGreaterEqual(result['eval_accuracy'] , 0.75 ) # Assert that the script takes less than 500 seconds to make sure it doesn't hang. self.assertLess(end - start , 500 ) def UpperCAmelCase_ ( self )-> Any: '''simple docstring''' import xla_spawn UpperCamelCase = '\n ./tests/test_trainer_tpu.py\n --num_cores=8\n ./tests/test_trainer_tpu.py\n '.split() with patch.object(A_ , 'argv' , A_ ): xla_spawn.main()
3
'''simple docstring''' import uuid from typing import Any, Dict, List, Optional, Union from ..utils import add_end_docstrings, is_tf_available, is_torch_available, logging from .base import PIPELINE_INIT_ARGS, Pipeline if is_tf_available(): import tensorflow as tf if is_torch_available(): import torch lowerCAmelCase : Optional[Any] = logging.get_logger(__name__) class SCREAMING_SNAKE_CASE__ : def __init__( self , A_ = None , A_ = None , A_=None , A_=None )-> Optional[Any]: '''simple docstring''' if not conversation_id: UpperCamelCase = uuid.uuida() if past_user_inputs is None: UpperCamelCase = [] if generated_responses is None: UpperCamelCase = [] UpperCamelCase = conversation_id UpperCamelCase = past_user_inputs UpperCamelCase = generated_responses UpperCamelCase = text def __eq__( self , A_ )-> List[Any]: '''simple docstring''' if not isinstance(A_ , A_ ): return False if self.uuid == other.uuid: return True return ( self.new_user_input == other.new_user_input and self.past_user_inputs == other.past_user_inputs and self.generated_responses == other.generated_responses ) def UpperCAmelCase_ ( self , A_ , A_ = False )-> int: '''simple docstring''' if self.new_user_input: if overwrite: logger.warning( F'''User input added while unprocessed input was existing: "{self.new_user_input}" was overwritten ''' F'''with: "{text}".''' ) UpperCamelCase = text else: logger.warning( F'''User input added while unprocessed input was existing: "{self.new_user_input}" new input ''' F'''ignored: "{text}". Set `overwrite` to True to overwrite unprocessed user input''' ) else: UpperCamelCase = text def UpperCAmelCase_ ( self )-> Any: '''simple docstring''' if self.new_user_input: self.past_user_inputs.append(self.new_user_input ) UpperCamelCase = None def UpperCAmelCase_ ( self , A_ )-> int: '''simple docstring''' self.generated_responses.append(A_ ) def UpperCAmelCase_ ( self )-> List[str]: '''simple docstring''' for user_input, generated_response in zip(self.past_user_inputs , self.generated_responses ): yield True, user_input yield False, generated_response if self.new_user_input: yield True, self.new_user_input def __repr__( self )-> Any: '''simple docstring''' UpperCamelCase = F'''Conversation id: {self.uuid} \n''' for is_user, text in self.iter_texts(): UpperCamelCase = 'user' if is_user else 'bot' output += F'''{name} >> {text} \n''' return output @add_end_docstrings( snake_case_ , R""" min_length_for_response (`int`, *optional*, defaults to 32): The minimum length (in number of tokens) for a response. minimum_tokens (`int`, *optional*, defaults to 10): The minimum length of tokens to leave for a response. """ , ) class SCREAMING_SNAKE_CASE__ ( snake_case_): def __init__( self , *A_ , **A_ )-> Any: '''simple docstring''' super().__init__(*A_ , **A_ ) if self.tokenizer.pad_token_id is None: UpperCamelCase = self.tokenizer.eos_token def UpperCAmelCase_ ( self , A_=None , A_=None , A_=None , **A_ )-> Union[str, Any]: '''simple docstring''' UpperCamelCase = {} UpperCamelCase = {} UpperCamelCase = {} if min_length_for_response is not None: UpperCamelCase = min_length_for_response if minimum_tokens is not None: UpperCamelCase = minimum_tokens if "max_length" in generate_kwargs: UpperCamelCase = generate_kwargs['max_length'] # self.max_length = generate_kwargs.get("max_length", self.model.config.max_length) if clean_up_tokenization_spaces is not None: UpperCamelCase = clean_up_tokenization_spaces if generate_kwargs: forward_params.update(A_ ) return preprocess_params, forward_params, postprocess_params def __call__( self , A_ , A_=0 , **A_ )-> Any: '''simple docstring''' UpperCamelCase = super().__call__(A_ , num_workers=A_ , **A_ ) if isinstance(A_ , A_ ) and len(A_ ) == 1: return outputs[0] return outputs def UpperCAmelCase_ ( self , A_ , A_=32 )-> Dict[str, Any]: '''simple docstring''' if not isinstance(A_ , A_ ): raise ValueError('ConversationalPipeline, expects Conversation as inputs' ) if conversation.new_user_input is None: raise ValueError( F'''Conversation with UUID {type(conversation.uuid )} does not contain new user input to process. ''' 'Add user inputs with the conversation\'s `add_user_input` method' ) if hasattr(self.tokenizer , '_build_conversation_input_ids' ): UpperCamelCase = self.tokenizer._build_conversation_input_ids(A_ ) else: # If the tokenizer cannot handle conversations, we default to only the old version UpperCamelCase = self._legacy_parse_and_tokenize(A_ ) if self.framework == "pt": UpperCamelCase = torch.LongTensor([input_ids] ) elif self.framework == "tf": UpperCamelCase = tf.constant([input_ids] ) return {"input_ids": input_ids, "conversation": conversation} def UpperCAmelCase_ ( self , A_ , A_=10 , **A_ )-> Optional[Any]: '''simple docstring''' UpperCamelCase = generate_kwargs.get('max_length' , self.model.config.max_length ) UpperCamelCase = model_inputs['input_ids'].shape[1] if max_length - minimum_tokens < n: logger.warning(F'''Conversation input is to long ({n}), trimming it to ({max_length} - {minimum_tokens})''' ) UpperCamelCase = max_length - minimum_tokens UpperCamelCase = model_inputs['input_ids'][:, -trim:] if "attention_mask" in model_inputs: UpperCamelCase = model_inputs['attention_mask'][:, -trim:] UpperCamelCase = model_inputs.pop('conversation' ) UpperCamelCase = max_length UpperCamelCase = self.model.generate(**A_ , **A_ ) if self.model.config.is_encoder_decoder: UpperCamelCase = 1 else: UpperCamelCase = n return {"output_ids": output_ids[:, start_position:], "conversation": conversation} def UpperCAmelCase_ ( self , A_ , A_=True )-> Tuple: '''simple docstring''' UpperCamelCase = model_outputs['output_ids'] UpperCamelCase = self.tokenizer.decode( output_ids[0] , skip_special_tokens=A_ , clean_up_tokenization_spaces=A_ , ) UpperCamelCase = model_outputs['conversation'] conversation.mark_processed() conversation.append_response(A_ ) return conversation def UpperCAmelCase_ ( self , A_ )-> Dict: '''simple docstring''' UpperCamelCase = self.tokenizer.eos_token_id UpperCamelCase = [] for is_user, text in conversation.iter_texts(): if eos_token_id is not None: input_ids.extend(self.tokenizer.encode(A_ , add_special_tokens=A_ ) + [eos_token_id] ) else: input_ids.extend(self.tokenizer.encode(A_ , add_special_tokens=A_ ) ) if len(A_ ) > self.tokenizer.model_max_length: UpperCamelCase = input_ids[-self.tokenizer.model_max_length :] return input_ids
3
1
'''simple docstring''' lowerCAmelCase : dict[str, float] = { "joule": 1.0, "kilojoule": 10_00, "megajoule": 1_00_00_00, "gigajoule": 10_00_00_00_00, "wattsecond": 1.0, "watthour": 36_00, "kilowatthour": 3_60_00_00, "newtonmeter": 1.0, "calorie_nutr": 41_86.8, "kilocalorie_nutr": 4_18_68_00.00, "electronvolt": 1.6_0217_6634e-19, "britishthermalunit_it": 10_55.0_55_85, "footpound": 1.355818, } def A_( A : str , A : str , A : float): if to_type not in ENERGY_CONVERSION or from_type not in ENERGY_CONVERSION: UpperCamelCase = ( f'''Incorrect \'from_type\' or \'to_type\' value: {from_type!r}, {to_type!r}\n''' f'''Valid values are: {", ".join(A)}''' ) raise ValueError(A) return value * ENERGY_CONVERSION[from_type] / ENERGY_CONVERSION[to_type] if __name__ == "__main__": import doctest doctest.testmod()
3
'''simple docstring''' import sys import webbrowser import requests from bsa import BeautifulSoup from fake_useragent import UserAgent if __name__ == "__main__": print('Googling.....') lowerCAmelCase : List[Any] = 'https://www.google.com/search?q=' + ' '.join(sys.argv[1:]) lowerCAmelCase : List[Any] = requests.get(url, headers={'UserAgent': UserAgent().random}) # res.raise_for_status() with open('project1a.html', 'wb') as out_file: # only for knowing the class for data in res.iter_content(1_00_00): out_file.write(data) lowerCAmelCase : Tuple = BeautifulSoup(res.text, 'html.parser') lowerCAmelCase : List[Any] = list(soup.select('.eZt8xd'))[:5] print(len(links)) for link in links: if link.text == "Maps": webbrowser.open(link.get('href')) else: webbrowser.open(f"""https://google.com{link.get('href')}""")
3
1
'''simple docstring''' def A_( A : int = 400_0000): UpperCamelCase = [] UpperCamelCase , UpperCamelCase = 0, 1 while b <= n: if b % 2 == 0: even_fibs.append(A) UpperCamelCase , UpperCamelCase = b, a + b return sum(A) if __name__ == "__main__": print(f"""{solution() = }""")
3
'''simple docstring''' import numpy as np def A_( A : str , A : Optional[Any] , A : Tuple , A : Optional[int] , A : str): UpperCamelCase = int(np.ceil((x_end - xa) / h)) UpperCamelCase = np.zeros((n + 1,)) UpperCamelCase = ya UpperCamelCase = xa for k in range(A): UpperCamelCase = f(A , y[k]) UpperCamelCase = f(x + 0.5 * h , y[k] + 0.5 * h * ka) UpperCamelCase = f(x + 0.5 * h , y[k] + 0.5 * h * ka) UpperCamelCase = f(x + h , y[k] + h * ka) UpperCamelCase = y[k] + (1 / 6) * h * (ka + 2 * ka + 2 * ka + ka) x += h return y if __name__ == "__main__": import doctest doctest.testmod()
3
1
'''simple docstring''' from ....configuration_utils import PretrainedConfig from ....utils import logging lowerCAmelCase : Optional[Any] = logging.get_logger(__name__) lowerCAmelCase : Dict = { 'speechbrain/m-ctc-t-large': 'https://huggingface.co/speechbrain/m-ctc-t-large/resolve/main/config.json', # See all M-CTC-T models at https://huggingface.co/models?filter=mctct } class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = """mctct""" def __init__( self , A_=8065 , A_=1536 , A_=36 , A_=6144 , A_=4 , A_=384 , A_=920 , A_=1e-5 , A_=0.3 , A_="relu" , A_=0.02 , A_=0.3 , A_=0.3 , A_=1 , A_=0 , A_=2 , A_=1 , A_=0.3 , A_=1 , A_=(7,) , A_=(3,) , A_=80 , A_=1 , A_=None , A_="sum" , A_=False , **A_ , )-> str: '''simple docstring''' super().__init__(**A_ , pad_token_id=A_ , bos_token_id=A_ , eos_token_id=A_ ) UpperCamelCase = vocab_size UpperCamelCase = hidden_size UpperCamelCase = num_hidden_layers UpperCamelCase = intermediate_size UpperCamelCase = num_attention_heads UpperCamelCase = attention_head_dim UpperCamelCase = max_position_embeddings UpperCamelCase = layer_norm_eps UpperCamelCase = layerdrop UpperCamelCase = hidden_act UpperCamelCase = initializer_range UpperCamelCase = hidden_dropout_prob UpperCamelCase = attention_probs_dropout_prob UpperCamelCase = pad_token_id UpperCamelCase = bos_token_id UpperCamelCase = eos_token_id UpperCamelCase = conv_glu_dim UpperCamelCase = conv_dropout UpperCamelCase = num_conv_layers UpperCamelCase = input_feat_per_channel UpperCamelCase = input_channels UpperCamelCase = conv_channels UpperCamelCase = ctc_loss_reduction UpperCamelCase = ctc_zero_infinity # prevents config testing fail with exporting to json UpperCamelCase = list(A_ ) UpperCamelCase = list(A_ ) if len(self.conv_kernel ) != self.num_conv_layers: raise ValueError( 'Configuration for convolutional module is incorrect. ' 'It is required that `len(config.conv_kernel)` == `config.num_conv_layers` ' F'''but is `len(config.conv_kernel) = {len(self.conv_kernel )}`, ''' F'''`config.num_conv_layers = {self.num_conv_layers}`.''' )
3
'''simple docstring''' from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Features, Value from .base import TaskTemplate @dataclass(frozen=snake_case_) class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = field(default="""language-modeling""" , metadata={"""include_in_asdict_even_if_is_default""": True}) lowerCAmelCase_ = Features({"""text""": Value("""string""")}) lowerCAmelCase_ = Features({}) lowerCAmelCase_ = "text" @property def UpperCAmelCase_ ( self )-> Dict[str, str]: '''simple docstring''' return {self.text_column: "text"}
3
1
'''simple docstring''' from manim import * class SCREAMING_SNAKE_CASE__ ( snake_case_): def UpperCAmelCase_ ( self )-> str: '''simple docstring''' UpperCamelCase = Rectangle(height=0.5 , width=0.5 ) UpperCamelCase = Rectangle(height=0.46 , width=0.46 ).set_stroke(width=0 ) UpperCamelCase = [mem.copy() for i in range(6 )] UpperCamelCase = [mem.copy() for i in range(6 )] UpperCamelCase = VGroup(*A_ ).arrange(A_ , buff=0 ) UpperCamelCase = VGroup(*A_ ).arrange(A_ , buff=0 ) UpperCamelCase = VGroup(A_ , A_ ).arrange(A_ , buff=0 ) UpperCamelCase = Text('CPU' , font_size=24 ) UpperCamelCase = Group(A_ , A_ ).arrange(A_ , buff=0.5 , aligned_edge=A_ ) cpu.move_to([-2.5, -0.5, 0] ) self.add(A_ ) UpperCamelCase = [mem.copy() for i in range(4 )] UpperCamelCase = VGroup(*A_ ).arrange(A_ , buff=0 ) UpperCamelCase = Text('GPU' , font_size=24 ) UpperCamelCase = Group(A_ , A_ ).arrange(A_ , buff=0.5 , aligned_edge=A_ ) gpu.move_to([-1, -1, 0] ) self.add(A_ ) UpperCamelCase = [mem.copy() for i in range(6 )] UpperCamelCase = VGroup(*A_ ).arrange(A_ , buff=0 ) UpperCamelCase = Text('Model' , font_size=24 ) UpperCamelCase = Group(A_ , A_ ).arrange(A_ , buff=0.5 , aligned_edge=A_ ) model.move_to([3, -1.0, 0] ) self.add(A_ ) UpperCamelCase = [] for i, rect in enumerate(A_ ): rect.set_stroke(A_ ) # target = fill.copy().set_fill(YELLOW, opacity=0.7) # target.move_to(rect) # self.add(target) UpperCamelCase = Rectangle(height=0.46 / 4 , width=0.46 / 3 ).set_stroke(width=0.0 ).set_fill(A_ , opacity=0.7 ) if i == 0: cpu_target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT ) , buff=0.02 , direction=A_ ) cpu_target.set_x(cpu_target.get_x() + 0.1 ) elif i == 3: cpu_target.next_to(cpu_targs[0] , direction=A_ , buff=0.0 ) else: cpu_target.next_to(cpu_targs[i - 1] , direction=A_ , buff=0.0 ) self.add(A_ ) cpu_targs.append(A_ ) UpperCamelCase = [mem.copy() for i in range(6 )] UpperCamelCase = VGroup(*A_ ).arrange(A_ , buff=0 ) UpperCamelCase = Text('Loaded Checkpoint' , font_size=24 ) UpperCamelCase = Group(A_ , A_ ).arrange(A_ , aligned_edge=A_ , buff=0.4 ) checkpoint.move_to([3, 0.5, 0] ) UpperCamelCase = Square(side_length=2.2 ) key.move_to([-5, 2, 0] ) UpperCamelCase = MarkupText( F'''<b>Key:</b>\n\n<span fgcolor=\'{YELLOW}\'>●</span> Empty Model''' , font_size=18 , ) key_text.move_to([-5, 2.4, 0] ) self.add(A_ , A_ ) UpperCamelCase = MarkupText( F'''<span fgcolor=\'{BLUE}\'>●</span> Checkpoint''' , font_size=18 , ) blue_text.next_to(A_ , DOWN * 2.4 , aligned_edge=key_text.get_left() ) UpperCamelCase = MarkupText( F'''Next, a <i><span fgcolor="{BLUE}">second</span></i> model is loaded into memory,\nwith the weights of a <span fgcolor="{BLUE}">single shard</span>.''' , font_size=24 , ) step_a.move_to([2, 2, 0] ) self.play(Write(A_ ) , Write(A_ ) ) self.play(Write(A_ , run_time=1 ) , Create(A_ , run_time=1 ) ) UpperCamelCase = [] UpperCamelCase = [] for i, rect in enumerate(A_ ): UpperCamelCase = fill.copy().set_fill(A_ , opacity=0.7 ) target.move_to(A_ ) first_animations.append(GrowFromCenter(A_ , run_time=1 ) ) UpperCamelCase = target.copy() cpu_target.generate_target() if i < 5: cpu_target.target.move_to(cpu_left_col_base[i + 1] ) else: cpu_target.target.move_to(cpu_right_col_base[i - 5] ) second_animations.append(MoveToTarget(A_ , run_time=1.5 ) ) self.play(*A_ ) self.play(*A_ ) self.wait()
3
'''simple docstring''' from __future__ import annotations lowerCAmelCase : Union[str, Any] = [-10, -5, 0, 5, 5.1, 11, 13, 21, 3, 4, -21, -10, -5, -1, 0] lowerCAmelCase : List[str] = [-5, 0, 5, 5.1, 11, 13, 21, -1, 4, -1, -10, -5, -1, 0, -1] def A_( A : list[float]): UpperCamelCase = [] UpperCamelCase = len(A) for i in range(A): UpperCamelCase = -1 for j in range(i + 1 , A): if arr[i] < arr[j]: UpperCamelCase = arr[j] break result.append(A) return result def A_( A : list[float]): UpperCamelCase = [] for i, outer in enumerate(A): UpperCamelCase = -1 for inner in arr[i + 1 :]: if outer < inner: UpperCamelCase = inner break result.append(A) return result def A_( A : list[float]): UpperCamelCase = len(A) UpperCamelCase = [] UpperCamelCase = [-1] * arr_size for index in reversed(range(A)): if stack: while stack[-1] <= arr[index]: stack.pop() if not stack: break if stack: UpperCamelCase = stack[-1] stack.append(arr[index]) return result if __name__ == "__main__": from doctest import testmod from timeit import timeit testmod() print(next_greatest_element_slow(arr)) print(next_greatest_element_fast(arr)) print(next_greatest_element(arr)) lowerCAmelCase : Optional[Any] = ( 'from __main__ import arr, next_greatest_element_slow, ' 'next_greatest_element_fast, next_greatest_element' ) print( 'next_greatest_element_slow():', timeit('next_greatest_element_slow(arr)', setup=setup), ) print( 'next_greatest_element_fast():', timeit('next_greatest_element_fast(arr)', setup=setup), ) print( ' next_greatest_element():', timeit('next_greatest_element(arr)', setup=setup), )
3
1
'''simple docstring''' import os def A_( A : Optional[Any]): UpperCamelCase = len(grid[0]) UpperCamelCase = len(A) UpperCamelCase = 0 UpperCamelCase = 0 UpperCamelCase = 0 # Check vertically, horizontally, diagonally at the same time (only works # for nxn grid) for i in range(A): for j in range(n_rows - 3): UpperCamelCase = grid[j][i] * grid[j + 1][i] * grid[j + 2][i] * grid[j + 3][i] UpperCamelCase = grid[i][j] * grid[i][j + 1] * grid[i][j + 2] * grid[i][j + 3] # Left-to-right diagonal (\) product if i < n_columns - 3: UpperCamelCase = ( grid[i][j] * grid[i + 1][j + 1] * grid[i + 2][j + 2] * grid[i + 3][j + 3] ) # Right-to-left diagonal(/) product if i > 2: UpperCamelCase = ( grid[i][j] * grid[i - 1][j + 1] * grid[i - 2][j + 2] * grid[i - 3][j + 3] ) UpperCamelCase = max( A , A , A , A) if max_product > largest: UpperCamelCase = max_product return largest def A_( ): UpperCamelCase = [] with open(os.path.dirname(A) + '/grid.txt') as file: for line in file: grid.append(line.strip('\n').split(' ')) UpperCamelCase = [[int(A) for i in grid[j]] for j in range(len(A))] return largest_product(A) if __name__ == "__main__": print(solution())
3
'''simple docstring''' from string import ascii_lowercase, ascii_uppercase def A_( A : str): if not sentence: return "" UpperCamelCase = dict(zip(A , A)) return lower_to_upper.get(sentence[0] , sentence[0]) + sentence[1:] if __name__ == "__main__": from doctest import testmod testmod()
3
1
'''simple docstring''' from .imports import is_tqdm_available if is_tqdm_available(): from tqdm.auto import tqdm as _tqdm from ..state import PartialState def A_( A : bool = True , *A : Any , **A : Tuple): if not is_tqdm_available(): raise ImportError('Accelerate\'s `tqdm` module requires `tqdm` to be installed. Please run `pip install tqdm`.') UpperCamelCase = False if main_process_only: UpperCamelCase = PartialState().local_process_index == 0 return _tqdm(*A , **A , disable=A)
3
'''simple docstring''' from typing import Optional, Tuple, Union import tensorflow as tf from ...activations_tf import ACTaFN from ...file_utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward from ...modeling_tf_outputs import ( TFBaseModelOutputWithNoAttention, TFBaseModelOutputWithPoolingAndNoAttention, TFSequenceClassifierOutput, ) from ...modeling_tf_utils import TFPreTrainedModel, TFSequenceClassificationLoss, keras_serializable, unpack_inputs from ...tf_utils import shape_list from ...utils import logging from .configuration_regnet import RegNetConfig lowerCAmelCase : Dict = logging.get_logger(__name__) # General docstring lowerCAmelCase : str = 'RegNetConfig' # Base docstring lowerCAmelCase : str = 'facebook/regnet-y-040' lowerCAmelCase : Dict = [1, 10_88, 7, 7] # Image classification docstring lowerCAmelCase : Dict = 'facebook/regnet-y-040' lowerCAmelCase : int = 'tabby, tabby cat' lowerCAmelCase : int = [ 'facebook/regnet-y-040', # See all regnet models at https://huggingface.co/models?filter=regnet ] class SCREAMING_SNAKE_CASE__ ( tf.keras.layers.Layer): def __init__( self , A_ , A_ = 3 , A_ = 1 , A_ = 1 , A_ = "relu" , **A_ , )-> str: '''simple docstring''' super().__init__(**A_ ) # The padding and conv has been verified in # https://colab.research.google.com/gist/sayakpaul/854bc10eeaf21c9ee2119e0b9f3841a7/scratchpad.ipynb UpperCamelCase = tf.keras.layers.ZeroPaddingaD(padding=kernel_size // 2 ) UpperCamelCase = tf.keras.layers.ConvaD( filters=A_ , kernel_size=A_ , strides=A_ , padding='VALID' , groups=A_ , use_bias=A_ , name='convolution' , ) UpperCamelCase = tf.keras.layers.BatchNormalization(epsilon=1e-5 , momentum=0.9 , name='normalization' ) UpperCamelCase = ACTaFN[activation] if activation is not None else tf.identity def UpperCAmelCase_ ( self , A_ )-> Any: '''simple docstring''' UpperCamelCase = self.convolution(self.padding(A_ ) ) UpperCamelCase = self.normalization(A_ ) UpperCamelCase = self.activation(A_ ) return hidden_state class SCREAMING_SNAKE_CASE__ ( tf.keras.layers.Layer): def __init__( self , A_ , **A_ )-> Optional[Any]: '''simple docstring''' super().__init__(**A_ ) UpperCamelCase = config.num_channels UpperCamelCase = TFRegNetConvLayer( out_channels=config.embedding_size , kernel_size=3 , stride=2 , activation=config.hidden_act , name='embedder' , ) def UpperCAmelCase_ ( self , A_ )-> List[Any]: '''simple docstring''' UpperCamelCase = shape_list(A_ )[1] if tf.executing_eagerly() and num_channels != self.num_channels: raise ValueError( 'Make sure that the channel dimension of the pixel values match with the one set in the configuration.' ) # When running on CPU, `tf.keras.layers.Conv2D` doesn't support `NCHW` format. # So change the input format from `NCHW` to `NHWC`. # shape = (batch_size, in_height, in_width, in_channels=num_channels) UpperCamelCase = tf.transpose(A_ , perm=(0, 2, 3, 1) ) UpperCamelCase = self.embedder(A_ ) return hidden_state class SCREAMING_SNAKE_CASE__ ( tf.keras.layers.Layer): def __init__( self , A_ , A_ = 2 , **A_ )-> List[Any]: '''simple docstring''' super().__init__(**A_ ) UpperCamelCase = tf.keras.layers.ConvaD( filters=A_ , kernel_size=1 , strides=A_ , use_bias=A_ , name='convolution' ) UpperCamelCase = tf.keras.layers.BatchNormalization(epsilon=1e-5 , momentum=0.9 , name='normalization' ) def UpperCAmelCase_ ( self , A_ , A_ = False )-> tf.Tensor: '''simple docstring''' return self.normalization(self.convolution(A_ ) , training=A_ ) class SCREAMING_SNAKE_CASE__ ( tf.keras.layers.Layer): def __init__( self , A_ , A_ , **A_ )-> Optional[Any]: '''simple docstring''' super().__init__(**A_ ) UpperCamelCase = tf.keras.layers.GlobalAveragePoolingaD(keepdims=A_ , name='pooler' ) UpperCamelCase = [ tf.keras.layers.ConvaD(filters=A_ , kernel_size=1 , activation='relu' , name='attention.0' ), tf.keras.layers.ConvaD(filters=A_ , kernel_size=1 , activation='sigmoid' , name='attention.2' ), ] def UpperCAmelCase_ ( self , A_ )-> Optional[int]: '''simple docstring''' UpperCamelCase = self.pooler(A_ ) for layer_module in self.attention: UpperCamelCase = layer_module(A_ ) UpperCamelCase = hidden_state * pooled return hidden_state class SCREAMING_SNAKE_CASE__ ( tf.keras.layers.Layer): def __init__( self , A_ , A_ , A_ , A_ = 1 , **A_ )-> Dict: '''simple docstring''' super().__init__(**A_ ) UpperCamelCase = in_channels != out_channels or stride != 1 UpperCamelCase = max(1 , out_channels // config.groups_width ) UpperCamelCase = ( TFRegNetShortCut(A_ , stride=A_ , name='shortcut' ) if should_apply_shortcut else tf.keras.layers.Activation('linear' , name='shortcut' ) ) # `self.layers` instead of `self.layer` because that is a reserved argument. UpperCamelCase = [ TFRegNetConvLayer(A_ , kernel_size=1 , activation=config.hidden_act , name='layer.0' ), TFRegNetConvLayer( A_ , stride=A_ , groups=A_ , activation=config.hidden_act , name='layer.1' ), TFRegNetConvLayer(A_ , kernel_size=1 , activation=A_ , name='layer.2' ), ] UpperCamelCase = ACTaFN[config.hidden_act] def UpperCAmelCase_ ( self , A_ )-> Tuple: '''simple docstring''' UpperCamelCase = hidden_state for layer_module in self.layers: UpperCamelCase = layer_module(A_ ) UpperCamelCase = self.shortcut(A_ ) hidden_state += residual UpperCamelCase = self.activation(A_ ) return hidden_state class SCREAMING_SNAKE_CASE__ ( tf.keras.layers.Layer): def __init__( self , A_ , A_ , A_ , A_ = 1 , **A_ )-> Any: '''simple docstring''' super().__init__(**A_ ) UpperCamelCase = in_channels != out_channels or stride != 1 UpperCamelCase = max(1 , out_channels // config.groups_width ) UpperCamelCase = ( TFRegNetShortCut(A_ , stride=A_ , name='shortcut' ) if should_apply_shortcut else tf.keras.layers.Activation('linear' , name='shortcut' ) ) UpperCamelCase = [ TFRegNetConvLayer(A_ , kernel_size=1 , activation=config.hidden_act , name='layer.0' ), TFRegNetConvLayer( A_ , stride=A_ , groups=A_ , activation=config.hidden_act , name='layer.1' ), TFRegNetSELayer(A_ , reduced_channels=int(round(in_channels / 4 ) ) , name='layer.2' ), TFRegNetConvLayer(A_ , kernel_size=1 , activation=A_ , name='layer.3' ), ] UpperCamelCase = ACTaFN[config.hidden_act] def UpperCAmelCase_ ( self , A_ )-> List[Any]: '''simple docstring''' UpperCamelCase = hidden_state for layer_module in self.layers: UpperCamelCase = layer_module(A_ ) UpperCamelCase = self.shortcut(A_ ) hidden_state += residual UpperCamelCase = self.activation(A_ ) return hidden_state class SCREAMING_SNAKE_CASE__ ( tf.keras.layers.Layer): def __init__( self , A_ , A_ , A_ , A_ = 2 , A_ = 2 , **A_ )-> Dict: '''simple docstring''' super().__init__(**A_ ) UpperCamelCase = TFRegNetXLayer if config.layer_type == 'x' else TFRegNetYLayer UpperCamelCase = [ # downsampling is done in the first layer with stride of 2 layer(A_ , A_ , A_ , stride=A_ , name='layers.0' ), *[layer(A_ , A_ , A_ , name=F'''layers.{i+1}''' ) for i in range(depth - 1 )], ] def UpperCAmelCase_ ( self , A_ )-> List[Any]: '''simple docstring''' for layer_module in self.layers: UpperCamelCase = layer_module(A_ ) return hidden_state class SCREAMING_SNAKE_CASE__ ( tf.keras.layers.Layer): def __init__( self , A_ , **A_ )-> str: '''simple docstring''' super().__init__(**A_ ) UpperCamelCase = [] # based on `downsample_in_first_stage`, the first layer of the first stage may or may not downsample the input self.stages.append( TFRegNetStage( A_ , config.embedding_size , config.hidden_sizes[0] , stride=2 if config.downsample_in_first_stage else 1 , depth=config.depths[0] , name='stages.0' , ) ) UpperCamelCase = zip(config.hidden_sizes , config.hidden_sizes[1:] ) for i, ((in_channels, out_channels), depth) in enumerate(zip(A_ , config.depths[1:] ) ): self.stages.append(TFRegNetStage(A_ , A_ , A_ , depth=A_ , name=F'''stages.{i+1}''' ) ) def UpperCAmelCase_ ( self , A_ , A_ = False , A_ = True )-> TFBaseModelOutputWithNoAttention: '''simple docstring''' UpperCamelCase = () if output_hidden_states else None for stage_module in self.stages: if output_hidden_states: UpperCamelCase = hidden_states + (hidden_state,) UpperCamelCase = stage_module(A_ ) if output_hidden_states: UpperCamelCase = hidden_states + (hidden_state,) if not return_dict: return tuple(v for v in [hidden_state, hidden_states] if v is not None ) return TFBaseModelOutputWithNoAttention(last_hidden_state=A_ , hidden_states=A_ ) @keras_serializable class SCREAMING_SNAKE_CASE__ ( tf.keras.layers.Layer): lowerCAmelCase_ = RegNetConfig def __init__( self , A_ , **A_ )-> Union[str, Any]: '''simple docstring''' super().__init__(**A_ ) UpperCamelCase = config UpperCamelCase = TFRegNetEmbeddings(A_ , name='embedder' ) UpperCamelCase = TFRegNetEncoder(A_ , name='encoder' ) UpperCamelCase = tf.keras.layers.GlobalAveragePoolingaD(keepdims=A_ , name='pooler' ) @unpack_inputs def UpperCAmelCase_ ( self , A_ , A_ = None , A_ = None , A_ = False , )-> TFBaseModelOutputWithPoolingAndNoAttention: '''simple docstring''' UpperCamelCase = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) UpperCamelCase = return_dict if return_dict is not None else self.config.use_return_dict UpperCamelCase = self.embedder(A_ , training=A_ ) UpperCamelCase = self.encoder( A_ , output_hidden_states=A_ , return_dict=A_ , training=A_ ) UpperCamelCase = encoder_outputs[0] UpperCamelCase = self.pooler(A_ ) # Change to NCHW output format have uniformity in the modules UpperCamelCase = tf.transpose(A_ , perm=(0, 3, 1, 2) ) UpperCamelCase = tf.transpose(A_ , perm=(0, 3, 1, 2) ) # Change the other hidden state outputs to NCHW as well if output_hidden_states: UpperCamelCase = tuple([tf.transpose(A_ , perm=(0, 3, 1, 2) ) for h in encoder_outputs[1]] ) if not return_dict: return (last_hidden_state, pooled_output) + encoder_outputs[1:] return TFBaseModelOutputWithPoolingAndNoAttention( last_hidden_state=A_ , pooler_output=A_ , hidden_states=hidden_states if output_hidden_states else encoder_outputs.hidden_states , ) class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = RegNetConfig lowerCAmelCase_ = """regnet""" lowerCAmelCase_ = """pixel_values""" @property def UpperCAmelCase_ ( self )-> List[str]: '''simple docstring''' return {"pixel_values": tf.TensorSpec(shape=(None, self.config.num_channels, 224, 224) , dtype=tf.floataa )} lowerCAmelCase : str = r'\n Parameters:\n This model is a Tensorflow\n [tf.keras.layers.Layer](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Layer) sub-class. Use it as a\n regular Tensorflow Module and refer to the Tensorflow documentation for all matter related to general usage and\n behavior.\n config ([`RegNetConfig`]): Model configuration class with all the parameters of the model.\n Initializing with a config file does not load the weights associated with the model, only the\n configuration. Check out the [`~TFPreTrainedModel.from_pretrained`] method to load the model weights.\n' lowerCAmelCase : List[str] = r'\n Args:\n pixel_values (`tf.Tensor` of shape `(batch_size, num_channels, height, width)`):\n Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n [`ConveNextImageProcessor.__call__`] for details.\n output_hidden_states (`bool`, *optional*):\n Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for\n more detail.\n return_dict (`bool`, *optional*):\n Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n' @add_start_docstrings( """The bare RegNet model outputting raw features without any specific head on top.""" , snake_case_ , ) class SCREAMING_SNAKE_CASE__ ( snake_case_): def __init__( self , A_ , *A_ , **A_ )-> List[Any]: '''simple docstring''' super().__init__(A_ , *A_ , **A_ ) UpperCamelCase = TFRegNetMainLayer(A_ , name='regnet' ) @unpack_inputs @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 UpperCAmelCase_ ( self , A_ , A_ = None , A_ = None , A_=False , )-> Union[TFBaseModelOutputWithPoolingAndNoAttention, Tuple[tf.Tensor]]: '''simple docstring''' UpperCamelCase = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) UpperCamelCase = return_dict if return_dict is not None else self.config.use_return_dict UpperCamelCase = self.regnet( pixel_values=A_ , output_hidden_states=A_ , return_dict=A_ , training=A_ , ) if not return_dict: return (outputs[0],) + outputs[1:] return TFBaseModelOutputWithPoolingAndNoAttention( last_hidden_state=outputs.last_hidden_state , pooler_output=outputs.pooler_output , hidden_states=outputs.hidden_states , ) @add_start_docstrings( """ RegNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for ImageNet. """ , snake_case_ , ) class SCREAMING_SNAKE_CASE__ ( snake_case_ , snake_case_): def __init__( self , A_ , *A_ , **A_ )-> str: '''simple docstring''' super().__init__(A_ , *A_ , **A_ ) UpperCamelCase = config.num_labels UpperCamelCase = TFRegNetMainLayer(A_ , name='regnet' ) # classification head UpperCamelCase = [ tf.keras.layers.Flatten(), tf.keras.layers.Dense(config.num_labels , name='classifier.1' ) if config.num_labels > 0 else tf.identity, ] @unpack_inputs @add_start_docstrings_to_model_forward(A_ ) @add_code_sample_docstrings( checkpoint=_IMAGE_CLASS_CHECKPOINT , output_type=A_ , config_class=_CONFIG_FOR_DOC , expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT , ) def UpperCAmelCase_ ( self , A_ = None , A_ = None , A_ = None , A_ = None , A_=False , )-> Union[TFSequenceClassifierOutput, Tuple[tf.Tensor]]: '''simple docstring''' UpperCamelCase = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) UpperCamelCase = return_dict if return_dict is not None else self.config.use_return_dict UpperCamelCase = self.regnet( A_ , output_hidden_states=A_ , return_dict=A_ , training=A_ ) UpperCamelCase = outputs.pooler_output if return_dict else outputs[1] UpperCamelCase = self.classifier[0](A_ ) UpperCamelCase = self.classifier[1](A_ ) UpperCamelCase = None if labels is None else self.hf_compute_loss(labels=A_ , logits=A_ ) if not return_dict: UpperCamelCase = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return TFSequenceClassifierOutput(loss=A_ , logits=A_ , hidden_states=outputs.hidden_states )
3
1
'''simple docstring''' from cva import destroyAllWindows, imread, imshow, waitKey def A_( A : Tuple): # getting number of pixels in the image UpperCamelCase , UpperCamelCase = img.shape[0], img.shape[1] # converting each pixel's color to its negative for i in range(A): for j in range(A): UpperCamelCase = [255, 255, 255] - img[i][j] return img if __name__ == "__main__": # read original image lowerCAmelCase : int = imread('image_data/lena.jpg', 1) # convert to its negative lowerCAmelCase : List[Any] = convert_to_negative(img) # show result image imshow('negative of original image', img) waitKey(0) destroyAllWindows()
3
'''simple docstring''' from collections import OrderedDict from typing import Any, Mapping, Optional, Union from ...configuration_utils import PretrainedConfig from ...feature_extraction_utils import FeatureExtractionMixin from ...onnx import OnnxConfig from ...onnx.utils import compute_effective_axis_dimension from ...tokenization_utils_base import PreTrainedTokenizerBase from ...utils import TensorType, logging lowerCAmelCase : Any = logging.get_logger(__name__) lowerCAmelCase : Optional[int] = { 'deepmind/language-perceiver': 'https://huggingface.co/deepmind/language-perceiver/resolve/main/config.json', # See all Perceiver models at https://huggingface.co/models?filter=perceiver } class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = """perceiver""" def __init__( self , A_=256 , A_=1280 , A_=768 , A_=1 , A_=26 , A_=8 , A_=8 , A_=None , A_=None , A_="kv" , A_=1 , A_=1 , A_="gelu" , A_=0.1 , A_=0.02 , A_=1e-12 , A_=True , A_=262 , A_=2048 , A_=56 , A_=[368, 496] , A_=16 , A_=1920 , A_=16 , A_=[1, 16, 224, 224] , **A_ , )-> str: '''simple docstring''' super().__init__(**A_ ) UpperCamelCase = num_latents UpperCamelCase = d_latents UpperCamelCase = d_model UpperCamelCase = num_blocks UpperCamelCase = num_self_attends_per_block UpperCamelCase = num_self_attention_heads UpperCamelCase = num_cross_attention_heads UpperCamelCase = qk_channels UpperCamelCase = v_channels UpperCamelCase = cross_attention_shape_for_attention UpperCamelCase = self_attention_widening_factor UpperCamelCase = cross_attention_widening_factor UpperCamelCase = hidden_act UpperCamelCase = attention_probs_dropout_prob UpperCamelCase = initializer_range UpperCamelCase = layer_norm_eps UpperCamelCase = use_query_residual # masked language modeling attributes UpperCamelCase = vocab_size UpperCamelCase = max_position_embeddings # image classification attributes UpperCamelCase = image_size # flow attributes UpperCamelCase = train_size # multimodal autoencoding attributes UpperCamelCase = num_frames UpperCamelCase = audio_samples_per_frame UpperCamelCase = samples_per_patch UpperCamelCase = output_shape class SCREAMING_SNAKE_CASE__ ( snake_case_): @property def UpperCAmelCase_ ( self )-> Mapping[str, Mapping[int, str]]: '''simple docstring''' if self.task == "multiple-choice": UpperCamelCase = {0: 'batch', 1: 'choice', 2: 'sequence'} else: UpperCamelCase = {0: 'batch', 1: 'sequence'} return OrderedDict( [ ('inputs', dynamic_axis), ('attention_mask', dynamic_axis), ] ) @property def UpperCAmelCase_ ( self )-> float: '''simple docstring''' return 1e-4 def UpperCAmelCase_ ( self , A_ , A_ = -1 , A_ = -1 , A_ = -1 , A_ = False , A_ = None , A_ = 3 , A_ = 40 , A_ = 40 , )-> Mapping[str, Any]: '''simple docstring''' if isinstance(A_ , A_ ): # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX UpperCamelCase = compute_effective_axis_dimension( A_ , fixed_dimension=OnnxConfig.default_fixed_batch , num_token_to_add=0 ) # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX UpperCamelCase = preprocessor.num_special_tokens_to_add(A_ ) UpperCamelCase = compute_effective_axis_dimension( A_ , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=A_ ) # Generate dummy inputs according to compute batch and sequence UpperCamelCase = [' '.join(['a'] ) * seq_length] * batch_size UpperCamelCase = dict(preprocessor(A_ , return_tensors=A_ ) ) UpperCamelCase = inputs.pop('input_ids' ) return inputs elif isinstance(A_ , A_ ) and preprocessor.model_input_names[0] == "pixel_values": # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX UpperCamelCase = compute_effective_axis_dimension(A_ , fixed_dimension=OnnxConfig.default_fixed_batch ) UpperCamelCase = self._generate_dummy_images(A_ , A_ , A_ , A_ ) UpperCamelCase = dict(preprocessor(images=A_ , return_tensors=A_ ) ) UpperCamelCase = inputs.pop('pixel_values' ) return inputs else: raise ValueError( 'Unable to generate dummy inputs for the model. Please provide a tokenizer or a preprocessor.' )
3
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 ( SwiftFormerConfig, SwiftFormerForImageClassification, ViTImageProcessor, ) from transformers.utils import logging logging.set_verbosity_info() lowerCAmelCase : int = logging.get_logger(__name__) lowerCAmelCase : Any = torch.device('cpu') def A_( ): UpperCamelCase = 'http://images.cocodataset.org/val2017/000000039769.jpg' UpperCamelCase = Image.open(requests.get(A , stream=A).raw) return im def A_( A : Optional[int]): if swiftformer_name == "swiftformer_xs": return torch.tensor([-2.1703E00, 2.1107E00, -2.0811E00, 8.8685E-01, 2.4360E-01]) elif swiftformer_name == "swiftformer_s": return torch.tensor([3.9636E-01, 2.3478E-01, -1.6963E00, -1.7381E00, -8.6337E-01]) elif swiftformer_name == "swiftformer_l1": return torch.tensor([-4.2768E-01, -4.7429E-01, -1.0897E00, -1.0248E00, 3.5523E-02]) elif swiftformer_name == "swiftformer_l3": return torch.tensor([-2.5330E-01, 2.4211E-01, -6.0185E-01, -8.2789E-01, -6.0446E-02]) def A_( A : str , A : List[Any] , A : int): UpperCamelCase = dct.pop(A) UpperCamelCase = val def A_( A : int): UpperCamelCase = [] for k in state_dict.keys(): UpperCamelCase = k if ".pwconv" in k: UpperCamelCase = k_new.replace('.pwconv' , '.point_wise_conv') if ".dwconv" in k: UpperCamelCase = k_new.replace('.dwconv' , '.depth_wise_conv') if ".Proj." in k: UpperCamelCase = k_new.replace('.Proj.' , '.proj.') if "patch_embed" in k_new: UpperCamelCase = k_new.replace('patch_embed' , 'swiftformer.patch_embed.patch_embedding') if "network" in k_new: UpperCamelCase = k_new.split('.') if ls[2].isdigit(): UpperCamelCase = 'swiftformer.encoder.network.' + ls[1] + '.blocks.' + ls[2] + '.' + '.'.join(ls[3:]) else: UpperCamelCase = k_new.replace('network' , 'swiftformer.encoder.network') rename_keys.append((k, k_new)) return rename_keys @torch.no_grad() def A_( A : List[str] , A : Optional[int] , A : Tuple): UpperCamelCase = SwiftFormerConfig() # dataset (ImageNet-21k only or also fine-tuned on ImageNet 2012), patch_size and image_size UpperCamelCase = 1000 UpperCamelCase = 'huggingface/label-files' UpperCamelCase = 'imagenet-1k-id2label.json' UpperCamelCase = json.load(open(hf_hub_download(A , A , repo_type='dataset') , 'r')) UpperCamelCase = {int(A): v for k, v in idalabel.items()} UpperCamelCase = idalabel UpperCamelCase = {v: k for k, v in idalabel.items()} # size of the architecture if swiftformer_name == "swiftformer_xs": UpperCamelCase = [3, 3, 6, 4] UpperCamelCase = [48, 56, 112, 220] elif swiftformer_name == "swiftformer_s": UpperCamelCase = [3, 3, 9, 6] UpperCamelCase = [48, 64, 168, 224] elif swiftformer_name == "swiftformer_l1": UpperCamelCase = [4, 3, 10, 5] UpperCamelCase = [48, 96, 192, 384] elif swiftformer_name == "swiftformer_l3": UpperCamelCase = [4, 4, 12, 6] UpperCamelCase = [64, 128, 320, 512] # load state_dict of original model, remove and rename some keys if original_ckpt: if original_ckpt.startswith('https'): UpperCamelCase = torch.hub.load_state_dict_from_url(A , map_location='cpu' , check_hash=A) else: UpperCamelCase = torch.load(A , map_location='cpu') UpperCamelCase = checkpoint UpperCamelCase = create_rename_keys(A) for rename_key_src, rename_key_dest in rename_keys: rename_key(A , A , A) # load HuggingFace model UpperCamelCase = SwiftFormerForImageClassification(A).eval() hf_model.load_state_dict(A) # prepare test inputs UpperCamelCase = prepare_img() UpperCamelCase = ViTImageProcessor.from_pretrained('preprocessor_config') UpperCamelCase = processor(images=A , return_tensors='pt') # compare outputs from both models UpperCamelCase = get_expected_output(A) UpperCamelCase = hf_model(inputs['pixel_values']).logits assert hf_logits.shape == torch.Size([1, 1000]) assert torch.allclose(hf_logits[0, 0:5] , A , atol=1E-3) Path(A).mkdir(exist_ok=A) print(f'''Saving model {swiftformer_name} to {pytorch_dump_folder_path}''') hf_model.save_pretrained(A) if __name__ == "__main__": lowerCAmelCase : Union[str, Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '--swiftformer_name', default='swiftformer_xs', choices=['swiftformer_xs', 'swiftformer_s', 'swiftformer_l1', 'swiftformer_l3'], type=str, help='Name of the SwiftFormer model you\'d like to convert.', ) parser.add_argument( '--pytorch_dump_folder_path', default='./converted_outputs/', type=str, help='Path to the output PyTorch model directory.', ) parser.add_argument('--original_ckpt', default=None, type=str, help='Path to the original model checkpoint.') lowerCAmelCase : List[Any] = parser.parse_args() convert_swiftformer_checkpoint(args.swiftformer_name, args.pytorch_dump_folder_path, args.original_ckpt)
3
'''simple docstring''' from ....configuration_utils import PretrainedConfig from ....utils import logging lowerCAmelCase : Optional[Any] = logging.get_logger(__name__) lowerCAmelCase : Dict = { 'speechbrain/m-ctc-t-large': 'https://huggingface.co/speechbrain/m-ctc-t-large/resolve/main/config.json', # See all M-CTC-T models at https://huggingface.co/models?filter=mctct } class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = """mctct""" def __init__( self , A_=8065 , A_=1536 , A_=36 , A_=6144 , A_=4 , A_=384 , A_=920 , A_=1e-5 , A_=0.3 , A_="relu" , A_=0.02 , A_=0.3 , A_=0.3 , A_=1 , A_=0 , A_=2 , A_=1 , A_=0.3 , A_=1 , A_=(7,) , A_=(3,) , A_=80 , A_=1 , A_=None , A_="sum" , A_=False , **A_ , )-> str: '''simple docstring''' super().__init__(**A_ , pad_token_id=A_ , bos_token_id=A_ , eos_token_id=A_ ) UpperCamelCase = vocab_size UpperCamelCase = hidden_size UpperCamelCase = num_hidden_layers UpperCamelCase = intermediate_size UpperCamelCase = num_attention_heads UpperCamelCase = attention_head_dim UpperCamelCase = max_position_embeddings UpperCamelCase = layer_norm_eps UpperCamelCase = layerdrop UpperCamelCase = hidden_act UpperCamelCase = initializer_range UpperCamelCase = hidden_dropout_prob UpperCamelCase = attention_probs_dropout_prob UpperCamelCase = pad_token_id UpperCamelCase = bos_token_id UpperCamelCase = eos_token_id UpperCamelCase = conv_glu_dim UpperCamelCase = conv_dropout UpperCamelCase = num_conv_layers UpperCamelCase = input_feat_per_channel UpperCamelCase = input_channels UpperCamelCase = conv_channels UpperCamelCase = ctc_loss_reduction UpperCamelCase = ctc_zero_infinity # prevents config testing fail with exporting to json UpperCamelCase = list(A_ ) UpperCamelCase = list(A_ ) if len(self.conv_kernel ) != self.num_conv_layers: raise ValueError( 'Configuration for convolutional module is incorrect. ' 'It is required that `len(config.conv_kernel)` == `config.num_conv_layers` ' F'''but is `len(config.conv_kernel) = {len(self.conv_kernel )}`, ''' F'''`config.num_conv_layers = {self.num_conv_layers}`.''' )
3
1
'''simple docstring''' import argparse import torch from transformers import BertConfig, BertForPreTraining, load_tf_weights_in_bert from transformers.utils import logging logging.set_verbosity_info() def A_( A : Tuple , A : Optional[Any] , A : Dict): # Initialise PyTorch model UpperCamelCase = BertConfig.from_json_file(A) print(f'''Building PyTorch model from configuration: {config}''') UpperCamelCase = BertForPreTraining(A) # Load weights from tf checkpoint load_tf_weights_in_bert(A , A , A) # Save pytorch-model print(f'''Save PyTorch model to {pytorch_dump_path}''') torch.save(model.state_dict() , A) if __name__ == "__main__": lowerCAmelCase : Optional[int] = argparse.ArgumentParser() # Required parameters parser.add_argument( '--tf_checkpoint_path', default=None, type=str, required=True, help='Path to the TensorFlow checkpoint path.' ) parser.add_argument( '--bert_config_file', default=None, type=str, required=True, help=( 'The config json file corresponding to the pre-trained BERT model. \n' 'This specifies the model architecture.' ), ) parser.add_argument( '--pytorch_dump_path', default=None, type=str, required=True, help='Path to the output PyTorch model.' ) lowerCAmelCase : int = parser.parse_args() convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.bert_config_file, args.pytorch_dump_path)
3
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) if is_sentencepiece_available(): from ..ta.tokenization_ta import TaTokenizer else: from ...utils.dummy_sentencepiece_objects import TaTokenizer lowerCAmelCase : Tuple = TaTokenizer if is_tokenizers_available(): from ..ta.tokenization_ta_fast import TaTokenizerFast else: from ...utils.dummy_tokenizers_objects import TaTokenizerFast lowerCAmelCase : Optional[int] = TaTokenizerFast lowerCAmelCase : Any = {'configuration_mt5': ['MT5Config', 'MT5OnnxConfig']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : Optional[int] = [ 'MT5EncoderModel', 'MT5ForConditionalGeneration', 'MT5ForQuestionAnswering', 'MT5Model', 'MT5PreTrainedModel', 'MT5Stack', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : Dict = ['TFMT5EncoderModel', 'TFMT5ForConditionalGeneration', 'TFMT5Model'] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : Optional[Any] = ['FlaxMT5EncoderModel', 'FlaxMT5ForConditionalGeneration', 'FlaxMT5Model'] if TYPE_CHECKING: from .configuration_mta import MTaConfig, MTaOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mta import ( MTaEncoderModel, MTaForConditionalGeneration, MTaForQuestionAnswering, MTaModel, MTaPreTrainedModel, MTaStack, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_mta import TFMTaEncoderModel, TFMTaForConditionalGeneration, TFMTaModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_mta import FlaxMTaEncoderModel, FlaxMTaForConditionalGeneration, FlaxMTaModel else: import sys lowerCAmelCase : Tuple = _LazyModule( __name__, globals()['__file__'], _import_structure, extra_objects={'MT5Tokenizer': MTaTokenizer, 'MT5TokenizerFast': MTaTokenizerFast}, module_spec=__spec__, )
3
1
'''simple docstring''' 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 SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = ["""image_processor""", """tokenizer"""] lowerCAmelCase_ = """Pix2StructImageProcessor""" lowerCAmelCase_ = ("""T5Tokenizer""", """T5TokenizerFast""") def __init__( self , A_ , A_ )-> Any: '''simple docstring''' UpperCamelCase = False super().__init__(A_ , A_ ) def __call__( self , A_=None , A_ = None , A_ = True , A_ = False , A_ = None , A_ = None , A_ = 2048 , A_ = 0 , A_ = None , A_ = None , A_ = False , A_ = False , A_ = False , A_ = False , A_ = False , A_ = True , A_ = None , **A_ , )-> BatchEncoding: '''simple docstring''' if images is None and text is None: raise ValueError('You have to specify either images or text.' ) # Get only text if images is None and not self.image_processor.is_vqa: UpperCamelCase = self.tokenizer UpperCamelCase = self.tokenizer( text=A_ , add_special_tokens=A_ , padding=A_ , truncation=A_ , max_length=A_ , stride=A_ , pad_to_multiple_of=A_ , return_attention_mask=A_ , return_overflowing_tokens=A_ , return_special_tokens_mask=A_ , return_offsets_mapping=A_ , return_token_type_ids=A_ , return_length=A_ , verbose=A_ , return_tensors=A_ , **A_ , ) return text_encoding if not self.image_processor.is_vqa: # add pixel_values UpperCamelCase = self.image_processor( A_ , return_tensors=A_ , max_patches=A_ , **A_ ) else: # add pixel_values and bbox UpperCamelCase = self.image_processor( A_ , return_tensors=A_ , max_patches=A_ , header_text=A_ , **A_ ) if text is not None and not self.image_processor.is_vqa: UpperCamelCase = self.tokenizer( text=A_ , add_special_tokens=A_ , padding=A_ , truncation=A_ , max_length=A_ , stride=A_ , pad_to_multiple_of=A_ , return_attention_mask=A_ , return_overflowing_tokens=A_ , return_special_tokens_mask=A_ , return_offsets_mapping=A_ , return_token_type_ids=A_ , return_length=A_ , verbose=A_ , return_tensors=A_ , **A_ , ) if "attention_mask" in text_encoding: UpperCamelCase = text_encoding.pop('attention_mask' ) if "input_ids" in text_encoding: UpperCamelCase = text_encoding.pop('input_ids' ) else: UpperCamelCase = None if text_encoding is not None: encoding_image_processor.update(A_ ) return encoding_image_processor def UpperCAmelCase_ ( self , *A_ , **A_ )-> Optional[Any]: '''simple docstring''' return self.tokenizer.batch_decode(*A_ , **A_ ) def UpperCAmelCase_ ( self , *A_ , **A_ )-> List[str]: '''simple docstring''' return self.tokenizer.decode(*A_ , **A_ ) @property def UpperCAmelCase_ ( self )-> Optional[int]: '''simple docstring''' UpperCamelCase = self.tokenizer.model_input_names UpperCamelCase = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) )
3
'''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 SCREAMING_SNAKE_CASE__ ( unittest.TestCase): 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] , )-> Dict: '''simple docstring''' UpperCamelCase = parent UpperCamelCase = batch_size UpperCamelCase = num_channels UpperCamelCase = image_size UpperCamelCase = min_resolution UpperCamelCase = max_resolution UpperCamelCase = do_resize UpperCamelCase = size if size is not None else {'height': 18, 'width': 20} UpperCamelCase = do_thumbnail UpperCamelCase = do_align_axis UpperCamelCase = do_pad UpperCamelCase = do_normalize UpperCamelCase = image_mean UpperCamelCase = image_std def UpperCAmelCase_ ( self )-> List[Any]: '''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 SCREAMING_SNAKE_CASE__ ( snake_case_ , unittest.TestCase): lowerCAmelCase_ = DonutImageProcessor if is_vision_available() else None def UpperCAmelCase_ ( self )-> str: '''simple docstring''' UpperCamelCase = DonutImageProcessingTester(self ) @property def UpperCAmelCase_ ( self )-> str: '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def UpperCAmelCase_ ( self )-> Optional[int]: '''simple docstring''' UpperCamelCase = 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 UpperCAmelCase_ ( self )-> Optional[int]: '''simple docstring''' UpperCamelCase = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'height': 18, 'width': 20} ) UpperCamelCase = 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 UpperCamelCase = self.image_processing_class.from_dict(self.image_processor_dict , size=(42, 84) ) self.assertEqual(image_processor.size , {'height': 84, 'width': 42} ) def UpperCAmelCase_ ( self )-> Tuple: '''simple docstring''' pass @is_flaky() def UpperCAmelCase_ ( self )-> Any: '''simple docstring''' UpperCamelCase = self.image_processing_class(**self.image_processor_dict ) # create random PIL images UpperCamelCase = prepare_image_inputs(self.image_processor_tester , equal_resolution=A_ ) for image in image_inputs: self.assertIsInstance(A_ , Image.Image ) # Test not batched input UpperCamelCase = 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 UpperCamelCase = 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 UpperCAmelCase_ ( self )-> Optional[int]: '''simple docstring''' UpperCamelCase = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors UpperCamelCase = prepare_image_inputs(self.image_processor_tester , equal_resolution=A_ , numpify=A_ ) for image in image_inputs: self.assertIsInstance(A_ , np.ndarray ) # Test not batched input UpperCamelCase = 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 UpperCamelCase = 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 UpperCAmelCase_ ( self )-> Dict: '''simple docstring''' UpperCamelCase = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors UpperCamelCase = prepare_image_inputs(self.image_processor_tester , equal_resolution=A_ , torchify=A_ ) for image in image_inputs: self.assertIsInstance(A_ , torch.Tensor ) # Test not batched input UpperCamelCase = 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 UpperCamelCase = 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'], ) , )
3
1
'''simple docstring''' from typing import List from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase : Union[str, Any] = logging.get_logger(__name__) lowerCAmelCase : Optional[Any] = { 'snap-research/efficientformer-l1-300': ( 'https://huggingface.co/snap-research/efficientformer-l1-300/resolve/main/config.json' ), } class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = """efficientformer""" def __init__( self , A_ = [3, 2, 6, 4] , A_ = [48, 96, 224, 448] , A_ = [True, True, True, True] , A_ = 448 , A_ = 32 , A_ = 4 , A_ = 7 , A_ = 5 , A_ = 8 , A_ = 4 , A_ = 0.0 , A_ = 16 , A_ = 3 , A_ = 3 , A_ = 3 , A_ = 2 , A_ = 1 , A_ = 0.0 , A_ = 1 , A_ = True , A_ = True , A_ = 1e-5 , A_ = "gelu" , A_ = 0.02 , A_ = 1e-12 , A_ = 224 , A_ = 1e-05 , **A_ , )-> None: '''simple docstring''' super().__init__(**A_ ) UpperCamelCase = hidden_act UpperCamelCase = hidden_dropout_prob UpperCamelCase = hidden_sizes UpperCamelCase = num_hidden_layers UpperCamelCase = num_attention_heads UpperCamelCase = initializer_range UpperCamelCase = layer_norm_eps UpperCamelCase = patch_size UpperCamelCase = num_channels UpperCamelCase = depths UpperCamelCase = mlp_expansion_ratio UpperCamelCase = downsamples UpperCamelCase = dim UpperCamelCase = key_dim UpperCamelCase = attention_ratio UpperCamelCase = resolution UpperCamelCase = pool_size UpperCamelCase = downsample_patch_size UpperCamelCase = downsample_stride UpperCamelCase = downsample_pad UpperCamelCase = drop_path_rate UpperCamelCase = num_metaad_blocks UpperCamelCase = distillation UpperCamelCase = use_layer_scale UpperCamelCase = layer_scale_init_value UpperCamelCase = image_size UpperCamelCase = batch_norm_eps
3
'''simple docstring''' def A_( A : list[int]): UpperCamelCase = [] if len(A) == 1: return [nums.copy()] for _ in range(len(A)): UpperCamelCase = nums.pop(0) UpperCamelCase = permute(A) for perm in permutations: perm.append(A) result.extend(A) nums.append(A) return result def A_( A : str): def backtrack(A : str): if start == len(A) - 1: output.append(nums[:]) else: for i in range(A , len(A)): UpperCamelCase , UpperCamelCase = nums[i], nums[start] backtrack(start + 1) UpperCamelCase , UpperCamelCase = nums[i], nums[start] # backtrack UpperCamelCase = [] backtrack(0) return output if __name__ == "__main__": import doctest # use res to print the data in permute2 function lowerCAmelCase : Dict = permutea([1, 2, 3]) print(res) doctest.testmod()
3
1
'''simple docstring''' import argparse import os from pathlib import Path from typing import Dict import tensorflow as tf import torch from tqdm import tqdm from transformers import PegasusConfig, PegasusForConditionalGeneration, PegasusTokenizer from transformers.models.pegasus.configuration_pegasus import DEFAULTS, task_specific_params lowerCAmelCase : Union[str, Any] = [ # replace left string with right string to get the relevant state_dict key (identical state dict to bart) ['memory_attention', 'encoder_attn'], ['attention', 'attn'], ['/', '.'], ['.LayerNorm.gamma', '_layer_norm.weight'], ['.LayerNorm.beta', '_layer_norm.bias'], ['r.layer_', 'r.layers.'], ['output_proj', 'out_proj'], ['ffn.dense_1.', 'fc2.'], ['ffn.dense.', 'fc1.'], ['ffn_layer_norm', 'final_layer_norm'], ['kernel', 'weight'], ['encoder_layer_norm.', 'encoder.layer_norm.'], ['decoder_layer_norm.', 'decoder.layer_norm.'], ['embeddings.weights', 'shared.weight'], ] def A_( A : Dict): for pegasus_name, hf_name in PATTERNS: UpperCamelCase = k.replace(A , A) return k def A_( A : dict , A : dict): UpperCamelCase = DEFAULTS.copy() cfg_kwargs.update(A) UpperCamelCase = PegasusConfig(**A) UpperCamelCase = PegasusForConditionalGeneration(A) UpperCamelCase = torch_model.model.state_dict() UpperCamelCase = {} for k, v in tf_weights.items(): UpperCamelCase = rename_state_dict_key(A) if new_k not in sd: raise ValueError(f'''could not find new key {new_k} in state dict. (converted from {k})''') if "dense" in k or "proj" in new_k: UpperCamelCase = v.T UpperCamelCase = torch.tensor(A , dtype=sd[new_k].dtype) assert v.shape == sd[new_k].shape, f'''{new_k}, {k}, {v.shape}, {sd[new_k].shape}''' # make sure embedding.padding_idx is respected UpperCamelCase = torch.zeros_like(mapping['shared.weight'][cfg.pad_token_id + 1]) UpperCamelCase = mapping['shared.weight'] UpperCamelCase = mapping['shared.weight'] UpperCamelCase = {k: torch.zeros_like(A) for k, v in sd.items() if k.endswith('bias') and k not in mapping} mapping.update(**A) UpperCamelCase , UpperCamelCase = torch_model.model.load_state_dict(A , strict=A) UpperCamelCase = [ k for k in missing if k not in ['encoder.embed_positions.weight', 'decoder.embed_positions.weight'] ] assert unexpected_missing == [], f'''no matches found for the following torch keys {unexpected_missing}''' assert extra == [], f'''no matches found for the following tf keys {extra}''' return torch_model def A_( A : Tuple="./ckpt/aeslc/model.ckpt-32000"): UpperCamelCase = tf.train.list_variables(A) UpperCamelCase = {} UpperCamelCase = ['Adafactor', 'global_step'] for name, shape in tqdm(A , desc='converting tf checkpoint to dict'): UpperCamelCase = any(pat in name for pat in ignore_name) if skip_key: continue UpperCamelCase = tf.train.load_variable(A , A) UpperCamelCase = array return tf_weights def A_( A : str , A : str): # save tokenizer first UpperCamelCase = Path(A).parent.name UpperCamelCase = task_specific_params[f'''summarization_{dataset}''']['max_position_embeddings'] UpperCamelCase = PegasusTokenizer.from_pretrained('sshleifer/pegasus' , model_max_length=A) assert tok.model_max_length == desired_max_model_length tok.save_pretrained(A) # convert model UpperCamelCase = get_tf_weights_as_numpy(A) UpperCamelCase = task_specific_params[f'''summarization_{dataset}'''] if dataset == "large": UpperCamelCase = task_specific_params UpperCamelCase = convert_pegasus(A , A) torch_model.save_pretrained(A) UpperCamelCase = torch_model.state_dict() sd.pop('model.decoder.embed_positions.weight') sd.pop('model.encoder.embed_positions.weight') torch.save(A , Path(A) / 'pytorch_model.bin') if __name__ == "__main__": lowerCAmelCase : List[str] = argparse.ArgumentParser() # Required parameters parser.add_argument('tf_ckpt_path', type=str, help='passed to tf.train.list_variables') parser.add_argument('save_dir', default=None, type=str, help='Path to the output PyTorch model.') lowerCAmelCase : Any = parser.parse_args() if args.save_dir is None: lowerCAmelCase : Optional[int] = Path(args.tf_ckpt_path).parent.name lowerCAmelCase : List[Any] = os.path.join('pegasus', dataset) convert_pegasus_ckpt_to_pytorch(args.tf_ckpt_path, args.save_dir)
3
'''simple docstring''' import colorsys from PIL import Image # type: ignore def A_( A : float , A : float , A : int): UpperCamelCase = x UpperCamelCase = y for step in range(A): # noqa: B007 UpperCamelCase = a * a - b * b + x UpperCamelCase = 2 * a * b + y UpperCamelCase = a_new # divergence happens for all complex number with an absolute value # greater than 4 if a * a + b * b > 4: break return step / (max_step - 1) def A_( A : float): if distance == 1: return (0, 0, 0) else: return (255, 255, 255) def A_( A : float): if distance == 1: return (0, 0, 0) else: return tuple(round(i * 255) for i in colorsys.hsv_to_rgb(A , 1 , 1)) def A_( A : int = 800 , A : int = 600 , A : float = -0.6 , A : float = 0 , A : float = 3.2 , A : int = 50 , A : bool = True , ): UpperCamelCase = Image.new('RGB' , (image_width, image_height)) UpperCamelCase = img.load() # loop through the image-coordinates for image_x in range(A): for image_y in range(A): # determine the figure-coordinates based on the image-coordinates UpperCamelCase = figure_width / image_width * image_height UpperCamelCase = figure_center_x + (image_x / image_width - 0.5) * figure_width UpperCamelCase = figure_center_y + (image_y / image_height - 0.5) * figure_height UpperCamelCase = get_distance(A , A , A) # color the corresponding pixel based on the selected coloring-function if use_distance_color_coding: UpperCamelCase = get_color_coded_rgb(A) else: UpperCamelCase = get_black_and_white_rgb(A) return img if __name__ == "__main__": import doctest doctest.testmod() # colored version, full figure lowerCAmelCase : Any = get_image() # uncomment for colored version, different section, zoomed in # img = get_image(figure_center_x = -0.6, figure_center_y = -0.4, # figure_width = 0.8) # uncomment for black and white version, full figure # img = get_image(use_distance_color_coding = False) # uncomment to save the image # img.save("mandelbrot.png") img.show()
3
1
'''simple docstring''' import argparse from argparse import Namespace import torch from torch import nn from transformers import XGLMConfig, XGLMForCausalLM def A_( A : Any): UpperCamelCase = [ 'decoder.version', 'decoder.output_projection.weight', '_float_tensor', 'decoder.embed_positions._float_tensor', ] for k in ignore_keys: state_dict.pop(A , A) def A_( A : Union[str, Any]): UpperCamelCase , UpperCamelCase = emb.weight.shape UpperCamelCase = nn.Linear(A , A , bias=A) UpperCamelCase = emb.weight.data return lin_layer def A_( A : Optional[int]): UpperCamelCase = torch.load(A , map_location='cpu') UpperCamelCase = Namespace(**checkpoint['cfg']['model']) UpperCamelCase = checkpoint['model'] remove_ignore_keys_(A) UpperCamelCase = state_dict['decoder.embed_tokens.weight'].shape[0] UpperCamelCase = {key.replace('decoder' , 'model'): val for key, val in state_dict.items()} UpperCamelCase = XGLMConfig( vocab_size=A , max_position_embeddings=args.max_target_positions , num_layers=args.decoder_layers , attention_heads=args.decoder_attention_heads , ffn_dim=args.decoder_ffn_embed_dim , d_model=args.decoder_embed_dim , layerdrop=args.decoder_layerdrop , dropout=args.dropout , attention_dropout=args.attention_dropout , activation_dropout=args.activation_dropout , activation_function='gelu' , scale_embedding=not args.no_scale_embedding , tie_word_embeddings=args.share_decoder_input_output_embed , ) UpperCamelCase = XGLMForCausalLM(A) UpperCamelCase = model.load_state_dict(A , strict=A) print(A) UpperCamelCase = make_linear_from_emb(model.model.embed_tokens) return model if __name__ == "__main__": lowerCAmelCase : Optional[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument('fairseq_path', type=str, help='path to a model.pt on local filesystem.') parser.add_argument('pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.') lowerCAmelCase : str = parser.parse_args() lowerCAmelCase : Dict = convert_fairseq_xglm_checkpoint_from_disk(args.fairseq_path) model.save_pretrained(args.pytorch_dump_folder_path)
3
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_torch_available, ) lowerCAmelCase : Optional[Any] = { 'configuration_falcon': ['FALCON_PRETRAINED_CONFIG_ARCHIVE_MAP', 'FalconConfig'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : str = [ 'FALCON_PRETRAINED_MODEL_ARCHIVE_LIST', 'FalconForCausalLM', 'FalconModel', 'FalconPreTrainedModel', 'FalconForSequenceClassification', 'FalconForTokenClassification', 'FalconForQuestionAnswering', ] if TYPE_CHECKING: from .configuration_falcon import FALCON_PRETRAINED_CONFIG_ARCHIVE_MAP, FalconConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_falcon import ( FALCON_PRETRAINED_MODEL_ARCHIVE_LIST, FalconForCausalLM, FalconForQuestionAnswering, FalconForSequenceClassification, FalconForTokenClassification, FalconModel, FalconPreTrainedModel, ) else: import sys lowerCAmelCase : Tuple = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
3
1
'''simple docstring''' import unittest from transformers import is_flax_available from transformers.testing_utils import require_flax, require_sentencepiece, require_tokenizers, require_torch, slow if is_flax_available(): import optax from flax.training.common_utils import onehot from transformers import AutoTokenizer, FlaxMTaForConditionalGeneration from transformers.models.ta.modeling_flax_ta import shift_tokens_right @require_torch @require_sentencepiece @require_tokenizers @require_flax class SCREAMING_SNAKE_CASE__ ( unittest.TestCase): @slow def UpperCAmelCase_ ( self )-> Dict: '''simple docstring''' UpperCamelCase = FlaxMTaForConditionalGeneration.from_pretrained('google/mt5-small' ) UpperCamelCase = AutoTokenizer.from_pretrained('google/mt5-small' ) UpperCamelCase = tokenizer('Hello there' , return_tensors='np' ).input_ids UpperCamelCase = tokenizer('Hi I am' , return_tensors='np' ).input_ids UpperCamelCase = shift_tokens_right(A_ , model.config.pad_token_id , model.config.decoder_start_token_id ) UpperCamelCase = model(A_ , decoder_input_ids=A_ ).logits UpperCamelCase = optax.softmax_cross_entropy(A_ , onehot(A_ , logits.shape[-1] ) ).mean() UpperCamelCase = -(labels.shape[-1] * loss.item()) UpperCamelCase = -84.9_127 self.assertTrue(abs(mtf_score - EXPECTED_SCORE ) < 1e-4 )
3
'''simple docstring''' lowerCAmelCase : Optional[Any] = { 'A': ['B', 'C', 'E'], 'B': ['A', 'D', 'E'], 'C': ['A', 'F', 'G'], 'D': ['B'], 'E': ['A', 'B', 'D'], 'F': ['C'], 'G': ['C'], } def A_( A : dict , A : str , A : Optional[Any]): UpperCamelCase = set() # keep track of all the paths to be checked UpperCamelCase = [[start]] # return path if start is goal if start == goal: return [start] # keeps looping until all possible paths have been checked while queue: # pop the first path from the queue UpperCamelCase = queue.pop(0) # get the last node from the path UpperCamelCase = path[-1] if node not in explored: UpperCamelCase = graph[node] # go through all neighbour nodes, construct a new path and # push it into the queue for neighbour in neighbours: UpperCamelCase = list(A) new_path.append(A) queue.append(A) # return path if neighbour is goal if neighbour == goal: return new_path # mark node as explored explored.add(A) # in case there's no path between the 2 nodes return [] def A_( A : dict , A : str , A : Tuple): if not graph or start not in graph or target not in graph: return -1 if start == target: return 0 UpperCamelCase = [start] UpperCamelCase = set(A) # Keep tab on distances from `start` node. UpperCamelCase = {start: 0, target: -1} while queue: UpperCamelCase = queue.pop(0) if node == target: UpperCamelCase = ( dist[node] if dist[target] == -1 else min(dist[target] , dist[node]) ) for adjacent in graph[node]: if adjacent not in visited: visited.add(A) queue.append(A) UpperCamelCase = dist[node] + 1 return dist[target] if __name__ == "__main__": print(bfs_shortest_path(demo_graph, 'G', 'D')) # returns ['G', 'C', 'A', 'B', 'D'] print(bfs_shortest_path_distance(demo_graph, 'G', 'D')) # returns 4
3
1
'''simple docstring''' from __future__ import annotations import numpy as np def A_( A : list[float]): return np.maximum(0 , A) if __name__ == "__main__": print(np.array(relu([-1, 0, 5]))) # --> [0, 0, 5]
3
'''simple docstring''' import copy import os import cva import numpy as np from matplotlib import pyplot as plt class SCREAMING_SNAKE_CASE__ : def __init__( self )-> Dict: '''simple docstring''' UpperCamelCase = '' UpperCamelCase = '' UpperCamelCase = [] UpperCamelCase = 0 UpperCamelCase = 256 UpperCamelCase = 0 UpperCamelCase = 0 UpperCamelCase = 0 UpperCamelCase = 0 def UpperCAmelCase_ ( self , A_ )-> str: '''simple docstring''' UpperCamelCase = cva.imread(A_ , 0 ) UpperCamelCase = copy.deepcopy(self.img ) UpperCamelCase , UpperCamelCase , UpperCamelCase = plt.hist(self.img.ravel() , 256 , [0, 256] , label='x' ) UpperCamelCase = np.sum(A_ ) for i in range(len(A_ ) ): UpperCamelCase = x[i] / self.k self.sk += prk UpperCamelCase = (self.L - 1) * self.sk if self.rem != 0: UpperCamelCase = int(last % last ) UpperCamelCase = int(last + 1 if self.rem >= 0.5 else last ) self.last_list.append(A_ ) UpperCamelCase = int(np.ma.count(self.img ) / self.img[1].size ) UpperCamelCase = self.img[1].size for i in range(self.number_of_cols ): for j in range(self.number_of_rows ): UpperCamelCase = self.img[j][i] if num != self.last_list[num]: UpperCamelCase = self.last_list[num] cva.imwrite('output_data/output.jpg' , self.img ) def UpperCAmelCase_ ( self )-> Any: '''simple docstring''' plt.hist(self.img.ravel() , 256 , [0, 256] ) def UpperCAmelCase_ ( self )-> Optional[Any]: '''simple docstring''' cva.imshow('Output-Image' , self.img ) cva.imshow('Input-Image' , self.original_image ) cva.waitKey(5000 ) cva.destroyAllWindows() if __name__ == "__main__": lowerCAmelCase : Union[str, Any] = os.path.join(os.path.basename(__file__), 'image_data/input.jpg') lowerCAmelCase : str = ConstantStretch() stretcher.stretch(file_path) stretcher.plot_histogram() stretcher.show_image()
3
1
'''simple docstring''' import inspect import os import unittest from dataclasses import dataclass import torch from accelerate import Accelerator, DistributedDataParallelKwargs, GradScalerKwargs from accelerate.state import AcceleratorState from accelerate.test_utils import execute_subprocess_async, require_cuda, require_multi_gpu from accelerate.utils import KwargsHandler @dataclass class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = 0 lowerCAmelCase_ = False lowerCAmelCase_ = 3.0 class SCREAMING_SNAKE_CASE__ ( unittest.TestCase): def UpperCAmelCase_ ( self )-> int: '''simple docstring''' self.assertDictEqual(MockClass().to_kwargs() , {} ) self.assertDictEqual(MockClass(a=2 ).to_kwargs() , {'a': 2} ) self.assertDictEqual(MockClass(a=2 , b=A_ ).to_kwargs() , {'a': 2, 'b': True} ) self.assertDictEqual(MockClass(a=2 , c=2.25 ).to_kwargs() , {'a': 2, 'c': 2.25} ) @require_cuda def UpperCAmelCase_ ( self )-> Dict: '''simple docstring''' UpperCamelCase = GradScalerKwargs(init_scale=1024 , growth_factor=2 ) AcceleratorState._reset_state() UpperCamelCase = Accelerator(mixed_precision='fp16' , kwargs_handlers=[scaler_handler] ) print(accelerator.use_fpaa ) UpperCamelCase = accelerator.scaler # Check the kwargs have been applied self.assertEqual(scaler._init_scale , 1_024.0 ) self.assertEqual(scaler._growth_factor , 2.0 ) # Check the other values are at the default self.assertEqual(scaler._backoff_factor , 0.5 ) self.assertEqual(scaler._growth_interval , 2000 ) self.assertEqual(scaler._enabled , A_ ) @require_multi_gpu def UpperCAmelCase_ ( self )-> Dict: '''simple docstring''' UpperCamelCase = ['torchrun', F'''--nproc_per_node={torch.cuda.device_count()}''', inspect.getfile(self.__class__ )] execute_subprocess_async(A_ , env=os.environ.copy() ) if __name__ == "__main__": lowerCAmelCase : Tuple = DistributedDataParallelKwargs(bucket_cap_mb=15, find_unused_parameters=True) lowerCAmelCase : List[str] = Accelerator(kwargs_handlers=[ddp_scaler]) lowerCAmelCase : List[Any] = torch.nn.Linear(1_00, 2_00) lowerCAmelCase : int = accelerator.prepare(model) # Check the values changed in kwargs lowerCAmelCase : Dict = '' lowerCAmelCase : Dict = model.bucket_bytes_cap // (10_24 * 10_24) if observed_bucket_cap_map != 15: error_msg += f"Kwargs badly passed, should have `15` but found {observed_bucket_cap_map}.\n" if model.find_unused_parameters is not True: error_msg += f"Kwargs badly passed, should have `True` but found {model.find_unused_parameters}.\n" # Check the values of the defaults if model.dim != 0: error_msg += f"Default value not respected, should have `0` but found {model.dim}.\n" if model.broadcast_buffers is not True: error_msg += f"Default value not respected, should have `True` but found {model.broadcast_buffers}.\n" if model.gradient_as_bucket_view is not False: error_msg += f"Default value not respected, should have `False` but found {model.gradient_as_bucket_view}.\n" # Raise error at the end to make sure we don't stop at the first failure. if len(error_msg) > 0: raise ValueError(error_msg)
3
'''simple docstring''' import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase : int = logging.get_logger(__name__) lowerCAmelCase : Tuple = { 'microsoft/unispeech-sat-base-100h-libri-ft': ( 'https://huggingface.co/microsoft/unispeech-sat-base-100h-libri-ft/resolve/main/config.json' ), # See all UniSpeechSat models at https://huggingface.co/models?filter=unispeech_sat } class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = """unispeech-sat""" def __init__( self , A_=32 , A_=768 , A_=12 , A_=12 , A_=3072 , A_="gelu" , A_=0.1 , A_=0.1 , A_=0.1 , A_=0.0 , A_=0.0 , A_=0.1 , A_=0.1 , A_=0.02 , A_=1e-5 , A_="group" , A_="gelu" , A_=(512, 512, 512, 512, 512, 512, 512) , A_=(5, 2, 2, 2, 2, 2, 2) , A_=(10, 3, 3, 3, 3, 2, 2) , A_=False , A_=128 , A_=16 , A_=False , A_=True , A_=0.05 , A_=10 , A_=2 , A_=0.0 , A_=10 , A_=0 , A_=320 , A_=2 , A_=0.1 , A_=100 , A_=256 , A_=256 , A_=0.1 , A_="mean" , A_=False , A_=False , A_=256 , A_=(512, 512, 512, 512, 1500) , A_=(5, 3, 3, 1, 1) , A_=(1, 2, 3, 1, 1) , A_=512 , A_=0 , A_=1 , A_=2 , A_=504 , **A_ , )-> Tuple: '''simple docstring''' super().__init__(**A_ , pad_token_id=A_ , bos_token_id=A_ , eos_token_id=A_ ) UpperCamelCase = hidden_size UpperCamelCase = feat_extract_norm UpperCamelCase = feat_extract_activation UpperCamelCase = list(A_ ) UpperCamelCase = list(A_ ) UpperCamelCase = list(A_ ) UpperCamelCase = conv_bias UpperCamelCase = num_conv_pos_embeddings UpperCamelCase = num_conv_pos_embedding_groups UpperCamelCase = len(self.conv_dim ) UpperCamelCase = num_hidden_layers UpperCamelCase = intermediate_size UpperCamelCase = hidden_act UpperCamelCase = num_attention_heads UpperCamelCase = hidden_dropout UpperCamelCase = attention_dropout UpperCamelCase = activation_dropout UpperCamelCase = feat_proj_dropout UpperCamelCase = final_dropout UpperCamelCase = layerdrop UpperCamelCase = layer_norm_eps UpperCamelCase = initializer_range UpperCamelCase = vocab_size UpperCamelCase = num_clusters UpperCamelCase = do_stable_layer_norm UpperCamelCase = use_weighted_layer_sum 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)`, but is `len(config.conv_dim) =' F''' {len(self.conv_dim )}`, `len(config.conv_stride) = {len(self.conv_stride )}`,''' F''' `len(config.conv_kernel) = {len(self.conv_kernel )}`.''' ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 UpperCamelCase = apply_spec_augment UpperCamelCase = mask_time_prob UpperCamelCase = mask_time_length UpperCamelCase = mask_time_min_masks UpperCamelCase = mask_feature_prob UpperCamelCase = mask_feature_length UpperCamelCase = mask_feature_min_masks # parameters for pretraining with codevector quantized representations UpperCamelCase = num_codevectors_per_group UpperCamelCase = num_codevector_groups UpperCamelCase = contrastive_logits_temperature UpperCamelCase = feat_quantizer_dropout UpperCamelCase = num_negatives UpperCamelCase = codevector_dim UpperCamelCase = proj_codevector_dim UpperCamelCase = diversity_loss_weight # ctc loss UpperCamelCase = ctc_loss_reduction UpperCamelCase = ctc_zero_infinity # SequenceClassification-specific parameter. Feel free to ignore for other classes. UpperCamelCase = classifier_proj_size # XVector-specific parameters. Feel free to ignore for other classes. UpperCamelCase = list(A_ ) UpperCamelCase = list(A_ ) UpperCamelCase = list(A_ ) UpperCamelCase = xvector_output_dim @property def UpperCAmelCase_ ( self )-> Optional[Any]: '''simple docstring''' return functools.reduce(operator.mul , self.conv_stride , 1 )
3
1
'''simple docstring''' import gc import unittest from parameterized import parameterized from diffusers import FlaxUNetaDConditionModel from diffusers.utils import is_flax_available from diffusers.utils.testing_utils import load_hf_numpy, require_flax, slow if is_flax_available(): import jax import jax.numpy as jnp @slow @require_flax class SCREAMING_SNAKE_CASE__ ( unittest.TestCase): def UpperCAmelCase_ ( self , A_ , A_ )-> Optional[Any]: '''simple docstring''' return F'''gaussian_noise_s={seed}_shape={"_".join([str(A_ ) for s in shape] )}.npy''' def UpperCAmelCase_ ( self )-> Any: '''simple docstring''' super().tearDown() gc.collect() def UpperCAmelCase_ ( self , A_=0 , A_=(4, 4, 64, 64) , A_=False )-> List[str]: '''simple docstring''' UpperCamelCase = jnp.bfloataa if fpaa else jnp.floataa UpperCamelCase = jnp.array(load_hf_numpy(self.get_file_format(A_ , A_ ) ) , dtype=A_ ) return image def UpperCAmelCase_ ( self , A_=False , A_="CompVis/stable-diffusion-v1-4" )-> Union[str, Any]: '''simple docstring''' UpperCamelCase = jnp.bfloataa if fpaa else jnp.floataa UpperCamelCase = 'bf16' if fpaa else None UpperCamelCase , UpperCamelCase = FlaxUNetaDConditionModel.from_pretrained( A_ , subfolder='unet' , dtype=A_ , revision=A_ ) return model, params def UpperCAmelCase_ ( self , A_=0 , A_=(4, 77, 768) , A_=False )-> Optional[int]: '''simple docstring''' UpperCamelCase = jnp.bfloataa if fpaa else jnp.floataa UpperCamelCase = jnp.array(load_hf_numpy(self.get_file_format(A_ , A_ ) ) , dtype=A_ ) return hidden_states @parameterized.expand( [ # fmt: off [83, 4, [-0.2_323, -0.1_304, 0.0_813, -0.3_093, -0.0_919, -0.1_571, -0.1_125, -0.5_806]], [17, 0.55, [-0.0_831, -0.2_443, 0.0_901, -0.0_919, 0.3_396, 0.0_103, -0.3_743, 0.0_701]], [8, 0.89, [-0.4_863, 0.0_859, 0.0_875, -0.1_658, 0.9_199, -0.0_114, 0.4_839, 0.4_639]], [3, 1000, [-0.5_649, 0.2_402, -0.5_518, 0.1_248, 1.1_328, -0.2_443, -0.0_325, -1.0_078]], # fmt: on ] ) def UpperCAmelCase_ ( self , A_ , A_ , A_ )-> List[Any]: '''simple docstring''' UpperCamelCase , UpperCamelCase = self.get_unet_model(model_id='CompVis/stable-diffusion-v1-4' , fpaa=A_ ) UpperCamelCase = self.get_latents(A_ , fpaa=A_ ) UpperCamelCase = self.get_encoder_hidden_states(A_ , fpaa=A_ ) UpperCamelCase = model.apply( {'params': params} , A_ , jnp.array(A_ , dtype=jnp.intaa ) , encoder_hidden_states=A_ , ).sample assert sample.shape == latents.shape UpperCamelCase = jnp.asarray(jax.device_get((sample[-1, -2:, -2:, :2].flatten()) ) , dtype=jnp.floataa ) UpperCamelCase = jnp.array(A_ , dtype=jnp.floataa ) # Found torch (float16) and flax (bfloat16) outputs to be within this tolerance, in the same hardware assert jnp.allclose(A_ , A_ , atol=1e-2 ) @parameterized.expand( [ # fmt: off [83, 4, [0.1_514, 0.0_807, 0.1_624, 0.1_016, -0.1_896, 0.0_263, 0.0_677, 0.2_310]], [17, 0.55, [0.1_164, -0.0_216, 0.0_170, 0.1_589, -0.3_120, 0.1_005, -0.0_581, -0.1_458]], [8, 0.89, [-0.1_758, -0.0_169, 0.1_004, -0.1_411, 0.1_312, 0.1_103, -0.1_996, 0.2_139]], [3, 1000, [0.1_214, 0.0_352, -0.0_731, -0.1_562, -0.0_994, -0.0_906, -0.2_340, -0.0_539]], # fmt: on ] ) def UpperCAmelCase_ ( self , A_ , A_ , A_ )-> Any: '''simple docstring''' UpperCamelCase , UpperCamelCase = self.get_unet_model(model_id='stabilityai/stable-diffusion-2' , fpaa=A_ ) UpperCamelCase = self.get_latents(A_ , shape=(4, 4, 96, 96) , fpaa=A_ ) UpperCamelCase = self.get_encoder_hidden_states(A_ , shape=(4, 77, 1024) , fpaa=A_ ) UpperCamelCase = model.apply( {'params': params} , A_ , jnp.array(A_ , dtype=jnp.intaa ) , encoder_hidden_states=A_ , ).sample assert sample.shape == latents.shape UpperCamelCase = jnp.asarray(jax.device_get((sample[-1, -2:, -2:, :2].flatten()) ) , dtype=jnp.floataa ) UpperCamelCase = jnp.array(A_ , dtype=jnp.floataa ) # Found torch (float16) and flax (bfloat16) outputs to be within this tolerance, on the same hardware assert jnp.allclose(A_ , A_ , atol=1e-2 )
3
'''simple docstring''' import inspect import unittest from datasets import load_dataset from packaging import version from transformers import BeitConfig from transformers.models.auto import get_values from transformers.testing_utils import require_torch, require_torch_multi_gpu, 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, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ( MODEL_MAPPING, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation, BeitModel, ) from transformers.models.beit.modeling_beit import BEIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): import PIL from PIL import Image from transformers import BeitImageProcessor class SCREAMING_SNAKE_CASE__ : def __init__( self , A_ , A_=100 , A_=13 , A_=30 , A_=2 , A_=3 , A_=True , A_=True , A_=32 , A_=4 , A_=4 , A_=37 , A_="gelu" , A_=0.1 , A_=0.1 , A_=10 , A_=0.02 , A_=3 , A_=None , A_=[0, 1, 2, 3] , )-> Any: '''simple docstring''' UpperCamelCase = parent UpperCamelCase = 100 UpperCamelCase = batch_size UpperCamelCase = image_size UpperCamelCase = patch_size UpperCamelCase = num_channels UpperCamelCase = is_training UpperCamelCase = use_labels UpperCamelCase = hidden_size UpperCamelCase = num_hidden_layers UpperCamelCase = num_attention_heads UpperCamelCase = intermediate_size UpperCamelCase = hidden_act UpperCamelCase = hidden_dropout_prob UpperCamelCase = attention_probs_dropout_prob UpperCamelCase = type_sequence_label_size UpperCamelCase = initializer_range UpperCamelCase = scope UpperCamelCase = out_indices UpperCamelCase = num_labels # in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) UpperCamelCase = (image_size // patch_size) ** 2 UpperCamelCase = num_patches + 1 def UpperCAmelCase_ ( self )-> List[str]: '''simple docstring''' UpperCamelCase = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) UpperCamelCase = None UpperCamelCase = None if self.use_labels: UpperCamelCase = ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCamelCase = ids_tensor([self.batch_size, self.image_size, self.image_size] , self.num_labels ) UpperCamelCase = self.get_config() return config, pixel_values, labels, pixel_labels def UpperCAmelCase_ ( self )-> Dict: '''simple docstring''' return BeitConfig( vocab_size=self.vocab_size , 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 , out_indices=self.out_indices , ) def UpperCAmelCase_ ( self , A_ , A_ , A_ , A_ )-> List[str]: '''simple docstring''' UpperCamelCase = BeitModel(config=A_ ) model.to(A_ ) model.eval() UpperCamelCase = model(A_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def UpperCAmelCase_ ( self , A_ , A_ , A_ , A_ )-> Any: '''simple docstring''' UpperCamelCase = BeitForMaskedImageModeling(config=A_ ) model.to(A_ ) model.eval() UpperCamelCase = model(A_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length - 1, self.vocab_size) ) def UpperCAmelCase_ ( self , A_ , A_ , A_ , A_ )-> Optional[int]: '''simple docstring''' UpperCamelCase = self.type_sequence_label_size UpperCamelCase = BeitForImageClassification(A_ ) model.to(A_ ) model.eval() UpperCamelCase = model(A_ , labels=A_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images UpperCamelCase = 1 UpperCamelCase = BeitForImageClassification(A_ ) model.to(A_ ) model.eval() UpperCamelCase = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) UpperCamelCase = model(A_ , labels=A_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def UpperCAmelCase_ ( self , A_ , A_ , A_ , A_ )-> Optional[Any]: '''simple docstring''' UpperCamelCase = self.num_labels UpperCamelCase = BeitForSemanticSegmentation(A_ ) model.to(A_ ) model.eval() UpperCamelCase = model(A_ ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2) ) UpperCamelCase = model(A_ , labels=A_ ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2) ) def UpperCAmelCase_ ( self )-> int: '''simple docstring''' UpperCamelCase = self.prepare_config_and_inputs() UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase = config_and_inputs UpperCamelCase = {'pixel_values': pixel_values} return config, inputs_dict @require_torch class SCREAMING_SNAKE_CASE__ ( snake_case_ , snake_case_ , unittest.TestCase): lowerCAmelCase_ = ( (BeitModel, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation) if is_torch_available() else () ) lowerCAmelCase_ = ( { """feature-extraction""": BeitModel, """image-classification""": BeitForImageClassification, """image-segmentation""": BeitForSemanticSegmentation, } if is_torch_available() else {} ) lowerCAmelCase_ = False lowerCAmelCase_ = False lowerCAmelCase_ = False def UpperCAmelCase_ ( self )-> Any: '''simple docstring''' UpperCamelCase = BeitModelTester(self ) UpperCamelCase = ConfigTester(self , config_class=A_ , has_text_modality=A_ , hidden_size=37 ) def UpperCAmelCase_ ( self )-> Union[str, Any]: '''simple docstring''' self.config_tester.run_common_tests() @unittest.skip(reason='BEiT does not use inputs_embeds' ) def UpperCAmelCase_ ( self )-> Optional[int]: '''simple docstring''' pass @require_torch_multi_gpu @unittest.skip(reason='BEiT has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`' ) def UpperCAmelCase_ ( self )-> Optional[Any]: '''simple docstring''' pass def UpperCAmelCase_ ( self )-> Tuple: '''simple docstring''' UpperCamelCase , UpperCamelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCamelCase = model_class(A_ ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) UpperCamelCase = model.get_output_embeddings() self.assertTrue(x is None or isinstance(A_ , nn.Linear ) ) def UpperCAmelCase_ ( self )-> List[Any]: '''simple docstring''' UpperCamelCase , UpperCamelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCamelCase = model_class(A_ ) UpperCamelCase = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic UpperCamelCase = [*signature.parameters.keys()] UpperCamelCase = ['pixel_values'] self.assertListEqual(arg_names[:1] , A_ ) def UpperCAmelCase_ ( self )-> Union[str, Any]: '''simple docstring''' UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*A_ ) def UpperCAmelCase_ ( self )-> List[Any]: '''simple docstring''' UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*A_ ) def UpperCAmelCase_ ( self )-> Any: '''simple docstring''' UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*A_ ) def UpperCAmelCase_ ( self )-> List[str]: '''simple docstring''' UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_semantic_segmentation(*A_ ) def UpperCAmelCase_ ( self )-> int: '''simple docstring''' if not self.model_tester.is_training: return UpperCamelCase , UpperCamelCase = self.model_tester.prepare_config_and_inputs_for_common() UpperCamelCase = True for model_class in self.all_model_classes: # we don't test BeitForMaskedImageModeling if model_class in [*get_values(A_ ), BeitForMaskedImageModeling]: continue UpperCamelCase = model_class(A_ ) model.to(A_ ) model.train() UpperCamelCase = self._prepare_for_class(A_ , A_ , return_labels=A_ ) UpperCamelCase = model(**A_ ).loss loss.backward() def UpperCAmelCase_ ( self )-> List[str]: '''simple docstring''' UpperCamelCase , UpperCamelCase = self.model_tester.prepare_config_and_inputs_for_common() if not self.model_tester.is_training: return UpperCamelCase = False UpperCamelCase = True for model_class in self.all_model_classes: # we don't test BeitForMaskedImageModeling if ( model_class in [*get_values(A_ ), BeitForMaskedImageModeling] or not model_class.supports_gradient_checkpointing ): continue UpperCamelCase = model_class(A_ ) model.gradient_checkpointing_enable() model.to(A_ ) model.train() UpperCamelCase = self._prepare_for_class(A_ , A_ , return_labels=A_ ) UpperCamelCase = model(**A_ ).loss loss.backward() def UpperCAmelCase_ ( self )-> Union[str, Any]: '''simple docstring''' UpperCamelCase , UpperCamelCase = self.model_tester.prepare_config_and_inputs_for_common() UpperCamelCase = _config_zero_init(A_ ) for model_class in self.all_model_classes: UpperCamelCase = model_class(config=A_ ) for name, param in model.named_parameters(): # we skip lambda parameters as these require special initial values # determined by config.layer_scale_init_value if "lambda" in name: continue if param.requires_grad: self.assertIn( ((param.data.mean() * 1e9).round() / 1e9).item() , [0.0, 1.0] , msg=F'''Parameter {name} of model {model_class} seems not properly initialized''' , ) @slow def UpperCAmelCase_ ( self )-> Dict: '''simple docstring''' for model_name in BEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCamelCase = BeitModel.from_pretrained(A_ ) self.assertIsNotNone(A_ ) def A_( ): UpperCamelCase = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png') return image @require_torch @require_vision class SCREAMING_SNAKE_CASE__ ( unittest.TestCase): @cached_property def UpperCAmelCase_ ( self )-> Optional[int]: '''simple docstring''' return BeitImageProcessor.from_pretrained('microsoft/beit-base-patch16-224' ) if is_vision_available() else None @slow def UpperCAmelCase_ ( self )-> Optional[Any]: '''simple docstring''' UpperCamelCase = BeitForMaskedImageModeling.from_pretrained('microsoft/beit-base-patch16-224-pt22k' ).to(A_ ) UpperCamelCase = self.default_image_processor UpperCamelCase = prepare_img() UpperCamelCase = image_processor(images=A_ , return_tensors='pt' ).pixel_values.to(A_ ) # prepare bool_masked_pos UpperCamelCase = torch.ones((1, 196) , dtype=torch.bool ).to(A_ ) # forward pass with torch.no_grad(): UpperCamelCase = model(pixel_values=A_ , bool_masked_pos=A_ ) UpperCamelCase = outputs.logits # verify the logits UpperCamelCase = torch.Size((1, 196, 8192) ) self.assertEqual(logits.shape , A_ ) UpperCamelCase = torch.tensor( [[-3.2_437, 0.5_072, -13.9_174], [-3.2_456, 0.4_948, -13.9_401], [-3.2_033, 0.5_121, -13.8_550]] ).to(A_ ) self.assertTrue(torch.allclose(logits[bool_masked_pos][:3, :3] , A_ , atol=1e-2 ) ) @slow def UpperCAmelCase_ ( self )-> Union[str, Any]: '''simple docstring''' UpperCamelCase = BeitForImageClassification.from_pretrained('microsoft/beit-base-patch16-224' ).to(A_ ) UpperCamelCase = self.default_image_processor UpperCamelCase = prepare_img() UpperCamelCase = image_processor(images=A_ , return_tensors='pt' ).to(A_ ) # forward pass with torch.no_grad(): UpperCamelCase = model(**A_ ) UpperCamelCase = outputs.logits # verify the logits UpperCamelCase = torch.Size((1, 1000) ) self.assertEqual(logits.shape , A_ ) UpperCamelCase = torch.tensor([-1.2_385, -1.0_987, -1.0_108] ).to(A_ ) self.assertTrue(torch.allclose(logits[0, :3] , A_ , atol=1e-4 ) ) UpperCamelCase = 281 self.assertEqual(logits.argmax(-1 ).item() , A_ ) @slow def UpperCAmelCase_ ( self )-> Optional[Any]: '''simple docstring''' UpperCamelCase = BeitForImageClassification.from_pretrained('microsoft/beit-large-patch16-224-pt22k-ft22k' ).to( A_ ) UpperCamelCase = self.default_image_processor UpperCamelCase = prepare_img() UpperCamelCase = image_processor(images=A_ , return_tensors='pt' ).to(A_ ) # forward pass with torch.no_grad(): UpperCamelCase = model(**A_ ) UpperCamelCase = outputs.logits # verify the logits UpperCamelCase = torch.Size((1, 21841) ) self.assertEqual(logits.shape , A_ ) UpperCamelCase = torch.tensor([1.6_881, -0.2_787, 0.5_901] ).to(A_ ) self.assertTrue(torch.allclose(logits[0, :3] , A_ , atol=1e-4 ) ) UpperCamelCase = 2396 self.assertEqual(logits.argmax(-1 ).item() , A_ ) @slow def UpperCAmelCase_ ( self )-> Any: '''simple docstring''' UpperCamelCase = BeitForSemanticSegmentation.from_pretrained('microsoft/beit-base-finetuned-ade-640-640' ) UpperCamelCase = model.to(A_ ) UpperCamelCase = BeitImageProcessor(do_resize=A_ , size=640 , do_center_crop=A_ ) UpperCamelCase = load_dataset('hf-internal-testing/fixtures_ade20k' , split='test' ) UpperCamelCase = Image.open(ds[0]['file'] ) UpperCamelCase = image_processor(images=A_ , return_tensors='pt' ).to(A_ ) # forward pass with torch.no_grad(): UpperCamelCase = model(**A_ ) UpperCamelCase = outputs.logits # verify the logits UpperCamelCase = torch.Size((1, 150, 160, 160) ) self.assertEqual(logits.shape , A_ ) UpperCamelCase = version.parse(PIL.__version__ ) < version.parse('9.0.0' ) if is_pillow_less_than_a: UpperCamelCase = torch.tensor( [ [[-4.9_225, -2.3_954, -3.0_522], [-2.8_822, -1.0_046, -1.7_561], [-2.9_549, -1.3_228, -2.1_347]], [[-5.8_168, -3.4_129, -4.0_778], [-3.8_651, -2.2_214, -3.0_277], [-3.8_356, -2.4_643, -3.3_535]], [[-0.0_078, 3.9_952, 4.0_754], [2.9_856, 4.6_944, 5.0_035], [3.2_413, 4.7_813, 4.9_969]], ] , device=A_ , ) else: UpperCamelCase = torch.tensor( [ [[-4.8_960, -2.3_688, -3.0_355], [-2.8_478, -0.9_836, -1.7_418], [-2.9_449, -1.3_332, -2.1_456]], [[-5.8_081, -3.4_124, -4.1_006], [-3.8_561, -2.2_081, -3.0_323], [-3.8_365, -2.4_601, -3.3_669]], [[-0.0_309, 3.9_868, 4.0_540], [2.9_640, 4.6_877, 4.9_976], [3.2_081, 4.7_690, 4.9_942]], ] , device=A_ , ) self.assertTrue(torch.allclose(logits[0, :3, :3, :3] , A_ , atol=1e-4 ) ) @slow def UpperCAmelCase_ ( self )-> Tuple: '''simple docstring''' UpperCamelCase = BeitForSemanticSegmentation.from_pretrained('microsoft/beit-base-finetuned-ade-640-640' ) UpperCamelCase = model.to(A_ ) UpperCamelCase = BeitImageProcessor(do_resize=A_ , size=640 , do_center_crop=A_ ) UpperCamelCase = load_dataset('hf-internal-testing/fixtures_ade20k' , split='test' ) UpperCamelCase = Image.open(ds[0]['file'] ) UpperCamelCase = image_processor(images=A_ , return_tensors='pt' ).to(A_ ) # forward pass with torch.no_grad(): UpperCamelCase = model(**A_ ) UpperCamelCase = outputs.logits.detach().cpu() UpperCamelCase = image_processor.post_process_semantic_segmentation(outputs=A_ , target_sizes=[(500, 300)] ) UpperCamelCase = torch.Size((500, 300) ) self.assertEqual(segmentation[0].shape , A_ ) UpperCamelCase = image_processor.post_process_semantic_segmentation(outputs=A_ ) UpperCamelCase = torch.Size((160, 160) ) self.assertEqual(segmentation[0].shape , A_ )
3
1
'''simple docstring''' def A_( A : int): if divisor % 5 == 0 or divisor % 2 == 0: return 0 UpperCamelCase = 1 UpperCamelCase = 1 while repunit: UpperCamelCase = (10 * repunit + 1) % divisor repunit_index += 1 return repunit_index def A_( A : int = 100_0000): UpperCamelCase = limit - 1 if divisor % 2 == 0: divisor += 1 while least_divisible_repunit(A) <= limit: divisor += 2 return divisor if __name__ == "__main__": print(f"""{solution() = }""")
3
'''simple docstring''' import enum import warnings from ..tokenization_utils import TruncationStrategy from ..utils import add_end_docstrings, is_tf_available, is_torch_available, logging from .base import PIPELINE_INIT_ARGS, Pipeline if is_tf_available(): import tensorflow as tf from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING if is_torch_available(): from ..models.auto.modeling_auto import MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING lowerCAmelCase : Dict = logging.get_logger(__name__) class SCREAMING_SNAKE_CASE__ ( enum.Enum): lowerCAmelCase_ = 0 lowerCAmelCase_ = 1 @add_end_docstrings(snake_case_) class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = """generated""" def __init__( self , *A_ , **A_ )-> Optional[int]: '''simple docstring''' super().__init__(*A_ , **A_ ) self.check_model_type( TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING if self.framework == 'tf' else MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING ) def UpperCAmelCase_ ( self , A_=None , A_=None , A_=None , A_=None , A_=None , A_=None , **A_ , )-> Optional[Any]: '''simple docstring''' UpperCamelCase = {} if truncation is not None: UpperCamelCase = truncation UpperCamelCase = generate_kwargs UpperCamelCase = {} if return_tensors is not None and return_type is None: UpperCamelCase = ReturnType.TENSORS if return_tensors else ReturnType.TEXT if return_type is not None: UpperCamelCase = return_type if clean_up_tokenization_spaces is not None: UpperCamelCase = clean_up_tokenization_spaces if stop_sequence is not None: UpperCamelCase = self.tokenizer.encode(A_ , add_special_tokens=A_ ) if len(A_ ) > 1: warnings.warn( 'Stopping on a multiple token sequence is not yet supported on transformers. The first token of' ' the stop sequence will be used as the stop sequence string in the interim.' ) UpperCamelCase = stop_sequence_ids[0] return preprocess_params, forward_params, postprocess_params def UpperCAmelCase_ ( self , A_ , A_ , A_ )-> Optional[int]: '''simple docstring''' return True def UpperCAmelCase_ ( self , *A_ , A_ )-> Any: '''simple docstring''' UpperCamelCase = self.model.config.prefix if self.model.config.prefix is not None else '' if isinstance(args[0] , A_ ): if self.tokenizer.pad_token_id is None: raise ValueError('Please make sure that the tokenizer has a pad_token_id when using a batch input' ) UpperCamelCase = ([prefix + arg for arg in args[0]],) UpperCamelCase = True elif isinstance(args[0] , A_ ): UpperCamelCase = (prefix + args[0],) UpperCamelCase = False else: raise ValueError( F''' `args[0]`: {args[0]} have the wrong format. The should be either of type `str` or type `list`''' ) UpperCamelCase = self.tokenizer(*A_ , padding=A_ , truncation=A_ , return_tensors=self.framework ) # This is produced by tokenizers but is an invalid generate kwargs if "token_type_ids" in inputs: del inputs["token_type_ids"] return inputs def __call__( self , *A_ , **A_ )-> Union[str, Any]: '''simple docstring''' UpperCamelCase = super().__call__(*A_ , **A_ ) if ( isinstance(args[0] , A_ ) and all(isinstance(A_ , A_ ) for el in args[0] ) and all(len(A_ ) == 1 for res in result ) ): return [res[0] for res in result] return result def UpperCAmelCase_ ( self , A_ , A_=TruncationStrategy.DO_NOT_TRUNCATE , **A_ )-> Any: '''simple docstring''' UpperCamelCase = self._parse_and_tokenize(A_ , truncation=A_ , **A_ ) return inputs def UpperCAmelCase_ ( self , A_ , **A_ )-> int: '''simple docstring''' if self.framework == "pt": UpperCamelCase , UpperCamelCase = model_inputs['input_ids'].shape elif self.framework == "tf": UpperCamelCase , UpperCamelCase = tf.shape(model_inputs['input_ids'] ).numpy() UpperCamelCase = generate_kwargs.get('min_length' , self.model.config.min_length ) UpperCamelCase = generate_kwargs.get('max_length' , self.model.config.max_length ) self.check_inputs(A_ , generate_kwargs['min_length'] , generate_kwargs['max_length'] ) UpperCamelCase = self.model.generate(**A_ , **A_ ) UpperCamelCase = output_ids.shape[0] if self.framework == "pt": UpperCamelCase = output_ids.reshape(A_ , out_b // in_b , *output_ids.shape[1:] ) elif self.framework == "tf": UpperCamelCase = tf.reshape(A_ , (in_b, out_b // in_b, *output_ids.shape[1:]) ) return {"output_ids": output_ids} def UpperCAmelCase_ ( self , A_ , A_=ReturnType.TEXT , A_=False )-> Optional[Any]: '''simple docstring''' UpperCamelCase = [] for output_ids in model_outputs["output_ids"][0]: if return_type == ReturnType.TENSORS: UpperCamelCase = {F'''{self.return_name}_token_ids''': output_ids} elif return_type == ReturnType.TEXT: UpperCamelCase = { F'''{self.return_name}_text''': self.tokenizer.decode( A_ , skip_special_tokens=A_ , clean_up_tokenization_spaces=A_ , ) } records.append(A_ ) return records @add_end_docstrings(snake_case_) class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = """summary""" def __call__( self , *A_ , **A_ )-> Optional[int]: '''simple docstring''' return super().__call__(*A_ , **A_ ) def UpperCAmelCase_ ( self , A_ , A_ , A_ )-> bool: '''simple docstring''' if max_length < min_length: logger.warning(F'''Your min_length={min_length} must be inferior than your max_length={max_length}.''' ) if input_length < max_length: logger.warning( F'''Your max_length is set to {max_length}, but your input_length is only {input_length}. Since this is ''' 'a summarization task, where outputs shorter than the input are typically wanted, you might ' F'''consider decreasing max_length manually, e.g. summarizer(\'...\', max_length={input_length//2})''' ) @add_end_docstrings(snake_case_) class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = """translation""" def UpperCAmelCase_ ( self , A_ , A_ , A_ )-> List[Any]: '''simple docstring''' if input_length > 0.9 * max_length: logger.warning( F'''Your input_length: {input_length} is bigger than 0.9 * max_length: {max_length}. You might consider ''' 'increasing your max_length manually, e.g. translator(\'...\', max_length=400)' ) return True def UpperCAmelCase_ ( self , *A_ , A_=TruncationStrategy.DO_NOT_TRUNCATE , A_=None , A_=None )-> Dict: '''simple docstring''' if getattr(self.tokenizer , '_build_translation_inputs' , A_ ): return self.tokenizer._build_translation_inputs( *A_ , return_tensors=self.framework , truncation=A_ , src_lang=A_ , tgt_lang=A_ ) else: return super()._parse_and_tokenize(*A_ , truncation=A_ ) def UpperCAmelCase_ ( self , A_=None , A_=None , **A_ )-> str: '''simple docstring''' UpperCamelCase , UpperCamelCase , UpperCamelCase = super()._sanitize_parameters(**A_ ) if src_lang is not None: UpperCamelCase = src_lang if tgt_lang is not None: UpperCamelCase = tgt_lang if src_lang is None and tgt_lang is None: # Backward compatibility, direct arguments use is preferred. UpperCamelCase = kwargs.get('task' , self.task ) UpperCamelCase = task.split('_' ) if task and len(A_ ) == 4: # translation, XX, to YY UpperCamelCase = items[1] UpperCamelCase = items[3] return preprocess_params, forward_params, postprocess_params def __call__( self , *A_ , **A_ )-> Any: '''simple docstring''' return super().__call__(*A_ , **A_ )
3
1
'''simple docstring''' import inspect import os import re from transformers.configuration_utils import PretrainedConfig from transformers.utils import direct_transformers_import # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_config_docstrings.py lowerCAmelCase : List[str] = 'src/transformers' # This is to make sure the transformers module imported is the one in the repo. lowerCAmelCase : Union[str, Any] = direct_transformers_import(PATH_TO_TRANSFORMERS) lowerCAmelCase : str = transformers.models.auto.configuration_auto.CONFIG_MAPPING lowerCAmelCase : Any = { # used to compute the property `self.chunk_length` 'EncodecConfig': ['overlap'], # used as `self.bert_model = BertModel(config, ...)` 'DPRConfig': True, # not used in modeling files, but it's an important information 'FSMTConfig': ['langs'], # used internally in the configuration class file 'GPTNeoConfig': ['attention_types'], # used internally in the configuration class file 'EsmConfig': ['is_folding_model'], # used during training (despite we don't have training script for these models yet) 'Mask2FormerConfig': ['ignore_value'], # `ignore_value` used during training (despite we don't have training script for these models yet) # `norm` used in conversion script (despite not using in the modeling file) 'OneFormerConfig': ['ignore_value', 'norm'], # used during preprocessing and collation, see `collating_graphormer.py` 'GraphormerConfig': ['spatial_pos_max'], # used internally in the configuration class file 'T5Config': ['feed_forward_proj'], # used internally in the configuration class file # `tokenizer_class` get default value `T5Tokenizer` intentionally 'MT5Config': ['feed_forward_proj', 'tokenizer_class'], 'UMT5Config': ['feed_forward_proj', 'tokenizer_class'], # used internally in the configuration class file 'LongT5Config': ['feed_forward_proj'], # used internally in the configuration class file 'SwitchTransformersConfig': ['feed_forward_proj'], # having default values other than `1e-5` - we can't fix them without breaking 'BioGptConfig': ['layer_norm_eps'], # having default values other than `1e-5` - we can't fix them without breaking 'GLPNConfig': ['layer_norm_eps'], # having default values other than `1e-5` - we can't fix them without breaking 'SegformerConfig': ['layer_norm_eps'], # having default values other than `1e-5` - we can't fix them without breaking 'CvtConfig': ['layer_norm_eps'], # having default values other than `1e-5` - we can't fix them without breaking 'PerceiverConfig': ['layer_norm_eps'], # used internally to calculate the feature size 'InformerConfig': ['num_static_real_features', 'num_time_features'], # used internally to calculate the feature size 'TimeSeriesTransformerConfig': ['num_static_real_features', 'num_time_features'], # used internally to calculate the feature size 'AutoformerConfig': ['num_static_real_features', 'num_time_features'], # used internally to calculate `mlp_dim` 'SamVisionConfig': ['mlp_ratio'], # For (head) training, but so far not implemented 'ClapAudioConfig': ['num_classes'], # Not used, but providing useful information to users 'SpeechT5HifiGanConfig': ['sampling_rate'], } # TODO (ydshieh): Check the failing cases, try to fix them or move some cases to the above block once we are sure SPECIAL_CASES_TO_ALLOW.update( { 'CLIPSegConfig': True, 'DeformableDetrConfig': True, 'DetaConfig': True, 'DinatConfig': True, 'DonutSwinConfig': True, 'EfficientFormerConfig': True, 'FSMTConfig': True, 'JukeboxConfig': True, 'LayoutLMv2Config': True, 'MaskFormerSwinConfig': True, 'MT5Config': True, 'NatConfig': True, 'OneFormerConfig': True, 'PerceiverConfig': True, 'RagConfig': True, 'SpeechT5Config': True, 'SwinConfig': True, 'Swin2SRConfig': True, 'Swinv2Config': True, 'SwitchTransformersConfig': True, 'TableTransformerConfig': True, 'TapasConfig': True, 'TransfoXLConfig': True, 'UniSpeechConfig': True, 'UniSpeechSatConfig': True, 'WavLMConfig': True, 'WhisperConfig': True, # TODO: @Arthur (for `alignment_head` and `alignment_layer`) 'JukeboxPriorConfig': True, # TODO: @Younes (for `is_decoder`) 'Pix2StructTextConfig': True, } ) def A_( A : Optional[int] , A : List[str] , A : Tuple , A : Tuple): UpperCamelCase = False for attribute in attributes: for modeling_source in source_strings: # check if we can find `config.xxx`, `getattr(config, "xxx", ...)` or `getattr(self.config, "xxx", ...)` if ( f'''config.{attribute}''' in modeling_source or f'''getattr(config, "{attribute}"''' in modeling_source or f'''getattr(self.config, "{attribute}"''' in modeling_source ): UpperCamelCase = True # Deal with multi-line cases elif ( re.search( rf'''getattr[ \t\v\n\r\f]*\([ \t\v\n\r\f]*(self\.)?config,[ \t\v\n\r\f]*"{attribute}"''' , A , ) is not None ): UpperCamelCase = True # `SequenceSummary` is called with `SequenceSummary(config)` elif attribute in [ "summary_type", "summary_use_proj", "summary_activation", "summary_last_dropout", "summary_proj_to_labels", "summary_first_dropout", ]: if "SequenceSummary" in modeling_source: UpperCamelCase = True if attribute_used: break if attribute_used: break # common and important attributes, even if they do not always appear in the modeling files UpperCamelCase = [ 'bos_index', 'eos_index', 'pad_index', 'unk_index', 'mask_index', 'image_size', 'use_cache', 'out_features', 'out_indices', ] UpperCamelCase = ['encoder_no_repeat_ngram_size'] # Special cases to be allowed UpperCamelCase = True if not attribute_used: UpperCamelCase = False for attribute in attributes: # Allow if the default value in the configuration class is different from the one in `PretrainedConfig` if attribute in ["is_encoder_decoder"] and default_value is True: UpperCamelCase = True elif attribute in ["tie_word_embeddings"] and default_value is False: UpperCamelCase = True # Allow cases without checking the default value in the configuration class elif attribute in attributes_to_allow + attributes_used_in_generation: UpperCamelCase = True elif attribute.endswith('_token_id'): UpperCamelCase = True # configuration class specific cases if not case_allowed: UpperCamelCase = SPECIAL_CASES_TO_ALLOW.get(config_class.__name__ , []) UpperCamelCase = allowed_cases is True or attribute in allowed_cases return attribute_used or case_allowed def A_( A : Optional[Any]): UpperCamelCase = dict(inspect.signature(config_class.__init__).parameters) UpperCamelCase = [x for x in list(signature.keys()) if x not in ['self', 'kwargs']] UpperCamelCase = [signature[param].default for param in parameter_names] # If `attribute_map` exists, an attribute can have different names to be used in the modeling files, and as long # as one variant is used, the test should pass UpperCamelCase = {} if len(config_class.attribute_map) > 0: UpperCamelCase = {v: k for k, v in config_class.attribute_map.items()} # Get the path to modeling source files UpperCamelCase = inspect.getsourcefile(A) UpperCamelCase = os.path.dirname(A) # Let's check against all frameworks: as long as one framework uses an attribute, we are good. UpperCamelCase = [os.path.join(A , A) for fn in os.listdir(A) if fn.startswith('modeling_')] # Get the source code strings UpperCamelCase = [] for path in modeling_paths: if os.path.isfile(A): with open(A) as fp: modeling_sources.append(fp.read()) UpperCamelCase = [] for config_param, default_value in zip(A , A): # `attributes` here is all the variant names for `config_param` UpperCamelCase = [config_param] # some configuration classes have non-empty `attribute_map`, and both names could be used in the # corresponding modeling files. As long as one of them appears, it is fine. if config_param in reversed_attribute_map: attributes.append(reversed_attribute_map[config_param]) if not check_attribute_being_used(A , A , A , A): unused_attributes.append(attributes[0]) return sorted(A) def A_( ): UpperCamelCase = {} for _config_class in list(CONFIG_MAPPING.values()): # Skip deprecated models if "models.deprecated" in _config_class.__module__: continue # Some config classes are not in `CONFIG_MAPPING` (e.g. `CLIPVisionConfig`, `Blip2VisionConfig`, etc.) UpperCamelCase = [ cls for name, cls in inspect.getmembers( inspect.getmodule(_config_class) , lambda A: inspect.isclass(A) and issubclass(A , A) and inspect.getmodule(A) == inspect.getmodule(_config_class) , ) ] for config_class in config_classes_in_module: UpperCamelCase = check_config_attributes_being_used(A) if len(A) > 0: UpperCamelCase = unused_attributes if len(A) > 0: UpperCamelCase = 'The following configuration classes contain unused attributes in the corresponding modeling files:\n' for name, attributes in configs_with_unused_attributes.items(): error += f'''{name}: {attributes}\n''' raise ValueError(A) if __name__ == "__main__": check_config_attributes()
3
'''simple docstring''' import inspect import os import unittest from dataclasses import dataclass import torch from accelerate import Accelerator, DistributedDataParallelKwargs, GradScalerKwargs from accelerate.state import AcceleratorState from accelerate.test_utils import execute_subprocess_async, require_cuda, require_multi_gpu from accelerate.utils import KwargsHandler @dataclass class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = 0 lowerCAmelCase_ = False lowerCAmelCase_ = 3.0 class SCREAMING_SNAKE_CASE__ ( unittest.TestCase): def UpperCAmelCase_ ( self )-> int: '''simple docstring''' self.assertDictEqual(MockClass().to_kwargs() , {} ) self.assertDictEqual(MockClass(a=2 ).to_kwargs() , {'a': 2} ) self.assertDictEqual(MockClass(a=2 , b=A_ ).to_kwargs() , {'a': 2, 'b': True} ) self.assertDictEqual(MockClass(a=2 , c=2.25 ).to_kwargs() , {'a': 2, 'c': 2.25} ) @require_cuda def UpperCAmelCase_ ( self )-> Dict: '''simple docstring''' UpperCamelCase = GradScalerKwargs(init_scale=1024 , growth_factor=2 ) AcceleratorState._reset_state() UpperCamelCase = Accelerator(mixed_precision='fp16' , kwargs_handlers=[scaler_handler] ) print(accelerator.use_fpaa ) UpperCamelCase = accelerator.scaler # Check the kwargs have been applied self.assertEqual(scaler._init_scale , 1_024.0 ) self.assertEqual(scaler._growth_factor , 2.0 ) # Check the other values are at the default self.assertEqual(scaler._backoff_factor , 0.5 ) self.assertEqual(scaler._growth_interval , 2000 ) self.assertEqual(scaler._enabled , A_ ) @require_multi_gpu def UpperCAmelCase_ ( self )-> Dict: '''simple docstring''' UpperCamelCase = ['torchrun', F'''--nproc_per_node={torch.cuda.device_count()}''', inspect.getfile(self.__class__ )] execute_subprocess_async(A_ , env=os.environ.copy() ) if __name__ == "__main__": lowerCAmelCase : Tuple = DistributedDataParallelKwargs(bucket_cap_mb=15, find_unused_parameters=True) lowerCAmelCase : List[str] = Accelerator(kwargs_handlers=[ddp_scaler]) lowerCAmelCase : List[Any] = torch.nn.Linear(1_00, 2_00) lowerCAmelCase : int = accelerator.prepare(model) # Check the values changed in kwargs lowerCAmelCase : Dict = '' lowerCAmelCase : Dict = model.bucket_bytes_cap // (10_24 * 10_24) if observed_bucket_cap_map != 15: error_msg += f"Kwargs badly passed, should have `15` but found {observed_bucket_cap_map}.\n" if model.find_unused_parameters is not True: error_msg += f"Kwargs badly passed, should have `True` but found {model.find_unused_parameters}.\n" # Check the values of the defaults if model.dim != 0: error_msg += f"Default value not respected, should have `0` but found {model.dim}.\n" if model.broadcast_buffers is not True: error_msg += f"Default value not respected, should have `True` but found {model.broadcast_buffers}.\n" if model.gradient_as_bucket_view is not False: error_msg += f"Default value not respected, should have `False` but found {model.gradient_as_bucket_view}.\n" # Raise error at the end to make sure we don't stop at the first failure. if len(error_msg) > 0: raise ValueError(error_msg)
3
1
'''simple docstring''' # coding=utf-8 # Copyright 2023 The HuggingFace Inc. team. # # 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. # this script dumps information about the environment import os import platform import sys lowerCAmelCase : Union[str, Any] = '3' print('Python version:', sys.version) print('OS platform:', platform.platform()) print('OS architecture:', platform.machine()) try: import torch print('Torch version:', torch.__version__) print('Cuda available:', torch.cuda.is_available()) print('Cuda version:', torch.version.cuda) print('CuDNN version:', torch.backends.cudnn.version()) print('Number of GPUs available:', torch.cuda.device_count()) except ImportError: print('Torch version:', None) try: import transformers print('transformers version:', transformers.__version__) except ImportError: print('transformers version:', None)
3
'''simple docstring''' from typing import Callable, List, Optional, Tuple, Union import torch from transformers import CLIPTextModel, CLIPTokenizer from ...configuration_utils import ConfigMixin, register_to_config from ...models import ModelMixin, TransformeraDModel, VQModel from ...schedulers import VQDiffusionScheduler from ...utils import logging from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput lowerCAmelCase : Optional[Any] = logging.get_logger(__name__) # pylint: disable=invalid-name class SCREAMING_SNAKE_CASE__ ( snake_case_ , snake_case_): @register_to_config def __init__( self , A_ , A_ = None , A_ = None )-> Tuple: '''simple docstring''' super().__init__() UpperCamelCase = learnable if self.learnable: assert hidden_size is not None, "learnable=True requires `hidden_size` to be set" assert length is not None, "learnable=True requires `length` to be set" UpperCamelCase = torch.zeros(A_ , A_ ) else: UpperCamelCase = None UpperCamelCase = torch.nn.Parameter(A_ ) class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = 42 lowerCAmelCase_ = 42 lowerCAmelCase_ = 42 lowerCAmelCase_ = 42 lowerCAmelCase_ = 42 lowerCAmelCase_ = 42 def __init__( self , A_ , A_ , A_ , A_ , A_ , A_ , )-> Union[str, Any]: '''simple docstring''' super().__init__() self.register_modules( vqvae=A_ , transformer=A_ , text_encoder=A_ , tokenizer=A_ , scheduler=A_ , learned_classifier_free_sampling_embeddings=A_ , ) def UpperCAmelCase_ ( self , A_ , A_ , A_ )-> Tuple: '''simple docstring''' UpperCamelCase = len(A_ ) if isinstance(A_ , A_ ) else 1 # get prompt text embeddings UpperCamelCase = self.tokenizer( A_ , padding='max_length' , max_length=self.tokenizer.model_max_length , return_tensors='pt' , ) UpperCamelCase = text_inputs.input_ids if text_input_ids.shape[-1] > self.tokenizer.model_max_length: UpperCamelCase = self.tokenizer.batch_decode(text_input_ids[:, self.tokenizer.model_max_length :] ) logger.warning( 'The following part of your input was truncated because CLIP can only handle sequences up to' F''' {self.tokenizer.model_max_length} tokens: {removed_text}''' ) UpperCamelCase = text_input_ids[:, : self.tokenizer.model_max_length] UpperCamelCase = self.text_encoder(text_input_ids.to(self.device ) )[0] # NOTE: This additional step of normalizing the text embeddings is from VQ-Diffusion. # While CLIP does normalize the pooled output of the text transformer when combining # the image and text embeddings, CLIP does not directly normalize the last hidden state. # # CLIP normalizing the pooled output. # https://github.com/huggingface/transformers/blob/d92e22d1f28324f513f3080e5c47c071a3916721/src/transformers/models/clip/modeling_clip.py#L1052-L1053 UpperCamelCase = prompt_embeds / prompt_embeds.norm(dim=-1 , keepdim=A_ ) # duplicate text embeddings for each generation per prompt UpperCamelCase = prompt_embeds.repeat_interleave(A_ , dim=0 ) if do_classifier_free_guidance: if self.learned_classifier_free_sampling_embeddings.learnable: UpperCamelCase = self.learned_classifier_free_sampling_embeddings.embeddings UpperCamelCase = negative_prompt_embeds.unsqueeze(0 ).repeat(A_ , 1 , 1 ) else: UpperCamelCase = [''] * batch_size UpperCamelCase = text_input_ids.shape[-1] UpperCamelCase = self.tokenizer( A_ , padding='max_length' , max_length=A_ , truncation=A_ , return_tensors='pt' , ) UpperCamelCase = self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # See comment for normalizing text embeddings UpperCamelCase = negative_prompt_embeds / negative_prompt_embeds.norm(dim=-1 , keepdim=A_ ) # duplicate unconditional embeddings for each generation per prompt, using mps friendly method UpperCamelCase = negative_prompt_embeds.shape[1] UpperCamelCase = negative_prompt_embeds.repeat(1 , A_ , 1 ) UpperCamelCase = negative_prompt_embeds.view(batch_size * num_images_per_prompt , A_ , -1 ) # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes UpperCamelCase = torch.cat([negative_prompt_embeds, prompt_embeds] ) return prompt_embeds @torch.no_grad() def __call__( self , A_ , A_ = 100 , A_ = 5.0 , A_ = 1.0 , A_ = 1 , A_ = None , A_ = None , A_ = "pil" , A_ = True , A_ = None , A_ = 1 , )-> Union[ImagePipelineOutput, Tuple]: '''simple docstring''' if isinstance(A_ , A_ ): UpperCamelCase = 1 elif isinstance(A_ , A_ ): UpperCamelCase = len(A_ ) else: raise ValueError(F'''`prompt` has to be of type `str` or `list` but is {type(A_ )}''' ) UpperCamelCase = batch_size * num_images_per_prompt UpperCamelCase = guidance_scale > 1.0 UpperCamelCase = self._encode_prompt(A_ , A_ , A_ ) 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_ )}.''' ) # get the initial completely masked latents unless the user supplied it UpperCamelCase = (batch_size, self.transformer.num_latent_pixels) if latents is None: UpperCamelCase = self.transformer.num_vector_embeds - 1 UpperCamelCase = torch.full(A_ , A_ ).to(self.device ) else: if latents.shape != latents_shape: raise ValueError(F'''Unexpected latents shape, got {latents.shape}, expected {latents_shape}''' ) if (latents < 0).any() or (latents >= self.transformer.num_vector_embeds).any(): raise ValueError( 'Unexpected latents value(s). All latents be valid embedding indices i.e. in the range 0,' F''' {self.transformer.num_vector_embeds - 1} (inclusive).''' ) UpperCamelCase = latents.to(self.device ) # set timesteps self.scheduler.set_timesteps(A_ , device=self.device ) UpperCamelCase = self.scheduler.timesteps.to(self.device ) UpperCamelCase = latents for i, t in enumerate(self.progress_bar(A_ ) ): # expand the sample if we are doing classifier free guidance UpperCamelCase = torch.cat([sample] * 2 ) if do_classifier_free_guidance else sample # predict the un-noised image # model_output == `log_p_x_0` UpperCamelCase = self.transformer(A_ , encoder_hidden_states=A_ , timestep=A_ ).sample if do_classifier_free_guidance: UpperCamelCase , UpperCamelCase = model_output.chunk(2 ) UpperCamelCase = model_output_uncond + guidance_scale * (model_output_text - model_output_uncond) model_output -= torch.logsumexp(A_ , dim=1 , keepdim=A_ ) UpperCamelCase = self.truncate(A_ , A_ ) # remove `log(0)`'s (`-inf`s) UpperCamelCase = model_output.clamp(-70 ) # compute the previous noisy sample x_t -> x_t-1 UpperCamelCase = self.scheduler.step(A_ , timestep=A_ , sample=A_ , generator=A_ ).prev_sample # call the callback, if provided if callback is not None and i % callback_steps == 0: callback(A_ , A_ , A_ ) UpperCamelCase = self.vqvae.config.vq_embed_dim UpperCamelCase = (batch_size, self.transformer.height, self.transformer.width, embedding_channels) UpperCamelCase = self.vqvae.quantize.get_codebook_entry(A_ , shape=A_ ) UpperCamelCase = self.vqvae.decode(A_ , force_not_quantize=A_ ).sample UpperCamelCase = (image / 2 + 0.5).clamp(0 , 1 ) UpperCamelCase = image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": UpperCamelCase = self.numpy_to_pil(A_ ) if not return_dict: return (image,) return ImagePipelineOutput(images=A_ ) def UpperCAmelCase_ ( self , A_ , A_ )-> torch.FloatTensor: '''simple docstring''' UpperCamelCase , UpperCamelCase = torch.sort(A_ , 1 , descending=A_ ) UpperCamelCase = torch.exp(A_ ) UpperCamelCase = sorted_p_x_0.cumsum(dim=1 ) < truncation_rate # Ensure that at least the largest probability is not zeroed out UpperCamelCase = torch.full_like(keep_mask[:, 0:1, :] , A_ ) UpperCamelCase = torch.cat((all_true, keep_mask) , dim=1 ) UpperCamelCase = keep_mask[:, :-1, :] UpperCamelCase = keep_mask.gather(1 , indices.argsort(1 ) ) UpperCamelCase = log_p_x_0.clone() UpperCamelCase = -torch.inf # -inf = log(0) return rv
3
1
'''simple docstring''' from PIL import Image def A_( A : Image , A : float): def brightness(A : int) -> float: return 128 + level + (c - 128) if not -255.0 <= level <= 255.0: raise ValueError('level must be between -255.0 (black) and 255.0 (white)') return img.point(A) if __name__ == "__main__": # Load image with Image.open('image_data/lena.jpg') as img: # Change brightness to 100 lowerCAmelCase : Dict = change_brightness(img, 1_00) brigt_img.save('image_data/lena_brightness.png', format='png')
3
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCAmelCase : Union[str, Any] = { 'configuration_git': ['GIT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'GitConfig', 'GitVisionConfig'], 'processing_git': ['GitProcessor'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : List[Any] = [ 'GIT_PRETRAINED_MODEL_ARCHIVE_LIST', 'GitForCausalLM', 'GitModel', 'GitPreTrainedModel', 'GitVisionModel', ] if TYPE_CHECKING: from .configuration_git import GIT_PRETRAINED_CONFIG_ARCHIVE_MAP, GitConfig, GitVisionConfig from .processing_git import GitProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_git import ( GIT_PRETRAINED_MODEL_ARCHIVE_LIST, GitForCausalLM, GitModel, GitPreTrainedModel, GitVisionModel, ) else: import sys lowerCAmelCase : Optional[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
3
1
'''simple docstring''' lowerCAmelCase : Any = [ 'DownloadConfig', 'DownloadManager', 'DownloadMode', 'StreamingDownloadManager', ] from .download_config import DownloadConfig from .download_manager import DownloadManager, DownloadMode from .streaming_download_manager import StreamingDownloadManager
3
'''simple docstring''' import uuid from typing import Any, Dict, List, Optional, Union from ..utils import add_end_docstrings, is_tf_available, is_torch_available, logging from .base import PIPELINE_INIT_ARGS, Pipeline if is_tf_available(): import tensorflow as tf if is_torch_available(): import torch lowerCAmelCase : Optional[Any] = logging.get_logger(__name__) class SCREAMING_SNAKE_CASE__ : def __init__( self , A_ = None , A_ = None , A_=None , A_=None )-> Optional[Any]: '''simple docstring''' if not conversation_id: UpperCamelCase = uuid.uuida() if past_user_inputs is None: UpperCamelCase = [] if generated_responses is None: UpperCamelCase = [] UpperCamelCase = conversation_id UpperCamelCase = past_user_inputs UpperCamelCase = generated_responses UpperCamelCase = text def __eq__( self , A_ )-> List[Any]: '''simple docstring''' if not isinstance(A_ , A_ ): return False if self.uuid == other.uuid: return True return ( self.new_user_input == other.new_user_input and self.past_user_inputs == other.past_user_inputs and self.generated_responses == other.generated_responses ) def UpperCAmelCase_ ( self , A_ , A_ = False )-> int: '''simple docstring''' if self.new_user_input: if overwrite: logger.warning( F'''User input added while unprocessed input was existing: "{self.new_user_input}" was overwritten ''' F'''with: "{text}".''' ) UpperCamelCase = text else: logger.warning( F'''User input added while unprocessed input was existing: "{self.new_user_input}" new input ''' F'''ignored: "{text}". Set `overwrite` to True to overwrite unprocessed user input''' ) else: UpperCamelCase = text def UpperCAmelCase_ ( self )-> Any: '''simple docstring''' if self.new_user_input: self.past_user_inputs.append(self.new_user_input ) UpperCamelCase = None def UpperCAmelCase_ ( self , A_ )-> int: '''simple docstring''' self.generated_responses.append(A_ ) def UpperCAmelCase_ ( self )-> List[str]: '''simple docstring''' for user_input, generated_response in zip(self.past_user_inputs , self.generated_responses ): yield True, user_input yield False, generated_response if self.new_user_input: yield True, self.new_user_input def __repr__( self )-> Any: '''simple docstring''' UpperCamelCase = F'''Conversation id: {self.uuid} \n''' for is_user, text in self.iter_texts(): UpperCamelCase = 'user' if is_user else 'bot' output += F'''{name} >> {text} \n''' return output @add_end_docstrings( snake_case_ , R""" min_length_for_response (`int`, *optional*, defaults to 32): The minimum length (in number of tokens) for a response. minimum_tokens (`int`, *optional*, defaults to 10): The minimum length of tokens to leave for a response. """ , ) class SCREAMING_SNAKE_CASE__ ( snake_case_): def __init__( self , *A_ , **A_ )-> Any: '''simple docstring''' super().__init__(*A_ , **A_ ) if self.tokenizer.pad_token_id is None: UpperCamelCase = self.tokenizer.eos_token def UpperCAmelCase_ ( self , A_=None , A_=None , A_=None , **A_ )-> Union[str, Any]: '''simple docstring''' UpperCamelCase = {} UpperCamelCase = {} UpperCamelCase = {} if min_length_for_response is not None: UpperCamelCase = min_length_for_response if minimum_tokens is not None: UpperCamelCase = minimum_tokens if "max_length" in generate_kwargs: UpperCamelCase = generate_kwargs['max_length'] # self.max_length = generate_kwargs.get("max_length", self.model.config.max_length) if clean_up_tokenization_spaces is not None: UpperCamelCase = clean_up_tokenization_spaces if generate_kwargs: forward_params.update(A_ ) return preprocess_params, forward_params, postprocess_params def __call__( self , A_ , A_=0 , **A_ )-> Any: '''simple docstring''' UpperCamelCase = super().__call__(A_ , num_workers=A_ , **A_ ) if isinstance(A_ , A_ ) and len(A_ ) == 1: return outputs[0] return outputs def UpperCAmelCase_ ( self , A_ , A_=32 )-> Dict[str, Any]: '''simple docstring''' if not isinstance(A_ , A_ ): raise ValueError('ConversationalPipeline, expects Conversation as inputs' ) if conversation.new_user_input is None: raise ValueError( F'''Conversation with UUID {type(conversation.uuid )} does not contain new user input to process. ''' 'Add user inputs with the conversation\'s `add_user_input` method' ) if hasattr(self.tokenizer , '_build_conversation_input_ids' ): UpperCamelCase = self.tokenizer._build_conversation_input_ids(A_ ) else: # If the tokenizer cannot handle conversations, we default to only the old version UpperCamelCase = self._legacy_parse_and_tokenize(A_ ) if self.framework == "pt": UpperCamelCase = torch.LongTensor([input_ids] ) elif self.framework == "tf": UpperCamelCase = tf.constant([input_ids] ) return {"input_ids": input_ids, "conversation": conversation} def UpperCAmelCase_ ( self , A_ , A_=10 , **A_ )-> Optional[Any]: '''simple docstring''' UpperCamelCase = generate_kwargs.get('max_length' , self.model.config.max_length ) UpperCamelCase = model_inputs['input_ids'].shape[1] if max_length - minimum_tokens < n: logger.warning(F'''Conversation input is to long ({n}), trimming it to ({max_length} - {minimum_tokens})''' ) UpperCamelCase = max_length - minimum_tokens UpperCamelCase = model_inputs['input_ids'][:, -trim:] if "attention_mask" in model_inputs: UpperCamelCase = model_inputs['attention_mask'][:, -trim:] UpperCamelCase = model_inputs.pop('conversation' ) UpperCamelCase = max_length UpperCamelCase = self.model.generate(**A_ , **A_ ) if self.model.config.is_encoder_decoder: UpperCamelCase = 1 else: UpperCamelCase = n return {"output_ids": output_ids[:, start_position:], "conversation": conversation} def UpperCAmelCase_ ( self , A_ , A_=True )-> Tuple: '''simple docstring''' UpperCamelCase = model_outputs['output_ids'] UpperCamelCase = self.tokenizer.decode( output_ids[0] , skip_special_tokens=A_ , clean_up_tokenization_spaces=A_ , ) UpperCamelCase = model_outputs['conversation'] conversation.mark_processed() conversation.append_response(A_ ) return conversation def UpperCAmelCase_ ( self , A_ )-> Dict: '''simple docstring''' UpperCamelCase = self.tokenizer.eos_token_id UpperCamelCase = [] for is_user, text in conversation.iter_texts(): if eos_token_id is not None: input_ids.extend(self.tokenizer.encode(A_ , add_special_tokens=A_ ) + [eos_token_id] ) else: input_ids.extend(self.tokenizer.encode(A_ , add_special_tokens=A_ ) ) if len(A_ ) > self.tokenizer.model_max_length: UpperCamelCase = input_ids[-self.tokenizer.model_max_length :] return input_ids
3
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 : List[Any] = logging.get_logger(__name__) lowerCAmelCase : str = { 'junnyu/roformer_chinese_small': 'https://huggingface.co/junnyu/roformer_chinese_small/resolve/main/config.json', 'junnyu/roformer_chinese_base': 'https://huggingface.co/junnyu/roformer_chinese_base/resolve/main/config.json', 'junnyu/roformer_chinese_char_small': ( 'https://huggingface.co/junnyu/roformer_chinese_char_small/resolve/main/config.json' ), 'junnyu/roformer_chinese_char_base': ( 'https://huggingface.co/junnyu/roformer_chinese_char_base/resolve/main/config.json' ), 'junnyu/roformer_small_discriminator': ( 'https://huggingface.co/junnyu/roformer_small_discriminator/resolve/main/config.json' ), 'junnyu/roformer_small_generator': ( 'https://huggingface.co/junnyu/roformer_small_generator/resolve/main/config.json' ), # See all RoFormer models at https://huggingface.co/models?filter=roformer } class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = """roformer""" def __init__( self , A_=50000 , A_=None , A_=768 , A_=12 , A_=12 , A_=3072 , A_="gelu" , A_=0.1 , A_=0.1 , A_=1536 , A_=2 , A_=0.02 , A_=1e-12 , A_=0 , A_=False , A_=True , **A_ , )-> Optional[Any]: '''simple docstring''' super().__init__(pad_token_id=A_ , **A_ ) UpperCamelCase = vocab_size UpperCamelCase = hidden_size if embedding_size is None else embedding_size UpperCamelCase = hidden_size UpperCamelCase = num_hidden_layers UpperCamelCase = num_attention_heads UpperCamelCase = hidden_act UpperCamelCase = intermediate_size UpperCamelCase = hidden_dropout_prob UpperCamelCase = attention_probs_dropout_prob UpperCamelCase = max_position_embeddings UpperCamelCase = type_vocab_size UpperCamelCase = initializer_range UpperCamelCase = layer_norm_eps UpperCamelCase = rotary_value UpperCamelCase = use_cache class SCREAMING_SNAKE_CASE__ ( snake_case_): @property def UpperCAmelCase_ ( self )-> Mapping[str, Mapping[int, str]]: '''simple docstring''' if self.task == "multiple-choice": UpperCamelCase = {0: 'batch', 1: 'choice', 2: 'sequence'} else: UpperCamelCase = {0: 'batch', 1: 'sequence'} UpperCamelCase = {0: 'batch', 1: 'sequence'} return OrderedDict( [ ('input_ids', dynamic_axis), ('attention_mask', dynamic_axis), ('token_type_ids', dynamic_axis), ] )
3
'''simple docstring''' import sys import webbrowser import requests from bsa import BeautifulSoup from fake_useragent import UserAgent if __name__ == "__main__": print('Googling.....') lowerCAmelCase : List[Any] = 'https://www.google.com/search?q=' + ' '.join(sys.argv[1:]) lowerCAmelCase : List[Any] = requests.get(url, headers={'UserAgent': UserAgent().random}) # res.raise_for_status() with open('project1a.html', 'wb') as out_file: # only for knowing the class for data in res.iter_content(1_00_00): out_file.write(data) lowerCAmelCase : Tuple = BeautifulSoup(res.text, 'html.parser') lowerCAmelCase : List[Any] = list(soup.select('.eZt8xd'))[:5] print(len(links)) for link in links: if link.text == "Maps": webbrowser.open(link.get('href')) else: webbrowser.open(f"""https://google.com{link.get('href')}""")
3
1
'''simple docstring''' import copy from typing import Any, Dict, List, Optional, Union import numpy as np import torch from ...audio_utils import mel_filter_bank, spectrogram, window_function from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import TensorType, logging lowerCAmelCase : List[Any] = logging.get_logger(__name__) class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = ["""input_features""", """is_longer"""] def __init__( self , A_=64 , A_=48000 , A_=480 , A_=10 , A_=1024 , A_=0.0 , A_=False , A_ = 0 , A_ = 14000 , A_ = None , A_ = "fusion" , A_ = "repeatpad" , **A_ , )-> str: '''simple docstring''' super().__init__( feature_size=A_ , sampling_rate=A_ , padding_value=A_ , return_attention_mask=A_ , **A_ , ) UpperCamelCase = top_db UpperCamelCase = truncation UpperCamelCase = padding UpperCamelCase = fft_window_size UpperCamelCase = (fft_window_size >> 1) + 1 UpperCamelCase = hop_length UpperCamelCase = max_length_s UpperCamelCase = max_length_s * sampling_rate UpperCamelCase = sampling_rate UpperCamelCase = frequency_min UpperCamelCase = frequency_max UpperCamelCase = mel_filter_bank( num_frequency_bins=self.nb_frequency_bins , num_mel_filters=A_ , min_frequency=A_ , max_frequency=A_ , sampling_rate=A_ , norm=A_ , mel_scale='htk' , ) UpperCamelCase = mel_filter_bank( num_frequency_bins=self.nb_frequency_bins , num_mel_filters=A_ , min_frequency=A_ , max_frequency=A_ , sampling_rate=A_ , norm='slaney' , mel_scale='slaney' , ) def UpperCAmelCase_ ( self )-> Dict[str, Any]: '''simple docstring''' UpperCamelCase = copy.deepcopy(self.__dict__ ) UpperCamelCase = self.__class__.__name__ if "mel_filters" in output: del output["mel_filters"] if "mel_filters_slaney" in output: del output["mel_filters_slaney"] return output def UpperCAmelCase_ ( self , A_ , A_ = None )-> np.ndarray: '''simple docstring''' UpperCamelCase = spectrogram( A_ , window_function(self.fft_window_size , 'hann' ) , frame_length=self.fft_window_size , hop_length=self.hop_length , power=2.0 , mel_filters=A_ , log_mel='dB' , ) return log_mel_spectrogram.T def UpperCAmelCase_ ( self , A_ , A_ , A_ )-> Union[str, Any]: '''simple docstring''' UpperCamelCase = np.array_split(list(range(0 , total_frames - chunk_frames + 1 ) ) , 3 ) if len(ranges[1] ) == 0: # if the audio is too short, we just use the first chunk UpperCamelCase = [0] if len(ranges[2] ) == 0: # if the audio is too short, we just use the first chunk UpperCamelCase = [0] # randomly choose index for each part UpperCamelCase = np.random.choice(ranges[0] ) UpperCamelCase = np.random.choice(ranges[1] ) UpperCamelCase = np.random.choice(ranges[2] ) UpperCamelCase = mel[idx_front : idx_front + chunk_frames, :] UpperCamelCase = mel[idx_middle : idx_middle + chunk_frames, :] UpperCamelCase = mel[idx_back : idx_back + chunk_frames, :] UpperCamelCase = torch.tensor(mel[None, None, :] ) UpperCamelCase = torch.nn.functional.interpolate( A_ , size=[chunk_frames, 64] , mode='bilinear' , align_corners=A_ ) UpperCamelCase = mel_shrink[0][0].numpy() UpperCamelCase = np.stack([mel_shrink, mel_chunk_front, mel_chunk_middle, mel_chunk_back] , axis=0 ) return mel_fusion def UpperCAmelCase_ ( self , A_ , A_ , A_ , A_ )-> np.array: '''simple docstring''' if waveform.shape[0] > max_length: if truncation == "rand_trunc": UpperCamelCase = True # random crop to max_length (for compatibility) -> this should be handled by self.pad UpperCamelCase = len(A_ ) - max_length UpperCamelCase = np.random.randint(0 , overflow + 1 ) UpperCamelCase = waveform[idx : idx + max_length] UpperCamelCase = self._np_extract_fbank_features(A_ , self.mel_filters_slaney )[None, :] elif truncation == "fusion": UpperCamelCase = self._np_extract_fbank_features(A_ , self.mel_filters ) UpperCamelCase = max_length // self.hop_length + 1 # the +1 related to how the spectrogram is computed UpperCamelCase = mel.shape[0] if chunk_frames == total_frames: # there is a corner case where the audio length is larger than max_length but smaller than max_length+hop_length. # In this case, we just use the whole audio. UpperCamelCase = np.stack([mel, mel, mel, mel] , axis=0 ) UpperCamelCase = False else: UpperCamelCase = self._random_mel_fusion(A_ , A_ , A_ ) UpperCamelCase = True else: raise NotImplementedError(F'''data_truncating {truncation} not implemented''' ) else: UpperCamelCase = False # only use repeat as a new possible value for padding. you repeat the audio before applying the usual max_length padding if waveform.shape[0] < max_length: if padding == "repeat": UpperCamelCase = int(max_length / len(A_ ) ) UpperCamelCase = np.stack(np.tile(A_ , n_repeat + 1 ) )[:max_length] if padding == "repeatpad": UpperCamelCase = int(max_length / len(A_ ) ) UpperCamelCase = np.stack(np.tile(A_ , A_ ) ) UpperCamelCase = np.pad(A_ , (0, max_length - waveform.shape[0]) , mode='constant' , constant_values=0 ) if truncation == "fusion": UpperCamelCase = self._np_extract_fbank_features(A_ , self.mel_filters ) UpperCamelCase = np.stack([input_mel, input_mel, input_mel, input_mel] , axis=0 ) else: UpperCamelCase = self._np_extract_fbank_features(A_ , self.mel_filters_slaney )[None, :] return input_mel, longer def __call__( self , A_ , A_ = None , A_ = None , A_ = None , A_ = None , A_ = None , **A_ , )-> BatchFeature: '''simple docstring''' UpperCamelCase = truncation if truncation is not None else self.truncation UpperCamelCase = padding if padding else self.padding if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( F'''The model corresponding to this feature extractor: {self.__class__.__name__} was trained using a''' F''' sampling rate of {self.sampling_rate}. Please make sure that the provided `raw_speech` input''' F''' was sampled with {self.sampling_rate} and not {sampling_rate}.''' ) else: logger.warning( 'It is strongly recommended to pass the `sampling_rate` argument to this function. ' 'Failing to do so can result in silent errors that might be hard to debug.' ) UpperCamelCase = isinstance(A_ , np.ndarray ) and len(raw_speech.shape ) > 1 if is_batched_numpy and len(raw_speech.shape ) > 2: raise ValueError(F'''Only mono-channel audio is supported for input to {self}''' ) UpperCamelCase = is_batched_numpy or ( isinstance(A_ , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: UpperCamelCase = [np.asarray(A_ , dtype=np.floataa ) for speech in raw_speech] elif not is_batched and not isinstance(A_ , np.ndarray ): UpperCamelCase = np.asarray(A_ , dtype=np.floataa ) elif isinstance(A_ , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): UpperCamelCase = raw_speech.astype(np.floataa ) # always return batch if not is_batched: UpperCamelCase = [np.asarray(A_ )] # convert to mel spectrogram, truncate and pad if needed. UpperCamelCase = [ self._get_input_mel(A_ , max_length if max_length else self.nb_max_samples , A_ , A_ ) for waveform in raw_speech ] UpperCamelCase = [] UpperCamelCase = [] for mel, longer in padded_inputs: input_mel.append(A_ ) is_longer.append(A_ ) if truncation == "fusion" and sum(A_ ) == 0: # if no audio is longer than 10s, then randomly select one audio to be longer UpperCamelCase = np.random.randint(0 , len(A_ ) ) UpperCamelCase = True if isinstance(input_mel[0] , A_ ): UpperCamelCase = [np.asarray(A_ , dtype=np.floataa ) for feature in input_mel] # is_longer is a list of bool UpperCamelCase = [[longer] for longer in is_longer] UpperCamelCase = {'input_features': input_mel, 'is_longer': is_longer} UpperCamelCase = BatchFeature(A_ ) if return_tensors is not None: UpperCamelCase = input_features.convert_to_tensors(A_ ) return input_features
3
'''simple docstring''' import numpy as np def A_( A : str , A : Optional[Any] , A : Tuple , A : Optional[int] , A : str): UpperCamelCase = int(np.ceil((x_end - xa) / h)) UpperCamelCase = np.zeros((n + 1,)) UpperCamelCase = ya UpperCamelCase = xa for k in range(A): UpperCamelCase = f(A , y[k]) UpperCamelCase = f(x + 0.5 * h , y[k] + 0.5 * h * ka) UpperCamelCase = f(x + 0.5 * h , y[k] + 0.5 * h * ka) UpperCamelCase = f(x + h , y[k] + h * ka) UpperCamelCase = y[k] + (1 / 6) * h * (ka + 2 * ka + 2 * ka + ka) x += h return y if __name__ == "__main__": import doctest doctest.testmod()
3
1
'''simple docstring''' lowerCAmelCase : List[Any] = [4, 1, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5] lowerCAmelCase : int = [3, 7, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5] lowerCAmelCase : List[Any] = { 0: 'Sunday', 1: 'Monday', 2: 'Tuesday', 3: 'Wednesday', 4: 'Thursday', 5: 'Friday', 6: 'Saturday', } def A_( A : int , A : int , A : int): assert len(str(A)) > 2, "year should be in YYYY format" assert 1 <= month <= 12, "month should be between 1 to 12" assert 1 <= day <= 31, "day should be between 1 to 31" # Doomsday algorithm: UpperCamelCase = year // 100 UpperCamelCase = (5 * (century % 4) + 2) % 7 UpperCamelCase = year % 100 UpperCamelCase = centurian % 12 UpperCamelCase = ( (centurian // 12) + centurian_m + (centurian_m // 4) + century_anchor ) % 7 UpperCamelCase = ( DOOMSDAY_NOT_LEAP[month - 1] if (year % 4 != 0) or (centurian == 0 and (year % 400) == 0) else DOOMSDAY_LEAP[month - 1] ) UpperCamelCase = (dooms_day + day - day_anchor) % 7 return WEEK_DAY_NAMES[week_day] if __name__ == "__main__": import doctest doctest.testmod()
3
'''simple docstring''' from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Features, Value from .base import TaskTemplate @dataclass(frozen=snake_case_) class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = field(default="""language-modeling""" , metadata={"""include_in_asdict_even_if_is_default""": True}) lowerCAmelCase_ = Features({"""text""": Value("""string""")}) lowerCAmelCase_ = Features({}) lowerCAmelCase_ = "text" @property def UpperCAmelCase_ ( self )-> Dict[str, str]: '''simple docstring''' return {self.text_column: "text"}
3
1
'''simple docstring''' import torch from diffusers import DDPMScheduler from .test_schedulers import SchedulerCommonTest class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = (DDPMScheduler,) def UpperCAmelCase_ ( self , **A_ )-> str: '''simple docstring''' UpperCamelCase = { 'num_train_timesteps': 1000, 'beta_start': 0.0_001, 'beta_end': 0.02, 'beta_schedule': 'linear', 'variance_type': 'fixed_small', 'clip_sample': True, } config.update(**A_ ) return config def UpperCAmelCase_ ( self )-> Optional[int]: '''simple docstring''' for timesteps in [1, 5, 100, 1000]: self.check_over_configs(num_train_timesteps=A_ ) def UpperCAmelCase_ ( self )-> Optional[int]: '''simple docstring''' for beta_start, beta_end in zip([0.0_001, 0.001, 0.01, 0.1] , [0.002, 0.02, 0.2, 2] ): self.check_over_configs(beta_start=A_ , beta_end=A_ ) def UpperCAmelCase_ ( self )-> Union[str, Any]: '''simple docstring''' for schedule in ["linear", "squaredcos_cap_v2"]: self.check_over_configs(beta_schedule=A_ ) def UpperCAmelCase_ ( self )-> Optional[Any]: '''simple docstring''' for variance in ["fixed_small", "fixed_large", "other"]: self.check_over_configs(variance_type=A_ ) def UpperCAmelCase_ ( self )-> Tuple: '''simple docstring''' for clip_sample in [True, False]: self.check_over_configs(clip_sample=A_ ) def UpperCAmelCase_ ( self )-> Tuple: '''simple docstring''' self.check_over_configs(thresholding=A_ ) for threshold in [0.5, 1.0, 2.0]: for prediction_type in ["epsilon", "sample", "v_prediction"]: self.check_over_configs( thresholding=A_ , prediction_type=A_ , sample_max_value=A_ , ) def UpperCAmelCase_ ( self )-> Union[str, Any]: '''simple docstring''' for prediction_type in ["epsilon", "sample", "v_prediction"]: self.check_over_configs(prediction_type=A_ ) def UpperCAmelCase_ ( self )-> Optional[int]: '''simple docstring''' for t in [0, 500, 999]: self.check_over_forward(time_step=A_ ) def UpperCAmelCase_ ( self )-> Tuple: '''simple docstring''' UpperCamelCase = self.scheduler_classes[0] UpperCamelCase = self.get_scheduler_config() UpperCamelCase = scheduler_class(**A_ ) assert torch.sum(torch.abs(scheduler._get_variance(0 ) - 0.0 ) ) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(487 ) - 0.00_979 ) ) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(999 ) - 0.02 ) ) < 1e-5 def UpperCAmelCase_ ( self )-> Optional[Any]: '''simple docstring''' UpperCamelCase = self.scheduler_classes[0] UpperCamelCase = self.get_scheduler_config() UpperCamelCase = scheduler_class(**A_ ) UpperCamelCase = len(A_ ) UpperCamelCase = self.dummy_model() UpperCamelCase = self.dummy_sample_deter UpperCamelCase = torch.manual_seed(0 ) for t in reversed(range(A_ ) ): # 1. predict noise residual UpperCamelCase = model(A_ , A_ ) # 2. predict previous mean of sample x_t-1 UpperCamelCase = scheduler.step(A_ , A_ , A_ , generator=A_ ).prev_sample # if t > 0: # noise = self.dummy_sample_deter # variance = scheduler.get_variance(t) ** (0.5) * noise # # sample = pred_prev_sample + variance UpperCamelCase = pred_prev_sample UpperCamelCase = torch.sum(torch.abs(A_ ) ) UpperCamelCase = torch.mean(torch.abs(A_ ) ) assert abs(result_sum.item() - 258.9_606 ) < 1e-2 assert abs(result_mean.item() - 0.3_372 ) < 1e-3 def UpperCAmelCase_ ( self )-> str: '''simple docstring''' UpperCamelCase = self.scheduler_classes[0] UpperCamelCase = self.get_scheduler_config(prediction_type='v_prediction' ) UpperCamelCase = scheduler_class(**A_ ) UpperCamelCase = len(A_ ) UpperCamelCase = self.dummy_model() UpperCamelCase = self.dummy_sample_deter UpperCamelCase = torch.manual_seed(0 ) for t in reversed(range(A_ ) ): # 1. predict noise residual UpperCamelCase = model(A_ , A_ ) # 2. predict previous mean of sample x_t-1 UpperCamelCase = scheduler.step(A_ , A_ , A_ , generator=A_ ).prev_sample # if t > 0: # noise = self.dummy_sample_deter # variance = scheduler.get_variance(t) ** (0.5) * noise # # sample = pred_prev_sample + variance UpperCamelCase = pred_prev_sample UpperCamelCase = torch.sum(torch.abs(A_ ) ) UpperCamelCase = torch.mean(torch.abs(A_ ) ) assert abs(result_sum.item() - 202.0_296 ) < 1e-2 assert abs(result_mean.item() - 0.2_631 ) < 1e-3 def UpperCAmelCase_ ( self )-> Optional[Any]: '''simple docstring''' UpperCamelCase = self.scheduler_classes[0] UpperCamelCase = self.get_scheduler_config() UpperCamelCase = scheduler_class(**A_ ) UpperCamelCase = [100, 87, 50, 1, 0] scheduler.set_timesteps(timesteps=A_ ) UpperCamelCase = scheduler.timesteps for i, timestep in enumerate(A_ ): if i == len(A_ ) - 1: UpperCamelCase = -1 else: UpperCamelCase = timesteps[i + 1] UpperCamelCase = scheduler.previous_timestep(A_ ) UpperCamelCase = prev_t.item() self.assertEqual(A_ , A_ ) def UpperCAmelCase_ ( self )-> Dict: '''simple docstring''' UpperCamelCase = self.scheduler_classes[0] UpperCamelCase = self.get_scheduler_config() UpperCamelCase = scheduler_class(**A_ ) UpperCamelCase = [100, 87, 50, 51, 0] with self.assertRaises(A_ , msg='`custom_timesteps` must be in descending order.' ): scheduler.set_timesteps(timesteps=A_ ) def UpperCAmelCase_ ( self )-> int: '''simple docstring''' UpperCamelCase = self.scheduler_classes[0] UpperCamelCase = self.get_scheduler_config() UpperCamelCase = scheduler_class(**A_ ) UpperCamelCase = [100, 87, 50, 1, 0] UpperCamelCase = len(A_ ) with self.assertRaises(A_ , msg='Can only pass one of `num_inference_steps` or `custom_timesteps`.' ): scheduler.set_timesteps(num_inference_steps=A_ , timesteps=A_ ) def UpperCAmelCase_ ( self )-> Union[str, Any]: '''simple docstring''' UpperCamelCase = self.scheduler_classes[0] UpperCamelCase = self.get_scheduler_config() UpperCamelCase = scheduler_class(**A_ ) UpperCamelCase = [scheduler.config.num_train_timesteps] with self.assertRaises( A_ , msg='`timesteps` must start before `self.config.train_timesteps`: {scheduler.config.num_train_timesteps}}' , ): scheduler.set_timesteps(timesteps=A_ )
3
'''simple docstring''' from __future__ import annotations lowerCAmelCase : Union[str, Any] = [-10, -5, 0, 5, 5.1, 11, 13, 21, 3, 4, -21, -10, -5, -1, 0] lowerCAmelCase : List[str] = [-5, 0, 5, 5.1, 11, 13, 21, -1, 4, -1, -10, -5, -1, 0, -1] def A_( A : list[float]): UpperCamelCase = [] UpperCamelCase = len(A) for i in range(A): UpperCamelCase = -1 for j in range(i + 1 , A): if arr[i] < arr[j]: UpperCamelCase = arr[j] break result.append(A) return result def A_( A : list[float]): UpperCamelCase = [] for i, outer in enumerate(A): UpperCamelCase = -1 for inner in arr[i + 1 :]: if outer < inner: UpperCamelCase = inner break result.append(A) return result def A_( A : list[float]): UpperCamelCase = len(A) UpperCamelCase = [] UpperCamelCase = [-1] * arr_size for index in reversed(range(A)): if stack: while stack[-1] <= arr[index]: stack.pop() if not stack: break if stack: UpperCamelCase = stack[-1] stack.append(arr[index]) return result if __name__ == "__main__": from doctest import testmod from timeit import timeit testmod() print(next_greatest_element_slow(arr)) print(next_greatest_element_fast(arr)) print(next_greatest_element(arr)) lowerCAmelCase : Optional[Any] = ( 'from __main__ import arr, next_greatest_element_slow, ' 'next_greatest_element_fast, next_greatest_element' ) print( 'next_greatest_element_slow():', timeit('next_greatest_element_slow(arr)', setup=setup), ) print( 'next_greatest_element_fast():', timeit('next_greatest_element_fast(arr)', setup=setup), ) print( ' next_greatest_element():', timeit('next_greatest_element(arr)', setup=setup), )
3
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 : Any = { 'configuration_vivit': ['VIVIT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'VivitConfig'], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : Union[str, Any] = ['VivitImageProcessor'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : Optional[Any] = [ '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 : List[str] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
3
'''simple docstring''' from string import ascii_lowercase, ascii_uppercase def A_( A : str): if not sentence: return "" UpperCamelCase = dict(zip(A , A)) return lower_to_upper.get(sentence[0] , sentence[0]) + sentence[1:] if __name__ == "__main__": from doctest import testmod testmod()
3
1
'''simple docstring''' import collections import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging lowerCAmelCase : Dict = logging.get_logger(__name__) lowerCAmelCase : str = '▁' lowerCAmelCase : List[str] = {'vocab_file': 'prophetnet.tokenizer'} lowerCAmelCase : Any = { 'vocab_file': { 'microsoft/xprophetnet-large-wiki100-cased': ( 'https://huggingface.co/microsoft/xprophetnet-large-wiki100-cased/resolve/main/prophetnet.tokenizer' ), } } lowerCAmelCase : Union[str, Any] = { 'microsoft/xprophetnet-large-wiki100-cased': {'do_lower_case': False}, } lowerCAmelCase : Dict = { 'microsoft/xprophetnet-large-wiki100-cased': 5_12, } def A_( A : Optional[Any]): UpperCamelCase = collections.OrderedDict() with open(A , 'r' , encoding='utf-8') as reader: UpperCamelCase = reader.readlines() for index, token in enumerate(A): UpperCamelCase = token.rstrip('\n') UpperCamelCase = index return vocab class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = VOCAB_FILES_NAMES lowerCAmelCase_ = PRETRAINED_VOCAB_FILES_MAP lowerCAmelCase_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCAmelCase_ = ["""input_ids""", """attention_mask"""] def __init__( self , A_ , A_="[SEP]" , A_="[SEP]" , A_="[SEP]" , A_="[UNK]" , A_="[PAD]" , A_="[CLS]" , A_="[MASK]" , A_ = None , **A_ , )-> None: '''simple docstring''' UpperCamelCase = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=A_ , eos_token=A_ , sep_token=A_ , unk_token=A_ , pad_token=A_ , cls_token=A_ , mask_token=A_ , sp_model_kwargs=self.sp_model_kwargs , **A_ , ) try: import sentencepiece as spm except ImportError: logger.warning( 'You need to install SentencePiece to use XLMRobertaTokenizer: https://github.com/google/sentencepiece' ' pip install sentencepiece' ) raise UpperCamelCase = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(A_ ) ) UpperCamelCase = vocab_file # Original fairseq vocab and spm vocab must be "aligned": # Vocab | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 # -------- | ------- | ------- | ------ | ------- | --- | --- | --- | ----- | ----- | ---- # fairseq | '<s>' | '<pad>' | '</s>' | '<unk>' | ',' | '.' | '▁' | 's' | '▁de' | '-' # spm | '<unk>' | '<s>' | '</s>' | ',' | '.' | '▁' | 's' | '▁de' | '-' | '▁a' # put special tokens and [unused] tokens into the vocab UpperCamelCase = {'[PAD]': 0, '[CLS]': 1, '[SEP]': 2, '[UNK]': 3, '[MASK]': 4} for i in range(10 ): UpperCamelCase = F'''[unused{i}]''' UpperCamelCase = 5 + i # The first "real" token "," has position 15 in the embedding vocab and position 3 in the spm vocab UpperCamelCase = 12 UpperCamelCase = {v: k for k, v in self.fairseq_tokens_to_ids.items()} for k in self.fairseq_tokens_to_ids.keys(): self.unique_no_split_tokens.append(A_ ) def __getstate__( self )-> Tuple: '''simple docstring''' UpperCamelCase = self.__dict__.copy() UpperCamelCase = None return state def __setstate__( self , A_ )-> List[Any]: '''simple docstring''' UpperCamelCase = d try: import sentencepiece as spm except ImportError: logger.warning( 'You need to install SentencePiece to use XLMRobertaTokenizer: https://github.com/google/sentencepiece' ' pip install sentencepiece' ) raise # for backward compatibility if not hasattr(self , 'sp_model_kwargs' ): UpperCamelCase = {} UpperCamelCase = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def UpperCAmelCase_ ( self , A_ , A_ = None , A_ = False )-> List[int]: '''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_ ) if token_ids_a is None: return ([0] * len(A_ )) + [1] return ([0] * len(A_ )) + [1] + ([0] * len(A_ )) + [1] def UpperCAmelCase_ ( self , A_ , A_ = None )-> List[int]: '''simple docstring''' UpperCamelCase = [self.sep_token_id] if token_ids_a is None: return len(token_ids_a + sep ) * [0] return len(token_ids_a + sep + sep + token_ids_a + sep ) * [0] @property def UpperCAmelCase_ ( self )-> Optional[Any]: '''simple docstring''' return len(self.sp_model ) + self.fairseq_offset def UpperCAmelCase_ ( self )-> Optional[Any]: '''simple docstring''' UpperCamelCase = {self.convert_ids_to_tokens(A_ ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def UpperCAmelCase_ ( self , A_ )-> str: '''simple docstring''' return self.sp_model.encode(A_ , out_type=A_ ) def UpperCAmelCase_ ( self , A_ )-> Union[str, Any]: '''simple docstring''' if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] UpperCamelCase = self.sp_model.PieceToId(A_ ) # Need to return unknown token if the SP model returned 0 return spm_id + self.fairseq_offset if spm_id else self.unk_token_id def UpperCAmelCase_ ( self , A_ )-> str: '''simple docstring''' if index in self.fairseq_ids_to_tokens: return self.fairseq_ids_to_tokens[index] return self.sp_model.IdToPiece(index - self.fairseq_offset ) def UpperCAmelCase_ ( self , A_ )-> List[Any]: '''simple docstring''' UpperCamelCase = ''.join(A_ ).replace(A_ , ' ' ).strip() return out_string def UpperCAmelCase_ ( self , A_ , A_ = None )-> Tuple[str]: '''simple docstring''' if not os.path.isdir(A_ ): logger.error(F'''Vocabulary path ({save_directory}) should be a directory''' ) return UpperCamelCase = os.path.join( A_ , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(A_ ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , A_ ) elif not os.path.isfile(self.vocab_file ): with open(A_ , 'wb' ) as fi: UpperCamelCase = self.sp_model.serialized_model_proto() fi.write(A_ ) return (out_vocab_file,) def UpperCAmelCase_ ( self , A_ , A_ = None )-> List[int]: '''simple docstring''' if token_ids_a is None: return token_ids_a + [self.sep_token_id] UpperCamelCase = [self.sep_token_id] return token_ids_a + sep + token_ids_a + sep
3
'''simple docstring''' from typing import Optional, Tuple, Union import tensorflow as tf from ...activations_tf import ACTaFN from ...file_utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward from ...modeling_tf_outputs import ( TFBaseModelOutputWithNoAttention, TFBaseModelOutputWithPoolingAndNoAttention, TFSequenceClassifierOutput, ) from ...modeling_tf_utils import TFPreTrainedModel, TFSequenceClassificationLoss, keras_serializable, unpack_inputs from ...tf_utils import shape_list from ...utils import logging from .configuration_regnet import RegNetConfig lowerCAmelCase : Dict = logging.get_logger(__name__) # General docstring lowerCAmelCase : str = 'RegNetConfig' # Base docstring lowerCAmelCase : str = 'facebook/regnet-y-040' lowerCAmelCase : Dict = [1, 10_88, 7, 7] # Image classification docstring lowerCAmelCase : Dict = 'facebook/regnet-y-040' lowerCAmelCase : int = 'tabby, tabby cat' lowerCAmelCase : int = [ 'facebook/regnet-y-040', # See all regnet models at https://huggingface.co/models?filter=regnet ] class SCREAMING_SNAKE_CASE__ ( tf.keras.layers.Layer): def __init__( self , A_ , A_ = 3 , A_ = 1 , A_ = 1 , A_ = "relu" , **A_ , )-> str: '''simple docstring''' super().__init__(**A_ ) # The padding and conv has been verified in # https://colab.research.google.com/gist/sayakpaul/854bc10eeaf21c9ee2119e0b9f3841a7/scratchpad.ipynb UpperCamelCase = tf.keras.layers.ZeroPaddingaD(padding=kernel_size // 2 ) UpperCamelCase = tf.keras.layers.ConvaD( filters=A_ , kernel_size=A_ , strides=A_ , padding='VALID' , groups=A_ , use_bias=A_ , name='convolution' , ) UpperCamelCase = tf.keras.layers.BatchNormalization(epsilon=1e-5 , momentum=0.9 , name='normalization' ) UpperCamelCase = ACTaFN[activation] if activation is not None else tf.identity def UpperCAmelCase_ ( self , A_ )-> Any: '''simple docstring''' UpperCamelCase = self.convolution(self.padding(A_ ) ) UpperCamelCase = self.normalization(A_ ) UpperCamelCase = self.activation(A_ ) return hidden_state class SCREAMING_SNAKE_CASE__ ( tf.keras.layers.Layer): def __init__( self , A_ , **A_ )-> Optional[Any]: '''simple docstring''' super().__init__(**A_ ) UpperCamelCase = config.num_channels UpperCamelCase = TFRegNetConvLayer( out_channels=config.embedding_size , kernel_size=3 , stride=2 , activation=config.hidden_act , name='embedder' , ) def UpperCAmelCase_ ( self , A_ )-> List[Any]: '''simple docstring''' UpperCamelCase = shape_list(A_ )[1] if tf.executing_eagerly() and num_channels != self.num_channels: raise ValueError( 'Make sure that the channel dimension of the pixel values match with the one set in the configuration.' ) # When running on CPU, `tf.keras.layers.Conv2D` doesn't support `NCHW` format. # So change the input format from `NCHW` to `NHWC`. # shape = (batch_size, in_height, in_width, in_channels=num_channels) UpperCamelCase = tf.transpose(A_ , perm=(0, 2, 3, 1) ) UpperCamelCase = self.embedder(A_ ) return hidden_state class SCREAMING_SNAKE_CASE__ ( tf.keras.layers.Layer): def __init__( self , A_ , A_ = 2 , **A_ )-> List[Any]: '''simple docstring''' super().__init__(**A_ ) UpperCamelCase = tf.keras.layers.ConvaD( filters=A_ , kernel_size=1 , strides=A_ , use_bias=A_ , name='convolution' ) UpperCamelCase = tf.keras.layers.BatchNormalization(epsilon=1e-5 , momentum=0.9 , name='normalization' ) def UpperCAmelCase_ ( self , A_ , A_ = False )-> tf.Tensor: '''simple docstring''' return self.normalization(self.convolution(A_ ) , training=A_ ) class SCREAMING_SNAKE_CASE__ ( tf.keras.layers.Layer): def __init__( self , A_ , A_ , **A_ )-> Optional[Any]: '''simple docstring''' super().__init__(**A_ ) UpperCamelCase = tf.keras.layers.GlobalAveragePoolingaD(keepdims=A_ , name='pooler' ) UpperCamelCase = [ tf.keras.layers.ConvaD(filters=A_ , kernel_size=1 , activation='relu' , name='attention.0' ), tf.keras.layers.ConvaD(filters=A_ , kernel_size=1 , activation='sigmoid' , name='attention.2' ), ] def UpperCAmelCase_ ( self , A_ )-> Optional[int]: '''simple docstring''' UpperCamelCase = self.pooler(A_ ) for layer_module in self.attention: UpperCamelCase = layer_module(A_ ) UpperCamelCase = hidden_state * pooled return hidden_state class SCREAMING_SNAKE_CASE__ ( tf.keras.layers.Layer): def __init__( self , A_ , A_ , A_ , A_ = 1 , **A_ )-> Dict: '''simple docstring''' super().__init__(**A_ ) UpperCamelCase = in_channels != out_channels or stride != 1 UpperCamelCase = max(1 , out_channels // config.groups_width ) UpperCamelCase = ( TFRegNetShortCut(A_ , stride=A_ , name='shortcut' ) if should_apply_shortcut else tf.keras.layers.Activation('linear' , name='shortcut' ) ) # `self.layers` instead of `self.layer` because that is a reserved argument. UpperCamelCase = [ TFRegNetConvLayer(A_ , kernel_size=1 , activation=config.hidden_act , name='layer.0' ), TFRegNetConvLayer( A_ , stride=A_ , groups=A_ , activation=config.hidden_act , name='layer.1' ), TFRegNetConvLayer(A_ , kernel_size=1 , activation=A_ , name='layer.2' ), ] UpperCamelCase = ACTaFN[config.hidden_act] def UpperCAmelCase_ ( self , A_ )-> Tuple: '''simple docstring''' UpperCamelCase = hidden_state for layer_module in self.layers: UpperCamelCase = layer_module(A_ ) UpperCamelCase = self.shortcut(A_ ) hidden_state += residual UpperCamelCase = self.activation(A_ ) return hidden_state class SCREAMING_SNAKE_CASE__ ( tf.keras.layers.Layer): def __init__( self , A_ , A_ , A_ , A_ = 1 , **A_ )-> Any: '''simple docstring''' super().__init__(**A_ ) UpperCamelCase = in_channels != out_channels or stride != 1 UpperCamelCase = max(1 , out_channels // config.groups_width ) UpperCamelCase = ( TFRegNetShortCut(A_ , stride=A_ , name='shortcut' ) if should_apply_shortcut else tf.keras.layers.Activation('linear' , name='shortcut' ) ) UpperCamelCase = [ TFRegNetConvLayer(A_ , kernel_size=1 , activation=config.hidden_act , name='layer.0' ), TFRegNetConvLayer( A_ , stride=A_ , groups=A_ , activation=config.hidden_act , name='layer.1' ), TFRegNetSELayer(A_ , reduced_channels=int(round(in_channels / 4 ) ) , name='layer.2' ), TFRegNetConvLayer(A_ , kernel_size=1 , activation=A_ , name='layer.3' ), ] UpperCamelCase = ACTaFN[config.hidden_act] def UpperCAmelCase_ ( self , A_ )-> List[Any]: '''simple docstring''' UpperCamelCase = hidden_state for layer_module in self.layers: UpperCamelCase = layer_module(A_ ) UpperCamelCase = self.shortcut(A_ ) hidden_state += residual UpperCamelCase = self.activation(A_ ) return hidden_state class SCREAMING_SNAKE_CASE__ ( tf.keras.layers.Layer): def __init__( self , A_ , A_ , A_ , A_ = 2 , A_ = 2 , **A_ )-> Dict: '''simple docstring''' super().__init__(**A_ ) UpperCamelCase = TFRegNetXLayer if config.layer_type == 'x' else TFRegNetYLayer UpperCamelCase = [ # downsampling is done in the first layer with stride of 2 layer(A_ , A_ , A_ , stride=A_ , name='layers.0' ), *[layer(A_ , A_ , A_ , name=F'''layers.{i+1}''' ) for i in range(depth - 1 )], ] def UpperCAmelCase_ ( self , A_ )-> List[Any]: '''simple docstring''' for layer_module in self.layers: UpperCamelCase = layer_module(A_ ) return hidden_state class SCREAMING_SNAKE_CASE__ ( tf.keras.layers.Layer): def __init__( self , A_ , **A_ )-> str: '''simple docstring''' super().__init__(**A_ ) UpperCamelCase = [] # based on `downsample_in_first_stage`, the first layer of the first stage may or may not downsample the input self.stages.append( TFRegNetStage( A_ , config.embedding_size , config.hidden_sizes[0] , stride=2 if config.downsample_in_first_stage else 1 , depth=config.depths[0] , name='stages.0' , ) ) UpperCamelCase = zip(config.hidden_sizes , config.hidden_sizes[1:] ) for i, ((in_channels, out_channels), depth) in enumerate(zip(A_ , config.depths[1:] ) ): self.stages.append(TFRegNetStage(A_ , A_ , A_ , depth=A_ , name=F'''stages.{i+1}''' ) ) def UpperCAmelCase_ ( self , A_ , A_ = False , A_ = True )-> TFBaseModelOutputWithNoAttention: '''simple docstring''' UpperCamelCase = () if output_hidden_states else None for stage_module in self.stages: if output_hidden_states: UpperCamelCase = hidden_states + (hidden_state,) UpperCamelCase = stage_module(A_ ) if output_hidden_states: UpperCamelCase = hidden_states + (hidden_state,) if not return_dict: return tuple(v for v in [hidden_state, hidden_states] if v is not None ) return TFBaseModelOutputWithNoAttention(last_hidden_state=A_ , hidden_states=A_ ) @keras_serializable class SCREAMING_SNAKE_CASE__ ( tf.keras.layers.Layer): lowerCAmelCase_ = RegNetConfig def __init__( self , A_ , **A_ )-> Union[str, Any]: '''simple docstring''' super().__init__(**A_ ) UpperCamelCase = config UpperCamelCase = TFRegNetEmbeddings(A_ , name='embedder' ) UpperCamelCase = TFRegNetEncoder(A_ , name='encoder' ) UpperCamelCase = tf.keras.layers.GlobalAveragePoolingaD(keepdims=A_ , name='pooler' ) @unpack_inputs def UpperCAmelCase_ ( self , A_ , A_ = None , A_ = None , A_ = False , )-> TFBaseModelOutputWithPoolingAndNoAttention: '''simple docstring''' UpperCamelCase = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) UpperCamelCase = return_dict if return_dict is not None else self.config.use_return_dict UpperCamelCase = self.embedder(A_ , training=A_ ) UpperCamelCase = self.encoder( A_ , output_hidden_states=A_ , return_dict=A_ , training=A_ ) UpperCamelCase = encoder_outputs[0] UpperCamelCase = self.pooler(A_ ) # Change to NCHW output format have uniformity in the modules UpperCamelCase = tf.transpose(A_ , perm=(0, 3, 1, 2) ) UpperCamelCase = tf.transpose(A_ , perm=(0, 3, 1, 2) ) # Change the other hidden state outputs to NCHW as well if output_hidden_states: UpperCamelCase = tuple([tf.transpose(A_ , perm=(0, 3, 1, 2) ) for h in encoder_outputs[1]] ) if not return_dict: return (last_hidden_state, pooled_output) + encoder_outputs[1:] return TFBaseModelOutputWithPoolingAndNoAttention( last_hidden_state=A_ , pooler_output=A_ , hidden_states=hidden_states if output_hidden_states else encoder_outputs.hidden_states , ) class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = RegNetConfig lowerCAmelCase_ = """regnet""" lowerCAmelCase_ = """pixel_values""" @property def UpperCAmelCase_ ( self )-> List[str]: '''simple docstring''' return {"pixel_values": tf.TensorSpec(shape=(None, self.config.num_channels, 224, 224) , dtype=tf.floataa )} lowerCAmelCase : str = r'\n Parameters:\n This model is a Tensorflow\n [tf.keras.layers.Layer](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Layer) sub-class. Use it as a\n regular Tensorflow Module and refer to the Tensorflow documentation for all matter related to general usage and\n behavior.\n config ([`RegNetConfig`]): Model configuration class with all the parameters of the model.\n Initializing with a config file does not load the weights associated with the model, only the\n configuration. Check out the [`~TFPreTrainedModel.from_pretrained`] method to load the model weights.\n' lowerCAmelCase : List[str] = r'\n Args:\n pixel_values (`tf.Tensor` of shape `(batch_size, num_channels, height, width)`):\n Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n [`ConveNextImageProcessor.__call__`] for details.\n output_hidden_states (`bool`, *optional*):\n Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for\n more detail.\n return_dict (`bool`, *optional*):\n Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n' @add_start_docstrings( """The bare RegNet model outputting raw features without any specific head on top.""" , snake_case_ , ) class SCREAMING_SNAKE_CASE__ ( snake_case_): def __init__( self , A_ , *A_ , **A_ )-> List[Any]: '''simple docstring''' super().__init__(A_ , *A_ , **A_ ) UpperCamelCase = TFRegNetMainLayer(A_ , name='regnet' ) @unpack_inputs @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 UpperCAmelCase_ ( self , A_ , A_ = None , A_ = None , A_=False , )-> Union[TFBaseModelOutputWithPoolingAndNoAttention, Tuple[tf.Tensor]]: '''simple docstring''' UpperCamelCase = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) UpperCamelCase = return_dict if return_dict is not None else self.config.use_return_dict UpperCamelCase = self.regnet( pixel_values=A_ , output_hidden_states=A_ , return_dict=A_ , training=A_ , ) if not return_dict: return (outputs[0],) + outputs[1:] return TFBaseModelOutputWithPoolingAndNoAttention( last_hidden_state=outputs.last_hidden_state , pooler_output=outputs.pooler_output , hidden_states=outputs.hidden_states , ) @add_start_docstrings( """ RegNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for ImageNet. """ , snake_case_ , ) class SCREAMING_SNAKE_CASE__ ( snake_case_ , snake_case_): def __init__( self , A_ , *A_ , **A_ )-> str: '''simple docstring''' super().__init__(A_ , *A_ , **A_ ) UpperCamelCase = config.num_labels UpperCamelCase = TFRegNetMainLayer(A_ , name='regnet' ) # classification head UpperCamelCase = [ tf.keras.layers.Flatten(), tf.keras.layers.Dense(config.num_labels , name='classifier.1' ) if config.num_labels > 0 else tf.identity, ] @unpack_inputs @add_start_docstrings_to_model_forward(A_ ) @add_code_sample_docstrings( checkpoint=_IMAGE_CLASS_CHECKPOINT , output_type=A_ , config_class=_CONFIG_FOR_DOC , expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT , ) def UpperCAmelCase_ ( self , A_ = None , A_ = None , A_ = None , A_ = None , A_=False , )-> Union[TFSequenceClassifierOutput, Tuple[tf.Tensor]]: '''simple docstring''' UpperCamelCase = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) UpperCamelCase = return_dict if return_dict is not None else self.config.use_return_dict UpperCamelCase = self.regnet( A_ , output_hidden_states=A_ , return_dict=A_ , training=A_ ) UpperCamelCase = outputs.pooler_output if return_dict else outputs[1] UpperCamelCase = self.classifier[0](A_ ) UpperCamelCase = self.classifier[1](A_ ) UpperCamelCase = None if labels is None else self.hf_compute_loss(labels=A_ , logits=A_ ) if not return_dict: UpperCamelCase = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return TFSequenceClassifierOutput(loss=A_ , logits=A_ , hidden_states=outputs.hidden_states )
3
1
'''simple docstring''' import os import unittest from transformers import BatchEncoding from transformers.models.bert.tokenization_bert import ( BasicTokenizer, WordpieceTokenizer, _is_control, _is_punctuation, _is_whitespace, ) from transformers.models.prophetnet.tokenization_prophetnet import VOCAB_FILES_NAMES, ProphetNetTokenizer from transformers.testing_utils import require_torch, slow from ...test_tokenization_common import TokenizerTesterMixin class SCREAMING_SNAKE_CASE__ ( snake_case_ , unittest.TestCase): lowerCAmelCase_ = ProphetNetTokenizer lowerCAmelCase_ = False def UpperCAmelCase_ ( self )-> Dict: '''simple docstring''' super().setUp() UpperCamelCase = [ '[UNK]', '[CLS]', '[SEP]', '[PAD]', '[MASK]', 'want', '##want', '##ed', 'wa', 'un', 'runn', '##ing', ',', 'low', 'lowest', ] UpperCamelCase = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['vocab_file'] ) with open(self.vocab_file , 'w' , encoding='utf-8' ) as vocab_writer: vocab_writer.write(''.join([x + '\n' for x in vocab_tokens] ) ) def UpperCAmelCase_ ( self , A_ )-> Tuple: '''simple docstring''' UpperCamelCase = 'UNwant\u00E9d,running' UpperCamelCase = 'unwanted, running' return input_text, output_text def UpperCAmelCase_ ( self )-> int: '''simple docstring''' UpperCamelCase = self.tokenizer_class(self.vocab_file ) UpperCamelCase = tokenizer.tokenize('UNwant\u00E9d,running' ) self.assertListEqual(A_ , ['un', '##want', '##ed', ',', 'runn', '##ing'] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(A_ ) , [9, 6, 7, 12, 10, 11] ) def UpperCAmelCase_ ( self )-> Optional[int]: '''simple docstring''' UpperCamelCase = BasicTokenizer() self.assertListEqual(tokenizer.tokenize('ah\u535A\u63A8zz' ) , ['ah', '\u535A', '\u63A8', 'zz'] ) def UpperCAmelCase_ ( self )-> int: '''simple docstring''' UpperCamelCase = BasicTokenizer(do_lower_case=A_ ) self.assertListEqual( tokenizer.tokenize(' \tHeLLo!how \n Are yoU? ' ) , ['hello', '!', 'how', 'are', 'you', '?'] ) self.assertListEqual(tokenizer.tokenize('H\u00E9llo' ) , ['hello'] ) def UpperCAmelCase_ ( self )-> List[Any]: '''simple docstring''' UpperCamelCase = BasicTokenizer(do_lower_case=A_ , strip_accents=A_ ) self.assertListEqual( tokenizer.tokenize(' \tHäLLo!how \n Are yoU? ' ) , ['hällo', '!', 'how', 'are', 'you', '?'] ) self.assertListEqual(tokenizer.tokenize('H\u00E9llo' ) , ['h\u00E9llo'] ) def UpperCAmelCase_ ( self )-> str: '''simple docstring''' UpperCamelCase = BasicTokenizer(do_lower_case=A_ , strip_accents=A_ ) self.assertListEqual( tokenizer.tokenize(' \tHäLLo!how \n Are yoU? ' ) , ['hallo', '!', 'how', 'are', 'you', '?'] ) self.assertListEqual(tokenizer.tokenize('H\u00E9llo' ) , ['hello'] ) def UpperCAmelCase_ ( self )-> Dict: '''simple docstring''' UpperCamelCase = BasicTokenizer(do_lower_case=A_ ) self.assertListEqual( tokenizer.tokenize(' \tHäLLo!how \n Are yoU? ' ) , ['hallo', '!', 'how', 'are', 'you', '?'] ) self.assertListEqual(tokenizer.tokenize('H\u00E9llo' ) , ['hello'] ) def UpperCAmelCase_ ( self )-> Any: '''simple docstring''' UpperCamelCase = BasicTokenizer(do_lower_case=A_ ) self.assertListEqual( tokenizer.tokenize(' \tHeLLo!how \n Are yoU? ' ) , ['HeLLo', '!', 'how', 'Are', 'yoU', '?'] ) def UpperCAmelCase_ ( self )-> Union[str, Any]: '''simple docstring''' UpperCamelCase = BasicTokenizer(do_lower_case=A_ , strip_accents=A_ ) self.assertListEqual( tokenizer.tokenize(' \tHäLLo!how \n Are yoU? ' ) , ['HäLLo', '!', 'how', 'Are', 'yoU', '?'] ) def UpperCAmelCase_ ( self )-> Union[str, Any]: '''simple docstring''' UpperCamelCase = BasicTokenizer(do_lower_case=A_ , strip_accents=A_ ) self.assertListEqual( tokenizer.tokenize(' \tHäLLo!how \n Are yoU? ' ) , ['HaLLo', '!', 'how', 'Are', 'yoU', '?'] ) def UpperCAmelCase_ ( self )-> str: '''simple docstring''' UpperCamelCase = BasicTokenizer(do_lower_case=A_ , never_split=['[UNK]'] ) self.assertListEqual( tokenizer.tokenize(' \tHeLLo!how \n Are yoU? [UNK]' ) , ['HeLLo', '!', 'how', 'Are', 'yoU', '?', '[UNK]'] ) def UpperCAmelCase_ ( self )-> Any: '''simple docstring''' UpperCamelCase = ['[UNK]', '[CLS]', '[SEP]', 'want', '##want', '##ed', 'wa', 'un', 'runn', '##ing'] UpperCamelCase = {} for i, token in enumerate(A_ ): UpperCamelCase = i UpperCamelCase = WordpieceTokenizer(vocab=A_ , unk_token='[UNK]' ) self.assertListEqual(tokenizer.tokenize('' ) , [] ) self.assertListEqual(tokenizer.tokenize('unwanted running' ) , ['un', '##want', '##ed', 'runn', '##ing'] ) self.assertListEqual(tokenizer.tokenize('unwantedX running' ) , ['[UNK]', 'runn', '##ing'] ) @require_torch def UpperCAmelCase_ ( self )-> Optional[int]: '''simple docstring''' UpperCamelCase = self.tokenizer_class.from_pretrained('microsoft/prophetnet-large-uncased' ) UpperCamelCase = ['A long paragraph for summarization.', 'Another paragraph for summarization.'] UpperCamelCase = [1037, 2146, 20423, 2005, 7680, 7849, 3989, 1012, 102] UpperCamelCase = tokenizer(A_ , padding=A_ , return_tensors='pt' ) self.assertIsInstance(A_ , A_ ) UpperCamelCase = list(batch.input_ids.numpy()[0] ) self.assertListEqual(A_ , A_ ) self.assertEqual((2, 9) , batch.input_ids.shape ) self.assertEqual((2, 9) , batch.attention_mask.shape ) def UpperCAmelCase_ ( self )-> str: '''simple docstring''' self.assertTrue(_is_whitespace(' ' ) ) self.assertTrue(_is_whitespace('\t' ) ) self.assertTrue(_is_whitespace('\r' ) ) self.assertTrue(_is_whitespace('\n' ) ) self.assertTrue(_is_whitespace('\u00A0' ) ) self.assertFalse(_is_whitespace('A' ) ) self.assertFalse(_is_whitespace('-' ) ) def UpperCAmelCase_ ( self )-> str: '''simple docstring''' self.assertTrue(_is_control('\u0005' ) ) self.assertFalse(_is_control('A' ) ) self.assertFalse(_is_control(' ' ) ) self.assertFalse(_is_control('\t' ) ) self.assertFalse(_is_control('\r' ) ) def UpperCAmelCase_ ( self )-> Tuple: '''simple docstring''' self.assertTrue(_is_punctuation('-' ) ) self.assertTrue(_is_punctuation('$' ) ) self.assertTrue(_is_punctuation('`' ) ) self.assertTrue(_is_punctuation('.' ) ) self.assertFalse(_is_punctuation('A' ) ) self.assertFalse(_is_punctuation(' ' ) ) @slow def UpperCAmelCase_ ( self )-> int: '''simple docstring''' UpperCamelCase = self.tokenizer_class.from_pretrained('microsoft/prophetnet-large-uncased' ) UpperCamelCase = tokenizer.encode('sequence builders' , add_special_tokens=A_ ) UpperCamelCase = tokenizer.encode('multi-sequence build' , add_special_tokens=A_ ) UpperCamelCase = tokenizer.build_inputs_with_special_tokens(A_ ) UpperCamelCase = tokenizer.build_inputs_with_special_tokens(A_ , A_ ) assert encoded_sentence == text + [102] assert encoded_pair == text + [102] + text_a + [102]
3
'''simple docstring''' from collections import OrderedDict from typing import Any, Mapping, Optional, Union from ...configuration_utils import PretrainedConfig from ...feature_extraction_utils import FeatureExtractionMixin from ...onnx import OnnxConfig from ...onnx.utils import compute_effective_axis_dimension from ...tokenization_utils_base import PreTrainedTokenizerBase from ...utils import TensorType, logging lowerCAmelCase : Any = logging.get_logger(__name__) lowerCAmelCase : Optional[int] = { 'deepmind/language-perceiver': 'https://huggingface.co/deepmind/language-perceiver/resolve/main/config.json', # See all Perceiver models at https://huggingface.co/models?filter=perceiver } class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = """perceiver""" def __init__( self , A_=256 , A_=1280 , A_=768 , A_=1 , A_=26 , A_=8 , A_=8 , A_=None , A_=None , A_="kv" , A_=1 , A_=1 , A_="gelu" , A_=0.1 , A_=0.02 , A_=1e-12 , A_=True , A_=262 , A_=2048 , A_=56 , A_=[368, 496] , A_=16 , A_=1920 , A_=16 , A_=[1, 16, 224, 224] , **A_ , )-> str: '''simple docstring''' super().__init__(**A_ ) UpperCamelCase = num_latents UpperCamelCase = d_latents UpperCamelCase = d_model UpperCamelCase = num_blocks UpperCamelCase = num_self_attends_per_block UpperCamelCase = num_self_attention_heads UpperCamelCase = num_cross_attention_heads UpperCamelCase = qk_channels UpperCamelCase = v_channels UpperCamelCase = cross_attention_shape_for_attention UpperCamelCase = self_attention_widening_factor UpperCamelCase = cross_attention_widening_factor UpperCamelCase = hidden_act UpperCamelCase = attention_probs_dropout_prob UpperCamelCase = initializer_range UpperCamelCase = layer_norm_eps UpperCamelCase = use_query_residual # masked language modeling attributes UpperCamelCase = vocab_size UpperCamelCase = max_position_embeddings # image classification attributes UpperCamelCase = image_size # flow attributes UpperCamelCase = train_size # multimodal autoencoding attributes UpperCamelCase = num_frames UpperCamelCase = audio_samples_per_frame UpperCamelCase = samples_per_patch UpperCamelCase = output_shape class SCREAMING_SNAKE_CASE__ ( snake_case_): @property def UpperCAmelCase_ ( self )-> Mapping[str, Mapping[int, str]]: '''simple docstring''' if self.task == "multiple-choice": UpperCamelCase = {0: 'batch', 1: 'choice', 2: 'sequence'} else: UpperCamelCase = {0: 'batch', 1: 'sequence'} return OrderedDict( [ ('inputs', dynamic_axis), ('attention_mask', dynamic_axis), ] ) @property def UpperCAmelCase_ ( self )-> float: '''simple docstring''' return 1e-4 def UpperCAmelCase_ ( self , A_ , A_ = -1 , A_ = -1 , A_ = -1 , A_ = False , A_ = None , A_ = 3 , A_ = 40 , A_ = 40 , )-> Mapping[str, Any]: '''simple docstring''' if isinstance(A_ , A_ ): # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX UpperCamelCase = compute_effective_axis_dimension( A_ , fixed_dimension=OnnxConfig.default_fixed_batch , num_token_to_add=0 ) # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX UpperCamelCase = preprocessor.num_special_tokens_to_add(A_ ) UpperCamelCase = compute_effective_axis_dimension( A_ , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=A_ ) # Generate dummy inputs according to compute batch and sequence UpperCamelCase = [' '.join(['a'] ) * seq_length] * batch_size UpperCamelCase = dict(preprocessor(A_ , return_tensors=A_ ) ) UpperCamelCase = inputs.pop('input_ids' ) return inputs elif isinstance(A_ , A_ ) and preprocessor.model_input_names[0] == "pixel_values": # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX UpperCamelCase = compute_effective_axis_dimension(A_ , fixed_dimension=OnnxConfig.default_fixed_batch ) UpperCamelCase = self._generate_dummy_images(A_ , A_ , A_ , A_ ) UpperCamelCase = dict(preprocessor(images=A_ , return_tensors=A_ ) ) UpperCamelCase = inputs.pop('pixel_values' ) return inputs else: raise ValueError( 'Unable to generate dummy inputs for the model. Please provide a tokenizer or a preprocessor.' )
3
1
'''simple docstring''' import argparse import logging import os from pathlib import Path from typing import Any, Dict import pytorch_lightning as pl from pytorch_lightning.utilities import rank_zero_info from transformers import ( AdamW, AutoConfig, AutoModel, AutoModelForPreTraining, AutoModelForQuestionAnswering, AutoModelForSeqaSeqLM, AutoModelForSequenceClassification, AutoModelForTokenClassification, AutoModelWithLMHead, AutoTokenizer, PretrainedConfig, PreTrainedTokenizer, ) from transformers.optimization import ( Adafactor, get_cosine_schedule_with_warmup, get_cosine_with_hard_restarts_schedule_with_warmup, get_linear_schedule_with_warmup, get_polynomial_decay_schedule_with_warmup, ) from transformers.utils.versions import require_version lowerCAmelCase : Optional[int] = logging.getLogger(__name__) require_version('pytorch_lightning>=1.0.4') lowerCAmelCase : List[str] = { 'base': AutoModel, 'sequence-classification': AutoModelForSequenceClassification, 'question-answering': AutoModelForQuestionAnswering, 'pretraining': AutoModelForPreTraining, 'token-classification': AutoModelForTokenClassification, 'language-modeling': AutoModelWithLMHead, 'summarization': AutoModelForSeqaSeqLM, 'translation': AutoModelForSeqaSeqLM, } # update this and the import above to support new schedulers from transformers.optimization lowerCAmelCase : int = { 'linear': get_linear_schedule_with_warmup, 'cosine': get_cosine_schedule_with_warmup, 'cosine_w_restarts': get_cosine_with_hard_restarts_schedule_with_warmup, 'polynomial': get_polynomial_decay_schedule_with_warmup, # '': get_constant_schedule, # not supported for now # '': get_constant_schedule_with_warmup, # not supported for now } lowerCAmelCase : Any = sorted(arg_to_scheduler.keys()) lowerCAmelCase : Optional[int] = '{' + ', '.join(arg_to_scheduler_choices) + '}' class SCREAMING_SNAKE_CASE__ ( pl.LightningModule): def __init__( self , A_ , A_=None , A_="base" , A_=None , A_=None , A_=None , **A_ , )-> str: '''simple docstring''' super().__init__() # TODO: move to self.save_hyperparameters() # self.save_hyperparameters() # can also expand arguments into trainer signature for easier reading self.save_hyperparameters(A_ ) UpperCamelCase = 0 UpperCamelCase = Path(self.hparams.output_dir ) UpperCamelCase = self.hparams.cache_dir if self.hparams.cache_dir else None if config is None: UpperCamelCase = AutoConfig.from_pretrained( self.hparams.config_name if self.hparams.config_name else self.hparams.model_name_or_path , **({'num_labels': num_labels} if num_labels is not None else {}) , cache_dir=A_ , **A_ , ) else: UpperCamelCase = config UpperCamelCase = ('encoder_layerdrop', 'decoder_layerdrop', 'dropout', 'attention_dropout') for p in extra_model_params: if getattr(self.hparams , A_ , A_ ): assert hasattr(self.config , A_ ), F'''model config doesn\'t have a `{p}` attribute''' setattr(self.config , A_ , getattr(self.hparams , A_ ) ) if tokenizer is None: UpperCamelCase = AutoTokenizer.from_pretrained( self.hparams.tokenizer_name if self.hparams.tokenizer_name else self.hparams.model_name_or_path , cache_dir=A_ , ) else: UpperCamelCase = tokenizer UpperCamelCase = MODEL_MODES[mode] if model is None: UpperCamelCase = self.model_type.from_pretrained( self.hparams.model_name_or_path , from_tf=bool('.ckpt' in self.hparams.model_name_or_path ) , config=self.config , cache_dir=A_ , ) else: UpperCamelCase = model def UpperCAmelCase_ ( self , *A_ , **A_ )-> Tuple: '''simple docstring''' UpperCamelCase = self.model_type.from_pretrained(*A_ , **A_ ) def UpperCAmelCase_ ( self )-> Union[str, Any]: '''simple docstring''' UpperCamelCase = arg_to_scheduler[self.hparams.lr_scheduler] UpperCamelCase = get_schedule_func( self.opt , num_warmup_steps=self.hparams.warmup_steps , num_training_steps=self.total_steps() ) UpperCamelCase = {'scheduler': scheduler, 'interval': 'step', 'frequency': 1} return scheduler def UpperCAmelCase_ ( self )-> List[str]: '''simple docstring''' UpperCamelCase = self.model UpperCamelCase = ['bias', 'LayerNorm.weight'] UpperCamelCase = [ { 'params': [ p for n, p in model.named_parameters() if not any(nd in n for nd in no_decay ) ], # check this named paramters 'weight_decay': self.hparams.weight_decay, }, { 'params': [p for n, p in model.named_parameters() if any(nd in n for nd in no_decay )], 'weight_decay': 0.0, }, ] if self.hparams.adafactor: UpperCamelCase = Adafactor( A_ , lr=self.hparams.learning_rate , scale_parameter=A_ , relative_step=A_ ) else: UpperCamelCase = AdamW( A_ , lr=self.hparams.learning_rate , eps=self.hparams.adam_epsilon ) UpperCamelCase = optimizer UpperCamelCase = self.get_lr_scheduler() return [optimizer], [scheduler] def UpperCAmelCase_ ( self , A_ , A_ )-> Optional[Any]: '''simple docstring''' return self.validation_step(A_ , A_ ) def UpperCAmelCase_ ( self , A_ )-> Union[str, Any]: '''simple docstring''' return self.validation_end(A_ ) def UpperCAmelCase_ ( self )-> int: '''simple docstring''' UpperCamelCase = max(1 , self.hparams.gpus ) # TODO: consider num_tpu_cores UpperCamelCase = self.hparams.train_batch_size * self.hparams.accumulate_grad_batches * num_devices return (self.dataset_size / effective_batch_size) * self.hparams.max_epochs def UpperCAmelCase_ ( self , A_ )-> str: '''simple docstring''' if stage == "test": UpperCamelCase = len(self.test_dataloader().dataset ) else: UpperCamelCase = self.get_dataloader('train' , self.hparams.train_batch_size , shuffle=A_ ) UpperCamelCase = len(self.train_dataloader().dataset ) def UpperCAmelCase_ ( self , A_ , A_ , A_ = False )-> Dict: '''simple docstring''' raise NotImplementedError('You must implement this for your task' ) def UpperCAmelCase_ ( self )-> str: '''simple docstring''' return self.train_loader def UpperCAmelCase_ ( self )-> List[str]: '''simple docstring''' return self.get_dataloader('dev' , self.hparams.eval_batch_size , shuffle=A_ ) def UpperCAmelCase_ ( self )-> Any: '''simple docstring''' return self.get_dataloader('test' , self.hparams.eval_batch_size , shuffle=A_ ) def UpperCAmelCase_ ( self , A_ )-> int: '''simple docstring''' return os.path.join( self.hparams.data_dir , 'cached_{}_{}_{}'.format( A_ , list(filter(A_ , self.hparams.model_name_or_path.split('/' ) ) ).pop() , str(self.hparams.max_seq_length ) , ) , ) @pl.utilities.rank_zero_only def UpperCAmelCase_ ( self , A_ )-> None: '''simple docstring''' UpperCamelCase = self.output_dir.joinpath('best_tfmr' ) UpperCamelCase = self.step_count self.model.save_pretrained(A_ ) self.tokenizer.save_pretrained(A_ ) @staticmethod def UpperCAmelCase_ ( A_ , A_ )-> Tuple: '''simple docstring''' parser.add_argument( '--model_name_or_path' , default=A_ , type=A_ , required=A_ , help='Path to pretrained model or model identifier from huggingface.co/models' , ) parser.add_argument( '--config_name' , default='' , type=A_ , help='Pretrained config name or path if not the same as model_name' ) parser.add_argument( '--tokenizer_name' , default=A_ , type=A_ , help='Pretrained tokenizer name or path if not the same as model_name' , ) parser.add_argument( '--cache_dir' , default=str(Path(A_ ).parent / 'test_run' / 'cache' ) , type=A_ , help='Where do you want to store the pre-trained models downloaded from huggingface.co' , ) parser.add_argument( '--encoder_layerdrop' , type=A_ , help='Encoder layer dropout probability (Optional). Goes into model.config' , ) parser.add_argument( '--decoder_layerdrop' , type=A_ , help='Decoder layer dropout probability (Optional). Goes into model.config' , ) parser.add_argument( '--dropout' , type=A_ , help='Dropout probability (Optional). Goes into model.config' , ) parser.add_argument( '--attention_dropout' , type=A_ , help='Attention dropout probability (Optional). Goes into model.config' , ) parser.add_argument('--learning_rate' , default=5e-5 , type=A_ , help='The initial learning rate for Adam.' ) parser.add_argument( '--lr_scheduler' , default='linear' , choices=A_ , metavar=A_ , type=A_ , help='Learning rate scheduler' , ) parser.add_argument('--weight_decay' , default=0.0 , type=A_ , help='Weight decay if we apply some.' ) parser.add_argument('--adam_epsilon' , default=1e-8 , type=A_ , help='Epsilon for Adam optimizer.' ) parser.add_argument('--warmup_steps' , default=0 , type=A_ , help='Linear warmup over warmup_steps.' ) parser.add_argument('--num_workers' , default=4 , type=A_ , help='kwarg passed to DataLoader' ) parser.add_argument('--num_train_epochs' , dest='max_epochs' , default=3 , type=A_ ) parser.add_argument('--train_batch_size' , default=32 , type=A_ ) parser.add_argument('--eval_batch_size' , default=32 , type=A_ ) parser.add_argument('--adafactor' , action='store_true' ) class SCREAMING_SNAKE_CASE__ ( pl.Callback): def UpperCAmelCase_ ( self , A_ , A_ )-> Tuple: '''simple docstring''' if ( trainer.is_global_zero and trainer.global_rank == 0 ): # we initialize the retriever only on master worker with RAY. In new pytorch-lightning accelorators are removed. pl_module.model.rag.retriever.init_retrieval() # better to use hook functions. class SCREAMING_SNAKE_CASE__ ( pl.Callback): def UpperCAmelCase_ ( self , A_ , A_ )-> Tuple: '''simple docstring''' for name, param in pl_module.model.rag.named_parameters(): if param.grad is None: print(A_ ) class SCREAMING_SNAKE_CASE__ ( pl.Callback): def UpperCAmelCase_ ( self , A_ , A_ )-> List[Any]: '''simple docstring''' UpperCamelCase = trainer.lr_schedulers[0]['scheduler'] UpperCamelCase = {F'''lr_group_{i}''': lr for i, lr in enumerate(lr_scheduler.get_lr() )} pl_module.logger.log_metrics(A_ ) def UpperCAmelCase_ ( self , A_ , A_ )-> List[str]: '''simple docstring''' rank_zero_info('***** Validation results *****' ) UpperCamelCase = trainer.callback_metrics # Log results for key in sorted(A_ ): if key not in ["log", "progress_bar"]: rank_zero_info('{} = {}\n'.format(A_ , str(metrics[key] ) ) ) def UpperCAmelCase_ ( self , A_ , A_ )-> Union[str, Any]: '''simple docstring''' rank_zero_info('***** Test results *****' ) UpperCamelCase = trainer.callback_metrics # Log and save results to file UpperCamelCase = os.path.join(pl_module.hparams.output_dir , 'test_results.txt' ) with open(A_ , 'w' ) as writer: for key in sorted(A_ ): if key not in ["log", "progress_bar"]: rank_zero_info('{} = {}\n'.format(A_ , str(metrics[key] ) ) ) writer.write('{} = {}\n'.format(A_ , str(metrics[key] ) ) ) def A_( A : Optional[int] , A : Tuple): # To allow all pl args uncomment the following line # parser = pl.Trainer.add_argparse_args(parser) parser.add_argument( '--output_dir' , default=str(Path(A).parent / 'test_run' / 'model_checkpoints') , type=A , help='The output directory where the model predictions and checkpoints will be written.' , ) parser.add_argument( '--fp16' , action='store_true' , help='Whether to use 16-bit (mixed) precision (through NVIDIA apex) instead of 32-bit' , ) parser.add_argument( '--fp16_opt_level' , type=A , default='O2' , help=( 'For fp16: Apex AMP optimization level selected in [\'O0\', \'O1\', \'O2\', and \'O3\'].' 'See details at https://nvidia.github.io/apex/amp.html' ) , ) parser.add_argument('--n_tpu_cores' , dest='tpu_cores' , type=A) parser.add_argument('--max_grad_norm' , dest='gradient_clip_val' , default=1.0 , type=A , help='Max gradient norm') parser.add_argument('--do_train' , action='store_true' , help='Whether to run training.') parser.add_argument('--do_predict' , action='store_true' , help='Whether to run predictions on the test set.') parser.add_argument( '--gradient_accumulation_steps' , dest='accumulate_grad_batches' , type=A , default=1 , help='Number of updates steps to accumulate before performing a backward/update pass.' , ) parser.add_argument('--seed' , type=A , default=42 , help='random seed for initialization') parser.add_argument( '--data_dir' , default=str(Path(A).parent / 'test_run' / 'dummy-train-data') , type=A , help='The input data dir. Should contain the training files for the CoNLL-2003 NER task.' , ) def A_( A : BaseTransformer , A : argparse.Namespace , A : Optional[int]=None , A : Optional[int]=True , A : Dict=[] , A : str=None , A : List[str]=None , **A : Tuple , ): pl.seed_everything(args.seed) # init model UpperCamelCase = Path(model.hparams.output_dir) odir.mkdir(exist_ok=A) # add custom checkpoints if checkpoint_callback is None: UpperCamelCase = pl.callbacks.ModelCheckpoint( filepath=args.output_dir , prefix='checkpoint' , monitor='val_loss' , mode='min' , save_top_k=1) if early_stopping_callback: extra_callbacks.append(A) if logging_callback is None: UpperCamelCase = LoggingCallback() UpperCamelCase = {} if args.fpaa: UpperCamelCase = 16 if args.gpus > 1: UpperCamelCase = 'auto' UpperCamelCase = 'ddp' UpperCamelCase = args.accumulate_grad_batches UpperCamelCase = None UpperCamelCase = 'auto' UpperCamelCase = pl.Trainer.from_argparse_args( A , weights_summary=A , callbacks=[logging_callback] + extra_callbacks + [InitCallback()] + [checkpoint_callback] , logger=A , val_check_interval=1 , num_sanity_val_steps=2 , **A , ) if args.do_train: trainer.fit(A) else: print('RAG modeling tests with new set functions successfuly executed!') return trainer
3
'''simple docstring''' from ....configuration_utils import PretrainedConfig from ....utils import logging lowerCAmelCase : Optional[Any] = logging.get_logger(__name__) lowerCAmelCase : Dict = { 'speechbrain/m-ctc-t-large': 'https://huggingface.co/speechbrain/m-ctc-t-large/resolve/main/config.json', # See all M-CTC-T models at https://huggingface.co/models?filter=mctct } class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = """mctct""" def __init__( self , A_=8065 , A_=1536 , A_=36 , A_=6144 , A_=4 , A_=384 , A_=920 , A_=1e-5 , A_=0.3 , A_="relu" , A_=0.02 , A_=0.3 , A_=0.3 , A_=1 , A_=0 , A_=2 , A_=1 , A_=0.3 , A_=1 , A_=(7,) , A_=(3,) , A_=80 , A_=1 , A_=None , A_="sum" , A_=False , **A_ , )-> str: '''simple docstring''' super().__init__(**A_ , pad_token_id=A_ , bos_token_id=A_ , eos_token_id=A_ ) UpperCamelCase = vocab_size UpperCamelCase = hidden_size UpperCamelCase = num_hidden_layers UpperCamelCase = intermediate_size UpperCamelCase = num_attention_heads UpperCamelCase = attention_head_dim UpperCamelCase = max_position_embeddings UpperCamelCase = layer_norm_eps UpperCamelCase = layerdrop UpperCamelCase = hidden_act UpperCamelCase = initializer_range UpperCamelCase = hidden_dropout_prob UpperCamelCase = attention_probs_dropout_prob UpperCamelCase = pad_token_id UpperCamelCase = bos_token_id UpperCamelCase = eos_token_id UpperCamelCase = conv_glu_dim UpperCamelCase = conv_dropout UpperCamelCase = num_conv_layers UpperCamelCase = input_feat_per_channel UpperCamelCase = input_channels UpperCamelCase = conv_channels UpperCamelCase = ctc_loss_reduction UpperCamelCase = ctc_zero_infinity # prevents config testing fail with exporting to json UpperCamelCase = list(A_ ) UpperCamelCase = list(A_ ) if len(self.conv_kernel ) != self.num_conv_layers: raise ValueError( 'Configuration for convolutional module is incorrect. ' 'It is required that `len(config.conv_kernel)` == `config.num_conv_layers` ' F'''but is `len(config.conv_kernel) = {len(self.conv_kernel )}`, ''' F'''`config.num_conv_layers = {self.num_conv_layers}`.''' )
3
1
'''simple docstring''' import unittest from transformers import is_torch_available, is_vision_available from transformers.testing_utils import require_torch, require_vision, slow, torch_device if is_torch_available(): import torch from transformers import AutoModelForImageClassification if is_vision_available(): from transformers import AutoImageProcessor @require_torch @require_vision class SCREAMING_SNAKE_CASE__ ( unittest.TestCase): @slow def UpperCAmelCase_ ( self )-> int: '''simple docstring''' UpperCamelCase = AutoImageProcessor.from_pretrained('microsoft/dit-base-finetuned-rvlcdip' ) UpperCamelCase = AutoModelForImageClassification.from_pretrained('microsoft/dit-base-finetuned-rvlcdip' ) model.to(A_ ) from datasets import load_dataset UpperCamelCase = load_dataset('nielsr/rvlcdip-demo' ) UpperCamelCase = dataset['train'][0]['image'].convert('RGB' ) UpperCamelCase = image_processor(A_ , return_tensors='pt' ).to(A_ ) # forward pass with torch.no_grad(): UpperCamelCase = model(**A_ ) UpperCamelCase = outputs.logits UpperCamelCase = torch.Size((1, 16) ) self.assertEqual(logits.shape , A_ ) UpperCamelCase = torch.tensor( [-0.4_158, -0.4_092, -0.4_347] , device=A_ , dtype=torch.float , ) self.assertTrue(torch.allclose(logits[0, :3] , A_ , atol=1e-4 ) )
3
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) if is_sentencepiece_available(): from ..ta.tokenization_ta import TaTokenizer else: from ...utils.dummy_sentencepiece_objects import TaTokenizer lowerCAmelCase : Tuple = TaTokenizer if is_tokenizers_available(): from ..ta.tokenization_ta_fast import TaTokenizerFast else: from ...utils.dummy_tokenizers_objects import TaTokenizerFast lowerCAmelCase : Optional[int] = TaTokenizerFast lowerCAmelCase : Any = {'configuration_mt5': ['MT5Config', 'MT5OnnxConfig']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : Optional[int] = [ 'MT5EncoderModel', 'MT5ForConditionalGeneration', 'MT5ForQuestionAnswering', 'MT5Model', 'MT5PreTrainedModel', 'MT5Stack', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : Dict = ['TFMT5EncoderModel', 'TFMT5ForConditionalGeneration', 'TFMT5Model'] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : Optional[Any] = ['FlaxMT5EncoderModel', 'FlaxMT5ForConditionalGeneration', 'FlaxMT5Model'] if TYPE_CHECKING: from .configuration_mta import MTaConfig, MTaOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mta import ( MTaEncoderModel, MTaForConditionalGeneration, MTaForQuestionAnswering, MTaModel, MTaPreTrainedModel, MTaStack, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_mta import TFMTaEncoderModel, TFMTaForConditionalGeneration, TFMTaModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_mta import FlaxMTaEncoderModel, FlaxMTaForConditionalGeneration, FlaxMTaModel else: import sys lowerCAmelCase : Tuple = _LazyModule( __name__, globals()['__file__'], _import_structure, extra_objects={'MT5Tokenizer': MTaTokenizer, 'MT5TokenizerFast': MTaTokenizerFast}, module_spec=__spec__, )
3
1
'''simple docstring''' import warnings from typing import Dict, List, Optional, Tuple from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging lowerCAmelCase : List[str] = logging.get_logger(__name__) class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = ["""input_ids""", """attention_mask"""] def __init__( self , A_="</s>" , A_="<unk>" , A_="<pad>" , A_=125 , A_=None , **A_ , )-> None: '''simple docstring''' if extra_ids > 0 and additional_special_tokens is None: UpperCamelCase = [F'''<extra_id_{i}>''' for i in range(A_ )] elif extra_ids > 0 and additional_special_tokens is not None: # Check that we have the right number of extra_id special tokens UpperCamelCase = len(set(filter(lambda A_ : bool('extra_id' in str(A_ ) ) , A_ ) ) ) if extra_tokens != extra_ids: raise ValueError( F'''Both extra_ids ({extra_ids}) and additional_special_tokens ({additional_special_tokens}) are''' ' provided to ByT5Tokenizer. In this case the additional_special_tokens must include the' ' extra_ids tokens' ) UpperCamelCase = AddedToken(A_ , lstrip=A_ , rstrip=A_ ) if isinstance(A_ , A_ ) else pad_token UpperCamelCase = AddedToken(A_ , lstrip=A_ , rstrip=A_ ) if isinstance(A_ , A_ ) else eos_token UpperCamelCase = AddedToken(A_ , lstrip=A_ , rstrip=A_ ) if isinstance(A_ , A_ ) else unk_token super().__init__( eos_token=A_ , unk_token=A_ , pad_token=A_ , extra_ids=A_ , additional_special_tokens=A_ , **A_ , ) UpperCamelCase = extra_ids UpperCamelCase = 2**8 # utf is 8 bits # define special tokens dict UpperCamelCase = { self.pad_token: 0, self.eos_token: 1, self.unk_token: 2, } UpperCamelCase = len(self.special_tokens_encoder ) UpperCamelCase = len(A_ ) for i, token in enumerate(A_ ): UpperCamelCase = self.vocab_size + i - n UpperCamelCase = {v: k for k, v in self.special_tokens_encoder.items()} @property def UpperCAmelCase_ ( self )-> Any: '''simple docstring''' return self._utf_vocab_size + self._num_special_tokens + self._extra_ids def UpperCAmelCase_ ( self , A_ , A_ = None , A_ = False )-> List[int]: '''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_ ) # normal case: some special tokens if token_ids_a is None: return ([0] * len(A_ )) + [1] return ([0] * len(A_ )) + [1] + ([0] * len(A_ )) + [1] def UpperCAmelCase_ ( self , A_ )-> List[int]: '''simple docstring''' if len(A_ ) > 0 and token_ids[-1] == self.eos_token_id: warnings.warn( F'''This sequence already has {self.eos_token}. In future versions this behavior may lead to duplicated''' ' eos tokens being added.' ) return token_ids else: return token_ids + [self.eos_token_id] def UpperCAmelCase_ ( self , A_ , A_ = None )-> List[int]: '''simple docstring''' UpperCamelCase = [self.eos_token_id] if token_ids_a is None: return len(token_ids_a + eos ) * [0] return len(token_ids_a + eos + token_ids_a + eos ) * [0] def UpperCAmelCase_ ( self , A_ , A_ = None )-> List[int]: '''simple docstring''' UpperCamelCase = self._add_eos_if_not_present(A_ ) if token_ids_a is None: return token_ids_a else: UpperCamelCase = self._add_eos_if_not_present(A_ ) return token_ids_a + token_ids_a def UpperCAmelCase_ ( self , A_ )-> List[str]: '''simple docstring''' UpperCamelCase = [chr(A_ ) for i in text.encode('utf-8' )] return tokens def UpperCAmelCase_ ( self , A_ )-> Any: '''simple docstring''' if token in self.special_tokens_encoder: UpperCamelCase = self.special_tokens_encoder[token] elif token in self.added_tokens_encoder: UpperCamelCase = self.added_tokens_encoder[token] elif len(A_ ) != 1: UpperCamelCase = self.unk_token_id else: UpperCamelCase = ord(A_ ) + self._num_special_tokens return token_id def UpperCAmelCase_ ( self , A_ )-> Optional[int]: '''simple docstring''' if index in self.special_tokens_decoder: UpperCamelCase = self.special_tokens_decoder[index] else: UpperCamelCase = chr(index - self._num_special_tokens ) return token def UpperCAmelCase_ ( self , A_ )-> int: '''simple docstring''' UpperCamelCase = B'' for token in tokens: if token in self.special_tokens_decoder: UpperCamelCase = self.special_tokens_decoder[token].encode('utf-8' ) elif token in self.added_tokens_decoder: UpperCamelCase = self.special_tokens_decoder[token].encode('utf-8' ) elif token in self.special_tokens_encoder: UpperCamelCase = token.encode('utf-8' ) elif token in self.added_tokens_encoder: UpperCamelCase = token.encode('utf-8' ) else: UpperCamelCase = bytes([ord(A_ )] ) bstring += tok_string UpperCamelCase = bstring.decode('utf-8' , errors='ignore' ) return string def UpperCAmelCase_ ( self , A_ , A_ = None )-> Tuple[str]: '''simple docstring''' return ()
3
'''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 SCREAMING_SNAKE_CASE__ ( unittest.TestCase): 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] , )-> Dict: '''simple docstring''' UpperCamelCase = parent UpperCamelCase = batch_size UpperCamelCase = num_channels UpperCamelCase = image_size UpperCamelCase = min_resolution UpperCamelCase = max_resolution UpperCamelCase = do_resize UpperCamelCase = size if size is not None else {'height': 18, 'width': 20} UpperCamelCase = do_thumbnail UpperCamelCase = do_align_axis UpperCamelCase = do_pad UpperCamelCase = do_normalize UpperCamelCase = image_mean UpperCamelCase = image_std def UpperCAmelCase_ ( self )-> List[Any]: '''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 SCREAMING_SNAKE_CASE__ ( snake_case_ , unittest.TestCase): lowerCAmelCase_ = DonutImageProcessor if is_vision_available() else None def UpperCAmelCase_ ( self )-> str: '''simple docstring''' UpperCamelCase = DonutImageProcessingTester(self ) @property def UpperCAmelCase_ ( self )-> str: '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def UpperCAmelCase_ ( self )-> Optional[int]: '''simple docstring''' UpperCamelCase = 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 UpperCAmelCase_ ( self )-> Optional[int]: '''simple docstring''' UpperCamelCase = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'height': 18, 'width': 20} ) UpperCamelCase = 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 UpperCamelCase = self.image_processing_class.from_dict(self.image_processor_dict , size=(42, 84) ) self.assertEqual(image_processor.size , {'height': 84, 'width': 42} ) def UpperCAmelCase_ ( self )-> Tuple: '''simple docstring''' pass @is_flaky() def UpperCAmelCase_ ( self )-> Any: '''simple docstring''' UpperCamelCase = self.image_processing_class(**self.image_processor_dict ) # create random PIL images UpperCamelCase = prepare_image_inputs(self.image_processor_tester , equal_resolution=A_ ) for image in image_inputs: self.assertIsInstance(A_ , Image.Image ) # Test not batched input UpperCamelCase = 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 UpperCamelCase = 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 UpperCAmelCase_ ( self )-> Optional[int]: '''simple docstring''' UpperCamelCase = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors UpperCamelCase = prepare_image_inputs(self.image_processor_tester , equal_resolution=A_ , numpify=A_ ) for image in image_inputs: self.assertIsInstance(A_ , np.ndarray ) # Test not batched input UpperCamelCase = 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 UpperCamelCase = 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 UpperCAmelCase_ ( self )-> Dict: '''simple docstring''' UpperCamelCase = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors UpperCamelCase = prepare_image_inputs(self.image_processor_tester , equal_resolution=A_ , torchify=A_ ) for image in image_inputs: self.assertIsInstance(A_ , torch.Tensor ) # Test not batched input UpperCamelCase = 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 UpperCamelCase = 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'], ) , )
3
1
'''simple docstring''' import json import os import re import unicodedata from json.encoder import INFINITY from typing import Any, Dict, List, Optional, Tuple, Union import numpy as np import regex from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...tokenization_utils_base import BatchEncoding from ...utils import TensorType, is_flax_available, is_tf_available, is_torch_available, logging from ...utils.generic import _is_jax, _is_numpy lowerCAmelCase : Dict = logging.get_logger(__name__) lowerCAmelCase : Tuple = { 'artists_file': 'artists.json', 'lyrics_file': 'lyrics.json', 'genres_file': 'genres.json', } lowerCAmelCase : Optional[Any] = { 'artists_file': { 'jukebox': 'https://huggingface.co/ArthurZ/jukebox/blob/main/artists.json', }, 'genres_file': { 'jukebox': 'https://huggingface.co/ArthurZ/jukebox/blob/main/genres.json', }, 'lyrics_file': { 'jukebox': 'https://huggingface.co/ArthurZ/jukebox/blob/main/lyrics.json', }, } lowerCAmelCase : Optional[int] = { 'jukebox': 5_12, } class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = VOCAB_FILES_NAMES lowerCAmelCase_ = PRETRAINED_VOCAB_FILES_MAP lowerCAmelCase_ = PRETRAINED_LYRIC_TOKENS_SIZES lowerCAmelCase_ = ["""input_ids""", """attention_mask"""] def __init__( self , A_ , A_ , A_ , A_=["v3", "v2", "v2"] , A_=512 , A_=5 , A_="<|endoftext|>" , **A_ , )-> str: '''simple docstring''' UpperCamelCase = AddedToken(A_ , lstrip=A_ , rstrip=A_ ) if isinstance(A_ , A_ ) else unk_token super().__init__( unk_token=A_ , n_genres=A_ , version=A_ , max_n_lyric_tokens=A_ , **A_ , ) UpperCamelCase = version UpperCamelCase = max_n_lyric_tokens UpperCamelCase = n_genres with open(A_ , encoding='utf-8' ) as vocab_handle: UpperCamelCase = json.load(A_ ) with open(A_ , encoding='utf-8' ) as vocab_handle: UpperCamelCase = json.load(A_ ) with open(A_ , encoding='utf-8' ) as vocab_handle: UpperCamelCase = json.load(A_ ) UpperCamelCase = R'[^A-Za-z0-9.,:;!?\-\'\"()\[\] \t\n]+' # In v2, we had a n_vocab=80 and in v3 we missed + and so n_vocab=79 of characters. if len(self.lyrics_encoder ) == 79: UpperCamelCase = oov.replace(R'\-\'' , R'\-+\'' ) UpperCamelCase = regex.compile(A_ ) UpperCamelCase = {v: k for k, v in self.artists_encoder.items()} UpperCamelCase = {v: k for k, v in self.genres_encoder.items()} UpperCamelCase = {v: k for k, v in self.lyrics_encoder.items()} @property def UpperCAmelCase_ ( self )-> str: '''simple docstring''' return len(self.artists_encoder ) + len(self.genres_encoder ) + len(self.lyrics_encoder ) def UpperCAmelCase_ ( self )-> Dict: '''simple docstring''' return dict(self.artists_encoder , self.genres_encoder , self.lyrics_encoder ) def UpperCAmelCase_ ( self , A_ , A_ , A_ )-> Dict: '''simple docstring''' UpperCamelCase = [self.artists_encoder.get(A_ , 0 ) for artist in list_artists] for genres in range(len(A_ ) ): UpperCamelCase = [self.genres_encoder.get(A_ , 0 ) for genre in list_genres[genres]] UpperCamelCase = list_genres[genres] + [-1] * (self.n_genres - len(list_genres[genres] )) UpperCamelCase = [[self.lyrics_encoder.get(A_ , 0 ) for character in list_lyrics[0]], [], []] return artists_id, list_genres, lyric_ids def UpperCAmelCase_ ( self , A_ )-> Optional[Any]: '''simple docstring''' return list(A_ ) def UpperCAmelCase_ ( self , A_ , A_ , A_ , **A_ )-> Optional[Any]: '''simple docstring''' UpperCamelCase , UpperCamelCase , UpperCamelCase = self.prepare_for_tokenization(A_ , A_ , A_ ) UpperCamelCase = self._tokenize(A_ ) return artist, genre, lyrics def UpperCAmelCase_ ( self , A_ , A_ , A_ , A_ = False )-> Tuple[str, str, str, Dict[str, Any]]: '''simple docstring''' for idx in range(len(self.version ) ): if self.version[idx] == "v3": UpperCamelCase = artists[idx].lower() UpperCamelCase = [genres[idx].lower()] else: UpperCamelCase = self._normalize(artists[idx] ) + '.v2' UpperCamelCase = [ self._normalize(A_ ) + '.v2' for genre in genres[idx].split('_' ) ] # split is for the full dictionary with combined genres if self.version[0] == "v2": UpperCamelCase = regex.compile(R'[^A-Za-z0-9.,:;!?\-\'\"()\[\] \t\n]+' ) UpperCamelCase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.,:;!?-+\'\"()[] \t\n' UpperCamelCase = {vocab[index]: index + 1 for index in range(len(A_ ) )} UpperCamelCase = 0 UpperCamelCase = len(A_ ) + 1 UpperCamelCase = self.vocab UpperCamelCase = {v: k for k, v in self.vocab.items()} UpperCamelCase = '' else: UpperCamelCase = regex.compile(R'[^A-Za-z0-9.,:;!?\-+\'\"()\[\] \t\n]+' ) UpperCamelCase = self._run_strip_accents(A_ ) UpperCamelCase = lyrics.replace('\\' , '\n' ) UpperCamelCase = self.out_of_vocab.sub('' , A_ ), [], [] return artists, genres, lyrics def UpperCAmelCase_ ( self , A_ )-> str: '''simple docstring''' UpperCamelCase = unicodedata.normalize('NFD' , A_ ) UpperCamelCase = [] for char in text: UpperCamelCase = unicodedata.category(A_ ) if cat == "Mn": continue output.append(A_ ) return "".join(A_ ) def UpperCAmelCase_ ( self , A_ )-> str: '''simple docstring''' UpperCamelCase = ( [chr(A_ ) for i in range(ord('a' ) , ord('z' ) + 1 )] + [chr(A_ ) for i in range(ord('A' ) , ord('Z' ) + 1 )] + [chr(A_ ) for i in range(ord('0' ) , ord('9' ) + 1 )] + ['.'] ) UpperCamelCase = frozenset(A_ ) UpperCamelCase = re.compile(R'_+' ) UpperCamelCase = ''.join([c if c in accepted else '_' for c in text.lower()] ) UpperCamelCase = pattern.sub('_' , A_ ).strip('_' ) return text def UpperCAmelCase_ ( self , A_ )-> str: '''simple docstring''' return " ".join(A_ ) def UpperCAmelCase_ ( self , A_ , A_ = None , A_ = False )-> Any: '''simple docstring''' if not isinstance(A_ , A_ ): UpperCamelCase = TensorType(A_ ) # Get a function reference for the correct framework if tensor_type == TensorType.TENSORFLOW: if not is_tf_available(): raise ImportError( 'Unable to convert output to TensorFlow tensors format, TensorFlow is not installed.' ) import tensorflow as tf UpperCamelCase = tf.constant UpperCamelCase = tf.is_tensor elif tensor_type == TensorType.PYTORCH: if not is_torch_available(): raise ImportError('Unable to convert output to PyTorch tensors format, PyTorch is not installed.' ) import torch UpperCamelCase = torch.tensor UpperCamelCase = torch.is_tensor elif tensor_type == TensorType.JAX: if not is_flax_available(): raise ImportError('Unable to convert output to JAX tensors format, JAX is not installed.' ) import jax.numpy as jnp # noqa: F811 UpperCamelCase = jnp.array UpperCamelCase = _is_jax else: UpperCamelCase = np.asarray UpperCamelCase = _is_numpy # Do the tensor conversion in batch try: if prepend_batch_axis: UpperCamelCase = [inputs] if not is_tensor(A_ ): UpperCamelCase = as_tensor(A_ ) except: # noqa E722 raise ValueError( 'Unable to create tensor, you should probably activate truncation and/or padding ' 'with \'padding=True\' \'truncation=True\' to have batched tensors with the same length.' ) return inputs def __call__( self , A_ , A_ , A_="" , A_="pt" )-> BatchEncoding: '''simple docstring''' UpperCamelCase = [0, 0, 0] UpperCamelCase = [artist] * len(self.version ) UpperCamelCase = [genres] * len(self.version ) UpperCamelCase , UpperCamelCase , UpperCamelCase = self.tokenize(A_ , A_ , A_ ) UpperCamelCase , UpperCamelCase , UpperCamelCase = self._convert_token_to_id(A_ , A_ , A_ ) UpperCamelCase = [-INFINITY] * len(full_tokens[-1] ) UpperCamelCase = [ self.convert_to_tensors( [input_ids + [artists_id[i]] + genres_ids[i] + full_tokens[i]] , tensor_type=A_ ) for i in range(len(self.version ) ) ] return BatchEncoding({'input_ids': input_ids, 'attention_masks': attention_masks} ) def UpperCAmelCase_ ( self , A_ , A_ = None )-> Tuple[str]: '''simple docstring''' if not os.path.isdir(A_ ): logger.error(F'''Vocabulary path ({save_directory}) should be a directory''' ) return UpperCamelCase = os.path.join( A_ , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['artists_file'] ) with open(A_ , 'w' , encoding='utf-8' ) as f: f.write(json.dumps(self.artists_encoder , ensure_ascii=A_ ) ) UpperCamelCase = os.path.join( A_ , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['genres_file'] ) with open(A_ , 'w' , encoding='utf-8' ) as f: f.write(json.dumps(self.genres_encoder , ensure_ascii=A_ ) ) UpperCamelCase = os.path.join( A_ , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['lyrics_file'] ) with open(A_ , 'w' , encoding='utf-8' ) as f: f.write(json.dumps(self.lyrics_encoder , ensure_ascii=A_ ) ) return (artists_file, genres_file, lyrics_file) def UpperCAmelCase_ ( self , A_ , A_ , A_ )-> str: '''simple docstring''' UpperCamelCase = self.artists_decoder.get(A_ ) UpperCamelCase = [self.genres_decoder.get(A_ ) for genre in genres_index] UpperCamelCase = [self.lyrics_decoder.get(A_ ) for character in lyric_index] return artist, genres, lyrics
3
'''simple docstring''' def A_( A : list[int]): UpperCamelCase = [] if len(A) == 1: return [nums.copy()] for _ in range(len(A)): UpperCamelCase = nums.pop(0) UpperCamelCase = permute(A) for perm in permutations: perm.append(A) result.extend(A) nums.append(A) return result def A_( A : str): def backtrack(A : str): if start == len(A) - 1: output.append(nums[:]) else: for i in range(A , len(A)): UpperCamelCase , UpperCamelCase = nums[i], nums[start] backtrack(start + 1) UpperCamelCase , UpperCamelCase = nums[i], nums[start] # backtrack UpperCamelCase = [] backtrack(0) return output if __name__ == "__main__": import doctest # use res to print the data in permute2 function lowerCAmelCase : Dict = permutea([1, 2, 3]) print(res) doctest.testmod()
3
1
'''simple docstring''' import json import pathlib import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision, slow from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import DeformableDetrImageProcessor class SCREAMING_SNAKE_CASE__ ( unittest.TestCase): def __init__( self , A_ , A_=7 , A_=3 , A_=30 , A_=400 , A_=True , A_=None , A_=True , A_=[0.5, 0.5, 0.5] , A_=[0.5, 0.5, 0.5] , A_=True , A_=1 / 255 , A_=True , )-> int: '''simple docstring''' UpperCamelCase = size if size is not None else {'shortest_edge': 18, 'longest_edge': 1333} UpperCamelCase = parent UpperCamelCase = batch_size UpperCamelCase = num_channels UpperCamelCase = min_resolution UpperCamelCase = max_resolution UpperCamelCase = do_resize UpperCamelCase = size UpperCamelCase = do_normalize UpperCamelCase = image_mean UpperCamelCase = image_std UpperCamelCase = do_rescale UpperCamelCase = rescale_factor UpperCamelCase = do_pad def UpperCAmelCase_ ( self )-> Dict: '''simple docstring''' return { "do_resize": self.do_resize, "size": self.size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, "do_rescale": self.do_rescale, "rescale_factor": self.rescale_factor, "do_pad": self.do_pad, } def UpperCAmelCase_ ( self , A_ , A_=False )-> Union[str, Any]: '''simple docstring''' if not batched: UpperCamelCase = image_inputs[0] if isinstance(A_ , Image.Image ): UpperCamelCase , UpperCamelCase = image.size else: UpperCamelCase , UpperCamelCase = image.shape[1], image.shape[2] if w < h: UpperCamelCase = int(self.size['shortest_edge'] * h / w ) UpperCamelCase = self.size['shortest_edge'] elif w > h: UpperCamelCase = self.size['shortest_edge'] UpperCamelCase = int(self.size['shortest_edge'] * w / h ) else: UpperCamelCase = self.size['shortest_edge'] UpperCamelCase = self.size['shortest_edge'] else: UpperCamelCase = [] for image in image_inputs: UpperCamelCase , UpperCamelCase = self.get_expected_values([image] ) expected_values.append((expected_height, expected_width) ) UpperCamelCase = max(A_ , key=lambda A_ : item[0] )[0] UpperCamelCase = max(A_ , key=lambda A_ : item[1] )[1] return expected_height, expected_width @require_torch @require_vision class SCREAMING_SNAKE_CASE__ ( snake_case_ , unittest.TestCase): lowerCAmelCase_ = DeformableDetrImageProcessor if is_vision_available() else None def UpperCAmelCase_ ( self )-> Optional[Any]: '''simple docstring''' UpperCamelCase = DeformableDetrImageProcessingTester(self ) @property def UpperCAmelCase_ ( self )-> Optional[int]: '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def UpperCAmelCase_ ( self )-> Union[str, Any]: '''simple docstring''' UpperCamelCase = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(A_ , 'image_mean' ) ) self.assertTrue(hasattr(A_ , 'image_std' ) ) self.assertTrue(hasattr(A_ , 'do_normalize' ) ) self.assertTrue(hasattr(A_ , 'do_resize' ) ) self.assertTrue(hasattr(A_ , 'do_rescale' ) ) self.assertTrue(hasattr(A_ , 'do_pad' ) ) self.assertTrue(hasattr(A_ , 'size' ) ) def UpperCAmelCase_ ( self )-> Optional[Any]: '''simple docstring''' UpperCamelCase = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'shortest_edge': 18, 'longest_edge': 1333} ) self.assertEqual(image_processor.do_pad , A_ ) UpperCamelCase = self.image_processing_class.from_dict( self.image_processor_dict , size=42 , max_size=84 , pad_and_return_pixel_mask=A_ ) self.assertEqual(image_processor.size , {'shortest_edge': 42, 'longest_edge': 84} ) self.assertEqual(image_processor.do_pad , A_ ) def UpperCAmelCase_ ( self )-> int: '''simple docstring''' pass def UpperCAmelCase_ ( self )-> int: '''simple docstring''' UpperCamelCase = self.image_processing_class(**self.image_processor_dict ) # create random PIL images UpperCamelCase = prepare_image_inputs(self.image_processor_tester , equal_resolution=A_ ) for image in image_inputs: self.assertIsInstance(A_ , Image.Image ) # Test not batched input UpperCamelCase = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values UpperCamelCase , UpperCamelCase = self.image_processor_tester.get_expected_values(A_ ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched UpperCamelCase , UpperCamelCase = self.image_processor_tester.get_expected_values(A_ , batched=A_ ) UpperCamelCase = 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, expected_height, expected_width, ) , ) def UpperCAmelCase_ ( self )-> List[Any]: '''simple docstring''' UpperCamelCase = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors UpperCamelCase = prepare_image_inputs(self.image_processor_tester , equal_resolution=A_ , numpify=A_ ) for image in image_inputs: self.assertIsInstance(A_ , np.ndarray ) # Test not batched input UpperCamelCase = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values UpperCamelCase , UpperCamelCase = self.image_processor_tester.get_expected_values(A_ ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched UpperCamelCase = image_processing(A_ , return_tensors='pt' ).pixel_values UpperCamelCase , UpperCamelCase = self.image_processor_tester.get_expected_values(A_ , batched=A_ ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def UpperCAmelCase_ ( self )-> List[Any]: '''simple docstring''' UpperCamelCase = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors UpperCamelCase = prepare_image_inputs(self.image_processor_tester , equal_resolution=A_ , torchify=A_ ) for image in image_inputs: self.assertIsInstance(A_ , torch.Tensor ) # Test not batched input UpperCamelCase = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values UpperCamelCase , UpperCamelCase = self.image_processor_tester.get_expected_values(A_ ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched UpperCamelCase = image_processing(A_ , return_tensors='pt' ).pixel_values UpperCamelCase , UpperCamelCase = self.image_processor_tester.get_expected_values(A_ , batched=A_ ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) @slow def UpperCAmelCase_ ( self )-> str: '''simple docstring''' UpperCamelCase = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) with open('./tests/fixtures/tests_samples/COCO/coco_annotations.txt' , 'r' ) as f: UpperCamelCase = json.loads(f.read() ) UpperCamelCase = {'image_id': 39769, 'annotations': target} # encode them UpperCamelCase = DeformableDetrImageProcessor() UpperCamelCase = image_processing(images=A_ , annotations=A_ , return_tensors='pt' ) # verify pixel values UpperCamelCase = torch.Size([1, 3, 800, 1066] ) self.assertEqual(encoding['pixel_values'].shape , A_ ) UpperCamelCase = torch.tensor([0.2_796, 0.3_138, 0.3_481] ) self.assertTrue(torch.allclose(encoding['pixel_values'][0, 0, 0, :3] , A_ , atol=1e-4 ) ) # verify area UpperCamelCase = torch.tensor([5_887.9_600, 11_250.2_061, 489_353.8_438, 837_122.7_500, 147_967.5_156, 165_732.3_438] ) self.assertTrue(torch.allclose(encoding['labels'][0]['area'] , A_ ) ) # verify boxes UpperCamelCase = torch.Size([6, 4] ) self.assertEqual(encoding['labels'][0]['boxes'].shape , A_ ) UpperCamelCase = torch.tensor([0.5_503, 0.2_765, 0.0_604, 0.2_215] ) self.assertTrue(torch.allclose(encoding['labels'][0]['boxes'][0] , A_ , atol=1e-3 ) ) # verify image_id UpperCamelCase = torch.tensor([39769] ) self.assertTrue(torch.allclose(encoding['labels'][0]['image_id'] , A_ ) ) # verify is_crowd UpperCamelCase = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding['labels'][0]['iscrowd'] , A_ ) ) # verify class_labels UpperCamelCase = torch.tensor([75, 75, 63, 65, 17, 17] ) self.assertTrue(torch.allclose(encoding['labels'][0]['class_labels'] , A_ ) ) # verify orig_size UpperCamelCase = torch.tensor([480, 640] ) self.assertTrue(torch.allclose(encoding['labels'][0]['orig_size'] , A_ ) ) # verify size UpperCamelCase = torch.tensor([800, 1066] ) self.assertTrue(torch.allclose(encoding['labels'][0]['size'] , A_ ) ) @slow def UpperCAmelCase_ ( self )-> Optional[int]: '''simple docstring''' UpperCamelCase = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) with open('./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt' , 'r' ) as f: UpperCamelCase = json.loads(f.read() ) UpperCamelCase = {'file_name': '000000039769.png', 'image_id': 39769, 'segments_info': target} UpperCamelCase = pathlib.Path('./tests/fixtures/tests_samples/COCO/coco_panoptic' ) # encode them UpperCamelCase = DeformableDetrImageProcessor(format='coco_panoptic' ) UpperCamelCase = image_processing(images=A_ , annotations=A_ , masks_path=A_ , return_tensors='pt' ) # verify pixel values UpperCamelCase = torch.Size([1, 3, 800, 1066] ) self.assertEqual(encoding['pixel_values'].shape , A_ ) UpperCamelCase = torch.tensor([0.2_796, 0.3_138, 0.3_481] ) self.assertTrue(torch.allclose(encoding['pixel_values'][0, 0, 0, :3] , A_ , atol=1e-4 ) ) # verify area UpperCamelCase = torch.tensor([147_979.6_875, 165_527.0_469, 484_638.5_938, 11_292.9_375, 5_879.6_562, 7_634.1_147] ) self.assertTrue(torch.allclose(encoding['labels'][0]['area'] , A_ ) ) # verify boxes UpperCamelCase = torch.Size([6, 4] ) self.assertEqual(encoding['labels'][0]['boxes'].shape , A_ ) UpperCamelCase = torch.tensor([0.2_625, 0.5_437, 0.4_688, 0.8_625] ) self.assertTrue(torch.allclose(encoding['labels'][0]['boxes'][0] , A_ , atol=1e-3 ) ) # verify image_id UpperCamelCase = torch.tensor([39769] ) self.assertTrue(torch.allclose(encoding['labels'][0]['image_id'] , A_ ) ) # verify is_crowd UpperCamelCase = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding['labels'][0]['iscrowd'] , A_ ) ) # verify class_labels UpperCamelCase = torch.tensor([17, 17, 63, 75, 75, 93] ) self.assertTrue(torch.allclose(encoding['labels'][0]['class_labels'] , A_ ) ) # verify masks UpperCamelCase = 822873 self.assertEqual(encoding['labels'][0]['masks'].sum().item() , A_ ) # verify orig_size UpperCamelCase = torch.tensor([480, 640] ) self.assertTrue(torch.allclose(encoding['labels'][0]['orig_size'] , A_ ) ) # verify size UpperCamelCase = torch.tensor([800, 1066] ) self.assertTrue(torch.allclose(encoding['labels'][0]['size'] , A_ ) )
3
'''simple docstring''' import colorsys from PIL import Image # type: ignore def A_( A : float , A : float , A : int): UpperCamelCase = x UpperCamelCase = y for step in range(A): # noqa: B007 UpperCamelCase = a * a - b * b + x UpperCamelCase = 2 * a * b + y UpperCamelCase = a_new # divergence happens for all complex number with an absolute value # greater than 4 if a * a + b * b > 4: break return step / (max_step - 1) def A_( A : float): if distance == 1: return (0, 0, 0) else: return (255, 255, 255) def A_( A : float): if distance == 1: return (0, 0, 0) else: return tuple(round(i * 255) for i in colorsys.hsv_to_rgb(A , 1 , 1)) def A_( A : int = 800 , A : int = 600 , A : float = -0.6 , A : float = 0 , A : float = 3.2 , A : int = 50 , A : bool = True , ): UpperCamelCase = Image.new('RGB' , (image_width, image_height)) UpperCamelCase = img.load() # loop through the image-coordinates for image_x in range(A): for image_y in range(A): # determine the figure-coordinates based on the image-coordinates UpperCamelCase = figure_width / image_width * image_height UpperCamelCase = figure_center_x + (image_x / image_width - 0.5) * figure_width UpperCamelCase = figure_center_y + (image_y / image_height - 0.5) * figure_height UpperCamelCase = get_distance(A , A , A) # color the corresponding pixel based on the selected coloring-function if use_distance_color_coding: UpperCamelCase = get_color_coded_rgb(A) else: UpperCamelCase = get_black_and_white_rgb(A) return img if __name__ == "__main__": import doctest doctest.testmod() # colored version, full figure lowerCAmelCase : Any = get_image() # uncomment for colored version, different section, zoomed in # img = get_image(figure_center_x = -0.6, figure_center_y = -0.4, # figure_width = 0.8) # uncomment for black and white version, full figure # img = get_image(use_distance_color_coding = False) # uncomment to save the image # img.save("mandelbrot.png") img.show()
3
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_torch_available, ) lowerCAmelCase : Dict = { 'configuration_encodec': [ 'ENCODEC_PRETRAINED_CONFIG_ARCHIVE_MAP', 'EncodecConfig', ], 'feature_extraction_encodec': ['EncodecFeatureExtractor'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : List[Any] = [ 'ENCODEC_PRETRAINED_MODEL_ARCHIVE_LIST', 'EncodecModel', 'EncodecPreTrainedModel', ] if TYPE_CHECKING: from .configuration_encodec import ( ENCODEC_PRETRAINED_CONFIG_ARCHIVE_MAP, EncodecConfig, ) from .feature_extraction_encodec import EncodecFeatureExtractor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_encodec import ( ENCODEC_PRETRAINED_MODEL_ARCHIVE_LIST, EncodecModel, EncodecPreTrainedModel, ) else: import sys lowerCAmelCase : Dict = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
3
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_torch_available, ) lowerCAmelCase : Optional[Any] = { 'configuration_falcon': ['FALCON_PRETRAINED_CONFIG_ARCHIVE_MAP', 'FalconConfig'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : str = [ 'FALCON_PRETRAINED_MODEL_ARCHIVE_LIST', 'FalconForCausalLM', 'FalconModel', 'FalconPreTrainedModel', 'FalconForSequenceClassification', 'FalconForTokenClassification', 'FalconForQuestionAnswering', ] if TYPE_CHECKING: from .configuration_falcon import FALCON_PRETRAINED_CONFIG_ARCHIVE_MAP, FalconConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_falcon import ( FALCON_PRETRAINED_MODEL_ARCHIVE_LIST, FalconForCausalLM, FalconForQuestionAnswering, FalconForSequenceClassification, FalconForTokenClassification, FalconModel, FalconPreTrainedModel, ) else: import sys lowerCAmelCase : Tuple = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
3
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_torch_available, ) lowerCAmelCase : Optional[Any] = { 'configuration_falcon': ['FALCON_PRETRAINED_CONFIG_ARCHIVE_MAP', 'FalconConfig'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : str = [ 'FALCON_PRETRAINED_MODEL_ARCHIVE_LIST', 'FalconForCausalLM', 'FalconModel', 'FalconPreTrainedModel', 'FalconForSequenceClassification', 'FalconForTokenClassification', 'FalconForQuestionAnswering', ] if TYPE_CHECKING: from .configuration_falcon import FALCON_PRETRAINED_CONFIG_ARCHIVE_MAP, FalconConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_falcon import ( FALCON_PRETRAINED_MODEL_ARCHIVE_LIST, FalconForCausalLM, FalconForQuestionAnswering, FalconForSequenceClassification, FalconForTokenClassification, FalconModel, FalconPreTrainedModel, ) else: import sys lowerCAmelCase : Tuple = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
3
'''simple docstring''' lowerCAmelCase : Optional[Any] = { 'A': ['B', 'C', 'E'], 'B': ['A', 'D', 'E'], 'C': ['A', 'F', 'G'], 'D': ['B'], 'E': ['A', 'B', 'D'], 'F': ['C'], 'G': ['C'], } def A_( A : dict , A : str , A : Optional[Any]): UpperCamelCase = set() # keep track of all the paths to be checked UpperCamelCase = [[start]] # return path if start is goal if start == goal: return [start] # keeps looping until all possible paths have been checked while queue: # pop the first path from the queue UpperCamelCase = queue.pop(0) # get the last node from the path UpperCamelCase = path[-1] if node not in explored: UpperCamelCase = graph[node] # go through all neighbour nodes, construct a new path and # push it into the queue for neighbour in neighbours: UpperCamelCase = list(A) new_path.append(A) queue.append(A) # return path if neighbour is goal if neighbour == goal: return new_path # mark node as explored explored.add(A) # in case there's no path between the 2 nodes return [] def A_( A : dict , A : str , A : Tuple): if not graph or start not in graph or target not in graph: return -1 if start == target: return 0 UpperCamelCase = [start] UpperCamelCase = set(A) # Keep tab on distances from `start` node. UpperCamelCase = {start: 0, target: -1} while queue: UpperCamelCase = queue.pop(0) if node == target: UpperCamelCase = ( dist[node] if dist[target] == -1 else min(dist[target] , dist[node]) ) for adjacent in graph[node]: if adjacent not in visited: visited.add(A) queue.append(A) UpperCamelCase = dist[node] + 1 return dist[target] if __name__ == "__main__": print(bfs_shortest_path(demo_graph, 'G', 'D')) # returns ['G', 'C', 'A', 'B', 'D'] print(bfs_shortest_path_distance(demo_graph, 'G', 'D')) # returns 4
3
1
import os # Precomputes a list of the 100 first triangular numbers SCREAMING_SNAKE_CASE__ : List[str] = [int(0.5 * n * (n + 1)) for n in range(1, 1_01)] def __lowercase ( ): """simple docstring""" __magic_name__ :List[Any] = os.path.dirname(os.path.realpath(snake_case ) ) __magic_name__ :str = os.path.join(snake_case, '''words.txt''' ) __magic_name__ :Any = '''''' with open(snake_case ) as f: __magic_name__ :List[Any] = f.readline() __magic_name__ :List[str] = [word.strip('''"''' ) for word in words.strip('''\r\n''' ).split(''',''' )] __magic_name__ :List[str] = [ word for word in [sum(ord(snake_case ) - 6_4 for x in word ) for word in words] if word in TRIANGULAR_NUMBERS ] return len(snake_case ) if __name__ == "__main__": print(solution())
0
'''simple docstring''' import copy import os import cva import numpy as np from matplotlib import pyplot as plt class SCREAMING_SNAKE_CASE__ : def __init__( self )-> Dict: '''simple docstring''' UpperCamelCase = '' UpperCamelCase = '' UpperCamelCase = [] UpperCamelCase = 0 UpperCamelCase = 256 UpperCamelCase = 0 UpperCamelCase = 0 UpperCamelCase = 0 UpperCamelCase = 0 def UpperCAmelCase_ ( self , A_ )-> str: '''simple docstring''' UpperCamelCase = cva.imread(A_ , 0 ) UpperCamelCase = copy.deepcopy(self.img ) UpperCamelCase , UpperCamelCase , UpperCamelCase = plt.hist(self.img.ravel() , 256 , [0, 256] , label='x' ) UpperCamelCase = np.sum(A_ ) for i in range(len(A_ ) ): UpperCamelCase = x[i] / self.k self.sk += prk UpperCamelCase = (self.L - 1) * self.sk if self.rem != 0: UpperCamelCase = int(last % last ) UpperCamelCase = int(last + 1 if self.rem >= 0.5 else last ) self.last_list.append(A_ ) UpperCamelCase = int(np.ma.count(self.img ) / self.img[1].size ) UpperCamelCase = self.img[1].size for i in range(self.number_of_cols ): for j in range(self.number_of_rows ): UpperCamelCase = self.img[j][i] if num != self.last_list[num]: UpperCamelCase = self.last_list[num] cva.imwrite('output_data/output.jpg' , self.img ) def UpperCAmelCase_ ( self )-> Any: '''simple docstring''' plt.hist(self.img.ravel() , 256 , [0, 256] ) def UpperCAmelCase_ ( self )-> Optional[Any]: '''simple docstring''' cva.imshow('Output-Image' , self.img ) cva.imshow('Input-Image' , self.original_image ) cva.waitKey(5000 ) cva.destroyAllWindows() if __name__ == "__main__": lowerCAmelCase : Union[str, Any] = os.path.join(os.path.basename(__file__), 'image_data/input.jpg') lowerCAmelCase : str = ConstantStretch() stretcher.stretch(file_path) stretcher.plot_histogram() stretcher.show_image()
3
0
def _A ( _lowercase , _lowercase ) -> int: """simple docstring""" return 1 if input_a == input_a else 0 def _A ( ) -> None: """simple docstring""" assert xnor_gate(0 , 0 ) == 1 assert xnor_gate(0 , 1 ) == 0 assert xnor_gate(1 , 0 ) == 0 assert xnor_gate(1 , 1 ) == 1 if __name__ == "__main__": print(xnor_gate(0, 0)) print(xnor_gate(0, 1)) print(xnor_gate(1, 0)) print(xnor_gate(1, 1))
1
'''simple docstring''' import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase : int = logging.get_logger(__name__) lowerCAmelCase : Tuple = { 'microsoft/unispeech-sat-base-100h-libri-ft': ( 'https://huggingface.co/microsoft/unispeech-sat-base-100h-libri-ft/resolve/main/config.json' ), # See all UniSpeechSat models at https://huggingface.co/models?filter=unispeech_sat } class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = """unispeech-sat""" def __init__( self , A_=32 , A_=768 , A_=12 , A_=12 , A_=3072 , A_="gelu" , A_=0.1 , A_=0.1 , A_=0.1 , A_=0.0 , A_=0.0 , A_=0.1 , A_=0.1 , A_=0.02 , A_=1e-5 , A_="group" , A_="gelu" , A_=(512, 512, 512, 512, 512, 512, 512) , A_=(5, 2, 2, 2, 2, 2, 2) , A_=(10, 3, 3, 3, 3, 2, 2) , A_=False , A_=128 , A_=16 , A_=False , A_=True , A_=0.05 , A_=10 , A_=2 , A_=0.0 , A_=10 , A_=0 , A_=320 , A_=2 , A_=0.1 , A_=100 , A_=256 , A_=256 , A_=0.1 , A_="mean" , A_=False , A_=False , A_=256 , A_=(512, 512, 512, 512, 1500) , A_=(5, 3, 3, 1, 1) , A_=(1, 2, 3, 1, 1) , A_=512 , A_=0 , A_=1 , A_=2 , A_=504 , **A_ , )-> Tuple: '''simple docstring''' super().__init__(**A_ , pad_token_id=A_ , bos_token_id=A_ , eos_token_id=A_ ) UpperCamelCase = hidden_size UpperCamelCase = feat_extract_norm UpperCamelCase = feat_extract_activation UpperCamelCase = list(A_ ) UpperCamelCase = list(A_ ) UpperCamelCase = list(A_ ) UpperCamelCase = conv_bias UpperCamelCase = num_conv_pos_embeddings UpperCamelCase = num_conv_pos_embedding_groups UpperCamelCase = len(self.conv_dim ) UpperCamelCase = num_hidden_layers UpperCamelCase = intermediate_size UpperCamelCase = hidden_act UpperCamelCase = num_attention_heads UpperCamelCase = hidden_dropout UpperCamelCase = attention_dropout UpperCamelCase = activation_dropout UpperCamelCase = feat_proj_dropout UpperCamelCase = final_dropout UpperCamelCase = layerdrop UpperCamelCase = layer_norm_eps UpperCamelCase = initializer_range UpperCamelCase = vocab_size UpperCamelCase = num_clusters UpperCamelCase = do_stable_layer_norm UpperCamelCase = use_weighted_layer_sum 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)`, but is `len(config.conv_dim) =' F''' {len(self.conv_dim )}`, `len(config.conv_stride) = {len(self.conv_stride )}`,''' F''' `len(config.conv_kernel) = {len(self.conv_kernel )}`.''' ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 UpperCamelCase = apply_spec_augment UpperCamelCase = mask_time_prob UpperCamelCase = mask_time_length UpperCamelCase = mask_time_min_masks UpperCamelCase = mask_feature_prob UpperCamelCase = mask_feature_length UpperCamelCase = mask_feature_min_masks # parameters for pretraining with codevector quantized representations UpperCamelCase = num_codevectors_per_group UpperCamelCase = num_codevector_groups UpperCamelCase = contrastive_logits_temperature UpperCamelCase = feat_quantizer_dropout UpperCamelCase = num_negatives UpperCamelCase = codevector_dim UpperCamelCase = proj_codevector_dim UpperCamelCase = diversity_loss_weight # ctc loss UpperCamelCase = ctc_loss_reduction UpperCamelCase = ctc_zero_infinity # SequenceClassification-specific parameter. Feel free to ignore for other classes. UpperCamelCase = classifier_proj_size # XVector-specific parameters. Feel free to ignore for other classes. UpperCamelCase = list(A_ ) UpperCamelCase = list(A_ ) UpperCamelCase = list(A_ ) UpperCamelCase = xvector_output_dim @property def UpperCAmelCase_ ( self )-> Optional[Any]: '''simple docstring''' return functools.reduce(operator.mul , self.conv_stride , 1 )
3
0
# We ignore warnings about stepping the scheduler since we step it ourselves during gradient accumulation import warnings from .state import AcceleratorState, GradientState warnings.filterwarnings("""ignore""", category=UserWarning, module="""torch.optim.lr_scheduler""") class lowerCamelCase__ : """simple docstring""" def __init__( self : int , __lowerCAmelCase : int , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : bool = True , __lowerCAmelCase : bool = False ) -> Union[str, Any]: _A = scheduler _A = optimizers if isinstance(__lowerCAmelCase , (list, tuple) ) else [optimizers] _A = split_batches _A = step_with_optimizer _A = GradientState() def snake_case_ ( self : List[str] , *__lowerCAmelCase : List[str] , **__lowerCAmelCase : List[Any] ) -> Optional[int]: if not self.step_with_optimizer: # No link between scheduler and optimizer -> just step self.scheduler.step(*__lowerCAmelCase , **__lowerCAmelCase ) return # Otherwise, first make sure the optimizer was stepped. if not self.gradient_state.sync_gradients: if self.gradient_state.adjust_scheduler: self.scheduler._step_count += 1 return for opt in self.optimizers: if opt.step_was_skipped: return if self.split_batches: # Split batches -> the training dataloader batch size is not changed so one step per training step self.scheduler.step(*__lowerCAmelCase , **__lowerCAmelCase ) else: # Otherwise the training dataloader batch size was multiplied by `num_processes`, so we need to do # num_processes steps per training step _A = AcceleratorState().num_processes for _ in range(__lowerCAmelCase ): # Special case when using OneCycle and `drop_last` was not used if hasattr(self.scheduler , '''total_steps''' ): if self.scheduler._step_count <= self.scheduler.total_steps: self.scheduler.step(*__lowerCAmelCase , **__lowerCAmelCase ) else: self.scheduler.step(*__lowerCAmelCase , **__lowerCAmelCase ) def snake_case_ ( self : int ) -> List[Any]: return self.scheduler.get_last_lr() def snake_case_ ( self : str ) -> Optional[int]: return self.scheduler.state_dict() def snake_case_ ( self : Optional[int] , __lowerCAmelCase : Optional[int] ) -> Optional[Any]: self.scheduler.load_state_dict(__lowerCAmelCase ) def snake_case_ ( self : str ) -> int: return self.scheduler.get_lr() def snake_case_ ( self : Optional[Any] , *__lowerCAmelCase : List[Any] , **__lowerCAmelCase : List[Any] ) -> Tuple: return self.scheduler.print_lr(*__lowerCAmelCase , **__lowerCAmelCase )
2
'''simple docstring''' import inspect import unittest from datasets import load_dataset from packaging import version from transformers import BeitConfig from transformers.models.auto import get_values from transformers.testing_utils import require_torch, require_torch_multi_gpu, 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, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ( MODEL_MAPPING, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation, BeitModel, ) from transformers.models.beit.modeling_beit import BEIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): import PIL from PIL import Image from transformers import BeitImageProcessor class SCREAMING_SNAKE_CASE__ : def __init__( self , A_ , A_=100 , A_=13 , A_=30 , A_=2 , A_=3 , A_=True , A_=True , A_=32 , A_=4 , A_=4 , A_=37 , A_="gelu" , A_=0.1 , A_=0.1 , A_=10 , A_=0.02 , A_=3 , A_=None , A_=[0, 1, 2, 3] , )-> Any: '''simple docstring''' UpperCamelCase = parent UpperCamelCase = 100 UpperCamelCase = batch_size UpperCamelCase = image_size UpperCamelCase = patch_size UpperCamelCase = num_channels UpperCamelCase = is_training UpperCamelCase = use_labels UpperCamelCase = hidden_size UpperCamelCase = num_hidden_layers UpperCamelCase = num_attention_heads UpperCamelCase = intermediate_size UpperCamelCase = hidden_act UpperCamelCase = hidden_dropout_prob UpperCamelCase = attention_probs_dropout_prob UpperCamelCase = type_sequence_label_size UpperCamelCase = initializer_range UpperCamelCase = scope UpperCamelCase = out_indices UpperCamelCase = num_labels # in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) UpperCamelCase = (image_size // patch_size) ** 2 UpperCamelCase = num_patches + 1 def UpperCAmelCase_ ( self )-> List[str]: '''simple docstring''' UpperCamelCase = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) UpperCamelCase = None UpperCamelCase = None if self.use_labels: UpperCamelCase = ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCamelCase = ids_tensor([self.batch_size, self.image_size, self.image_size] , self.num_labels ) UpperCamelCase = self.get_config() return config, pixel_values, labels, pixel_labels def UpperCAmelCase_ ( self )-> Dict: '''simple docstring''' return BeitConfig( vocab_size=self.vocab_size , 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 , out_indices=self.out_indices , ) def UpperCAmelCase_ ( self , A_ , A_ , A_ , A_ )-> List[str]: '''simple docstring''' UpperCamelCase = BeitModel(config=A_ ) model.to(A_ ) model.eval() UpperCamelCase = model(A_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def UpperCAmelCase_ ( self , A_ , A_ , A_ , A_ )-> Any: '''simple docstring''' UpperCamelCase = BeitForMaskedImageModeling(config=A_ ) model.to(A_ ) model.eval() UpperCamelCase = model(A_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length - 1, self.vocab_size) ) def UpperCAmelCase_ ( self , A_ , A_ , A_ , A_ )-> Optional[int]: '''simple docstring''' UpperCamelCase = self.type_sequence_label_size UpperCamelCase = BeitForImageClassification(A_ ) model.to(A_ ) model.eval() UpperCamelCase = model(A_ , labels=A_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images UpperCamelCase = 1 UpperCamelCase = BeitForImageClassification(A_ ) model.to(A_ ) model.eval() UpperCamelCase = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) UpperCamelCase = model(A_ , labels=A_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def UpperCAmelCase_ ( self , A_ , A_ , A_ , A_ )-> Optional[Any]: '''simple docstring''' UpperCamelCase = self.num_labels UpperCamelCase = BeitForSemanticSegmentation(A_ ) model.to(A_ ) model.eval() UpperCamelCase = model(A_ ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2) ) UpperCamelCase = model(A_ , labels=A_ ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2) ) def UpperCAmelCase_ ( self )-> int: '''simple docstring''' UpperCamelCase = self.prepare_config_and_inputs() UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase = config_and_inputs UpperCamelCase = {'pixel_values': pixel_values} return config, inputs_dict @require_torch class SCREAMING_SNAKE_CASE__ ( snake_case_ , snake_case_ , unittest.TestCase): lowerCAmelCase_ = ( (BeitModel, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation) if is_torch_available() else () ) lowerCAmelCase_ = ( { """feature-extraction""": BeitModel, """image-classification""": BeitForImageClassification, """image-segmentation""": BeitForSemanticSegmentation, } if is_torch_available() else {} ) lowerCAmelCase_ = False lowerCAmelCase_ = False lowerCAmelCase_ = False def UpperCAmelCase_ ( self )-> Any: '''simple docstring''' UpperCamelCase = BeitModelTester(self ) UpperCamelCase = ConfigTester(self , config_class=A_ , has_text_modality=A_ , hidden_size=37 ) def UpperCAmelCase_ ( self )-> Union[str, Any]: '''simple docstring''' self.config_tester.run_common_tests() @unittest.skip(reason='BEiT does not use inputs_embeds' ) def UpperCAmelCase_ ( self )-> Optional[int]: '''simple docstring''' pass @require_torch_multi_gpu @unittest.skip(reason='BEiT has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`' ) def UpperCAmelCase_ ( self )-> Optional[Any]: '''simple docstring''' pass def UpperCAmelCase_ ( self )-> Tuple: '''simple docstring''' UpperCamelCase , UpperCamelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCamelCase = model_class(A_ ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) UpperCamelCase = model.get_output_embeddings() self.assertTrue(x is None or isinstance(A_ , nn.Linear ) ) def UpperCAmelCase_ ( self )-> List[Any]: '''simple docstring''' UpperCamelCase , UpperCamelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCamelCase = model_class(A_ ) UpperCamelCase = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic UpperCamelCase = [*signature.parameters.keys()] UpperCamelCase = ['pixel_values'] self.assertListEqual(arg_names[:1] , A_ ) def UpperCAmelCase_ ( self )-> Union[str, Any]: '''simple docstring''' UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*A_ ) def UpperCAmelCase_ ( self )-> List[Any]: '''simple docstring''' UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*A_ ) def UpperCAmelCase_ ( self )-> Any: '''simple docstring''' UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*A_ ) def UpperCAmelCase_ ( self )-> List[str]: '''simple docstring''' UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_semantic_segmentation(*A_ ) def UpperCAmelCase_ ( self )-> int: '''simple docstring''' if not self.model_tester.is_training: return UpperCamelCase , UpperCamelCase = self.model_tester.prepare_config_and_inputs_for_common() UpperCamelCase = True for model_class in self.all_model_classes: # we don't test BeitForMaskedImageModeling if model_class in [*get_values(A_ ), BeitForMaskedImageModeling]: continue UpperCamelCase = model_class(A_ ) model.to(A_ ) model.train() UpperCamelCase = self._prepare_for_class(A_ , A_ , return_labels=A_ ) UpperCamelCase = model(**A_ ).loss loss.backward() def UpperCAmelCase_ ( self )-> List[str]: '''simple docstring''' UpperCamelCase , UpperCamelCase = self.model_tester.prepare_config_and_inputs_for_common() if not self.model_tester.is_training: return UpperCamelCase = False UpperCamelCase = True for model_class in self.all_model_classes: # we don't test BeitForMaskedImageModeling if ( model_class in [*get_values(A_ ), BeitForMaskedImageModeling] or not model_class.supports_gradient_checkpointing ): continue UpperCamelCase = model_class(A_ ) model.gradient_checkpointing_enable() model.to(A_ ) model.train() UpperCamelCase = self._prepare_for_class(A_ , A_ , return_labels=A_ ) UpperCamelCase = model(**A_ ).loss loss.backward() def UpperCAmelCase_ ( self )-> Union[str, Any]: '''simple docstring''' UpperCamelCase , UpperCamelCase = self.model_tester.prepare_config_and_inputs_for_common() UpperCamelCase = _config_zero_init(A_ ) for model_class in self.all_model_classes: UpperCamelCase = model_class(config=A_ ) for name, param in model.named_parameters(): # we skip lambda parameters as these require special initial values # determined by config.layer_scale_init_value if "lambda" in name: continue if param.requires_grad: self.assertIn( ((param.data.mean() * 1e9).round() / 1e9).item() , [0.0, 1.0] , msg=F'''Parameter {name} of model {model_class} seems not properly initialized''' , ) @slow def UpperCAmelCase_ ( self )-> Dict: '''simple docstring''' for model_name in BEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCamelCase = BeitModel.from_pretrained(A_ ) self.assertIsNotNone(A_ ) def A_( ): UpperCamelCase = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png') return image @require_torch @require_vision class SCREAMING_SNAKE_CASE__ ( unittest.TestCase): @cached_property def UpperCAmelCase_ ( self )-> Optional[int]: '''simple docstring''' return BeitImageProcessor.from_pretrained('microsoft/beit-base-patch16-224' ) if is_vision_available() else None @slow def UpperCAmelCase_ ( self )-> Optional[Any]: '''simple docstring''' UpperCamelCase = BeitForMaskedImageModeling.from_pretrained('microsoft/beit-base-patch16-224-pt22k' ).to(A_ ) UpperCamelCase = self.default_image_processor UpperCamelCase = prepare_img() UpperCamelCase = image_processor(images=A_ , return_tensors='pt' ).pixel_values.to(A_ ) # prepare bool_masked_pos UpperCamelCase = torch.ones((1, 196) , dtype=torch.bool ).to(A_ ) # forward pass with torch.no_grad(): UpperCamelCase = model(pixel_values=A_ , bool_masked_pos=A_ ) UpperCamelCase = outputs.logits # verify the logits UpperCamelCase = torch.Size((1, 196, 8192) ) self.assertEqual(logits.shape , A_ ) UpperCamelCase = torch.tensor( [[-3.2_437, 0.5_072, -13.9_174], [-3.2_456, 0.4_948, -13.9_401], [-3.2_033, 0.5_121, -13.8_550]] ).to(A_ ) self.assertTrue(torch.allclose(logits[bool_masked_pos][:3, :3] , A_ , atol=1e-2 ) ) @slow def UpperCAmelCase_ ( self )-> Union[str, Any]: '''simple docstring''' UpperCamelCase = BeitForImageClassification.from_pretrained('microsoft/beit-base-patch16-224' ).to(A_ ) UpperCamelCase = self.default_image_processor UpperCamelCase = prepare_img() UpperCamelCase = image_processor(images=A_ , return_tensors='pt' ).to(A_ ) # forward pass with torch.no_grad(): UpperCamelCase = model(**A_ ) UpperCamelCase = outputs.logits # verify the logits UpperCamelCase = torch.Size((1, 1000) ) self.assertEqual(logits.shape , A_ ) UpperCamelCase = torch.tensor([-1.2_385, -1.0_987, -1.0_108] ).to(A_ ) self.assertTrue(torch.allclose(logits[0, :3] , A_ , atol=1e-4 ) ) UpperCamelCase = 281 self.assertEqual(logits.argmax(-1 ).item() , A_ ) @slow def UpperCAmelCase_ ( self )-> Optional[Any]: '''simple docstring''' UpperCamelCase = BeitForImageClassification.from_pretrained('microsoft/beit-large-patch16-224-pt22k-ft22k' ).to( A_ ) UpperCamelCase = self.default_image_processor UpperCamelCase = prepare_img() UpperCamelCase = image_processor(images=A_ , return_tensors='pt' ).to(A_ ) # forward pass with torch.no_grad(): UpperCamelCase = model(**A_ ) UpperCamelCase = outputs.logits # verify the logits UpperCamelCase = torch.Size((1, 21841) ) self.assertEqual(logits.shape , A_ ) UpperCamelCase = torch.tensor([1.6_881, -0.2_787, 0.5_901] ).to(A_ ) self.assertTrue(torch.allclose(logits[0, :3] , A_ , atol=1e-4 ) ) UpperCamelCase = 2396 self.assertEqual(logits.argmax(-1 ).item() , A_ ) @slow def UpperCAmelCase_ ( self )-> Any: '''simple docstring''' UpperCamelCase = BeitForSemanticSegmentation.from_pretrained('microsoft/beit-base-finetuned-ade-640-640' ) UpperCamelCase = model.to(A_ ) UpperCamelCase = BeitImageProcessor(do_resize=A_ , size=640 , do_center_crop=A_ ) UpperCamelCase = load_dataset('hf-internal-testing/fixtures_ade20k' , split='test' ) UpperCamelCase = Image.open(ds[0]['file'] ) UpperCamelCase = image_processor(images=A_ , return_tensors='pt' ).to(A_ ) # forward pass with torch.no_grad(): UpperCamelCase = model(**A_ ) UpperCamelCase = outputs.logits # verify the logits UpperCamelCase = torch.Size((1, 150, 160, 160) ) self.assertEqual(logits.shape , A_ ) UpperCamelCase = version.parse(PIL.__version__ ) < version.parse('9.0.0' ) if is_pillow_less_than_a: UpperCamelCase = torch.tensor( [ [[-4.9_225, -2.3_954, -3.0_522], [-2.8_822, -1.0_046, -1.7_561], [-2.9_549, -1.3_228, -2.1_347]], [[-5.8_168, -3.4_129, -4.0_778], [-3.8_651, -2.2_214, -3.0_277], [-3.8_356, -2.4_643, -3.3_535]], [[-0.0_078, 3.9_952, 4.0_754], [2.9_856, 4.6_944, 5.0_035], [3.2_413, 4.7_813, 4.9_969]], ] , device=A_ , ) else: UpperCamelCase = torch.tensor( [ [[-4.8_960, -2.3_688, -3.0_355], [-2.8_478, -0.9_836, -1.7_418], [-2.9_449, -1.3_332, -2.1_456]], [[-5.8_081, -3.4_124, -4.1_006], [-3.8_561, -2.2_081, -3.0_323], [-3.8_365, -2.4_601, -3.3_669]], [[-0.0_309, 3.9_868, 4.0_540], [2.9_640, 4.6_877, 4.9_976], [3.2_081, 4.7_690, 4.9_942]], ] , device=A_ , ) self.assertTrue(torch.allclose(logits[0, :3, :3, :3] , A_ , atol=1e-4 ) ) @slow def UpperCAmelCase_ ( self )-> Tuple: '''simple docstring''' UpperCamelCase = BeitForSemanticSegmentation.from_pretrained('microsoft/beit-base-finetuned-ade-640-640' ) UpperCamelCase = model.to(A_ ) UpperCamelCase = BeitImageProcessor(do_resize=A_ , size=640 , do_center_crop=A_ ) UpperCamelCase = load_dataset('hf-internal-testing/fixtures_ade20k' , split='test' ) UpperCamelCase = Image.open(ds[0]['file'] ) UpperCamelCase = image_processor(images=A_ , return_tensors='pt' ).to(A_ ) # forward pass with torch.no_grad(): UpperCamelCase = model(**A_ ) UpperCamelCase = outputs.logits.detach().cpu() UpperCamelCase = image_processor.post_process_semantic_segmentation(outputs=A_ , target_sizes=[(500, 300)] ) UpperCamelCase = torch.Size((500, 300) ) self.assertEqual(segmentation[0].shape , A_ ) UpperCamelCase = image_processor.post_process_semantic_segmentation(outputs=A_ ) UpperCamelCase = torch.Size((160, 160) ) self.assertEqual(segmentation[0].shape , A_ )
3
0
"""simple docstring""" import math def _SCREAMING_SNAKE_CASE (_UpperCAmelCase : int ): if not isinstance(_UpperCAmelCase , _UpperCAmelCase ): lowerCAmelCase = F'Input value of [number={number}] must be an integer' raise TypeError(_UpperCAmelCase ) if number < 1: lowerCAmelCase = F'Input value of [number={number}] must be > 0' raise ValueError(_UpperCAmelCase ) elif number == 1: return 3 elif number == 2: return 5 else: lowerCAmelCase = int(math.log(number // 3 , 2 ) ) + 2 lowerCAmelCase = [3, 5] lowerCAmelCase = 2 lowerCAmelCase = 3 for block in range(1 , _UpperCAmelCase ): for _ in range(_UpperCAmelCase ): proth_list.append(2 ** (block + 1) + proth_list[proth_index - 1] ) proth_index += 1 increment *= 2 return proth_list[number - 1] if __name__ == "__main__": import doctest doctest.testmod() for number in range(11): __UpperCamelCase : Optional[int] = 0 try: __UpperCamelCase : List[str] = proth(number) except ValueError: print(f'''ValueError: there is no {number}th Proth number''') continue print(f'''The {number}th Proth number: {value}''')
4
'''simple docstring''' import enum import warnings from ..tokenization_utils import TruncationStrategy from ..utils import add_end_docstrings, is_tf_available, is_torch_available, logging from .base import PIPELINE_INIT_ARGS, Pipeline if is_tf_available(): import tensorflow as tf from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING if is_torch_available(): from ..models.auto.modeling_auto import MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING lowerCAmelCase : Dict = logging.get_logger(__name__) class SCREAMING_SNAKE_CASE__ ( enum.Enum): lowerCAmelCase_ = 0 lowerCAmelCase_ = 1 @add_end_docstrings(snake_case_) class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = """generated""" def __init__( self , *A_ , **A_ )-> Optional[int]: '''simple docstring''' super().__init__(*A_ , **A_ ) self.check_model_type( TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING if self.framework == 'tf' else MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING ) def UpperCAmelCase_ ( self , A_=None , A_=None , A_=None , A_=None , A_=None , A_=None , **A_ , )-> Optional[Any]: '''simple docstring''' UpperCamelCase = {} if truncation is not None: UpperCamelCase = truncation UpperCamelCase = generate_kwargs UpperCamelCase = {} if return_tensors is not None and return_type is None: UpperCamelCase = ReturnType.TENSORS if return_tensors else ReturnType.TEXT if return_type is not None: UpperCamelCase = return_type if clean_up_tokenization_spaces is not None: UpperCamelCase = clean_up_tokenization_spaces if stop_sequence is not None: UpperCamelCase = self.tokenizer.encode(A_ , add_special_tokens=A_ ) if len(A_ ) > 1: warnings.warn( 'Stopping on a multiple token sequence is not yet supported on transformers. The first token of' ' the stop sequence will be used as the stop sequence string in the interim.' ) UpperCamelCase = stop_sequence_ids[0] return preprocess_params, forward_params, postprocess_params def UpperCAmelCase_ ( self , A_ , A_ , A_ )-> Optional[int]: '''simple docstring''' return True def UpperCAmelCase_ ( self , *A_ , A_ )-> Any: '''simple docstring''' UpperCamelCase = self.model.config.prefix if self.model.config.prefix is not None else '' if isinstance(args[0] , A_ ): if self.tokenizer.pad_token_id is None: raise ValueError('Please make sure that the tokenizer has a pad_token_id when using a batch input' ) UpperCamelCase = ([prefix + arg for arg in args[0]],) UpperCamelCase = True elif isinstance(args[0] , A_ ): UpperCamelCase = (prefix + args[0],) UpperCamelCase = False else: raise ValueError( F''' `args[0]`: {args[0]} have the wrong format. The should be either of type `str` or type `list`''' ) UpperCamelCase = self.tokenizer(*A_ , padding=A_ , truncation=A_ , return_tensors=self.framework ) # This is produced by tokenizers but is an invalid generate kwargs if "token_type_ids" in inputs: del inputs["token_type_ids"] return inputs def __call__( self , *A_ , **A_ )-> Union[str, Any]: '''simple docstring''' UpperCamelCase = super().__call__(*A_ , **A_ ) if ( isinstance(args[0] , A_ ) and all(isinstance(A_ , A_ ) for el in args[0] ) and all(len(A_ ) == 1 for res in result ) ): return [res[0] for res in result] return result def UpperCAmelCase_ ( self , A_ , A_=TruncationStrategy.DO_NOT_TRUNCATE , **A_ )-> Any: '''simple docstring''' UpperCamelCase = self._parse_and_tokenize(A_ , truncation=A_ , **A_ ) return inputs def UpperCAmelCase_ ( self , A_ , **A_ )-> int: '''simple docstring''' if self.framework == "pt": UpperCamelCase , UpperCamelCase = model_inputs['input_ids'].shape elif self.framework == "tf": UpperCamelCase , UpperCamelCase = tf.shape(model_inputs['input_ids'] ).numpy() UpperCamelCase = generate_kwargs.get('min_length' , self.model.config.min_length ) UpperCamelCase = generate_kwargs.get('max_length' , self.model.config.max_length ) self.check_inputs(A_ , generate_kwargs['min_length'] , generate_kwargs['max_length'] ) UpperCamelCase = self.model.generate(**A_ , **A_ ) UpperCamelCase = output_ids.shape[0] if self.framework == "pt": UpperCamelCase = output_ids.reshape(A_ , out_b // in_b , *output_ids.shape[1:] ) elif self.framework == "tf": UpperCamelCase = tf.reshape(A_ , (in_b, out_b // in_b, *output_ids.shape[1:]) ) return {"output_ids": output_ids} def UpperCAmelCase_ ( self , A_ , A_=ReturnType.TEXT , A_=False )-> Optional[Any]: '''simple docstring''' UpperCamelCase = [] for output_ids in model_outputs["output_ids"][0]: if return_type == ReturnType.TENSORS: UpperCamelCase = {F'''{self.return_name}_token_ids''': output_ids} elif return_type == ReturnType.TEXT: UpperCamelCase = { F'''{self.return_name}_text''': self.tokenizer.decode( A_ , skip_special_tokens=A_ , clean_up_tokenization_spaces=A_ , ) } records.append(A_ ) return records @add_end_docstrings(snake_case_) class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = """summary""" def __call__( self , *A_ , **A_ )-> Optional[int]: '''simple docstring''' return super().__call__(*A_ , **A_ ) def UpperCAmelCase_ ( self , A_ , A_ , A_ )-> bool: '''simple docstring''' if max_length < min_length: logger.warning(F'''Your min_length={min_length} must be inferior than your max_length={max_length}.''' ) if input_length < max_length: logger.warning( F'''Your max_length is set to {max_length}, but your input_length is only {input_length}. Since this is ''' 'a summarization task, where outputs shorter than the input are typically wanted, you might ' F'''consider decreasing max_length manually, e.g. summarizer(\'...\', max_length={input_length//2})''' ) @add_end_docstrings(snake_case_) class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = """translation""" def UpperCAmelCase_ ( self , A_ , A_ , A_ )-> List[Any]: '''simple docstring''' if input_length > 0.9 * max_length: logger.warning( F'''Your input_length: {input_length} is bigger than 0.9 * max_length: {max_length}. You might consider ''' 'increasing your max_length manually, e.g. translator(\'...\', max_length=400)' ) return True def UpperCAmelCase_ ( self , *A_ , A_=TruncationStrategy.DO_NOT_TRUNCATE , A_=None , A_=None )-> Dict: '''simple docstring''' if getattr(self.tokenizer , '_build_translation_inputs' , A_ ): return self.tokenizer._build_translation_inputs( *A_ , return_tensors=self.framework , truncation=A_ , src_lang=A_ , tgt_lang=A_ ) else: return super()._parse_and_tokenize(*A_ , truncation=A_ ) def UpperCAmelCase_ ( self , A_=None , A_=None , **A_ )-> str: '''simple docstring''' UpperCamelCase , UpperCamelCase , UpperCamelCase = super()._sanitize_parameters(**A_ ) if src_lang is not None: UpperCamelCase = src_lang if tgt_lang is not None: UpperCamelCase = tgt_lang if src_lang is None and tgt_lang is None: # Backward compatibility, direct arguments use is preferred. UpperCamelCase = kwargs.get('task' , self.task ) UpperCamelCase = task.split('_' ) if task and len(A_ ) == 4: # translation, XX, to YY UpperCamelCase = items[1] UpperCamelCase = items[3] return preprocess_params, forward_params, postprocess_params def __call__( self , *A_ , **A_ )-> Any: '''simple docstring''' return super().__call__(*A_ , **A_ )
3
0
'''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 UpperCAmelCase_ ( _SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' _lowercase : Dict = KandinskyVaaControlnetImgaImgPipeline _lowercase : int = ['''image_embeds''', '''negative_image_embeds''', '''image''', '''hint'''] _lowercase : Tuple = ['''image_embeds''', '''negative_image_embeds''', '''image''', '''hint'''] _lowercase : Dict = [ '''generator''', '''height''', '''width''', '''strength''', '''guidance_scale''', '''num_inference_steps''', '''return_dict''', '''guidance_scale''', '''num_images_per_prompt''', '''output_type''', '''return_dict''', ] _lowercase : str = False @property def _lowercase ( self ): """simple docstring""" return 32 @property def _lowercase ( self ): """simple docstring""" return 32 @property def _lowercase ( self ): """simple docstring""" return self.time_input_dim @property def _lowercase ( self ): """simple docstring""" return self.time_input_dim * 4 @property def _lowercase ( self ): """simple docstring""" return 100 @property def _lowercase ( 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(**_lowercase ) return model @property def _lowercase ( 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 _lowercase ( self ): """simple docstring""" torch.manual_seed(0 ) _lowerCAmelCase = VQModel(**self.dummy_movq_kwargs ) return model def _lowercase ( self ): """simple docstring""" _lowerCAmelCase = self.dummy_unet _lowerCAmelCase = self.dummy_movq _lowerCAmelCase = { """num_train_timesteps""": 1_000, """beta_schedule""": """linear""", """beta_start""": 0.0_0085, """beta_end""": 0.012, """clip_sample""": False, """set_alpha_to_one""": False, """steps_offset""": 0, """prediction_type""": """epsilon""", """thresholding""": False, } _lowerCAmelCase = DDIMScheduler(**_lowercase ) _lowerCAmelCase = { """unet""": unet, """scheduler""": scheduler, """movq""": movq, } return components def _lowercase ( self , _lowercase , _lowercase=0 ): """simple docstring""" _lowerCAmelCase = floats_tensor((1, self.text_embedder_hidden_size) , rng=random.Random(_lowercase ) ).to(_lowercase ) _lowerCAmelCase = floats_tensor((1, self.text_embedder_hidden_size) , rng=random.Random(seed + 1 ) ).to( _lowercase ) # create init_image _lowerCAmelCase = floats_tensor((1, 3, 64, 64) , rng=random.Random(_lowercase ) ).to(_lowercase ) _lowerCAmelCase = image.cpu().permute(0 , 2 , 3 , 1 )[0] _lowerCAmelCase = Image.fromarray(np.uinta(_lowercase ) ).convert("""RGB""" ).resize((256, 256) ) # create hint _lowerCAmelCase = floats_tensor((1, 3, 64, 64) , rng=random.Random(_lowercase ) ).to(_lowercase ) if str(_lowercase ).startswith("""mps""" ): _lowerCAmelCase = torch.manual_seed(_lowercase ) else: _lowerCAmelCase = torch.Generator(device=_lowercase ).manual_seed(_lowercase ) _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 _lowercase ( self ): """simple docstring""" _lowerCAmelCase = """cpu""" _lowerCAmelCase = self.get_dummy_components() _lowerCAmelCase = self.pipeline_class(**_lowercase ) _lowerCAmelCase = pipe.to(_lowercase ) pipe.set_progress_bar_config(disable=_lowercase ) _lowerCAmelCase = pipe(**self.get_dummy_inputs(_lowercase ) ) _lowerCAmelCase = output.images _lowerCAmelCase = pipe( **self.get_dummy_inputs(_lowercase ) , return_dict=_lowercase , )[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.5498_5034, 0.5550_9365, 0.5256_1504, 0.557_0494, 0.559_3818, 0.526_3979, 0.5028_5643, 0.506_9846, 0.5119_6736] ) 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 UpperCAmelCase_ ( unittest.TestCase ): '''simple docstring''' def _lowercase ( self ): """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def _lowercase ( 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(_lowercase ) ).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(_lowercase ) _lowerCAmelCase = KandinskyVaaControlnetImgaImgPipeline.from_pretrained( """kandinsky-community/kandinsky-2-2-controlnet-depth""" , torch_dtype=torch.floataa ) _lowerCAmelCase = pipeline.to(_lowercase ) pipeline.set_progress_bar_config(disable=_lowercase ) _lowerCAmelCase = torch.Generator(device="""cpu""" ).manual_seed(0 ) _lowerCAmelCase , _lowerCAmelCase = pipe_prior( _lowercase , image=_lowercase , strength=0.85 , generator=_lowercase , negative_prompt="""""" , ).to_tuple() _lowerCAmelCase = pipeline( image=_lowercase , image_embeds=_lowercase , negative_image_embeds=_lowercase , hint=_lowercase , generator=_lowercase , 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(_lowercase , _lowercase )
5
'''simple docstring''' import inspect import os import unittest from dataclasses import dataclass import torch from accelerate import Accelerator, DistributedDataParallelKwargs, GradScalerKwargs from accelerate.state import AcceleratorState from accelerate.test_utils import execute_subprocess_async, require_cuda, require_multi_gpu from accelerate.utils import KwargsHandler @dataclass class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = 0 lowerCAmelCase_ = False lowerCAmelCase_ = 3.0 class SCREAMING_SNAKE_CASE__ ( unittest.TestCase): def UpperCAmelCase_ ( self )-> int: '''simple docstring''' self.assertDictEqual(MockClass().to_kwargs() , {} ) self.assertDictEqual(MockClass(a=2 ).to_kwargs() , {'a': 2} ) self.assertDictEqual(MockClass(a=2 , b=A_ ).to_kwargs() , {'a': 2, 'b': True} ) self.assertDictEqual(MockClass(a=2 , c=2.25 ).to_kwargs() , {'a': 2, 'c': 2.25} ) @require_cuda def UpperCAmelCase_ ( self )-> Dict: '''simple docstring''' UpperCamelCase = GradScalerKwargs(init_scale=1024 , growth_factor=2 ) AcceleratorState._reset_state() UpperCamelCase = Accelerator(mixed_precision='fp16' , kwargs_handlers=[scaler_handler] ) print(accelerator.use_fpaa ) UpperCamelCase = accelerator.scaler # Check the kwargs have been applied self.assertEqual(scaler._init_scale , 1_024.0 ) self.assertEqual(scaler._growth_factor , 2.0 ) # Check the other values are at the default self.assertEqual(scaler._backoff_factor , 0.5 ) self.assertEqual(scaler._growth_interval , 2000 ) self.assertEqual(scaler._enabled , A_ ) @require_multi_gpu def UpperCAmelCase_ ( self )-> Dict: '''simple docstring''' UpperCamelCase = ['torchrun', F'''--nproc_per_node={torch.cuda.device_count()}''', inspect.getfile(self.__class__ )] execute_subprocess_async(A_ , env=os.environ.copy() ) if __name__ == "__main__": lowerCAmelCase : Tuple = DistributedDataParallelKwargs(bucket_cap_mb=15, find_unused_parameters=True) lowerCAmelCase : List[str] = Accelerator(kwargs_handlers=[ddp_scaler]) lowerCAmelCase : List[Any] = torch.nn.Linear(1_00, 2_00) lowerCAmelCase : int = accelerator.prepare(model) # Check the values changed in kwargs lowerCAmelCase : Dict = '' lowerCAmelCase : Dict = model.bucket_bytes_cap // (10_24 * 10_24) if observed_bucket_cap_map != 15: error_msg += f"Kwargs badly passed, should have `15` but found {observed_bucket_cap_map}.\n" if model.find_unused_parameters is not True: error_msg += f"Kwargs badly passed, should have `True` but found {model.find_unused_parameters}.\n" # Check the values of the defaults if model.dim != 0: error_msg += f"Default value not respected, should have `0` but found {model.dim}.\n" if model.broadcast_buffers is not True: error_msg += f"Default value not respected, should have `True` but found {model.broadcast_buffers}.\n" if model.gradient_as_bucket_view is not False: error_msg += f"Default value not respected, should have `False` but found {model.gradient_as_bucket_view}.\n" # Raise error at the end to make sure we don't stop at the first failure. if len(error_msg) > 0: raise ValueError(error_msg)
3
0
from collections import Counter from pathlib import Path from typing import Optional, Tuple import yaml class UpperCamelCase_ ( yaml.SafeLoader ): def _snake_case ( self :List[str] , __A :List[Any] ) -> Optional[int]: """simple docstring""" SCREAMING_SNAKE_CASE__ = [self.constructed_objects[key_node] for key_node, _ in node.value] SCREAMING_SNAKE_CASE__ = [tuple(__A ) if isinstance(__A , __A ) else key for key in keys] SCREAMING_SNAKE_CASE__ = Counter(__A ) SCREAMING_SNAKE_CASE__ = [key for key in counter if counter[key] > 1] if duplicate_keys: raise TypeError(f'''Got duplicate yaml keys: {duplicate_keys}''' ) def _snake_case ( self :Optional[int] , __A :List[Any] , __A :Union[str, Any]=False ) -> int: """simple docstring""" SCREAMING_SNAKE_CASE__ = super().construct_mapping(__A , deep=__A ) self._check_no_duplicates_on_constructed_node(__A ) return mapping def SCREAMING_SNAKE_CASE__ ( UpperCamelCase__: str ): SCREAMING_SNAKE_CASE__ = list(readme_content.splitlines() ) if full_content and full_content[0] == "---" and "---" in full_content[1:]: SCREAMING_SNAKE_CASE__ = full_content[1:].index("""---""" ) + 1 SCREAMING_SNAKE_CASE__ = """\n""".join(full_content[1:sep_idx] ) return yamlblock, "\n".join(full_content[sep_idx + 1 :] ) return None, "\n".join(UpperCamelCase__ ) class UpperCamelCase_ ( UpperCamelCase__ ): # class attributes lowerCamelCase_ = {"train_eval_index"} # train-eval-index in the YAML metadata @classmethod def _snake_case ( cls :int , __A :Path ) -> "DatasetMetadata": """simple docstring""" with open(__A , encoding="""utf-8""" ) as readme_file: SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = _split_yaml_from_readme(readme_file.read() ) if yaml_string is not None: return cls.from_yaml_string(__A ) else: return cls() def _snake_case ( self :int , __A :Path ) -> str: """simple docstring""" if path.exists(): with open(__A , encoding="""utf-8""" ) as readme_file: SCREAMING_SNAKE_CASE__ = readme_file.read() else: SCREAMING_SNAKE_CASE__ = None SCREAMING_SNAKE_CASE__ = self._to_readme(__A ) with open(__A , """w""" , encoding="""utf-8""" ) as readme_file: readme_file.write(__A ) def _snake_case ( self :int , __A :Optional[str] = None ) -> str: """simple docstring""" if readme_content is not None: SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = _split_yaml_from_readme(__A ) SCREAMING_SNAKE_CASE__ = """---\n""" + self.to_yaml_string() + """---\n""" + content else: SCREAMING_SNAKE_CASE__ = """---\n""" + self.to_yaml_string() + """---\n""" return full_content @classmethod def _snake_case ( cls :Tuple , __A :str ) -> "DatasetMetadata": """simple docstring""" SCREAMING_SNAKE_CASE__ = yaml.load(__A , Loader=_NoDuplicateSafeLoader ) or {} # Convert the YAML keys to DatasetMetadata fields SCREAMING_SNAKE_CASE__ = { (key.replace("""-""" , """_""" ) if key.replace("""-""" , """_""" ) in cls._FIELDS_WITH_DASHES else key): value for key, value in metadata_dict.items() } return cls(**__A ) def _snake_case ( self :Union[str, Any] ) -> str: """simple docstring""" return yaml.safe_dump( { (key.replace("""_""" , """-""" ) if key in self._FIELDS_WITH_DASHES else key): value for key, value in self.items() } , sort_keys=__A , allow_unicode=__A , encoding="""utf-8""" , ).decode("""utf-8""" ) _lowerCamelCase = { 'image-classification': [], 'translation': [], 'image-segmentation': [], 'fill-mask': [], 'automatic-speech-recognition': [], 'token-classification': [], 'sentence-similarity': [], 'audio-classification': [], 'question-answering': [], 'summarization': [], 'zero-shot-classification': [], 'table-to-text': [], 'feature-extraction': [], 'other': [], 'multiple-choice': [], 'text-classification': [], 'text-to-image': [], 'text2text-generation': [], 'zero-shot-image-classification': [], 'tabular-classification': [], 'tabular-regression': [], 'image-to-image': [], 'tabular-to-text': [], 'unconditional-image-generation': [], 'text-retrieval': [], 'text-to-speech': [], 'object-detection': [], 'audio-to-audio': [], 'text-generation': [], 'conversational': [], 'table-question-answering': [], 'visual-question-answering': [], 'image-to-text': [], 'reinforcement-learning': [], 'voice-activity-detection': [], 'time-series-forecasting': [], 'document-question-answering': [], } if __name__ == "__main__": from argparse import ArgumentParser _lowerCamelCase = ArgumentParser(usage='Validate the yaml metadata block of a README.md file.') ap.add_argument('readme_filepath') _lowerCamelCase = ap.parse_args() _lowerCamelCase = Path(args.readme_filepath) _lowerCamelCase = DatasetMetadata.from_readme(readme_filepath) print(dataset_metadata) dataset_metadata.to_readme(readme_filepath)
6
'''simple docstring''' from typing import Callable, List, Optional, Tuple, Union import torch from transformers import CLIPTextModel, CLIPTokenizer from ...configuration_utils import ConfigMixin, register_to_config from ...models import ModelMixin, TransformeraDModel, VQModel from ...schedulers import VQDiffusionScheduler from ...utils import logging from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput lowerCAmelCase : Optional[Any] = logging.get_logger(__name__) # pylint: disable=invalid-name class SCREAMING_SNAKE_CASE__ ( snake_case_ , snake_case_): @register_to_config def __init__( self , A_ , A_ = None , A_ = None )-> Tuple: '''simple docstring''' super().__init__() UpperCamelCase = learnable if self.learnable: assert hidden_size is not None, "learnable=True requires `hidden_size` to be set" assert length is not None, "learnable=True requires `length` to be set" UpperCamelCase = torch.zeros(A_ , A_ ) else: UpperCamelCase = None UpperCamelCase = torch.nn.Parameter(A_ ) class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = 42 lowerCAmelCase_ = 42 lowerCAmelCase_ = 42 lowerCAmelCase_ = 42 lowerCAmelCase_ = 42 lowerCAmelCase_ = 42 def __init__( self , A_ , A_ , A_ , A_ , A_ , A_ , )-> Union[str, Any]: '''simple docstring''' super().__init__() self.register_modules( vqvae=A_ , transformer=A_ , text_encoder=A_ , tokenizer=A_ , scheduler=A_ , learned_classifier_free_sampling_embeddings=A_ , ) def UpperCAmelCase_ ( self , A_ , A_ , A_ )-> Tuple: '''simple docstring''' UpperCamelCase = len(A_ ) if isinstance(A_ , A_ ) else 1 # get prompt text embeddings UpperCamelCase = self.tokenizer( A_ , padding='max_length' , max_length=self.tokenizer.model_max_length , return_tensors='pt' , ) UpperCamelCase = text_inputs.input_ids if text_input_ids.shape[-1] > self.tokenizer.model_max_length: UpperCamelCase = self.tokenizer.batch_decode(text_input_ids[:, self.tokenizer.model_max_length :] ) logger.warning( 'The following part of your input was truncated because CLIP can only handle sequences up to' F''' {self.tokenizer.model_max_length} tokens: {removed_text}''' ) UpperCamelCase = text_input_ids[:, : self.tokenizer.model_max_length] UpperCamelCase = self.text_encoder(text_input_ids.to(self.device ) )[0] # NOTE: This additional step of normalizing the text embeddings is from VQ-Diffusion. # While CLIP does normalize the pooled output of the text transformer when combining # the image and text embeddings, CLIP does not directly normalize the last hidden state. # # CLIP normalizing the pooled output. # https://github.com/huggingface/transformers/blob/d92e22d1f28324f513f3080e5c47c071a3916721/src/transformers/models/clip/modeling_clip.py#L1052-L1053 UpperCamelCase = prompt_embeds / prompt_embeds.norm(dim=-1 , keepdim=A_ ) # duplicate text embeddings for each generation per prompt UpperCamelCase = prompt_embeds.repeat_interleave(A_ , dim=0 ) if do_classifier_free_guidance: if self.learned_classifier_free_sampling_embeddings.learnable: UpperCamelCase = self.learned_classifier_free_sampling_embeddings.embeddings UpperCamelCase = negative_prompt_embeds.unsqueeze(0 ).repeat(A_ , 1 , 1 ) else: UpperCamelCase = [''] * batch_size UpperCamelCase = text_input_ids.shape[-1] UpperCamelCase = self.tokenizer( A_ , padding='max_length' , max_length=A_ , truncation=A_ , return_tensors='pt' , ) UpperCamelCase = self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # See comment for normalizing text embeddings UpperCamelCase = negative_prompt_embeds / negative_prompt_embeds.norm(dim=-1 , keepdim=A_ ) # duplicate unconditional embeddings for each generation per prompt, using mps friendly method UpperCamelCase = negative_prompt_embeds.shape[1] UpperCamelCase = negative_prompt_embeds.repeat(1 , A_ , 1 ) UpperCamelCase = negative_prompt_embeds.view(batch_size * num_images_per_prompt , A_ , -1 ) # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes UpperCamelCase = torch.cat([negative_prompt_embeds, prompt_embeds] ) return prompt_embeds @torch.no_grad() def __call__( self , A_ , A_ = 100 , A_ = 5.0 , A_ = 1.0 , A_ = 1 , A_ = None , A_ = None , A_ = "pil" , A_ = True , A_ = None , A_ = 1 , )-> Union[ImagePipelineOutput, Tuple]: '''simple docstring''' if isinstance(A_ , A_ ): UpperCamelCase = 1 elif isinstance(A_ , A_ ): UpperCamelCase = len(A_ ) else: raise ValueError(F'''`prompt` has to be of type `str` or `list` but is {type(A_ )}''' ) UpperCamelCase = batch_size * num_images_per_prompt UpperCamelCase = guidance_scale > 1.0 UpperCamelCase = self._encode_prompt(A_ , A_ , A_ ) 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_ )}.''' ) # get the initial completely masked latents unless the user supplied it UpperCamelCase = (batch_size, self.transformer.num_latent_pixels) if latents is None: UpperCamelCase = self.transformer.num_vector_embeds - 1 UpperCamelCase = torch.full(A_ , A_ ).to(self.device ) else: if latents.shape != latents_shape: raise ValueError(F'''Unexpected latents shape, got {latents.shape}, expected {latents_shape}''' ) if (latents < 0).any() or (latents >= self.transformer.num_vector_embeds).any(): raise ValueError( 'Unexpected latents value(s). All latents be valid embedding indices i.e. in the range 0,' F''' {self.transformer.num_vector_embeds - 1} (inclusive).''' ) UpperCamelCase = latents.to(self.device ) # set timesteps self.scheduler.set_timesteps(A_ , device=self.device ) UpperCamelCase = self.scheduler.timesteps.to(self.device ) UpperCamelCase = latents for i, t in enumerate(self.progress_bar(A_ ) ): # expand the sample if we are doing classifier free guidance UpperCamelCase = torch.cat([sample] * 2 ) if do_classifier_free_guidance else sample # predict the un-noised image # model_output == `log_p_x_0` UpperCamelCase = self.transformer(A_ , encoder_hidden_states=A_ , timestep=A_ ).sample if do_classifier_free_guidance: UpperCamelCase , UpperCamelCase = model_output.chunk(2 ) UpperCamelCase = model_output_uncond + guidance_scale * (model_output_text - model_output_uncond) model_output -= torch.logsumexp(A_ , dim=1 , keepdim=A_ ) UpperCamelCase = self.truncate(A_ , A_ ) # remove `log(0)`'s (`-inf`s) UpperCamelCase = model_output.clamp(-70 ) # compute the previous noisy sample x_t -> x_t-1 UpperCamelCase = self.scheduler.step(A_ , timestep=A_ , sample=A_ , generator=A_ ).prev_sample # call the callback, if provided if callback is not None and i % callback_steps == 0: callback(A_ , A_ , A_ ) UpperCamelCase = self.vqvae.config.vq_embed_dim UpperCamelCase = (batch_size, self.transformer.height, self.transformer.width, embedding_channels) UpperCamelCase = self.vqvae.quantize.get_codebook_entry(A_ , shape=A_ ) UpperCamelCase = self.vqvae.decode(A_ , force_not_quantize=A_ ).sample UpperCamelCase = (image / 2 + 0.5).clamp(0 , 1 ) UpperCamelCase = image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": UpperCamelCase = self.numpy_to_pil(A_ ) if not return_dict: return (image,) return ImagePipelineOutput(images=A_ ) def UpperCAmelCase_ ( self , A_ , A_ )-> torch.FloatTensor: '''simple docstring''' UpperCamelCase , UpperCamelCase = torch.sort(A_ , 1 , descending=A_ ) UpperCamelCase = torch.exp(A_ ) UpperCamelCase = sorted_p_x_0.cumsum(dim=1 ) < truncation_rate # Ensure that at least the largest probability is not zeroed out UpperCamelCase = torch.full_like(keep_mask[:, 0:1, :] , A_ ) UpperCamelCase = torch.cat((all_true, keep_mask) , dim=1 ) UpperCamelCase = keep_mask[:, :-1, :] UpperCamelCase = keep_mask.gather(1 , indices.argsort(1 ) ) UpperCamelCase = log_p_x_0.clone() UpperCamelCase = -torch.inf # -inf = log(0) return rv
3
0
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available a = { '''configuration_squeezebert''': [ '''SQUEEZEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''SqueezeBertConfig''', '''SqueezeBertOnnxConfig''', ], '''tokenization_squeezebert''': ['''SqueezeBertTokenizer'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a = ['''SqueezeBertTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a = [ '''SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''SqueezeBertForMaskedLM''', '''SqueezeBertForMultipleChoice''', '''SqueezeBertForQuestionAnswering''', '''SqueezeBertForSequenceClassification''', '''SqueezeBertForTokenClassification''', '''SqueezeBertModel''', '''SqueezeBertModule''', '''SqueezeBertPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_squeezebert import ( SQUEEZEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, SqueezeBertConfig, SqueezeBertOnnxConfig, ) from .tokenization_squeezebert import SqueezeBertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_squeezebert_fast import SqueezeBertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_squeezebert import ( SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST, SqueezeBertForMaskedLM, SqueezeBertForMultipleChoice, SqueezeBertForQuestionAnswering, SqueezeBertForSequenceClassification, SqueezeBertForTokenClassification, SqueezeBertModel, SqueezeBertModule, SqueezeBertPreTrainedModel, ) else: import sys a = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
7
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCAmelCase : Union[str, Any] = { 'configuration_git': ['GIT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'GitConfig', 'GitVisionConfig'], 'processing_git': ['GitProcessor'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : List[Any] = [ 'GIT_PRETRAINED_MODEL_ARCHIVE_LIST', 'GitForCausalLM', 'GitModel', 'GitPreTrainedModel', 'GitVisionModel', ] if TYPE_CHECKING: from .configuration_git import GIT_PRETRAINED_CONFIG_ARCHIVE_MAP, GitConfig, GitVisionConfig from .processing_git import GitProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_git import ( GIT_PRETRAINED_MODEL_ARCHIVE_LIST, GitForCausalLM, GitModel, GitPreTrainedModel, GitVisionModel, ) else: import sys lowerCAmelCase : Optional[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
3
0
'''simple docstring''' import unittest from datasets import load_dataset from transformers.pipelines import pipeline from transformers.testing_utils import is_pipeline_test, nested_simplify, require_torch, slow @is_pipeline_test @require_torch class SCREAMING_SNAKE_CASE (unittest.TestCase ): @require_torch def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A : List[str] = pipeline( task='zero-shot-audio-classification' , model='hf-internal-testing/tiny-clap-htsat-unfused') __A : str = load_dataset('ashraq/esc50') __A : str = dataset['train']['audio'][-1]['array'] __A : Union[str, Any] = audio_classifier(_UpperCAmelCase , candidate_labels=['Sound of a dog', 'Sound of vaccum cleaner']) self.assertEqual( nested_simplify(_UpperCAmelCase) , [{'score': 0.501, 'label': 'Sound of a dog'}, {'score': 0.499, 'label': 'Sound of vaccum cleaner'}] , ) @unittest.skip('No models are available in TF') def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' pass @slow @require_torch def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A : List[str] = pipeline( task='zero-shot-audio-classification' , model='laion/clap-htsat-unfused' , ) # This is an audio of a dog __A : Any = load_dataset('ashraq/esc50') __A : Dict = dataset['train']['audio'][-1]['array'] __A : Tuple = audio_classifier(_UpperCAmelCase , candidate_labels=['Sound of a dog', 'Sound of vaccum cleaner']) self.assertEqual( nested_simplify(_UpperCAmelCase) , [ {'score': 0.999, 'label': 'Sound of a dog'}, {'score': 0.001, 'label': 'Sound of vaccum cleaner'}, ] , ) __A : Tuple = audio_classifier([audio] * 5 , candidate_labels=['Sound of a dog', 'Sound of vaccum cleaner']) self.assertEqual( nested_simplify(_UpperCAmelCase) , [ [ {'score': 0.999, 'label': 'Sound of a dog'}, {'score': 0.001, 'label': 'Sound of vaccum cleaner'}, ], ] * 5 , ) __A : List[str] = audio_classifier( [audio] * 5 , candidate_labels=['Sound of a dog', 'Sound of vaccum cleaner'] , batch_size=5) self.assertEqual( nested_simplify(_UpperCAmelCase) , [ [ {'score': 0.999, 'label': 'Sound of a dog'}, {'score': 0.001, 'label': 'Sound of vaccum cleaner'}, ], ] * 5 , ) @unittest.skip('No models are available in TF') def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' pass
8
'''simple docstring''' import uuid from typing import Any, Dict, List, Optional, Union from ..utils import add_end_docstrings, is_tf_available, is_torch_available, logging from .base import PIPELINE_INIT_ARGS, Pipeline if is_tf_available(): import tensorflow as tf if is_torch_available(): import torch lowerCAmelCase : Optional[Any] = logging.get_logger(__name__) class SCREAMING_SNAKE_CASE__ : def __init__( self , A_ = None , A_ = None , A_=None , A_=None )-> Optional[Any]: '''simple docstring''' if not conversation_id: UpperCamelCase = uuid.uuida() if past_user_inputs is None: UpperCamelCase = [] if generated_responses is None: UpperCamelCase = [] UpperCamelCase = conversation_id UpperCamelCase = past_user_inputs UpperCamelCase = generated_responses UpperCamelCase = text def __eq__( self , A_ )-> List[Any]: '''simple docstring''' if not isinstance(A_ , A_ ): return False if self.uuid == other.uuid: return True return ( self.new_user_input == other.new_user_input and self.past_user_inputs == other.past_user_inputs and self.generated_responses == other.generated_responses ) def UpperCAmelCase_ ( self , A_ , A_ = False )-> int: '''simple docstring''' if self.new_user_input: if overwrite: logger.warning( F'''User input added while unprocessed input was existing: "{self.new_user_input}" was overwritten ''' F'''with: "{text}".''' ) UpperCamelCase = text else: logger.warning( F'''User input added while unprocessed input was existing: "{self.new_user_input}" new input ''' F'''ignored: "{text}". Set `overwrite` to True to overwrite unprocessed user input''' ) else: UpperCamelCase = text def UpperCAmelCase_ ( self )-> Any: '''simple docstring''' if self.new_user_input: self.past_user_inputs.append(self.new_user_input ) UpperCamelCase = None def UpperCAmelCase_ ( self , A_ )-> int: '''simple docstring''' self.generated_responses.append(A_ ) def UpperCAmelCase_ ( self )-> List[str]: '''simple docstring''' for user_input, generated_response in zip(self.past_user_inputs , self.generated_responses ): yield True, user_input yield False, generated_response if self.new_user_input: yield True, self.new_user_input def __repr__( self )-> Any: '''simple docstring''' UpperCamelCase = F'''Conversation id: {self.uuid} \n''' for is_user, text in self.iter_texts(): UpperCamelCase = 'user' if is_user else 'bot' output += F'''{name} >> {text} \n''' return output @add_end_docstrings( snake_case_ , R""" min_length_for_response (`int`, *optional*, defaults to 32): The minimum length (in number of tokens) for a response. minimum_tokens (`int`, *optional*, defaults to 10): The minimum length of tokens to leave for a response. """ , ) class SCREAMING_SNAKE_CASE__ ( snake_case_): def __init__( self , *A_ , **A_ )-> Any: '''simple docstring''' super().__init__(*A_ , **A_ ) if self.tokenizer.pad_token_id is None: UpperCamelCase = self.tokenizer.eos_token def UpperCAmelCase_ ( self , A_=None , A_=None , A_=None , **A_ )-> Union[str, Any]: '''simple docstring''' UpperCamelCase = {} UpperCamelCase = {} UpperCamelCase = {} if min_length_for_response is not None: UpperCamelCase = min_length_for_response if minimum_tokens is not None: UpperCamelCase = minimum_tokens if "max_length" in generate_kwargs: UpperCamelCase = generate_kwargs['max_length'] # self.max_length = generate_kwargs.get("max_length", self.model.config.max_length) if clean_up_tokenization_spaces is not None: UpperCamelCase = clean_up_tokenization_spaces if generate_kwargs: forward_params.update(A_ ) return preprocess_params, forward_params, postprocess_params def __call__( self , A_ , A_=0 , **A_ )-> Any: '''simple docstring''' UpperCamelCase = super().__call__(A_ , num_workers=A_ , **A_ ) if isinstance(A_ , A_ ) and len(A_ ) == 1: return outputs[0] return outputs def UpperCAmelCase_ ( self , A_ , A_=32 )-> Dict[str, Any]: '''simple docstring''' if not isinstance(A_ , A_ ): raise ValueError('ConversationalPipeline, expects Conversation as inputs' ) if conversation.new_user_input is None: raise ValueError( F'''Conversation with UUID {type(conversation.uuid )} does not contain new user input to process. ''' 'Add user inputs with the conversation\'s `add_user_input` method' ) if hasattr(self.tokenizer , '_build_conversation_input_ids' ): UpperCamelCase = self.tokenizer._build_conversation_input_ids(A_ ) else: # If the tokenizer cannot handle conversations, we default to only the old version UpperCamelCase = self._legacy_parse_and_tokenize(A_ ) if self.framework == "pt": UpperCamelCase = torch.LongTensor([input_ids] ) elif self.framework == "tf": UpperCamelCase = tf.constant([input_ids] ) return {"input_ids": input_ids, "conversation": conversation} def UpperCAmelCase_ ( self , A_ , A_=10 , **A_ )-> Optional[Any]: '''simple docstring''' UpperCamelCase = generate_kwargs.get('max_length' , self.model.config.max_length ) UpperCamelCase = model_inputs['input_ids'].shape[1] if max_length - minimum_tokens < n: logger.warning(F'''Conversation input is to long ({n}), trimming it to ({max_length} - {minimum_tokens})''' ) UpperCamelCase = max_length - minimum_tokens UpperCamelCase = model_inputs['input_ids'][:, -trim:] if "attention_mask" in model_inputs: UpperCamelCase = model_inputs['attention_mask'][:, -trim:] UpperCamelCase = model_inputs.pop('conversation' ) UpperCamelCase = max_length UpperCamelCase = self.model.generate(**A_ , **A_ ) if self.model.config.is_encoder_decoder: UpperCamelCase = 1 else: UpperCamelCase = n return {"output_ids": output_ids[:, start_position:], "conversation": conversation} def UpperCAmelCase_ ( self , A_ , A_=True )-> Tuple: '''simple docstring''' UpperCamelCase = model_outputs['output_ids'] UpperCamelCase = self.tokenizer.decode( output_ids[0] , skip_special_tokens=A_ , clean_up_tokenization_spaces=A_ , ) UpperCamelCase = model_outputs['conversation'] conversation.mark_processed() conversation.append_response(A_ ) return conversation def UpperCAmelCase_ ( self , A_ )-> Dict: '''simple docstring''' UpperCamelCase = self.tokenizer.eos_token_id UpperCamelCase = [] for is_user, text in conversation.iter_texts(): if eos_token_id is not None: input_ids.extend(self.tokenizer.encode(A_ , add_special_tokens=A_ ) + [eos_token_id] ) else: input_ids.extend(self.tokenizer.encode(A_ , add_special_tokens=A_ ) ) if len(A_ ) > self.tokenizer.model_max_length: UpperCamelCase = input_ids[-self.tokenizer.model_max_length :] return input_ids
3
0
from __future__ import annotations def A ( __UpperCamelCase ) -> bool: A__ = len(__UpperCamelCase ) # We need to create solution object to save path. A__ = [[0 for _ in range(__UpperCamelCase )] for _ in range(__UpperCamelCase )] A__ = run_maze(__UpperCamelCase , 0 , 0 , __UpperCamelCase ) if solved: print('\n'.join(str(__UpperCamelCase ) for row in solutions ) ) else: print('No solution exists!' ) return solved def A ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> bool: A__ = len(__UpperCamelCase ) # Final check point. if i == j == (size - 1): A__ = 1 return True A__ = (not i < 0) and (not j < 0) # Check lower bounds A__ = (i < size) and (j < size) # Check upper bounds if lower_flag and upper_flag: # check for already visited and block points. A__ = (not solutions[i][j]) and (not maze[i][j]) if block_flag: # check visited A__ = 1 # check for directions if ( run_maze(__UpperCamelCase , i + 1 , __UpperCamelCase , __UpperCamelCase ) or run_maze(__UpperCamelCase , __UpperCamelCase , j + 1 , __UpperCamelCase ) or run_maze(__UpperCamelCase , i - 1 , __UpperCamelCase , __UpperCamelCase ) or run_maze(__UpperCamelCase , __UpperCamelCase , j - 1 , __UpperCamelCase ) ): return True A__ = 0 return False return False if __name__ == "__main__": import doctest doctest.testmod()
9
'''simple docstring''' import sys import webbrowser import requests from bsa import BeautifulSoup from fake_useragent import UserAgent if __name__ == "__main__": print('Googling.....') lowerCAmelCase : List[Any] = 'https://www.google.com/search?q=' + ' '.join(sys.argv[1:]) lowerCAmelCase : List[Any] = requests.get(url, headers={'UserAgent': UserAgent().random}) # res.raise_for_status() with open('project1a.html', 'wb') as out_file: # only for knowing the class for data in res.iter_content(1_00_00): out_file.write(data) lowerCAmelCase : Tuple = BeautifulSoup(res.text, 'html.parser') lowerCAmelCase : List[Any] = list(soup.select('.eZt8xd'))[:5] print(len(links)) for link in links: if link.text == "Maps": webbrowser.open(link.get('href')) else: webbrowser.open(f"""https://google.com{link.get('href')}""")
3
0
import gc import unittest import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTextModelWithProjection, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, DDPMScheduler, PriorTransformer, StableUnCLIPPipeline, UNetaDConditionModel, ) from diffusers.pipelines.stable_diffusion.stable_unclip_image_normalizer import StableUnCLIPImageNormalizer from diffusers.utils.testing_utils import enable_full_determinism, load_numpy, require_torch_gpu, slow, torch_device from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import ( PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin, assert_mean_pixel_difference, ) enable_full_determinism() class lowerCAmelCase_ ( __lowercase, __lowercase, __lowercase, unittest.TestCase ): UpperCAmelCase = StableUnCLIPPipeline UpperCAmelCase = TEXT_TO_IMAGE_PARAMS UpperCAmelCase = TEXT_TO_IMAGE_BATCH_PARAMS UpperCAmelCase = TEXT_TO_IMAGE_IMAGE_PARAMS UpperCAmelCase = TEXT_TO_IMAGE_IMAGE_PARAMS # TODO(will) Expected attn_bias.stride(1) == 0 to be true, but got false UpperCAmelCase = False def UpperCamelCase_ ( self : Optional[int] ): _UpperCamelCase = 32 _UpperCamelCase = embedder_hidden_size # prior components torch.manual_seed(0 ) _UpperCamelCase = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) torch.manual_seed(0 ) _UpperCamelCase = CLIPTextModelWithProjection( CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=_A , projection_dim=_A , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , ) ) torch.manual_seed(0 ) _UpperCamelCase = PriorTransformer( num_attention_heads=2 , attention_head_dim=12 , embedding_dim=_A , num_layers=1 , ) torch.manual_seed(0 ) _UpperCamelCase = DDPMScheduler( variance_type='''fixed_small_log''' , prediction_type='''sample''' , num_train_timesteps=1000 , clip_sample=_A , clip_sample_range=5.0 , beta_schedule='''squaredcos_cap_v2''' , ) # regular denoising components torch.manual_seed(0 ) _UpperCamelCase = StableUnCLIPImageNormalizer(embedding_dim=_A ) _UpperCamelCase = DDPMScheduler(beta_schedule='''squaredcos_cap_v2''' ) torch.manual_seed(0 ) _UpperCamelCase = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) torch.manual_seed(0 ) _UpperCamelCase = CLIPTextModel( CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=_A , projection_dim=32 , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , ) ) torch.manual_seed(0 ) _UpperCamelCase = UNetaDConditionModel( sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=('''CrossAttnDownBlock2D''', '''DownBlock2D''') , up_block_types=('''UpBlock2D''', '''CrossAttnUpBlock2D''') , block_out_channels=(32, 64) , attention_head_dim=(2, 4) , class_embed_type='''projection''' , projection_class_embeddings_input_dim=embedder_projection_dim * 2 , cross_attention_dim=_A , layers_per_block=1 , upcast_attention=_A , use_linear_projection=_A , ) torch.manual_seed(0 ) _UpperCamelCase = DDIMScheduler( beta_schedule='''scaled_linear''' , beta_start=0.0_0085 , beta_end=0.012 , prediction_type='''v_prediction''' , set_alpha_to_one=_A , steps_offset=1 , ) torch.manual_seed(0 ) _UpperCamelCase = AutoencoderKL() _UpperCamelCase = { # prior components '''prior_tokenizer''': prior_tokenizer, '''prior_text_encoder''': prior_text_encoder, '''prior''': prior, '''prior_scheduler''': prior_scheduler, # image noising components '''image_normalizer''': image_normalizer, '''image_noising_scheduler''': image_noising_scheduler, # regular denoising components '''tokenizer''': tokenizer, '''text_encoder''': text_encoder, '''unet''': unet, '''scheduler''': scheduler, '''vae''': vae, } return components def UpperCamelCase_ ( self : Dict , _A : Tuple , _A : Dict=0 ): if str(_A ).startswith('''mps''' ): _UpperCamelCase = torch.manual_seed(_A ) else: _UpperCamelCase = torch.Generator(device=_A ).manual_seed(_A ) _UpperCamelCase = { '''prompt''': '''A painting of a squirrel eating a burger''', '''generator''': generator, '''num_inference_steps''': 2, '''prior_num_inference_steps''': 2, '''output_type''': '''numpy''', } return inputs def UpperCamelCase_ ( self : Dict ): _UpperCamelCase = torch_device == '''cpu''' self._test_attention_slicing_forward_pass(test_max_difference=_A ) def UpperCamelCase_ ( self : List[Any] ): _UpperCamelCase = torch_device in ['''cpu''', '''mps'''] self._test_inference_batch_single_identical(test_max_difference=_A ) @slow @require_torch_gpu class lowerCAmelCase_ ( unittest.TestCase ): def UpperCamelCase_ ( self : Optional[Any] ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def UpperCamelCase_ ( self : List[str] ): _UpperCamelCase = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/stable_unclip_2_1_l_anime_turtle_fp16.npy''' ) _UpperCamelCase = StableUnCLIPPipeline.from_pretrained('''fusing/stable-unclip-2-1-l''' , torch_dtype=torch.floataa ) pipe.to(_A ) pipe.set_progress_bar_config(disable=_A ) # stable unclip will oom when integration tests are run on a V100, # so turn on memory savings pipe.enable_attention_slicing() pipe.enable_sequential_cpu_offload() _UpperCamelCase = torch.Generator(device='''cpu''' ).manual_seed(0 ) _UpperCamelCase = pipe('''anime turle''' , generator=_A , output_type='''np''' ) _UpperCamelCase = output.images[0] assert image.shape == (768, 768, 3) assert_mean_pixel_difference(_A , _A ) def UpperCamelCase_ ( self : Optional[Any] ): torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() _UpperCamelCase = StableUnCLIPPipeline.from_pretrained('''fusing/stable-unclip-2-1-l''' , torch_dtype=torch.floataa ) _UpperCamelCase = pipe.to(_A ) pipe.set_progress_bar_config(disable=_A ) pipe.enable_attention_slicing() pipe.enable_sequential_cpu_offload() _UpperCamelCase = pipe( '''anime turtle''' , prior_num_inference_steps=2 , num_inference_steps=2 , output_type='''np''' , ) _UpperCamelCase = torch.cuda.max_memory_allocated() # make sure that less than 7 GB is allocated assert mem_bytes < 7 * 10**9
10
'''simple docstring''' import numpy as np def A_( A : str , A : Optional[Any] , A : Tuple , A : Optional[int] , A : str): UpperCamelCase = int(np.ceil((x_end - xa) / h)) UpperCamelCase = np.zeros((n + 1,)) UpperCamelCase = ya UpperCamelCase = xa for k in range(A): UpperCamelCase = f(A , y[k]) UpperCamelCase = f(x + 0.5 * h , y[k] + 0.5 * h * ka) UpperCamelCase = f(x + 0.5 * h , y[k] + 0.5 * h * ka) UpperCamelCase = f(x + h , y[k] + h * ka) UpperCamelCase = y[k] + (1 / 6) * h * (ka + 2 * ka + 2 * ka + ka) x += h return y if __name__ == "__main__": import doctest doctest.testmod()
3
0
'''simple docstring''' from transformers import DistilBertTokenizer, DistilBertTokenizerFast from transformers.testing_utils import require_tokenizers, slow from ..bert.test_tokenization_bert import BertTokenizationTest @require_tokenizers class __A ( A ): '''simple docstring''' __lowerCamelCase : int = DistilBertTokenizer __lowerCamelCase : Union[str, Any] = DistilBertTokenizerFast __lowerCamelCase : Dict = True @slow def a__ (self ) -> str: """simple docstring""" _a = DistilBertTokenizer.from_pretrained('''distilbert-base-uncased''' ) _a = tokenizer.encode('''sequence builders''' , add_special_tokens=A ) _a = tokenizer.encode('''multi-sequence build''' , add_special_tokens=A ) _a = tokenizer.build_inputs_with_special_tokens(A ) _a = tokenizer.build_inputs_with_special_tokens(A , A ) assert encoded_sentence == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] assert encoded_pair == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] + text_a + [ tokenizer.sep_token_id ]
11
'''simple docstring''' from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Features, Value from .base import TaskTemplate @dataclass(frozen=snake_case_) class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = field(default="""language-modeling""" , metadata={"""include_in_asdict_even_if_is_default""": True}) lowerCAmelCase_ = Features({"""text""": Value("""string""")}) lowerCAmelCase_ = Features({}) lowerCAmelCase_ = "text" @property def UpperCAmelCase_ ( self )-> Dict[str, str]: '''simple docstring''' return {self.text_column: "text"}
3
0
from __future__ import annotations import inspect import unittest from transformers import ViTConfig from transformers.testing_utils import require_tf, require_vision, slow from transformers.utils import cached_property, is_tf_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFViTForImageClassification, TFViTModel if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class _snake_case : def __init__( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_=13 , SCREAMING_SNAKE_CASE_=30 , SCREAMING_SNAKE_CASE_=2 , SCREAMING_SNAKE_CASE_=3 , SCREAMING_SNAKE_CASE_=True , SCREAMING_SNAKE_CASE_=True , SCREAMING_SNAKE_CASE_=32 , SCREAMING_SNAKE_CASE_=2 , SCREAMING_SNAKE_CASE_=4 , SCREAMING_SNAKE_CASE_=37 , SCREAMING_SNAKE_CASE_="gelu" , SCREAMING_SNAKE_CASE_=0.1 , SCREAMING_SNAKE_CASE_=0.1 , SCREAMING_SNAKE_CASE_=10 , SCREAMING_SNAKE_CASE_=0.0_2 , SCREAMING_SNAKE_CASE_=3 , SCREAMING_SNAKE_CASE_=None , ): '''simple docstring''' lowercase__ : int = parent lowercase__ : Tuple = batch_size lowercase__ : List[str] = image_size lowercase__ : Optional[int] = patch_size lowercase__ : List[str] = num_channels lowercase__ : Dict = is_training lowercase__ : Dict = use_labels lowercase__ : List[Any] = hidden_size lowercase__ : Union[str, Any] = num_hidden_layers lowercase__ : Optional[int] = num_attention_heads lowercase__ : Dict = intermediate_size lowercase__ : Optional[Any] = hidden_act lowercase__ : List[str] = hidden_dropout_prob lowercase__ : List[Any] = attention_probs_dropout_prob lowercase__ : int = type_sequence_label_size lowercase__ : Any = initializer_range lowercase__ : List[str] = scope # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) lowercase__ : Union[str, Any] = (image_size // patch_size) ** 2 lowercase__ : Union[str, Any] = num_patches + 1 def lowercase__ ( self): '''simple docstring''' lowercase__ : Dict = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]) lowercase__ : List[str] = None if self.use_labels: lowercase__ : Tuple = ids_tensor([self.batch_size] , self.type_sequence_label_size) lowercase__ : Optional[int] = self.get_config() return config, pixel_values, labels def lowercase__ ( self): '''simple docstring''' return ViTConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=SCREAMING_SNAKE_CASE_ , initializer_range=self.initializer_range , ) def lowercase__ ( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_): '''simple docstring''' lowercase__ : Any = TFViTModel(config=SCREAMING_SNAKE_CASE_) lowercase__ : Optional[int] = model(SCREAMING_SNAKE_CASE_ , training=SCREAMING_SNAKE_CASE_) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size)) # Test with an image with different size than the one specified in config. lowercase__ : Optional[Any] = self.image_size // 2 lowercase__ : Tuple = pixel_values[:, :, :image_size, :image_size] lowercase__ : str = model(SCREAMING_SNAKE_CASE_ , interpolate_pos_encoding=SCREAMING_SNAKE_CASE_ , training=SCREAMING_SNAKE_CASE_) lowercase__ : str = (image_size // self.patch_size) ** 2 + 1 self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, seq_length, self.hidden_size)) def lowercase__ ( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_): '''simple docstring''' lowercase__ : str = self.type_sequence_label_size lowercase__ : Any = TFViTForImageClassification(SCREAMING_SNAKE_CASE_) lowercase__ : Dict = model(SCREAMING_SNAKE_CASE_ , labels=SCREAMING_SNAKE_CASE_ , training=SCREAMING_SNAKE_CASE_) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size)) # Test with an image with different size than the one specified in config. lowercase__ : str = self.image_size // 2 lowercase__ : Dict = pixel_values[:, :, :image_size, :image_size] lowercase__ : Union[str, Any] = model(SCREAMING_SNAKE_CASE_ , interpolate_pos_encoding=SCREAMING_SNAKE_CASE_ , training=SCREAMING_SNAKE_CASE_) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size)) # test greyscale images lowercase__ : Any = 1 lowercase__ : Any = TFViTForImageClassification(SCREAMING_SNAKE_CASE_) lowercase__ : int = floats_tensor([self.batch_size, 1, self.image_size, self.image_size]) lowercase__ : Optional[int] = model(SCREAMING_SNAKE_CASE_) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size)) def lowercase__ ( self): '''simple docstring''' lowercase__ : Any = self.prepare_config_and_inputs() lowercase__ , lowercase__ , lowercase__ : int = config_and_inputs lowercase__ : List[Any] = {"""pixel_values""": pixel_values} return config, inputs_dict @require_tf class _snake_case ( UpperCAmelCase_ , UpperCAmelCase_ , unittest.TestCase ): __lowerCAmelCase : Dict = (TFViTModel, TFViTForImageClassification) if is_tf_available() else () __lowerCAmelCase : Dict = ( {'feature-extraction': TFViTModel, 'image-classification': TFViTForImageClassification} if is_tf_available() else {} ) __lowerCAmelCase : Tuple = False __lowerCAmelCase : List[str] = False __lowerCAmelCase : Union[str, Any] = False def lowercase__ ( self): '''simple docstring''' lowercase__ : List[Any] = TFViTModelTester(self) lowercase__ : Dict = ConfigTester(self , config_class=SCREAMING_SNAKE_CASE_ , has_text_modality=SCREAMING_SNAKE_CASE_ , hidden_size=37) def lowercase__ ( self): '''simple docstring''' self.config_tester.run_common_tests() @unittest.skip(reason="""ViT does not use inputs_embeds""") def lowercase__ ( self): '''simple docstring''' pass @unittest.skip(reason="""ViT does not use inputs_embeds""") def lowercase__ ( self): '''simple docstring''' pass def lowercase__ ( self): '''simple docstring''' lowercase__ , lowercase__ : Any = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowercase__ : Tuple = model_class(SCREAMING_SNAKE_CASE_) self.assertIsInstance(model.get_input_embeddings() , (tf.keras.layers.Layer)) lowercase__ : Dict = model.get_output_embeddings() self.assertTrue(x is None or isinstance(SCREAMING_SNAKE_CASE_ , tf.keras.layers.Layer)) def lowercase__ ( self): '''simple docstring''' lowercase__ , lowercase__ : Any = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowercase__ : str = model_class(SCREAMING_SNAKE_CASE_) lowercase__ : int = inspect.signature(model.call) # signature.parameters is an OrderedDict => so arg_names order is deterministic lowercase__ : str = [*signature.parameters.keys()] lowercase__ : int = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , SCREAMING_SNAKE_CASE_) def lowercase__ ( self): '''simple docstring''' lowercase__ : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_) def lowercase__ ( self): '''simple docstring''' lowercase__ : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*SCREAMING_SNAKE_CASE_) @slow def lowercase__ ( self): '''simple docstring''' lowercase__ : List[str] = TFViTModel.from_pretrained("""google/vit-base-patch16-224""") self.assertIsNotNone(SCREAMING_SNAKE_CASE_) def UpperCamelCase ( ) -> Union[str, Any]: '''simple docstring''' lowercase__ : str = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) return image @require_tf @require_vision class _snake_case ( unittest.TestCase ): @cached_property def lowercase__ ( self): '''simple docstring''' return ViTImageProcessor.from_pretrained("""google/vit-base-patch16-224""") if is_vision_available() else None @slow def lowercase__ ( self): '''simple docstring''' lowercase__ : Any = TFViTForImageClassification.from_pretrained("""google/vit-base-patch16-224""") lowercase__ : Optional[int] = self.default_image_processor lowercase__ : List[str] = prepare_img() lowercase__ : Any = image_processor(images=SCREAMING_SNAKE_CASE_ , return_tensors="""tf""") # forward pass lowercase__ : Any = model(**SCREAMING_SNAKE_CASE_) # verify the logits lowercase__ : Dict = tf.TensorShape((1, 10_00)) self.assertEqual(outputs.logits.shape , SCREAMING_SNAKE_CASE_) lowercase__ : Dict = tf.constant([-0.2_7_4_4, 0.8_2_1_5, -0.0_8_3_6]) tf.debugging.assert_near(outputs.logits[0, :3] , SCREAMING_SNAKE_CASE_ , atol=1E-4)
12
'''simple docstring''' from __future__ import annotations lowerCAmelCase : Union[str, Any] = [-10, -5, 0, 5, 5.1, 11, 13, 21, 3, 4, -21, -10, -5, -1, 0] lowerCAmelCase : List[str] = [-5, 0, 5, 5.1, 11, 13, 21, -1, 4, -1, -10, -5, -1, 0, -1] def A_( A : list[float]): UpperCamelCase = [] UpperCamelCase = len(A) for i in range(A): UpperCamelCase = -1 for j in range(i + 1 , A): if arr[i] < arr[j]: UpperCamelCase = arr[j] break result.append(A) return result def A_( A : list[float]): UpperCamelCase = [] for i, outer in enumerate(A): UpperCamelCase = -1 for inner in arr[i + 1 :]: if outer < inner: UpperCamelCase = inner break result.append(A) return result def A_( A : list[float]): UpperCamelCase = len(A) UpperCamelCase = [] UpperCamelCase = [-1] * arr_size for index in reversed(range(A)): if stack: while stack[-1] <= arr[index]: stack.pop() if not stack: break if stack: UpperCamelCase = stack[-1] stack.append(arr[index]) return result if __name__ == "__main__": from doctest import testmod from timeit import timeit testmod() print(next_greatest_element_slow(arr)) print(next_greatest_element_fast(arr)) print(next_greatest_element(arr)) lowerCAmelCase : Optional[Any] = ( 'from __main__ import arr, next_greatest_element_slow, ' 'next_greatest_element_fast, next_greatest_element' ) print( 'next_greatest_element_slow():', timeit('next_greatest_element_slow(arr)', setup=setup), ) print( 'next_greatest_element_fast():', timeit('next_greatest_element_fast(arr)', setup=setup), ) print( ' next_greatest_element():', timeit('next_greatest_element(arr)', setup=setup), )
3
0
'''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 ( MobileViTConfig, MobileViTForImageClassification, MobileViTForSemanticSegmentation, MobileViTImageProcessor, ) from transformers.utils import logging logging.set_verbosity_info() A__ : List[Any] = logging.get_logger(__name__) def UpperCAmelCase__ ( UpperCAmelCase_ : List[Any] ) -> Union[str, Any]: __lowerCamelCase : Optional[int] = MobileViTConfig() # size of the architecture if "mobilevit_s" in mobilevit_name: __lowerCamelCase : Any = [1_44, 1_92, 2_40] __lowerCamelCase : str = [16, 32, 64, 96, 1_28, 1_60, 6_40] elif "mobilevit_xs" in mobilevit_name: __lowerCamelCase : int = [96, 1_20, 1_44] __lowerCamelCase : Tuple = [16, 32, 48, 64, 80, 96, 3_84] elif "mobilevit_xxs" in mobilevit_name: __lowerCamelCase : Union[str, Any] = [64, 80, 96] __lowerCamelCase : List[str] = [16, 16, 24, 48, 64, 80, 3_20] __lowerCamelCase : Union[str, Any] = 0.05 __lowerCamelCase : List[str] = 2.0 if mobilevit_name.startswith('deeplabv3_' ): __lowerCamelCase : Union[str, Any] = 5_12 __lowerCamelCase : Any = 16 __lowerCamelCase : Union[str, Any] = 21 __lowerCamelCase : Tuple = 'pascal-voc-id2label.json' else: __lowerCamelCase : Dict = 10_00 __lowerCamelCase : Union[str, Any] = 'imagenet-1k-id2label.json' __lowerCamelCase : List[Any] = 'huggingface/label-files' __lowerCamelCase : Union[str, Any] = json.load(open(hf_hub_download(UpperCAmelCase_ , UpperCAmelCase_ , repo_type='dataset' ) , 'r' ) ) __lowerCamelCase : List[str] = {int(UpperCAmelCase_ ): v for k, v in idalabel.items()} __lowerCamelCase : Union[str, Any] = idalabel __lowerCamelCase : Any = {v: k for k, v in idalabel.items()} return config def UpperCAmelCase__ ( UpperCAmelCase_ : Any , UpperCAmelCase_ : str=False ) -> Optional[int]: for i in range(1 , 6 ): if F'layer_{i}.' in name: __lowerCamelCase : Optional[Any] = name.replace(F'layer_{i}.' , F'encoder.layer.{i - 1}.' ) if "conv_1." in name: __lowerCamelCase : int = name.replace('conv_1.' , 'conv_stem.' ) if ".block." in name: __lowerCamelCase : int = name.replace('.block.' , '.' ) if "exp_1x1" in name: __lowerCamelCase : Optional[Any] = name.replace('exp_1x1' , 'expand_1x1' ) if "red_1x1" in name: __lowerCamelCase : Tuple = name.replace('red_1x1' , 'reduce_1x1' ) if ".local_rep.conv_3x3." in name: __lowerCamelCase : List[str] = name.replace('.local_rep.conv_3x3.' , '.conv_kxk.' ) if ".local_rep.conv_1x1." in name: __lowerCamelCase : Union[str, Any] = name.replace('.local_rep.conv_1x1.' , '.conv_1x1.' ) if ".norm." in name: __lowerCamelCase : Optional[int] = name.replace('.norm.' , '.normalization.' ) if ".conv." in name: __lowerCamelCase : str = name.replace('.conv.' , '.convolution.' ) if ".conv_proj." in name: __lowerCamelCase : Optional[int] = name.replace('.conv_proj.' , '.conv_projection.' ) for i in range(0 , 2 ): for j in range(0 , 4 ): if F'.{i}.{j}.' in name: __lowerCamelCase : Tuple = name.replace(F'.{i}.{j}.' , F'.{i}.layer.{j}.' ) for i in range(2 , 6 ): for j in range(0 , 4 ): if F'.{i}.{j}.' in name: __lowerCamelCase : Tuple = name.replace(F'.{i}.{j}.' , F'.{i}.' ) if "expand_1x1" in name: __lowerCamelCase : Union[str, Any] = name.replace('expand_1x1' , 'downsampling_layer.expand_1x1' ) if "conv_3x3" in name: __lowerCamelCase : Optional[Any] = name.replace('conv_3x3' , 'downsampling_layer.conv_3x3' ) if "reduce_1x1" in name: __lowerCamelCase : Optional[Any] = name.replace('reduce_1x1' , 'downsampling_layer.reduce_1x1' ) for i in range(2 , 5 ): if F'.global_rep.{i}.weight' in name: __lowerCamelCase : List[Any] = name.replace(F'.global_rep.{i}.weight' , '.layernorm.weight' ) if F'.global_rep.{i}.bias' in name: __lowerCamelCase : Tuple = name.replace(F'.global_rep.{i}.bias' , '.layernorm.bias' ) if ".global_rep." in name: __lowerCamelCase : int = name.replace('.global_rep.' , '.transformer.' ) if ".pre_norm_mha.0." in name: __lowerCamelCase : Optional[int] = name.replace('.pre_norm_mha.0.' , '.layernorm_before.' ) if ".pre_norm_mha.1.out_proj." in name: __lowerCamelCase : Union[str, Any] = name.replace('.pre_norm_mha.1.out_proj.' , '.attention.output.dense.' ) if ".pre_norm_ffn.0." in name: __lowerCamelCase : Tuple = name.replace('.pre_norm_ffn.0.' , '.layernorm_after.' ) if ".pre_norm_ffn.1." in name: __lowerCamelCase : str = name.replace('.pre_norm_ffn.1.' , '.intermediate.dense.' ) if ".pre_norm_ffn.4." in name: __lowerCamelCase : int = name.replace('.pre_norm_ffn.4.' , '.output.dense.' ) if ".transformer." in name: __lowerCamelCase : Optional[Any] = name.replace('.transformer.' , '.transformer.layer.' ) if ".aspp_layer." in name: __lowerCamelCase : int = name.replace('.aspp_layer.' , '.' ) if ".aspp_pool." in name: __lowerCamelCase : Any = name.replace('.aspp_pool.' , '.' ) if "seg_head." in name: __lowerCamelCase : str = name.replace('seg_head.' , 'segmentation_head.' ) if "segmentation_head.classifier.classifier." in name: __lowerCamelCase : Any = name.replace('segmentation_head.classifier.classifier.' , 'segmentation_head.classifier.' ) if "classifier.fc." in name: __lowerCamelCase : Optional[Any] = name.replace('classifier.fc.' , 'classifier.' ) elif (not base_model) and ("segmentation_head." not in name): __lowerCamelCase : Tuple = 'mobilevit.' + name return name def UpperCAmelCase__ ( UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Any=False ) -> Union[str, Any]: if base_model: __lowerCamelCase : Optional[int] = '' else: __lowerCamelCase : Union[str, Any] = 'mobilevit.' for key in orig_state_dict.copy().keys(): __lowerCamelCase : List[Any] = orig_state_dict.pop(UpperCAmelCase_ ) if key[:8] == "encoder.": __lowerCamelCase : Any = key[8:] if "qkv" in key: __lowerCamelCase : Any = key.split('.' ) __lowerCamelCase : Optional[int] = int(key_split[0][6:] ) - 1 __lowerCamelCase : Optional[Any] = int(key_split[3] ) __lowerCamelCase : Union[str, Any] = model.get_submodule(F'{model_prefix}encoder.layer.{layer_num}' ) __lowerCamelCase : int = layer.transformer.layer[transformer_num].attention.attention.all_head_size __lowerCamelCase : Union[str, Any] = ( F'{model_prefix}encoder.layer.{layer_num}.transformer.layer.{transformer_num}.attention.attention.' ) if "weight" in key: __lowerCamelCase : str = val[:dim, :] __lowerCamelCase : Tuple = val[dim : dim * 2, :] __lowerCamelCase : str = val[-dim:, :] else: __lowerCamelCase : str = val[:dim] __lowerCamelCase : Dict = val[dim : dim * 2] __lowerCamelCase : List[Any] = val[-dim:] else: __lowerCamelCase : List[Any] = val return orig_state_dict def UpperCAmelCase__ ( ) -> Union[str, Any]: __lowerCamelCase : Tuple = 'http://images.cocodataset.org/val2017/000000039769.jpg' __lowerCamelCase : Any = Image.open(requests.get(UpperCAmelCase_ , stream=UpperCAmelCase_ ).raw ) return im @torch.no_grad() def UpperCAmelCase__ ( UpperCAmelCase_ : Dict , UpperCAmelCase_ : Dict , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Tuple=False ) -> List[Any]: __lowerCamelCase : Tuple = get_mobilevit_config(UpperCAmelCase_ ) # load original state_dict __lowerCamelCase : Dict = torch.load(UpperCAmelCase_ , map_location='cpu' ) # load 🤗 model if mobilevit_name.startswith('deeplabv3_' ): __lowerCamelCase : int = MobileViTForSemanticSegmentation(UpperCAmelCase_ ).eval() else: __lowerCamelCase : Any = MobileViTForImageClassification(UpperCAmelCase_ ).eval() __lowerCamelCase : Any = convert_state_dict(UpperCAmelCase_ , UpperCAmelCase_ ) model.load_state_dict(UpperCAmelCase_ ) # Check outputs on an image, prepared by MobileViTImageProcessor __lowerCamelCase : Union[str, Any] = MobileViTImageProcessor(crop_size=config.image_size , size=config.image_size + 32 ) __lowerCamelCase : List[str] = image_processor(images=prepare_img() , return_tensors='pt' ) __lowerCamelCase : List[Any] = model(**UpperCAmelCase_ ) __lowerCamelCase : List[Any] = outputs.logits if mobilevit_name.startswith('deeplabv3_' ): assert logits.shape == (1, 21, 32, 32) if mobilevit_name == "deeplabv3_mobilevit_s": __lowerCamelCase : List[str] = torch.tensor( [ [[6.2_065, 6.1_292, 6.2_070], [6.1_079, 6.1_254, 6.1_747], [6.0_042, 6.1_071, 6.1_034]], [[-6.9_253, -6.8_653, -7.0_398], [-7.3_218, -7.3_983, -7.3_670], [-7.1_961, -7.2_482, -7.1_569]], [[-4.4_723, -4.4_348, -4.3_769], [-5.3_629, -5.4_632, -5.4_598], [-5.1_587, -5.3_402, -5.5_059]], ] ) elif mobilevit_name == "deeplabv3_mobilevit_xs": __lowerCamelCase : str = torch.tensor( [ [[5.4_449, 5.5_733, 5.6_314], [5.1_815, 5.3_930, 5.5_963], [5.1_656, 5.4_333, 5.4_853]], [[-9.4_423, -9.7_766, -9.6_714], [-9.1_581, -9.5_720, -9.5_519], [-9.1_006, -9.6_458, -9.5_703]], [[-7.7_721, -7.3_716, -7.1_583], [-8.4_599, -8.0_624, -7.7_944], [-8.4_172, -7.8_366, -7.5_025]], ] ) elif mobilevit_name == "deeplabv3_mobilevit_xxs": __lowerCamelCase : int = torch.tensor( [ [[6.9_811, 6.9_743, 7.3_123], [7.1_777, 7.1_931, 7.3_938], [7.5_633, 7.8_050, 7.8_901]], [[-10.5_536, -10.2_332, -10.2_924], [-10.2_336, -9.8_624, -9.5_964], [-10.8_840, -10.8_158, -10.6_659]], [[-3.4_938, -3.0_631, -2.8_620], [-3.4_205, -2.8_135, -2.6_875], [-3.4_179, -2.7_945, -2.8_750]], ] ) else: raise ValueError(F'Unknown mobilevit_name: {mobilevit_name}' ) assert torch.allclose(logits[0, :3, :3, :3] , UpperCAmelCase_ , atol=1e-4 ) else: assert logits.shape == (1, 10_00) if mobilevit_name == "mobilevit_s": __lowerCamelCase : Any = torch.tensor([-0.9_866, 0.2_392, -1.1_241] ) elif mobilevit_name == "mobilevit_xs": __lowerCamelCase : Union[str, Any] = torch.tensor([-2.4_761, -0.9_399, -1.9_587] ) elif mobilevit_name == "mobilevit_xxs": __lowerCamelCase : List[Any] = torch.tensor([-1.9_364, -1.2_327, -0.4_653] ) else: raise ValueError(F'Unknown mobilevit_name: {mobilevit_name}' ) assert torch.allclose(logits[0, :3] , UpperCAmelCase_ , atol=1e-4 ) Path(UpperCAmelCase_ ).mkdir(exist_ok=UpperCAmelCase_ ) print(F'Saving model {mobilevit_name} to {pytorch_dump_folder_path}' ) model.save_pretrained(UpperCAmelCase_ ) print(F'Saving image processor to {pytorch_dump_folder_path}' ) image_processor.save_pretrained(UpperCAmelCase_ ) if push_to_hub: __lowerCamelCase : Optional[Any] = { 'mobilevit_s': 'mobilevit-small', 'mobilevit_xs': 'mobilevit-x-small', 'mobilevit_xxs': 'mobilevit-xx-small', 'deeplabv3_mobilevit_s': 'deeplabv3-mobilevit-small', 'deeplabv3_mobilevit_xs': 'deeplabv3-mobilevit-x-small', 'deeplabv3_mobilevit_xxs': 'deeplabv3-mobilevit-xx-small', } print('Pushing to the hub...' ) __lowerCamelCase : List[str] = model_mapping[mobilevit_name] image_processor.push_to_hub(UpperCAmelCase_ , organization='apple' ) model.push_to_hub(UpperCAmelCase_ , organization='apple' ) if __name__ == "__main__": A__ : Any = argparse.ArgumentParser() # Required parameters parser.add_argument( """--mobilevit_name""", default="""mobilevit_s""", type=str, help=( """Name of the MobileViT model you'd like to convert. Should be one of 'mobilevit_s', 'mobilevit_xs',""" """ 'mobilevit_xxs', 'deeplabv3_mobilevit_s', 'deeplabv3_mobilevit_xs', 'deeplabv3_mobilevit_xxs'.""" ), ) parser.add_argument( """--checkpoint_path""", required=True, type=str, help="""Path to the original state dict (.pt file).""" ) parser.add_argument( """--pytorch_dump_folder_path""", required=True, type=str, help="""Path to the output PyTorch model directory.""" ) parser.add_argument( """--push_to_hub""", action="""store_true""", help="""Whether or not to push the converted model to the 🤗 hub.""" ) A__ : Tuple = parser.parse_args() convert_movilevit_checkpoint( args.mobilevit_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub )
13
'''simple docstring''' from string import ascii_lowercase, ascii_uppercase def A_( A : str): if not sentence: return "" UpperCamelCase = dict(zip(A , A)) return lower_to_upper.get(sentence[0] , sentence[0]) + sentence[1:] if __name__ == "__main__": from doctest import testmod testmod()
3
0
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging a__ = logging.get_logger(__name__) a__ = { '''microsoft/beit-base-patch16-224-pt22k''': ( '''https://huggingface.co/microsoft/beit-base-patch16-224-pt22k/resolve/main/config.json''' ), # See all BEiT models at https://huggingface.co/models?filter=beit } class UpperCAmelCase_ ( __lowercase ): """simple docstring""" UpperCAmelCase__ : str = "beit" def __init__( self , _a=8_1_9_2 , _a=7_6_8 , _a=1_2 , _a=1_2 , _a=3_0_7_2 , _a="gelu" , _a=0.0 , _a=0.0 , _a=0.02 , _a=1e-1_2 , _a=2_2_4 , _a=1_6 , _a=3 , _a=False , _a=False , _a=False , _a=False , _a=0.1 , _a=0.1 , _a=True , _a=[3, 5, 7, 1_1] , _a=[1, 2, 3, 6] , _a=True , _a=0.4 , _a=2_5_6 , _a=1 , _a=False , _a=2_5_5 , **_a , ) -> Tuple: super().__init__(**_a ) _a : Dict = vocab_size _a : Tuple = hidden_size _a : List[Any] = num_hidden_layers _a : int = num_attention_heads _a : List[Any] = intermediate_size _a : Any = hidden_act _a : Optional[int] = hidden_dropout_prob _a : Union[str, Any] = attention_probs_dropout_prob _a : Any = initializer_range _a : List[str] = layer_norm_eps _a : Dict = image_size _a : int = patch_size _a : Tuple = num_channels _a : Any = use_mask_token _a : int = use_absolute_position_embeddings _a : Optional[int] = use_relative_position_bias _a : List[Any] = use_shared_relative_position_bias _a : str = layer_scale_init_value _a : List[str] = drop_path_rate _a : List[Any] = use_mean_pooling # decode head attributes (semantic segmentation) _a : str = out_indices _a : Tuple = pool_scales # auxiliary head attributes (semantic segmentation) _a : Any = use_auxiliary_head _a : List[Any] = auxiliary_loss_weight _a : Optional[int] = auxiliary_channels _a : Tuple = auxiliary_num_convs _a : List[Any] = auxiliary_concat_input _a : Tuple = semantic_loss_ignore_index class UpperCAmelCase_ ( __lowercase ): """simple docstring""" UpperCAmelCase__ : str = version.parse("1.11" ) @property def __lowercase ( self ) -> Mapping[str, Mapping[int, str]]: return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ] ) @property def __lowercase ( self ) -> float: return 1e-4
14
'''simple docstring''' from typing import Optional, Tuple, Union import tensorflow as tf from ...activations_tf import ACTaFN from ...file_utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward from ...modeling_tf_outputs import ( TFBaseModelOutputWithNoAttention, TFBaseModelOutputWithPoolingAndNoAttention, TFSequenceClassifierOutput, ) from ...modeling_tf_utils import TFPreTrainedModel, TFSequenceClassificationLoss, keras_serializable, unpack_inputs from ...tf_utils import shape_list from ...utils import logging from .configuration_regnet import RegNetConfig lowerCAmelCase : Dict = logging.get_logger(__name__) # General docstring lowerCAmelCase : str = 'RegNetConfig' # Base docstring lowerCAmelCase : str = 'facebook/regnet-y-040' lowerCAmelCase : Dict = [1, 10_88, 7, 7] # Image classification docstring lowerCAmelCase : Dict = 'facebook/regnet-y-040' lowerCAmelCase : int = 'tabby, tabby cat' lowerCAmelCase : int = [ 'facebook/regnet-y-040', # See all regnet models at https://huggingface.co/models?filter=regnet ] class SCREAMING_SNAKE_CASE__ ( tf.keras.layers.Layer): def __init__( self , A_ , A_ = 3 , A_ = 1 , A_ = 1 , A_ = "relu" , **A_ , )-> str: '''simple docstring''' super().__init__(**A_ ) # The padding and conv has been verified in # https://colab.research.google.com/gist/sayakpaul/854bc10eeaf21c9ee2119e0b9f3841a7/scratchpad.ipynb UpperCamelCase = tf.keras.layers.ZeroPaddingaD(padding=kernel_size // 2 ) UpperCamelCase = tf.keras.layers.ConvaD( filters=A_ , kernel_size=A_ , strides=A_ , padding='VALID' , groups=A_ , use_bias=A_ , name='convolution' , ) UpperCamelCase = tf.keras.layers.BatchNormalization(epsilon=1e-5 , momentum=0.9 , name='normalization' ) UpperCamelCase = ACTaFN[activation] if activation is not None else tf.identity def UpperCAmelCase_ ( self , A_ )-> Any: '''simple docstring''' UpperCamelCase = self.convolution(self.padding(A_ ) ) UpperCamelCase = self.normalization(A_ ) UpperCamelCase = self.activation(A_ ) return hidden_state class SCREAMING_SNAKE_CASE__ ( tf.keras.layers.Layer): def __init__( self , A_ , **A_ )-> Optional[Any]: '''simple docstring''' super().__init__(**A_ ) UpperCamelCase = config.num_channels UpperCamelCase = TFRegNetConvLayer( out_channels=config.embedding_size , kernel_size=3 , stride=2 , activation=config.hidden_act , name='embedder' , ) def UpperCAmelCase_ ( self , A_ )-> List[Any]: '''simple docstring''' UpperCamelCase = shape_list(A_ )[1] if tf.executing_eagerly() and num_channels != self.num_channels: raise ValueError( 'Make sure that the channel dimension of the pixel values match with the one set in the configuration.' ) # When running on CPU, `tf.keras.layers.Conv2D` doesn't support `NCHW` format. # So change the input format from `NCHW` to `NHWC`. # shape = (batch_size, in_height, in_width, in_channels=num_channels) UpperCamelCase = tf.transpose(A_ , perm=(0, 2, 3, 1) ) UpperCamelCase = self.embedder(A_ ) return hidden_state class SCREAMING_SNAKE_CASE__ ( tf.keras.layers.Layer): def __init__( self , A_ , A_ = 2 , **A_ )-> List[Any]: '''simple docstring''' super().__init__(**A_ ) UpperCamelCase = tf.keras.layers.ConvaD( filters=A_ , kernel_size=1 , strides=A_ , use_bias=A_ , name='convolution' ) UpperCamelCase = tf.keras.layers.BatchNormalization(epsilon=1e-5 , momentum=0.9 , name='normalization' ) def UpperCAmelCase_ ( self , A_ , A_ = False )-> tf.Tensor: '''simple docstring''' return self.normalization(self.convolution(A_ ) , training=A_ ) class SCREAMING_SNAKE_CASE__ ( tf.keras.layers.Layer): def __init__( self , A_ , A_ , **A_ )-> Optional[Any]: '''simple docstring''' super().__init__(**A_ ) UpperCamelCase = tf.keras.layers.GlobalAveragePoolingaD(keepdims=A_ , name='pooler' ) UpperCamelCase = [ tf.keras.layers.ConvaD(filters=A_ , kernel_size=1 , activation='relu' , name='attention.0' ), tf.keras.layers.ConvaD(filters=A_ , kernel_size=1 , activation='sigmoid' , name='attention.2' ), ] def UpperCAmelCase_ ( self , A_ )-> Optional[int]: '''simple docstring''' UpperCamelCase = self.pooler(A_ ) for layer_module in self.attention: UpperCamelCase = layer_module(A_ ) UpperCamelCase = hidden_state * pooled return hidden_state class SCREAMING_SNAKE_CASE__ ( tf.keras.layers.Layer): def __init__( self , A_ , A_ , A_ , A_ = 1 , **A_ )-> Dict: '''simple docstring''' super().__init__(**A_ ) UpperCamelCase = in_channels != out_channels or stride != 1 UpperCamelCase = max(1 , out_channels // config.groups_width ) UpperCamelCase = ( TFRegNetShortCut(A_ , stride=A_ , name='shortcut' ) if should_apply_shortcut else tf.keras.layers.Activation('linear' , name='shortcut' ) ) # `self.layers` instead of `self.layer` because that is a reserved argument. UpperCamelCase = [ TFRegNetConvLayer(A_ , kernel_size=1 , activation=config.hidden_act , name='layer.0' ), TFRegNetConvLayer( A_ , stride=A_ , groups=A_ , activation=config.hidden_act , name='layer.1' ), TFRegNetConvLayer(A_ , kernel_size=1 , activation=A_ , name='layer.2' ), ] UpperCamelCase = ACTaFN[config.hidden_act] def UpperCAmelCase_ ( self , A_ )-> Tuple: '''simple docstring''' UpperCamelCase = hidden_state for layer_module in self.layers: UpperCamelCase = layer_module(A_ ) UpperCamelCase = self.shortcut(A_ ) hidden_state += residual UpperCamelCase = self.activation(A_ ) return hidden_state class SCREAMING_SNAKE_CASE__ ( tf.keras.layers.Layer): def __init__( self , A_ , A_ , A_ , A_ = 1 , **A_ )-> Any: '''simple docstring''' super().__init__(**A_ ) UpperCamelCase = in_channels != out_channels or stride != 1 UpperCamelCase = max(1 , out_channels // config.groups_width ) UpperCamelCase = ( TFRegNetShortCut(A_ , stride=A_ , name='shortcut' ) if should_apply_shortcut else tf.keras.layers.Activation('linear' , name='shortcut' ) ) UpperCamelCase = [ TFRegNetConvLayer(A_ , kernel_size=1 , activation=config.hidden_act , name='layer.0' ), TFRegNetConvLayer( A_ , stride=A_ , groups=A_ , activation=config.hidden_act , name='layer.1' ), TFRegNetSELayer(A_ , reduced_channels=int(round(in_channels / 4 ) ) , name='layer.2' ), TFRegNetConvLayer(A_ , kernel_size=1 , activation=A_ , name='layer.3' ), ] UpperCamelCase = ACTaFN[config.hidden_act] def UpperCAmelCase_ ( self , A_ )-> List[Any]: '''simple docstring''' UpperCamelCase = hidden_state for layer_module in self.layers: UpperCamelCase = layer_module(A_ ) UpperCamelCase = self.shortcut(A_ ) hidden_state += residual UpperCamelCase = self.activation(A_ ) return hidden_state class SCREAMING_SNAKE_CASE__ ( tf.keras.layers.Layer): def __init__( self , A_ , A_ , A_ , A_ = 2 , A_ = 2 , **A_ )-> Dict: '''simple docstring''' super().__init__(**A_ ) UpperCamelCase = TFRegNetXLayer if config.layer_type == 'x' else TFRegNetYLayer UpperCamelCase = [ # downsampling is done in the first layer with stride of 2 layer(A_ , A_ , A_ , stride=A_ , name='layers.0' ), *[layer(A_ , A_ , A_ , name=F'''layers.{i+1}''' ) for i in range(depth - 1 )], ] def UpperCAmelCase_ ( self , A_ )-> List[Any]: '''simple docstring''' for layer_module in self.layers: UpperCamelCase = layer_module(A_ ) return hidden_state class SCREAMING_SNAKE_CASE__ ( tf.keras.layers.Layer): def __init__( self , A_ , **A_ )-> str: '''simple docstring''' super().__init__(**A_ ) UpperCamelCase = [] # based on `downsample_in_first_stage`, the first layer of the first stage may or may not downsample the input self.stages.append( TFRegNetStage( A_ , config.embedding_size , config.hidden_sizes[0] , stride=2 if config.downsample_in_first_stage else 1 , depth=config.depths[0] , name='stages.0' , ) ) UpperCamelCase = zip(config.hidden_sizes , config.hidden_sizes[1:] ) for i, ((in_channels, out_channels), depth) in enumerate(zip(A_ , config.depths[1:] ) ): self.stages.append(TFRegNetStage(A_ , A_ , A_ , depth=A_ , name=F'''stages.{i+1}''' ) ) def UpperCAmelCase_ ( self , A_ , A_ = False , A_ = True )-> TFBaseModelOutputWithNoAttention: '''simple docstring''' UpperCamelCase = () if output_hidden_states else None for stage_module in self.stages: if output_hidden_states: UpperCamelCase = hidden_states + (hidden_state,) UpperCamelCase = stage_module(A_ ) if output_hidden_states: UpperCamelCase = hidden_states + (hidden_state,) if not return_dict: return tuple(v for v in [hidden_state, hidden_states] if v is not None ) return TFBaseModelOutputWithNoAttention(last_hidden_state=A_ , hidden_states=A_ ) @keras_serializable class SCREAMING_SNAKE_CASE__ ( tf.keras.layers.Layer): lowerCAmelCase_ = RegNetConfig def __init__( self , A_ , **A_ )-> Union[str, Any]: '''simple docstring''' super().__init__(**A_ ) UpperCamelCase = config UpperCamelCase = TFRegNetEmbeddings(A_ , name='embedder' ) UpperCamelCase = TFRegNetEncoder(A_ , name='encoder' ) UpperCamelCase = tf.keras.layers.GlobalAveragePoolingaD(keepdims=A_ , name='pooler' ) @unpack_inputs def UpperCAmelCase_ ( self , A_ , A_ = None , A_ = None , A_ = False , )-> TFBaseModelOutputWithPoolingAndNoAttention: '''simple docstring''' UpperCamelCase = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) UpperCamelCase = return_dict if return_dict is not None else self.config.use_return_dict UpperCamelCase = self.embedder(A_ , training=A_ ) UpperCamelCase = self.encoder( A_ , output_hidden_states=A_ , return_dict=A_ , training=A_ ) UpperCamelCase = encoder_outputs[0] UpperCamelCase = self.pooler(A_ ) # Change to NCHW output format have uniformity in the modules UpperCamelCase = tf.transpose(A_ , perm=(0, 3, 1, 2) ) UpperCamelCase = tf.transpose(A_ , perm=(0, 3, 1, 2) ) # Change the other hidden state outputs to NCHW as well if output_hidden_states: UpperCamelCase = tuple([tf.transpose(A_ , perm=(0, 3, 1, 2) ) for h in encoder_outputs[1]] ) if not return_dict: return (last_hidden_state, pooled_output) + encoder_outputs[1:] return TFBaseModelOutputWithPoolingAndNoAttention( last_hidden_state=A_ , pooler_output=A_ , hidden_states=hidden_states if output_hidden_states else encoder_outputs.hidden_states , ) class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = RegNetConfig lowerCAmelCase_ = """regnet""" lowerCAmelCase_ = """pixel_values""" @property def UpperCAmelCase_ ( self )-> List[str]: '''simple docstring''' return {"pixel_values": tf.TensorSpec(shape=(None, self.config.num_channels, 224, 224) , dtype=tf.floataa )} lowerCAmelCase : str = r'\n Parameters:\n This model is a Tensorflow\n [tf.keras.layers.Layer](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Layer) sub-class. Use it as a\n regular Tensorflow Module and refer to the Tensorflow documentation for all matter related to general usage and\n behavior.\n config ([`RegNetConfig`]): Model configuration class with all the parameters of the model.\n Initializing with a config file does not load the weights associated with the model, only the\n configuration. Check out the [`~TFPreTrainedModel.from_pretrained`] method to load the model weights.\n' lowerCAmelCase : List[str] = r'\n Args:\n pixel_values (`tf.Tensor` of shape `(batch_size, num_channels, height, width)`):\n Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n [`ConveNextImageProcessor.__call__`] for details.\n output_hidden_states (`bool`, *optional*):\n Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for\n more detail.\n return_dict (`bool`, *optional*):\n Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n' @add_start_docstrings( """The bare RegNet model outputting raw features without any specific head on top.""" , snake_case_ , ) class SCREAMING_SNAKE_CASE__ ( snake_case_): def __init__( self , A_ , *A_ , **A_ )-> List[Any]: '''simple docstring''' super().__init__(A_ , *A_ , **A_ ) UpperCamelCase = TFRegNetMainLayer(A_ , name='regnet' ) @unpack_inputs @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 UpperCAmelCase_ ( self , A_ , A_ = None , A_ = None , A_=False , )-> Union[TFBaseModelOutputWithPoolingAndNoAttention, Tuple[tf.Tensor]]: '''simple docstring''' UpperCamelCase = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) UpperCamelCase = return_dict if return_dict is not None else self.config.use_return_dict UpperCamelCase = self.regnet( pixel_values=A_ , output_hidden_states=A_ , return_dict=A_ , training=A_ , ) if not return_dict: return (outputs[0],) + outputs[1:] return TFBaseModelOutputWithPoolingAndNoAttention( last_hidden_state=outputs.last_hidden_state , pooler_output=outputs.pooler_output , hidden_states=outputs.hidden_states , ) @add_start_docstrings( """ RegNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for ImageNet. """ , snake_case_ , ) class SCREAMING_SNAKE_CASE__ ( snake_case_ , snake_case_): def __init__( self , A_ , *A_ , **A_ )-> str: '''simple docstring''' super().__init__(A_ , *A_ , **A_ ) UpperCamelCase = config.num_labels UpperCamelCase = TFRegNetMainLayer(A_ , name='regnet' ) # classification head UpperCamelCase = [ tf.keras.layers.Flatten(), tf.keras.layers.Dense(config.num_labels , name='classifier.1' ) if config.num_labels > 0 else tf.identity, ] @unpack_inputs @add_start_docstrings_to_model_forward(A_ ) @add_code_sample_docstrings( checkpoint=_IMAGE_CLASS_CHECKPOINT , output_type=A_ , config_class=_CONFIG_FOR_DOC , expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT , ) def UpperCAmelCase_ ( self , A_ = None , A_ = None , A_ = None , A_ = None , A_=False , )-> Union[TFSequenceClassifierOutput, Tuple[tf.Tensor]]: '''simple docstring''' UpperCamelCase = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) UpperCamelCase = return_dict if return_dict is not None else self.config.use_return_dict UpperCamelCase = self.regnet( A_ , output_hidden_states=A_ , return_dict=A_ , training=A_ ) UpperCamelCase = outputs.pooler_output if return_dict else outputs[1] UpperCamelCase = self.classifier[0](A_ ) UpperCamelCase = self.classifier[1](A_ ) UpperCamelCase = None if labels is None else self.hf_compute_loss(labels=A_ , logits=A_ ) if not return_dict: UpperCamelCase = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return TFSequenceClassifierOutput(loss=A_ , logits=A_ , hidden_states=outputs.hidden_states )
3
0
import copy from collections import OrderedDict from typing import Dict, Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ..auto import CONFIG_MAPPING A : Union[str, Any] = logging.get_logger(__name__) A : str = { 'facebook/detr-resnet-50': 'https://huggingface.co/facebook/detr-resnet-50/resolve/main/config.json', # See all DETR models at https://huggingface.co/models?filter=detr } class A ( UpperCAmelCase__ ): '''simple docstring''' A__ = '''detr''' A__ = ['''past_key_values'''] A__ = { '''hidden_size''': '''d_model''', '''num_attention_heads''': '''encoder_attention_heads''', } def __init__(self : Any , _UpperCAmelCase : Tuple=True , _UpperCAmelCase : Optional[int]=None , _UpperCAmelCase : str=3 , _UpperCAmelCase : str=100 , _UpperCAmelCase : Tuple=6 , _UpperCAmelCase : Optional[Any]=2048 , _UpperCAmelCase : Optional[int]=8 , _UpperCAmelCase : int=6 , _UpperCAmelCase : Optional[int]=2048 , _UpperCAmelCase : str=8 , _UpperCAmelCase : Tuple=0.0 , _UpperCAmelCase : List[str]=0.0 , _UpperCAmelCase : Optional[Any]=True , _UpperCAmelCase : str="relu" , _UpperCAmelCase : int=256 , _UpperCAmelCase : Union[str, Any]=0.1 , _UpperCAmelCase : List[Any]=0.0 , _UpperCAmelCase : List[Any]=0.0 , _UpperCAmelCase : List[str]=0.02 , _UpperCAmelCase : Union[str, Any]=1.0 , _UpperCAmelCase : Optional[Any]=False , _UpperCAmelCase : Optional[Any]="sine" , _UpperCAmelCase : Union[str, Any]="resnet50" , _UpperCAmelCase : str=True , _UpperCAmelCase : Optional[int]=False , _UpperCAmelCase : int=1 , _UpperCAmelCase : Dict=5 , _UpperCAmelCase : Optional[int]=2 , _UpperCAmelCase : Tuple=1 , _UpperCAmelCase : Optional[Any]=1 , _UpperCAmelCase : int=5 , _UpperCAmelCase : Dict=2 , _UpperCAmelCase : List[str]=0.1 , **_UpperCAmelCase : str , ) -> str: """simple docstring""" if backbone_config is not None and use_timm_backbone: raise ValueError("""You can't specify both `backbone_config` and `use_timm_backbone`.""" ) if not use_timm_backbone: if backbone_config is None: logger.info("""`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.""" ) lowercase__ = CONFIG_MAPPING["""resnet"""](out_features=["""stage4"""] ) elif isinstance(_UpperCAmelCase , _UpperCAmelCase ): lowercase__ = backbone_config.get("""model_type""" ) lowercase__ = CONFIG_MAPPING[backbone_model_type] lowercase__ = config_class.from_dict(_UpperCAmelCase ) # set timm attributes to None lowercase__ , lowercase__ , lowercase__ = None, None, None lowercase__ = use_timm_backbone lowercase__ = backbone_config lowercase__ = num_channels lowercase__ = num_queries lowercase__ = d_model lowercase__ = encoder_ffn_dim lowercase__ = encoder_layers lowercase__ = encoder_attention_heads lowercase__ = decoder_ffn_dim lowercase__ = decoder_layers lowercase__ = decoder_attention_heads lowercase__ = dropout lowercase__ = attention_dropout lowercase__ = activation_dropout lowercase__ = activation_function lowercase__ = init_std lowercase__ = init_xavier_std lowercase__ = encoder_layerdrop lowercase__ = decoder_layerdrop lowercase__ = encoder_layers lowercase__ = auxiliary_loss lowercase__ = position_embedding_type lowercase__ = backbone lowercase__ = use_pretrained_backbone lowercase__ = dilation # Hungarian matcher lowercase__ = class_cost lowercase__ = bbox_cost lowercase__ = giou_cost # Loss coefficients lowercase__ = mask_loss_coefficient lowercase__ = dice_loss_coefficient lowercase__ = bbox_loss_coefficient lowercase__ = giou_loss_coefficient lowercase__ = eos_coefficient super().__init__(is_encoder_decoder=_UpperCAmelCase , **_UpperCAmelCase ) @property def lowerCamelCase__ (self : List[Any] ) -> int: """simple docstring""" return self.encoder_attention_heads @property def lowerCamelCase__ (self : int ) -> int: """simple docstring""" return self.d_model @classmethod def lowerCamelCase__ (cls : Tuple , _UpperCAmelCase : PretrainedConfig , **_UpperCAmelCase : List[str] ) -> Optional[Any]: """simple docstring""" return cls(backbone_config=_UpperCAmelCase , **_UpperCAmelCase ) def lowerCamelCase__ (self : Any ) -> Dict[str, any]: """simple docstring""" lowercase__ = copy.deepcopy(self.__dict__ ) if output["backbone_config"] is not None: lowercase__ = self.backbone_config.to_dict() lowercase__ = self.__class__.model_type return output class A ( UpperCAmelCase__ ): '''simple docstring''' A__ = version.parse('''1.11''' ) @property def lowerCamelCase__ (self : Tuple ) -> Mapping[str, Mapping[int, str]]: """simple docstring""" return OrderedDict( [ ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}), ("""pixel_mask""", {0: """batch"""}), ] ) @property def lowerCamelCase__ (self : Optional[int] ) -> float: """simple docstring""" return 1E-5 @property def lowerCamelCase__ (self : Any ) -> int: """simple docstring""" return 12
15
'''simple docstring''' from collections import OrderedDict from typing import Any, Mapping, Optional, Union from ...configuration_utils import PretrainedConfig from ...feature_extraction_utils import FeatureExtractionMixin from ...onnx import OnnxConfig from ...onnx.utils import compute_effective_axis_dimension from ...tokenization_utils_base import PreTrainedTokenizerBase from ...utils import TensorType, logging lowerCAmelCase : Any = logging.get_logger(__name__) lowerCAmelCase : Optional[int] = { 'deepmind/language-perceiver': 'https://huggingface.co/deepmind/language-perceiver/resolve/main/config.json', # See all Perceiver models at https://huggingface.co/models?filter=perceiver } class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = """perceiver""" def __init__( self , A_=256 , A_=1280 , A_=768 , A_=1 , A_=26 , A_=8 , A_=8 , A_=None , A_=None , A_="kv" , A_=1 , A_=1 , A_="gelu" , A_=0.1 , A_=0.02 , A_=1e-12 , A_=True , A_=262 , A_=2048 , A_=56 , A_=[368, 496] , A_=16 , A_=1920 , A_=16 , A_=[1, 16, 224, 224] , **A_ , )-> str: '''simple docstring''' super().__init__(**A_ ) UpperCamelCase = num_latents UpperCamelCase = d_latents UpperCamelCase = d_model UpperCamelCase = num_blocks UpperCamelCase = num_self_attends_per_block UpperCamelCase = num_self_attention_heads UpperCamelCase = num_cross_attention_heads UpperCamelCase = qk_channels UpperCamelCase = v_channels UpperCamelCase = cross_attention_shape_for_attention UpperCamelCase = self_attention_widening_factor UpperCamelCase = cross_attention_widening_factor UpperCamelCase = hidden_act UpperCamelCase = attention_probs_dropout_prob UpperCamelCase = initializer_range UpperCamelCase = layer_norm_eps UpperCamelCase = use_query_residual # masked language modeling attributes UpperCamelCase = vocab_size UpperCamelCase = max_position_embeddings # image classification attributes UpperCamelCase = image_size # flow attributes UpperCamelCase = train_size # multimodal autoencoding attributes UpperCamelCase = num_frames UpperCamelCase = audio_samples_per_frame UpperCamelCase = samples_per_patch UpperCamelCase = output_shape class SCREAMING_SNAKE_CASE__ ( snake_case_): @property def UpperCAmelCase_ ( self )-> Mapping[str, Mapping[int, str]]: '''simple docstring''' if self.task == "multiple-choice": UpperCamelCase = {0: 'batch', 1: 'choice', 2: 'sequence'} else: UpperCamelCase = {0: 'batch', 1: 'sequence'} return OrderedDict( [ ('inputs', dynamic_axis), ('attention_mask', dynamic_axis), ] ) @property def UpperCAmelCase_ ( self )-> float: '''simple docstring''' return 1e-4 def UpperCAmelCase_ ( self , A_ , A_ = -1 , A_ = -1 , A_ = -1 , A_ = False , A_ = None , A_ = 3 , A_ = 40 , A_ = 40 , )-> Mapping[str, Any]: '''simple docstring''' if isinstance(A_ , A_ ): # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX UpperCamelCase = compute_effective_axis_dimension( A_ , fixed_dimension=OnnxConfig.default_fixed_batch , num_token_to_add=0 ) # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX UpperCamelCase = preprocessor.num_special_tokens_to_add(A_ ) UpperCamelCase = compute_effective_axis_dimension( A_ , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=A_ ) # Generate dummy inputs according to compute batch and sequence UpperCamelCase = [' '.join(['a'] ) * seq_length] * batch_size UpperCamelCase = dict(preprocessor(A_ , return_tensors=A_ ) ) UpperCamelCase = inputs.pop('input_ids' ) return inputs elif isinstance(A_ , A_ ) and preprocessor.model_input_names[0] == "pixel_values": # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX UpperCamelCase = compute_effective_axis_dimension(A_ , fixed_dimension=OnnxConfig.default_fixed_batch ) UpperCamelCase = self._generate_dummy_images(A_ , A_ , A_ , A_ ) UpperCamelCase = dict(preprocessor(images=A_ , return_tensors=A_ ) ) UpperCamelCase = inputs.pop('pixel_values' ) return inputs else: raise ValueError( 'Unable to generate dummy inputs for the model. Please provide a tokenizer or a preprocessor.' )
3
0
from __future__ import annotations def __a ( A__ : list , A__ : int , A__ : int , A__ : int ): SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = input_list[low:mid], input_list[mid : high + 1] while left and right: result.append((left if left[0] <= right[0] else right).pop(0 ) ) SCREAMING_SNAKE_CASE = result + left + right return input_list def __a ( A__ : list ): if len(A__ ) <= 1: return input_list SCREAMING_SNAKE_CASE = list(A__ ) # iteration for two-way merging SCREAMING_SNAKE_CASE = 2 while p <= len(A__ ): # getting low, high and middle value for merge-sort of single list for i in range(0 , len(A__ ) , A__ ): SCREAMING_SNAKE_CASE = i SCREAMING_SNAKE_CASE = i + p - 1 SCREAMING_SNAKE_CASE = (low + high + 1) // 2 SCREAMING_SNAKE_CASE = merge(A__ , A__ , A__ , A__ ) # final merge of last two parts if p * 2 >= len(A__ ): SCREAMING_SNAKE_CASE = i SCREAMING_SNAKE_CASE = merge(A__ , 0 , A__ , len(A__ ) - 1 ) break p *= 2 return input_list if __name__ == "__main__": __A : Any = input('Enter numbers separated by a comma:\n').strip() if user_input == "": __A : Union[str, Any] = [] else: __A : Optional[Any] = [int(item.strip()) for item in user_input.split(',')] print(iter_merge_sort(unsorted))
16
'''simple docstring''' from ....configuration_utils import PretrainedConfig from ....utils import logging lowerCAmelCase : Optional[Any] = logging.get_logger(__name__) lowerCAmelCase : Dict = { 'speechbrain/m-ctc-t-large': 'https://huggingface.co/speechbrain/m-ctc-t-large/resolve/main/config.json', # See all M-CTC-T models at https://huggingface.co/models?filter=mctct } class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = """mctct""" def __init__( self , A_=8065 , A_=1536 , A_=36 , A_=6144 , A_=4 , A_=384 , A_=920 , A_=1e-5 , A_=0.3 , A_="relu" , A_=0.02 , A_=0.3 , A_=0.3 , A_=1 , A_=0 , A_=2 , A_=1 , A_=0.3 , A_=1 , A_=(7,) , A_=(3,) , A_=80 , A_=1 , A_=None , A_="sum" , A_=False , **A_ , )-> str: '''simple docstring''' super().__init__(**A_ , pad_token_id=A_ , bos_token_id=A_ , eos_token_id=A_ ) UpperCamelCase = vocab_size UpperCamelCase = hidden_size UpperCamelCase = num_hidden_layers UpperCamelCase = intermediate_size UpperCamelCase = num_attention_heads UpperCamelCase = attention_head_dim UpperCamelCase = max_position_embeddings UpperCamelCase = layer_norm_eps UpperCamelCase = layerdrop UpperCamelCase = hidden_act UpperCamelCase = initializer_range UpperCamelCase = hidden_dropout_prob UpperCamelCase = attention_probs_dropout_prob UpperCamelCase = pad_token_id UpperCamelCase = bos_token_id UpperCamelCase = eos_token_id UpperCamelCase = conv_glu_dim UpperCamelCase = conv_dropout UpperCamelCase = num_conv_layers UpperCamelCase = input_feat_per_channel UpperCamelCase = input_channels UpperCamelCase = conv_channels UpperCamelCase = ctc_loss_reduction UpperCamelCase = ctc_zero_infinity # prevents config testing fail with exporting to json UpperCamelCase = list(A_ ) UpperCamelCase = list(A_ ) if len(self.conv_kernel ) != self.num_conv_layers: raise ValueError( 'Configuration for convolutional module is incorrect. ' 'It is required that `len(config.conv_kernel)` == `config.num_conv_layers` ' F'''but is `len(config.conv_kernel) = {len(self.conv_kernel )}`, ''' F'''`config.num_conv_layers = {self.num_conv_layers}`.''' )
3
0
import faiss # noqa: F401 # Here to have a nice missing dependency error message early on import numpy # noqa: F401 # Here to have a nice missing dependency error message early on import requests # noqa: F401 # Here to have a nice missing dependency error message early on import sklearn # noqa: F401 # Here to have a nice missing dependency error message early on import tqdm # noqa: F401 # Here to have a nice missing dependency error message early on from mauve import compute_mauve # From: mauve-text import datasets UpperCAmelCase_ : Union[str, Any] = '''\ @inproceedings{pillutla-etal:mauve:neurips2021, title={MAUVE: Measuring the Gap Between Neural Text and Human Text using Divergence Frontiers}, author={Pillutla, Krishna and Swayamdipta, Swabha and Zellers, Rowan and Thickstun, John and Welleck, Sean and Choi, Yejin and Harchaoui, Zaid}, booktitle = {NeurIPS}, year = {2021} } ''' UpperCAmelCase_ : Union[str, Any] = '''\ MAUVE is a library built on PyTorch and HuggingFace Transformers to measure the gap between neural text and human text with the eponymous MAUVE measure. MAUVE summarizes both Type I and Type II errors measured softly using Kullback–Leibler (KL) divergences. For details, see the MAUVE paper: https://arxiv.org/abs/2102.01454 (Neurips, 2021). This metrics is a wrapper around the official implementation of MAUVE: https://github.com/krishnap25/mauve ''' UpperCAmelCase_ : int = ''' Calculates MAUVE scores between two lists of generated text and reference text. Args: predictions: list of generated text to score. Each predictions should be a string with tokens separated by spaces. references: list of reference for each prediction. Each reference should be a string with tokens separated by spaces. Optional Args: num_buckets: the size of the histogram to quantize P and Q. Options: \'auto\' (default) or an integer pca_max_data: the number data points to use for PCA dimensionality reduction prior to clustering. If -1, use all the data. Default -1 kmeans_explained_var: amount of variance of the data to keep in dimensionality reduction by PCA. Default 0.9 kmeans_num_redo: number of times to redo k-means clustering (the best objective is kept). Default 5 kmeans_max_iter: maximum number of k-means iterations. Default 500 featurize_model_name: name of the model from which features are obtained. Default \'gpt2-large\' Use one of [\'gpt2\', \'gpt2-medium\', \'gpt2-large\', \'gpt2-xl\']. device_id: Device for featurization. Supply a GPU id (e.g. 0 or 3) to use GPU. If no GPU with this id is found, use CPU max_text_length: maximum number of tokens to consider. Default 1024 divergence_curve_discretization_size: Number of points to consider on the divergence curve. Default 25 mauve_scaling_factor: "c" from the paper. Default 5. verbose: If True (default), print running time updates seed: random seed to initialize k-means cluster assignments. Returns: mauve: MAUVE score, a number between 0 and 1. Larger values indicate that P and Q are closer, frontier_integral: Frontier Integral, a number between 0 and 1. Smaller values indicate that P and Q are closer, divergence_curve: a numpy.ndarray of shape (m, 2); plot it with matplotlib to view the divergence curve, p_hist: a discrete distribution, which is a quantized version of the text distribution p_text, q_hist: same as above, but with q_text. Examples: >>> # faiss segfaults in doctest for some reason, so the .compute call is not tested with doctest >>> import datasets >>> mauve = datasets.load_metric(\'mauve\') >>> predictions = ["hello there", "general kenobi"] >>> references = ["hello there", "general kenobi"] >>> out = mauve.compute(predictions=predictions, references=references) # doctest: +SKIP >>> print(out.mauve) # doctest: +SKIP 1.0 ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class lowerCamelCase_ ( datasets.Metric ): def lowerCAmelCase_ ( self : List[str] ): return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , homepage="""https://github.com/krishnap25/mauve""" , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": datasets.Value("""string""" , id="""sequence""" ), """references""": datasets.Value("""string""" , id="""sequence""" ), } ) , codebase_urls=["""https://github.com/krishnap25/mauve"""] , reference_urls=[ """https://arxiv.org/abs/2102.01454""", """https://github.com/krishnap25/mauve""", ] , ) def lowerCAmelCase_ ( self : Optional[int] , __A : List[str] , __A : int , __A : Any=None , __A : Optional[Any]=None , __A : Optional[int]=None , __A : List[Any]=None , __A : List[str]="auto" , __A : List[str]=-1 , __A : int=0.9 , __A : List[Any]=5 , __A : Optional[Any]=500 , __A : List[str]="gpt2-large" , __A : Union[str, Any]=-1 , __A : List[str]=1024 , __A : Optional[Any]=25 , __A : str=5 , __A : List[Any]=True , __A : int=25 , ): __A : Any = compute_mauve( p_text=__A , q_text=__A , p_features=__A , q_features=__A , p_tokens=__A , q_tokens=__A , num_buckets=__A , pca_max_data=__A , kmeans_explained_var=__A , kmeans_num_redo=__A , kmeans_max_iter=__A , featurize_model_name=__A , device_id=__A , max_text_length=__A , divergence_curve_discretization_size=__A , mauve_scaling_factor=__A , verbose=__A , seed=__A , ) return out
17
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) if is_sentencepiece_available(): from ..ta.tokenization_ta import TaTokenizer else: from ...utils.dummy_sentencepiece_objects import TaTokenizer lowerCAmelCase : Tuple = TaTokenizer if is_tokenizers_available(): from ..ta.tokenization_ta_fast import TaTokenizerFast else: from ...utils.dummy_tokenizers_objects import TaTokenizerFast lowerCAmelCase : Optional[int] = TaTokenizerFast lowerCAmelCase : Any = {'configuration_mt5': ['MT5Config', 'MT5OnnxConfig']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : Optional[int] = [ 'MT5EncoderModel', 'MT5ForConditionalGeneration', 'MT5ForQuestionAnswering', 'MT5Model', 'MT5PreTrainedModel', 'MT5Stack', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : Dict = ['TFMT5EncoderModel', 'TFMT5ForConditionalGeneration', 'TFMT5Model'] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : Optional[Any] = ['FlaxMT5EncoderModel', 'FlaxMT5ForConditionalGeneration', 'FlaxMT5Model'] if TYPE_CHECKING: from .configuration_mta import MTaConfig, MTaOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mta import ( MTaEncoderModel, MTaForConditionalGeneration, MTaForQuestionAnswering, MTaModel, MTaPreTrainedModel, MTaStack, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_mta import TFMTaEncoderModel, TFMTaForConditionalGeneration, TFMTaModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_mta import FlaxMTaEncoderModel, FlaxMTaForConditionalGeneration, FlaxMTaModel else: import sys lowerCAmelCase : Tuple = _LazyModule( __name__, globals()['__file__'], _import_structure, extra_objects={'MT5Tokenizer': MTaTokenizer, 'MT5TokenizerFast': MTaTokenizerFast}, module_spec=__spec__, )
3
0
'''simple docstring''' import tempfile import torch from diffusers import ( DEISMultistepScheduler, DPMSolverMultistepScheduler, DPMSolverSinglestepScheduler, UniPCMultistepScheduler, ) from .test_schedulers import SchedulerCommonTest class lowerCAmelCase_ ( __magic_name__ ): __lowerCamelCase : List[str] = (DPMSolverSinglestepScheduler,) __lowerCamelCase : int = (("num_inference_steps", 25),) def _snake_case ( self , **_lowerCAmelCase ) -> Any: _lowerCAmelCase = { "num_train_timesteps": 1000, "beta_start": 0.0001, "beta_end": 0.02, "beta_schedule": "linear", "solver_order": 2, "prediction_type": "epsilon", "thresholding": False, "sample_max_value": 1.0, "algorithm_type": "dpmsolver++", "solver_type": "midpoint", "lambda_min_clipped": -float("inf" ), "variance_type": None, } config.update(**_lowerCAmelCase ) return config def _snake_case ( self , _lowerCAmelCase=0 , **_lowerCAmelCase ) -> List[Any]: _lowerCAmelCase = dict(self.forward_default_kwargs ) _lowerCAmelCase = kwargs.pop("num_inference_steps" , _lowerCAmelCase ) _lowerCAmelCase = self.dummy_sample _lowerCAmelCase = 0.1 * sample _lowerCAmelCase = [residual + 0.2, residual + 0.15, residual + 0.10] for scheduler_class in self.scheduler_classes: _lowerCAmelCase = self.get_scheduler_config(**_lowerCAmelCase ) _lowerCAmelCase = scheduler_class(**_lowerCAmelCase ) scheduler.set_timesteps(_lowerCAmelCase ) # copy over dummy past residuals _lowerCAmelCase = dummy_past_residuals[: scheduler.config.solver_order] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(_lowerCAmelCase ) _lowerCAmelCase = scheduler_class.from_pretrained(_lowerCAmelCase ) new_scheduler.set_timesteps(_lowerCAmelCase ) # copy over dummy past residuals _lowerCAmelCase = dummy_past_residuals[: new_scheduler.config.solver_order] _lowerCAmelCase , _lowerCAmelCase = sample, sample for t in range(_lowerCAmelCase , time_step + scheduler.config.solver_order + 1 ): _lowerCAmelCase = scheduler.step(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , **_lowerCAmelCase ).prev_sample _lowerCAmelCase = new_scheduler.step(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , **_lowerCAmelCase ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" def _snake_case ( self ) -> int: pass def _snake_case ( self , _lowerCAmelCase=0 , **_lowerCAmelCase ) -> Optional[int]: _lowerCAmelCase = dict(self.forward_default_kwargs ) _lowerCAmelCase = kwargs.pop("num_inference_steps" , _lowerCAmelCase ) _lowerCAmelCase = self.dummy_sample _lowerCAmelCase = 0.1 * sample _lowerCAmelCase = [residual + 0.2, residual + 0.15, residual + 0.10] for scheduler_class in self.scheduler_classes: _lowerCAmelCase = self.get_scheduler_config() _lowerCAmelCase = scheduler_class(**_lowerCAmelCase ) scheduler.set_timesteps(_lowerCAmelCase ) # copy over dummy past residuals (must be after setting timesteps) _lowerCAmelCase = dummy_past_residuals[: scheduler.config.solver_order] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(_lowerCAmelCase ) _lowerCAmelCase = scheduler_class.from_pretrained(_lowerCAmelCase ) # copy over dummy past residuals new_scheduler.set_timesteps(_lowerCAmelCase ) # copy over dummy past residual (must be after setting timesteps) _lowerCAmelCase = dummy_past_residuals[: new_scheduler.config.solver_order] _lowerCAmelCase = scheduler.step(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , **_lowerCAmelCase ).prev_sample _lowerCAmelCase = new_scheduler.step(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , **_lowerCAmelCase ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" def _snake_case ( self , _lowerCAmelCase=None , **_lowerCAmelCase ) -> Tuple: if scheduler is None: _lowerCAmelCase = self.scheduler_classes[0] _lowerCAmelCase = self.get_scheduler_config(**_lowerCAmelCase ) _lowerCAmelCase = scheduler_class(**_lowerCAmelCase ) _lowerCAmelCase = self.scheduler_classes[0] _lowerCAmelCase = self.get_scheduler_config(**_lowerCAmelCase ) _lowerCAmelCase = scheduler_class(**_lowerCAmelCase ) _lowerCAmelCase = 10 _lowerCAmelCase = self.dummy_model() _lowerCAmelCase = self.dummy_sample_deter scheduler.set_timesteps(_lowerCAmelCase ) for i, t in enumerate(scheduler.timesteps ): _lowerCAmelCase = model(_lowerCAmelCase , _lowerCAmelCase ) _lowerCAmelCase = scheduler.step(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ).prev_sample return sample def _snake_case ( self ) -> Union[str, Any]: _lowerCAmelCase = DPMSolverSinglestepScheduler(**self.get_scheduler_config() ) _lowerCAmelCase = 50 _lowerCAmelCase = self.dummy_model() _lowerCAmelCase = self.dummy_sample_deter scheduler.set_timesteps(_lowerCAmelCase ) # make sure that the first t is uneven for i, t in enumerate(scheduler.timesteps[3:] ): _lowerCAmelCase = model(_lowerCAmelCase , _lowerCAmelCase ) _lowerCAmelCase = scheduler.step(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ).prev_sample _lowerCAmelCase = torch.mean(torch.abs(_lowerCAmelCase ) ) assert abs(result_mean.item() - 0.2574 ) < 1E-3 def _snake_case ( self ) -> Optional[Any]: for timesteps in [25, 50, 100, 999, 1000]: self.check_over_configs(num_train_timesteps=_lowerCAmelCase ) def _snake_case ( self ) -> List[Any]: # make sure that iterating over schedulers with same config names gives same results # for defaults _lowerCAmelCase = DPMSolverSinglestepScheduler(**self.get_scheduler_config() ) _lowerCAmelCase = self.full_loop(scheduler=_lowerCAmelCase ) _lowerCAmelCase = torch.mean(torch.abs(_lowerCAmelCase ) ) assert abs(result_mean.item() - 0.2791 ) < 1E-3 _lowerCAmelCase = DEISMultistepScheduler.from_config(scheduler.config ) _lowerCAmelCase = DPMSolverMultistepScheduler.from_config(scheduler.config ) _lowerCAmelCase = UniPCMultistepScheduler.from_config(scheduler.config ) _lowerCAmelCase = DPMSolverSinglestepScheduler.from_config(scheduler.config ) _lowerCAmelCase = self.full_loop(scheduler=_lowerCAmelCase ) _lowerCAmelCase = torch.mean(torch.abs(_lowerCAmelCase ) ) assert abs(result_mean.item() - 0.2791 ) < 1E-3 def _snake_case ( self ) -> str: self.check_over_configs(thresholding=_lowerCAmelCase ) for order in [1, 2, 3]: for solver_type in ["midpoint", "heun"]: for threshold in [0.5, 1.0, 2.0]: for prediction_type in ["epsilon", "sample"]: self.check_over_configs( thresholding=_lowerCAmelCase , prediction_type=_lowerCAmelCase , sample_max_value=_lowerCAmelCase , algorithm_type="dpmsolver++" , solver_order=_lowerCAmelCase , solver_type=_lowerCAmelCase , ) def _snake_case ( self ) -> Dict: for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=_lowerCAmelCase ) def _snake_case ( self ) -> Union[str, Any]: for algorithm_type in ["dpmsolver", "dpmsolver++"]: for solver_type in ["midpoint", "heun"]: for order in [1, 2, 3]: for prediction_type in ["epsilon", "sample"]: self.check_over_configs( solver_order=_lowerCAmelCase , solver_type=_lowerCAmelCase , prediction_type=_lowerCAmelCase , algorithm_type=_lowerCAmelCase , ) _lowerCAmelCase = self.full_loop( solver_order=_lowerCAmelCase , solver_type=_lowerCAmelCase , prediction_type=_lowerCAmelCase , algorithm_type=_lowerCAmelCase , ) assert not torch.isnan(_lowerCAmelCase ).any(), "Samples have nan numbers" def _snake_case ( self ) -> Optional[Any]: self.check_over_configs(lower_order_final=_lowerCAmelCase ) self.check_over_configs(lower_order_final=_lowerCAmelCase ) def _snake_case ( self ) -> Optional[Any]: self.check_over_configs(lambda_min_clipped=-float("inf" ) ) self.check_over_configs(lambda_min_clipped=-5.1 ) def _snake_case ( self ) -> str: self.check_over_configs(variance_type=_lowerCAmelCase ) self.check_over_configs(variance_type="learned_range" ) def _snake_case ( self ) -> int: for num_inference_steps in [1, 2, 3, 5, 10, 50, 100, 999, 1000]: self.check_over_forward(num_inference_steps=_lowerCAmelCase , time_step=0 ) def _snake_case ( self ) -> Any: _lowerCAmelCase = self.full_loop() _lowerCAmelCase = torch.mean(torch.abs(_lowerCAmelCase ) ) assert abs(result_mean.item() - 0.2791 ) < 1E-3 def _snake_case ( self ) -> List[str]: _lowerCAmelCase = self.full_loop(use_karras_sigmas=_lowerCAmelCase ) _lowerCAmelCase = torch.mean(torch.abs(_lowerCAmelCase ) ) assert abs(result_mean.item() - 0.2248 ) < 1E-3 def _snake_case ( self ) -> Union[str, Any]: _lowerCAmelCase = self.full_loop(prediction_type="v_prediction" ) _lowerCAmelCase = torch.mean(torch.abs(_lowerCAmelCase ) ) assert abs(result_mean.item() - 0.1453 ) < 1E-3 def _snake_case ( self ) -> Any: _lowerCAmelCase = self.full_loop(prediction_type="v_prediction" , use_karras_sigmas=_lowerCAmelCase ) _lowerCAmelCase = torch.mean(torch.abs(_lowerCAmelCase ) ) assert abs(result_mean.item() - 0.0649 ) < 1E-3 def _snake_case ( self ) -> List[Any]: _lowerCAmelCase = self.scheduler_classes[0] _lowerCAmelCase = self.get_scheduler_config(thresholding=_lowerCAmelCase , dynamic_thresholding_ratio=0 ) _lowerCAmelCase = scheduler_class(**_lowerCAmelCase ) _lowerCAmelCase = 10 _lowerCAmelCase = self.dummy_model() _lowerCAmelCase = self.dummy_sample_deter.half() scheduler.set_timesteps(_lowerCAmelCase ) for i, t in enumerate(scheduler.timesteps ): _lowerCAmelCase = model(_lowerCAmelCase , _lowerCAmelCase ) _lowerCAmelCase = scheduler.step(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ).prev_sample assert sample.dtype == torch.floataa
18
'''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 SCREAMING_SNAKE_CASE__ ( unittest.TestCase): 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] , )-> Dict: '''simple docstring''' UpperCamelCase = parent UpperCamelCase = batch_size UpperCamelCase = num_channels UpperCamelCase = image_size UpperCamelCase = min_resolution UpperCamelCase = max_resolution UpperCamelCase = do_resize UpperCamelCase = size if size is not None else {'height': 18, 'width': 20} UpperCamelCase = do_thumbnail UpperCamelCase = do_align_axis UpperCamelCase = do_pad UpperCamelCase = do_normalize UpperCamelCase = image_mean UpperCamelCase = image_std def UpperCAmelCase_ ( self )-> List[Any]: '''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 SCREAMING_SNAKE_CASE__ ( snake_case_ , unittest.TestCase): lowerCAmelCase_ = DonutImageProcessor if is_vision_available() else None def UpperCAmelCase_ ( self )-> str: '''simple docstring''' UpperCamelCase = DonutImageProcessingTester(self ) @property def UpperCAmelCase_ ( self )-> str: '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def UpperCAmelCase_ ( self )-> Optional[int]: '''simple docstring''' UpperCamelCase = 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 UpperCAmelCase_ ( self )-> Optional[int]: '''simple docstring''' UpperCamelCase = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'height': 18, 'width': 20} ) UpperCamelCase = 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 UpperCamelCase = self.image_processing_class.from_dict(self.image_processor_dict , size=(42, 84) ) self.assertEqual(image_processor.size , {'height': 84, 'width': 42} ) def UpperCAmelCase_ ( self )-> Tuple: '''simple docstring''' pass @is_flaky() def UpperCAmelCase_ ( self )-> Any: '''simple docstring''' UpperCamelCase = self.image_processing_class(**self.image_processor_dict ) # create random PIL images UpperCamelCase = prepare_image_inputs(self.image_processor_tester , equal_resolution=A_ ) for image in image_inputs: self.assertIsInstance(A_ , Image.Image ) # Test not batched input UpperCamelCase = 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 UpperCamelCase = 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 UpperCAmelCase_ ( self )-> Optional[int]: '''simple docstring''' UpperCamelCase = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors UpperCamelCase = prepare_image_inputs(self.image_processor_tester , equal_resolution=A_ , numpify=A_ ) for image in image_inputs: self.assertIsInstance(A_ , np.ndarray ) # Test not batched input UpperCamelCase = 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 UpperCamelCase = 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 UpperCAmelCase_ ( self )-> Dict: '''simple docstring''' UpperCamelCase = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors UpperCamelCase = prepare_image_inputs(self.image_processor_tester , equal_resolution=A_ , torchify=A_ ) for image in image_inputs: self.assertIsInstance(A_ , torch.Tensor ) # Test not batched input UpperCamelCase = 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 UpperCamelCase = 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'], ) , )
3
0
"""simple docstring""" import logging import os import sys import warnings from dataclasses import dataclass, field from random import randint from typing import Optional import datasets import evaluate import numpy as np from datasets import DatasetDict, load_dataset import transformers from transformers import ( AutoConfig, AutoFeatureExtractor, AutoModelForAudioClassification, HfArgumentParser, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import get_last_checkpoint from transformers.utils import check_min_version, send_example_telemetry from transformers.utils.versions import require_version _a = logging.getLogger(__name__) # Will error if the minimal version of Transformers is not installed. Remove at your own risks. check_min_version("""4.31.0""") require_version("""datasets>=1.14.0""", """To fix: pip install -r examples/pytorch/audio-classification/requirements.txt""") def lowerCamelCase__ ( __snake_case, __snake_case, __snake_case = 1_60_00 ) -> int: """simple docstring""" _UpperCamelCase = int(round(sample_rate * max_length ) ) if len(__snake_case ) <= sample_length: return wav _UpperCamelCase = randint(0, len(__snake_case ) - sample_length - 1 ) return wav[random_offset : random_offset + sample_length] @dataclass class _UpperCAmelCase: lowercase__ = field(default=lowerCamelCase , metadata={'help': 'Name of a dataset from the datasets package'} ) lowercase__ = field( default=lowerCamelCase , metadata={'help': 'The configuration name of the dataset to use (via the datasets library).'} ) lowercase__ = field( default=lowerCamelCase , metadata={'help': 'A file containing the training audio paths and labels.'} ) lowercase__ = field( default=lowerCamelCase , metadata={'help': 'A file containing the validation audio paths and labels.'} ) lowercase__ = field( default='train' , metadata={ 'help': 'The name of the training data set split to use (via the datasets library). Defaults to \'train\'' } , ) lowercase__ = field( default='validation' , metadata={ 'help': ( 'The name of the training data set split to use (via the datasets library). Defaults to \'validation\'' ) } , ) lowercase__ = field( default='audio' , metadata={'help': 'The name of the dataset column containing the audio data. Defaults to \'audio\''} , ) lowercase__ = field( default='label' , metadata={'help': 'The name of the dataset column containing the labels. Defaults to \'label\''} ) lowercase__ = field( default=lowerCamelCase , metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of training examples to this ' 'value if set.' ) } , ) lowercase__ = field( default=lowerCamelCase , metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of evaluation examples to this ' 'value if set.' ) } , ) lowercase__ = field( default=20 , metadata={'help': 'Audio clips will be randomly cut to this length during training if the value is set.'} , ) @dataclass class _UpperCAmelCase: lowercase__ = field( default='facebook/wav2vec2-base' , metadata={'help': 'Path to pretrained model or model identifier from huggingface.co/models'} , ) lowercase__ = field( default=lowerCamelCase , metadata={'help': 'Pretrained config name or path if not the same as model_name'} ) lowercase__ = field( default=lowerCamelCase , metadata={'help': 'Where do you want to store the pretrained models downloaded from the Hub'} ) lowercase__ = field( default='main' , metadata={'help': 'The specific model version to use (can be a branch name, tag name or commit id).'} , ) lowercase__ = field( default=lowerCamelCase , metadata={'help': 'Name or path of preprocessor config.'} ) lowercase__ = field( default=lowerCamelCase , metadata={'help': 'Whether to freeze the feature encoder layers of the model.'} ) lowercase__ = field( default=lowerCamelCase , metadata={'help': 'Whether to generate an attention mask in the feature extractor.'} ) lowercase__ = field( default=lowerCamelCase , metadata={ 'help': ( 'Will use the token generated when running `huggingface-cli login` (necessary to use this script ' 'with private models).' ) } , ) lowercase__ = field( default=lowerCamelCase , metadata={'help': 'Whether to freeze the feature extractor layers of the model.'} ) lowercase__ = field( default=lowerCamelCase , metadata={'help': 'Will enable to load a pretrained model whose head dimensions are different.'} , ) def UpperCAmelCase ( self) -> Union[str, Any]: '''simple docstring''' if not self.freeze_feature_extractor and self.freeze_feature_encoder: warnings.warn( '''The argument `--freeze_feature_extractor` is deprecated and ''' '''will be removed in a future version. Use `--freeze_feature_encoder`''' '''instead. Setting `freeze_feature_encoder==True`.''' , __a , ) if self.freeze_feature_extractor and not self.freeze_feature_encoder: raise ValueError( '''The argument `--freeze_feature_extractor` is deprecated and ''' '''should not be used in combination with `--freeze_feature_encoder`.''' '''Only make use of `--freeze_feature_encoder`.''') def lowerCamelCase__ ( ) -> List[Any]: """simple docstring""" _UpperCamelCase = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith('''.json''' ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. _UpperCamelCase , _UpperCamelCase , _UpperCamelCase = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: _UpperCamelCase , _UpperCamelCase , _UpperCamelCase = parser.parse_args_into_dataclasses() # Sending telemetry. Tracking the example usage helps us better allocate resources to maintain them. The # information sent is the one passed as arguments along with your Python/PyTorch versions. send_example_telemetry('''run_audio_classification''', __snake_case, __snake_case ) # Setup logging logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''', datefmt='''%m/%d/%Y %H:%M:%S''', handlers=[logging.StreamHandler(sys.stdout )], ) if training_args.should_log: # The default of training_args.log_level is passive, so we set log level at info here to have that default. transformers.utils.logging.set_verbosity_info() _UpperCamelCase = training_args.get_process_log_level() logger.setLevel(__snake_case ) transformers.utils.logging.set_verbosity(__snake_case ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Log on each process the small summary: logger.warning( F'''Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu} ''' + F'''distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}''' ) logger.info(F'''Training/evaluation parameters {training_args}''' ) # Set seed before initializing model. set_seed(training_args.seed ) # Detecting last checkpoint. _UpperCamelCase = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: _UpperCamelCase = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( F'''Output directory ({training_args.output_dir}) already exists and is not empty. ''' '''Use --overwrite_output_dir to train from scratch.''' ) elif last_checkpoint is not None and training_args.resume_from_checkpoint is None: logger.info( F'''Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change ''' '''the `--output_dir` or add `--overwrite_output_dir` to train from scratch.''' ) # Initialize our dataset and prepare it for the audio classification task. _UpperCamelCase = DatasetDict() _UpperCamelCase = load_dataset( data_args.dataset_name, data_args.dataset_config_name, split=data_args.train_split_name, use_auth_token=True if model_args.use_auth_token else None, ) _UpperCamelCase = load_dataset( data_args.dataset_name, data_args.dataset_config_name, split=data_args.eval_split_name, use_auth_token=True if model_args.use_auth_token else None, ) if data_args.audio_column_name not in raw_datasets["train"].column_names: raise ValueError( F'''--audio_column_name {data_args.audio_column_name} not found in dataset \'{data_args.dataset_name}\'. ''' '''Make sure to set `--audio_column_name` to the correct audio column - one of ''' F'''{", ".join(raw_datasets["train"].column_names )}.''' ) if data_args.label_column_name not in raw_datasets["train"].column_names: raise ValueError( F'''--label_column_name {data_args.label_column_name} not found in dataset \'{data_args.dataset_name}\'. ''' '''Make sure to set `--label_column_name` to the correct text column - one of ''' F'''{", ".join(raw_datasets["train"].column_names )}.''' ) # Setting `return_attention_mask=True` is the way to get a correctly masked mean-pooling over # transformer outputs in the classifier, but it doesn't always lead to better accuracy _UpperCamelCase = AutoFeatureExtractor.from_pretrained( model_args.feature_extractor_name or model_args.model_name_or_path, return_attention_mask=model_args.attention_mask, cache_dir=model_args.cache_dir, revision=model_args.model_revision, use_auth_token=True if model_args.use_auth_token else None, ) # `datasets` takes care of automatically loading and resampling the audio, # so we just need to set the correct target sampling rate. _UpperCamelCase = raw_datasets.cast_column( data_args.audio_column_name, datasets.features.Audio(sampling_rate=feature_extractor.sampling_rate ) ) _UpperCamelCase = feature_extractor.model_input_names[0] def train_transforms(__snake_case ): _UpperCamelCase = [] for audio in batch[data_args.audio_column_name]: _UpperCamelCase = random_subsample( audio['''array'''], max_length=data_args.max_length_seconds, sample_rate=feature_extractor.sampling_rate ) subsampled_wavs.append(__snake_case ) _UpperCamelCase = feature_extractor(__snake_case, sampling_rate=feature_extractor.sampling_rate ) _UpperCamelCase = {model_input_name: inputs.get(__snake_case )} _UpperCamelCase = list(batch[data_args.label_column_name] ) return output_batch def val_transforms(__snake_case ): _UpperCamelCase = [audio['''array'''] for audio in batch[data_args.audio_column_name]] _UpperCamelCase = feature_extractor(__snake_case, sampling_rate=feature_extractor.sampling_rate ) _UpperCamelCase = {model_input_name: inputs.get(__snake_case )} _UpperCamelCase = list(batch[data_args.label_column_name] ) return output_batch # Prepare label mappings. # We'll include these in the model's config to get human readable labels in the Inference API. _UpperCamelCase = raw_datasets['''train'''].features[data_args.label_column_name].names _UpperCamelCase , _UpperCamelCase = {}, {} for i, label in enumerate(__snake_case ): _UpperCamelCase = str(__snake_case ) _UpperCamelCase = label # Load the accuracy metric from the datasets package _UpperCamelCase = evaluate.load('''accuracy''' ) # Define our compute_metrics function. It takes an `EvalPrediction` object (a namedtuple with # `predictions` and `label_ids` fields) and has to return a dictionary string to float. def compute_metrics(__snake_case ): _UpperCamelCase = np.argmax(eval_pred.predictions, axis=1 ) return metric.compute(predictions=__snake_case, references=eval_pred.label_ids ) _UpperCamelCase = AutoConfig.from_pretrained( model_args.config_name or model_args.model_name_or_path, num_labels=len(__snake_case ), labelaid=__snake_case, idalabel=__snake_case, finetuning_task='''audio-classification''', cache_dir=model_args.cache_dir, revision=model_args.model_revision, use_auth_token=True if model_args.use_auth_token else None, ) _UpperCamelCase = AutoModelForAudioClassification.from_pretrained( model_args.model_name_or_path, from_tf=bool('''.ckpt''' in model_args.model_name_or_path ), config=__snake_case, cache_dir=model_args.cache_dir, revision=model_args.model_revision, use_auth_token=True if model_args.use_auth_token else None, ignore_mismatched_sizes=model_args.ignore_mismatched_sizes, ) # freeze the convolutional waveform encoder if model_args.freeze_feature_encoder: model.freeze_feature_encoder() if training_args.do_train: if data_args.max_train_samples is not None: _UpperCamelCase = ( raw_datasets['''train'''].shuffle(seed=training_args.seed ).select(range(data_args.max_train_samples ) ) ) # Set the training transforms raw_datasets["train"].set_transform(__snake_case, output_all_columns=__snake_case ) if training_args.do_eval: if data_args.max_eval_samples is not None: _UpperCamelCase = ( raw_datasets['''eval'''].shuffle(seed=training_args.seed ).select(range(data_args.max_eval_samples ) ) ) # Set the validation transforms raw_datasets["eval"].set_transform(__snake_case, output_all_columns=__snake_case ) # Initialize our trainer _UpperCamelCase = Trainer( model=__snake_case, args=__snake_case, train_dataset=raw_datasets['''train'''] if training_args.do_train else None, eval_dataset=raw_datasets['''eval'''] if training_args.do_eval else None, compute_metrics=__snake_case, tokenizer=__snake_case, ) # Training if training_args.do_train: _UpperCamelCase = None if training_args.resume_from_checkpoint is not None: _UpperCamelCase = training_args.resume_from_checkpoint elif last_checkpoint is not None: _UpperCamelCase = last_checkpoint _UpperCamelCase = trainer.train(resume_from_checkpoint=__snake_case ) trainer.save_model() trainer.log_metrics('''train''', train_result.metrics ) trainer.save_metrics('''train''', train_result.metrics ) trainer.save_state() # Evaluation if training_args.do_eval: _UpperCamelCase = trainer.evaluate() trainer.log_metrics('''eval''', __snake_case ) trainer.save_metrics('''eval''', __snake_case ) # Write model card and (optionally) push to hub _UpperCamelCase = { '''finetuned_from''': model_args.model_name_or_path, '''tasks''': '''audio-classification''', '''dataset''': data_args.dataset_name, '''tags''': ['''audio-classification'''], } if training_args.push_to_hub: trainer.push_to_hub(**__snake_case ) else: trainer.create_model_card(**__snake_case ) if __name__ == "__main__": main()
19
'''simple docstring''' def A_( A : list[int]): UpperCamelCase = [] if len(A) == 1: return [nums.copy()] for _ in range(len(A)): UpperCamelCase = nums.pop(0) UpperCamelCase = permute(A) for perm in permutations: perm.append(A) result.extend(A) nums.append(A) return result def A_( A : str): def backtrack(A : str): if start == len(A) - 1: output.append(nums[:]) else: for i in range(A , len(A)): UpperCamelCase , UpperCamelCase = nums[i], nums[start] backtrack(start + 1) UpperCamelCase , UpperCamelCase = nums[i], nums[start] # backtrack UpperCamelCase = [] backtrack(0) return output if __name__ == "__main__": import doctest # use res to print the data in permute2 function lowerCAmelCase : Dict = permutea([1, 2, 3]) print(res) doctest.testmod()
3
0
import json import os import unittest from transformers import MgpstrTokenizer from transformers.models.mgp_str.tokenization_mgp_str import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class lowercase_ (lowercase__ , unittest.TestCase ): snake_case =MgpstrTokenizer snake_case =False snake_case ={} snake_case =False def __UpperCamelCase ( self) -> Tuple: super().setUp() # fmt: off a__ =['[GO]', '[s]', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] # fmt: on a__ =dict(zip(lowercase_ , range(len(lowercase_)))) a__ =os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['vocab_file']) with open(self.vocab_file , 'w' , encoding='utf-8') as fp: fp.write(json.dumps(lowercase_) + '\n') def __UpperCamelCase ( self , **lowercase_) -> Optional[Any]: return MgpstrTokenizer.from_pretrained(self.tmpdirname , **lowercase_) def __UpperCamelCase ( self , lowercase_) -> Any: a__ ='tester' a__ ='tester' return input_text, output_text @unittest.skip('MGP-STR always lower cases letters.') def __UpperCamelCase ( self) -> Optional[Any]: pass def __UpperCamelCase ( self) -> int: a__ =self.get_tokenizers(do_lower_case=lowercase_) for tokenizer in tokenizers: with self.subTest(F"""{tokenizer.__class__.__name__}"""): a__ ='[SPECIAL_TOKEN]' tokenizer.add_special_tokens({'cls_token': special_token}) a__ =tokenizer.encode([special_token] , add_special_tokens=lowercase_) self.assertEqual(len(lowercase_) , 1) a__ =tokenizer.decode(lowercase_ , skip_special_tokens=lowercase_) self.assertTrue(special_token not in decoded) def __UpperCamelCase ( self) -> Dict: a__ =self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(F"""{tokenizer.__class__.__name__}"""): a__ , a__ =self.get_input_output_texts(lowercase_) a__ =tokenizer.tokenize(lowercase_) a__ =tokenizer.convert_tokens_to_ids(lowercase_) a__ =tokenizer.encode(lowercase_ , add_special_tokens=lowercase_) self.assertListEqual(lowercase_ , lowercase_) a__ =tokenizer.convert_ids_to_tokens(lowercase_) self.assertNotEqual(len(lowercase_) , 0) a__ =tokenizer.decode(lowercase_) self.assertIsInstance(lowercase_ , lowercase_) self.assertEqual(text_a.replace(' ' , '') , lowercase_) @unittest.skip('MGP-STR tokenizer only handles one sequence.') def __UpperCamelCase ( self) -> Union[str, Any]: pass @unittest.skip('inputs cannot be pretokenized in MgpstrTokenizer') def __UpperCamelCase ( self) -> str: pass
20
'''simple docstring''' import colorsys from PIL import Image # type: ignore def A_( A : float , A : float , A : int): UpperCamelCase = x UpperCamelCase = y for step in range(A): # noqa: B007 UpperCamelCase = a * a - b * b + x UpperCamelCase = 2 * a * b + y UpperCamelCase = a_new # divergence happens for all complex number with an absolute value # greater than 4 if a * a + b * b > 4: break return step / (max_step - 1) def A_( A : float): if distance == 1: return (0, 0, 0) else: return (255, 255, 255) def A_( A : float): if distance == 1: return (0, 0, 0) else: return tuple(round(i * 255) for i in colorsys.hsv_to_rgb(A , 1 , 1)) def A_( A : int = 800 , A : int = 600 , A : float = -0.6 , A : float = 0 , A : float = 3.2 , A : int = 50 , A : bool = True , ): UpperCamelCase = Image.new('RGB' , (image_width, image_height)) UpperCamelCase = img.load() # loop through the image-coordinates for image_x in range(A): for image_y in range(A): # determine the figure-coordinates based on the image-coordinates UpperCamelCase = figure_width / image_width * image_height UpperCamelCase = figure_center_x + (image_x / image_width - 0.5) * figure_width UpperCamelCase = figure_center_y + (image_y / image_height - 0.5) * figure_height UpperCamelCase = get_distance(A , A , A) # color the corresponding pixel based on the selected coloring-function if use_distance_color_coding: UpperCamelCase = get_color_coded_rgb(A) else: UpperCamelCase = get_black_and_white_rgb(A) return img if __name__ == "__main__": import doctest doctest.testmod() # colored version, full figure lowerCAmelCase : Any = get_image() # uncomment for colored version, different section, zoomed in # img = get_image(figure_center_x = -0.6, figure_center_y = -0.4, # figure_width = 0.8) # uncomment for black and white version, full figure # img = get_image(use_distance_color_coding = False) # uncomment to save the image # img.save("mandelbrot.png") img.show()
3
0
import unittest from transformers import SPIECE_UNDERLINE from transformers.models.speechta import SpeechTaTokenizer from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow from transformers.tokenization_utils import AddedToken from ...test_tokenization_common import TokenizerTesterMixin UpperCAmelCase_ : int = get_tests_dir("fixtures/test_sentencepiece_bpe_char.model") @require_sentencepiece @require_tokenizers class __A ( UpperCamelCase__ , unittest.TestCase ): UpperCamelCase = SpeechTaTokenizer UpperCamelCase = False UpperCamelCase = True def A__ ( self :Dict ): '''simple docstring''' super().setUp() # We have a SentencePiece fixture for testing __magic_name__ : int =SpeechTaTokenizer(__snake_case ) __magic_name__ : Any =AddedToken("""<mask>""" , lstrip=__snake_case , rstrip=__snake_case ) __magic_name__ : List[Any] =mask_token tokenizer.add_special_tokens({"""mask_token""": mask_token} ) tokenizer.add_tokens(["""<ctc_blank>"""] ) tokenizer.save_pretrained(self.tmpdirname ) def A__ ( self :List[Any] , __snake_case :List[Any] ): '''simple docstring''' __magic_name__ : Union[str, Any] ="""this is a test""" __magic_name__ : Tuple ="""this is a test""" return input_text, output_text def A__ ( self :str , __snake_case :Tuple , __snake_case :Union[str, Any]=False , __snake_case :List[str]=20 , __snake_case :str=5 ): '''simple docstring''' __magic_name__ , __magic_name__ : List[str] =self.get_input_output_texts(__snake_case ) __magic_name__ : Tuple =tokenizer.encode(__snake_case , add_special_tokens=__snake_case ) __magic_name__ : Optional[int] =tokenizer.decode(__snake_case , clean_up_tokenization_spaces=__snake_case ) return text, ids def A__ ( self :str ): '''simple docstring''' __magic_name__ : int ="""<pad>""" __magic_name__ : Union[str, Any] =1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(__snake_case ) , __snake_case ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(__snake_case ) , __snake_case ) def A__ ( self :int ): '''simple docstring''' __magic_name__ : Optional[int] =list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , """<s>""" ) self.assertEqual(vocab_keys[1] , """<pad>""" ) self.assertEqual(vocab_keys[-4] , """œ""" ) self.assertEqual(vocab_keys[-2] , """<mask>""" ) self.assertEqual(vocab_keys[-1] , """<ctc_blank>""" ) self.assertEqual(len(__snake_case ) , 81 ) def A__ ( self :str ): '''simple docstring''' self.assertEqual(self.get_tokenizer().vocab_size , 79 ) def A__ ( self :List[str] ): '''simple docstring''' __magic_name__ : List[Any] =self.get_tokenizers(do_lower_case=__snake_case ) for tokenizer in tokenizers: with self.subTest(f"{tokenizer.__class__.__name__}" ): __magic_name__ : Tuple =tokenizer.vocab_size __magic_name__ : Optional[Any] =len(__snake_case ) self.assertNotEqual(__snake_case , 0 ) # We usually have added tokens from the start in tests because our vocab fixtures are # smaller than the original vocabs - let's not assert this # self.assertEqual(vocab_size, all_size) __magic_name__ : Optional[Any] =["""aaaaa bbbbbb""", """cccccccccdddddddd"""] __magic_name__ : Dict =tokenizer.add_tokens(__snake_case ) __magic_name__ : List[str] =tokenizer.vocab_size __magic_name__ : Tuple =len(__snake_case ) self.assertNotEqual(__snake_case , 0 ) self.assertEqual(__snake_case , __snake_case ) self.assertEqual(__snake_case , len(__snake_case ) ) self.assertEqual(__snake_case , all_size + len(__snake_case ) ) __magic_name__ : Any =tokenizer.encode("""aaaaa bbbbbb low cccccccccdddddddd l""" , add_special_tokens=__snake_case ) self.assertGreaterEqual(len(__snake_case ) , 4 ) self.assertGreater(tokens[0] , tokenizer.vocab_size - 1 ) self.assertGreater(tokens[-3] , tokenizer.vocab_size - 1 ) __magic_name__ : Any ={"""eos_token""": """>>>>|||<||<<|<<""", """pad_token""": """<<<<<|||>|>>>>|>"""} __magic_name__ : Any =tokenizer.add_special_tokens(__snake_case ) __magic_name__ : Dict =tokenizer.vocab_size __magic_name__ : str =len(__snake_case ) self.assertNotEqual(__snake_case , 0 ) self.assertEqual(__snake_case , __snake_case ) self.assertEqual(__snake_case , len(__snake_case ) ) self.assertEqual(__snake_case , all_size_a + len(__snake_case ) ) __magic_name__ : Optional[int] =tokenizer.encode( """>>>>|||<||<<|<< aaaaabbbbbb low cccccccccdddddddd <<<<<|||>|>>>>|> l""" , add_special_tokens=__snake_case ) self.assertGreaterEqual(len(__snake_case ) , 6 ) self.assertGreater(tokens[0] , tokenizer.vocab_size - 1 ) self.assertGreater(tokens[0] , tokens[1] ) self.assertGreater(tokens[-3] , tokenizer.vocab_size - 1 ) self.assertGreater(tokens[-3] , tokens[-4] ) self.assertEqual(tokens[0] , tokenizer.eos_token_id ) self.assertEqual(tokens[-3] , tokenizer.pad_token_id ) def A__ ( self :Optional[int] ): '''simple docstring''' pass def A__ ( self :List[str] ): '''simple docstring''' pass def A__ ( self :Dict ): '''simple docstring''' __magic_name__ : Optional[int] =self.get_tokenizer() __magic_name__ : Optional[int] =tokenizer.tokenize("""This is a test""" ) # fmt: off self.assertListEqual(__snake_case , [SPIECE_UNDERLINE, """T""", """h""", """i""", """s""", SPIECE_UNDERLINE, """i""", """s""", SPIECE_UNDERLINE, """a""", SPIECE_UNDERLINE, """t""", """e""", """s""", """t"""] ) # fmt: on self.assertListEqual( tokenizer.convert_tokens_to_ids(__snake_case ) , [4, 32, 11, 10, 12, 4, 10, 12, 4, 7, 4, 6, 5, 12, 6] , ) __magic_name__ : int =tokenizer.tokenize("""I was born in 92000, and this is falsé.""" ) self.assertListEqual( __snake_case , [SPIECE_UNDERLINE, """I""", SPIECE_UNDERLINE, """w""", """a""", """s""", SPIECE_UNDERLINE, """b""", """o""", """r""", """n""", SPIECE_UNDERLINE, """i""", """n""", SPIECE_UNDERLINE, """92000""", """,""", SPIECE_UNDERLINE, """a""", """n""", """d""", SPIECE_UNDERLINE, """t""", """h""", """i""", """s""", SPIECE_UNDERLINE, """i""", """s""", SPIECE_UNDERLINE, """f""", """a""", """l""", """s""", """é""", """."""] ) __magic_name__ : Any =tokenizer.convert_tokens_to_ids(__snake_case ) # fmt: off self.assertListEqual(__snake_case , [4, 30, 4, 20, 7, 12, 4, 25, 8, 13, 9, 4, 10, 9, 4, 3, 23, 4, 7, 9, 14, 4, 6, 11, 10, 12, 4, 10, 12, 4, 19, 7, 15, 12, 73, 26] ) # fmt: on __magic_name__ : List[str] =tokenizer.convert_ids_to_tokens(__snake_case ) self.assertListEqual( __snake_case , [SPIECE_UNDERLINE, """I""", SPIECE_UNDERLINE, """w""", """a""", """s""", SPIECE_UNDERLINE, """b""", """o""", """r""", """n""", SPIECE_UNDERLINE, """i""", """n""", SPIECE_UNDERLINE, """<unk>""", """,""", SPIECE_UNDERLINE, """a""", """n""", """d""", SPIECE_UNDERLINE, """t""", """h""", """i""", """s""", SPIECE_UNDERLINE, """i""", """s""", SPIECE_UNDERLINE, """f""", """a""", """l""", """s""", """é""", """."""] ) @slow def A__ ( self :Optional[Any] ): '''simple docstring''' __magic_name__ : Dict =[ """Transformers (formerly known as pytorch-transformers and pytorch-pretrained-bert) provides """ """general-purpose architectures (BERT, GPT, RoBERTa, XLM, DistilBert, XLNet...) for Natural """ """Language Understanding (NLU) and Natural Language Generation (NLG) with over thirty-two pretrained """ """models in one hundred plus languages and deep interoperability between Jax, PyTorch and TensorFlow.""", """BERT is designed to pre-train deep bidirectional representations from unlabeled text by jointly """ """conditioning on both left and right context in all layers.""", """The quick brown fox jumps over the lazy dog.""", ] # fmt: off __magic_name__ : Tuple ={ """input_ids""": [ [4, 32, 13, 7, 9, 12, 19, 8, 13, 18, 5, 13, 12, 4, 64, 19, 8, 13, 18, 5, 13, 15, 22, 4, 28, 9, 8, 20, 9, 4, 7, 12, 4, 24, 22, 6, 8, 13, 17, 11, 39, 6, 13, 7, 9, 12, 19, 8, 13, 18, 5, 13, 12, 4, 7, 9, 14, 4, 24, 22, 6, 8, 13, 17, 11, 39, 24, 13, 5, 6, 13, 7, 10, 9, 5, 14, 39, 25, 5, 13, 6, 63, 4, 24, 13, 8, 27, 10, 14, 5, 12, 4, 21, 5, 9, 5, 13, 7, 15, 39, 24, 16, 13, 24, 8, 12, 5, 4, 7, 13, 17, 11, 10, 6, 5, 17, 6, 16, 13, 5, 12, 4, 64, 40, 47, 54, 32, 23, 4, 53, 49, 32, 23, 4, 54, 8, 40, 47, 54, 32, 7, 23, 4, 69, 52, 43, 23, 4, 51, 10, 12, 6, 10, 15, 40, 5, 13, 6, 23, 4, 69, 52, 48, 5, 6, 26, 26, 26, 63, 4, 19, 8, 13, 4, 48, 7, 6, 16, 13, 7, 15, 4, 52, 7, 9, 21, 16, 7, 21, 5, 4, 61, 9, 14, 5, 13, 12, 6, 7, 9, 14, 10, 9, 21, 4, 64, 48, 52, 61, 63, 4, 7, 9, 14, 4, 48, 7, 6, 16, 13, 7, 15, 4, 52, 7, 9, 21, 16, 7, 21, 5, 4, 53, 5, 9, 5, 13, 7, 6, 10, 8, 9, 4, 64, 48, 52, 53, 63, 4, 20, 10, 6, 11, 4, 8, 27, 5, 13, 4, 6, 11, 10, 13, 6, 22, 39, 6, 20, 8, 4, 24, 13, 5, 6, 13, 7, 10, 9, 5, 14, 4, 18, 8, 14, 5, 15, 12, 4, 10, 9, 4, 8, 9, 5, 4, 11, 16, 9, 14, 13, 5, 14, 4, 24, 15, 16, 12, 4, 15, 7, 9, 21, 16, 7, 21, 5, 12, 4, 7, 9, 14, 4, 14, 5, 5, 24, 4, 10, 9, 6, 5, 13, 8, 24, 5, 13, 7, 25, 10, 15, 10, 6, 22, 4, 25, 5, 6, 20, 5, 5, 9, 4, 58, 7, 37, 23, 4, 49, 22, 32, 8, 13, 17, 11, 4, 7, 9, 14, 4, 32, 5, 9, 12, 8, 13, 55, 15, 8, 20, 26, 2], [4, 40, 47, 54, 32, 4, 10, 12, 4, 14, 5, 12, 10, 21, 9, 5, 14, 4, 6, 8, 4, 24, 13, 5, 39, 6, 13, 7, 10, 9, 4, 14, 5, 5, 24, 4, 25, 10, 14, 10, 13, 5, 17, 6, 10, 8, 9, 7, 15, 4, 13, 5, 24, 13, 5, 12, 5, 9, 6, 7, 6, 10, 8, 9, 12, 4, 19, 13, 8, 18, 4, 16, 9, 15, 7, 25, 5, 15, 5, 14, 4, 6, 5, 37, 6, 4, 25, 22, 4, 46, 8, 10, 9, 6, 15, 22, 4, 17, 8, 9, 14, 10, 6, 10, 8, 9, 10, 9, 21, 4, 8, 9, 4, 25, 8, 6, 11, 4, 15, 5, 19, 6, 4, 7, 9, 14, 4, 13, 10, 21, 11, 6, 4, 17, 8, 9, 6, 5, 37, 6, 4, 10, 9, 4, 7, 15, 15, 4, 15, 7, 22, 5, 13, 12, 26, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [4, 32, 11, 5, 4, 45, 16, 10, 17, 28, 4, 25, 13, 8, 20, 9, 4, 19, 8, 37, 4, 46, 16, 18, 24, 12, 4, 8, 27, 5, 13, 4, 6, 11, 5, 4, 15, 7, 57, 22, 4, 14, 8, 21, 26, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], ], """attention_mask""": [ [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], ] } # fmt: on self.tokenizer_integration_test_util( expected_encoding=__snake_case , model_name="""microsoft/speecht5_asr""" , revision="""c5ef64c71905caeccde0e4462ef3f9077224c524""" , sequences=__snake_case , )
21
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_torch_available, ) lowerCAmelCase : Optional[Any] = { 'configuration_falcon': ['FALCON_PRETRAINED_CONFIG_ARCHIVE_MAP', 'FalconConfig'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : str = [ 'FALCON_PRETRAINED_MODEL_ARCHIVE_LIST', 'FalconForCausalLM', 'FalconModel', 'FalconPreTrainedModel', 'FalconForSequenceClassification', 'FalconForTokenClassification', 'FalconForQuestionAnswering', ] if TYPE_CHECKING: from .configuration_falcon import FALCON_PRETRAINED_CONFIG_ARCHIVE_MAP, FalconConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_falcon import ( FALCON_PRETRAINED_MODEL_ARCHIVE_LIST, FalconForCausalLM, FalconForQuestionAnswering, FalconForSequenceClassification, FalconForTokenClassification, FalconModel, FalconPreTrainedModel, ) else: import sys lowerCAmelCase : Tuple = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
3
0
'''simple docstring''' import copy from ...configuration_utils import PretrainedConfig from ...utils import logging from ..bit import BitConfig _snake_case : Dict = logging.get_logger(__name__) _snake_case : int = { 'Intel/dpt-large': 'https://huggingface.co/Intel/dpt-large/resolve/main/config.json', # See all DPT models at https://huggingface.co/models?filter=dpt } class A ( _a ): lowercase_ = 'dpt' def __init__( self : List[Any] , lowerCAmelCase_ : int=7_68 , lowerCAmelCase_ : str=12 , lowerCAmelCase_ : Any=12 , lowerCAmelCase_ : List[Any]=30_72 , lowerCAmelCase_ : Any="gelu" , lowerCAmelCase_ : Any=0.0 , lowerCAmelCase_ : Union[str, Any]=0.0 , lowerCAmelCase_ : List[str]=0.0_2 , lowerCAmelCase_ : Any=1e-12 , lowerCAmelCase_ : List[Any]=3_84 , lowerCAmelCase_ : Optional[Any]=16 , lowerCAmelCase_ : Optional[int]=3 , lowerCAmelCase_ : int=False , lowerCAmelCase_ : str=True , lowerCAmelCase_ : Dict=[2, 5, 8, 11] , lowerCAmelCase_ : Optional[Any]="project" , lowerCAmelCase_ : int=[4, 2, 1, 0.5] , lowerCAmelCase_ : Optional[Any]=[96, 1_92, 3_84, 7_68] , lowerCAmelCase_ : List[Any]=2_56 , lowerCAmelCase_ : Optional[int]=-1 , lowerCAmelCase_ : int=False , lowerCAmelCase_ : Optional[int]=True , lowerCAmelCase_ : Any=0.4 , lowerCAmelCase_ : List[str]=2_55 , lowerCAmelCase_ : Any=0.1 , lowerCAmelCase_ : Tuple=[1, 10_24, 24, 24] , lowerCAmelCase_ : Optional[int]=[0, 1] , lowerCAmelCase_ : int=None , **lowerCAmelCase_ : Dict , ) -> Union[str, Any]: """simple docstring""" super().__init__(**lowerCAmelCase_ ) _a = hidden_size _a = is_hybrid if self.is_hybrid: if backbone_config is None: logger.info('''Initializing the config with a `BiT` backbone.''' ) _a = { '''global_padding''': '''same''', '''layer_type''': '''bottleneck''', '''depths''': [3, 4, 9], '''out_features''': ['''stage1''', '''stage2''', '''stage3'''], '''embedding_dynamic_padding''': True, } _a = BitConfig(**lowerCAmelCase_ ) elif isinstance(lowerCAmelCase_ , lowerCAmelCase_ ): logger.info('''Initializing the config with a `BiT` backbone.''' ) _a = BitConfig(**lowerCAmelCase_ ) elif isinstance(lowerCAmelCase_ , lowerCAmelCase_ ): _a = backbone_config else: raise ValueError( F'backbone_config must be a dictionary or a `PretrainedConfig`, got {backbone_config.__class__}.' ) _a = backbone_featmap_shape _a = neck_ignore_stages if readout_type != "project": raise ValueError('''Readout type must be \'project\' when using `DPT-hybrid` mode.''' ) else: _a = None _a = None _a = [] _a = num_hidden_layers _a = num_attention_heads _a = intermediate_size _a = hidden_act _a = hidden_dropout_prob _a = attention_probs_dropout_prob _a = initializer_range _a = layer_norm_eps _a = image_size _a = patch_size _a = num_channels _a = qkv_bias _a = backbone_out_indices if readout_type not in ["ignore", "add", "project"]: raise ValueError('''Readout_type must be one of [\'ignore\', \'add\', \'project\']''' ) _a = readout_type _a = reassemble_factors _a = neck_hidden_sizes _a = fusion_hidden_size _a = head_in_index _a = use_batch_norm_in_fusion_residual # auxiliary head attributes (semantic segmentation) _a = use_auxiliary_head _a = auxiliary_loss_weight _a = semantic_loss_ignore_index _a = semantic_classifier_dropout def __lowerCAmelCase ( self : Tuple ) -> List[str]: """simple docstring""" _a = copy.deepcopy(self.__dict__ ) if output["backbone_config"] is not None: _a = self.backbone_config.to_dict() _a = self.__class__.model_type return output
22
'''simple docstring''' lowerCAmelCase : Optional[Any] = { 'A': ['B', 'C', 'E'], 'B': ['A', 'D', 'E'], 'C': ['A', 'F', 'G'], 'D': ['B'], 'E': ['A', 'B', 'D'], 'F': ['C'], 'G': ['C'], } def A_( A : dict , A : str , A : Optional[Any]): UpperCamelCase = set() # keep track of all the paths to be checked UpperCamelCase = [[start]] # return path if start is goal if start == goal: return [start] # keeps looping until all possible paths have been checked while queue: # pop the first path from the queue UpperCamelCase = queue.pop(0) # get the last node from the path UpperCamelCase = path[-1] if node not in explored: UpperCamelCase = graph[node] # go through all neighbour nodes, construct a new path and # push it into the queue for neighbour in neighbours: UpperCamelCase = list(A) new_path.append(A) queue.append(A) # return path if neighbour is goal if neighbour == goal: return new_path # mark node as explored explored.add(A) # in case there's no path between the 2 nodes return [] def A_( A : dict , A : str , A : Tuple): if not graph or start not in graph or target not in graph: return -1 if start == target: return 0 UpperCamelCase = [start] UpperCamelCase = set(A) # Keep tab on distances from `start` node. UpperCamelCase = {start: 0, target: -1} while queue: UpperCamelCase = queue.pop(0) if node == target: UpperCamelCase = ( dist[node] if dist[target] == -1 else min(dist[target] , dist[node]) ) for adjacent in graph[node]: if adjacent not in visited: visited.add(A) queue.append(A) UpperCamelCase = dist[node] + 1 return dist[target] if __name__ == "__main__": print(bfs_shortest_path(demo_graph, 'G', 'D')) # returns ['G', 'C', 'A', 'B', 'D'] print(bfs_shortest_path_distance(demo_graph, 'G', 'D')) # returns 4
3
0
from bisect import bisect from itertools import accumulate def _snake_case (__lowercase , __lowercase , __lowercase , __lowercase): UpperCamelCase_ = sorted(zip(__lowercase , __lowercase) , key=lambda __lowercase: x[0] / x[1] , reverse=__lowercase) UpperCamelCase_ , UpperCamelCase_ = [i[0] for i in r], [i[1] for i in r] UpperCamelCase_ = list(accumulate(__lowercase)) UpperCamelCase_ = bisect(__lowercase , __lowercase) return ( 0 if k == 0 else sum(vl[:k]) + (w - acc[k - 1]) * (vl[k]) / (wt[k]) if k != n else sum(vl[:k]) ) if __name__ == "__main__": import doctest doctest.testmod()
23
'''simple docstring''' import copy import os import cva import numpy as np from matplotlib import pyplot as plt class SCREAMING_SNAKE_CASE__ : def __init__( self )-> Dict: '''simple docstring''' UpperCamelCase = '' UpperCamelCase = '' UpperCamelCase = [] UpperCamelCase = 0 UpperCamelCase = 256 UpperCamelCase = 0 UpperCamelCase = 0 UpperCamelCase = 0 UpperCamelCase = 0 def UpperCAmelCase_ ( self , A_ )-> str: '''simple docstring''' UpperCamelCase = cva.imread(A_ , 0 ) UpperCamelCase = copy.deepcopy(self.img ) UpperCamelCase , UpperCamelCase , UpperCamelCase = plt.hist(self.img.ravel() , 256 , [0, 256] , label='x' ) UpperCamelCase = np.sum(A_ ) for i in range(len(A_ ) ): UpperCamelCase = x[i] / self.k self.sk += prk UpperCamelCase = (self.L - 1) * self.sk if self.rem != 0: UpperCamelCase = int(last % last ) UpperCamelCase = int(last + 1 if self.rem >= 0.5 else last ) self.last_list.append(A_ ) UpperCamelCase = int(np.ma.count(self.img ) / self.img[1].size ) UpperCamelCase = self.img[1].size for i in range(self.number_of_cols ): for j in range(self.number_of_rows ): UpperCamelCase = self.img[j][i] if num != self.last_list[num]: UpperCamelCase = self.last_list[num] cva.imwrite('output_data/output.jpg' , self.img ) def UpperCAmelCase_ ( self )-> Any: '''simple docstring''' plt.hist(self.img.ravel() , 256 , [0, 256] ) def UpperCAmelCase_ ( self )-> Optional[Any]: '''simple docstring''' cva.imshow('Output-Image' , self.img ) cva.imshow('Input-Image' , self.original_image ) cva.waitKey(5000 ) cva.destroyAllWindows() if __name__ == "__main__": lowerCAmelCase : Union[str, Any] = os.path.join(os.path.basename(__file__), 'image_data/input.jpg') lowerCAmelCase : str = ConstantStretch() stretcher.stretch(file_path) stretcher.plot_histogram() stretcher.show_image()
3
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available UpperCAmelCase_ : Any = { '''configuration_rag''': ['''RagConfig'''], '''retrieval_rag''': ['''RagRetriever'''], '''tokenization_rag''': ['''RagTokenizer'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase_ : Union[str, Any] = [ '''RagModel''', '''RagPreTrainedModel''', '''RagSequenceForGeneration''', '''RagTokenForGeneration''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase_ : Union[str, Any] = [ '''TFRagModel''', '''TFRagPreTrainedModel''', '''TFRagSequenceForGeneration''', '''TFRagTokenForGeneration''', ] if TYPE_CHECKING: from .configuration_rag import RagConfig from .retrieval_rag import RagRetriever from .tokenization_rag import RagTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_rag import RagModel, RagPreTrainedModel, RagSequenceForGeneration, RagTokenForGeneration try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_rag import ( TFRagModel, TFRagPreTrainedModel, TFRagSequenceForGeneration, TFRagTokenForGeneration, ) else: import sys UpperCAmelCase_ : int = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
24
'''simple docstring''' import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase : int = logging.get_logger(__name__) lowerCAmelCase : Tuple = { 'microsoft/unispeech-sat-base-100h-libri-ft': ( 'https://huggingface.co/microsoft/unispeech-sat-base-100h-libri-ft/resolve/main/config.json' ), # See all UniSpeechSat models at https://huggingface.co/models?filter=unispeech_sat } class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = """unispeech-sat""" def __init__( self , A_=32 , A_=768 , A_=12 , A_=12 , A_=3072 , A_="gelu" , A_=0.1 , A_=0.1 , A_=0.1 , A_=0.0 , A_=0.0 , A_=0.1 , A_=0.1 , A_=0.02 , A_=1e-5 , A_="group" , A_="gelu" , A_=(512, 512, 512, 512, 512, 512, 512) , A_=(5, 2, 2, 2, 2, 2, 2) , A_=(10, 3, 3, 3, 3, 2, 2) , A_=False , A_=128 , A_=16 , A_=False , A_=True , A_=0.05 , A_=10 , A_=2 , A_=0.0 , A_=10 , A_=0 , A_=320 , A_=2 , A_=0.1 , A_=100 , A_=256 , A_=256 , A_=0.1 , A_="mean" , A_=False , A_=False , A_=256 , A_=(512, 512, 512, 512, 1500) , A_=(5, 3, 3, 1, 1) , A_=(1, 2, 3, 1, 1) , A_=512 , A_=0 , A_=1 , A_=2 , A_=504 , **A_ , )-> Tuple: '''simple docstring''' super().__init__(**A_ , pad_token_id=A_ , bos_token_id=A_ , eos_token_id=A_ ) UpperCamelCase = hidden_size UpperCamelCase = feat_extract_norm UpperCamelCase = feat_extract_activation UpperCamelCase = list(A_ ) UpperCamelCase = list(A_ ) UpperCamelCase = list(A_ ) UpperCamelCase = conv_bias UpperCamelCase = num_conv_pos_embeddings UpperCamelCase = num_conv_pos_embedding_groups UpperCamelCase = len(self.conv_dim ) UpperCamelCase = num_hidden_layers UpperCamelCase = intermediate_size UpperCamelCase = hidden_act UpperCamelCase = num_attention_heads UpperCamelCase = hidden_dropout UpperCamelCase = attention_dropout UpperCamelCase = activation_dropout UpperCamelCase = feat_proj_dropout UpperCamelCase = final_dropout UpperCamelCase = layerdrop UpperCamelCase = layer_norm_eps UpperCamelCase = initializer_range UpperCamelCase = vocab_size UpperCamelCase = num_clusters UpperCamelCase = do_stable_layer_norm UpperCamelCase = use_weighted_layer_sum 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)`, but is `len(config.conv_dim) =' F''' {len(self.conv_dim )}`, `len(config.conv_stride) = {len(self.conv_stride )}`,''' F''' `len(config.conv_kernel) = {len(self.conv_kernel )}`.''' ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 UpperCamelCase = apply_spec_augment UpperCamelCase = mask_time_prob UpperCamelCase = mask_time_length UpperCamelCase = mask_time_min_masks UpperCamelCase = mask_feature_prob UpperCamelCase = mask_feature_length UpperCamelCase = mask_feature_min_masks # parameters for pretraining with codevector quantized representations UpperCamelCase = num_codevectors_per_group UpperCamelCase = num_codevector_groups UpperCamelCase = contrastive_logits_temperature UpperCamelCase = feat_quantizer_dropout UpperCamelCase = num_negatives UpperCamelCase = codevector_dim UpperCamelCase = proj_codevector_dim UpperCamelCase = diversity_loss_weight # ctc loss UpperCamelCase = ctc_loss_reduction UpperCamelCase = ctc_zero_infinity # SequenceClassification-specific parameter. Feel free to ignore for other classes. UpperCamelCase = classifier_proj_size # XVector-specific parameters. Feel free to ignore for other classes. UpperCamelCase = list(A_ ) UpperCamelCase = list(A_ ) UpperCamelCase = list(A_ ) UpperCamelCase = xvector_output_dim @property def UpperCAmelCase_ ( self )-> Optional[Any]: '''simple docstring''' return functools.reduce(operator.mul , self.conv_stride , 1 )
3
0
from unittest.mock import Mock, patch from file_transfer.send_file import send_file @patch("socket.socket") @patch("builtins.open") def lowerCamelCase__ ( _a , _a): # ===== initialization ===== SCREAMING_SNAKE_CASE : int = Mock() SCREAMING_SNAKE_CASE : List[str] = conn, Mock() SCREAMING_SNAKE_CASE : str = iter([1, None]) SCREAMING_SNAKE_CASE : str = lambda _a: next(_a) # ===== invoke ===== send_file(filename="mytext.txt" , testing=_a) # ===== ensurance ===== sock.assert_called_once() sock.return_value.bind.assert_called_once() sock.return_value.listen.assert_called_once() sock.return_value.accept.assert_called_once() conn.recv.assert_called_once() file.return_value.__enter__.assert_called_once() file.return_value.__enter__.return_value.read.assert_called() conn.send.assert_called_once() conn.close.assert_called_once() sock.return_value.shutdown.assert_called_once() sock.return_value.close.assert_called_once()
25
'''simple docstring''' import inspect import unittest from datasets import load_dataset from packaging import version from transformers import BeitConfig from transformers.models.auto import get_values from transformers.testing_utils import require_torch, require_torch_multi_gpu, 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, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ( MODEL_MAPPING, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation, BeitModel, ) from transformers.models.beit.modeling_beit import BEIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): import PIL from PIL import Image from transformers import BeitImageProcessor class SCREAMING_SNAKE_CASE__ : def __init__( self , A_ , A_=100 , A_=13 , A_=30 , A_=2 , A_=3 , A_=True , A_=True , A_=32 , A_=4 , A_=4 , A_=37 , A_="gelu" , A_=0.1 , A_=0.1 , A_=10 , A_=0.02 , A_=3 , A_=None , A_=[0, 1, 2, 3] , )-> Any: '''simple docstring''' UpperCamelCase = parent UpperCamelCase = 100 UpperCamelCase = batch_size UpperCamelCase = image_size UpperCamelCase = patch_size UpperCamelCase = num_channels UpperCamelCase = is_training UpperCamelCase = use_labels UpperCamelCase = hidden_size UpperCamelCase = num_hidden_layers UpperCamelCase = num_attention_heads UpperCamelCase = intermediate_size UpperCamelCase = hidden_act UpperCamelCase = hidden_dropout_prob UpperCamelCase = attention_probs_dropout_prob UpperCamelCase = type_sequence_label_size UpperCamelCase = initializer_range UpperCamelCase = scope UpperCamelCase = out_indices UpperCamelCase = num_labels # in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) UpperCamelCase = (image_size // patch_size) ** 2 UpperCamelCase = num_patches + 1 def UpperCAmelCase_ ( self )-> List[str]: '''simple docstring''' UpperCamelCase = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) UpperCamelCase = None UpperCamelCase = None if self.use_labels: UpperCamelCase = ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCamelCase = ids_tensor([self.batch_size, self.image_size, self.image_size] , self.num_labels ) UpperCamelCase = self.get_config() return config, pixel_values, labels, pixel_labels def UpperCAmelCase_ ( self )-> Dict: '''simple docstring''' return BeitConfig( vocab_size=self.vocab_size , 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 , out_indices=self.out_indices , ) def UpperCAmelCase_ ( self , A_ , A_ , A_ , A_ )-> List[str]: '''simple docstring''' UpperCamelCase = BeitModel(config=A_ ) model.to(A_ ) model.eval() UpperCamelCase = model(A_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def UpperCAmelCase_ ( self , A_ , A_ , A_ , A_ )-> Any: '''simple docstring''' UpperCamelCase = BeitForMaskedImageModeling(config=A_ ) model.to(A_ ) model.eval() UpperCamelCase = model(A_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length - 1, self.vocab_size) ) def UpperCAmelCase_ ( self , A_ , A_ , A_ , A_ )-> Optional[int]: '''simple docstring''' UpperCamelCase = self.type_sequence_label_size UpperCamelCase = BeitForImageClassification(A_ ) model.to(A_ ) model.eval() UpperCamelCase = model(A_ , labels=A_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images UpperCamelCase = 1 UpperCamelCase = BeitForImageClassification(A_ ) model.to(A_ ) model.eval() UpperCamelCase = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) UpperCamelCase = model(A_ , labels=A_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def UpperCAmelCase_ ( self , A_ , A_ , A_ , A_ )-> Optional[Any]: '''simple docstring''' UpperCamelCase = self.num_labels UpperCamelCase = BeitForSemanticSegmentation(A_ ) model.to(A_ ) model.eval() UpperCamelCase = model(A_ ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2) ) UpperCamelCase = model(A_ , labels=A_ ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2) ) def UpperCAmelCase_ ( self )-> int: '''simple docstring''' UpperCamelCase = self.prepare_config_and_inputs() UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase = config_and_inputs UpperCamelCase = {'pixel_values': pixel_values} return config, inputs_dict @require_torch class SCREAMING_SNAKE_CASE__ ( snake_case_ , snake_case_ , unittest.TestCase): lowerCAmelCase_ = ( (BeitModel, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation) if is_torch_available() else () ) lowerCAmelCase_ = ( { """feature-extraction""": BeitModel, """image-classification""": BeitForImageClassification, """image-segmentation""": BeitForSemanticSegmentation, } if is_torch_available() else {} ) lowerCAmelCase_ = False lowerCAmelCase_ = False lowerCAmelCase_ = False def UpperCAmelCase_ ( self )-> Any: '''simple docstring''' UpperCamelCase = BeitModelTester(self ) UpperCamelCase = ConfigTester(self , config_class=A_ , has_text_modality=A_ , hidden_size=37 ) def UpperCAmelCase_ ( self )-> Union[str, Any]: '''simple docstring''' self.config_tester.run_common_tests() @unittest.skip(reason='BEiT does not use inputs_embeds' ) def UpperCAmelCase_ ( self )-> Optional[int]: '''simple docstring''' pass @require_torch_multi_gpu @unittest.skip(reason='BEiT has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`' ) def UpperCAmelCase_ ( self )-> Optional[Any]: '''simple docstring''' pass def UpperCAmelCase_ ( self )-> Tuple: '''simple docstring''' UpperCamelCase , UpperCamelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCamelCase = model_class(A_ ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) UpperCamelCase = model.get_output_embeddings() self.assertTrue(x is None or isinstance(A_ , nn.Linear ) ) def UpperCAmelCase_ ( self )-> List[Any]: '''simple docstring''' UpperCamelCase , UpperCamelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCamelCase = model_class(A_ ) UpperCamelCase = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic UpperCamelCase = [*signature.parameters.keys()] UpperCamelCase = ['pixel_values'] self.assertListEqual(arg_names[:1] , A_ ) def UpperCAmelCase_ ( self )-> Union[str, Any]: '''simple docstring''' UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*A_ ) def UpperCAmelCase_ ( self )-> List[Any]: '''simple docstring''' UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*A_ ) def UpperCAmelCase_ ( self )-> Any: '''simple docstring''' UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*A_ ) def UpperCAmelCase_ ( self )-> List[str]: '''simple docstring''' UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_semantic_segmentation(*A_ ) def UpperCAmelCase_ ( self )-> int: '''simple docstring''' if not self.model_tester.is_training: return UpperCamelCase , UpperCamelCase = self.model_tester.prepare_config_and_inputs_for_common() UpperCamelCase = True for model_class in self.all_model_classes: # we don't test BeitForMaskedImageModeling if model_class in [*get_values(A_ ), BeitForMaskedImageModeling]: continue UpperCamelCase = model_class(A_ ) model.to(A_ ) model.train() UpperCamelCase = self._prepare_for_class(A_ , A_ , return_labels=A_ ) UpperCamelCase = model(**A_ ).loss loss.backward() def UpperCAmelCase_ ( self )-> List[str]: '''simple docstring''' UpperCamelCase , UpperCamelCase = self.model_tester.prepare_config_and_inputs_for_common() if not self.model_tester.is_training: return UpperCamelCase = False UpperCamelCase = True for model_class in self.all_model_classes: # we don't test BeitForMaskedImageModeling if ( model_class in [*get_values(A_ ), BeitForMaskedImageModeling] or not model_class.supports_gradient_checkpointing ): continue UpperCamelCase = model_class(A_ ) model.gradient_checkpointing_enable() model.to(A_ ) model.train() UpperCamelCase = self._prepare_for_class(A_ , A_ , return_labels=A_ ) UpperCamelCase = model(**A_ ).loss loss.backward() def UpperCAmelCase_ ( self )-> Union[str, Any]: '''simple docstring''' UpperCamelCase , UpperCamelCase = self.model_tester.prepare_config_and_inputs_for_common() UpperCamelCase = _config_zero_init(A_ ) for model_class in self.all_model_classes: UpperCamelCase = model_class(config=A_ ) for name, param in model.named_parameters(): # we skip lambda parameters as these require special initial values # determined by config.layer_scale_init_value if "lambda" in name: continue if param.requires_grad: self.assertIn( ((param.data.mean() * 1e9).round() / 1e9).item() , [0.0, 1.0] , msg=F'''Parameter {name} of model {model_class} seems not properly initialized''' , ) @slow def UpperCAmelCase_ ( self )-> Dict: '''simple docstring''' for model_name in BEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCamelCase = BeitModel.from_pretrained(A_ ) self.assertIsNotNone(A_ ) def A_( ): UpperCamelCase = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png') return image @require_torch @require_vision class SCREAMING_SNAKE_CASE__ ( unittest.TestCase): @cached_property def UpperCAmelCase_ ( self )-> Optional[int]: '''simple docstring''' return BeitImageProcessor.from_pretrained('microsoft/beit-base-patch16-224' ) if is_vision_available() else None @slow def UpperCAmelCase_ ( self )-> Optional[Any]: '''simple docstring''' UpperCamelCase = BeitForMaskedImageModeling.from_pretrained('microsoft/beit-base-patch16-224-pt22k' ).to(A_ ) UpperCamelCase = self.default_image_processor UpperCamelCase = prepare_img() UpperCamelCase = image_processor(images=A_ , return_tensors='pt' ).pixel_values.to(A_ ) # prepare bool_masked_pos UpperCamelCase = torch.ones((1, 196) , dtype=torch.bool ).to(A_ ) # forward pass with torch.no_grad(): UpperCamelCase = model(pixel_values=A_ , bool_masked_pos=A_ ) UpperCamelCase = outputs.logits # verify the logits UpperCamelCase = torch.Size((1, 196, 8192) ) self.assertEqual(logits.shape , A_ ) UpperCamelCase = torch.tensor( [[-3.2_437, 0.5_072, -13.9_174], [-3.2_456, 0.4_948, -13.9_401], [-3.2_033, 0.5_121, -13.8_550]] ).to(A_ ) self.assertTrue(torch.allclose(logits[bool_masked_pos][:3, :3] , A_ , atol=1e-2 ) ) @slow def UpperCAmelCase_ ( self )-> Union[str, Any]: '''simple docstring''' UpperCamelCase = BeitForImageClassification.from_pretrained('microsoft/beit-base-patch16-224' ).to(A_ ) UpperCamelCase = self.default_image_processor UpperCamelCase = prepare_img() UpperCamelCase = image_processor(images=A_ , return_tensors='pt' ).to(A_ ) # forward pass with torch.no_grad(): UpperCamelCase = model(**A_ ) UpperCamelCase = outputs.logits # verify the logits UpperCamelCase = torch.Size((1, 1000) ) self.assertEqual(logits.shape , A_ ) UpperCamelCase = torch.tensor([-1.2_385, -1.0_987, -1.0_108] ).to(A_ ) self.assertTrue(torch.allclose(logits[0, :3] , A_ , atol=1e-4 ) ) UpperCamelCase = 281 self.assertEqual(logits.argmax(-1 ).item() , A_ ) @slow def UpperCAmelCase_ ( self )-> Optional[Any]: '''simple docstring''' UpperCamelCase = BeitForImageClassification.from_pretrained('microsoft/beit-large-patch16-224-pt22k-ft22k' ).to( A_ ) UpperCamelCase = self.default_image_processor UpperCamelCase = prepare_img() UpperCamelCase = image_processor(images=A_ , return_tensors='pt' ).to(A_ ) # forward pass with torch.no_grad(): UpperCamelCase = model(**A_ ) UpperCamelCase = outputs.logits # verify the logits UpperCamelCase = torch.Size((1, 21841) ) self.assertEqual(logits.shape , A_ ) UpperCamelCase = torch.tensor([1.6_881, -0.2_787, 0.5_901] ).to(A_ ) self.assertTrue(torch.allclose(logits[0, :3] , A_ , atol=1e-4 ) ) UpperCamelCase = 2396 self.assertEqual(logits.argmax(-1 ).item() , A_ ) @slow def UpperCAmelCase_ ( self )-> Any: '''simple docstring''' UpperCamelCase = BeitForSemanticSegmentation.from_pretrained('microsoft/beit-base-finetuned-ade-640-640' ) UpperCamelCase = model.to(A_ ) UpperCamelCase = BeitImageProcessor(do_resize=A_ , size=640 , do_center_crop=A_ ) UpperCamelCase = load_dataset('hf-internal-testing/fixtures_ade20k' , split='test' ) UpperCamelCase = Image.open(ds[0]['file'] ) UpperCamelCase = image_processor(images=A_ , return_tensors='pt' ).to(A_ ) # forward pass with torch.no_grad(): UpperCamelCase = model(**A_ ) UpperCamelCase = outputs.logits # verify the logits UpperCamelCase = torch.Size((1, 150, 160, 160) ) self.assertEqual(logits.shape , A_ ) UpperCamelCase = version.parse(PIL.__version__ ) < version.parse('9.0.0' ) if is_pillow_less_than_a: UpperCamelCase = torch.tensor( [ [[-4.9_225, -2.3_954, -3.0_522], [-2.8_822, -1.0_046, -1.7_561], [-2.9_549, -1.3_228, -2.1_347]], [[-5.8_168, -3.4_129, -4.0_778], [-3.8_651, -2.2_214, -3.0_277], [-3.8_356, -2.4_643, -3.3_535]], [[-0.0_078, 3.9_952, 4.0_754], [2.9_856, 4.6_944, 5.0_035], [3.2_413, 4.7_813, 4.9_969]], ] , device=A_ , ) else: UpperCamelCase = torch.tensor( [ [[-4.8_960, -2.3_688, -3.0_355], [-2.8_478, -0.9_836, -1.7_418], [-2.9_449, -1.3_332, -2.1_456]], [[-5.8_081, -3.4_124, -4.1_006], [-3.8_561, -2.2_081, -3.0_323], [-3.8_365, -2.4_601, -3.3_669]], [[-0.0_309, 3.9_868, 4.0_540], [2.9_640, 4.6_877, 4.9_976], [3.2_081, 4.7_690, 4.9_942]], ] , device=A_ , ) self.assertTrue(torch.allclose(logits[0, :3, :3, :3] , A_ , atol=1e-4 ) ) @slow def UpperCAmelCase_ ( self )-> Tuple: '''simple docstring''' UpperCamelCase = BeitForSemanticSegmentation.from_pretrained('microsoft/beit-base-finetuned-ade-640-640' ) UpperCamelCase = model.to(A_ ) UpperCamelCase = BeitImageProcessor(do_resize=A_ , size=640 , do_center_crop=A_ ) UpperCamelCase = load_dataset('hf-internal-testing/fixtures_ade20k' , split='test' ) UpperCamelCase = Image.open(ds[0]['file'] ) UpperCamelCase = image_processor(images=A_ , return_tensors='pt' ).to(A_ ) # forward pass with torch.no_grad(): UpperCamelCase = model(**A_ ) UpperCamelCase = outputs.logits.detach().cpu() UpperCamelCase = image_processor.post_process_semantic_segmentation(outputs=A_ , target_sizes=[(500, 300)] ) UpperCamelCase = torch.Size((500, 300) ) self.assertEqual(segmentation[0].shape , A_ ) UpperCamelCase = image_processor.post_process_semantic_segmentation(outputs=A_ ) UpperCamelCase = torch.Size((160, 160) ) self.assertEqual(segmentation[0].shape , A_ )
3
0
'''simple docstring''' import pytest from datasets.utils.sharding import _distribute_shards, _number_of_shards_in_gen_kwargs, _split_gen_kwargs @pytest.mark.parametrize( """kwargs, expected""" , [ ({"""num_shards""": 0, """max_num_jobs""": 1}, []), ({"""num_shards""": 10, """max_num_jobs""": 1}, [range(10 )]), ({"""num_shards""": 10, """max_num_jobs""": 10}, [range(_lowerCamelCase , i + 1 ) for i in range(10 )]), ({"""num_shards""": 1, """max_num_jobs""": 10}, [range(1 )]), ({"""num_shards""": 10, """max_num_jobs""": 3}, [range(0 , 4 ), range(4 , 7 ), range(7 , 10 )]), ({"""num_shards""": 3, """max_num_jobs""": 10}, [range(0 , 1 ), range(1 , 2 ), range(2 , 3 )]), ] , ) def _a ( _lowerCamelCase , _lowerCamelCase ) -> Union[str, Any]: """simple docstring""" __snake_case : Tuple = _distribute_shards(**_lowerCamelCase ) assert out == expected @pytest.mark.parametrize( """gen_kwargs, max_num_jobs, expected""" , [ ({"""foo""": 0}, 10, [{"""foo""": 0}]), ({"""shards""": [0, 1, 2, 3]}, 1, [{"""shards""": [0, 1, 2, 3]}]), ({"""shards""": [0, 1, 2, 3]}, 4, [{"""shards""": [0]}, {"""shards""": [1]}, {"""shards""": [2]}, {"""shards""": [3]}]), ({"""shards""": [0, 1]}, 4, [{"""shards""": [0]}, {"""shards""": [1]}]), ({"""shards""": [0, 1, 2, 3]}, 2, [{"""shards""": [0, 1]}, {"""shards""": [2, 3]}]), ] , ) def _a ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) -> Optional[int]: """simple docstring""" __snake_case : str = _split_gen_kwargs(_lowerCamelCase , _lowerCamelCase ) assert out == expected @pytest.mark.parametrize( """gen_kwargs, expected""" , [ ({"""foo""": 0}, 1), ({"""shards""": [0]}, 1), ({"""shards""": [0, 1, 2, 3]}, 4), ({"""shards""": [0, 1, 2, 3], """foo""": 0}, 4), ({"""shards""": [0, 1, 2, 3], """other""": (0, 1)}, 4), ({"""shards""": [0, 1, 2, 3], """shards2""": [0, 1]}, RuntimeError), ] , ) def _a ( _lowerCamelCase , _lowerCamelCase ) -> Tuple: """simple docstring""" if expected is RuntimeError: with pytest.raises(_lowerCamelCase ): _number_of_shards_in_gen_kwargs(_lowerCamelCase ) else: __snake_case : Tuple = _number_of_shards_in_gen_kwargs(_lowerCamelCase ) assert out == expected
26
'''simple docstring''' import enum import warnings from ..tokenization_utils import TruncationStrategy from ..utils import add_end_docstrings, is_tf_available, is_torch_available, logging from .base import PIPELINE_INIT_ARGS, Pipeline if is_tf_available(): import tensorflow as tf from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING if is_torch_available(): from ..models.auto.modeling_auto import MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING lowerCAmelCase : Dict = logging.get_logger(__name__) class SCREAMING_SNAKE_CASE__ ( enum.Enum): lowerCAmelCase_ = 0 lowerCAmelCase_ = 1 @add_end_docstrings(snake_case_) class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = """generated""" def __init__( self , *A_ , **A_ )-> Optional[int]: '''simple docstring''' super().__init__(*A_ , **A_ ) self.check_model_type( TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING if self.framework == 'tf' else MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING ) def UpperCAmelCase_ ( self , A_=None , A_=None , A_=None , A_=None , A_=None , A_=None , **A_ , )-> Optional[Any]: '''simple docstring''' UpperCamelCase = {} if truncation is not None: UpperCamelCase = truncation UpperCamelCase = generate_kwargs UpperCamelCase = {} if return_tensors is not None and return_type is None: UpperCamelCase = ReturnType.TENSORS if return_tensors else ReturnType.TEXT if return_type is not None: UpperCamelCase = return_type if clean_up_tokenization_spaces is not None: UpperCamelCase = clean_up_tokenization_spaces if stop_sequence is not None: UpperCamelCase = self.tokenizer.encode(A_ , add_special_tokens=A_ ) if len(A_ ) > 1: warnings.warn( 'Stopping on a multiple token sequence is not yet supported on transformers. The first token of' ' the stop sequence will be used as the stop sequence string in the interim.' ) UpperCamelCase = stop_sequence_ids[0] return preprocess_params, forward_params, postprocess_params def UpperCAmelCase_ ( self , A_ , A_ , A_ )-> Optional[int]: '''simple docstring''' return True def UpperCAmelCase_ ( self , *A_ , A_ )-> Any: '''simple docstring''' UpperCamelCase = self.model.config.prefix if self.model.config.prefix is not None else '' if isinstance(args[0] , A_ ): if self.tokenizer.pad_token_id is None: raise ValueError('Please make sure that the tokenizer has a pad_token_id when using a batch input' ) UpperCamelCase = ([prefix + arg for arg in args[0]],) UpperCamelCase = True elif isinstance(args[0] , A_ ): UpperCamelCase = (prefix + args[0],) UpperCamelCase = False else: raise ValueError( F''' `args[0]`: {args[0]} have the wrong format. The should be either of type `str` or type `list`''' ) UpperCamelCase = self.tokenizer(*A_ , padding=A_ , truncation=A_ , return_tensors=self.framework ) # This is produced by tokenizers but is an invalid generate kwargs if "token_type_ids" in inputs: del inputs["token_type_ids"] return inputs def __call__( self , *A_ , **A_ )-> Union[str, Any]: '''simple docstring''' UpperCamelCase = super().__call__(*A_ , **A_ ) if ( isinstance(args[0] , A_ ) and all(isinstance(A_ , A_ ) for el in args[0] ) and all(len(A_ ) == 1 for res in result ) ): return [res[0] for res in result] return result def UpperCAmelCase_ ( self , A_ , A_=TruncationStrategy.DO_NOT_TRUNCATE , **A_ )-> Any: '''simple docstring''' UpperCamelCase = self._parse_and_tokenize(A_ , truncation=A_ , **A_ ) return inputs def UpperCAmelCase_ ( self , A_ , **A_ )-> int: '''simple docstring''' if self.framework == "pt": UpperCamelCase , UpperCamelCase = model_inputs['input_ids'].shape elif self.framework == "tf": UpperCamelCase , UpperCamelCase = tf.shape(model_inputs['input_ids'] ).numpy() UpperCamelCase = generate_kwargs.get('min_length' , self.model.config.min_length ) UpperCamelCase = generate_kwargs.get('max_length' , self.model.config.max_length ) self.check_inputs(A_ , generate_kwargs['min_length'] , generate_kwargs['max_length'] ) UpperCamelCase = self.model.generate(**A_ , **A_ ) UpperCamelCase = output_ids.shape[0] if self.framework == "pt": UpperCamelCase = output_ids.reshape(A_ , out_b // in_b , *output_ids.shape[1:] ) elif self.framework == "tf": UpperCamelCase = tf.reshape(A_ , (in_b, out_b // in_b, *output_ids.shape[1:]) ) return {"output_ids": output_ids} def UpperCAmelCase_ ( self , A_ , A_=ReturnType.TEXT , A_=False )-> Optional[Any]: '''simple docstring''' UpperCamelCase = [] for output_ids in model_outputs["output_ids"][0]: if return_type == ReturnType.TENSORS: UpperCamelCase = {F'''{self.return_name}_token_ids''': output_ids} elif return_type == ReturnType.TEXT: UpperCamelCase = { F'''{self.return_name}_text''': self.tokenizer.decode( A_ , skip_special_tokens=A_ , clean_up_tokenization_spaces=A_ , ) } records.append(A_ ) return records @add_end_docstrings(snake_case_) class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = """summary""" def __call__( self , *A_ , **A_ )-> Optional[int]: '''simple docstring''' return super().__call__(*A_ , **A_ ) def UpperCAmelCase_ ( self , A_ , A_ , A_ )-> bool: '''simple docstring''' if max_length < min_length: logger.warning(F'''Your min_length={min_length} must be inferior than your max_length={max_length}.''' ) if input_length < max_length: logger.warning( F'''Your max_length is set to {max_length}, but your input_length is only {input_length}. Since this is ''' 'a summarization task, where outputs shorter than the input are typically wanted, you might ' F'''consider decreasing max_length manually, e.g. summarizer(\'...\', max_length={input_length//2})''' ) @add_end_docstrings(snake_case_) class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = """translation""" def UpperCAmelCase_ ( self , A_ , A_ , A_ )-> List[Any]: '''simple docstring''' if input_length > 0.9 * max_length: logger.warning( F'''Your input_length: {input_length} is bigger than 0.9 * max_length: {max_length}. You might consider ''' 'increasing your max_length manually, e.g. translator(\'...\', max_length=400)' ) return True def UpperCAmelCase_ ( self , *A_ , A_=TruncationStrategy.DO_NOT_TRUNCATE , A_=None , A_=None )-> Dict: '''simple docstring''' if getattr(self.tokenizer , '_build_translation_inputs' , A_ ): return self.tokenizer._build_translation_inputs( *A_ , return_tensors=self.framework , truncation=A_ , src_lang=A_ , tgt_lang=A_ ) else: return super()._parse_and_tokenize(*A_ , truncation=A_ ) def UpperCAmelCase_ ( self , A_=None , A_=None , **A_ )-> str: '''simple docstring''' UpperCamelCase , UpperCamelCase , UpperCamelCase = super()._sanitize_parameters(**A_ ) if src_lang is not None: UpperCamelCase = src_lang if tgt_lang is not None: UpperCamelCase = tgt_lang if src_lang is None and tgt_lang is None: # Backward compatibility, direct arguments use is preferred. UpperCamelCase = kwargs.get('task' , self.task ) UpperCamelCase = task.split('_' ) if task and len(A_ ) == 4: # translation, XX, to YY UpperCamelCase = items[1] UpperCamelCase = items[3] return preprocess_params, forward_params, postprocess_params def __call__( self , *A_ , **A_ )-> Any: '''simple docstring''' return super().__call__(*A_ , **A_ )
3
0
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 ViTConfig, ViTForImageClassification, ViTImageProcessor, ViTModel from transformers.utils import logging logging.set_verbosity_info() __A : str = logging.get_logger(__name__) def __lowerCAmelCase( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=False ) -> int: """simple docstring""" _A = [] for i in range(config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append((F"blocks.{i}.norm1.weight", F"vit.encoder.layer.{i}.layernorm_before.weight") ) rename_keys.append((F"blocks.{i}.norm1.bias", F"vit.encoder.layer.{i}.layernorm_before.bias") ) rename_keys.append((F"blocks.{i}.attn.proj.weight", F"vit.encoder.layer.{i}.attention.output.dense.weight") ) rename_keys.append((F"blocks.{i}.attn.proj.bias", F"vit.encoder.layer.{i}.attention.output.dense.bias") ) rename_keys.append((F"blocks.{i}.norm2.weight", F"vit.encoder.layer.{i}.layernorm_after.weight") ) rename_keys.append((F"blocks.{i}.norm2.bias", F"vit.encoder.layer.{i}.layernorm_after.bias") ) rename_keys.append((F"blocks.{i}.mlp.fc1.weight", F"vit.encoder.layer.{i}.intermediate.dense.weight") ) rename_keys.append((F"blocks.{i}.mlp.fc1.bias", F"vit.encoder.layer.{i}.intermediate.dense.bias") ) rename_keys.append((F"blocks.{i}.mlp.fc2.weight", F"vit.encoder.layer.{i}.output.dense.weight") ) rename_keys.append((F"blocks.{i}.mlp.fc2.bias", F"vit.encoder.layer.{i}.output.dense.bias") ) # projection layer + position embeddings rename_keys.extend( [ ('cls_token', 'vit.embeddings.cls_token'), ('patch_embed.proj.weight', 'vit.embeddings.patch_embeddings.projection.weight'), ('patch_embed.proj.bias', 'vit.embeddings.patch_embeddings.projection.bias'), ('pos_embed', 'vit.embeddings.position_embeddings'), ] ) if base_model: # layernorm + pooler rename_keys.extend( [ ('norm.weight', 'layernorm.weight'), ('norm.bias', 'layernorm.bias'), ] ) # if just the base model, we should remove "vit" from all keys that start with "vit" _A = [(pair[0], pair[1][4:]) if pair[1].startswith('vit' ) else pair for pair in rename_keys] else: # layernorm + classification head rename_keys.extend( [ ('norm.weight', 'vit.layernorm.weight'), ('norm.bias', 'vit.layernorm.bias'), ('head.weight', 'classifier.weight'), ('head.bias', 'classifier.bias'), ] ) return rename_keys def __lowerCAmelCase( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=False ) -> int: """simple docstring""" for i in range(config.num_hidden_layers ): if base_model: _A = '' else: _A = 'vit.' # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) _A = state_dict.pop(F"blocks.{i}.attn.qkv.weight" ) _A = state_dict.pop(F"blocks.{i}.attn.qkv.bias" ) # next, add query, keys and values (in that order) to the state dict _A = in_proj_weight[ : config.hidden_size, : ] _A = in_proj_bias[: config.hidden_size] _A = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] _A = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] _A = in_proj_weight[ -config.hidden_size :, : ] _A = in_proj_bias[-config.hidden_size :] def __lowerCAmelCase( _SCREAMING_SNAKE_CASE ) -> List[str]: """simple docstring""" _A = ['head.weight', 'head.bias'] for k in ignore_keys: state_dict.pop(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) def __lowerCAmelCase( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> List[str]: """simple docstring""" _A = dct.pop(_SCREAMING_SNAKE_CASE ) _A = val def __lowerCAmelCase( ) -> str: """simple docstring""" _A = 'http://images.cocodataset.org/val2017/000000039769.jpg' _A = Image.open(requests.get(_SCREAMING_SNAKE_CASE , stream=_SCREAMING_SNAKE_CASE ).raw ) return im @torch.no_grad() def __lowerCAmelCase( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=True ) -> Optional[Any]: """simple docstring""" _A = ViTConfig() # patch_size if model_name[-1] == "8": _A = 8 # set labels if required if not base_model: _A = 1_000 _A = 'huggingface/label-files' _A = 'imagenet-1k-id2label.json' _A = json.load(open(hf_hub_download(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , repo_type='dataset' ) , 'r' ) ) _A = {int(_SCREAMING_SNAKE_CASE ): v for k, v in idalabel.items()} _A = idalabel _A = {v: k for k, v in idalabel.items()} # size of the architecture if model_name in ["dino_vits8", "dino_vits16"]: _A = 384 _A = 1_536 _A = 12 _A = 6 # load original model from torch hub _A = torch.hub.load('facebookresearch/dino:main' , _SCREAMING_SNAKE_CASE ) original_model.eval() # load state_dict of original model, remove and rename some keys _A = original_model.state_dict() if base_model: remove_classification_head_(_SCREAMING_SNAKE_CASE ) _A = create_rename_keys(_SCREAMING_SNAKE_CASE , base_model=_SCREAMING_SNAKE_CASE ) for src, dest in rename_keys: rename_key(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) read_in_q_k_v(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # load HuggingFace model if base_model: _A = ViTModel(_SCREAMING_SNAKE_CASE , add_pooling_layer=_SCREAMING_SNAKE_CASE ).eval() else: _A = ViTForImageClassification(_SCREAMING_SNAKE_CASE ).eval() model.load_state_dict(_SCREAMING_SNAKE_CASE ) # Check outputs on an image, prepared by ViTImageProcessor _A = ViTImageProcessor() _A = image_processor(images=prepare_img() , return_tensors='pt' ) _A = encoding['pixel_values'] _A = model(_SCREAMING_SNAKE_CASE ) if base_model: _A = original_model(_SCREAMING_SNAKE_CASE ) assert torch.allclose(_SCREAMING_SNAKE_CASE , outputs.last_hidden_state[:, 0, :] , atol=1e-1 ) else: _A = original_model(_SCREAMING_SNAKE_CASE ) assert logits.shape == outputs.logits.shape assert torch.allclose(_SCREAMING_SNAKE_CASE , outputs.logits , atol=1e-3 ) Path(_SCREAMING_SNAKE_CASE ).mkdir(exist_ok=_SCREAMING_SNAKE_CASE ) print(F"Saving model {model_name} to {pytorch_dump_folder_path}" ) model.save_pretrained(_SCREAMING_SNAKE_CASE ) print(F"Saving image processor to {pytorch_dump_folder_path}" ) image_processor.save_pretrained(_SCREAMING_SNAKE_CASE ) if __name__ == "__main__": __A : List[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( "--model_name", default="dino_vitb16", type=str, help="Name of the model trained with DINO 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( "--base_model", action="store_true", help="Whether to only convert the base model (no projection head weights).", ) parser.set_defaults(base_model=True) __A : List[Any] = parser.parse_args() convert_vit_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.base_model)
27
'''simple docstring''' import inspect import os import unittest from dataclasses import dataclass import torch from accelerate import Accelerator, DistributedDataParallelKwargs, GradScalerKwargs from accelerate.state import AcceleratorState from accelerate.test_utils import execute_subprocess_async, require_cuda, require_multi_gpu from accelerate.utils import KwargsHandler @dataclass class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = 0 lowerCAmelCase_ = False lowerCAmelCase_ = 3.0 class SCREAMING_SNAKE_CASE__ ( unittest.TestCase): def UpperCAmelCase_ ( self )-> int: '''simple docstring''' self.assertDictEqual(MockClass().to_kwargs() , {} ) self.assertDictEqual(MockClass(a=2 ).to_kwargs() , {'a': 2} ) self.assertDictEqual(MockClass(a=2 , b=A_ ).to_kwargs() , {'a': 2, 'b': True} ) self.assertDictEqual(MockClass(a=2 , c=2.25 ).to_kwargs() , {'a': 2, 'c': 2.25} ) @require_cuda def UpperCAmelCase_ ( self )-> Dict: '''simple docstring''' UpperCamelCase = GradScalerKwargs(init_scale=1024 , growth_factor=2 ) AcceleratorState._reset_state() UpperCamelCase = Accelerator(mixed_precision='fp16' , kwargs_handlers=[scaler_handler] ) print(accelerator.use_fpaa ) UpperCamelCase = accelerator.scaler # Check the kwargs have been applied self.assertEqual(scaler._init_scale , 1_024.0 ) self.assertEqual(scaler._growth_factor , 2.0 ) # Check the other values are at the default self.assertEqual(scaler._backoff_factor , 0.5 ) self.assertEqual(scaler._growth_interval , 2000 ) self.assertEqual(scaler._enabled , A_ ) @require_multi_gpu def UpperCAmelCase_ ( self )-> Dict: '''simple docstring''' UpperCamelCase = ['torchrun', F'''--nproc_per_node={torch.cuda.device_count()}''', inspect.getfile(self.__class__ )] execute_subprocess_async(A_ , env=os.environ.copy() ) if __name__ == "__main__": lowerCAmelCase : Tuple = DistributedDataParallelKwargs(bucket_cap_mb=15, find_unused_parameters=True) lowerCAmelCase : List[str] = Accelerator(kwargs_handlers=[ddp_scaler]) lowerCAmelCase : List[Any] = torch.nn.Linear(1_00, 2_00) lowerCAmelCase : int = accelerator.prepare(model) # Check the values changed in kwargs lowerCAmelCase : Dict = '' lowerCAmelCase : Dict = model.bucket_bytes_cap // (10_24 * 10_24) if observed_bucket_cap_map != 15: error_msg += f"Kwargs badly passed, should have `15` but found {observed_bucket_cap_map}.\n" if model.find_unused_parameters is not True: error_msg += f"Kwargs badly passed, should have `True` but found {model.find_unused_parameters}.\n" # Check the values of the defaults if model.dim != 0: error_msg += f"Default value not respected, should have `0` but found {model.dim}.\n" if model.broadcast_buffers is not True: error_msg += f"Default value not respected, should have `True` but found {model.broadcast_buffers}.\n" if model.gradient_as_bucket_view is not False: error_msg += f"Default value not respected, should have `False` but found {model.gradient_as_bucket_view}.\n" # Raise error at the end to make sure we don't stop at the first failure. if len(error_msg) > 0: raise ValueError(error_msg)
3
0
'''simple docstring''' from __future__ import annotations import string from itertools import cycle, product from pathlib import Path UpperCamelCase_ = ( string.ascii_letters + string.digits + string.punctuation + string.whitespace ) UpperCamelCase_ = [ord(letter) for letter in string.ascii_lowercase] UpperCamelCase_ = {ord(char) for char in VALID_CHARS} UpperCamelCase_ = ["the", "be", "to", "of", "and", "in", "that", "have"] def lowercase__( __UpperCamelCase: list[int] ,__UpperCamelCase: tuple[int, ...] ): """simple docstring""" SCREAMING_SNAKE_CASE : str = "" SCREAMING_SNAKE_CASE : int SCREAMING_SNAKE_CASE : int SCREAMING_SNAKE_CASE : int for keychar, cipherchar in zip(cycle(__UpperCamelCase ) ,__UpperCamelCase ): SCREAMING_SNAKE_CASE : Union[str, Any] = cipherchar ^ keychar if decodedchar not in VALID_INTS: return None decoded += chr(__UpperCamelCase ) return decoded def lowercase__( __UpperCamelCase: list[int] ): """simple docstring""" SCREAMING_SNAKE_CASE : list[str] = [] for key in product(__UpperCamelCase ,repeat=3 ): SCREAMING_SNAKE_CASE : List[str] = try_key(__UpperCamelCase ,__UpperCamelCase ) if encoded is not None: possibles.append(__UpperCamelCase ) return possibles def lowercase__( __UpperCamelCase: list[str] ,__UpperCamelCase: str ): """simple docstring""" return [possible for possible in possibles if common_word in possible.lower()] def lowercase__( __UpperCamelCase: str = "p059_cipher.txt" ): """simple docstring""" SCREAMING_SNAKE_CASE : list[int] SCREAMING_SNAKE_CASE : list[str] SCREAMING_SNAKE_CASE : str SCREAMING_SNAKE_CASE : str SCREAMING_SNAKE_CASE : str = Path(__UpperCamelCase ).parent.joinpath(__UpperCamelCase ).read_text(encoding='utf-8' ) SCREAMING_SNAKE_CASE : Tuple = [int(__UpperCamelCase ) for number in data.strip().split(',' )] SCREAMING_SNAKE_CASE : Dict = filter_valid_chars(__UpperCamelCase ) for common_word in COMMON_WORDS: SCREAMING_SNAKE_CASE : List[Any] = filter_common_word(__UpperCamelCase ,__UpperCamelCase ) if len(__UpperCamelCase ) == 1: break SCREAMING_SNAKE_CASE : Optional[int] = possibles[0] return sum(ord(__UpperCamelCase ) for char in decoded_text ) if __name__ == "__main__": print(F"""{solution() = }""")
28
'''simple docstring''' from typing import Callable, List, Optional, Tuple, Union import torch from transformers import CLIPTextModel, CLIPTokenizer from ...configuration_utils import ConfigMixin, register_to_config from ...models import ModelMixin, TransformeraDModel, VQModel from ...schedulers import VQDiffusionScheduler from ...utils import logging from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput lowerCAmelCase : Optional[Any] = logging.get_logger(__name__) # pylint: disable=invalid-name class SCREAMING_SNAKE_CASE__ ( snake_case_ , snake_case_): @register_to_config def __init__( self , A_ , A_ = None , A_ = None )-> Tuple: '''simple docstring''' super().__init__() UpperCamelCase = learnable if self.learnable: assert hidden_size is not None, "learnable=True requires `hidden_size` to be set" assert length is not None, "learnable=True requires `length` to be set" UpperCamelCase = torch.zeros(A_ , A_ ) else: UpperCamelCase = None UpperCamelCase = torch.nn.Parameter(A_ ) class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = 42 lowerCAmelCase_ = 42 lowerCAmelCase_ = 42 lowerCAmelCase_ = 42 lowerCAmelCase_ = 42 lowerCAmelCase_ = 42 def __init__( self , A_ , A_ , A_ , A_ , A_ , A_ , )-> Union[str, Any]: '''simple docstring''' super().__init__() self.register_modules( vqvae=A_ , transformer=A_ , text_encoder=A_ , tokenizer=A_ , scheduler=A_ , learned_classifier_free_sampling_embeddings=A_ , ) def UpperCAmelCase_ ( self , A_ , A_ , A_ )-> Tuple: '''simple docstring''' UpperCamelCase = len(A_ ) if isinstance(A_ , A_ ) else 1 # get prompt text embeddings UpperCamelCase = self.tokenizer( A_ , padding='max_length' , max_length=self.tokenizer.model_max_length , return_tensors='pt' , ) UpperCamelCase = text_inputs.input_ids if text_input_ids.shape[-1] > self.tokenizer.model_max_length: UpperCamelCase = self.tokenizer.batch_decode(text_input_ids[:, self.tokenizer.model_max_length :] ) logger.warning( 'The following part of your input was truncated because CLIP can only handle sequences up to' F''' {self.tokenizer.model_max_length} tokens: {removed_text}''' ) UpperCamelCase = text_input_ids[:, : self.tokenizer.model_max_length] UpperCamelCase = self.text_encoder(text_input_ids.to(self.device ) )[0] # NOTE: This additional step of normalizing the text embeddings is from VQ-Diffusion. # While CLIP does normalize the pooled output of the text transformer when combining # the image and text embeddings, CLIP does not directly normalize the last hidden state. # # CLIP normalizing the pooled output. # https://github.com/huggingface/transformers/blob/d92e22d1f28324f513f3080e5c47c071a3916721/src/transformers/models/clip/modeling_clip.py#L1052-L1053 UpperCamelCase = prompt_embeds / prompt_embeds.norm(dim=-1 , keepdim=A_ ) # duplicate text embeddings for each generation per prompt UpperCamelCase = prompt_embeds.repeat_interleave(A_ , dim=0 ) if do_classifier_free_guidance: if self.learned_classifier_free_sampling_embeddings.learnable: UpperCamelCase = self.learned_classifier_free_sampling_embeddings.embeddings UpperCamelCase = negative_prompt_embeds.unsqueeze(0 ).repeat(A_ , 1 , 1 ) else: UpperCamelCase = [''] * batch_size UpperCamelCase = text_input_ids.shape[-1] UpperCamelCase = self.tokenizer( A_ , padding='max_length' , max_length=A_ , truncation=A_ , return_tensors='pt' , ) UpperCamelCase = self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # See comment for normalizing text embeddings UpperCamelCase = negative_prompt_embeds / negative_prompt_embeds.norm(dim=-1 , keepdim=A_ ) # duplicate unconditional embeddings for each generation per prompt, using mps friendly method UpperCamelCase = negative_prompt_embeds.shape[1] UpperCamelCase = negative_prompt_embeds.repeat(1 , A_ , 1 ) UpperCamelCase = negative_prompt_embeds.view(batch_size * num_images_per_prompt , A_ , -1 ) # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes UpperCamelCase = torch.cat([negative_prompt_embeds, prompt_embeds] ) return prompt_embeds @torch.no_grad() def __call__( self , A_ , A_ = 100 , A_ = 5.0 , A_ = 1.0 , A_ = 1 , A_ = None , A_ = None , A_ = "pil" , A_ = True , A_ = None , A_ = 1 , )-> Union[ImagePipelineOutput, Tuple]: '''simple docstring''' if isinstance(A_ , A_ ): UpperCamelCase = 1 elif isinstance(A_ , A_ ): UpperCamelCase = len(A_ ) else: raise ValueError(F'''`prompt` has to be of type `str` or `list` but is {type(A_ )}''' ) UpperCamelCase = batch_size * num_images_per_prompt UpperCamelCase = guidance_scale > 1.0 UpperCamelCase = self._encode_prompt(A_ , A_ , A_ ) 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_ )}.''' ) # get the initial completely masked latents unless the user supplied it UpperCamelCase = (batch_size, self.transformer.num_latent_pixels) if latents is None: UpperCamelCase = self.transformer.num_vector_embeds - 1 UpperCamelCase = torch.full(A_ , A_ ).to(self.device ) else: if latents.shape != latents_shape: raise ValueError(F'''Unexpected latents shape, got {latents.shape}, expected {latents_shape}''' ) if (latents < 0).any() or (latents >= self.transformer.num_vector_embeds).any(): raise ValueError( 'Unexpected latents value(s). All latents be valid embedding indices i.e. in the range 0,' F''' {self.transformer.num_vector_embeds - 1} (inclusive).''' ) UpperCamelCase = latents.to(self.device ) # set timesteps self.scheduler.set_timesteps(A_ , device=self.device ) UpperCamelCase = self.scheduler.timesteps.to(self.device ) UpperCamelCase = latents for i, t in enumerate(self.progress_bar(A_ ) ): # expand the sample if we are doing classifier free guidance UpperCamelCase = torch.cat([sample] * 2 ) if do_classifier_free_guidance else sample # predict the un-noised image # model_output == `log_p_x_0` UpperCamelCase = self.transformer(A_ , encoder_hidden_states=A_ , timestep=A_ ).sample if do_classifier_free_guidance: UpperCamelCase , UpperCamelCase = model_output.chunk(2 ) UpperCamelCase = model_output_uncond + guidance_scale * (model_output_text - model_output_uncond) model_output -= torch.logsumexp(A_ , dim=1 , keepdim=A_ ) UpperCamelCase = self.truncate(A_ , A_ ) # remove `log(0)`'s (`-inf`s) UpperCamelCase = model_output.clamp(-70 ) # compute the previous noisy sample x_t -> x_t-1 UpperCamelCase = self.scheduler.step(A_ , timestep=A_ , sample=A_ , generator=A_ ).prev_sample # call the callback, if provided if callback is not None and i % callback_steps == 0: callback(A_ , A_ , A_ ) UpperCamelCase = self.vqvae.config.vq_embed_dim UpperCamelCase = (batch_size, self.transformer.height, self.transformer.width, embedding_channels) UpperCamelCase = self.vqvae.quantize.get_codebook_entry(A_ , shape=A_ ) UpperCamelCase = self.vqvae.decode(A_ , force_not_quantize=A_ ).sample UpperCamelCase = (image / 2 + 0.5).clamp(0 , 1 ) UpperCamelCase = image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": UpperCamelCase = self.numpy_to_pil(A_ ) if not return_dict: return (image,) return ImagePipelineOutput(images=A_ ) def UpperCAmelCase_ ( self , A_ , A_ )-> torch.FloatTensor: '''simple docstring''' UpperCamelCase , UpperCamelCase = torch.sort(A_ , 1 , descending=A_ ) UpperCamelCase = torch.exp(A_ ) UpperCamelCase = sorted_p_x_0.cumsum(dim=1 ) < truncation_rate # Ensure that at least the largest probability is not zeroed out UpperCamelCase = torch.full_like(keep_mask[:, 0:1, :] , A_ ) UpperCamelCase = torch.cat((all_true, keep_mask) , dim=1 ) UpperCamelCase = keep_mask[:, :-1, :] UpperCamelCase = keep_mask.gather(1 , indices.argsort(1 ) ) UpperCamelCase = log_p_x_0.clone() UpperCamelCase = -torch.inf # -inf = log(0) return rv
3
0
"""simple docstring""" import requests def lowercase ( lowerCAmelCase__ ,lowerCAmelCase__ ): lowerCamelCase_ = {'''Content-Type''': '''application/json'''} lowerCamelCase_ = requests.post(lowerCAmelCase__ ,json={'''text''': message_body} ,headers=lowerCAmelCase__ ) if response.status_code != 200: lowerCamelCase_ = ( '''Request to slack returned an error ''' f"{response.status_code}, the response is:\n{response.text}" ) raise ValueError(lowerCAmelCase__ ) if __name__ == "__main__": # Set the slack url to the one provided by Slack when you create the webhook at # https://my.slack.com/services/new/incoming-webhook/ send_slack_message("""<YOUR MESSAGE BODY>""", """<SLACK CHANNEL URL>""")
29
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCAmelCase : Union[str, Any] = { 'configuration_git': ['GIT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'GitConfig', 'GitVisionConfig'], 'processing_git': ['GitProcessor'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : List[Any] = [ 'GIT_PRETRAINED_MODEL_ARCHIVE_LIST', 'GitForCausalLM', 'GitModel', 'GitPreTrainedModel', 'GitVisionModel', ] if TYPE_CHECKING: from .configuration_git import GIT_PRETRAINED_CONFIG_ARCHIVE_MAP, GitConfig, GitVisionConfig from .processing_git import GitProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_git import ( GIT_PRETRAINED_MODEL_ARCHIVE_LIST, GitForCausalLM, GitModel, GitPreTrainedModel, GitVisionModel, ) else: import sys lowerCAmelCase : Optional[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
3
0
from __future__ import annotations from math import pow, sqrt def lowerCamelCase__ ( _lowercase , _lowercase , _lowercase ): '''simple docstring''' if (resistance, reactance, impedance).count(0 ) != 1: raise ValueError('''One and only one argument must be 0''' ) if resistance == 0: return {"resistance": sqrt(pow(_lowercase , 2 ) - pow(_lowercase , 2 ) )} elif reactance == 0: return {"reactance": sqrt(pow(_lowercase , 2 ) - pow(_lowercase , 2 ) )} elif impedance == 0: return {"impedance": sqrt(pow(_lowercase , 2 ) + pow(_lowercase , 2 ) )} else: raise ValueError('''Exactly one argument must be 0''' ) if __name__ == "__main__": import doctest doctest.testmod()
30
'''simple docstring''' import uuid from typing import Any, Dict, List, Optional, Union from ..utils import add_end_docstrings, is_tf_available, is_torch_available, logging from .base import PIPELINE_INIT_ARGS, Pipeline if is_tf_available(): import tensorflow as tf if is_torch_available(): import torch lowerCAmelCase : Optional[Any] = logging.get_logger(__name__) class SCREAMING_SNAKE_CASE__ : def __init__( self , A_ = None , A_ = None , A_=None , A_=None )-> Optional[Any]: '''simple docstring''' if not conversation_id: UpperCamelCase = uuid.uuida() if past_user_inputs is None: UpperCamelCase = [] if generated_responses is None: UpperCamelCase = [] UpperCamelCase = conversation_id UpperCamelCase = past_user_inputs UpperCamelCase = generated_responses UpperCamelCase = text def __eq__( self , A_ )-> List[Any]: '''simple docstring''' if not isinstance(A_ , A_ ): return False if self.uuid == other.uuid: return True return ( self.new_user_input == other.new_user_input and self.past_user_inputs == other.past_user_inputs and self.generated_responses == other.generated_responses ) def UpperCAmelCase_ ( self , A_ , A_ = False )-> int: '''simple docstring''' if self.new_user_input: if overwrite: logger.warning( F'''User input added while unprocessed input was existing: "{self.new_user_input}" was overwritten ''' F'''with: "{text}".''' ) UpperCamelCase = text else: logger.warning( F'''User input added while unprocessed input was existing: "{self.new_user_input}" new input ''' F'''ignored: "{text}". Set `overwrite` to True to overwrite unprocessed user input''' ) else: UpperCamelCase = text def UpperCAmelCase_ ( self )-> Any: '''simple docstring''' if self.new_user_input: self.past_user_inputs.append(self.new_user_input ) UpperCamelCase = None def UpperCAmelCase_ ( self , A_ )-> int: '''simple docstring''' self.generated_responses.append(A_ ) def UpperCAmelCase_ ( self )-> List[str]: '''simple docstring''' for user_input, generated_response in zip(self.past_user_inputs , self.generated_responses ): yield True, user_input yield False, generated_response if self.new_user_input: yield True, self.new_user_input def __repr__( self )-> Any: '''simple docstring''' UpperCamelCase = F'''Conversation id: {self.uuid} \n''' for is_user, text in self.iter_texts(): UpperCamelCase = 'user' if is_user else 'bot' output += F'''{name} >> {text} \n''' return output @add_end_docstrings( snake_case_ , R""" min_length_for_response (`int`, *optional*, defaults to 32): The minimum length (in number of tokens) for a response. minimum_tokens (`int`, *optional*, defaults to 10): The minimum length of tokens to leave for a response. """ , ) class SCREAMING_SNAKE_CASE__ ( snake_case_): def __init__( self , *A_ , **A_ )-> Any: '''simple docstring''' super().__init__(*A_ , **A_ ) if self.tokenizer.pad_token_id is None: UpperCamelCase = self.tokenizer.eos_token def UpperCAmelCase_ ( self , A_=None , A_=None , A_=None , **A_ )-> Union[str, Any]: '''simple docstring''' UpperCamelCase = {} UpperCamelCase = {} UpperCamelCase = {} if min_length_for_response is not None: UpperCamelCase = min_length_for_response if minimum_tokens is not None: UpperCamelCase = minimum_tokens if "max_length" in generate_kwargs: UpperCamelCase = generate_kwargs['max_length'] # self.max_length = generate_kwargs.get("max_length", self.model.config.max_length) if clean_up_tokenization_spaces is not None: UpperCamelCase = clean_up_tokenization_spaces if generate_kwargs: forward_params.update(A_ ) return preprocess_params, forward_params, postprocess_params def __call__( self , A_ , A_=0 , **A_ )-> Any: '''simple docstring''' UpperCamelCase = super().__call__(A_ , num_workers=A_ , **A_ ) if isinstance(A_ , A_ ) and len(A_ ) == 1: return outputs[0] return outputs def UpperCAmelCase_ ( self , A_ , A_=32 )-> Dict[str, Any]: '''simple docstring''' if not isinstance(A_ , A_ ): raise ValueError('ConversationalPipeline, expects Conversation as inputs' ) if conversation.new_user_input is None: raise ValueError( F'''Conversation with UUID {type(conversation.uuid )} does not contain new user input to process. ''' 'Add user inputs with the conversation\'s `add_user_input` method' ) if hasattr(self.tokenizer , '_build_conversation_input_ids' ): UpperCamelCase = self.tokenizer._build_conversation_input_ids(A_ ) else: # If the tokenizer cannot handle conversations, we default to only the old version UpperCamelCase = self._legacy_parse_and_tokenize(A_ ) if self.framework == "pt": UpperCamelCase = torch.LongTensor([input_ids] ) elif self.framework == "tf": UpperCamelCase = tf.constant([input_ids] ) return {"input_ids": input_ids, "conversation": conversation} def UpperCAmelCase_ ( self , A_ , A_=10 , **A_ )-> Optional[Any]: '''simple docstring''' UpperCamelCase = generate_kwargs.get('max_length' , self.model.config.max_length ) UpperCamelCase = model_inputs['input_ids'].shape[1] if max_length - minimum_tokens < n: logger.warning(F'''Conversation input is to long ({n}), trimming it to ({max_length} - {minimum_tokens})''' ) UpperCamelCase = max_length - minimum_tokens UpperCamelCase = model_inputs['input_ids'][:, -trim:] if "attention_mask" in model_inputs: UpperCamelCase = model_inputs['attention_mask'][:, -trim:] UpperCamelCase = model_inputs.pop('conversation' ) UpperCamelCase = max_length UpperCamelCase = self.model.generate(**A_ , **A_ ) if self.model.config.is_encoder_decoder: UpperCamelCase = 1 else: UpperCamelCase = n return {"output_ids": output_ids[:, start_position:], "conversation": conversation} def UpperCAmelCase_ ( self , A_ , A_=True )-> Tuple: '''simple docstring''' UpperCamelCase = model_outputs['output_ids'] UpperCamelCase = self.tokenizer.decode( output_ids[0] , skip_special_tokens=A_ , clean_up_tokenization_spaces=A_ , ) UpperCamelCase = model_outputs['conversation'] conversation.mark_processed() conversation.append_response(A_ ) return conversation def UpperCAmelCase_ ( self , A_ )-> Dict: '''simple docstring''' UpperCamelCase = self.tokenizer.eos_token_id UpperCamelCase = [] for is_user, text in conversation.iter_texts(): if eos_token_id is not None: input_ids.extend(self.tokenizer.encode(A_ , add_special_tokens=A_ ) + [eos_token_id] ) else: input_ids.extend(self.tokenizer.encode(A_ , add_special_tokens=A_ ) ) if len(A_ ) > self.tokenizer.model_max_length: UpperCamelCase = input_ids[-self.tokenizer.model_max_length :] return input_ids
3
0
def UpperCAmelCase_ ( __UpperCAmelCase : int = 4_00_00_00 ) -> int: SCREAMING_SNAKE_CASE_ = [] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = 0, 1 while b <= n: if b % 2 == 0: even_fibs.append(__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = b, a + b return sum(__UpperCAmelCase ) if __name__ == "__main__": print(f'''{solution() = }''')
31
'''simple docstring''' import sys import webbrowser import requests from bsa import BeautifulSoup from fake_useragent import UserAgent if __name__ == "__main__": print('Googling.....') lowerCAmelCase : List[Any] = 'https://www.google.com/search?q=' + ' '.join(sys.argv[1:]) lowerCAmelCase : List[Any] = requests.get(url, headers={'UserAgent': UserAgent().random}) # res.raise_for_status() with open('project1a.html', 'wb') as out_file: # only for knowing the class for data in res.iter_content(1_00_00): out_file.write(data) lowerCAmelCase : Tuple = BeautifulSoup(res.text, 'html.parser') lowerCAmelCase : List[Any] = list(soup.select('.eZt8xd'))[:5] print(len(links)) for link in links: if link.text == "Maps": webbrowser.open(link.get('href')) else: webbrowser.open(f"""https://google.com{link.get('href')}""")
3
0
import copy from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Audio, Features, Value from .base import TaskTemplate @dataclass(frozen=A__ ) class __UpperCamelCase ( A__ ): __A : str = field(default="""automatic-speech-recognition""" , metadata={"""include_in_asdict_even_if_is_default""": True} ) __A : ClassVar[Features] = Features({"""audio""": Audio()} ) __A : ClassVar[Features] = Features({"""transcription""": Value("""string""" )} ) __A : str = "audio" __A : str = "transcription" def UpperCamelCase( self , _UpperCamelCase ): if self.audio_column not in features: raise ValueError(f'''Column {self.audio_column} is not present in features.''' ) if not isinstance(features[self.audio_column] , _UpperCamelCase ): raise ValueError(f'''Column {self.audio_column} is not an Audio type.''' ) _UpperCAmelCase = copy.deepcopy(self ) _UpperCAmelCase = self.input_schema.copy() _UpperCAmelCase = features[self.audio_column] _UpperCAmelCase = input_schema return task_template @property def UpperCamelCase( self ): return {self.audio_column: "audio", self.transcription_column: "transcription"}
32
'''simple docstring''' import numpy as np def A_( A : str , A : Optional[Any] , A : Tuple , A : Optional[int] , A : str): UpperCamelCase = int(np.ceil((x_end - xa) / h)) UpperCamelCase = np.zeros((n + 1,)) UpperCamelCase = ya UpperCamelCase = xa for k in range(A): UpperCamelCase = f(A , y[k]) UpperCamelCase = f(x + 0.5 * h , y[k] + 0.5 * h * ka) UpperCamelCase = f(x + 0.5 * h , y[k] + 0.5 * h * ka) UpperCamelCase = f(x + h , y[k] + h * ka) UpperCamelCase = y[k] + (1 / 6) * h * (ka + 2 * ka + 2 * ka + ka) x += h return y if __name__ == "__main__": import doctest doctest.testmod()
3
0
def SCREAMING_SNAKE_CASE ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) -> int: def count_of_possible_combinations(__lowerCAmelCase ) -> int: if target < 0: return 0 if target == 0: return 1 return sum(count_of_possible_combinations(target - item ) for item in array ) return count_of_possible_combinations(__lowerCAmelCase ) def SCREAMING_SNAKE_CASE ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) -> int: def count_of_possible_combinations_with_dp_array( __lowerCAmelCase , __lowerCAmelCase ) -> int: if target < 0: return 0 if target == 0: return 1 if dp_array[target] != -1: return dp_array[target] snake_case__ = sum( count_of_possible_combinations_with_dp_array(target - item , __lowerCAmelCase ) for item in array ) snake_case__ = answer return answer snake_case__ = [-1] * (target + 1) return count_of_possible_combinations_with_dp_array(__lowerCAmelCase , __lowerCAmelCase ) def SCREAMING_SNAKE_CASE ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) -> int: snake_case__ = [0] * (target + 1) snake_case__ = 1 for i in range(1 , target + 1 ): for j in range(__lowerCAmelCase ): if i - array[j] >= 0: dp_array[i] += dp_array[i - array[j]] return dp_array[target] if __name__ == "__main__": import doctest doctest.testmod() lowerCamelCase__ : Any = 3 lowerCamelCase__ : int = 5 lowerCamelCase__ : List[Any] = [1, 2, 5] print(combination_sum_iv(n, array, target))
33
'''simple docstring''' from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Features, Value from .base import TaskTemplate @dataclass(frozen=snake_case_) class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = field(default="""language-modeling""" , metadata={"""include_in_asdict_even_if_is_default""": True}) lowerCAmelCase_ = Features({"""text""": Value("""string""")}) lowerCAmelCase_ = Features({}) lowerCAmelCase_ = "text" @property def UpperCAmelCase_ ( self )-> Dict[str, str]: '''simple docstring''' return {self.text_column: "text"}
3
0
"""simple docstring""" from __future__ import annotations from scipy.special import comb # type: ignore class snake_case_ : """simple docstring""" def __init__( self , lowerCamelCase_) -> Optional[Any]: UpperCamelCase = list_of_points # Degree determines the flexibility of the curve. # Degree = 1 will produce a straight line. UpperCamelCase = len(lowerCamelCase_) - 1 def UpperCAmelCase__ ( self , lowerCamelCase_) -> list[float]: assert 0 <= t <= 1, "Time t must be between 0 and 1." UpperCamelCase = [] for i in range(len(self.list_of_points)): # basis function for each i output_values.append( comb(self.degree , lowerCamelCase_) * ((1 - t) ** (self.degree - i)) * (t**i)) # the basis must sum up to 1 for it to produce a valid Bezier curve. assert round(sum(lowerCamelCase_) , 5) == 1 return output_values def UpperCAmelCase__ ( self , lowerCamelCase_) -> tuple[float, float]: assert 0 <= t <= 1, "Time t must be between 0 and 1." UpperCamelCase = self.basis_function(lowerCamelCase_) UpperCamelCase = 0.0 UpperCamelCase = 0.0 for i in range(len(self.list_of_points)): # For all points, sum up the product of i-th basis function and i-th point. x += basis_function[i] * self.list_of_points[i][0] y += basis_function[i] * self.list_of_points[i][1] return (x, y) def UpperCAmelCase__ ( self , lowerCamelCase_ = 0.01) -> List[Any]: from matplotlib import pyplot as plt # type: ignore UpperCamelCase = [] # x coordinates of points to plot UpperCamelCase = [] # y coordinates of points to plot UpperCamelCase = 0.0 while t <= 1: UpperCamelCase = self.bezier_curve_function(lowerCamelCase_) to_plot_x.append(value[0]) to_plot_y.append(value[1]) t += step_size UpperCamelCase = [i[0] for i in self.list_of_points] UpperCamelCase = [i[1] for i in self.list_of_points] plt.plot( lowerCamelCase_ , lowerCamelCase_ , color='''blue''' , label='''Curve of Degree ''' + str(self.degree) , ) plt.scatter(lowerCamelCase_ , lowerCamelCase_ , color='''red''' , label='''Control Points''') plt.legend() plt.show() if __name__ == "__main__": import doctest doctest.testmod() BezierCurve([(1, 2), (3, 5)]).plot_curve() # degree 1 BezierCurve([(0, 0), (5, 5), (5, 0)]).plot_curve() # degree 2 BezierCurve([(0, 0), (5, 5), (5, 0), (2.5, -2.5)]).plot_curve() # degree 3
34
'''simple docstring''' from __future__ import annotations lowerCAmelCase : Union[str, Any] = [-10, -5, 0, 5, 5.1, 11, 13, 21, 3, 4, -21, -10, -5, -1, 0] lowerCAmelCase : List[str] = [-5, 0, 5, 5.1, 11, 13, 21, -1, 4, -1, -10, -5, -1, 0, -1] def A_( A : list[float]): UpperCamelCase = [] UpperCamelCase = len(A) for i in range(A): UpperCamelCase = -1 for j in range(i + 1 , A): if arr[i] < arr[j]: UpperCamelCase = arr[j] break result.append(A) return result def A_( A : list[float]): UpperCamelCase = [] for i, outer in enumerate(A): UpperCamelCase = -1 for inner in arr[i + 1 :]: if outer < inner: UpperCamelCase = inner break result.append(A) return result def A_( A : list[float]): UpperCamelCase = len(A) UpperCamelCase = [] UpperCamelCase = [-1] * arr_size for index in reversed(range(A)): if stack: while stack[-1] <= arr[index]: stack.pop() if not stack: break if stack: UpperCamelCase = stack[-1] stack.append(arr[index]) return result if __name__ == "__main__": from doctest import testmod from timeit import timeit testmod() print(next_greatest_element_slow(arr)) print(next_greatest_element_fast(arr)) print(next_greatest_element(arr)) lowerCAmelCase : Optional[Any] = ( 'from __main__ import arr, next_greatest_element_slow, ' 'next_greatest_element_fast, next_greatest_element' ) print( 'next_greatest_element_slow():', timeit('next_greatest_element_slow(arr)', setup=setup), ) print( 'next_greatest_element_fast():', timeit('next_greatest_element_fast(arr)', setup=setup), ) print( ' next_greatest_element():', timeit('next_greatest_element(arr)', setup=setup), )
3
0
from sklearn.metrics import mean_squared_error import datasets a_ :Tuple = '\\n@article{scikit-learn,\n title={Scikit-learn: Machine Learning in {P}ython},\n author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V.\n and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P.\n and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and\n Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.},\n journal={Journal of Machine Learning Research},\n volume={12},\n pages={2825--2830},\n year={2011}\n}\n' a_ :Optional[Any] = '\\nMean Squared Error(MSE) is the average of the square of difference between the predicted\nand actual values.\n' a_ :int = '\nArgs:\n predictions: array-like of shape (n_samples,) or (n_samples, n_outputs)\n Estimated target values.\n references: array-like of shape (n_samples,) or (n_samples, n_outputs)\n Ground truth (correct) target values.\n sample_weight: array-like of shape (n_samples,), default=None\n Sample weights.\n multioutput: {"raw_values", "uniform_average"} or array-like of shape (n_outputs,), default="uniform_average"\n Defines aggregating of multiple output values. Array-like value defines weights used to average errors.\n\n "raw_values" : Returns a full set of errors in case of multioutput input.\n\n "uniform_average" : Errors of all outputs are averaged with uniform weight.\n\n squared : bool, default=True\n If True returns MSE value, if False returns RMSE (Root Mean Squared Error) value.\n\nReturns:\n mse : mean squared error.\nExamples:\n\n >>> mse_metric = datasets.load_metric("mse")\n >>> predictions = [2.5, 0.0, 2, 8]\n >>> references = [3, -0.5, 2, 7]\n >>> results = mse_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'mse\': 0.375}\n >>> rmse_result = mse_metric.compute(predictions=predictions, references=references, squared=False)\n >>> print(rmse_result)\n {\'mse\': 0.6123724356957945}\n\n If you\'re using multi-dimensional lists, then set the config as follows :\n\n >>> mse_metric = datasets.load_metric("mse", "multilist")\n >>> predictions = [[0.5, 1], [-1, 1], [7, -6]]\n >>> references = [[0, 2], [-1, 2], [8, -5]]\n >>> results = mse_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'mse\': 0.7083333333333334}\n >>> results = mse_metric.compute(predictions=predictions, references=references, multioutput=\'raw_values\')\n >>> print(results) # doctest: +NORMALIZE_WHITESPACE\n {\'mse\': array([0.41666667, 1. ])}\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class lowercase ( datasets.Metric ): def lowercase__ ( self : Dict ): return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features(self._get_feature_types() ) , reference_urls=[ '''https://scikit-learn.org/stable/modules/generated/sklearn.metrics.mean_squared_error.html''' ] , ) def lowercase__ ( self : Dict ): if self.config_name == "multilist": return { "predictions": datasets.Sequence(datasets.Value('''float''' ) ), "references": datasets.Sequence(datasets.Value('''float''' ) ), } else: return { "predictions": datasets.Value('''float''' ), "references": datasets.Value('''float''' ), } def lowercase__ ( self : str , _lowercase : str , _lowercase : int , _lowercase : int=None , _lowercase : Dict="uniform_average" , _lowercase : List[str]=True ): SCREAMING_SNAKE_CASE__ : str = mean_squared_error( _lowercase , _lowercase , sample_weight=_lowercase , multioutput=_lowercase , squared=_lowercase ) return {"mse": mse}
35
'''simple docstring''' from string import ascii_lowercase, ascii_uppercase def A_( A : str): if not sentence: return "" UpperCamelCase = dict(zip(A , A)) return lower_to_upper.get(sentence[0] , sentence[0]) + sentence[1:] if __name__ == "__main__": from doctest import testmod testmod()
3
0
from __future__ import annotations import unittest from transformers import DebertaVaConfig, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TFDebertaVaForMaskedLM, TFDebertaVaForQuestionAnswering, TFDebertaVaForSequenceClassification, TFDebertaVaForTokenClassification, TFDebertaVaModel, ) class _A : '''simple docstring''' def __init__( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_=13 ,SCREAMING_SNAKE_CASE_=7 ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_=99 ,SCREAMING_SNAKE_CASE_=32 ,SCREAMING_SNAKE_CASE_=2 ,SCREAMING_SNAKE_CASE_=4 ,SCREAMING_SNAKE_CASE_=37 ,SCREAMING_SNAKE_CASE_="gelu" ,SCREAMING_SNAKE_CASE_=0.1 ,SCREAMING_SNAKE_CASE_=0.1 ,SCREAMING_SNAKE_CASE_=512 ,SCREAMING_SNAKE_CASE_=16 ,SCREAMING_SNAKE_CASE_=2 ,SCREAMING_SNAKE_CASE_=0.02 ,SCREAMING_SNAKE_CASE_=False ,SCREAMING_SNAKE_CASE_=True ,SCREAMING_SNAKE_CASE_="None" ,SCREAMING_SNAKE_CASE_=3 ,SCREAMING_SNAKE_CASE_=4 ,SCREAMING_SNAKE_CASE_=None ,): '''simple docstring''' snake_case : List[Any] = parent snake_case : Dict = batch_size snake_case : Dict = seq_length snake_case : Optional[Any] = is_training snake_case : Optional[Any] = use_input_mask snake_case : Any = use_token_type_ids snake_case : str = use_labels snake_case : int = vocab_size snake_case : Optional[int] = hidden_size snake_case : Optional[int] = num_hidden_layers snake_case : str = num_attention_heads snake_case : List[Any] = intermediate_size snake_case : str = hidden_act snake_case : Optional[Any] = hidden_dropout_prob snake_case : Any = attention_probs_dropout_prob snake_case : Union[str, Any] = max_position_embeddings snake_case : Tuple = type_vocab_size snake_case : Optional[Any] = type_sequence_label_size snake_case : List[Any] = initializer_range snake_case : Tuple = num_labels snake_case : int = num_choices snake_case : List[Any] = relative_attention snake_case : int = position_biased_input snake_case : Any = pos_att_type snake_case : Optional[int] = scope def snake_case_ ( self ): '''simple docstring''' snake_case : Optional[Any] = ids_tensor([self.batch_size, self.seq_length] ,self.vocab_size ) snake_case : List[Any] = None if self.use_input_mask: snake_case : Any = random_attention_mask([self.batch_size, self.seq_length] ) snake_case : List[str] = None if self.use_token_type_ids: snake_case : Tuple = ids_tensor([self.batch_size, self.seq_length] ,self.type_vocab_size ) snake_case : Any = None snake_case : Optional[Any] = None snake_case : str = None if self.use_labels: snake_case : Optional[Any] = ids_tensor([self.batch_size] ,self.type_sequence_label_size ) snake_case : Union[str, Any] = ids_tensor([self.batch_size, self.seq_length] ,self.num_labels ) snake_case : int = DebertaVaConfig( vocab_size=self.vocab_size ,hidden_size=self.hidden_size ,num_hidden_layers=self.num_hidden_layers ,num_attention_heads=self.num_attention_heads ,intermediate_size=self.intermediate_size ,hidden_act=self.hidden_act ,hidden_dropout_prob=self.hidden_dropout_prob ,attention_probs_dropout_prob=self.attention_probs_dropout_prob ,max_position_embeddings=self.max_position_embeddings ,type_vocab_size=self.type_vocab_size ,relative_attention=self.relative_attention ,position_biased_input=self.position_biased_input ,initializer_range=self.initializer_range ,return_dict=SCREAMING_SNAKE_CASE_ ,) return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : Optional[Any] = TFDebertaVaModel(config=SCREAMING_SNAKE_CASE_ ) snake_case : Optional[int] = {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids} snake_case : Dict = [input_ids, input_mask] snake_case : List[Any] = model(SCREAMING_SNAKE_CASE_ ) snake_case : Optional[Any] = model(SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, self.seq_length, self.hidden_size) ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : Any = TFDebertaVaForMaskedLM(config=SCREAMING_SNAKE_CASE_ ) snake_case : int = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } snake_case : Any = model(SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.seq_length, self.vocab_size) ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : Any = self.num_labels snake_case : Any = TFDebertaVaForSequenceClassification(config=SCREAMING_SNAKE_CASE_ ) snake_case : List[str] = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } snake_case : List[str] = model(SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.num_labels) ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : Tuple = self.num_labels snake_case : Union[str, Any] = TFDebertaVaForTokenClassification(config=SCREAMING_SNAKE_CASE_ ) snake_case : Optional[int] = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } snake_case : Union[str, Any] = model(SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.seq_length, self.num_labels) ) def snake_case_ ( self ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ): '''simple docstring''' snake_case : Dict = TFDebertaVaForQuestionAnswering(config=SCREAMING_SNAKE_CASE_ ) snake_case : List[Any] = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } snake_case : Tuple = model(SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual(result.start_logits.shape ,(self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape ,(self.batch_size, self.seq_length) ) def snake_case_ ( self ): '''simple docstring''' snake_case : Tuple = self.prepare_config_and_inputs() ( ( snake_case ) , ( snake_case ) , ( snake_case ) , ( snake_case ) , ( snake_case ) , ( snake_case ) , ( snake_case ) , ) : Optional[Any] = config_and_inputs snake_case : List[Any] = {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask} return config, inputs_dict @require_tf class _A ( snake_case , snake_case , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Optional[int] = ( ( TFDebertaVaModel, TFDebertaVaForMaskedLM, TFDebertaVaForQuestionAnswering, TFDebertaVaForSequenceClassification, TFDebertaVaForTokenClassification, ) if is_tf_available() else () ) __lowerCamelCase : Any = ( { '''feature-extraction''': TFDebertaVaModel, '''fill-mask''': TFDebertaVaForMaskedLM, '''question-answering''': TFDebertaVaForQuestionAnswering, '''text-classification''': TFDebertaVaForSequenceClassification, '''token-classification''': TFDebertaVaForTokenClassification, '''zero-shot''': TFDebertaVaForSequenceClassification, } if is_tf_available() else {} ) __lowerCamelCase : Optional[int] = False __lowerCamelCase : Dict = False def snake_case_ ( self ): '''simple docstring''' snake_case : Optional[int] = TFDebertaVaModelTester(self ) snake_case : Dict = ConfigTester(self ,config_class=SCREAMING_SNAKE_CASE_ ,hidden_size=37 ) def snake_case_ ( self ): '''simple docstring''' self.config_tester.run_common_tests() def snake_case_ ( self ): '''simple docstring''' snake_case : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ): '''simple docstring''' snake_case : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ): '''simple docstring''' snake_case : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ): '''simple docstring''' snake_case : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*SCREAMING_SNAKE_CASE_ ) def snake_case_ ( self ): '''simple docstring''' snake_case : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*SCREAMING_SNAKE_CASE_ ) @slow def snake_case_ ( self ): '''simple docstring''' snake_case : Any = TFDebertaVaModel.from_pretrained("""kamalkraj/deberta-v2-xlarge""" ) self.assertIsNotNone(SCREAMING_SNAKE_CASE_ ) @require_tf class _A ( unittest.TestCase ): '''simple docstring''' @unittest.skip(reason="""Model not available yet""" ) def snake_case_ ( self ): '''simple docstring''' pass @slow def snake_case_ ( self ): '''simple docstring''' snake_case : Dict = TFDebertaVaModel.from_pretrained("""kamalkraj/deberta-v2-xlarge""" ) snake_case : Dict = tf.constant([[0, 31414, 232, 328, 740, 1140, 12695, 69, 46078, 1588, 2]] ) snake_case : Tuple = tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) snake_case : Optional[Any] = model(SCREAMING_SNAKE_CASE_ ,attention_mask=SCREAMING_SNAKE_CASE_ )[0] snake_case : Optional[Any] = tf.constant( [[[0.23_56, 0.19_48, 0.03_69], [-0.10_63, 0.35_86, -0.51_52], [-0.63_99, -0.02_59, -0.25_25]]] ) tf.debugging.assert_near(output[:, 1:4, 1:4] ,SCREAMING_SNAKE_CASE_ ,atol=1E-4 )
36
'''simple docstring''' from typing import Optional, Tuple, Union import tensorflow as tf from ...activations_tf import ACTaFN from ...file_utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward from ...modeling_tf_outputs import ( TFBaseModelOutputWithNoAttention, TFBaseModelOutputWithPoolingAndNoAttention, TFSequenceClassifierOutput, ) from ...modeling_tf_utils import TFPreTrainedModel, TFSequenceClassificationLoss, keras_serializable, unpack_inputs from ...tf_utils import shape_list from ...utils import logging from .configuration_regnet import RegNetConfig lowerCAmelCase : Dict = logging.get_logger(__name__) # General docstring lowerCAmelCase : str = 'RegNetConfig' # Base docstring lowerCAmelCase : str = 'facebook/regnet-y-040' lowerCAmelCase : Dict = [1, 10_88, 7, 7] # Image classification docstring lowerCAmelCase : Dict = 'facebook/regnet-y-040' lowerCAmelCase : int = 'tabby, tabby cat' lowerCAmelCase : int = [ 'facebook/regnet-y-040', # See all regnet models at https://huggingface.co/models?filter=regnet ] class SCREAMING_SNAKE_CASE__ ( tf.keras.layers.Layer): def __init__( self , A_ , A_ = 3 , A_ = 1 , A_ = 1 , A_ = "relu" , **A_ , )-> str: '''simple docstring''' super().__init__(**A_ ) # The padding and conv has been verified in # https://colab.research.google.com/gist/sayakpaul/854bc10eeaf21c9ee2119e0b9f3841a7/scratchpad.ipynb UpperCamelCase = tf.keras.layers.ZeroPaddingaD(padding=kernel_size // 2 ) UpperCamelCase = tf.keras.layers.ConvaD( filters=A_ , kernel_size=A_ , strides=A_ , padding='VALID' , groups=A_ , use_bias=A_ , name='convolution' , ) UpperCamelCase = tf.keras.layers.BatchNormalization(epsilon=1e-5 , momentum=0.9 , name='normalization' ) UpperCamelCase = ACTaFN[activation] if activation is not None else tf.identity def UpperCAmelCase_ ( self , A_ )-> Any: '''simple docstring''' UpperCamelCase = self.convolution(self.padding(A_ ) ) UpperCamelCase = self.normalization(A_ ) UpperCamelCase = self.activation(A_ ) return hidden_state class SCREAMING_SNAKE_CASE__ ( tf.keras.layers.Layer): def __init__( self , A_ , **A_ )-> Optional[Any]: '''simple docstring''' super().__init__(**A_ ) UpperCamelCase = config.num_channels UpperCamelCase = TFRegNetConvLayer( out_channels=config.embedding_size , kernel_size=3 , stride=2 , activation=config.hidden_act , name='embedder' , ) def UpperCAmelCase_ ( self , A_ )-> List[Any]: '''simple docstring''' UpperCamelCase = shape_list(A_ )[1] if tf.executing_eagerly() and num_channels != self.num_channels: raise ValueError( 'Make sure that the channel dimension of the pixel values match with the one set in the configuration.' ) # When running on CPU, `tf.keras.layers.Conv2D` doesn't support `NCHW` format. # So change the input format from `NCHW` to `NHWC`. # shape = (batch_size, in_height, in_width, in_channels=num_channels) UpperCamelCase = tf.transpose(A_ , perm=(0, 2, 3, 1) ) UpperCamelCase = self.embedder(A_ ) return hidden_state class SCREAMING_SNAKE_CASE__ ( tf.keras.layers.Layer): def __init__( self , A_ , A_ = 2 , **A_ )-> List[Any]: '''simple docstring''' super().__init__(**A_ ) UpperCamelCase = tf.keras.layers.ConvaD( filters=A_ , kernel_size=1 , strides=A_ , use_bias=A_ , name='convolution' ) UpperCamelCase = tf.keras.layers.BatchNormalization(epsilon=1e-5 , momentum=0.9 , name='normalization' ) def UpperCAmelCase_ ( self , A_ , A_ = False )-> tf.Tensor: '''simple docstring''' return self.normalization(self.convolution(A_ ) , training=A_ ) class SCREAMING_SNAKE_CASE__ ( tf.keras.layers.Layer): def __init__( self , A_ , A_ , **A_ )-> Optional[Any]: '''simple docstring''' super().__init__(**A_ ) UpperCamelCase = tf.keras.layers.GlobalAveragePoolingaD(keepdims=A_ , name='pooler' ) UpperCamelCase = [ tf.keras.layers.ConvaD(filters=A_ , kernel_size=1 , activation='relu' , name='attention.0' ), tf.keras.layers.ConvaD(filters=A_ , kernel_size=1 , activation='sigmoid' , name='attention.2' ), ] def UpperCAmelCase_ ( self , A_ )-> Optional[int]: '''simple docstring''' UpperCamelCase = self.pooler(A_ ) for layer_module in self.attention: UpperCamelCase = layer_module(A_ ) UpperCamelCase = hidden_state * pooled return hidden_state class SCREAMING_SNAKE_CASE__ ( tf.keras.layers.Layer): def __init__( self , A_ , A_ , A_ , A_ = 1 , **A_ )-> Dict: '''simple docstring''' super().__init__(**A_ ) UpperCamelCase = in_channels != out_channels or stride != 1 UpperCamelCase = max(1 , out_channels // config.groups_width ) UpperCamelCase = ( TFRegNetShortCut(A_ , stride=A_ , name='shortcut' ) if should_apply_shortcut else tf.keras.layers.Activation('linear' , name='shortcut' ) ) # `self.layers` instead of `self.layer` because that is a reserved argument. UpperCamelCase = [ TFRegNetConvLayer(A_ , kernel_size=1 , activation=config.hidden_act , name='layer.0' ), TFRegNetConvLayer( A_ , stride=A_ , groups=A_ , activation=config.hidden_act , name='layer.1' ), TFRegNetConvLayer(A_ , kernel_size=1 , activation=A_ , name='layer.2' ), ] UpperCamelCase = ACTaFN[config.hidden_act] def UpperCAmelCase_ ( self , A_ )-> Tuple: '''simple docstring''' UpperCamelCase = hidden_state for layer_module in self.layers: UpperCamelCase = layer_module(A_ ) UpperCamelCase = self.shortcut(A_ ) hidden_state += residual UpperCamelCase = self.activation(A_ ) return hidden_state class SCREAMING_SNAKE_CASE__ ( tf.keras.layers.Layer): def __init__( self , A_ , A_ , A_ , A_ = 1 , **A_ )-> Any: '''simple docstring''' super().__init__(**A_ ) UpperCamelCase = in_channels != out_channels or stride != 1 UpperCamelCase = max(1 , out_channels // config.groups_width ) UpperCamelCase = ( TFRegNetShortCut(A_ , stride=A_ , name='shortcut' ) if should_apply_shortcut else tf.keras.layers.Activation('linear' , name='shortcut' ) ) UpperCamelCase = [ TFRegNetConvLayer(A_ , kernel_size=1 , activation=config.hidden_act , name='layer.0' ), TFRegNetConvLayer( A_ , stride=A_ , groups=A_ , activation=config.hidden_act , name='layer.1' ), TFRegNetSELayer(A_ , reduced_channels=int(round(in_channels / 4 ) ) , name='layer.2' ), TFRegNetConvLayer(A_ , kernel_size=1 , activation=A_ , name='layer.3' ), ] UpperCamelCase = ACTaFN[config.hidden_act] def UpperCAmelCase_ ( self , A_ )-> List[Any]: '''simple docstring''' UpperCamelCase = hidden_state for layer_module in self.layers: UpperCamelCase = layer_module(A_ ) UpperCamelCase = self.shortcut(A_ ) hidden_state += residual UpperCamelCase = self.activation(A_ ) return hidden_state class SCREAMING_SNAKE_CASE__ ( tf.keras.layers.Layer): def __init__( self , A_ , A_ , A_ , A_ = 2 , A_ = 2 , **A_ )-> Dict: '''simple docstring''' super().__init__(**A_ ) UpperCamelCase = TFRegNetXLayer if config.layer_type == 'x' else TFRegNetYLayer UpperCamelCase = [ # downsampling is done in the first layer with stride of 2 layer(A_ , A_ , A_ , stride=A_ , name='layers.0' ), *[layer(A_ , A_ , A_ , name=F'''layers.{i+1}''' ) for i in range(depth - 1 )], ] def UpperCAmelCase_ ( self , A_ )-> List[Any]: '''simple docstring''' for layer_module in self.layers: UpperCamelCase = layer_module(A_ ) return hidden_state class SCREAMING_SNAKE_CASE__ ( tf.keras.layers.Layer): def __init__( self , A_ , **A_ )-> str: '''simple docstring''' super().__init__(**A_ ) UpperCamelCase = [] # based on `downsample_in_first_stage`, the first layer of the first stage may or may not downsample the input self.stages.append( TFRegNetStage( A_ , config.embedding_size , config.hidden_sizes[0] , stride=2 if config.downsample_in_first_stage else 1 , depth=config.depths[0] , name='stages.0' , ) ) UpperCamelCase = zip(config.hidden_sizes , config.hidden_sizes[1:] ) for i, ((in_channels, out_channels), depth) in enumerate(zip(A_ , config.depths[1:] ) ): self.stages.append(TFRegNetStage(A_ , A_ , A_ , depth=A_ , name=F'''stages.{i+1}''' ) ) def UpperCAmelCase_ ( self , A_ , A_ = False , A_ = True )-> TFBaseModelOutputWithNoAttention: '''simple docstring''' UpperCamelCase = () if output_hidden_states else None for stage_module in self.stages: if output_hidden_states: UpperCamelCase = hidden_states + (hidden_state,) UpperCamelCase = stage_module(A_ ) if output_hidden_states: UpperCamelCase = hidden_states + (hidden_state,) if not return_dict: return tuple(v for v in [hidden_state, hidden_states] if v is not None ) return TFBaseModelOutputWithNoAttention(last_hidden_state=A_ , hidden_states=A_ ) @keras_serializable class SCREAMING_SNAKE_CASE__ ( tf.keras.layers.Layer): lowerCAmelCase_ = RegNetConfig def __init__( self , A_ , **A_ )-> Union[str, Any]: '''simple docstring''' super().__init__(**A_ ) UpperCamelCase = config UpperCamelCase = TFRegNetEmbeddings(A_ , name='embedder' ) UpperCamelCase = TFRegNetEncoder(A_ , name='encoder' ) UpperCamelCase = tf.keras.layers.GlobalAveragePoolingaD(keepdims=A_ , name='pooler' ) @unpack_inputs def UpperCAmelCase_ ( self , A_ , A_ = None , A_ = None , A_ = False , )-> TFBaseModelOutputWithPoolingAndNoAttention: '''simple docstring''' UpperCamelCase = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) UpperCamelCase = return_dict if return_dict is not None else self.config.use_return_dict UpperCamelCase = self.embedder(A_ , training=A_ ) UpperCamelCase = self.encoder( A_ , output_hidden_states=A_ , return_dict=A_ , training=A_ ) UpperCamelCase = encoder_outputs[0] UpperCamelCase = self.pooler(A_ ) # Change to NCHW output format have uniformity in the modules UpperCamelCase = tf.transpose(A_ , perm=(0, 3, 1, 2) ) UpperCamelCase = tf.transpose(A_ , perm=(0, 3, 1, 2) ) # Change the other hidden state outputs to NCHW as well if output_hidden_states: UpperCamelCase = tuple([tf.transpose(A_ , perm=(0, 3, 1, 2) ) for h in encoder_outputs[1]] ) if not return_dict: return (last_hidden_state, pooled_output) + encoder_outputs[1:] return TFBaseModelOutputWithPoolingAndNoAttention( last_hidden_state=A_ , pooler_output=A_ , hidden_states=hidden_states if output_hidden_states else encoder_outputs.hidden_states , ) class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = RegNetConfig lowerCAmelCase_ = """regnet""" lowerCAmelCase_ = """pixel_values""" @property def UpperCAmelCase_ ( self )-> List[str]: '''simple docstring''' return {"pixel_values": tf.TensorSpec(shape=(None, self.config.num_channels, 224, 224) , dtype=tf.floataa )} lowerCAmelCase : str = r'\n Parameters:\n This model is a Tensorflow\n [tf.keras.layers.Layer](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Layer) sub-class. Use it as a\n regular Tensorflow Module and refer to the Tensorflow documentation for all matter related to general usage and\n behavior.\n config ([`RegNetConfig`]): Model configuration class with all the parameters of the model.\n Initializing with a config file does not load the weights associated with the model, only the\n configuration. Check out the [`~TFPreTrainedModel.from_pretrained`] method to load the model weights.\n' lowerCAmelCase : List[str] = r'\n Args:\n pixel_values (`tf.Tensor` of shape `(batch_size, num_channels, height, width)`):\n Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n [`ConveNextImageProcessor.__call__`] for details.\n output_hidden_states (`bool`, *optional*):\n Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for\n more detail.\n return_dict (`bool`, *optional*):\n Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n' @add_start_docstrings( """The bare RegNet model outputting raw features without any specific head on top.""" , snake_case_ , ) class SCREAMING_SNAKE_CASE__ ( snake_case_): def __init__( self , A_ , *A_ , **A_ )-> List[Any]: '''simple docstring''' super().__init__(A_ , *A_ , **A_ ) UpperCamelCase = TFRegNetMainLayer(A_ , name='regnet' ) @unpack_inputs @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 UpperCAmelCase_ ( self , A_ , A_ = None , A_ = None , A_=False , )-> Union[TFBaseModelOutputWithPoolingAndNoAttention, Tuple[tf.Tensor]]: '''simple docstring''' UpperCamelCase = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) UpperCamelCase = return_dict if return_dict is not None else self.config.use_return_dict UpperCamelCase = self.regnet( pixel_values=A_ , output_hidden_states=A_ , return_dict=A_ , training=A_ , ) if not return_dict: return (outputs[0],) + outputs[1:] return TFBaseModelOutputWithPoolingAndNoAttention( last_hidden_state=outputs.last_hidden_state , pooler_output=outputs.pooler_output , hidden_states=outputs.hidden_states , ) @add_start_docstrings( """ RegNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for ImageNet. """ , snake_case_ , ) class SCREAMING_SNAKE_CASE__ ( snake_case_ , snake_case_): def __init__( self , A_ , *A_ , **A_ )-> str: '''simple docstring''' super().__init__(A_ , *A_ , **A_ ) UpperCamelCase = config.num_labels UpperCamelCase = TFRegNetMainLayer(A_ , name='regnet' ) # classification head UpperCamelCase = [ tf.keras.layers.Flatten(), tf.keras.layers.Dense(config.num_labels , name='classifier.1' ) if config.num_labels > 0 else tf.identity, ] @unpack_inputs @add_start_docstrings_to_model_forward(A_ ) @add_code_sample_docstrings( checkpoint=_IMAGE_CLASS_CHECKPOINT , output_type=A_ , config_class=_CONFIG_FOR_DOC , expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT , ) def UpperCAmelCase_ ( self , A_ = None , A_ = None , A_ = None , A_ = None , A_=False , )-> Union[TFSequenceClassifierOutput, Tuple[tf.Tensor]]: '''simple docstring''' UpperCamelCase = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) UpperCamelCase = return_dict if return_dict is not None else self.config.use_return_dict UpperCamelCase = self.regnet( A_ , output_hidden_states=A_ , return_dict=A_ , training=A_ ) UpperCamelCase = outputs.pooler_output if return_dict else outputs[1] UpperCamelCase = self.classifier[0](A_ ) UpperCamelCase = self.classifier[1](A_ ) UpperCamelCase = None if labels is None else self.hf_compute_loss(labels=A_ , logits=A_ ) if not return_dict: UpperCamelCase = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return TFSequenceClassifierOutput(loss=A_ , logits=A_ , hidden_states=outputs.hidden_states )
3
0
from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCamelCase : Optional[int] = logging.get_logger(__name__) UpperCamelCase : List[str] = { """tiiuae/falcon-40b""": """https://huggingface.co/tiiuae/falcon-40b/resolve/main/config.json""", """tiiuae/falcon-7b""": """https://huggingface.co/tiiuae/falcon-7b/resolve/main/config.json""", } class A__ ( A__ ): """simple docstring""" _lowercase = 'falcon' _lowercase = ['past_key_values'] def __init__( self : Optional[Any] , lowerCamelCase__ : Union[str, Any]=65_024 , lowerCamelCase__ : List[Any]=4_544 , lowerCamelCase__ : str=32 , lowerCamelCase__ : List[Any]=71 , lowerCamelCase__ : List[Any]=1E-5 , lowerCamelCase__ : List[Any]=0.02 , lowerCamelCase__ : Optional[int]=True , lowerCamelCase__ : Optional[int]=0.0 , lowerCamelCase__ : Optional[int]=0.0 , lowerCamelCase__ : Optional[Any]=None , lowerCamelCase__ : Optional[int]=False , lowerCamelCase__ : Tuple=False , lowerCamelCase__ : List[Any]=True , lowerCamelCase__ : Any=True , lowerCamelCase__ : List[Any]=False , lowerCamelCase__ : int=11 , lowerCamelCase__ : List[Any]=11 , **lowerCamelCase__ : int , ): a__ : Any = vocab_size # Backward compatibility with n_embed kwarg a__ : Dict = kwargs.pop("n_embed" , lowerCamelCase__ ) a__ : str = hidden_size if n_embed is None else n_embed a__ : Tuple = num_hidden_layers a__ : Dict = num_attention_heads a__ : Tuple = layer_norm_epsilon a__ : List[str] = initializer_range a__ : Dict = use_cache a__ : Tuple = hidden_dropout a__ : str = attention_dropout a__ : str = bos_token_id a__ : Any = eos_token_id a__ : Tuple = num_attention_heads if num_kv_heads is None else num_kv_heads a__ : Union[str, Any] = alibi a__ : List[str] = new_decoder_architecture a__ : Optional[Any] = multi_query # Ignored when new_decoder_architecture is True a__ : int = parallel_attn a__ : Any = bias super().__init__(bos_token_id=lowerCamelCase__ , eos_token_id=lowerCamelCase__ , **lowerCamelCase__ ) @property def _UpperCamelCase( self : Optional[Any] ): return self.hidden_size // self.num_attention_heads @property def _UpperCamelCase( self : Dict ): return not self.alibi
37
'''simple docstring''' from collections import OrderedDict from typing import Any, Mapping, Optional, Union from ...configuration_utils import PretrainedConfig from ...feature_extraction_utils import FeatureExtractionMixin from ...onnx import OnnxConfig from ...onnx.utils import compute_effective_axis_dimension from ...tokenization_utils_base import PreTrainedTokenizerBase from ...utils import TensorType, logging lowerCAmelCase : Any = logging.get_logger(__name__) lowerCAmelCase : Optional[int] = { 'deepmind/language-perceiver': 'https://huggingface.co/deepmind/language-perceiver/resolve/main/config.json', # See all Perceiver models at https://huggingface.co/models?filter=perceiver } class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = """perceiver""" def __init__( self , A_=256 , A_=1280 , A_=768 , A_=1 , A_=26 , A_=8 , A_=8 , A_=None , A_=None , A_="kv" , A_=1 , A_=1 , A_="gelu" , A_=0.1 , A_=0.02 , A_=1e-12 , A_=True , A_=262 , A_=2048 , A_=56 , A_=[368, 496] , A_=16 , A_=1920 , A_=16 , A_=[1, 16, 224, 224] , **A_ , )-> str: '''simple docstring''' super().__init__(**A_ ) UpperCamelCase = num_latents UpperCamelCase = d_latents UpperCamelCase = d_model UpperCamelCase = num_blocks UpperCamelCase = num_self_attends_per_block UpperCamelCase = num_self_attention_heads UpperCamelCase = num_cross_attention_heads UpperCamelCase = qk_channels UpperCamelCase = v_channels UpperCamelCase = cross_attention_shape_for_attention UpperCamelCase = self_attention_widening_factor UpperCamelCase = cross_attention_widening_factor UpperCamelCase = hidden_act UpperCamelCase = attention_probs_dropout_prob UpperCamelCase = initializer_range UpperCamelCase = layer_norm_eps UpperCamelCase = use_query_residual # masked language modeling attributes UpperCamelCase = vocab_size UpperCamelCase = max_position_embeddings # image classification attributes UpperCamelCase = image_size # flow attributes UpperCamelCase = train_size # multimodal autoencoding attributes UpperCamelCase = num_frames UpperCamelCase = audio_samples_per_frame UpperCamelCase = samples_per_patch UpperCamelCase = output_shape class SCREAMING_SNAKE_CASE__ ( snake_case_): @property def UpperCAmelCase_ ( self )-> Mapping[str, Mapping[int, str]]: '''simple docstring''' if self.task == "multiple-choice": UpperCamelCase = {0: 'batch', 1: 'choice', 2: 'sequence'} else: UpperCamelCase = {0: 'batch', 1: 'sequence'} return OrderedDict( [ ('inputs', dynamic_axis), ('attention_mask', dynamic_axis), ] ) @property def UpperCAmelCase_ ( self )-> float: '''simple docstring''' return 1e-4 def UpperCAmelCase_ ( self , A_ , A_ = -1 , A_ = -1 , A_ = -1 , A_ = False , A_ = None , A_ = 3 , A_ = 40 , A_ = 40 , )-> Mapping[str, Any]: '''simple docstring''' if isinstance(A_ , A_ ): # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX UpperCamelCase = compute_effective_axis_dimension( A_ , fixed_dimension=OnnxConfig.default_fixed_batch , num_token_to_add=0 ) # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX UpperCamelCase = preprocessor.num_special_tokens_to_add(A_ ) UpperCamelCase = compute_effective_axis_dimension( A_ , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=A_ ) # Generate dummy inputs according to compute batch and sequence UpperCamelCase = [' '.join(['a'] ) * seq_length] * batch_size UpperCamelCase = dict(preprocessor(A_ , return_tensors=A_ ) ) UpperCamelCase = inputs.pop('input_ids' ) return inputs elif isinstance(A_ , A_ ) and preprocessor.model_input_names[0] == "pixel_values": # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX UpperCamelCase = compute_effective_axis_dimension(A_ , fixed_dimension=OnnxConfig.default_fixed_batch ) UpperCamelCase = self._generate_dummy_images(A_ , A_ , A_ , A_ ) UpperCamelCase = dict(preprocessor(images=A_ , return_tensors=A_ ) ) UpperCamelCase = inputs.pop('pixel_values' ) return inputs else: raise ValueError( 'Unable to generate dummy inputs for the model. Please provide a tokenizer or a preprocessor.' )
3
0
'''simple docstring''' from random import randint from tempfile import TemporaryFile import numpy as np def UpperCamelCase__ ( __magic_name__ : List[str] , __magic_name__ : Union[str, Any] , __magic_name__ : Union[str, Any] ) -> Any: '''simple docstring''' snake_case__ : str = 0 if start < end: snake_case__ : str = randint(__magic_name__ , __magic_name__ ) snake_case__ : Optional[Any] = a[end] snake_case__ : Dict = a[pivot] snake_case__ : Union[str, Any] = temp snake_case__ , snake_case__ : List[Any] = _in_place_partition(__magic_name__ , __magic_name__ , __magic_name__ ) count += _in_place_quick_sort(__magic_name__ , __magic_name__ , p - 1 ) count += _in_place_quick_sort(__magic_name__ , p + 1 , __magic_name__ ) return count def UpperCamelCase__ ( __magic_name__ : Union[str, Any] , __magic_name__ : str , __magic_name__ : int ) -> Union[str, Any]: '''simple docstring''' snake_case__ : Optional[int] = 0 snake_case__ : int = randint(__magic_name__ , __magic_name__ ) snake_case__ : Any = a[end] snake_case__ : Dict = a[pivot] snake_case__ : List[Any] = temp snake_case__ : List[str] = start - 1 for index in range(__magic_name__ , __magic_name__ ): count += 1 if a[index] < a[end]: # check if current val is less than pivot value snake_case__ : Dict = new_pivot_index + 1 snake_case__ : Union[str, Any] = a[new_pivot_index] snake_case__ : int = a[index] snake_case__ : str = temp snake_case__ : List[Any] = a[new_pivot_index + 1] snake_case__ : Optional[int] = a[end] snake_case__ : List[Any] = temp return new_pivot_index + 1, count A_ : Dict = TemporaryFile() A_ : str = 100 # 1000 elements are to be sorted A_ , A_ : Union[str, Any] = 0, 1 # mean and standard deviation A_ : Optional[Any] = np.random.normal(mu, sigma, p) np.save(outfile, X) print("The array is") print(X) outfile.seek(0) # using the same array A_ : List[str] = np.load(outfile) A_ : List[Any] = len(M) - 1 A_ : List[Any] = _in_place_quick_sort(M, 0, r) print( "No of Comparisons for 100 elements selected from a standard normal distribution" "is :" ) print(z)
38
'''simple docstring''' from ....configuration_utils import PretrainedConfig from ....utils import logging lowerCAmelCase : Optional[Any] = logging.get_logger(__name__) lowerCAmelCase : Dict = { 'speechbrain/m-ctc-t-large': 'https://huggingface.co/speechbrain/m-ctc-t-large/resolve/main/config.json', # See all M-CTC-T models at https://huggingface.co/models?filter=mctct } class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = """mctct""" def __init__( self , A_=8065 , A_=1536 , A_=36 , A_=6144 , A_=4 , A_=384 , A_=920 , A_=1e-5 , A_=0.3 , A_="relu" , A_=0.02 , A_=0.3 , A_=0.3 , A_=1 , A_=0 , A_=2 , A_=1 , A_=0.3 , A_=1 , A_=(7,) , A_=(3,) , A_=80 , A_=1 , A_=None , A_="sum" , A_=False , **A_ , )-> str: '''simple docstring''' super().__init__(**A_ , pad_token_id=A_ , bos_token_id=A_ , eos_token_id=A_ ) UpperCamelCase = vocab_size UpperCamelCase = hidden_size UpperCamelCase = num_hidden_layers UpperCamelCase = intermediate_size UpperCamelCase = num_attention_heads UpperCamelCase = attention_head_dim UpperCamelCase = max_position_embeddings UpperCamelCase = layer_norm_eps UpperCamelCase = layerdrop UpperCamelCase = hidden_act UpperCamelCase = initializer_range UpperCamelCase = hidden_dropout_prob UpperCamelCase = attention_probs_dropout_prob UpperCamelCase = pad_token_id UpperCamelCase = bos_token_id UpperCamelCase = eos_token_id UpperCamelCase = conv_glu_dim UpperCamelCase = conv_dropout UpperCamelCase = num_conv_layers UpperCamelCase = input_feat_per_channel UpperCamelCase = input_channels UpperCamelCase = conv_channels UpperCamelCase = ctc_loss_reduction UpperCamelCase = ctc_zero_infinity # prevents config testing fail with exporting to json UpperCamelCase = list(A_ ) UpperCamelCase = list(A_ ) if len(self.conv_kernel ) != self.num_conv_layers: raise ValueError( 'Configuration for convolutional module is incorrect. ' 'It is required that `len(config.conv_kernel)` == `config.num_conv_layers` ' F'''but is `len(config.conv_kernel) = {len(self.conv_kernel )}`, ''' F'''`config.num_conv_layers = {self.num_conv_layers}`.''' )
3
0
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 BeitConfig, BeitForImageClassification, BeitForMaskedImageModeling, BeitImageProcessor from transformers.image_utils import PILImageResampling from transformers.utils import logging logging.set_verbosity_info() lowerCAmelCase_ = logging.get_logger(__name__) def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__=False , SCREAMING_SNAKE_CASE__=False ): snake_case_ = '''backbone.''' if is_semantic else '''''' snake_case_ = [] for i in range(config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append((F'''{prefix}blocks.{i}.norm1.weight''', F'''beit.encoder.layer.{i}.layernorm_before.weight''') ) rename_keys.append((F'''{prefix}blocks.{i}.norm1.bias''', F'''beit.encoder.layer.{i}.layernorm_before.bias''') ) rename_keys.append( (F'''{prefix}blocks.{i}.attn.proj.weight''', F'''beit.encoder.layer.{i}.attention.output.dense.weight''') ) rename_keys.append( (F'''{prefix}blocks.{i}.attn.proj.bias''', F'''beit.encoder.layer.{i}.attention.output.dense.bias''') ) rename_keys.append((F'''{prefix}blocks.{i}.norm2.weight''', F'''beit.encoder.layer.{i}.layernorm_after.weight''') ) rename_keys.append((F'''{prefix}blocks.{i}.norm2.bias''', F'''beit.encoder.layer.{i}.layernorm_after.bias''') ) rename_keys.append((F'''{prefix}blocks.{i}.mlp.fc1.weight''', F'''beit.encoder.layer.{i}.intermediate.dense.weight''') ) rename_keys.append((F'''{prefix}blocks.{i}.mlp.fc1.bias''', F'''beit.encoder.layer.{i}.intermediate.dense.bias''') ) rename_keys.append((F'''{prefix}blocks.{i}.mlp.fc2.weight''', F'''beit.encoder.layer.{i}.output.dense.weight''') ) rename_keys.append((F'''{prefix}blocks.{i}.mlp.fc2.bias''', F'''beit.encoder.layer.{i}.output.dense.bias''') ) # projection layer + position embeddings rename_keys.extend( [ (F'''{prefix}cls_token''', '''beit.embeddings.cls_token'''), (F'''{prefix}patch_embed.proj.weight''', '''beit.embeddings.patch_embeddings.projection.weight'''), (F'''{prefix}patch_embed.proj.bias''', '''beit.embeddings.patch_embeddings.projection.bias'''), (F'''{prefix}pos_embed''', '''beit.embeddings.position_embeddings'''), ] ) if has_lm_head: # mask token + layernorm rename_keys.extend( [ ('''mask_token''', '''beit.embeddings.mask_token'''), ('''norm.weight''', '''layernorm.weight'''), ('''norm.bias''', '''layernorm.bias'''), ] ) else: # layernorm + classification head rename_keys.extend( [ ('''fc_norm.weight''', '''beit.pooler.layernorm.weight'''), ('''fc_norm.bias''', '''beit.pooler.layernorm.bias'''), ('''head.weight''', '''classifier.weight'''), ('''head.bias''', '''classifier.bias'''), ] ) return rename_keys def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__=False , SCREAMING_SNAKE_CASE__=False ): for i in range(config.num_hidden_layers ): snake_case_ = '''backbone.''' if is_semantic else '''''' # queries, keys and values snake_case_ = state_dict.pop(F'''{prefix}blocks.{i}.attn.qkv.weight''' ) snake_case_ = state_dict.pop(F'''{prefix}blocks.{i}.attn.q_bias''' ) snake_case_ = state_dict.pop(F'''{prefix}blocks.{i}.attn.v_bias''' ) snake_case_ = in_proj_weight[ : config.hidden_size, : ] snake_case_ = q_bias snake_case_ = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] snake_case_ = in_proj_weight[ -config.hidden_size :, : ] snake_case_ = v_bias # gamma_1 and gamma_2 # we call them lambda because otherwise they are renamed when using .from_pretrained snake_case_ = state_dict.pop(F'''{prefix}blocks.{i}.gamma_1''' ) snake_case_ = state_dict.pop(F'''{prefix}blocks.{i}.gamma_2''' ) snake_case_ = gamma_a snake_case_ = gamma_a def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): snake_case_ = dct.pop(SCREAMING_SNAKE_CASE__ ) snake_case_ = val def __SCREAMING_SNAKE_CASE (): snake_case_ = '''http://images.cocodataset.org/val2017/000000039769.jpg''' snake_case_ = Image.open(requests.get(SCREAMING_SNAKE_CASE__ , stream=SCREAMING_SNAKE_CASE__ ).raw ) return im @torch.no_grad() def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__=False ): snake_case_ = False if '''rvlcdip''' in checkpoint_url else True snake_case_ = BeitConfig(use_absolute_position_embeddings=SCREAMING_SNAKE_CASE__ , use_mask_token=SCREAMING_SNAKE_CASE__ ) # size of the architecture if "large" in checkpoint_url or "dit-l" in checkpoint_url: snake_case_ = 1024 snake_case_ = 4096 snake_case_ = 24 snake_case_ = 16 # labels if "rvlcdip" in checkpoint_url: snake_case_ = 16 snake_case_ = '''huggingface/label-files''' snake_case_ = '''rvlcdip-id2label.json''' snake_case_ = json.load(open(hf_hub_download(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , repo_type='''dataset''' ) , '''r''' ) ) snake_case_ = {int(SCREAMING_SNAKE_CASE__ ): v for k, v in idalabel.items()} snake_case_ = idalabel snake_case_ = {v: k for k, v in idalabel.items()} # load state_dict of original model, remove and rename some keys snake_case_ = torch.hub.load_state_dict_from_url(SCREAMING_SNAKE_CASE__ , map_location='''cpu''' )['''model'''] snake_case_ = create_rename_keys(SCREAMING_SNAKE_CASE__ , has_lm_head=SCREAMING_SNAKE_CASE__ ) for src, dest in rename_keys: rename_key(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) read_in_q_k_v(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , has_lm_head=SCREAMING_SNAKE_CASE__ ) # load HuggingFace model snake_case_ = BeitForMaskedImageModeling(SCREAMING_SNAKE_CASE__ ) if has_lm_head else BeitForImageClassification(SCREAMING_SNAKE_CASE__ ) model.eval() model.load_state_dict(SCREAMING_SNAKE_CASE__ ) # Check outputs on an image snake_case_ = BeitImageProcessor( size=config.image_size , resample=PILImageResampling.BILINEAR , do_center_crop=SCREAMING_SNAKE_CASE__ ) snake_case_ = prepare_img() snake_case_ = image_processor(images=SCREAMING_SNAKE_CASE__ , return_tensors='''pt''' ) snake_case_ = encoding['''pixel_values'''] snake_case_ = model(SCREAMING_SNAKE_CASE__ ) snake_case_ = outputs.logits # verify logits snake_case_ = [1, 16] if '''rvlcdip''' in checkpoint_url else [1, 196, 8192] assert logits.shape == torch.Size(SCREAMING_SNAKE_CASE__ ), "Shape of logits not as expected" Path(SCREAMING_SNAKE_CASE__ ).mkdir(exist_ok=SCREAMING_SNAKE_CASE__ ) print(F'''Saving model to {pytorch_dump_folder_path}''' ) model.save_pretrained(SCREAMING_SNAKE_CASE__ ) print(F'''Saving image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(SCREAMING_SNAKE_CASE__ ) if push_to_hub: if has_lm_head: snake_case_ = '''dit-base''' if '''base''' in checkpoint_url else '''dit-large''' else: snake_case_ = '''dit-base-finetuned-rvlcdip''' if '''dit-b''' in checkpoint_url else '''dit-large-finetuned-rvlcdip''' image_processor.push_to_hub( repo_path_or_name=Path(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) , organization='''nielsr''' , commit_message='''Add image processor''' , use_temp_dir=SCREAMING_SNAKE_CASE__ , ) model.push_to_hub( repo_path_or_name=Path(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) , organization='''nielsr''' , commit_message='''Add model''' , use_temp_dir=SCREAMING_SNAKE_CASE__ , ) if __name__ == "__main__": lowerCAmelCase_ = argparse.ArgumentParser() parser.add_argument( '''--checkpoint_url''', default='''https://layoutlm.blob.core.windows.net/dit/dit-pts/dit-base-224-p16-500k-62d53a.pth''', type=str, help='''URL to the original PyTorch checkpoint (.pth file).''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the folder to output PyTorch model.''' ) parser.add_argument( '''--push_to_hub''', action='''store_true''', ) lowerCAmelCase_ = parser.parse_args() convert_dit_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path, args.push_to_hub)
39
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) if is_sentencepiece_available(): from ..ta.tokenization_ta import TaTokenizer else: from ...utils.dummy_sentencepiece_objects import TaTokenizer lowerCAmelCase : Tuple = TaTokenizer if is_tokenizers_available(): from ..ta.tokenization_ta_fast import TaTokenizerFast else: from ...utils.dummy_tokenizers_objects import TaTokenizerFast lowerCAmelCase : Optional[int] = TaTokenizerFast lowerCAmelCase : Any = {'configuration_mt5': ['MT5Config', 'MT5OnnxConfig']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : Optional[int] = [ 'MT5EncoderModel', 'MT5ForConditionalGeneration', 'MT5ForQuestionAnswering', 'MT5Model', 'MT5PreTrainedModel', 'MT5Stack', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : Dict = ['TFMT5EncoderModel', 'TFMT5ForConditionalGeneration', 'TFMT5Model'] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : Optional[Any] = ['FlaxMT5EncoderModel', 'FlaxMT5ForConditionalGeneration', 'FlaxMT5Model'] if TYPE_CHECKING: from .configuration_mta import MTaConfig, MTaOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mta import ( MTaEncoderModel, MTaForConditionalGeneration, MTaForQuestionAnswering, MTaModel, MTaPreTrainedModel, MTaStack, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_mta import TFMTaEncoderModel, TFMTaForConditionalGeneration, TFMTaModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_mta import FlaxMTaEncoderModel, FlaxMTaForConditionalGeneration, FlaxMTaModel else: import sys lowerCAmelCase : Tuple = _LazyModule( __name__, globals()['__file__'], _import_structure, extra_objects={'MT5Tokenizer': MTaTokenizer, 'MT5TokenizerFast': MTaTokenizerFast}, module_spec=__spec__, )
3
0
import unittest from transformers import ( MODEL_FOR_CAUSAL_LM_MAPPING, TF_MODEL_FOR_CAUSAL_LM_MAPPING, TextGenerationPipeline, logging, pipeline, ) from transformers.testing_utils import ( CaptureLogger, is_pipeline_test, require_accelerate, require_tf, require_torch, require_torch_gpu, require_torch_or_tf, ) from .test_pipelines_common import ANY @is_pipeline_test @require_torch_or_tf class lowerCAmelCase_ ( unittest.TestCase ): UpperCAmelCase__ : Optional[int] = MODEL_FOR_CAUSAL_LM_MAPPING UpperCAmelCase__ : Union[str, Any] = TF_MODEL_FOR_CAUSAL_LM_MAPPING @require_torch def snake_case_ ( self ) -> int: UpperCamelCase : Any = pipeline(task='text-generation', model='sshleifer/tiny-ctrl', framework='pt' ) # Using `do_sample=False` to force deterministic output UpperCamelCase : Union[str, Any] = text_generator('This is a test', do_sample=SCREAMING_SNAKE_CASE_ ) self.assertEqual( SCREAMING_SNAKE_CASE_, [ { 'generated_text': ( 'This is a test ☃ ☃ segmental segmental segmental 议议eski eski flutter flutter Lacy oscope.' ' oscope. FiliFili@@' ) } ], ) UpperCamelCase : Any = text_generator(['This is a test', 'This is a second test'] ) self.assertEqual( SCREAMING_SNAKE_CASE_, [ [ { 'generated_text': ( 'This is a test ☃ ☃ segmental segmental segmental 议议eski eski flutter flutter Lacy oscope.' ' oscope. FiliFili@@' ) } ], [ { 'generated_text': ( 'This is a second test ☃ segmental segmental segmental 议议eski eski flutter flutter Lacy' ' oscope. oscope. FiliFili@@' ) } ], ], ) UpperCamelCase : Optional[int] = text_generator('This is a test', do_sample=SCREAMING_SNAKE_CASE_, num_return_sequences=2, return_tensors=SCREAMING_SNAKE_CASE_ ) self.assertEqual( SCREAMING_SNAKE_CASE_, [ {'generated_token_ids': ANY(SCREAMING_SNAKE_CASE_ )}, {'generated_token_ids': ANY(SCREAMING_SNAKE_CASE_ )}, ], ) UpperCamelCase : Union[str, Any] = text_generator.model.config.eos_token_id UpperCamelCase : Union[str, Any] = '<pad>' UpperCamelCase : str = text_generator( ['This is a test', 'This is a second test'], do_sample=SCREAMING_SNAKE_CASE_, num_return_sequences=2, batch_size=2, return_tensors=SCREAMING_SNAKE_CASE_, ) self.assertEqual( SCREAMING_SNAKE_CASE_, [ [ {'generated_token_ids': ANY(SCREAMING_SNAKE_CASE_ )}, {'generated_token_ids': ANY(SCREAMING_SNAKE_CASE_ )}, ], [ {'generated_token_ids': ANY(SCREAMING_SNAKE_CASE_ )}, {'generated_token_ids': ANY(SCREAMING_SNAKE_CASE_ )}, ], ], ) @require_tf def snake_case_ ( self ) -> str: UpperCamelCase : Tuple = pipeline(task='text-generation', model='sshleifer/tiny-ctrl', framework='tf' ) # Using `do_sample=False` to force deterministic output UpperCamelCase : Optional[int] = text_generator('This is a test', do_sample=SCREAMING_SNAKE_CASE_ ) self.assertEqual( SCREAMING_SNAKE_CASE_, [ { 'generated_text': ( 'This is a test FeyFeyFey(Croatis.), s.), Cannes Cannes Cannes 閲閲Cannes Cannes Cannes 攵' ' please,' ) } ], ) UpperCamelCase : Dict = text_generator(['This is a test', 'This is a second test'], do_sample=SCREAMING_SNAKE_CASE_ ) self.assertEqual( SCREAMING_SNAKE_CASE_, [ [ { 'generated_text': ( 'This is a test FeyFeyFey(Croatis.), s.), Cannes Cannes Cannes 閲閲Cannes Cannes Cannes 攵' ' please,' ) } ], [ { 'generated_text': ( 'This is a second test Chieftain Chieftain prefecture prefecture prefecture Cannes Cannes' ' Cannes 閲閲Cannes Cannes Cannes 攵 please,' ) } ], ], ) def snake_case_ ( self, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ ) -> int: UpperCamelCase : List[Any] = TextGenerationPipeline(model=SCREAMING_SNAKE_CASE_, tokenizer=SCREAMING_SNAKE_CASE_ ) return text_generator, ["This is a test", "Another test"] def snake_case_ ( self ) -> List[Any]: UpperCamelCase : Tuple = 'Hello I believe in' UpperCamelCase : Optional[int] = pipeline('text-generation', model='hf-internal-testing/tiny-random-gpt2' ) UpperCamelCase : List[str] = text_generator(SCREAMING_SNAKE_CASE_ ) self.assertEqual( SCREAMING_SNAKE_CASE_, [{'generated_text': 'Hello I believe in fe fe fe fe fe fe fe fe fe fe fe fe'}], ) UpperCamelCase : Optional[Any] = text_generator(SCREAMING_SNAKE_CASE_, stop_sequence=' fe' ) self.assertEqual(SCREAMING_SNAKE_CASE_, [{'generated_text': 'Hello I believe in fe'}] ) def snake_case_ ( self, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ ) -> int: UpperCamelCase : Optional[int] = text_generator.model UpperCamelCase : List[Any] = text_generator.tokenizer UpperCamelCase : Union[str, Any] = text_generator('This is a test' ) self.assertEqual(SCREAMING_SNAKE_CASE_, [{'generated_text': ANY(SCREAMING_SNAKE_CASE_ )}] ) self.assertTrue(outputs[0]['generated_text'].startswith('This is a test' ) ) UpperCamelCase : Optional[int] = text_generator('This is a test', return_full_text=SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_, [{'generated_text': ANY(SCREAMING_SNAKE_CASE_ )}] ) self.assertNotIn('This is a test', outputs[0]['generated_text'] ) UpperCamelCase : Tuple = pipeline(task='text-generation', model=SCREAMING_SNAKE_CASE_, tokenizer=SCREAMING_SNAKE_CASE_, return_full_text=SCREAMING_SNAKE_CASE_ ) UpperCamelCase : int = text_generator('This is a test' ) self.assertEqual(SCREAMING_SNAKE_CASE_, [{'generated_text': ANY(SCREAMING_SNAKE_CASE_ )}] ) self.assertNotIn('This is a test', outputs[0]['generated_text'] ) UpperCamelCase : Dict = text_generator('This is a test', return_full_text=SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_, [{'generated_text': ANY(SCREAMING_SNAKE_CASE_ )}] ) self.assertTrue(outputs[0]['generated_text'].startswith('This is a test' ) ) UpperCamelCase : List[Any] = text_generator(['This is great !', 'Something else'], num_return_sequences=2, do_sample=SCREAMING_SNAKE_CASE_ ) self.assertEqual( SCREAMING_SNAKE_CASE_, [ [{'generated_text': ANY(SCREAMING_SNAKE_CASE_ )}, {'generated_text': ANY(SCREAMING_SNAKE_CASE_ )}], [{'generated_text': ANY(SCREAMING_SNAKE_CASE_ )}, {'generated_text': ANY(SCREAMING_SNAKE_CASE_ )}], ], ) if text_generator.tokenizer.pad_token is not None: UpperCamelCase : Optional[Any] = text_generator( ['This is great !', 'Something else'], num_return_sequences=2, batch_size=2, do_sample=SCREAMING_SNAKE_CASE_ ) self.assertEqual( SCREAMING_SNAKE_CASE_, [ [{'generated_text': ANY(SCREAMING_SNAKE_CASE_ )}, {'generated_text': ANY(SCREAMING_SNAKE_CASE_ )}], [{'generated_text': ANY(SCREAMING_SNAKE_CASE_ )}, {'generated_text': ANY(SCREAMING_SNAKE_CASE_ )}], ], ) with self.assertRaises(SCREAMING_SNAKE_CASE_ ): UpperCamelCase : Any = text_generator('test', return_full_text=SCREAMING_SNAKE_CASE_, return_text=SCREAMING_SNAKE_CASE_ ) with self.assertRaises(SCREAMING_SNAKE_CASE_ ): UpperCamelCase : Any = text_generator('test', return_full_text=SCREAMING_SNAKE_CASE_, return_tensors=SCREAMING_SNAKE_CASE_ ) with self.assertRaises(SCREAMING_SNAKE_CASE_ ): UpperCamelCase : int = text_generator('test', return_text=SCREAMING_SNAKE_CASE_, return_tensors=SCREAMING_SNAKE_CASE_ ) # Empty prompt is slighly special # it requires BOS token to exist. # Special case for Pegasus which will always append EOS so will # work even without BOS. if ( text_generator.tokenizer.bos_token_id is not None or "Pegasus" in tokenizer.__class__.__name__ or "Git" in model.__class__.__name__ ): UpperCamelCase : Dict = text_generator('' ) self.assertEqual(SCREAMING_SNAKE_CASE_, [{'generated_text': ANY(SCREAMING_SNAKE_CASE_ )}] ) else: with self.assertRaises((ValueError, AssertionError) ): UpperCamelCase : Optional[Any] = text_generator('' ) if text_generator.framework == "tf": # TF generation does not support max_new_tokens, and it's impossible # to control long generation with only max_length without # fancy calculation, dismissing tests for now. return # We don't care about infinite range models. # They already work. # Skip this test for XGLM, since it uses sinusoidal positional embeddings which are resized on-the-fly. UpperCamelCase : str = ['RwkvForCausalLM', 'XGLMForCausalLM', 'GPTNeoXForCausalLM'] if ( tokenizer.model_max_length < 1_0000 and text_generator.model.__class__.__name__ not in EXTRA_MODELS_CAN_HANDLE_LONG_INPUTS ): # Handling of large generations with self.assertRaises((RuntimeError, IndexError, ValueError, AssertionError) ): text_generator('This is a test' * 500, max_new_tokens=20 ) UpperCamelCase : Dict = text_generator('This is a test' * 500, handle_long_generation='hole', max_new_tokens=20 ) # Hole strategy cannot work with self.assertRaises(SCREAMING_SNAKE_CASE_ ): text_generator( 'This is a test' * 500, handle_long_generation='hole', max_new_tokens=tokenizer.model_max_length + 10, ) @require_torch @require_accelerate @require_torch_gpu def snake_case_ ( self ) -> Any: import torch # Classic `model_kwargs` UpperCamelCase : int = pipeline( model='hf-internal-testing/tiny-random-bloom', model_kwargs={'device_map': 'auto', 'torch_dtype': torch.bfloataa}, ) self.assertEqual(pipe.model.device, torch.device(0 ) ) self.assertEqual(pipe.model.lm_head.weight.dtype, torch.bfloataa ) UpperCamelCase : Any = pipe('This is a test' ) self.assertEqual( SCREAMING_SNAKE_CASE_, [ { 'generated_text': ( 'This is a test test test test test test test test test test test test test test test test' ' test' ) } ], ) # Upgraded those two to real pipeline arguments (they just get sent for the model as they're unlikely to mean anything else.) UpperCamelCase : List[str] = pipeline(model='hf-internal-testing/tiny-random-bloom', device_map='auto', torch_dtype=torch.bfloataa ) self.assertEqual(pipe.model.device, torch.device(0 ) ) self.assertEqual(pipe.model.lm_head.weight.dtype, torch.bfloataa ) UpperCamelCase : Optional[Any] = pipe('This is a test' ) self.assertEqual( SCREAMING_SNAKE_CASE_, [ { 'generated_text': ( 'This is a test test test test test test test test test test test test test test test test' ' test' ) } ], ) # torch_dtype will be automatically set to float32 if not provided - check: https://github.com/huggingface/transformers/pull/20602 UpperCamelCase : Dict = pipeline(model='hf-internal-testing/tiny-random-bloom', device_map='auto' ) self.assertEqual(pipe.model.device, torch.device(0 ) ) self.assertEqual(pipe.model.lm_head.weight.dtype, torch.floataa ) UpperCamelCase : Optional[int] = pipe('This is a test' ) self.assertEqual( SCREAMING_SNAKE_CASE_, [ { 'generated_text': ( 'This is a test test test test test test test test test test test test test test test test' ' test' ) } ], ) @require_torch @require_torch_gpu def snake_case_ ( self ) -> Union[str, Any]: import torch UpperCamelCase : Optional[Any] = pipeline(model='hf-internal-testing/tiny-random-bloom', device=0, torch_dtype=torch.floataa ) pipe('This is a test' ) @require_torch @require_accelerate @require_torch_gpu def snake_case_ ( self ) -> int: import torch UpperCamelCase : Dict = pipeline(model='hf-internal-testing/tiny-random-bloom', device_map='auto', torch_dtype=torch.floataa ) pipe('This is a test', do_sample=SCREAMING_SNAKE_CASE_, top_p=0.5 ) def snake_case_ ( self ) -> Optional[int]: UpperCamelCase : Union[str, Any] = 'Hello world' UpperCamelCase : Optional[int] = pipeline('text-generation', model='hf-internal-testing/tiny-random-gpt2' ) if text_generator.model.framework == "tf": UpperCamelCase : Dict = logging.get_logger('transformers.generation.tf_utils' ) else: UpperCamelCase : List[Any] = logging.get_logger('transformers.generation.utils' ) UpperCamelCase : Dict = 'Both `max_new_tokens`' # The beggining of the message to be checked in this test # Both are set by the user -> log warning with CaptureLogger(SCREAMING_SNAKE_CASE_ ) as cl: UpperCamelCase : Tuple = text_generator(SCREAMING_SNAKE_CASE_, max_length=10, max_new_tokens=1 ) self.assertIn(SCREAMING_SNAKE_CASE_, cl.out ) # The user only sets one -> no warning with CaptureLogger(SCREAMING_SNAKE_CASE_ ) as cl: UpperCamelCase : Tuple = text_generator(SCREAMING_SNAKE_CASE_, max_new_tokens=1 ) self.assertNotIn(SCREAMING_SNAKE_CASE_, cl.out ) with CaptureLogger(SCREAMING_SNAKE_CASE_ ) as cl: UpperCamelCase : List[str] = text_generator(SCREAMING_SNAKE_CASE_, max_length=10 ) self.assertNotIn(SCREAMING_SNAKE_CASE_, cl.out )
40
'''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 SCREAMING_SNAKE_CASE__ ( unittest.TestCase): 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] , )-> Dict: '''simple docstring''' UpperCamelCase = parent UpperCamelCase = batch_size UpperCamelCase = num_channels UpperCamelCase = image_size UpperCamelCase = min_resolution UpperCamelCase = max_resolution UpperCamelCase = do_resize UpperCamelCase = size if size is not None else {'height': 18, 'width': 20} UpperCamelCase = do_thumbnail UpperCamelCase = do_align_axis UpperCamelCase = do_pad UpperCamelCase = do_normalize UpperCamelCase = image_mean UpperCamelCase = image_std def UpperCAmelCase_ ( self )-> List[Any]: '''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 SCREAMING_SNAKE_CASE__ ( snake_case_ , unittest.TestCase): lowerCAmelCase_ = DonutImageProcessor if is_vision_available() else None def UpperCAmelCase_ ( self )-> str: '''simple docstring''' UpperCamelCase = DonutImageProcessingTester(self ) @property def UpperCAmelCase_ ( self )-> str: '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def UpperCAmelCase_ ( self )-> Optional[int]: '''simple docstring''' UpperCamelCase = 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 UpperCAmelCase_ ( self )-> Optional[int]: '''simple docstring''' UpperCamelCase = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'height': 18, 'width': 20} ) UpperCamelCase = 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 UpperCamelCase = self.image_processing_class.from_dict(self.image_processor_dict , size=(42, 84) ) self.assertEqual(image_processor.size , {'height': 84, 'width': 42} ) def UpperCAmelCase_ ( self )-> Tuple: '''simple docstring''' pass @is_flaky() def UpperCAmelCase_ ( self )-> Any: '''simple docstring''' UpperCamelCase = self.image_processing_class(**self.image_processor_dict ) # create random PIL images UpperCamelCase = prepare_image_inputs(self.image_processor_tester , equal_resolution=A_ ) for image in image_inputs: self.assertIsInstance(A_ , Image.Image ) # Test not batched input UpperCamelCase = 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 UpperCamelCase = 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 UpperCAmelCase_ ( self )-> Optional[int]: '''simple docstring''' UpperCamelCase = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors UpperCamelCase = prepare_image_inputs(self.image_processor_tester , equal_resolution=A_ , numpify=A_ ) for image in image_inputs: self.assertIsInstance(A_ , np.ndarray ) # Test not batched input UpperCamelCase = 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 UpperCamelCase = 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 UpperCAmelCase_ ( self )-> Dict: '''simple docstring''' UpperCamelCase = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors UpperCamelCase = prepare_image_inputs(self.image_processor_tester , equal_resolution=A_ , torchify=A_ ) for image in image_inputs: self.assertIsInstance(A_ , torch.Tensor ) # Test not batched input UpperCamelCase = 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 UpperCamelCase = 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'], ) , )
3
0
'''simple docstring''' import inspect import os import re from transformers.configuration_utils import PretrainedConfig from transformers.utils import direct_transformers_import # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_config_docstrings.py lowerCAmelCase__ = '''src/transformers''' # This is to make sure the transformers module imported is the one in the repo. lowerCAmelCase__ = direct_transformers_import(PATH_TO_TRANSFORMERS) lowerCAmelCase__ = transformers.models.auto.configuration_auto.CONFIG_MAPPING lowerCAmelCase__ = { # used to compute the property `self.chunk_length` '''EncodecConfig''': ['''overlap'''], # used as `self.bert_model = BertModel(config, ...)` '''DPRConfig''': True, # not used in modeling files, but it's an important information '''FSMTConfig''': ['''langs'''], # used internally in the configuration class file '''GPTNeoConfig''': ['''attention_types'''], # used internally in the configuration class file '''EsmConfig''': ['''is_folding_model'''], # used during training (despite we don't have training script for these models yet) '''Mask2FormerConfig''': ['''ignore_value'''], # `ignore_value` used during training (despite we don't have training script for these models yet) # `norm` used in conversion script (despite not using in the modeling file) '''OneFormerConfig''': ['''ignore_value''', '''norm'''], # used during preprocessing and collation, see `collating_graphormer.py` '''GraphormerConfig''': ['''spatial_pos_max'''], # used internally in the configuration class file '''T5Config''': ['''feed_forward_proj'''], # used internally in the configuration class file # `tokenizer_class` get default value `T5Tokenizer` intentionally '''MT5Config''': ['''feed_forward_proj''', '''tokenizer_class'''], '''UMT5Config''': ['''feed_forward_proj''', '''tokenizer_class'''], # used internally in the configuration class file '''LongT5Config''': ['''feed_forward_proj'''], # used internally in the configuration class file '''SwitchTransformersConfig''': ['''feed_forward_proj'''], # having default values other than `1e-5` - we can't fix them without breaking '''BioGptConfig''': ['''layer_norm_eps'''], # having default values other than `1e-5` - we can't fix them without breaking '''GLPNConfig''': ['''layer_norm_eps'''], # having default values other than `1e-5` - we can't fix them without breaking '''SegformerConfig''': ['''layer_norm_eps'''], # having default values other than `1e-5` - we can't fix them without breaking '''CvtConfig''': ['''layer_norm_eps'''], # having default values other than `1e-5` - we can't fix them without breaking '''PerceiverConfig''': ['''layer_norm_eps'''], # used internally to calculate the feature size '''InformerConfig''': ['''num_static_real_features''', '''num_time_features'''], # used internally to calculate the feature size '''TimeSeriesTransformerConfig''': ['''num_static_real_features''', '''num_time_features'''], # used internally to calculate the feature size '''AutoformerConfig''': ['''num_static_real_features''', '''num_time_features'''], # used internally to calculate `mlp_dim` '''SamVisionConfig''': ['''mlp_ratio'''], # For (head) training, but so far not implemented '''ClapAudioConfig''': ['''num_classes'''], # Not used, but providing useful information to users '''SpeechT5HifiGanConfig''': ['''sampling_rate'''], } # TODO (ydshieh): Check the failing cases, try to fix them or move some cases to the above block once we are sure SPECIAL_CASES_TO_ALLOW.update( { '''CLIPSegConfig''': True, '''DeformableDetrConfig''': True, '''DetaConfig''': True, '''DinatConfig''': True, '''DonutSwinConfig''': True, '''EfficientFormerConfig''': True, '''FSMTConfig''': True, '''JukeboxConfig''': True, '''LayoutLMv2Config''': True, '''MaskFormerSwinConfig''': True, '''MT5Config''': True, '''NatConfig''': True, '''OneFormerConfig''': True, '''PerceiverConfig''': True, '''RagConfig''': True, '''SpeechT5Config''': True, '''SwinConfig''': True, '''Swin2SRConfig''': True, '''Swinv2Config''': True, '''SwitchTransformersConfig''': True, '''TableTransformerConfig''': True, '''TapasConfig''': True, '''TransfoXLConfig''': True, '''UniSpeechConfig''': True, '''UniSpeechSatConfig''': True, '''WavLMConfig''': True, '''WhisperConfig''': True, # TODO: @Arthur (for `alignment_head` and `alignment_layer`) '''JukeboxPriorConfig''': True, # TODO: @Younes (for `is_decoder`) '''Pix2StructTextConfig''': True, } ) def _A ( A__ , A__ , A__ , A__ ): """simple docstring""" __lowercase = False for attribute in attributes: for modeling_source in source_strings: # check if we can find `config.xxx`, `getattr(config, "xxx", ...)` or `getattr(self.config, "xxx", ...)` if ( F"config.{attribute}" in modeling_source or F"getattr(config, \"{attribute}\"" in modeling_source or F"getattr(self.config, \"{attribute}\"" in modeling_source ): __lowercase = True # Deal with multi-line cases elif ( re.search( RF"getattr[ \t\v\n\r\f]*\([ \t\v\n\r\f]*(self\.)?config,[ \t\v\n\r\f]*\"{attribute}\"" , A__ , ) is not None ): __lowercase = True # `SequenceSummary` is called with `SequenceSummary(config)` elif attribute in [ "summary_type", "summary_use_proj", "summary_activation", "summary_last_dropout", "summary_proj_to_labels", "summary_first_dropout", ]: if "SequenceSummary" in modeling_source: __lowercase = True if attribute_used: break if attribute_used: break # common and important attributes, even if they do not always appear in the modeling files __lowercase = [ '''bos_index''', '''eos_index''', '''pad_index''', '''unk_index''', '''mask_index''', '''image_size''', '''use_cache''', '''out_features''', '''out_indices''', ] __lowercase = ['''encoder_no_repeat_ngram_size'''] # Special cases to be allowed __lowercase = True if not attribute_used: __lowercase = False for attribute in attributes: # Allow if the default value in the configuration class is different from the one in `PretrainedConfig` if attribute in ["is_encoder_decoder"] and default_value is True: __lowercase = True elif attribute in ["tie_word_embeddings"] and default_value is False: __lowercase = True # Allow cases without checking the default value in the configuration class elif attribute in attributes_to_allow + attributes_used_in_generation: __lowercase = True elif attribute.endswith('''_token_id''' ): __lowercase = True # configuration class specific cases if not case_allowed: __lowercase = SPECIAL_CASES_TO_ALLOW.get(config_class.__name__ , [] ) __lowercase = allowed_cases is True or attribute in allowed_cases return attribute_used or case_allowed def _A ( A__ ): """simple docstring""" __lowercase = dict(inspect.signature(config_class.__init__ ).parameters ) __lowercase = [x for x in list(signature.keys() ) if x not in ['''self''', '''kwargs''']] __lowercase = [signature[param].default for param in parameter_names] # If `attribute_map` exists, an attribute can have different names to be used in the modeling files, and as long # as one variant is used, the test should pass __lowercase = {} if len(config_class.attribute_map ) > 0: __lowercase = {v: k for k, v in config_class.attribute_map.items()} # Get the path to modeling source files __lowercase = inspect.getsourcefile(A__ ) __lowercase = os.path.dirname(A__ ) # Let's check against all frameworks: as long as one framework uses an attribute, we are good. __lowercase = [os.path.join(A__ , A__ ) for fn in os.listdir(A__ ) if fn.startswith('''modeling_''' )] # Get the source code strings __lowercase = [] for path in modeling_paths: if os.path.isfile(A__ ): with open(A__ ) as fp: modeling_sources.append(fp.read() ) __lowercase = [] for config_param, default_value in zip(A__ , A__ ): # `attributes` here is all the variant names for `config_param` __lowercase = [config_param] # some configuration classes have non-empty `attribute_map`, and both names could be used in the # corresponding modeling files. As long as one of them appears, it is fine. if config_param in reversed_attribute_map: attributes.append(reversed_attribute_map[config_param] ) if not check_attribute_being_used(A__ , A__ , A__ , A__ ): unused_attributes.append(attributes[0] ) return sorted(A__ ) def _A ( ): """simple docstring""" __lowercase = {} for _config_class in list(CONFIG_MAPPING.values() ): # Skip deprecated models if "models.deprecated" in _config_class.__module__: continue # Some config classes are not in `CONFIG_MAPPING` (e.g. `CLIPVisionConfig`, `Blip2VisionConfig`, etc.) __lowercase = [ cls for name, cls in inspect.getmembers( inspect.getmodule(_config_class ) , lambda A__ : inspect.isclass(A__ ) and issubclass(A__ , A__ ) and inspect.getmodule(A__ ) == inspect.getmodule(_config_class ) , ) ] for config_class in config_classes_in_module: __lowercase = check_config_attributes_being_used(A__ ) if len(A__ ) > 0: __lowercase = unused_attributes if len(A__ ) > 0: __lowercase = '''The following configuration classes contain unused attributes in the corresponding modeling files:\n''' for name, attributes in configs_with_unused_attributes.items(): error += F"{name}: {attributes}\n" raise ValueError(A__ ) if __name__ == "__main__": check_config_attributes()
41
'''simple docstring''' def A_( A : list[int]): UpperCamelCase = [] if len(A) == 1: return [nums.copy()] for _ in range(len(A)): UpperCamelCase = nums.pop(0) UpperCamelCase = permute(A) for perm in permutations: perm.append(A) result.extend(A) nums.append(A) return result def A_( A : str): def backtrack(A : str): if start == len(A) - 1: output.append(nums[:]) else: for i in range(A , len(A)): UpperCamelCase , UpperCamelCase = nums[i], nums[start] backtrack(start + 1) UpperCamelCase , UpperCamelCase = nums[i], nums[start] # backtrack UpperCamelCase = [] backtrack(0) return output if __name__ == "__main__": import doctest # use res to print the data in permute2 function lowerCAmelCase : Dict = permutea([1, 2, 3]) print(res) doctest.testmod()
3
0
'''simple docstring''' from __future__ import annotations from math import gcd def _UpperCamelCase ( __UpperCamelCase ,__UpperCamelCase = 2 ,__UpperCamelCase = 1 ,__UpperCamelCase = 3 ,) -> int | None: # A value less than 2 can cause an infinite loop in the algorithm. if num < 2: raise ValueError('The input value cannot be less than 2' ) # Because of the relationship between ``f(f(x))`` and ``f(x)``, this # algorithm struggles to find factors that are divisible by two. # As a workaround, we specifically check for two and even inputs. # See: https://math.stackexchange.com/a/2856214/165820 if num > 2 and num % 2 == 0: return 2 # Pollard's Rho algorithm requires a function that returns pseudorandom # values between 0 <= X < ``num``. It doesn't need to be random in the # sense that the output value is cryptographically secure or difficult # to calculate, it only needs to be random in the sense that all output # values should be equally likely to appear. # For this reason, Pollard suggested using ``f(x) = (x**2 - 1) % num`` # However, the success of Pollard's algorithm isn't guaranteed and is # determined in part by the initial seed and the chosen random function. # To make retries easier, we will instead use ``f(x) = (x**2 + C) % num`` # where ``C`` is a value that we can modify between each attempt. def rand_fn(__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ) -> int: return (pow(__UpperCamelCase ,2 ) + step) % modulus for _ in range(__UpperCamelCase ): # These track the position within the cycle detection logic. lowerCamelCase_ = seed lowerCamelCase_ = seed while True: # At each iteration, the tortoise moves one step and the hare moves two. lowerCamelCase_ = rand_fn(__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ) lowerCamelCase_ = rand_fn(__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ) lowerCamelCase_ = rand_fn(__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ) # At some point both the tortoise and the hare will enter a cycle whose # length ``p`` is a divisor of ``num``. Once in that cycle, at some point # the tortoise and hare will end up on the same value modulo ``p``. # We can detect when this happens because the position difference between # the tortoise and the hare will share a common divisor with ``num``. lowerCamelCase_ = gcd(hare - tortoise ,__UpperCamelCase ) if divisor == 1: # No common divisor yet, just keep searching. continue else: # We found a common divisor! if divisor == num: # Unfortunately, the divisor is ``num`` itself and is useless. break else: # The divisor is a nontrivial factor of ``num``! return divisor # If we made it here, then this attempt failed. # We need to pick a new starting seed for the tortoise and hare # in addition to a new step value for the random function. # To keep this example implementation deterministic, the # new values will be generated based on currently available # values instead of using something like ``random.randint``. # We can use the hare's position as the new seed. # This is actually what Richard Brent's the "optimized" variant does. lowerCamelCase_ = hare # The new step value for the random function can just be incremented. # At first the results will be similar to what the old function would # have produced, but the value will quickly diverge after a bit. step += 1 # We haven't found a divisor within the requested number of attempts. # We were unlucky or ``num`` itself is actually prime. return None if __name__ == "__main__": import argparse A_ = argparse.ArgumentParser() parser.add_argument( "num", type=int, help="The value to find a divisor of", ) parser.add_argument( "--attempts", type=int, default=3, help="The number of attempts before giving up", ) A_ = parser.parse_args() A_ = pollard_rho(args.num, attempts=args.attempts) if divisor is None: print(f'''{args.num} is probably prime''') else: A_ = args.num // divisor print(f'''{args.num} = {divisor} * {quotient}''')
42
'''simple docstring''' import colorsys from PIL import Image # type: ignore def A_( A : float , A : float , A : int): UpperCamelCase = x UpperCamelCase = y for step in range(A): # noqa: B007 UpperCamelCase = a * a - b * b + x UpperCamelCase = 2 * a * b + y UpperCamelCase = a_new # divergence happens for all complex number with an absolute value # greater than 4 if a * a + b * b > 4: break return step / (max_step - 1) def A_( A : float): if distance == 1: return (0, 0, 0) else: return (255, 255, 255) def A_( A : float): if distance == 1: return (0, 0, 0) else: return tuple(round(i * 255) for i in colorsys.hsv_to_rgb(A , 1 , 1)) def A_( A : int = 800 , A : int = 600 , A : float = -0.6 , A : float = 0 , A : float = 3.2 , A : int = 50 , A : bool = True , ): UpperCamelCase = Image.new('RGB' , (image_width, image_height)) UpperCamelCase = img.load() # loop through the image-coordinates for image_x in range(A): for image_y in range(A): # determine the figure-coordinates based on the image-coordinates UpperCamelCase = figure_width / image_width * image_height UpperCamelCase = figure_center_x + (image_x / image_width - 0.5) * figure_width UpperCamelCase = figure_center_y + (image_y / image_height - 0.5) * figure_height UpperCamelCase = get_distance(A , A , A) # color the corresponding pixel based on the selected coloring-function if use_distance_color_coding: UpperCamelCase = get_color_coded_rgb(A) else: UpperCamelCase = get_black_and_white_rgb(A) return img if __name__ == "__main__": import doctest doctest.testmod() # colored version, full figure lowerCAmelCase : Any = get_image() # uncomment for colored version, different section, zoomed in # img = get_image(figure_center_x = -0.6, figure_center_y = -0.4, # figure_width = 0.8) # uncomment for black and white version, full figure # img = get_image(use_distance_color_coding = False) # uncomment to save the image # img.save("mandelbrot.png") img.show()
3
0
import math def _a ( SCREAMING_SNAKE_CASE ): """simple docstring""" lowercase__ = [] lowercase__ = 2 lowercase__ = int(math.sqrt(SCREAMING_SNAKE_CASE ) ) # Size of every segment lowercase__ = [True] * (end + 1) lowercase__ = [] while start <= end: if temp[start] is True: in_prime.append(SCREAMING_SNAKE_CASE ) for i in range(start * start , end + 1 , SCREAMING_SNAKE_CASE ): lowercase__ = False start += 1 prime += in_prime lowercase__ = end + 1 lowercase__ = min(2 * end , SCREAMING_SNAKE_CASE ) while low <= n: lowercase__ = [True] * (high - low + 1) for each in in_prime: lowercase__ = math.floor(low / each ) * each if t < low: t += each for j in range(SCREAMING_SNAKE_CASE , high + 1 , SCREAMING_SNAKE_CASE ): lowercase__ = False for j in range(len(SCREAMING_SNAKE_CASE ) ): if temp[j] is True: prime.append(j + low ) lowercase__ = high + 1 lowercase__ = min(high + end , SCREAMING_SNAKE_CASE ) return prime print(sieve(10**6))
43
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_torch_available, ) lowerCAmelCase : Optional[Any] = { 'configuration_falcon': ['FALCON_PRETRAINED_CONFIG_ARCHIVE_MAP', 'FalconConfig'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : str = [ 'FALCON_PRETRAINED_MODEL_ARCHIVE_LIST', 'FalconForCausalLM', 'FalconModel', 'FalconPreTrainedModel', 'FalconForSequenceClassification', 'FalconForTokenClassification', 'FalconForQuestionAnswering', ] if TYPE_CHECKING: from .configuration_falcon import FALCON_PRETRAINED_CONFIG_ARCHIVE_MAP, FalconConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_falcon import ( FALCON_PRETRAINED_MODEL_ARCHIVE_LIST, FalconForCausalLM, FalconForQuestionAnswering, FalconForSequenceClassification, FalconForTokenClassification, FalconModel, FalconPreTrainedModel, ) else: import sys lowerCAmelCase : Tuple = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
3
0
'''simple docstring''' import copy from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Audio, Features, Value from .base import TaskTemplate @dataclass(frozen=A ) class UpperCAmelCase__ ( A ): lowerCAmelCase_ = field(default='automatic-speech-recognition' , metadata={'include_in_asdict_even_if_is_default': True} ) lowerCAmelCase_ = Features({'audio': Audio()} ) lowerCAmelCase_ = Features({'transcription': Value('string' )} ) lowerCAmelCase_ = "audio" lowerCAmelCase_ = "transcription" def lowerCamelCase_ ( self : int,__A : Union[str, Any] ): if self.audio_column not in features: raise ValueError(f'Column {self.audio_column} is not present in features.' ) if not isinstance(features[self.audio_column],__A ): raise ValueError(f'Column {self.audio_column} is not an Audio type.' ) _lowerCamelCase : Optional[int] = copy.deepcopy(self ) _lowerCamelCase : int = self.input_schema.copy() _lowerCamelCase : Any = features[self.audio_column] _lowerCamelCase : Dict = input_schema return task_template @property def lowerCamelCase_ ( self : Union[str, Any] ): return {self.audio_column: "audio", self.transcription_column: "transcription"}
44
'''simple docstring''' lowerCAmelCase : Optional[Any] = { 'A': ['B', 'C', 'E'], 'B': ['A', 'D', 'E'], 'C': ['A', 'F', 'G'], 'D': ['B'], 'E': ['A', 'B', 'D'], 'F': ['C'], 'G': ['C'], } def A_( A : dict , A : str , A : Optional[Any]): UpperCamelCase = set() # keep track of all the paths to be checked UpperCamelCase = [[start]] # return path if start is goal if start == goal: return [start] # keeps looping until all possible paths have been checked while queue: # pop the first path from the queue UpperCamelCase = queue.pop(0) # get the last node from the path UpperCamelCase = path[-1] if node not in explored: UpperCamelCase = graph[node] # go through all neighbour nodes, construct a new path and # push it into the queue for neighbour in neighbours: UpperCamelCase = list(A) new_path.append(A) queue.append(A) # return path if neighbour is goal if neighbour == goal: return new_path # mark node as explored explored.add(A) # in case there's no path between the 2 nodes return [] def A_( A : dict , A : str , A : Tuple): if not graph or start not in graph or target not in graph: return -1 if start == target: return 0 UpperCamelCase = [start] UpperCamelCase = set(A) # Keep tab on distances from `start` node. UpperCamelCase = {start: 0, target: -1} while queue: UpperCamelCase = queue.pop(0) if node == target: UpperCamelCase = ( dist[node] if dist[target] == -1 else min(dist[target] , dist[node]) ) for adjacent in graph[node]: if adjacent not in visited: visited.add(A) queue.append(A) UpperCamelCase = dist[node] + 1 return dist[target] if __name__ == "__main__": print(bfs_shortest_path(demo_graph, 'G', 'D')) # returns ['G', 'C', 'A', 'B', 'D'] print(bfs_shortest_path_distance(demo_graph, 'G', 'D')) # returns 4
3
0
import inspect import os import torch from transformers import AutoModel from transformers.testing_utils import mockenv_context from transformers.trainer_utils import set_seed import accelerate from accelerate.accelerator import Accelerator from accelerate.state import AcceleratorState from accelerate.test_utils.testing import ( AccelerateTestCase, TempDirTestCase, execute_subprocess_async, require_cuda, require_fsdp, require_multi_gpu, slow, ) from accelerate.utils.constants import ( FSDP_AUTO_WRAP_POLICY, FSDP_BACKWARD_PREFETCH, FSDP_SHARDING_STRATEGY, FSDP_STATE_DICT_TYPE, ) from accelerate.utils.dataclasses import FullyShardedDataParallelPlugin from accelerate.utils.other import patch_environment set_seed(42) UpperCamelCase = "bert-base-cased" UpperCamelCase = "fp16" UpperCamelCase = "bf16" UpperCamelCase = [FPaa, BFaa] @require_fsdp @require_cuda class lowerCAmelCase_ ( lowercase ): """simple docstring""" def __a ( self :List[str] ): super().setUp() UpperCamelCase__ :str = dict( ACCELERATE_USE_FSDP="""true""" , MASTER_ADDR="""localhost""" , MASTER_PORT="""10999""" , RANK="""0""" , LOCAL_RANK="""0""" , WORLD_SIZE="""1""" , ) def __a ( self :Union[str, Any] ): from torch.distributed.fsdp.fully_sharded_data_parallel import ShardingStrategy for i, strategy in enumerate(lowerCamelCase__ ): UpperCamelCase__ :Optional[int] = self.dist_env.copy() UpperCamelCase__ :List[Any] = f"""{i + 1}""" UpperCamelCase__ :List[Any] = strategy with mockenv_context(**lowerCamelCase__ ): UpperCamelCase__ :Tuple = FullyShardedDataParallelPlugin() self.assertEqual(fsdp_plugin.sharding_strategy , ShardingStrategy(i + 1 ) ) def __a ( self :Union[str, Any] ): from torch.distributed.fsdp.fully_sharded_data_parallel import BackwardPrefetch for i, prefetch_policy in enumerate(lowerCamelCase__ ): UpperCamelCase__ :Optional[int] = self.dist_env.copy() UpperCamelCase__ :Optional[int] = prefetch_policy with mockenv_context(**lowerCamelCase__ ): UpperCamelCase__ :Dict = FullyShardedDataParallelPlugin() if prefetch_policy == "NO_PREFETCH": self.assertIsNone(fsdp_plugin.backward_prefetch ) else: self.assertEqual(fsdp_plugin.backward_prefetch , BackwardPrefetch(i + 1 ) ) def __a ( self :Optional[Any] ): from torch.distributed.fsdp.fully_sharded_data_parallel import StateDictType for i, state_dict_type in enumerate(lowerCamelCase__ ): UpperCamelCase__ :Optional[int] = self.dist_env.copy() UpperCamelCase__ :Tuple = state_dict_type with mockenv_context(**lowerCamelCase__ ): UpperCamelCase__ :List[str] = FullyShardedDataParallelPlugin() self.assertEqual(fsdp_plugin.state_dict_type , StateDictType(i + 1 ) ) if state_dict_type == "FULL_STATE_DICT": self.assertTrue(fsdp_plugin.state_dict_config.offload_to_cpu ) self.assertTrue(fsdp_plugin.state_dict_config.ranka_only ) def __a ( self :List[str] ): UpperCamelCase__ :List[Any] = AutoModel.from_pretrained(lowerCamelCase__ ) for policy in FSDP_AUTO_WRAP_POLICY: UpperCamelCase__ :Optional[int] = self.dist_env.copy() UpperCamelCase__ :int = policy if policy == "TRANSFORMER_BASED_WRAP": UpperCamelCase__ :Optional[Any] = """BertLayer""" elif policy == "SIZE_BASED_WRAP": UpperCamelCase__ :Union[str, Any] = """2000""" with mockenv_context(**lowerCamelCase__ ): UpperCamelCase__ :int = FullyShardedDataParallelPlugin() fsdp_plugin.set_auto_wrap_policy(lowerCamelCase__ ) if policy == "NO_WRAP": self.assertIsNone(fsdp_plugin.auto_wrap_policy ) else: self.assertIsNotNone(fsdp_plugin.auto_wrap_policy ) UpperCamelCase__ :Optional[int] = self.dist_env.copy() UpperCamelCase__ :str = """TRANSFORMER_BASED_WRAP""" UpperCamelCase__ :Union[str, Any] = """T5Layer""" with mockenv_context(**lowerCamelCase__ ): UpperCamelCase__ :Any = FullyShardedDataParallelPlugin() with self.assertRaises(lowerCamelCase__ ) as cm: fsdp_plugin.set_auto_wrap_policy(lowerCamelCase__ ) self.assertTrue("""Could not find the transformer layer class to wrap in the model.""" in str(cm.exception ) ) UpperCamelCase__ :Dict = self.dist_env.copy() UpperCamelCase__ :int = """SIZE_BASED_WRAP""" UpperCamelCase__ :Union[str, Any] = """0""" with mockenv_context(**lowerCamelCase__ ): UpperCamelCase__ :Optional[Any] = FullyShardedDataParallelPlugin() fsdp_plugin.set_auto_wrap_policy(lowerCamelCase__ ) self.assertIsNone(fsdp_plugin.auto_wrap_policy ) def __a ( self :Optional[Any] ): from torch.distributed.fsdp.fully_sharded_data_parallel import MixedPrecision from torch.distributed.fsdp.sharded_grad_scaler import ShardedGradScaler for mp_dtype in dtypes: UpperCamelCase__ :Dict = self.dist_env.copy() UpperCamelCase__ :Dict = mp_dtype with mockenv_context(**lowerCamelCase__ ): UpperCamelCase__ :Optional[Any] = Accelerator() if mp_dtype == "fp16": UpperCamelCase__ :Tuple = torch.floataa elif mp_dtype == "bf16": UpperCamelCase__ :Tuple = torch.bfloataa UpperCamelCase__ :int = MixedPrecision(param_dtype=lowerCamelCase__ , reduce_dtype=lowerCamelCase__ , buffer_dtype=lowerCamelCase__ ) self.assertEqual(accelerator.state.fsdp_plugin.mixed_precision_policy , lowerCamelCase__ ) if mp_dtype == FPaa: self.assertTrue(isinstance(accelerator.scaler , lowerCamelCase__ ) ) elif mp_dtype == BFaa: self.assertIsNone(accelerator.scaler ) AcceleratorState._reset_state(lowerCamelCase__ ) def __a ( self :Optional[Any] ): from torch.distributed.fsdp.fully_sharded_data_parallel import CPUOffload for flag in [True, False]: UpperCamelCase__ :List[str] = self.dist_env.copy() UpperCamelCase__ :Dict = str(lowerCamelCase__ ).lower() with mockenv_context(**lowerCamelCase__ ): UpperCamelCase__ :List[str] = FullyShardedDataParallelPlugin() self.assertEqual(fsdp_plugin.cpu_offload , CPUOffload(offload_params=lowerCamelCase__ ) ) @require_fsdp @require_multi_gpu @slow class lowerCAmelCase_ ( lowercase ): """simple docstring""" def __a ( self :Dict ): super().setUp() UpperCamelCase__ :str = 0.82 UpperCamelCase__ :int = [ """fsdp_shard_grad_op_transformer_based_wrap""", """fsdp_full_shard_transformer_based_wrap""", ] UpperCamelCase__ :int = { """multi_gpu_fp16""": 32_00, """fsdp_shard_grad_op_transformer_based_wrap_fp16""": 20_00, """fsdp_full_shard_transformer_based_wrap_fp16""": 19_00, # Disabling below test as it overwhelms the RAM memory usage # on CI self-hosted runner leading to tests getting killed. # "fsdp_full_shard_cpu_offload_transformer_based_wrap_fp32": 1500, # fp16 was leading to indefinite hang } UpperCamelCase__ :Optional[Any] = 1_60 UpperCamelCase__ :List[str] = 1_60 UpperCamelCase__ :Union[str, Any] = inspect.getfile(accelerate.test_utils ) UpperCamelCase__ :Dict = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ["""scripts""", """external_deps"""] ) def __a ( self :str ): UpperCamelCase__ :int = os.path.join(self.test_scripts_folder , """test_performance.py""" ) UpperCamelCase__ :List[str] = ["""accelerate""", """launch""", """--num_processes=2""", """--num_machines=1""", """--machine_rank=0""", """--use_fsdp"""] for config in self.performance_configs: UpperCamelCase__ :Optional[Any] = cmd.copy() for i, strategy in enumerate(lowerCamelCase__ ): if strategy.lower() in config: cmd_config.append(f"""--fsdp_sharding_strategy={i+1}""" ) break if "fp32" in config: cmd_config.append("""--mixed_precision=no""" ) else: cmd_config.append("""--mixed_precision=fp16""" ) if "cpu_offload" in config: cmd_config.append("""--fsdp_offload_params=True""" ) for policy in FSDP_AUTO_WRAP_POLICY: if policy.lower() in config: cmd_config.append(f"""--fsdp_auto_wrap_policy={policy}""" ) break if policy == "TRANSFORMER_BASED_WRAP": cmd_config.append("""--fsdp_transformer_layer_cls_to_wrap=BertLayer""" ) elif policy == "SIZE_BASED_WRAP": cmd_config.append("""--fsdp_min_num_params=2000""" ) cmd_config.extend( [ self.test_file_path, f"""--output_dir={self.tmpdir}""", f"""--performance_lower_bound={self.performance_lower_bound}""", ] ) with patch_environment(omp_num_threads=1 ): execute_subprocess_async(lowerCamelCase__ , env=os.environ.copy() ) def __a ( self :str ): UpperCamelCase__ :List[Any] = os.path.join(self.test_scripts_folder , """test_checkpointing.py""" ) UpperCamelCase__ :Any = [ """accelerate""", """launch""", """--num_processes=2""", """--num_machines=1""", """--machine_rank=0""", """--use_fsdp""", """--mixed_precision=fp16""", """--fsdp_transformer_layer_cls_to_wrap=BertLayer""", ] for i, strategy in enumerate(lowerCamelCase__ ): UpperCamelCase__ :Optional[Any] = cmd.copy() cmd_config.append(f"""--fsdp_sharding_strategy={i+1}""" ) if strategy != "FULL_SHARD": continue UpperCamelCase__ :Optional[int] = len(lowerCamelCase__ ) for state_dict_type in FSDP_STATE_DICT_TYPE: UpperCamelCase__ :Tuple = cmd_config[:state_dict_config_index] cmd_config.append(f"""--fsdp_state_dict_type={state_dict_type}""" ) cmd_config.extend( [ self.test_file_path, f"""--output_dir={self.tmpdir}""", """--partial_train_epoch=1""", ] ) with patch_environment(omp_num_threads=1 ): execute_subprocess_async(lowerCamelCase__ , env=os.environ.copy() ) UpperCamelCase__ :List[Any] = cmd_config[:-1] UpperCamelCase__ :Tuple = os.path.join(self.tmpdir , """epoch_0""" ) cmd_config.extend( [ f"""--resume_from_checkpoint={resume_from_checkpoint}""", ] ) with patch_environment(omp_num_threads=1 ): execute_subprocess_async(lowerCamelCase__ , env=os.environ.copy() ) def __a ( self :List[str] ): UpperCamelCase__ :List[str] = os.path.join(self.test_scripts_folder , """test_peak_memory_usage.py""" ) UpperCamelCase__ :Optional[int] = [ """accelerate""", """launch""", """--num_processes=2""", """--num_machines=1""", """--machine_rank=0""", ] for spec, peak_mem_upper_bound in self.peak_memory_usage_upper_bound.items(): UpperCamelCase__ :Optional[int] = cmd.copy() if "fp16" in spec: cmd_config.extend(["""--mixed_precision=fp16"""] ) else: cmd_config.extend(["""--mixed_precision=no"""] ) if "multi_gpu" in spec: continue else: cmd_config.extend(["""--use_fsdp"""] ) for i, strategy in enumerate(lowerCamelCase__ ): if strategy.lower() in spec: cmd_config.append(f"""--fsdp_sharding_strategy={i+1}""" ) break if "cpu_offload" in spec: cmd_config.append("""--fsdp_offload_params=True""" ) for policy in FSDP_AUTO_WRAP_POLICY: if policy.lower() in spec: cmd_config.append(f"""--fsdp_auto_wrap_policy={policy}""" ) break if policy == "TRANSFORMER_BASED_WRAP": cmd_config.append("""--fsdp_transformer_layer_cls_to_wrap=BertLayer""" ) elif policy == "SIZE_BASED_WRAP": cmd_config.append("""--fsdp_min_num_params=2000""" ) cmd_config.extend( [ self.test_file_path, f"""--output_dir={self.tmpdir}""", f"""--peak_memory_upper_bound={peak_mem_upper_bound}""", f"""--n_train={self.n_train}""", f"""--n_val={self.n_val}""", ] ) with patch_environment(omp_num_threads=1 ): execute_subprocess_async(lowerCamelCase__ , env=os.environ.copy() )
45
'''simple docstring''' import copy import os import cva import numpy as np from matplotlib import pyplot as plt class SCREAMING_SNAKE_CASE__ : def __init__( self )-> Dict: '''simple docstring''' UpperCamelCase = '' UpperCamelCase = '' UpperCamelCase = [] UpperCamelCase = 0 UpperCamelCase = 256 UpperCamelCase = 0 UpperCamelCase = 0 UpperCamelCase = 0 UpperCamelCase = 0 def UpperCAmelCase_ ( self , A_ )-> str: '''simple docstring''' UpperCamelCase = cva.imread(A_ , 0 ) UpperCamelCase = copy.deepcopy(self.img ) UpperCamelCase , UpperCamelCase , UpperCamelCase = plt.hist(self.img.ravel() , 256 , [0, 256] , label='x' ) UpperCamelCase = np.sum(A_ ) for i in range(len(A_ ) ): UpperCamelCase = x[i] / self.k self.sk += prk UpperCamelCase = (self.L - 1) * self.sk if self.rem != 0: UpperCamelCase = int(last % last ) UpperCamelCase = int(last + 1 if self.rem >= 0.5 else last ) self.last_list.append(A_ ) UpperCamelCase = int(np.ma.count(self.img ) / self.img[1].size ) UpperCamelCase = self.img[1].size for i in range(self.number_of_cols ): for j in range(self.number_of_rows ): UpperCamelCase = self.img[j][i] if num != self.last_list[num]: UpperCamelCase = self.last_list[num] cva.imwrite('output_data/output.jpg' , self.img ) def UpperCAmelCase_ ( self )-> Any: '''simple docstring''' plt.hist(self.img.ravel() , 256 , [0, 256] ) def UpperCAmelCase_ ( self )-> Optional[Any]: '''simple docstring''' cva.imshow('Output-Image' , self.img ) cva.imshow('Input-Image' , self.original_image ) cva.waitKey(5000 ) cva.destroyAllWindows() if __name__ == "__main__": lowerCAmelCase : Union[str, Any] = os.path.join(os.path.basename(__file__), 'image_data/input.jpg') lowerCAmelCase : str = ConstantStretch() stretcher.stretch(file_path) stretcher.plot_histogram() stretcher.show_image()
3
0
"""simple docstring""" def lowerCamelCase_( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) -> float: '''simple docstring''' _lowerCamelCase : Optional[Any] = (num_of_terms / 2) * (2 * first_term + (num_of_terms - 1) * common_diff) # formula for sum of series return total def lowerCamelCase_( ) -> Optional[Any]: '''simple docstring''' print(sum_of_series(1 , 1 , 10 ) ) if __name__ == "__main__": import doctest doctest.testmod()
46
'''simple docstring''' import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase : int = logging.get_logger(__name__) lowerCAmelCase : Tuple = { 'microsoft/unispeech-sat-base-100h-libri-ft': ( 'https://huggingface.co/microsoft/unispeech-sat-base-100h-libri-ft/resolve/main/config.json' ), # See all UniSpeechSat models at https://huggingface.co/models?filter=unispeech_sat } class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = """unispeech-sat""" def __init__( self , A_=32 , A_=768 , A_=12 , A_=12 , A_=3072 , A_="gelu" , A_=0.1 , A_=0.1 , A_=0.1 , A_=0.0 , A_=0.0 , A_=0.1 , A_=0.1 , A_=0.02 , A_=1e-5 , A_="group" , A_="gelu" , A_=(512, 512, 512, 512, 512, 512, 512) , A_=(5, 2, 2, 2, 2, 2, 2) , A_=(10, 3, 3, 3, 3, 2, 2) , A_=False , A_=128 , A_=16 , A_=False , A_=True , A_=0.05 , A_=10 , A_=2 , A_=0.0 , A_=10 , A_=0 , A_=320 , A_=2 , A_=0.1 , A_=100 , A_=256 , A_=256 , A_=0.1 , A_="mean" , A_=False , A_=False , A_=256 , A_=(512, 512, 512, 512, 1500) , A_=(5, 3, 3, 1, 1) , A_=(1, 2, 3, 1, 1) , A_=512 , A_=0 , A_=1 , A_=2 , A_=504 , **A_ , )-> Tuple: '''simple docstring''' super().__init__(**A_ , pad_token_id=A_ , bos_token_id=A_ , eos_token_id=A_ ) UpperCamelCase = hidden_size UpperCamelCase = feat_extract_norm UpperCamelCase = feat_extract_activation UpperCamelCase = list(A_ ) UpperCamelCase = list(A_ ) UpperCamelCase = list(A_ ) UpperCamelCase = conv_bias UpperCamelCase = num_conv_pos_embeddings UpperCamelCase = num_conv_pos_embedding_groups UpperCamelCase = len(self.conv_dim ) UpperCamelCase = num_hidden_layers UpperCamelCase = intermediate_size UpperCamelCase = hidden_act UpperCamelCase = num_attention_heads UpperCamelCase = hidden_dropout UpperCamelCase = attention_dropout UpperCamelCase = activation_dropout UpperCamelCase = feat_proj_dropout UpperCamelCase = final_dropout UpperCamelCase = layerdrop UpperCamelCase = layer_norm_eps UpperCamelCase = initializer_range UpperCamelCase = vocab_size UpperCamelCase = num_clusters UpperCamelCase = do_stable_layer_norm UpperCamelCase = use_weighted_layer_sum 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)`, but is `len(config.conv_dim) =' F''' {len(self.conv_dim )}`, `len(config.conv_stride) = {len(self.conv_stride )}`,''' F''' `len(config.conv_kernel) = {len(self.conv_kernel )}`.''' ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 UpperCamelCase = apply_spec_augment UpperCamelCase = mask_time_prob UpperCamelCase = mask_time_length UpperCamelCase = mask_time_min_masks UpperCamelCase = mask_feature_prob UpperCamelCase = mask_feature_length UpperCamelCase = mask_feature_min_masks # parameters for pretraining with codevector quantized representations UpperCamelCase = num_codevectors_per_group UpperCamelCase = num_codevector_groups UpperCamelCase = contrastive_logits_temperature UpperCamelCase = feat_quantizer_dropout UpperCamelCase = num_negatives UpperCamelCase = codevector_dim UpperCamelCase = proj_codevector_dim UpperCamelCase = diversity_loss_weight # ctc loss UpperCamelCase = ctc_loss_reduction UpperCamelCase = ctc_zero_infinity # SequenceClassification-specific parameter. Feel free to ignore for other classes. UpperCamelCase = classifier_proj_size # XVector-specific parameters. Feel free to ignore for other classes. UpperCamelCase = list(A_ ) UpperCamelCase = list(A_ ) UpperCamelCase = list(A_ ) UpperCamelCase = xvector_output_dim @property def UpperCAmelCase_ ( self )-> Optional[Any]: '''simple docstring''' return functools.reduce(operator.mul , self.conv_stride , 1 )
3
0
from binascii import hexlify from hashlib import shaaaa from os import urandom # RFC 3526 - More Modular Exponential (MODP) Diffie-Hellman groups for # Internet Key Exchange (IKE) https://tools.ietf.org/html/rfc3526 SCREAMING_SNAKE_CASE__ = { # 1536-bit 5: { '''prime''': int( '''FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1''' + '''29024E088A67CC74020BBEA63B139B22514A08798E3404DD''' + '''EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245''' + '''E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED''' + '''EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D''' + '''C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F''' + '''83655D23DCA3AD961C62F356208552BB9ED529077096966D''' + '''670C354E4ABC9804F1746C08CA237327FFFFFFFFFFFFFFFF''', base=16, ), '''generator''': 2, }, # 2048-bit 14: { '''prime''': int( '''FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1''' + '''29024E088A67CC74020BBEA63B139B22514A08798E3404DD''' + '''EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245''' + '''E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED''' + '''EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D''' + '''C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F''' + '''83655D23DCA3AD961C62F356208552BB9ED529077096966D''' + '''670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B''' + '''E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9''' + '''DE2BCBF6955817183995497CEA956AE515D2261898FA0510''' + '''15728E5A8AACAA68FFFFFFFFFFFFFFFF''', base=16, ), '''generator''': 2, }, # 3072-bit 15: { '''prime''': int( '''FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1''' + '''29024E088A67CC74020BBEA63B139B22514A08798E3404DD''' + '''EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245''' + '''E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED''' + '''EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D''' + '''C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F''' + '''83655D23DCA3AD961C62F356208552BB9ED529077096966D''' + '''670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B''' + '''E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9''' + '''DE2BCBF6955817183995497CEA956AE515D2261898FA0510''' + '''15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64''' + '''ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7''' + '''ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B''' + '''F12FFA06D98A0864D87602733EC86A64521F2B18177B200C''' + '''BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31''' + '''43DB5BFCE0FD108E4B82D120A93AD2CAFFFFFFFFFFFFFFFF''', base=16, ), '''generator''': 2, }, # 4096-bit 16: { '''prime''': int( '''FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1''' + '''29024E088A67CC74020BBEA63B139B22514A08798E3404DD''' + '''EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245''' + '''E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED''' + '''EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D''' + '''C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F''' + '''83655D23DCA3AD961C62F356208552BB9ED529077096966D''' + '''670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B''' + '''E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9''' + '''DE2BCBF6955817183995497CEA956AE515D2261898FA0510''' + '''15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64''' + '''ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7''' + '''ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B''' + '''F12FFA06D98A0864D87602733EC86A64521F2B18177B200C''' + '''BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31''' + '''43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7''' + '''88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA''' + '''2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6''' + '''287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED''' + '''1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9''' + '''93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934063199''' + '''FFFFFFFFFFFFFFFF''', base=16, ), '''generator''': 2, }, # 6144-bit 17: { '''prime''': int( '''FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E08''' + '''8A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B''' + '''302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9''' + '''A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE6''' + '''49286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8''' + '''FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D''' + '''670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C''' + '''180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF695581718''' + '''3995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D''' + '''04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7D''' + '''B3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D226''' + '''1AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200C''' + '''BBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFC''' + '''E0FD108E4B82D120A92108011A723C12A787E6D788719A10BDBA5B26''' + '''99C327186AF4E23C1A946834B6150BDA2583E9CA2AD44CE8DBBBC2DB''' + '''04DE8EF92E8EFC141FBECAA6287C59474E6BC05D99B2964FA090C3A2''' + '''233BA186515BE7ED1F612970CEE2D7AFB81BDD762170481CD0069127''' + '''D5B05AA993B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492''' + '''36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BDF8FF9406''' + '''AD9E530EE5DB382F413001AEB06A53ED9027D831179727B0865A8918''' + '''DA3EDBEBCF9B14ED44CE6CBACED4BB1BDB7F1447E6CC254B33205151''' + '''2BD7AF426FB8F401378CD2BF5983CA01C64B92ECF032EA15D1721D03''' + '''F482D7CE6E74FEF6D55E702F46980C82B5A84031900B1C9E59E7C97F''' + '''BEC7E8F323A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA''' + '''CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE32806A1D58B''' + '''B7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55CDA56C9EC2EF29632''' + '''387FE8D76E3C0468043E8F663F4860EE12BF2D5B0B7474D6E694F91E''' + '''6DCC4024FFFFFFFFFFFFFFFF''', base=16, ), '''generator''': 2, }, # 8192-bit 18: { '''prime''': int( '''FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1''' + '''29024E088A67CC74020BBEA63B139B22514A08798E3404DD''' + '''EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245''' + '''E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED''' + '''EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D''' + '''C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F''' + '''83655D23DCA3AD961C62F356208552BB9ED529077096966D''' + '''670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B''' + '''E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9''' + '''DE2BCBF6955817183995497CEA956AE515D2261898FA0510''' + '''15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64''' + '''ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7''' + '''ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B''' + '''F12FFA06D98A0864D87602733EC86A64521F2B18177B200C''' + '''BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31''' + '''43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7''' + '''88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA''' + '''2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6''' + '''287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED''' + '''1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9''' + '''93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492''' + '''36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BD''' + '''F8FF9406AD9E530EE5DB382F413001AEB06A53ED9027D831''' + '''179727B0865A8918DA3EDBEBCF9B14ED44CE6CBACED4BB1B''' + '''DB7F1447E6CC254B332051512BD7AF426FB8F401378CD2BF''' + '''5983CA01C64B92ECF032EA15D1721D03F482D7CE6E74FEF6''' + '''D55E702F46980C82B5A84031900B1C9E59E7C97FBEC7E8F3''' + '''23A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA''' + '''CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE328''' + '''06A1D58BB7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55C''' + '''DA56C9EC2EF29632387FE8D76E3C0468043E8F663F4860EE''' + '''12BF2D5B0B7474D6E694F91E6DBE115974A3926F12FEE5E4''' + '''38777CB6A932DF8CD8BEC4D073B931BA3BC832B68D9DD300''' + '''741FA7BF8AFC47ED2576F6936BA424663AAB639C5AE4F568''' + '''3423B4742BF1C978238F16CBE39D652DE3FDB8BEFC848AD9''' + '''22222E04A4037C0713EB57A81A23F0C73473FC646CEA306B''' + '''4BCBC8862F8385DDFA9D4B7FA2C087E879683303ED5BDD3A''' + '''062B3CF5B3A278A66D2A13F83F44F82DDF310EE074AB6A36''' + '''4597E899A0255DC164F31CC50846851DF9AB48195DED7EA1''' + '''B1D510BD7EE74D73FAF36BC31ECFA268359046F4EB879F92''' + '''4009438B481C6CD7889A002ED5EE382BC9190DA6FC026E47''' + '''9558E4475677E9AA9E3050E2765694DFC81F56E880B96E71''' + '''60C980DD98EDD3DFFFFFFFFFFFFFFFFF''', base=16, ), '''generator''': 2, }, } class _UpperCamelCase: def __init__( self : List[Any] , SCREAMING_SNAKE_CASE__ : int = 1_4 ): '''simple docstring''' if group not in primes: raise ValueError('Unsupported Group' ) __a : List[str] = primes[group]['prime'] __a : Optional[Any] = primes[group]['generator'] __a : List[Any] = int(hexlify(urandom(3_2 ) ) , base=1_6 ) def __lowerCAmelCase ( self : int ): '''simple docstring''' return hex(self.__private_key )[2:] def __lowerCAmelCase ( self : Tuple ): '''simple docstring''' __a : List[Any] = pow(self.generator , self.__private_key , self.prime ) return hex(SCREAMING_SNAKE_CASE__ )[2:] def __lowerCAmelCase ( self : List[str] , SCREAMING_SNAKE_CASE__ : int ): '''simple docstring''' return ( 2 <= key <= self.prime - 2 and pow(SCREAMING_SNAKE_CASE__ , (self.prime - 1) // 2 , self.prime ) == 1 ) def __lowerCAmelCase ( self : Dict , SCREAMING_SNAKE_CASE__ : str ): '''simple docstring''' __a : int = int(SCREAMING_SNAKE_CASE__ , base=1_6 ) if not self.is_valid_public_key(SCREAMING_SNAKE_CASE__ ): raise ValueError('Invalid public key' ) __a : Optional[int] = pow(SCREAMING_SNAKE_CASE__ , self.__private_key , self.prime ) return shaaaa(str(SCREAMING_SNAKE_CASE__ ).encode() ).hexdigest() @staticmethod def __lowerCAmelCase ( SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : int ): '''simple docstring''' return ( 2 <= remote_public_key_str <= prime - 2 and pow(SCREAMING_SNAKE_CASE__ , (prime - 1) // 2 , SCREAMING_SNAKE_CASE__ ) == 1 ) @staticmethod def __lowerCAmelCase ( SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : int = 1_4 ): '''simple docstring''' __a : Optional[int] = int(SCREAMING_SNAKE_CASE__ , base=1_6 ) __a : Any = int(SCREAMING_SNAKE_CASE__ , base=1_6 ) __a : List[str] = primes[group]['prime'] if not DiffieHellman.is_valid_public_key_static(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): raise ValueError('Invalid public key' ) __a : Any = pow(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) return shaaaa(str(SCREAMING_SNAKE_CASE__ ).encode() ).hexdigest() if __name__ == "__main__": import doctest doctest.testmod()
47
'''simple docstring''' import inspect import unittest from datasets import load_dataset from packaging import version from transformers import BeitConfig from transformers.models.auto import get_values from transformers.testing_utils import require_torch, require_torch_multi_gpu, 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, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ( MODEL_MAPPING, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation, BeitModel, ) from transformers.models.beit.modeling_beit import BEIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): import PIL from PIL import Image from transformers import BeitImageProcessor class SCREAMING_SNAKE_CASE__ : def __init__( self , A_ , A_=100 , A_=13 , A_=30 , A_=2 , A_=3 , A_=True , A_=True , A_=32 , A_=4 , A_=4 , A_=37 , A_="gelu" , A_=0.1 , A_=0.1 , A_=10 , A_=0.02 , A_=3 , A_=None , A_=[0, 1, 2, 3] , )-> Any: '''simple docstring''' UpperCamelCase = parent UpperCamelCase = 100 UpperCamelCase = batch_size UpperCamelCase = image_size UpperCamelCase = patch_size UpperCamelCase = num_channels UpperCamelCase = is_training UpperCamelCase = use_labels UpperCamelCase = hidden_size UpperCamelCase = num_hidden_layers UpperCamelCase = num_attention_heads UpperCamelCase = intermediate_size UpperCamelCase = hidden_act UpperCamelCase = hidden_dropout_prob UpperCamelCase = attention_probs_dropout_prob UpperCamelCase = type_sequence_label_size UpperCamelCase = initializer_range UpperCamelCase = scope UpperCamelCase = out_indices UpperCamelCase = num_labels # in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) UpperCamelCase = (image_size // patch_size) ** 2 UpperCamelCase = num_patches + 1 def UpperCAmelCase_ ( self )-> List[str]: '''simple docstring''' UpperCamelCase = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) UpperCamelCase = None UpperCamelCase = None if self.use_labels: UpperCamelCase = ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCamelCase = ids_tensor([self.batch_size, self.image_size, self.image_size] , self.num_labels ) UpperCamelCase = self.get_config() return config, pixel_values, labels, pixel_labels def UpperCAmelCase_ ( self )-> Dict: '''simple docstring''' return BeitConfig( vocab_size=self.vocab_size , 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 , out_indices=self.out_indices , ) def UpperCAmelCase_ ( self , A_ , A_ , A_ , A_ )-> List[str]: '''simple docstring''' UpperCamelCase = BeitModel(config=A_ ) model.to(A_ ) model.eval() UpperCamelCase = model(A_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def UpperCAmelCase_ ( self , A_ , A_ , A_ , A_ )-> Any: '''simple docstring''' UpperCamelCase = BeitForMaskedImageModeling(config=A_ ) model.to(A_ ) model.eval() UpperCamelCase = model(A_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length - 1, self.vocab_size) ) def UpperCAmelCase_ ( self , A_ , A_ , A_ , A_ )-> Optional[int]: '''simple docstring''' UpperCamelCase = self.type_sequence_label_size UpperCamelCase = BeitForImageClassification(A_ ) model.to(A_ ) model.eval() UpperCamelCase = model(A_ , labels=A_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images UpperCamelCase = 1 UpperCamelCase = BeitForImageClassification(A_ ) model.to(A_ ) model.eval() UpperCamelCase = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) UpperCamelCase = model(A_ , labels=A_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def UpperCAmelCase_ ( self , A_ , A_ , A_ , A_ )-> Optional[Any]: '''simple docstring''' UpperCamelCase = self.num_labels UpperCamelCase = BeitForSemanticSegmentation(A_ ) model.to(A_ ) model.eval() UpperCamelCase = model(A_ ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2) ) UpperCamelCase = model(A_ , labels=A_ ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2) ) def UpperCAmelCase_ ( self )-> int: '''simple docstring''' UpperCamelCase = self.prepare_config_and_inputs() UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase = config_and_inputs UpperCamelCase = {'pixel_values': pixel_values} return config, inputs_dict @require_torch class SCREAMING_SNAKE_CASE__ ( snake_case_ , snake_case_ , unittest.TestCase): lowerCAmelCase_ = ( (BeitModel, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation) if is_torch_available() else () ) lowerCAmelCase_ = ( { """feature-extraction""": BeitModel, """image-classification""": BeitForImageClassification, """image-segmentation""": BeitForSemanticSegmentation, } if is_torch_available() else {} ) lowerCAmelCase_ = False lowerCAmelCase_ = False lowerCAmelCase_ = False def UpperCAmelCase_ ( self )-> Any: '''simple docstring''' UpperCamelCase = BeitModelTester(self ) UpperCamelCase = ConfigTester(self , config_class=A_ , has_text_modality=A_ , hidden_size=37 ) def UpperCAmelCase_ ( self )-> Union[str, Any]: '''simple docstring''' self.config_tester.run_common_tests() @unittest.skip(reason='BEiT does not use inputs_embeds' ) def UpperCAmelCase_ ( self )-> Optional[int]: '''simple docstring''' pass @require_torch_multi_gpu @unittest.skip(reason='BEiT has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`' ) def UpperCAmelCase_ ( self )-> Optional[Any]: '''simple docstring''' pass def UpperCAmelCase_ ( self )-> Tuple: '''simple docstring''' UpperCamelCase , UpperCamelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCamelCase = model_class(A_ ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) UpperCamelCase = model.get_output_embeddings() self.assertTrue(x is None or isinstance(A_ , nn.Linear ) ) def UpperCAmelCase_ ( self )-> List[Any]: '''simple docstring''' UpperCamelCase , UpperCamelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCamelCase = model_class(A_ ) UpperCamelCase = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic UpperCamelCase = [*signature.parameters.keys()] UpperCamelCase = ['pixel_values'] self.assertListEqual(arg_names[:1] , A_ ) def UpperCAmelCase_ ( self )-> Union[str, Any]: '''simple docstring''' UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*A_ ) def UpperCAmelCase_ ( self )-> List[Any]: '''simple docstring''' UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*A_ ) def UpperCAmelCase_ ( self )-> Any: '''simple docstring''' UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*A_ ) def UpperCAmelCase_ ( self )-> List[str]: '''simple docstring''' UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_semantic_segmentation(*A_ ) def UpperCAmelCase_ ( self )-> int: '''simple docstring''' if not self.model_tester.is_training: return UpperCamelCase , UpperCamelCase = self.model_tester.prepare_config_and_inputs_for_common() UpperCamelCase = True for model_class in self.all_model_classes: # we don't test BeitForMaskedImageModeling if model_class in [*get_values(A_ ), BeitForMaskedImageModeling]: continue UpperCamelCase = model_class(A_ ) model.to(A_ ) model.train() UpperCamelCase = self._prepare_for_class(A_ , A_ , return_labels=A_ ) UpperCamelCase = model(**A_ ).loss loss.backward() def UpperCAmelCase_ ( self )-> List[str]: '''simple docstring''' UpperCamelCase , UpperCamelCase = self.model_tester.prepare_config_and_inputs_for_common() if not self.model_tester.is_training: return UpperCamelCase = False UpperCamelCase = True for model_class in self.all_model_classes: # we don't test BeitForMaskedImageModeling if ( model_class in [*get_values(A_ ), BeitForMaskedImageModeling] or not model_class.supports_gradient_checkpointing ): continue UpperCamelCase = model_class(A_ ) model.gradient_checkpointing_enable() model.to(A_ ) model.train() UpperCamelCase = self._prepare_for_class(A_ , A_ , return_labels=A_ ) UpperCamelCase = model(**A_ ).loss loss.backward() def UpperCAmelCase_ ( self )-> Union[str, Any]: '''simple docstring''' UpperCamelCase , UpperCamelCase = self.model_tester.prepare_config_and_inputs_for_common() UpperCamelCase = _config_zero_init(A_ ) for model_class in self.all_model_classes: UpperCamelCase = model_class(config=A_ ) for name, param in model.named_parameters(): # we skip lambda parameters as these require special initial values # determined by config.layer_scale_init_value if "lambda" in name: continue if param.requires_grad: self.assertIn( ((param.data.mean() * 1e9).round() / 1e9).item() , [0.0, 1.0] , msg=F'''Parameter {name} of model {model_class} seems not properly initialized''' , ) @slow def UpperCAmelCase_ ( self )-> Dict: '''simple docstring''' for model_name in BEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCamelCase = BeitModel.from_pretrained(A_ ) self.assertIsNotNone(A_ ) def A_( ): UpperCamelCase = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png') return image @require_torch @require_vision class SCREAMING_SNAKE_CASE__ ( unittest.TestCase): @cached_property def UpperCAmelCase_ ( self )-> Optional[int]: '''simple docstring''' return BeitImageProcessor.from_pretrained('microsoft/beit-base-patch16-224' ) if is_vision_available() else None @slow def UpperCAmelCase_ ( self )-> Optional[Any]: '''simple docstring''' UpperCamelCase = BeitForMaskedImageModeling.from_pretrained('microsoft/beit-base-patch16-224-pt22k' ).to(A_ ) UpperCamelCase = self.default_image_processor UpperCamelCase = prepare_img() UpperCamelCase = image_processor(images=A_ , return_tensors='pt' ).pixel_values.to(A_ ) # prepare bool_masked_pos UpperCamelCase = torch.ones((1, 196) , dtype=torch.bool ).to(A_ ) # forward pass with torch.no_grad(): UpperCamelCase = model(pixel_values=A_ , bool_masked_pos=A_ ) UpperCamelCase = outputs.logits # verify the logits UpperCamelCase = torch.Size((1, 196, 8192) ) self.assertEqual(logits.shape , A_ ) UpperCamelCase = torch.tensor( [[-3.2_437, 0.5_072, -13.9_174], [-3.2_456, 0.4_948, -13.9_401], [-3.2_033, 0.5_121, -13.8_550]] ).to(A_ ) self.assertTrue(torch.allclose(logits[bool_masked_pos][:3, :3] , A_ , atol=1e-2 ) ) @slow def UpperCAmelCase_ ( self )-> Union[str, Any]: '''simple docstring''' UpperCamelCase = BeitForImageClassification.from_pretrained('microsoft/beit-base-patch16-224' ).to(A_ ) UpperCamelCase = self.default_image_processor UpperCamelCase = prepare_img() UpperCamelCase = image_processor(images=A_ , return_tensors='pt' ).to(A_ ) # forward pass with torch.no_grad(): UpperCamelCase = model(**A_ ) UpperCamelCase = outputs.logits # verify the logits UpperCamelCase = torch.Size((1, 1000) ) self.assertEqual(logits.shape , A_ ) UpperCamelCase = torch.tensor([-1.2_385, -1.0_987, -1.0_108] ).to(A_ ) self.assertTrue(torch.allclose(logits[0, :3] , A_ , atol=1e-4 ) ) UpperCamelCase = 281 self.assertEqual(logits.argmax(-1 ).item() , A_ ) @slow def UpperCAmelCase_ ( self )-> Optional[Any]: '''simple docstring''' UpperCamelCase = BeitForImageClassification.from_pretrained('microsoft/beit-large-patch16-224-pt22k-ft22k' ).to( A_ ) UpperCamelCase = self.default_image_processor UpperCamelCase = prepare_img() UpperCamelCase = image_processor(images=A_ , return_tensors='pt' ).to(A_ ) # forward pass with torch.no_grad(): UpperCamelCase = model(**A_ ) UpperCamelCase = outputs.logits # verify the logits UpperCamelCase = torch.Size((1, 21841) ) self.assertEqual(logits.shape , A_ ) UpperCamelCase = torch.tensor([1.6_881, -0.2_787, 0.5_901] ).to(A_ ) self.assertTrue(torch.allclose(logits[0, :3] , A_ , atol=1e-4 ) ) UpperCamelCase = 2396 self.assertEqual(logits.argmax(-1 ).item() , A_ ) @slow def UpperCAmelCase_ ( self )-> Any: '''simple docstring''' UpperCamelCase = BeitForSemanticSegmentation.from_pretrained('microsoft/beit-base-finetuned-ade-640-640' ) UpperCamelCase = model.to(A_ ) UpperCamelCase = BeitImageProcessor(do_resize=A_ , size=640 , do_center_crop=A_ ) UpperCamelCase = load_dataset('hf-internal-testing/fixtures_ade20k' , split='test' ) UpperCamelCase = Image.open(ds[0]['file'] ) UpperCamelCase = image_processor(images=A_ , return_tensors='pt' ).to(A_ ) # forward pass with torch.no_grad(): UpperCamelCase = model(**A_ ) UpperCamelCase = outputs.logits # verify the logits UpperCamelCase = torch.Size((1, 150, 160, 160) ) self.assertEqual(logits.shape , A_ ) UpperCamelCase = version.parse(PIL.__version__ ) < version.parse('9.0.0' ) if is_pillow_less_than_a: UpperCamelCase = torch.tensor( [ [[-4.9_225, -2.3_954, -3.0_522], [-2.8_822, -1.0_046, -1.7_561], [-2.9_549, -1.3_228, -2.1_347]], [[-5.8_168, -3.4_129, -4.0_778], [-3.8_651, -2.2_214, -3.0_277], [-3.8_356, -2.4_643, -3.3_535]], [[-0.0_078, 3.9_952, 4.0_754], [2.9_856, 4.6_944, 5.0_035], [3.2_413, 4.7_813, 4.9_969]], ] , device=A_ , ) else: UpperCamelCase = torch.tensor( [ [[-4.8_960, -2.3_688, -3.0_355], [-2.8_478, -0.9_836, -1.7_418], [-2.9_449, -1.3_332, -2.1_456]], [[-5.8_081, -3.4_124, -4.1_006], [-3.8_561, -2.2_081, -3.0_323], [-3.8_365, -2.4_601, -3.3_669]], [[-0.0_309, 3.9_868, 4.0_540], [2.9_640, 4.6_877, 4.9_976], [3.2_081, 4.7_690, 4.9_942]], ] , device=A_ , ) self.assertTrue(torch.allclose(logits[0, :3, :3, :3] , A_ , atol=1e-4 ) ) @slow def UpperCAmelCase_ ( self )-> Tuple: '''simple docstring''' UpperCamelCase = BeitForSemanticSegmentation.from_pretrained('microsoft/beit-base-finetuned-ade-640-640' ) UpperCamelCase = model.to(A_ ) UpperCamelCase = BeitImageProcessor(do_resize=A_ , size=640 , do_center_crop=A_ ) UpperCamelCase = load_dataset('hf-internal-testing/fixtures_ade20k' , split='test' ) UpperCamelCase = Image.open(ds[0]['file'] ) UpperCamelCase = image_processor(images=A_ , return_tensors='pt' ).to(A_ ) # forward pass with torch.no_grad(): UpperCamelCase = model(**A_ ) UpperCamelCase = outputs.logits.detach().cpu() UpperCamelCase = image_processor.post_process_semantic_segmentation(outputs=A_ , target_sizes=[(500, 300)] ) UpperCamelCase = torch.Size((500, 300) ) self.assertEqual(segmentation[0].shape , A_ ) UpperCamelCase = image_processor.post_process_semantic_segmentation(outputs=A_ ) UpperCamelCase = torch.Size((160, 160) ) self.assertEqual(segmentation[0].shape , A_ )
3
0
'''simple docstring''' import unittest from parameterized import parameterized from transformers import OpenLlamaConfig, is_torch_available, set_seed from transformers.testing_utils import require_torch, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import OpenLlamaForCausalLM, OpenLlamaForSequenceClassification, OpenLlamaModel class A : def __init__( self : Optional[int] , __magic_name__ : Optional[Any] , __magic_name__ : int=13 , __magic_name__ : List[Any]=7 , __magic_name__ : Any=True , __magic_name__ : Optional[int]=True , __magic_name__ : Tuple=False , __magic_name__ : List[Any]=True , __magic_name__ : List[Any]=99 , __magic_name__ : List[Any]=32 , __magic_name__ : str=5 , __magic_name__ : int=4 , __magic_name__ : Dict=37 , __magic_name__ : Tuple="gelu" , __magic_name__ : Union[str, Any]=0.1 , __magic_name__ : Optional[int]=0.1 , __magic_name__ : List[str]=512 , __magic_name__ : Dict=16 , __magic_name__ : Any=2 , __magic_name__ : Any=0.02 , __magic_name__ : Tuple=3 , __magic_name__ : List[str]=4 , __magic_name__ : Union[str, Any]=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 __SCREAMING_SNAKE_CASE ( self : Optional[int] ): """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 __SCREAMING_SNAKE_CASE ( self : List[Any] ): """simple docstring""" return OpenLlamaConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=__magic_name__ , initializer_range=self.initializer_range , use_stable_embedding=__magic_name__ , ) def __SCREAMING_SNAKE_CASE ( self : List[str] , __magic_name__ : Tuple , __magic_name__ : List[Any] , __magic_name__ : Union[str, Any] , __magic_name__ : str , __magic_name__ : Optional[Any] , __magic_name__ : Optional[Any] , __magic_name__ : Dict ): """simple docstring""" lowerCAmelCase__ = OpenLlamaModel(config=__magic_name__ ) model.to(__magic_name__ ) model.eval() lowerCAmelCase__ = model(__magic_name__ , attention_mask=__magic_name__ ) lowerCAmelCase__ = model(__magic_name__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __SCREAMING_SNAKE_CASE ( self : Tuple , __magic_name__ : List[Any] , __magic_name__ : Any , __magic_name__ : Tuple , __magic_name__ : Optional[Any] , __magic_name__ : Tuple , __magic_name__ : Dict , __magic_name__ : Optional[Any] , __magic_name__ : Dict , __magic_name__ : Optional[Any] , ): """simple docstring""" lowerCAmelCase__ = True lowerCAmelCase__ = OpenLlamaModel(__magic_name__ ) model.to(__magic_name__ ) model.eval() lowerCAmelCase__ = model( __magic_name__ , attention_mask=__magic_name__ , encoder_hidden_states=__magic_name__ , encoder_attention_mask=__magic_name__ , ) lowerCAmelCase__ = model( __magic_name__ , attention_mask=__magic_name__ , encoder_hidden_states=__magic_name__ , ) lowerCAmelCase__ = model(__magic_name__ , attention_mask=__magic_name__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __SCREAMING_SNAKE_CASE ( self : Union[str, Any] , __magic_name__ : str , __magic_name__ : Optional[Any] , __magic_name__ : str , __magic_name__ : List[str] , __magic_name__ : int , __magic_name__ : str , __magic_name__ : Dict , __magic_name__ : Union[str, Any] , __magic_name__ : Optional[Any] , ): """simple docstring""" lowerCAmelCase__ = OpenLlamaForCausalLM(config=__magic_name__ ) model.to(__magic_name__ ) model.eval() lowerCAmelCase__ = model(__magic_name__ , attention_mask=__magic_name__ , labels=__magic_name__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def __SCREAMING_SNAKE_CASE ( self : Dict , __magic_name__ : Tuple , __magic_name__ : Dict , __magic_name__ : int , __magic_name__ : int , __magic_name__ : str , __magic_name__ : List[str] , __magic_name__ : Dict , __magic_name__ : Union[str, Any] , __magic_name__ : Any , ): """simple docstring""" lowerCAmelCase__ = True lowerCAmelCase__ = True lowerCAmelCase__ = OpenLlamaForCausalLM(config=__magic_name__ ) model.to(__magic_name__ ) model.eval() # first forward pass lowerCAmelCase__ = model( __magic_name__ , attention_mask=__magic_name__ , encoder_hidden_states=__magic_name__ , encoder_attention_mask=__magic_name__ , use_cache=__magic_name__ , ) lowerCAmelCase__ = outputs.past_key_values # create hypothetical multiple next token and extent to next_input_ids lowerCAmelCase__ = ids_tensor((self.batch_size, 3) , config.vocab_size ) lowerCAmelCase__ = ids_tensor((self.batch_size, 3) , vocab_size=2 ) # append to next input_ids and lowerCAmelCase__ = torch.cat([input_ids, next_tokens] , dim=-1 ) lowerCAmelCase__ = torch.cat([input_mask, next_mask] , dim=-1 ) lowerCAmelCase__ = model( __magic_name__ , attention_mask=__magic_name__ , encoder_hidden_states=__magic_name__ , encoder_attention_mask=__magic_name__ , output_hidden_states=__magic_name__ , )["hidden_states"][0] lowerCAmelCase__ = model( __magic_name__ , attention_mask=__magic_name__ , encoder_hidden_states=__magic_name__ , encoder_attention_mask=__magic_name__ , past_key_values=__magic_name__ , output_hidden_states=__magic_name__ , )["hidden_states"][0] # select random slice lowerCAmelCase__ = ids_tensor((1,) , output_from_past.shape[-1] ).item() lowerCAmelCase__ = output_from_no_past[:, -3:, random_slice_idx].detach() lowerCAmelCase__ = output_from_past[:, :, random_slice_idx].detach() self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1] ) # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(__magic_name__ , __magic_name__ , atol=1E-3 ) ) def __SCREAMING_SNAKE_CASE ( self : Any ): """simple docstring""" lowerCAmelCase__ = self.prepare_config_and_inputs() ( ( lowerCAmelCase__ ) ,( lowerCAmelCase__ ) ,( lowerCAmelCase__ ) ,( lowerCAmelCase__ ) ,( lowerCAmelCase__ ) ,( lowerCAmelCase__ ) ,( lowerCAmelCase__ ) , ) = config_and_inputs lowerCAmelCase__ = {"input_ids": input_ids, "attention_mask": input_mask} return config, inputs_dict @require_torch class A ( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , unittest.TestCase ): snake_case__ :Dict = ( (OpenLlamaModel, OpenLlamaForCausalLM, OpenLlamaForSequenceClassification) if is_torch_available() else () ) snake_case__ :Tuple = (OpenLlamaForCausalLM,) if is_torch_available() else () snake_case__ :List[str] = ( { 'feature-extraction': OpenLlamaModel, 'text-classification': OpenLlamaForSequenceClassification, 'text-generation': OpenLlamaForCausalLM, 'zero-shot': OpenLlamaForSequenceClassification, } if is_torch_available() else {} ) snake_case__ :List[str] = False snake_case__ :Any = False def __SCREAMING_SNAKE_CASE ( self : Dict ): """simple docstring""" lowerCAmelCase__ = OpenLlamaModelTester(self ) lowerCAmelCase__ = ConfigTester(self , config_class=__magic_name__ , hidden_size=37 ) def __SCREAMING_SNAKE_CASE ( self : int ): """simple docstring""" self.config_tester.run_common_tests() def __SCREAMING_SNAKE_CASE ( self : List[str] ): """simple docstring""" lowerCAmelCase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__magic_name__ ) def __SCREAMING_SNAKE_CASE ( self : Union[str, Any] ): """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(*__magic_name__ ) def __SCREAMING_SNAKE_CASE ( self : str ): """simple docstring""" lowerCAmelCase__ ,lowerCAmelCase__ = self.model_tester.prepare_config_and_inputs_for_common() lowerCAmelCase__ = 3 lowerCAmelCase__ = input_dict["input_ids"] lowerCAmelCase__ = input_ids.ne(1 ).to(__magic_name__ ) lowerCAmelCase__ = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) lowerCAmelCase__ = OpenLlamaForSequenceClassification(__magic_name__ ) model.to(__magic_name__ ) model.eval() lowerCAmelCase__ = model(__magic_name__ , attention_mask=__magic_name__ , labels=__magic_name__ ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def __SCREAMING_SNAKE_CASE ( self : List[Any] ): """simple docstring""" lowerCAmelCase__ ,lowerCAmelCase__ = self.model_tester.prepare_config_and_inputs_for_common() lowerCAmelCase__ = 3 lowerCAmelCase__ = "single_label_classification" lowerCAmelCase__ = input_dict["input_ids"] lowerCAmelCase__ = input_ids.ne(1 ).to(__magic_name__ ) lowerCAmelCase__ = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) lowerCAmelCase__ = OpenLlamaForSequenceClassification(__magic_name__ ) model.to(__magic_name__ ) model.eval() lowerCAmelCase__ = model(__magic_name__ , attention_mask=__magic_name__ , labels=__magic_name__ ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def __SCREAMING_SNAKE_CASE ( self : Optional[Any] ): """simple docstring""" lowerCAmelCase__ ,lowerCAmelCase__ = self.model_tester.prepare_config_and_inputs_for_common() lowerCAmelCase__ = 3 lowerCAmelCase__ = "multi_label_classification" lowerCAmelCase__ = input_dict["input_ids"] lowerCAmelCase__ = input_ids.ne(1 ).to(__magic_name__ ) lowerCAmelCase__ = ids_tensor( [self.model_tester.batch_size, config.num_labels] , self.model_tester.type_sequence_label_size ).to(torch.float ) lowerCAmelCase__ = OpenLlamaForSequenceClassification(__magic_name__ ) model.to(__magic_name__ ) model.eval() lowerCAmelCase__ = model(__magic_name__ , attention_mask=__magic_name__ , labels=__magic_name__ ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) @unittest.skip("Open-Llama buffers include complex numbers, which breaks this test" ) def __SCREAMING_SNAKE_CASE ( self : Any ): """simple docstring""" pass @parameterized.expand([("linear",), ("dynamic",)] ) def __SCREAMING_SNAKE_CASE ( self : Tuple , __magic_name__ : Optional[Any] ): """simple docstring""" lowerCAmelCase__ ,lowerCAmelCase__ = self.model_tester.prepare_config_and_inputs_for_common() lowerCAmelCase__ = ids_tensor([1, 10] , config.vocab_size ) lowerCAmelCase__ = ids_tensor([1, int(config.max_position_embeddings * 1.5 )] , config.vocab_size ) set_seed(42 ) # Fixed seed at init time so the two models get the same random weights lowerCAmelCase__ = OpenLlamaModel(__magic_name__ ) original_model.to(__magic_name__ ) original_model.eval() lowerCAmelCase__ = original_model(__magic_name__ ).last_hidden_state lowerCAmelCase__ = original_model(__magic_name__ ).last_hidden_state set_seed(42 ) # Fixed seed at init time so the two models get the same random weights lowerCAmelCase__ = {"type": scaling_type, "factor": 10.0} lowerCAmelCase__ = OpenLlamaModel(__magic_name__ ) scaled_model.to(__magic_name__ ) scaled_model.eval() lowerCAmelCase__ = scaled_model(__magic_name__ ).last_hidden_state lowerCAmelCase__ = scaled_model(__magic_name__ ).last_hidden_state # Dynamic scaling does not change the RoPE embeddings until it receives an input longer than the original # maximum sequence length, so the outputs for the short input should match. if scaling_type == "dynamic": self.assertTrue(torch.allclose(__magic_name__ , __magic_name__ , atol=1E-5 ) ) else: self.assertFalse(torch.allclose(__magic_name__ , __magic_name__ , atol=1E-5 ) ) # The output should be different for long inputs self.assertFalse(torch.allclose(__magic_name__ , __magic_name__ , atol=1E-5 ) )
48
'''simple docstring''' import enum import warnings from ..tokenization_utils import TruncationStrategy from ..utils import add_end_docstrings, is_tf_available, is_torch_available, logging from .base import PIPELINE_INIT_ARGS, Pipeline if is_tf_available(): import tensorflow as tf from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING if is_torch_available(): from ..models.auto.modeling_auto import MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING lowerCAmelCase : Dict = logging.get_logger(__name__) class SCREAMING_SNAKE_CASE__ ( enum.Enum): lowerCAmelCase_ = 0 lowerCAmelCase_ = 1 @add_end_docstrings(snake_case_) class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = """generated""" def __init__( self , *A_ , **A_ )-> Optional[int]: '''simple docstring''' super().__init__(*A_ , **A_ ) self.check_model_type( TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING if self.framework == 'tf' else MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING ) def UpperCAmelCase_ ( self , A_=None , A_=None , A_=None , A_=None , A_=None , A_=None , **A_ , )-> Optional[Any]: '''simple docstring''' UpperCamelCase = {} if truncation is not None: UpperCamelCase = truncation UpperCamelCase = generate_kwargs UpperCamelCase = {} if return_tensors is not None and return_type is None: UpperCamelCase = ReturnType.TENSORS if return_tensors else ReturnType.TEXT if return_type is not None: UpperCamelCase = return_type if clean_up_tokenization_spaces is not None: UpperCamelCase = clean_up_tokenization_spaces if stop_sequence is not None: UpperCamelCase = self.tokenizer.encode(A_ , add_special_tokens=A_ ) if len(A_ ) > 1: warnings.warn( 'Stopping on a multiple token sequence is not yet supported on transformers. The first token of' ' the stop sequence will be used as the stop sequence string in the interim.' ) UpperCamelCase = stop_sequence_ids[0] return preprocess_params, forward_params, postprocess_params def UpperCAmelCase_ ( self , A_ , A_ , A_ )-> Optional[int]: '''simple docstring''' return True def UpperCAmelCase_ ( self , *A_ , A_ )-> Any: '''simple docstring''' UpperCamelCase = self.model.config.prefix if self.model.config.prefix is not None else '' if isinstance(args[0] , A_ ): if self.tokenizer.pad_token_id is None: raise ValueError('Please make sure that the tokenizer has a pad_token_id when using a batch input' ) UpperCamelCase = ([prefix + arg for arg in args[0]],) UpperCamelCase = True elif isinstance(args[0] , A_ ): UpperCamelCase = (prefix + args[0],) UpperCamelCase = False else: raise ValueError( F''' `args[0]`: {args[0]} have the wrong format. The should be either of type `str` or type `list`''' ) UpperCamelCase = self.tokenizer(*A_ , padding=A_ , truncation=A_ , return_tensors=self.framework ) # This is produced by tokenizers but is an invalid generate kwargs if "token_type_ids" in inputs: del inputs["token_type_ids"] return inputs def __call__( self , *A_ , **A_ )-> Union[str, Any]: '''simple docstring''' UpperCamelCase = super().__call__(*A_ , **A_ ) if ( isinstance(args[0] , A_ ) and all(isinstance(A_ , A_ ) for el in args[0] ) and all(len(A_ ) == 1 for res in result ) ): return [res[0] for res in result] return result def UpperCAmelCase_ ( self , A_ , A_=TruncationStrategy.DO_NOT_TRUNCATE , **A_ )-> Any: '''simple docstring''' UpperCamelCase = self._parse_and_tokenize(A_ , truncation=A_ , **A_ ) return inputs def UpperCAmelCase_ ( self , A_ , **A_ )-> int: '''simple docstring''' if self.framework == "pt": UpperCamelCase , UpperCamelCase = model_inputs['input_ids'].shape elif self.framework == "tf": UpperCamelCase , UpperCamelCase = tf.shape(model_inputs['input_ids'] ).numpy() UpperCamelCase = generate_kwargs.get('min_length' , self.model.config.min_length ) UpperCamelCase = generate_kwargs.get('max_length' , self.model.config.max_length ) self.check_inputs(A_ , generate_kwargs['min_length'] , generate_kwargs['max_length'] ) UpperCamelCase = self.model.generate(**A_ , **A_ ) UpperCamelCase = output_ids.shape[0] if self.framework == "pt": UpperCamelCase = output_ids.reshape(A_ , out_b // in_b , *output_ids.shape[1:] ) elif self.framework == "tf": UpperCamelCase = tf.reshape(A_ , (in_b, out_b // in_b, *output_ids.shape[1:]) ) return {"output_ids": output_ids} def UpperCAmelCase_ ( self , A_ , A_=ReturnType.TEXT , A_=False )-> Optional[Any]: '''simple docstring''' UpperCamelCase = [] for output_ids in model_outputs["output_ids"][0]: if return_type == ReturnType.TENSORS: UpperCamelCase = {F'''{self.return_name}_token_ids''': output_ids} elif return_type == ReturnType.TEXT: UpperCamelCase = { F'''{self.return_name}_text''': self.tokenizer.decode( A_ , skip_special_tokens=A_ , clean_up_tokenization_spaces=A_ , ) } records.append(A_ ) return records @add_end_docstrings(snake_case_) class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = """summary""" def __call__( self , *A_ , **A_ )-> Optional[int]: '''simple docstring''' return super().__call__(*A_ , **A_ ) def UpperCAmelCase_ ( self , A_ , A_ , A_ )-> bool: '''simple docstring''' if max_length < min_length: logger.warning(F'''Your min_length={min_length} must be inferior than your max_length={max_length}.''' ) if input_length < max_length: logger.warning( F'''Your max_length is set to {max_length}, but your input_length is only {input_length}. Since this is ''' 'a summarization task, where outputs shorter than the input are typically wanted, you might ' F'''consider decreasing max_length manually, e.g. summarizer(\'...\', max_length={input_length//2})''' ) @add_end_docstrings(snake_case_) class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = """translation""" def UpperCAmelCase_ ( self , A_ , A_ , A_ )-> List[Any]: '''simple docstring''' if input_length > 0.9 * max_length: logger.warning( F'''Your input_length: {input_length} is bigger than 0.9 * max_length: {max_length}. You might consider ''' 'increasing your max_length manually, e.g. translator(\'...\', max_length=400)' ) return True def UpperCAmelCase_ ( self , *A_ , A_=TruncationStrategy.DO_NOT_TRUNCATE , A_=None , A_=None )-> Dict: '''simple docstring''' if getattr(self.tokenizer , '_build_translation_inputs' , A_ ): return self.tokenizer._build_translation_inputs( *A_ , return_tensors=self.framework , truncation=A_ , src_lang=A_ , tgt_lang=A_ ) else: return super()._parse_and_tokenize(*A_ , truncation=A_ ) def UpperCAmelCase_ ( self , A_=None , A_=None , **A_ )-> str: '''simple docstring''' UpperCamelCase , UpperCamelCase , UpperCamelCase = super()._sanitize_parameters(**A_ ) if src_lang is not None: UpperCamelCase = src_lang if tgt_lang is not None: UpperCamelCase = tgt_lang if src_lang is None and tgt_lang is None: # Backward compatibility, direct arguments use is preferred. UpperCamelCase = kwargs.get('task' , self.task ) UpperCamelCase = task.split('_' ) if task and len(A_ ) == 4: # translation, XX, to YY UpperCamelCase = items[1] UpperCamelCase = items[3] return preprocess_params, forward_params, postprocess_params def __call__( self , *A_ , **A_ )-> Any: '''simple docstring''' return super().__call__(*A_ , **A_ )
3
0
"""simple docstring""" import argparse from pathlib import Path import requests import torch from PIL import Image from transformers import ( RobertaTokenizer, TrOCRConfig, TrOCRForCausalLM, TrOCRProcessor, VisionEncoderDecoderModel, ViTConfig, ViTImageProcessor, ViTModel, ) from transformers.utils import logging logging.set_verbosity_info() _lowercase : List[Any] = logging.get_logger(__name__) def lowercase__ ( snake_case_ :Union[str, Any] , snake_case_ :Optional[Any] ): __UpperCAmelCase = [] for i in range(encoder_config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append( (F'''encoder.deit.blocks.{i}.norm1.weight''', F'''encoder.encoder.layer.{i}.layernorm_before.weight''') ) rename_keys.append((F'''encoder.deit.blocks.{i}.norm1.bias''', F'''encoder.encoder.layer.{i}.layernorm_before.bias''') ) rename_keys.append( (F'''encoder.deit.blocks.{i}.attn.proj.weight''', F'''encoder.encoder.layer.{i}.attention.output.dense.weight''') ) rename_keys.append( (F'''encoder.deit.blocks.{i}.attn.proj.bias''', F'''encoder.encoder.layer.{i}.attention.output.dense.bias''') ) rename_keys.append( (F'''encoder.deit.blocks.{i}.norm2.weight''', F'''encoder.encoder.layer.{i}.layernorm_after.weight''') ) rename_keys.append((F'''encoder.deit.blocks.{i}.norm2.bias''', F'''encoder.encoder.layer.{i}.layernorm_after.bias''') ) rename_keys.append( (F'''encoder.deit.blocks.{i}.mlp.fc1.weight''', F'''encoder.encoder.layer.{i}.intermediate.dense.weight''') ) rename_keys.append( (F'''encoder.deit.blocks.{i}.mlp.fc1.bias''', F'''encoder.encoder.layer.{i}.intermediate.dense.bias''') ) rename_keys.append( (F'''encoder.deit.blocks.{i}.mlp.fc2.weight''', F'''encoder.encoder.layer.{i}.output.dense.weight''') ) rename_keys.append((F'''encoder.deit.blocks.{i}.mlp.fc2.bias''', F'''encoder.encoder.layer.{i}.output.dense.bias''') ) # cls token, position embeddings and patch embeddings of encoder rename_keys.extend( [ ('''encoder.deit.cls_token''', '''encoder.embeddings.cls_token'''), ('''encoder.deit.pos_embed''', '''encoder.embeddings.position_embeddings'''), ('''encoder.deit.patch_embed.proj.weight''', '''encoder.embeddings.patch_embeddings.projection.weight'''), ('''encoder.deit.patch_embed.proj.bias''', '''encoder.embeddings.patch_embeddings.projection.bias'''), ('''encoder.deit.norm.weight''', '''encoder.layernorm.weight'''), ('''encoder.deit.norm.bias''', '''encoder.layernorm.bias'''), ] ) return rename_keys def lowercase__ ( snake_case_ :int , snake_case_ :Any ): for i in range(encoder_config.num_hidden_layers ): # queries, keys and values (only weights, no biases) __UpperCAmelCase = state_dict.pop(F'''encoder.deit.blocks.{i}.attn.qkv.weight''' ) __UpperCAmelCase = in_proj_weight[ : encoder_config.hidden_size, : ] __UpperCAmelCase = in_proj_weight[ encoder_config.hidden_size : encoder_config.hidden_size * 2, : ] __UpperCAmelCase = in_proj_weight[ -encoder_config.hidden_size :, : ] def lowercase__ ( snake_case_ :Dict , snake_case_ :Tuple , snake_case_ :Tuple ): __UpperCAmelCase = dct.pop(snake_case_ ) __UpperCAmelCase = val def lowercase__ ( snake_case_ :Optional[Any] ): if "handwritten" in checkpoint_url: __UpperCAmelCase = '''https://fki.tic.heia-fr.ch/static/img/a01-122-02-00.jpg''' # industry # url = "https://fki.tic.heia-fr.ch/static/img/a01-122-02-12.jpg" # have # url = "https://fki.tic.heia-fr.ch/static/img/a01-122-02-10.jpg" # let # url = "https://fki.tic.heia-fr.ch/static/img/a01-122-02.jpg" # # url = "https://fki.tic.heia-fr.ch/static/img/a01-122.jpg" elif "printed" in checkpoint_url or "stage1" in checkpoint_url: __UpperCAmelCase = '''https://www.researchgate.net/profile/Dinh-Sang/publication/338099565/figure/fig8/AS:840413229350922@1577381536857/An-receipt-example-in-the-SROIE-2019-dataset_Q640.jpg''' __UpperCAmelCase = Image.open(requests.get(snake_case_ , stream=snake_case_ ).raw ).convert('''RGB''' ) return im @torch.no_grad() def lowercase__ ( snake_case_ :Tuple , snake_case_ :str ): __UpperCAmelCase = ViTConfig(image_size=384 , qkv_bias=snake_case_ ) __UpperCAmelCase = TrOCRConfig() # size of the architecture if "base" in checkpoint_url: __UpperCAmelCase = 768 elif "large" in checkpoint_url: # use ViT-large encoder __UpperCAmelCase = 1_024 __UpperCAmelCase = 4_096 __UpperCAmelCase = 24 __UpperCAmelCase = 16 __UpperCAmelCase = 1_024 else: raise ValueError('''Should either find \'base\' or \'large\' in checkpoint URL''' ) # the large-printed + stage1 checkpoints uses sinusoidal position embeddings, no layernorm afterwards if "large-printed" in checkpoint_url or "stage1" in checkpoint_url: __UpperCAmelCase = False __UpperCAmelCase = '''relu''' __UpperCAmelCase = 1_024 __UpperCAmelCase = True __UpperCAmelCase = False __UpperCAmelCase = False # load HuggingFace model __UpperCAmelCase = ViTModel(snake_case_ , add_pooling_layer=snake_case_ ) __UpperCAmelCase = TrOCRForCausalLM(snake_case_ ) __UpperCAmelCase = VisionEncoderDecoderModel(encoder=snake_case_ , decoder=snake_case_ ) model.eval() # load state_dict of original model, rename some keys __UpperCAmelCase = torch.hub.load_state_dict_from_url(snake_case_ , map_location='''cpu''' , check_hash=snake_case_ )['''model'''] __UpperCAmelCase = create_rename_keys(snake_case_ , snake_case_ ) for src, dest in rename_keys: rename_key(snake_case_ , snake_case_ , snake_case_ ) read_in_q_k_v(snake_case_ , snake_case_ ) # remove parameters we don't need del state_dict["encoder.deit.head.weight"] del state_dict["encoder.deit.head.bias"] del state_dict["decoder.version"] # add prefix to decoder keys for key, val in state_dict.copy().items(): __UpperCAmelCase = state_dict.pop(snake_case_ ) if key.startswith('''decoder''' ) and "output_projection" not in key: __UpperCAmelCase = val else: __UpperCAmelCase = val # load state dict model.load_state_dict(snake_case_ ) # Check outputs on an image __UpperCAmelCase = ViTImageProcessor(size=encoder_config.image_size ) __UpperCAmelCase = RobertaTokenizer.from_pretrained('''roberta-large''' ) __UpperCAmelCase = TrOCRProcessor(snake_case_ , snake_case_ ) __UpperCAmelCase = processor(images=prepare_img(snake_case_ ) , return_tensors='''pt''' ).pixel_values # verify logits __UpperCAmelCase = torch.tensor([[model.config.decoder.decoder_start_token_id]] ) __UpperCAmelCase = model(pixel_values=snake_case_ , decoder_input_ids=snake_case_ ) __UpperCAmelCase = outputs.logits __UpperCAmelCase = torch.Size([1, 1, 50_265] ) if "trocr-base-handwritten" in checkpoint_url: __UpperCAmelCase = torch.tensor( [-1.4502, -4.6683, -0.5347, -2.9291, 9.1435, -3.0571, 8.9764, 1.7560, 8.7358, -1.5311] ) elif "trocr-large-handwritten" in checkpoint_url: __UpperCAmelCase = torch.tensor( [-2.6437, -1.3129, -2.2596, -5.3455, 6.3539, 1.7604, 5.4991, 1.4702, 5.6113, 2.0170] ) elif "trocr-base-printed" in checkpoint_url: __UpperCAmelCase = torch.tensor( [-5.6816, -5.8388, 1.1398, -6.9034, 6.8505, -2.4393, 1.2284, -1.0232, -1.9661, -3.9210] ) elif "trocr-large-printed" in checkpoint_url: __UpperCAmelCase = torch.tensor( [-6.0162, -7.0959, 4.4155, -5.1063, 7.0468, -3.1631, 2.6466, -0.3081, -0.8106, -1.7535] ) if "stage1" not in checkpoint_url: assert logits.shape == expected_shape, "Shape of logits not as expected" assert torch.allclose(logits[0, 0, :10] , snake_case_ , atol=1E-3 ), "First elements of logits not as expected" Path(snake_case_ ).mkdir(exist_ok=snake_case_ ) print(F'''Saving model to {pytorch_dump_folder_path}''' ) model.save_pretrained(snake_case_ ) print(F'''Saving processor to {pytorch_dump_folder_path}''' ) processor.save_pretrained(snake_case_ ) if __name__ == "__main__": _lowercase : Optional[Any] = argparse.ArgumentParser() parser.add_argument( '--checkpoint_url', default='https://layoutlm.blob.core.windows.net/trocr/model_zoo/fairseq/trocr-base-handwritten.pt', type=str, help='URL to the original PyTorch checkpoint (.pth file).', ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, help='Path to the folder to output PyTorch model.' ) _lowercase : Any = parser.parse_args() convert_tr_ocr_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)
49
'''simple docstring''' import inspect import os import unittest from dataclasses import dataclass import torch from accelerate import Accelerator, DistributedDataParallelKwargs, GradScalerKwargs from accelerate.state import AcceleratorState from accelerate.test_utils import execute_subprocess_async, require_cuda, require_multi_gpu from accelerate.utils import KwargsHandler @dataclass class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = 0 lowerCAmelCase_ = False lowerCAmelCase_ = 3.0 class SCREAMING_SNAKE_CASE__ ( unittest.TestCase): def UpperCAmelCase_ ( self )-> int: '''simple docstring''' self.assertDictEqual(MockClass().to_kwargs() , {} ) self.assertDictEqual(MockClass(a=2 ).to_kwargs() , {'a': 2} ) self.assertDictEqual(MockClass(a=2 , b=A_ ).to_kwargs() , {'a': 2, 'b': True} ) self.assertDictEqual(MockClass(a=2 , c=2.25 ).to_kwargs() , {'a': 2, 'c': 2.25} ) @require_cuda def UpperCAmelCase_ ( self )-> Dict: '''simple docstring''' UpperCamelCase = GradScalerKwargs(init_scale=1024 , growth_factor=2 ) AcceleratorState._reset_state() UpperCamelCase = Accelerator(mixed_precision='fp16' , kwargs_handlers=[scaler_handler] ) print(accelerator.use_fpaa ) UpperCamelCase = accelerator.scaler # Check the kwargs have been applied self.assertEqual(scaler._init_scale , 1_024.0 ) self.assertEqual(scaler._growth_factor , 2.0 ) # Check the other values are at the default self.assertEqual(scaler._backoff_factor , 0.5 ) self.assertEqual(scaler._growth_interval , 2000 ) self.assertEqual(scaler._enabled , A_ ) @require_multi_gpu def UpperCAmelCase_ ( self )-> Dict: '''simple docstring''' UpperCamelCase = ['torchrun', F'''--nproc_per_node={torch.cuda.device_count()}''', inspect.getfile(self.__class__ )] execute_subprocess_async(A_ , env=os.environ.copy() ) if __name__ == "__main__": lowerCAmelCase : Tuple = DistributedDataParallelKwargs(bucket_cap_mb=15, find_unused_parameters=True) lowerCAmelCase : List[str] = Accelerator(kwargs_handlers=[ddp_scaler]) lowerCAmelCase : List[Any] = torch.nn.Linear(1_00, 2_00) lowerCAmelCase : int = accelerator.prepare(model) # Check the values changed in kwargs lowerCAmelCase : Dict = '' lowerCAmelCase : Dict = model.bucket_bytes_cap // (10_24 * 10_24) if observed_bucket_cap_map != 15: error_msg += f"Kwargs badly passed, should have `15` but found {observed_bucket_cap_map}.\n" if model.find_unused_parameters is not True: error_msg += f"Kwargs badly passed, should have `True` but found {model.find_unused_parameters}.\n" # Check the values of the defaults if model.dim != 0: error_msg += f"Default value not respected, should have `0` but found {model.dim}.\n" if model.broadcast_buffers is not True: error_msg += f"Default value not respected, should have `True` but found {model.broadcast_buffers}.\n" if model.gradient_as_bucket_view is not False: error_msg += f"Default value not respected, should have `False` but found {model.gradient_as_bucket_view}.\n" # Raise error at the end to make sure we don't stop at the first failure. if len(error_msg) > 0: raise ValueError(error_msg)
3
0
'''simple docstring''' import math UpperCamelCase : Union[str, Any] = 10 UpperCamelCase : Optional[Any] = 7 UpperCamelCase : str = BALLS_PER_COLOUR * NUM_COLOURS def A__ ( __lowerCAmelCase : int = 20 ): lowerCamelCase__ = math.comb(__lowerCAmelCase , __lowerCAmelCase ) lowerCamelCase__ = math.comb(NUM_BALLS - BALLS_PER_COLOUR , __lowerCAmelCase ) lowerCamelCase__ = NUM_COLOURS * (1 - missing_colour / total) return F'''{result:.9f}''' if __name__ == "__main__": print(solution(20))
50
'''simple docstring''' from typing import Callable, List, Optional, Tuple, Union import torch from transformers import CLIPTextModel, CLIPTokenizer from ...configuration_utils import ConfigMixin, register_to_config from ...models import ModelMixin, TransformeraDModel, VQModel from ...schedulers import VQDiffusionScheduler from ...utils import logging from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput lowerCAmelCase : Optional[Any] = logging.get_logger(__name__) # pylint: disable=invalid-name class SCREAMING_SNAKE_CASE__ ( snake_case_ , snake_case_): @register_to_config def __init__( self , A_ , A_ = None , A_ = None )-> Tuple: '''simple docstring''' super().__init__() UpperCamelCase = learnable if self.learnable: assert hidden_size is not None, "learnable=True requires `hidden_size` to be set" assert length is not None, "learnable=True requires `length` to be set" UpperCamelCase = torch.zeros(A_ , A_ ) else: UpperCamelCase = None UpperCamelCase = torch.nn.Parameter(A_ ) class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = 42 lowerCAmelCase_ = 42 lowerCAmelCase_ = 42 lowerCAmelCase_ = 42 lowerCAmelCase_ = 42 lowerCAmelCase_ = 42 def __init__( self , A_ , A_ , A_ , A_ , A_ , A_ , )-> Union[str, Any]: '''simple docstring''' super().__init__() self.register_modules( vqvae=A_ , transformer=A_ , text_encoder=A_ , tokenizer=A_ , scheduler=A_ , learned_classifier_free_sampling_embeddings=A_ , ) def UpperCAmelCase_ ( self , A_ , A_ , A_ )-> Tuple: '''simple docstring''' UpperCamelCase = len(A_ ) if isinstance(A_ , A_ ) else 1 # get prompt text embeddings UpperCamelCase = self.tokenizer( A_ , padding='max_length' , max_length=self.tokenizer.model_max_length , return_tensors='pt' , ) UpperCamelCase = text_inputs.input_ids if text_input_ids.shape[-1] > self.tokenizer.model_max_length: UpperCamelCase = self.tokenizer.batch_decode(text_input_ids[:, self.tokenizer.model_max_length :] ) logger.warning( 'The following part of your input was truncated because CLIP can only handle sequences up to' F''' {self.tokenizer.model_max_length} tokens: {removed_text}''' ) UpperCamelCase = text_input_ids[:, : self.tokenizer.model_max_length] UpperCamelCase = self.text_encoder(text_input_ids.to(self.device ) )[0] # NOTE: This additional step of normalizing the text embeddings is from VQ-Diffusion. # While CLIP does normalize the pooled output of the text transformer when combining # the image and text embeddings, CLIP does not directly normalize the last hidden state. # # CLIP normalizing the pooled output. # https://github.com/huggingface/transformers/blob/d92e22d1f28324f513f3080e5c47c071a3916721/src/transformers/models/clip/modeling_clip.py#L1052-L1053 UpperCamelCase = prompt_embeds / prompt_embeds.norm(dim=-1 , keepdim=A_ ) # duplicate text embeddings for each generation per prompt UpperCamelCase = prompt_embeds.repeat_interleave(A_ , dim=0 ) if do_classifier_free_guidance: if self.learned_classifier_free_sampling_embeddings.learnable: UpperCamelCase = self.learned_classifier_free_sampling_embeddings.embeddings UpperCamelCase = negative_prompt_embeds.unsqueeze(0 ).repeat(A_ , 1 , 1 ) else: UpperCamelCase = [''] * batch_size UpperCamelCase = text_input_ids.shape[-1] UpperCamelCase = self.tokenizer( A_ , padding='max_length' , max_length=A_ , truncation=A_ , return_tensors='pt' , ) UpperCamelCase = self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # See comment for normalizing text embeddings UpperCamelCase = negative_prompt_embeds / negative_prompt_embeds.norm(dim=-1 , keepdim=A_ ) # duplicate unconditional embeddings for each generation per prompt, using mps friendly method UpperCamelCase = negative_prompt_embeds.shape[1] UpperCamelCase = negative_prompt_embeds.repeat(1 , A_ , 1 ) UpperCamelCase = negative_prompt_embeds.view(batch_size * num_images_per_prompt , A_ , -1 ) # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes UpperCamelCase = torch.cat([negative_prompt_embeds, prompt_embeds] ) return prompt_embeds @torch.no_grad() def __call__( self , A_ , A_ = 100 , A_ = 5.0 , A_ = 1.0 , A_ = 1 , A_ = None , A_ = None , A_ = "pil" , A_ = True , A_ = None , A_ = 1 , )-> Union[ImagePipelineOutput, Tuple]: '''simple docstring''' if isinstance(A_ , A_ ): UpperCamelCase = 1 elif isinstance(A_ , A_ ): UpperCamelCase = len(A_ ) else: raise ValueError(F'''`prompt` has to be of type `str` or `list` but is {type(A_ )}''' ) UpperCamelCase = batch_size * num_images_per_prompt UpperCamelCase = guidance_scale > 1.0 UpperCamelCase = self._encode_prompt(A_ , A_ , A_ ) 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_ )}.''' ) # get the initial completely masked latents unless the user supplied it UpperCamelCase = (batch_size, self.transformer.num_latent_pixels) if latents is None: UpperCamelCase = self.transformer.num_vector_embeds - 1 UpperCamelCase = torch.full(A_ , A_ ).to(self.device ) else: if latents.shape != latents_shape: raise ValueError(F'''Unexpected latents shape, got {latents.shape}, expected {latents_shape}''' ) if (latents < 0).any() or (latents >= self.transformer.num_vector_embeds).any(): raise ValueError( 'Unexpected latents value(s). All latents be valid embedding indices i.e. in the range 0,' F''' {self.transformer.num_vector_embeds - 1} (inclusive).''' ) UpperCamelCase = latents.to(self.device ) # set timesteps self.scheduler.set_timesteps(A_ , device=self.device ) UpperCamelCase = self.scheduler.timesteps.to(self.device ) UpperCamelCase = latents for i, t in enumerate(self.progress_bar(A_ ) ): # expand the sample if we are doing classifier free guidance UpperCamelCase = torch.cat([sample] * 2 ) if do_classifier_free_guidance else sample # predict the un-noised image # model_output == `log_p_x_0` UpperCamelCase = self.transformer(A_ , encoder_hidden_states=A_ , timestep=A_ ).sample if do_classifier_free_guidance: UpperCamelCase , UpperCamelCase = model_output.chunk(2 ) UpperCamelCase = model_output_uncond + guidance_scale * (model_output_text - model_output_uncond) model_output -= torch.logsumexp(A_ , dim=1 , keepdim=A_ ) UpperCamelCase = self.truncate(A_ , A_ ) # remove `log(0)`'s (`-inf`s) UpperCamelCase = model_output.clamp(-70 ) # compute the previous noisy sample x_t -> x_t-1 UpperCamelCase = self.scheduler.step(A_ , timestep=A_ , sample=A_ , generator=A_ ).prev_sample # call the callback, if provided if callback is not None and i % callback_steps == 0: callback(A_ , A_ , A_ ) UpperCamelCase = self.vqvae.config.vq_embed_dim UpperCamelCase = (batch_size, self.transformer.height, self.transformer.width, embedding_channels) UpperCamelCase = self.vqvae.quantize.get_codebook_entry(A_ , shape=A_ ) UpperCamelCase = self.vqvae.decode(A_ , force_not_quantize=A_ ).sample UpperCamelCase = (image / 2 + 0.5).clamp(0 , 1 ) UpperCamelCase = image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": UpperCamelCase = self.numpy_to_pil(A_ ) if not return_dict: return (image,) return ImagePipelineOutput(images=A_ ) def UpperCAmelCase_ ( self , A_ , A_ )-> torch.FloatTensor: '''simple docstring''' UpperCamelCase , UpperCamelCase = torch.sort(A_ , 1 , descending=A_ ) UpperCamelCase = torch.exp(A_ ) UpperCamelCase = sorted_p_x_0.cumsum(dim=1 ) < truncation_rate # Ensure that at least the largest probability is not zeroed out UpperCamelCase = torch.full_like(keep_mask[:, 0:1, :] , A_ ) UpperCamelCase = torch.cat((all_true, keep_mask) , dim=1 ) UpperCamelCase = keep_mask[:, :-1, :] UpperCamelCase = keep_mask.gather(1 , indices.argsort(1 ) ) UpperCamelCase = log_p_x_0.clone() UpperCamelCase = -torch.inf # -inf = log(0) return rv
3
0
'''simple docstring''' import copy import os from typing import Union from ...configuration_utils import PretrainedConfig from ...utils import logging a__ : Any = logging.get_logger(__name__) a__ : Optional[Any] = { 'BAAI/AltCLIP': 'https://huggingface.co/BAAI/AltCLIP/resolve/main/config.json', # See all AltCLIP models at https://huggingface.co/models?filter=altclip } class lowerCAmelCase__ ( UpperCAmelCase_ ): '''simple docstring''' _lowerCamelCase ="altclip_text_model" def __init__( self : Any , a__ : Dict=250002 , a__ : int=1024 , a__ : Tuple=24 , a__ : Union[str, Any]=16 , a__ : List[Any]=4096 , a__ : Union[str, Any]="gelu" , a__ : Optional[Any]=0.1 , a__ : Any=0.1 , a__ : List[Any]=514 , a__ : str=1 , a__ : Tuple=0.02 , a__ : Optional[Any]=0.02 , a__ : int=1e-0_5 , a__ : str=1 , a__ : Tuple=0 , a__ : List[Any]=2 , a__ : List[str]="absolute" , a__ : Union[str, Any]=True , a__ : int=768 , **a__ : str , ): super().__init__(pad_token_id=a__ , bos_token_id=a__ , eos_token_id=a__ , **a__ ) UpperCAmelCase = vocab_size UpperCAmelCase = hidden_size UpperCAmelCase = num_hidden_layers UpperCAmelCase = num_attention_heads UpperCAmelCase = hidden_act UpperCAmelCase = intermediate_size UpperCAmelCase = hidden_dropout_prob UpperCAmelCase = attention_probs_dropout_prob UpperCAmelCase = max_position_embeddings UpperCAmelCase = type_vocab_size UpperCAmelCase = initializer_range UpperCAmelCase = initializer_factor UpperCAmelCase = layer_norm_eps UpperCAmelCase = position_embedding_type UpperCAmelCase = use_cache UpperCAmelCase = project_dim class lowerCAmelCase__ ( UpperCAmelCase_ ): '''simple docstring''' _lowerCamelCase ="altclip_vision_model" def __init__( self : Dict , a__ : Tuple=768 , a__ : Dict=3072 , a__ : Any=512 , a__ : Any=12 , a__ : List[Any]=12 , a__ : int=3 , a__ : Dict=224 , a__ : Any=32 , a__ : Tuple="quick_gelu" , a__ : Tuple=1e-5 , a__ : Optional[int]=0.0 , a__ : List[Any]=0.02 , a__ : List[Any]=1.0 , **a__ : Dict , ): super().__init__(**a__ ) UpperCAmelCase = hidden_size UpperCAmelCase = intermediate_size UpperCAmelCase = projection_dim UpperCAmelCase = num_hidden_layers UpperCAmelCase = num_attention_heads UpperCAmelCase = num_channels UpperCAmelCase = patch_size UpperCAmelCase = image_size UpperCAmelCase = initializer_range UpperCAmelCase = initializer_factor UpperCAmelCase = attention_dropout UpperCAmelCase = layer_norm_eps UpperCAmelCase = hidden_act @classmethod def __snake_case ( cls : Tuple , a__ : Union[str, os.PathLike] , **a__ : Optional[int] ): cls._set_token_in_kwargs(a__ ) UpperCAmelCase, UpperCAmelCase = cls.get_config_dict(a__ , **a__ ) # get the vision config dict if we are loading from AltCLIPConfig if config_dict.get('''model_type''' ) == "altclip": UpperCAmelCase = config_dict['''vision_config'''] if "model_type" in config_dict and hasattr(cls , '''model_type''' ) and config_dict["model_type"] != cls.model_type: logger.warning( f"You are using a model of type {config_dict['model_type']} to instantiate a model of type " f"{cls.model_type}. This is not supported for all configurations of models and can yield errors." ) return cls.from_dict(a__ , **a__ ) class lowerCAmelCase__ ( UpperCAmelCase_ ): '''simple docstring''' _lowerCamelCase ="altclip" _lowerCamelCase =True def __init__( self : Optional[Any] , a__ : List[Any]=None , a__ : Any=None , a__ : Optional[Any]=768 , a__ : List[Any]=2.6_592 , **a__ : str ): # If `_config_dict` exist, we use them for the backward compatibility. # We pop out these 2 attributes before calling `super().__init__` to avoid them being saved (which causes a lot # of confusion!). UpperCAmelCase = kwargs.pop('''text_config_dict''' , a__ ) UpperCAmelCase = kwargs.pop('''vision_config_dict''' , a__ ) super().__init__(**a__ ) # Instead of simply assigning `[text|vision]_config_dict` to `[text|vision]_config`, we use the values in # `[text|vision]_config_dict` to update the values in `[text|vision]_config`. The values should be same in most # cases, but we don't want to break anything regarding `_config_dict` that existed before commit `8827e1b2`. if text_config_dict is not None: if text_config is None: UpperCAmelCase = {} # This is the complete result when using `text_config_dict`. UpperCAmelCase = AltCLIPTextConfig(**a__ ).to_dict() # Give a warning if the values exist in both `_text_config_dict` and `text_config` but being different. for key, value in _text_config_dict.items(): if key in text_config and value != text_config[key] and key not in ["transformers_version"]: # If specified in `text_config_dict` if key in text_config_dict: UpperCAmelCase = ( f"`{key}` is found in both `text_config_dict` and `text_config` but with different values. " f"The value `text_config_dict[\"{key}\"]` will be used instead." ) # If inferred from default argument values (just to be super careful) else: UpperCAmelCase = ( f"`text_config_dict` is provided which will be used to initialize `AltCLIPTextConfig`. The " f"value `text_config[\"{key}\"]` will be overriden." ) logger.warning(a__ ) # Update all values in `text_config` with the ones in `_text_config_dict`. text_config.update(_text_config_dict ) if vision_config_dict is not None: if vision_config is None: UpperCAmelCase = {} # This is the complete result when using `vision_config_dict`. UpperCAmelCase = AltCLIPVisionConfig(**a__ ).to_dict() # convert keys to string instead of integer if "id2label" in _vision_config_dict: UpperCAmelCase = { str(a__ ): value for key, value in _vision_config_dict['''id2label'''].items() } # Give a warning if the values exist in both `_vision_config_dict` and `vision_config` but being different. for key, value in _vision_config_dict.items(): if key in vision_config and value != vision_config[key] and key not in ["transformers_version"]: # If specified in `vision_config_dict` if key in vision_config_dict: UpperCAmelCase = ( f"`{key}` is found in both `vision_config_dict` and `vision_config` but with different " f"values. The value `vision_config_dict[\"{key}\"]` will be used instead." ) # If inferred from default argument values (just to be super careful) else: UpperCAmelCase = ( f"`vision_config_dict` is provided which will be used to initialize `AltCLIPVisionConfig`. " f"The value `vision_config[\"{key}\"]` will be overriden." ) logger.warning(a__ ) # Update all values in `vision_config` with the ones in `_vision_config_dict`. vision_config.update(_vision_config_dict ) if text_config is None: UpperCAmelCase = {} logger.info('''`text_config` is `None`. Initializing the `AltCLIPTextConfig` with default values.''' ) if vision_config is None: UpperCAmelCase = {} logger.info('''`vision_config` is `None`. initializing the `AltCLIPVisionConfig` with default values.''' ) UpperCAmelCase = AltCLIPTextConfig(**a__ ) UpperCAmelCase = AltCLIPVisionConfig(**a__ ) UpperCAmelCase = projection_dim UpperCAmelCase = logit_scale_init_value UpperCAmelCase = 1.0 @classmethod def __snake_case ( cls : int , a__ : AltCLIPTextConfig , a__ : AltCLIPVisionConfig , **a__ : Any ): return cls(text_config=text_config.to_dict() , vision_config=vision_config.to_dict() , **a__ ) def __snake_case ( self : Optional[Any] ): UpperCAmelCase = copy.deepcopy(self.__dict__ ) UpperCAmelCase = self.text_config.to_dict() UpperCAmelCase = self.vision_config.to_dict() UpperCAmelCase = self.__class__.model_type return output
51
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCAmelCase : Union[str, Any] = { 'configuration_git': ['GIT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'GitConfig', 'GitVisionConfig'], 'processing_git': ['GitProcessor'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : List[Any] = [ 'GIT_PRETRAINED_MODEL_ARCHIVE_LIST', 'GitForCausalLM', 'GitModel', 'GitPreTrainedModel', 'GitVisionModel', ] if TYPE_CHECKING: from .configuration_git import GIT_PRETRAINED_CONFIG_ARCHIVE_MAP, GitConfig, GitVisionConfig from .processing_git import GitProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_git import ( GIT_PRETRAINED_MODEL_ARCHIVE_LIST, GitForCausalLM, GitModel, GitPreTrainedModel, GitVisionModel, ) else: import sys lowerCAmelCase : Optional[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
3
0
"""simple docstring""" import re def __A ( a_ :str) -> str: if len(re.findall('''[ATCG]''' , a_)) != len(a_): raise ValueError('''Invalid Strand''') return dna.translate(dna.maketrans('''ATCG''' , '''TAGC''')) if __name__ == "__main__": import doctest doctest.testmod()
52
'''simple docstring''' import uuid from typing import Any, Dict, List, Optional, Union from ..utils import add_end_docstrings, is_tf_available, is_torch_available, logging from .base import PIPELINE_INIT_ARGS, Pipeline if is_tf_available(): import tensorflow as tf if is_torch_available(): import torch lowerCAmelCase : Optional[Any] = logging.get_logger(__name__) class SCREAMING_SNAKE_CASE__ : def __init__( self , A_ = None , A_ = None , A_=None , A_=None )-> Optional[Any]: '''simple docstring''' if not conversation_id: UpperCamelCase = uuid.uuida() if past_user_inputs is None: UpperCamelCase = [] if generated_responses is None: UpperCamelCase = [] UpperCamelCase = conversation_id UpperCamelCase = past_user_inputs UpperCamelCase = generated_responses UpperCamelCase = text def __eq__( self , A_ )-> List[Any]: '''simple docstring''' if not isinstance(A_ , A_ ): return False if self.uuid == other.uuid: return True return ( self.new_user_input == other.new_user_input and self.past_user_inputs == other.past_user_inputs and self.generated_responses == other.generated_responses ) def UpperCAmelCase_ ( self , A_ , A_ = False )-> int: '''simple docstring''' if self.new_user_input: if overwrite: logger.warning( F'''User input added while unprocessed input was existing: "{self.new_user_input}" was overwritten ''' F'''with: "{text}".''' ) UpperCamelCase = text else: logger.warning( F'''User input added while unprocessed input was existing: "{self.new_user_input}" new input ''' F'''ignored: "{text}". Set `overwrite` to True to overwrite unprocessed user input''' ) else: UpperCamelCase = text def UpperCAmelCase_ ( self )-> Any: '''simple docstring''' if self.new_user_input: self.past_user_inputs.append(self.new_user_input ) UpperCamelCase = None def UpperCAmelCase_ ( self , A_ )-> int: '''simple docstring''' self.generated_responses.append(A_ ) def UpperCAmelCase_ ( self )-> List[str]: '''simple docstring''' for user_input, generated_response in zip(self.past_user_inputs , self.generated_responses ): yield True, user_input yield False, generated_response if self.new_user_input: yield True, self.new_user_input def __repr__( self )-> Any: '''simple docstring''' UpperCamelCase = F'''Conversation id: {self.uuid} \n''' for is_user, text in self.iter_texts(): UpperCamelCase = 'user' if is_user else 'bot' output += F'''{name} >> {text} \n''' return output @add_end_docstrings( snake_case_ , R""" min_length_for_response (`int`, *optional*, defaults to 32): The minimum length (in number of tokens) for a response. minimum_tokens (`int`, *optional*, defaults to 10): The minimum length of tokens to leave for a response. """ , ) class SCREAMING_SNAKE_CASE__ ( snake_case_): def __init__( self , *A_ , **A_ )-> Any: '''simple docstring''' super().__init__(*A_ , **A_ ) if self.tokenizer.pad_token_id is None: UpperCamelCase = self.tokenizer.eos_token def UpperCAmelCase_ ( self , A_=None , A_=None , A_=None , **A_ )-> Union[str, Any]: '''simple docstring''' UpperCamelCase = {} UpperCamelCase = {} UpperCamelCase = {} if min_length_for_response is not None: UpperCamelCase = min_length_for_response if minimum_tokens is not None: UpperCamelCase = minimum_tokens if "max_length" in generate_kwargs: UpperCamelCase = generate_kwargs['max_length'] # self.max_length = generate_kwargs.get("max_length", self.model.config.max_length) if clean_up_tokenization_spaces is not None: UpperCamelCase = clean_up_tokenization_spaces if generate_kwargs: forward_params.update(A_ ) return preprocess_params, forward_params, postprocess_params def __call__( self , A_ , A_=0 , **A_ )-> Any: '''simple docstring''' UpperCamelCase = super().__call__(A_ , num_workers=A_ , **A_ ) if isinstance(A_ , A_ ) and len(A_ ) == 1: return outputs[0] return outputs def UpperCAmelCase_ ( self , A_ , A_=32 )-> Dict[str, Any]: '''simple docstring''' if not isinstance(A_ , A_ ): raise ValueError('ConversationalPipeline, expects Conversation as inputs' ) if conversation.new_user_input is None: raise ValueError( F'''Conversation with UUID {type(conversation.uuid )} does not contain new user input to process. ''' 'Add user inputs with the conversation\'s `add_user_input` method' ) if hasattr(self.tokenizer , '_build_conversation_input_ids' ): UpperCamelCase = self.tokenizer._build_conversation_input_ids(A_ ) else: # If the tokenizer cannot handle conversations, we default to only the old version UpperCamelCase = self._legacy_parse_and_tokenize(A_ ) if self.framework == "pt": UpperCamelCase = torch.LongTensor([input_ids] ) elif self.framework == "tf": UpperCamelCase = tf.constant([input_ids] ) return {"input_ids": input_ids, "conversation": conversation} def UpperCAmelCase_ ( self , A_ , A_=10 , **A_ )-> Optional[Any]: '''simple docstring''' UpperCamelCase = generate_kwargs.get('max_length' , self.model.config.max_length ) UpperCamelCase = model_inputs['input_ids'].shape[1] if max_length - minimum_tokens < n: logger.warning(F'''Conversation input is to long ({n}), trimming it to ({max_length} - {minimum_tokens})''' ) UpperCamelCase = max_length - minimum_tokens UpperCamelCase = model_inputs['input_ids'][:, -trim:] if "attention_mask" in model_inputs: UpperCamelCase = model_inputs['attention_mask'][:, -trim:] UpperCamelCase = model_inputs.pop('conversation' ) UpperCamelCase = max_length UpperCamelCase = self.model.generate(**A_ , **A_ ) if self.model.config.is_encoder_decoder: UpperCamelCase = 1 else: UpperCamelCase = n return {"output_ids": output_ids[:, start_position:], "conversation": conversation} def UpperCAmelCase_ ( self , A_ , A_=True )-> Tuple: '''simple docstring''' UpperCamelCase = model_outputs['output_ids'] UpperCamelCase = self.tokenizer.decode( output_ids[0] , skip_special_tokens=A_ , clean_up_tokenization_spaces=A_ , ) UpperCamelCase = model_outputs['conversation'] conversation.mark_processed() conversation.append_response(A_ ) return conversation def UpperCAmelCase_ ( self , A_ )-> Dict: '''simple docstring''' UpperCamelCase = self.tokenizer.eos_token_id UpperCamelCase = [] for is_user, text in conversation.iter_texts(): if eos_token_id is not None: input_ids.extend(self.tokenizer.encode(A_ , add_special_tokens=A_ ) + [eos_token_id] ) else: input_ids.extend(self.tokenizer.encode(A_ , add_special_tokens=A_ ) ) if len(A_ ) > self.tokenizer.model_max_length: UpperCamelCase = input_ids[-self.tokenizer.model_max_length :] return input_ids
3
0
def a_ ( lowerCAmelCase_ : int = 10 ): if not isinstance(lowerCAmelCase_, lowerCAmelCase_ ) or n < 0: raise ValueError('Invalid input' ) __lowerCAmelCase = 10**n __lowerCAmelCase = 2_8433 * (pow(2, 783_0457, lowerCAmelCase_ )) + 1 return str(number % modulus ) if __name__ == "__main__": from doctest import testmod testmod() print(F"""{solution(10) = }""")
53
'''simple docstring''' import sys import webbrowser import requests from bsa import BeautifulSoup from fake_useragent import UserAgent if __name__ == "__main__": print('Googling.....') lowerCAmelCase : List[Any] = 'https://www.google.com/search?q=' + ' '.join(sys.argv[1:]) lowerCAmelCase : List[Any] = requests.get(url, headers={'UserAgent': UserAgent().random}) # res.raise_for_status() with open('project1a.html', 'wb') as out_file: # only for knowing the class for data in res.iter_content(1_00_00): out_file.write(data) lowerCAmelCase : Tuple = BeautifulSoup(res.text, 'html.parser') lowerCAmelCase : List[Any] = list(soup.select('.eZt8xd'))[:5] print(len(links)) for link in links: if link.text == "Maps": webbrowser.open(link.get('href')) else: webbrowser.open(f"""https://google.com{link.get('href')}""")
3
0
import pickle import unittest import torch from accelerate import Accelerator from accelerate.state import AcceleratorState from accelerate.test_utils import require_cpu @require_cpu class A ( unittest.TestCase ): def lowerCAmelCase__ ( self: Optional[int] ) -> List[str]: '''simple docstring''' UpperCAmelCase_ =torch.nn.Linear(10 , 10 ) UpperCAmelCase_ =torch.optim.SGD(model.parameters() , 0.1 ) UpperCAmelCase_ =Accelerator() UpperCAmelCase_ =accelerator.prepare(_lowerCAmelCase ) try: pickle.loads(pickle.dumps(_lowerCAmelCase ) ) except Exception as e: self.fail(F'Accelerated optimizer pickling failed with {e}' ) AcceleratorState._reset_state()
54
'''simple docstring''' import numpy as np def A_( A : str , A : Optional[Any] , A : Tuple , A : Optional[int] , A : str): UpperCamelCase = int(np.ceil((x_end - xa) / h)) UpperCamelCase = np.zeros((n + 1,)) UpperCamelCase = ya UpperCamelCase = xa for k in range(A): UpperCamelCase = f(A , y[k]) UpperCamelCase = f(x + 0.5 * h , y[k] + 0.5 * h * ka) UpperCamelCase = f(x + 0.5 * h , y[k] + 0.5 * h * ka) UpperCamelCase = f(x + h , y[k] + h * ka) UpperCamelCase = y[k] + (1 / 6) * h * (ka + 2 * ka + 2 * ka + ka) x += h return y if __name__ == "__main__": import doctest doctest.testmod()
3
0
from ...configuration_utils import PretrainedConfig SCREAMING_SNAKE_CASE :Optional[int] = { 'google/tapas-base-finetuned-sqa': ( 'https://huggingface.co/google/tapas-base-finetuned-sqa/resolve/main/config.json' ), 'google/tapas-base-finetuned-wtq': ( 'https://huggingface.co/google/tapas-base-finetuned-wtq/resolve/main/config.json' ), 'google/tapas-base-finetuned-wikisql-supervised': ( 'https://huggingface.co/google/tapas-base-finetuned-wikisql-supervised/resolve/main/config.json' ), 'google/tapas-base-finetuned-tabfact': ( 'https://huggingface.co/google/tapas-base-finetuned-tabfact/resolve/main/config.json' ), } class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = "tapas" def __init__( self : List[Any] ,A : Dict=3_05_22 ,A : int=7_68 ,A : List[Any]=12 ,A : Any=12 ,A : str=30_72 ,A : Dict="gelu" ,A : str=0.1 ,A : Any=0.1 ,A : Union[str, Any]=10_24 ,A : Dict=[3, 2_56, 2_56, 2, 2_56, 2_56, 10] ,A : str=0.02 ,A : List[str]=1E-12 ,A : List[Any]=0 ,A : str=10.0 ,A : Any=0 ,A : Optional[int]=1.0 ,A : Optional[Any]=None ,A : Any=1.0 ,A : int=False ,A : str=None ,A : str=1.0 ,A : Optional[Any]=1.0 ,A : List[Any]=False ,A : Any=False ,A : Tuple="ratio" ,A : Optional[int]=None ,A : Any=None ,A : Optional[int]=64 ,A : List[Any]=32 ,A : int=False ,A : List[Any]=True ,A : str=False ,A : Optional[int]=False ,A : Any=True ,A : Tuple=False ,A : Tuple=None ,A : List[str]=None ,**A : Union[str, Any] ,): super().__init__(pad_token_id=A ,**A ) # BERT hyperparameters (with updated max_position_embeddings and type_vocab_sizes) __A = vocab_size __A = hidden_size __A = num_hidden_layers __A = num_attention_heads __A = hidden_act __A = intermediate_size __A = hidden_dropout_prob __A = attention_probs_dropout_prob __A = max_position_embeddings __A = type_vocab_sizes __A = initializer_range __A = layer_norm_eps # Fine-tuning task hyperparameters __A = positive_label_weight __A = num_aggregation_labels __A = aggregation_loss_weight __A = use_answer_as_supervision __A = answer_loss_importance __A = use_normalized_answer_loss __A = huber_loss_delta __A = temperature __A = aggregation_temperature __A = use_gumbel_for_cells __A = use_gumbel_for_aggregation __A = average_approximation_function __A = cell_selection_preference __A = answer_loss_cutoff __A = max_num_rows __A = max_num_columns __A = average_logits_per_cell __A = select_one_column __A = allow_empty_column_selection __A = init_cell_selection_weights_to_zero __A = reset_position_index_per_cell __A = disable_per_token_loss # Aggregation hyperparameters __A = aggregation_labels __A = no_aggregation_label_index if isinstance(self.aggregation_labels ,A ): __A = {int(A ): v for k, v in aggregation_labels.items()}
55
'''simple docstring''' from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Features, Value from .base import TaskTemplate @dataclass(frozen=snake_case_) class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = field(default="""language-modeling""" , metadata={"""include_in_asdict_even_if_is_default""": True}) lowerCAmelCase_ = Features({"""text""": Value("""string""")}) lowerCAmelCase_ = Features({}) lowerCAmelCase_ = "text" @property def UpperCAmelCase_ ( self )-> Dict[str, str]: '''simple docstring''' return {self.text_column: "text"}
3
0
'''simple docstring''' from __future__ import annotations from random import choice def _a (lowercase__ : Optional[Any] ) -> Tuple: """simple docstring""" return choice(lowercase__ ) def _a (lowercase__ : list[int] , lowercase__ : int ) -> int: """simple docstring""" __snake_case = random_pivot(lowercase__ ) # partition based on pivot # linear time __snake_case = [e for e in lst if e < pivot] __snake_case = [e for e in lst if e > pivot] # if we get lucky, pivot might be the element we want. # we can easily see this: # small (elements smaller than k) # + pivot (kth element) # + big (elements larger than k) if len(lowercase__ ) == k - 1: return pivot # pivot is in elements bigger than k elif len(lowercase__ ) < k - 1: return kth_number(lowercase__ , k - len(lowercase__ ) - 1 ) # pivot is in elements smaller than k else: return kth_number(lowercase__ , lowercase__ ) if __name__ == "__main__": import doctest doctest.testmod()
56
'''simple docstring''' from __future__ import annotations lowerCAmelCase : Union[str, Any] = [-10, -5, 0, 5, 5.1, 11, 13, 21, 3, 4, -21, -10, -5, -1, 0] lowerCAmelCase : List[str] = [-5, 0, 5, 5.1, 11, 13, 21, -1, 4, -1, -10, -5, -1, 0, -1] def A_( A : list[float]): UpperCamelCase = [] UpperCamelCase = len(A) for i in range(A): UpperCamelCase = -1 for j in range(i + 1 , A): if arr[i] < arr[j]: UpperCamelCase = arr[j] break result.append(A) return result def A_( A : list[float]): UpperCamelCase = [] for i, outer in enumerate(A): UpperCamelCase = -1 for inner in arr[i + 1 :]: if outer < inner: UpperCamelCase = inner break result.append(A) return result def A_( A : list[float]): UpperCamelCase = len(A) UpperCamelCase = [] UpperCamelCase = [-1] * arr_size for index in reversed(range(A)): if stack: while stack[-1] <= arr[index]: stack.pop() if not stack: break if stack: UpperCamelCase = stack[-1] stack.append(arr[index]) return result if __name__ == "__main__": from doctest import testmod from timeit import timeit testmod() print(next_greatest_element_slow(arr)) print(next_greatest_element_fast(arr)) print(next_greatest_element(arr)) lowerCAmelCase : Optional[Any] = ( 'from __main__ import arr, next_greatest_element_slow, ' 'next_greatest_element_fast, next_greatest_element' ) print( 'next_greatest_element_slow():', timeit('next_greatest_element_slow(arr)', setup=setup), ) print( 'next_greatest_element_fast():', timeit('next_greatest_element_fast(arr)', setup=setup), ) print( ' next_greatest_element():', timeit('next_greatest_element(arr)', setup=setup), )
3
0
from math import pi, sqrt def snake_case (UpperCAmelCase__ ) -> float: if num <= 0: raise ValueError('math domain error' ) if num > 171.5: raise OverflowError('math range error' ) elif num - int(UpperCAmelCase__ ) not in (0, 0.5): raise NotImplementedError('num must be an integer or a half-integer' ) elif num == 0.5: return sqrt(UpperCAmelCase__ ) else: return 1.0 if num == 1 else (num - 1) * gamma(num - 1 ) def snake_case () -> None: assert gamma(0.5 ) == sqrt(UpperCAmelCase__ ) assert gamma(1 ) == 1.0 assert gamma(2 ) == 1.0 if __name__ == "__main__": from doctest import testmod testmod() A_ : Union[str, Any] = 1.0 while num: A_ : str = float(input('Gamma of: ')) print(F'''gamma({num}) = {gamma(num)}''') print('\nEnter 0 to exit...')
57
'''simple docstring''' from string import ascii_lowercase, ascii_uppercase def A_( A : str): if not sentence: return "" UpperCamelCase = dict(zip(A , A)) return lower_to_upper.get(sentence[0] , sentence[0]) + sentence[1:] if __name__ == "__main__": from doctest import testmod testmod()
3
0
"""simple docstring""" from __future__ import annotations import csv import requests from bsa import BeautifulSoup def __lowerCAmelCase ( __UpperCamelCase : str = "" ): '''simple docstring''' snake_case_ : Optional[int] = url or """https://www.imdb.com/chart/top/?ref_=nv_mv_250""" snake_case_ : Dict = BeautifulSoup(requests.get(__UpperCamelCase ).text , """html.parser""" ) snake_case_ : str = soup.find_all("""td""" , attrs="""titleColumn""" ) snake_case_ : Union[str, Any] = soup.find_all("""td""" , class_="""ratingColumn imdbRating""" ) return { title.a.text: float(rating.strong.text ) for title, rating in zip(__UpperCamelCase , __UpperCamelCase ) } def __lowerCAmelCase ( __UpperCamelCase : str = "IMDb_Top_250_Movies.csv" ): '''simple docstring''' snake_case_ : int = get_imdb_top_aaa_movies() with open(__UpperCamelCase , """w""" , newline="""""" ) as out_file: snake_case_ : str = csv.writer(__UpperCamelCase ) writer.writerow(["""Movie title""", """IMDb rating"""] ) for title, rating in movies.items(): writer.writerow([title, rating] ) if __name__ == "__main__": write_movies()
58
'''simple docstring''' from typing import Optional, Tuple, Union import tensorflow as tf from ...activations_tf import ACTaFN from ...file_utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward from ...modeling_tf_outputs import ( TFBaseModelOutputWithNoAttention, TFBaseModelOutputWithPoolingAndNoAttention, TFSequenceClassifierOutput, ) from ...modeling_tf_utils import TFPreTrainedModel, TFSequenceClassificationLoss, keras_serializable, unpack_inputs from ...tf_utils import shape_list from ...utils import logging from .configuration_regnet import RegNetConfig lowerCAmelCase : Dict = logging.get_logger(__name__) # General docstring lowerCAmelCase : str = 'RegNetConfig' # Base docstring lowerCAmelCase : str = 'facebook/regnet-y-040' lowerCAmelCase : Dict = [1, 10_88, 7, 7] # Image classification docstring lowerCAmelCase : Dict = 'facebook/regnet-y-040' lowerCAmelCase : int = 'tabby, tabby cat' lowerCAmelCase : int = [ 'facebook/regnet-y-040', # See all regnet models at https://huggingface.co/models?filter=regnet ] class SCREAMING_SNAKE_CASE__ ( tf.keras.layers.Layer): def __init__( self , A_ , A_ = 3 , A_ = 1 , A_ = 1 , A_ = "relu" , **A_ , )-> str: '''simple docstring''' super().__init__(**A_ ) # The padding and conv has been verified in # https://colab.research.google.com/gist/sayakpaul/854bc10eeaf21c9ee2119e0b9f3841a7/scratchpad.ipynb UpperCamelCase = tf.keras.layers.ZeroPaddingaD(padding=kernel_size // 2 ) UpperCamelCase = tf.keras.layers.ConvaD( filters=A_ , kernel_size=A_ , strides=A_ , padding='VALID' , groups=A_ , use_bias=A_ , name='convolution' , ) UpperCamelCase = tf.keras.layers.BatchNormalization(epsilon=1e-5 , momentum=0.9 , name='normalization' ) UpperCamelCase = ACTaFN[activation] if activation is not None else tf.identity def UpperCAmelCase_ ( self , A_ )-> Any: '''simple docstring''' UpperCamelCase = self.convolution(self.padding(A_ ) ) UpperCamelCase = self.normalization(A_ ) UpperCamelCase = self.activation(A_ ) return hidden_state class SCREAMING_SNAKE_CASE__ ( tf.keras.layers.Layer): def __init__( self , A_ , **A_ )-> Optional[Any]: '''simple docstring''' super().__init__(**A_ ) UpperCamelCase = config.num_channels UpperCamelCase = TFRegNetConvLayer( out_channels=config.embedding_size , kernel_size=3 , stride=2 , activation=config.hidden_act , name='embedder' , ) def UpperCAmelCase_ ( self , A_ )-> List[Any]: '''simple docstring''' UpperCamelCase = shape_list(A_ )[1] if tf.executing_eagerly() and num_channels != self.num_channels: raise ValueError( 'Make sure that the channel dimension of the pixel values match with the one set in the configuration.' ) # When running on CPU, `tf.keras.layers.Conv2D` doesn't support `NCHW` format. # So change the input format from `NCHW` to `NHWC`. # shape = (batch_size, in_height, in_width, in_channels=num_channels) UpperCamelCase = tf.transpose(A_ , perm=(0, 2, 3, 1) ) UpperCamelCase = self.embedder(A_ ) return hidden_state class SCREAMING_SNAKE_CASE__ ( tf.keras.layers.Layer): def __init__( self , A_ , A_ = 2 , **A_ )-> List[Any]: '''simple docstring''' super().__init__(**A_ ) UpperCamelCase = tf.keras.layers.ConvaD( filters=A_ , kernel_size=1 , strides=A_ , use_bias=A_ , name='convolution' ) UpperCamelCase = tf.keras.layers.BatchNormalization(epsilon=1e-5 , momentum=0.9 , name='normalization' ) def UpperCAmelCase_ ( self , A_ , A_ = False )-> tf.Tensor: '''simple docstring''' return self.normalization(self.convolution(A_ ) , training=A_ ) class SCREAMING_SNAKE_CASE__ ( tf.keras.layers.Layer): def __init__( self , A_ , A_ , **A_ )-> Optional[Any]: '''simple docstring''' super().__init__(**A_ ) UpperCamelCase = tf.keras.layers.GlobalAveragePoolingaD(keepdims=A_ , name='pooler' ) UpperCamelCase = [ tf.keras.layers.ConvaD(filters=A_ , kernel_size=1 , activation='relu' , name='attention.0' ), tf.keras.layers.ConvaD(filters=A_ , kernel_size=1 , activation='sigmoid' , name='attention.2' ), ] def UpperCAmelCase_ ( self , A_ )-> Optional[int]: '''simple docstring''' UpperCamelCase = self.pooler(A_ ) for layer_module in self.attention: UpperCamelCase = layer_module(A_ ) UpperCamelCase = hidden_state * pooled return hidden_state class SCREAMING_SNAKE_CASE__ ( tf.keras.layers.Layer): def __init__( self , A_ , A_ , A_ , A_ = 1 , **A_ )-> Dict: '''simple docstring''' super().__init__(**A_ ) UpperCamelCase = in_channels != out_channels or stride != 1 UpperCamelCase = max(1 , out_channels // config.groups_width ) UpperCamelCase = ( TFRegNetShortCut(A_ , stride=A_ , name='shortcut' ) if should_apply_shortcut else tf.keras.layers.Activation('linear' , name='shortcut' ) ) # `self.layers` instead of `self.layer` because that is a reserved argument. UpperCamelCase = [ TFRegNetConvLayer(A_ , kernel_size=1 , activation=config.hidden_act , name='layer.0' ), TFRegNetConvLayer( A_ , stride=A_ , groups=A_ , activation=config.hidden_act , name='layer.1' ), TFRegNetConvLayer(A_ , kernel_size=1 , activation=A_ , name='layer.2' ), ] UpperCamelCase = ACTaFN[config.hidden_act] def UpperCAmelCase_ ( self , A_ )-> Tuple: '''simple docstring''' UpperCamelCase = hidden_state for layer_module in self.layers: UpperCamelCase = layer_module(A_ ) UpperCamelCase = self.shortcut(A_ ) hidden_state += residual UpperCamelCase = self.activation(A_ ) return hidden_state class SCREAMING_SNAKE_CASE__ ( tf.keras.layers.Layer): def __init__( self , A_ , A_ , A_ , A_ = 1 , **A_ )-> Any: '''simple docstring''' super().__init__(**A_ ) UpperCamelCase = in_channels != out_channels or stride != 1 UpperCamelCase = max(1 , out_channels // config.groups_width ) UpperCamelCase = ( TFRegNetShortCut(A_ , stride=A_ , name='shortcut' ) if should_apply_shortcut else tf.keras.layers.Activation('linear' , name='shortcut' ) ) UpperCamelCase = [ TFRegNetConvLayer(A_ , kernel_size=1 , activation=config.hidden_act , name='layer.0' ), TFRegNetConvLayer( A_ , stride=A_ , groups=A_ , activation=config.hidden_act , name='layer.1' ), TFRegNetSELayer(A_ , reduced_channels=int(round(in_channels / 4 ) ) , name='layer.2' ), TFRegNetConvLayer(A_ , kernel_size=1 , activation=A_ , name='layer.3' ), ] UpperCamelCase = ACTaFN[config.hidden_act] def UpperCAmelCase_ ( self , A_ )-> List[Any]: '''simple docstring''' UpperCamelCase = hidden_state for layer_module in self.layers: UpperCamelCase = layer_module(A_ ) UpperCamelCase = self.shortcut(A_ ) hidden_state += residual UpperCamelCase = self.activation(A_ ) return hidden_state class SCREAMING_SNAKE_CASE__ ( tf.keras.layers.Layer): def __init__( self , A_ , A_ , A_ , A_ = 2 , A_ = 2 , **A_ )-> Dict: '''simple docstring''' super().__init__(**A_ ) UpperCamelCase = TFRegNetXLayer if config.layer_type == 'x' else TFRegNetYLayer UpperCamelCase = [ # downsampling is done in the first layer with stride of 2 layer(A_ , A_ , A_ , stride=A_ , name='layers.0' ), *[layer(A_ , A_ , A_ , name=F'''layers.{i+1}''' ) for i in range(depth - 1 )], ] def UpperCAmelCase_ ( self , A_ )-> List[Any]: '''simple docstring''' for layer_module in self.layers: UpperCamelCase = layer_module(A_ ) return hidden_state class SCREAMING_SNAKE_CASE__ ( tf.keras.layers.Layer): def __init__( self , A_ , **A_ )-> str: '''simple docstring''' super().__init__(**A_ ) UpperCamelCase = [] # based on `downsample_in_first_stage`, the first layer of the first stage may or may not downsample the input self.stages.append( TFRegNetStage( A_ , config.embedding_size , config.hidden_sizes[0] , stride=2 if config.downsample_in_first_stage else 1 , depth=config.depths[0] , name='stages.0' , ) ) UpperCamelCase = zip(config.hidden_sizes , config.hidden_sizes[1:] ) for i, ((in_channels, out_channels), depth) in enumerate(zip(A_ , config.depths[1:] ) ): self.stages.append(TFRegNetStage(A_ , A_ , A_ , depth=A_ , name=F'''stages.{i+1}''' ) ) def UpperCAmelCase_ ( self , A_ , A_ = False , A_ = True )-> TFBaseModelOutputWithNoAttention: '''simple docstring''' UpperCamelCase = () if output_hidden_states else None for stage_module in self.stages: if output_hidden_states: UpperCamelCase = hidden_states + (hidden_state,) UpperCamelCase = stage_module(A_ ) if output_hidden_states: UpperCamelCase = hidden_states + (hidden_state,) if not return_dict: return tuple(v for v in [hidden_state, hidden_states] if v is not None ) return TFBaseModelOutputWithNoAttention(last_hidden_state=A_ , hidden_states=A_ ) @keras_serializable class SCREAMING_SNAKE_CASE__ ( tf.keras.layers.Layer): lowerCAmelCase_ = RegNetConfig def __init__( self , A_ , **A_ )-> Union[str, Any]: '''simple docstring''' super().__init__(**A_ ) UpperCamelCase = config UpperCamelCase = TFRegNetEmbeddings(A_ , name='embedder' ) UpperCamelCase = TFRegNetEncoder(A_ , name='encoder' ) UpperCamelCase = tf.keras.layers.GlobalAveragePoolingaD(keepdims=A_ , name='pooler' ) @unpack_inputs def UpperCAmelCase_ ( self , A_ , A_ = None , A_ = None , A_ = False , )-> TFBaseModelOutputWithPoolingAndNoAttention: '''simple docstring''' UpperCamelCase = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) UpperCamelCase = return_dict if return_dict is not None else self.config.use_return_dict UpperCamelCase = self.embedder(A_ , training=A_ ) UpperCamelCase = self.encoder( A_ , output_hidden_states=A_ , return_dict=A_ , training=A_ ) UpperCamelCase = encoder_outputs[0] UpperCamelCase = self.pooler(A_ ) # Change to NCHW output format have uniformity in the modules UpperCamelCase = tf.transpose(A_ , perm=(0, 3, 1, 2) ) UpperCamelCase = tf.transpose(A_ , perm=(0, 3, 1, 2) ) # Change the other hidden state outputs to NCHW as well if output_hidden_states: UpperCamelCase = tuple([tf.transpose(A_ , perm=(0, 3, 1, 2) ) for h in encoder_outputs[1]] ) if not return_dict: return (last_hidden_state, pooled_output) + encoder_outputs[1:] return TFBaseModelOutputWithPoolingAndNoAttention( last_hidden_state=A_ , pooler_output=A_ , hidden_states=hidden_states if output_hidden_states else encoder_outputs.hidden_states , ) class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = RegNetConfig lowerCAmelCase_ = """regnet""" lowerCAmelCase_ = """pixel_values""" @property def UpperCAmelCase_ ( self )-> List[str]: '''simple docstring''' return {"pixel_values": tf.TensorSpec(shape=(None, self.config.num_channels, 224, 224) , dtype=tf.floataa )} lowerCAmelCase : str = r'\n Parameters:\n This model is a Tensorflow\n [tf.keras.layers.Layer](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Layer) sub-class. Use it as a\n regular Tensorflow Module and refer to the Tensorflow documentation for all matter related to general usage and\n behavior.\n config ([`RegNetConfig`]): Model configuration class with all the parameters of the model.\n Initializing with a config file does not load the weights associated with the model, only the\n configuration. Check out the [`~TFPreTrainedModel.from_pretrained`] method to load the model weights.\n' lowerCAmelCase : List[str] = r'\n Args:\n pixel_values (`tf.Tensor` of shape `(batch_size, num_channels, height, width)`):\n Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n [`ConveNextImageProcessor.__call__`] for details.\n output_hidden_states (`bool`, *optional*):\n Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for\n more detail.\n return_dict (`bool`, *optional*):\n Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n' @add_start_docstrings( """The bare RegNet model outputting raw features without any specific head on top.""" , snake_case_ , ) class SCREAMING_SNAKE_CASE__ ( snake_case_): def __init__( self , A_ , *A_ , **A_ )-> List[Any]: '''simple docstring''' super().__init__(A_ , *A_ , **A_ ) UpperCamelCase = TFRegNetMainLayer(A_ , name='regnet' ) @unpack_inputs @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 UpperCAmelCase_ ( self , A_ , A_ = None , A_ = None , A_=False , )-> Union[TFBaseModelOutputWithPoolingAndNoAttention, Tuple[tf.Tensor]]: '''simple docstring''' UpperCamelCase = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) UpperCamelCase = return_dict if return_dict is not None else self.config.use_return_dict UpperCamelCase = self.regnet( pixel_values=A_ , output_hidden_states=A_ , return_dict=A_ , training=A_ , ) if not return_dict: return (outputs[0],) + outputs[1:] return TFBaseModelOutputWithPoolingAndNoAttention( last_hidden_state=outputs.last_hidden_state , pooler_output=outputs.pooler_output , hidden_states=outputs.hidden_states , ) @add_start_docstrings( """ RegNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for ImageNet. """ , snake_case_ , ) class SCREAMING_SNAKE_CASE__ ( snake_case_ , snake_case_): def __init__( self , A_ , *A_ , **A_ )-> str: '''simple docstring''' super().__init__(A_ , *A_ , **A_ ) UpperCamelCase = config.num_labels UpperCamelCase = TFRegNetMainLayer(A_ , name='regnet' ) # classification head UpperCamelCase = [ tf.keras.layers.Flatten(), tf.keras.layers.Dense(config.num_labels , name='classifier.1' ) if config.num_labels > 0 else tf.identity, ] @unpack_inputs @add_start_docstrings_to_model_forward(A_ ) @add_code_sample_docstrings( checkpoint=_IMAGE_CLASS_CHECKPOINT , output_type=A_ , config_class=_CONFIG_FOR_DOC , expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT , ) def UpperCAmelCase_ ( self , A_ = None , A_ = None , A_ = None , A_ = None , A_=False , )-> Union[TFSequenceClassifierOutput, Tuple[tf.Tensor]]: '''simple docstring''' UpperCamelCase = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) UpperCamelCase = return_dict if return_dict is not None else self.config.use_return_dict UpperCamelCase = self.regnet( A_ , output_hidden_states=A_ , return_dict=A_ , training=A_ ) UpperCamelCase = outputs.pooler_output if return_dict else outputs[1] UpperCamelCase = self.classifier[0](A_ ) UpperCamelCase = self.classifier[1](A_ ) UpperCamelCase = None if labels is None else self.hf_compute_loss(labels=A_ , logits=A_ ) if not return_dict: UpperCamelCase = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return TFSequenceClassifierOutput(loss=A_ , logits=A_ , hidden_states=outputs.hidden_states )
3
0
import tempfile import unittest import numpy as np from huggingface_hub import HfFolder, delete_repo from requests.exceptions import HTTPError from transformers import BertConfig, is_flax_available from transformers.testing_utils import TOKEN, USER, is_staging_test, require_flax if is_flax_available(): import os from flax.core.frozen_dict import unfreeze from flax.traverse_util import flatten_dict from transformers import FlaxBertModel __A = "0.12" # assumed parallelism: 8 @require_flax @is_staging_test class _SCREAMING_SNAKE_CASE ( unittest.TestCase ): '''simple docstring''' @classmethod def SCREAMING_SNAKE_CASE_ (cls : str) ->Optional[Any]: '''simple docstring''' lowerCamelCase__: Optional[int] =TOKEN HfFolder.save_token(UpperCAmelCase_) @classmethod def SCREAMING_SNAKE_CASE_ (cls : Optional[Any]) ->int: '''simple docstring''' try: delete_repo(token=cls._token , repo_id="test-model-flax") except HTTPError: pass try: delete_repo(token=cls._token , repo_id="valid_org/test-model-flax-org") except HTTPError: pass def SCREAMING_SNAKE_CASE_ (self : Any) ->Tuple: '''simple docstring''' lowerCamelCase__: str =BertConfig( vocab_size=99 , hidden_size=32 , num_hidden_layers=5 , num_attention_heads=4 , intermediate_size=37) lowerCamelCase__: Dict =FlaxBertModel(UpperCAmelCase_) model.push_to_hub("test-model-flax" , use_auth_token=self._token) lowerCamelCase__: Union[str, Any] =FlaxBertModel.from_pretrained(F"""{USER}/test-model-flax""") lowerCamelCase__: str =flatten_dict(unfreeze(model.params)) lowerCamelCase__: Union[str, Any] =flatten_dict(unfreeze(new_model.params)) for key in base_params.keys(): lowerCamelCase__: List[str] =(base_params[key] - new_params[key]).sum().item() self.assertLessEqual(UpperCAmelCase_ , 1E-3 , msg=F"""{key} not identical""") # Reset repo delete_repo(token=self._token , repo_id="test-model-flax") # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(UpperCAmelCase_ , repo_id="test-model-flax" , push_to_hub=UpperCAmelCase_ , use_auth_token=self._token) lowerCamelCase__: Tuple =FlaxBertModel.from_pretrained(F"""{USER}/test-model-flax""") lowerCamelCase__: Optional[int] =flatten_dict(unfreeze(model.params)) lowerCamelCase__: List[str] =flatten_dict(unfreeze(new_model.params)) for key in base_params.keys(): lowerCamelCase__: List[Any] =(base_params[key] - new_params[key]).sum().item() self.assertLessEqual(UpperCAmelCase_ , 1E-3 , msg=F"""{key} not identical""") def SCREAMING_SNAKE_CASE_ (self : Union[str, Any]) ->str: '''simple docstring''' lowerCamelCase__: Any =BertConfig( vocab_size=99 , hidden_size=32 , num_hidden_layers=5 , num_attention_heads=4 , intermediate_size=37) lowerCamelCase__: List[str] =FlaxBertModel(UpperCAmelCase_) model.push_to_hub("valid_org/test-model-flax-org" , use_auth_token=self._token) lowerCamelCase__: Optional[int] =FlaxBertModel.from_pretrained("valid_org/test-model-flax-org") lowerCamelCase__: int =flatten_dict(unfreeze(model.params)) lowerCamelCase__: Any =flatten_dict(unfreeze(new_model.params)) for key in base_params.keys(): lowerCamelCase__: List[str] =(base_params[key] - new_params[key]).sum().item() self.assertLessEqual(UpperCAmelCase_ , 1E-3 , msg=F"""{key} not identical""") # Reset repo delete_repo(token=self._token , repo_id="valid_org/test-model-flax-org") # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained( UpperCAmelCase_ , repo_id="valid_org/test-model-flax-org" , push_to_hub=UpperCAmelCase_ , use_auth_token=self._token) lowerCamelCase__: int =FlaxBertModel.from_pretrained("valid_org/test-model-flax-org") lowerCamelCase__: Union[str, Any] =flatten_dict(unfreeze(model.params)) lowerCamelCase__: Union[str, Any] =flatten_dict(unfreeze(new_model.params)) for key in base_params.keys(): lowerCamelCase__: Tuple =(base_params[key] - new_params[key]).sum().item() self.assertLessEqual(UpperCAmelCase_ , 1E-3 , msg=F"""{key} not identical""") def lowerCAmelCase_ ( __a , __a ) -> Union[str, Any]: """simple docstring""" lowerCamelCase__: int =True lowerCamelCase__: Any =flatten_dict(modela.params ) lowerCamelCase__: Any =flatten_dict(modela.params ) for key in flat_params_a.keys(): if np.sum(np.abs(flat_params_a[key] - flat_params_a[key] ) ) > 1e-4: lowerCamelCase__: Dict =False return models_are_equal @require_flax class _SCREAMING_SNAKE_CASE ( unittest.TestCase ): '''simple docstring''' def SCREAMING_SNAKE_CASE_ (self : str) ->Any: '''simple docstring''' lowerCamelCase__: Optional[Any] =BertConfig.from_pretrained("hf-internal-testing/tiny-bert-flax-only") lowerCamelCase__: str =FlaxBertModel(UpperCAmelCase_) lowerCamelCase__: int ="bert" with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(os.path.join(UpperCAmelCase_ , UpperCAmelCase_)) with self.assertRaises(UpperCAmelCase_): lowerCamelCase__: Tuple =FlaxBertModel.from_pretrained(UpperCAmelCase_) lowerCamelCase__: Optional[int] =FlaxBertModel.from_pretrained(UpperCAmelCase_ , subfolder=UpperCAmelCase_) self.assertTrue(check_models_equal(UpperCAmelCase_ , UpperCAmelCase_)) def SCREAMING_SNAKE_CASE_ (self : Any) ->Any: '''simple docstring''' lowerCamelCase__: Union[str, Any] =BertConfig.from_pretrained("hf-internal-testing/tiny-bert-flax-only") lowerCamelCase__: List[Any] =FlaxBertModel(UpperCAmelCase_) lowerCamelCase__: int ="bert" with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(os.path.join(UpperCAmelCase_ , UpperCAmelCase_) , max_shard_size="10KB") with self.assertRaises(UpperCAmelCase_): lowerCamelCase__: Dict =FlaxBertModel.from_pretrained(UpperCAmelCase_) lowerCamelCase__: str =FlaxBertModel.from_pretrained(UpperCAmelCase_ , subfolder=UpperCAmelCase_) self.assertTrue(check_models_equal(UpperCAmelCase_ , UpperCAmelCase_)) def SCREAMING_SNAKE_CASE_ (self : List[Any]) ->Optional[Any]: '''simple docstring''' lowerCamelCase__: str ="bert" lowerCamelCase__: Optional[Any] ="hf-internal-testing/tiny-random-bert-subfolder" with self.assertRaises(UpperCAmelCase_): lowerCamelCase__: Union[str, Any] =FlaxBertModel.from_pretrained(UpperCAmelCase_) lowerCamelCase__: Tuple =FlaxBertModel.from_pretrained(UpperCAmelCase_ , subfolder=UpperCAmelCase_) self.assertIsNotNone(UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ (self : Dict) ->Union[str, Any]: '''simple docstring''' lowerCamelCase__: List[str] ="bert" lowerCamelCase__: Optional[int] ="hf-internal-testing/tiny-random-bert-sharded-subfolder" with self.assertRaises(UpperCAmelCase_): lowerCamelCase__: Dict =FlaxBertModel.from_pretrained(UpperCAmelCase_) lowerCamelCase__: Any =FlaxBertModel.from_pretrained(UpperCAmelCase_ , subfolder=UpperCAmelCase_) self.assertIsNotNone(UpperCAmelCase_)
59
'''simple docstring''' from collections import OrderedDict from typing import Any, Mapping, Optional, Union from ...configuration_utils import PretrainedConfig from ...feature_extraction_utils import FeatureExtractionMixin from ...onnx import OnnxConfig from ...onnx.utils import compute_effective_axis_dimension from ...tokenization_utils_base import PreTrainedTokenizerBase from ...utils import TensorType, logging lowerCAmelCase : Any = logging.get_logger(__name__) lowerCAmelCase : Optional[int] = { 'deepmind/language-perceiver': 'https://huggingface.co/deepmind/language-perceiver/resolve/main/config.json', # See all Perceiver models at https://huggingface.co/models?filter=perceiver } class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = """perceiver""" def __init__( self , A_=256 , A_=1280 , A_=768 , A_=1 , A_=26 , A_=8 , A_=8 , A_=None , A_=None , A_="kv" , A_=1 , A_=1 , A_="gelu" , A_=0.1 , A_=0.02 , A_=1e-12 , A_=True , A_=262 , A_=2048 , A_=56 , A_=[368, 496] , A_=16 , A_=1920 , A_=16 , A_=[1, 16, 224, 224] , **A_ , )-> str: '''simple docstring''' super().__init__(**A_ ) UpperCamelCase = num_latents UpperCamelCase = d_latents UpperCamelCase = d_model UpperCamelCase = num_blocks UpperCamelCase = num_self_attends_per_block UpperCamelCase = num_self_attention_heads UpperCamelCase = num_cross_attention_heads UpperCamelCase = qk_channels UpperCamelCase = v_channels UpperCamelCase = cross_attention_shape_for_attention UpperCamelCase = self_attention_widening_factor UpperCamelCase = cross_attention_widening_factor UpperCamelCase = hidden_act UpperCamelCase = attention_probs_dropout_prob UpperCamelCase = initializer_range UpperCamelCase = layer_norm_eps UpperCamelCase = use_query_residual # masked language modeling attributes UpperCamelCase = vocab_size UpperCamelCase = max_position_embeddings # image classification attributes UpperCamelCase = image_size # flow attributes UpperCamelCase = train_size # multimodal autoencoding attributes UpperCamelCase = num_frames UpperCamelCase = audio_samples_per_frame UpperCamelCase = samples_per_patch UpperCamelCase = output_shape class SCREAMING_SNAKE_CASE__ ( snake_case_): @property def UpperCAmelCase_ ( self )-> Mapping[str, Mapping[int, str]]: '''simple docstring''' if self.task == "multiple-choice": UpperCamelCase = {0: 'batch', 1: 'choice', 2: 'sequence'} else: UpperCamelCase = {0: 'batch', 1: 'sequence'} return OrderedDict( [ ('inputs', dynamic_axis), ('attention_mask', dynamic_axis), ] ) @property def UpperCAmelCase_ ( self )-> float: '''simple docstring''' return 1e-4 def UpperCAmelCase_ ( self , A_ , A_ = -1 , A_ = -1 , A_ = -1 , A_ = False , A_ = None , A_ = 3 , A_ = 40 , A_ = 40 , )-> Mapping[str, Any]: '''simple docstring''' if isinstance(A_ , A_ ): # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX UpperCamelCase = compute_effective_axis_dimension( A_ , fixed_dimension=OnnxConfig.default_fixed_batch , num_token_to_add=0 ) # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX UpperCamelCase = preprocessor.num_special_tokens_to_add(A_ ) UpperCamelCase = compute_effective_axis_dimension( A_ , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=A_ ) # Generate dummy inputs according to compute batch and sequence UpperCamelCase = [' '.join(['a'] ) * seq_length] * batch_size UpperCamelCase = dict(preprocessor(A_ , return_tensors=A_ ) ) UpperCamelCase = inputs.pop('input_ids' ) return inputs elif isinstance(A_ , A_ ) and preprocessor.model_input_names[0] == "pixel_values": # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX UpperCamelCase = compute_effective_axis_dimension(A_ , fixed_dimension=OnnxConfig.default_fixed_batch ) UpperCamelCase = self._generate_dummy_images(A_ , A_ , A_ , A_ ) UpperCamelCase = dict(preprocessor(images=A_ , return_tensors=A_ ) ) UpperCamelCase = inputs.pop('pixel_values' ) return inputs else: raise ValueError( 'Unable to generate dummy inputs for the model. Please provide a tokenizer or a preprocessor.' )
3
0
import sys def lowerCamelCase_ ( _UpperCamelCase ) -> Tuple: """simple docstring""" snake_case_ : Union[str, Any] = len(_UpperCamelCase ) snake_case_ : List[Any] = [[0 for x in range(_UpperCamelCase )] for x in range(_UpperCamelCase )] snake_case_ : List[Any] = [[0 for x in range(_UpperCamelCase )] for x in range(_UpperCamelCase )] for chain_length in range(2 , _UpperCamelCase ): for a in range(1 , n - chain_length + 1 ): snake_case_ : Dict = a + chain_length - 1 snake_case_ : Tuple = sys.maxsize for c in range(_UpperCamelCase , _UpperCamelCase ): snake_case_ : Dict = ( matrix[a][c] + matrix[c + 1][b] + array[a - 1] * array[c] * array[b] ) if cost < matrix[a][b]: snake_case_ : List[str] = cost snake_case_ : int = c return matrix, sol def lowerCamelCase_ ( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) -> Any: """simple docstring""" if i == j: print('''A''' + str(_UpperCamelCase ) , end=''' ''' ) else: print('''(''' , end=''' ''' ) print_optiomal_solution(_UpperCamelCase , _UpperCamelCase , optimal_solution[i][j] ) print_optiomal_solution(_UpperCamelCase , optimal_solution[i][j] + 1 , _UpperCamelCase ) print(''')''' , end=''' ''' ) def lowerCamelCase_ ( ) -> Union[str, Any]: """simple docstring""" snake_case_ : Tuple = [30, 35, 15, 5, 10, 20, 25] snake_case_ : str = len(_UpperCamelCase ) # Size of matrix created from above array will be # 30*35 35*15 15*5 5*10 10*20 20*25 snake_case_ , snake_case_ : Optional[Any] = matrix_chain_order(_UpperCamelCase ) print('''No. of Operation required: ''' + str(matrix[1][n - 1] ) ) print_optiomal_solution(_UpperCamelCase , 1 , n - 1 ) if __name__ == "__main__": main()
60
'''simple docstring''' from ....configuration_utils import PretrainedConfig from ....utils import logging lowerCAmelCase : Optional[Any] = logging.get_logger(__name__) lowerCAmelCase : Dict = { 'speechbrain/m-ctc-t-large': 'https://huggingface.co/speechbrain/m-ctc-t-large/resolve/main/config.json', # See all M-CTC-T models at https://huggingface.co/models?filter=mctct } class SCREAMING_SNAKE_CASE__ ( snake_case_): lowerCAmelCase_ = """mctct""" def __init__( self , A_=8065 , A_=1536 , A_=36 , A_=6144 , A_=4 , A_=384 , A_=920 , A_=1e-5 , A_=0.3 , A_="relu" , A_=0.02 , A_=0.3 , A_=0.3 , A_=1 , A_=0 , A_=2 , A_=1 , A_=0.3 , A_=1 , A_=(7,) , A_=(3,) , A_=80 , A_=1 , A_=None , A_="sum" , A_=False , **A_ , )-> str: '''simple docstring''' super().__init__(**A_ , pad_token_id=A_ , bos_token_id=A_ , eos_token_id=A_ ) UpperCamelCase = vocab_size UpperCamelCase = hidden_size UpperCamelCase = num_hidden_layers UpperCamelCase = intermediate_size UpperCamelCase = num_attention_heads UpperCamelCase = attention_head_dim UpperCamelCase = max_position_embeddings UpperCamelCase = layer_norm_eps UpperCamelCase = layerdrop UpperCamelCase = hidden_act UpperCamelCase = initializer_range UpperCamelCase = hidden_dropout_prob UpperCamelCase = attention_probs_dropout_prob UpperCamelCase = pad_token_id UpperCamelCase = bos_token_id UpperCamelCase = eos_token_id UpperCamelCase = conv_glu_dim UpperCamelCase = conv_dropout UpperCamelCase = num_conv_layers UpperCamelCase = input_feat_per_channel UpperCamelCase = input_channels UpperCamelCase = conv_channels UpperCamelCase = ctc_loss_reduction UpperCamelCase = ctc_zero_infinity # prevents config testing fail with exporting to json UpperCamelCase = list(A_ ) UpperCamelCase = list(A_ ) if len(self.conv_kernel ) != self.num_conv_layers: raise ValueError( 'Configuration for convolutional module is incorrect. ' 'It is required that `len(config.conv_kernel)` == `config.num_conv_layers` ' F'''but is `len(config.conv_kernel) = {len(self.conv_kernel )}`, ''' F'''`config.num_conv_layers = {self.num_conv_layers}`.''' )
3
0