code
stringlengths
87
55.2k
code_codestyle
int64
0
349
style_context
stringlengths
135
49.1k
style_context_codestyle
int64
0
349
label
int64
0
1
from tempfile import TemporaryDirectory from unittest import TestCase from unittest.mock import MagicMock, patch from transformers import AutoModel, TFAutoModel from transformers.onnx import FeaturesManager from transformers.testing_utils import SMALL_MODEL_IDENTIFIER, require_tf, require_torch @require_torch @require_tf class __magic_name__ ( lowerCamelCase__ ): """simple docstring""" def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' A_ : Optional[int] = SMALL_MODEL_IDENTIFIER A_ : Optional[int] = "pt" A_ : List[str] = "tf" def SCREAMING_SNAKE_CASE ( self :int , snake_case :List[str] ): '''simple docstring''' A_ : int = AutoModel.from_pretrained(self.test_model ) model_pt.save_pretrained(snake_case ) def SCREAMING_SNAKE_CASE ( self :Union[str, Any] , snake_case :int ): '''simple docstring''' A_ : Union[str, Any] = TFAutoModel.from_pretrained(self.test_model , from_pt=snake_case ) model_tf.save_pretrained(snake_case ) def SCREAMING_SNAKE_CASE ( self :Dict ): '''simple docstring''' A_ : str = "mock_framework" # Framework provided - return whatever the user provides A_ : Any = FeaturesManager.determine_framework(self.test_model , snake_case ) self.assertEqual(snake_case , snake_case ) # Local checkpoint and framework provided - return provided framework # PyTorch checkpoint with TemporaryDirectory() as local_pt_ckpt: self._setup_pt_ckpt(snake_case ) A_ : Tuple = FeaturesManager.determine_framework(snake_case , snake_case ) self.assertEqual(snake_case , snake_case ) # TensorFlow checkpoint with TemporaryDirectory() as local_tf_ckpt: self._setup_tf_ckpt(snake_case ) A_ : int = FeaturesManager.determine_framework(snake_case , snake_case ) self.assertEqual(snake_case , snake_case ) def SCREAMING_SNAKE_CASE ( self :Optional[Any] ): '''simple docstring''' with TemporaryDirectory() as local_pt_ckpt: self._setup_pt_ckpt(snake_case ) A_ : Dict = FeaturesManager.determine_framework(snake_case ) self.assertEqual(snake_case , self.framework_pt ) # TensorFlow checkpoint with TemporaryDirectory() as local_tf_ckpt: self._setup_tf_ckpt(snake_case ) A_ : int = FeaturesManager.determine_framework(snake_case ) self.assertEqual(snake_case , self.framework_tf ) # Invalid local checkpoint with TemporaryDirectory() as local_invalid_ckpt: with self.assertRaises(snake_case ): A_ : Optional[Any] = FeaturesManager.determine_framework(snake_case ) def SCREAMING_SNAKE_CASE ( self :Optional[Any] ): '''simple docstring''' A_ : str = MagicMock(return_value=snake_case ) with patch("transformers.onnx.features.is_tf_available" , snake_case ): A_ : Optional[int] = FeaturesManager.determine_framework(self.test_model ) self.assertEqual(snake_case , self.framework_pt ) # PyTorch not in environment -> use TensorFlow A_ : int = MagicMock(return_value=snake_case ) with patch("transformers.onnx.features.is_torch_available" , snake_case ): A_ : Tuple = FeaturesManager.determine_framework(self.test_model ) self.assertEqual(snake_case , self.framework_tf ) # Both in environment -> use PyTorch A_ : int = MagicMock(return_value=snake_case ) A_ : Union[str, Any] = MagicMock(return_value=snake_case ) with patch("transformers.onnx.features.is_tf_available" , snake_case ), patch( "transformers.onnx.features.is_torch_available" , snake_case ): A_ : List[str] = FeaturesManager.determine_framework(self.test_model ) self.assertEqual(snake_case , self.framework_pt ) # Both not in environment -> raise error A_ : Optional[Any] = MagicMock(return_value=snake_case ) A_ : List[str] = MagicMock(return_value=snake_case ) with patch("transformers.onnx.features.is_tf_available" , snake_case ), patch( "transformers.onnx.features.is_torch_available" , snake_case ): with self.assertRaises(snake_case ): A_ : str = FeaturesManager.determine_framework(self.test_model )
300
def __snake_case ( _lowerCAmelCase : List[str] , _lowerCAmelCase : int ) -> str: return (pointa[0] - pointa[0]) ** 2 + (pointa[1] - pointa[1]) ** 2 def __snake_case ( _lowerCAmelCase : int , _lowerCAmelCase : Union[str, Any]=0 ) -> Any: return sorted(_lowerCAmelCase , key=lambda _lowerCAmelCase : x[column] ) def __snake_case ( _lowerCAmelCase : List[str] , _lowerCAmelCase : Any , _lowerCAmelCase : Any=float("inf" ) ) -> int: for i in range(points_counts - 1 ): for j in range(i + 1 , _lowerCAmelCase ): A_ : Tuple = euclidean_distance_sqr(points[i] , points[j] ) if current_dis < min_dis: A_ : Union[str, Any] = current_dis return min_dis def __snake_case ( _lowerCAmelCase : Optional[int] , _lowerCAmelCase : Dict , _lowerCAmelCase : List[str]=float("inf" ) ) -> Dict: for i in range(min(6 , points_counts - 1 ) , _lowerCAmelCase ): for j in range(max(0 , i - 6 ) , _lowerCAmelCase ): A_ : List[Any] = euclidean_distance_sqr(points[i] , points[j] ) if current_dis < min_dis: A_ : Union[str, Any] = current_dis return min_dis def __snake_case ( _lowerCAmelCase : List[Any] , _lowerCAmelCase : str , _lowerCAmelCase : Dict ) -> List[str]: # base case if points_counts <= 3: return dis_between_closest_pair(_lowerCAmelCase , _lowerCAmelCase ) # recursion A_ : Optional[int] = points_counts // 2 A_ : List[Any] = closest_pair_of_points_sqr( _lowerCAmelCase , points_sorted_on_y[:mid] , _lowerCAmelCase ) A_ : List[Any] = closest_pair_of_points_sqr( _lowerCAmelCase , points_sorted_on_y[mid:] , points_counts - mid ) A_ : Tuple = min(_lowerCAmelCase , _lowerCAmelCase ) A_ : Dict = [] for point in points_sorted_on_x: if abs(point[0] - points_sorted_on_x[mid][0] ) < closest_pair_dis: cross_strip.append(_lowerCAmelCase ) A_ : Tuple = dis_between_closest_in_strip( _lowerCAmelCase , len(_lowerCAmelCase ) , _lowerCAmelCase ) return min(_lowerCAmelCase , _lowerCAmelCase ) def __snake_case ( _lowerCAmelCase : str , _lowerCAmelCase : Optional[Any] ) -> Any: A_ : Optional[Any] = column_based_sort(_lowerCAmelCase , column=0 ) A_ : Optional[int] = column_based_sort(_lowerCAmelCase , column=1 ) return ( closest_pair_of_points_sqr( _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) ) ** 0.5 if __name__ == "__main__": _lowerCAmelCase : List[Any] = [(2, 3), (12, 30), (40, 50), (5, 1), (12, 10), (3, 4)] print('''Distance:''', closest_pair_of_points(points, len(points)))
300
1
from collections import UserDict from typing import Union import numpy as np import requests from ..utils import ( add_end_docstrings, logging, ) from .audio_classification import ffmpeg_read from .base import PIPELINE_INIT_ARGS, Pipeline _lowerCAmelCase : Dict = logging.get_logger(__name__) @add_end_docstrings(lowerCamelCase__ ) class __magic_name__ ( lowerCamelCase__ ): """simple docstring""" def __init__( self :List[Any] , **snake_case :Any ): '''simple docstring''' super().__init__(**snake_case ) if self.framework != "pt": raise ValueError(f"The {self.__class__} is only available in PyTorch." ) # No specific FOR_XXX available yet def __call__( self :Dict , snake_case :Union[np.ndarray, bytes, str] , **snake_case :Tuple ): '''simple docstring''' return super().__call__(snake_case , **snake_case ) def SCREAMING_SNAKE_CASE ( self :Optional[Any] , **snake_case :List[Any] ): '''simple docstring''' A_ : int = {} if "candidate_labels" in kwargs: A_ : Dict = kwargs["candidate_labels"] if "hypothesis_template" in kwargs: A_ : Dict = kwargs["hypothesis_template"] return preprocess_params, {}, {} def SCREAMING_SNAKE_CASE ( self :Tuple , snake_case :Optional[Any] , snake_case :int=None , snake_case :Union[str, Any]="This is a sound of {}." ): '''simple docstring''' if isinstance(snake_case , snake_case ): if audio.startswith("http://" ) or audio.startswith("https://" ): # We need to actually check for a real protocol, otherwise it's impossible to use a local file # like http_huggingface_co.png A_ : Optional[Any] = requests.get(snake_case ).content else: with open(snake_case , "rb" ) as f: A_ : Union[str, Any] = f.read() if isinstance(snake_case , snake_case ): A_ : str = ffmpeg_read(snake_case , self.feature_extractor.sampling_rate ) if not isinstance(snake_case , np.ndarray ): raise ValueError("We expect a numpy ndarray as input" ) if len(audio.shape ) != 1: raise ValueError("We expect a single channel audio input for ZeroShotAudioClassificationPipeline" ) A_ : Any = self.feature_extractor( [audio] , sampling_rate=self.feature_extractor.sampling_rate , return_tensors="pt" ) A_ : Tuple = candidate_labels A_ : List[str] = [hypothesis_template.format(snake_case ) for x in candidate_labels] A_ : List[str] = self.tokenizer(snake_case , return_tensors=self.framework , padding=snake_case ) A_ : List[str] = [text_inputs] return inputs def SCREAMING_SNAKE_CASE ( self :Dict , snake_case :Optional[Any] ): '''simple docstring''' A_ : Dict = model_inputs.pop("candidate_labels" ) A_ : List[Any] = model_inputs.pop("text_inputs" ) if isinstance(text_inputs[0] , snake_case ): A_ : List[Any] = text_inputs[0] else: # Batching case. A_ : List[Any] = text_inputs[0][0] A_ : Any = self.model(**snake_case , **snake_case ) A_ : Any = { "candidate_labels": candidate_labels, "logits": outputs.logits_per_audio, } return model_outputs def SCREAMING_SNAKE_CASE ( self :List[Any] , snake_case :str ): '''simple docstring''' A_ : Any = model_outputs.pop("candidate_labels" ) A_ : Tuple = model_outputs["logits"][0] if self.framework == "pt": A_ : str = logits.softmax(dim=0 ) A_ : Any = probs.tolist() else: raise ValueError("`tf` framework not supported." ) A_ : Optional[Any] = [ {"score": score, "label": candidate_label} for score, candidate_label in sorted(zip(snake_case , snake_case ) , key=lambda snake_case : -x[0] ) ] return result
300
import inspect import unittest from transformers import ViTMSNConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTMSNForImageClassification, ViTMSNModel from transformers.models.vit_msn.modeling_vit_msn import VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class __magic_name__ : """simple docstring""" def __init__( self :Dict , snake_case :Optional[int] , snake_case :Tuple=13 , snake_case :List[Any]=30 , snake_case :Union[str, Any]=2 , snake_case :List[Any]=3 , snake_case :Tuple=True , snake_case :Dict=True , snake_case :Dict=32 , snake_case :List[str]=5 , snake_case :Optional[Any]=4 , snake_case :Any=37 , snake_case :Dict="gelu" , snake_case :List[str]=0.1 , snake_case :str=0.1 , snake_case :Tuple=10 , snake_case :str=0.02 , snake_case :Optional[Any]=None , ): '''simple docstring''' A_ : Tuple = parent A_ : int = batch_size A_ : List[str] = image_size A_ : List[Any] = patch_size A_ : Optional[Any] = num_channels A_ : List[Any] = is_training A_ : Tuple = use_labels A_ : Union[str, Any] = hidden_size A_ : Tuple = num_hidden_layers A_ : Any = num_attention_heads A_ : List[str] = intermediate_size A_ : Optional[int] = hidden_act A_ : List[str] = hidden_dropout_prob A_ : str = attention_probs_dropout_prob A_ : Any = type_sequence_label_size A_ : List[str] = initializer_range A_ : Dict = scope # in ViT MSN, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) A_ : Optional[int] = (image_size // patch_size) ** 2 A_ : List[str] = num_patches + 1 def SCREAMING_SNAKE_CASE ( self :Optional[Any] ): '''simple docstring''' A_ : Any = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) A_ : Tuple = None if self.use_labels: A_ : Dict = ids_tensor([self.batch_size] , self.type_sequence_label_size ) A_ : Dict = self.get_config() return config, pixel_values, labels def SCREAMING_SNAKE_CASE ( self :Union[str, Any] ): '''simple docstring''' return ViTMSNConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , initializer_range=self.initializer_range , ) def SCREAMING_SNAKE_CASE ( self :List[Any] , snake_case :List[Any] , snake_case :str , snake_case :Tuple ): '''simple docstring''' A_ : Optional[Any] = ViTMSNModel(config=snake_case ) model.to(snake_case ) model.eval() A_ : int = model(snake_case ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def SCREAMING_SNAKE_CASE ( self :int , snake_case :Optional[int] , snake_case :List[str] , snake_case :List[str] ): '''simple docstring''' A_ : Dict = self.type_sequence_label_size A_ : Tuple = ViTMSNForImageClassification(snake_case ) model.to(snake_case ) model.eval() A_ : Union[str, Any] = model(snake_case , labels=snake_case ) print("Pixel and labels shape: {pixel_values.shape}, {labels.shape}" ) print("Labels: {labels}" ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images A_ : Union[str, Any] = 1 A_ : int = ViTMSNForImageClassification(snake_case ) model.to(snake_case ) model.eval() A_ : Dict = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) A_ : Optional[Any] = model(snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def SCREAMING_SNAKE_CASE ( self :List[Any] ): '''simple docstring''' A_ : List[str] = self.prepare_config_and_inputs() A_ , A_ , A_ : Optional[int] = config_and_inputs A_ : Dict = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class __magic_name__ ( lowerCamelCase__ , lowerCamelCase__ , unittest.TestCase ): """simple docstring""" __UpperCamelCase = (ViTMSNModel, ViTMSNForImageClassification) if is_torch_available() else () __UpperCamelCase = ( {'''feature-extraction''': ViTMSNModel, '''image-classification''': ViTMSNForImageClassification} if is_torch_available() else {} ) __UpperCamelCase = False __UpperCamelCase = False __UpperCamelCase = False __UpperCamelCase = False def SCREAMING_SNAKE_CASE ( self :Union[str, Any] ): '''simple docstring''' A_ : Tuple = ViTMSNModelTester(self ) A_ : str = ConfigTester(self , config_class=snake_case , has_text_modality=snake_case , hidden_size=37 ) def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' self.config_tester.run_common_tests() @unittest.skip(reason="ViTMSN does not use inputs_embeds" ) def SCREAMING_SNAKE_CASE ( self :Dict ): '''simple docstring''' pass def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' A_ , A_ : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A_ : Optional[int] = model_class(snake_case ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) A_ : Optional[int] = model.get_output_embeddings() self.assertTrue(x is None or isinstance(snake_case , nn.Linear ) ) def SCREAMING_SNAKE_CASE ( self :Optional[Any] ): '''simple docstring''' A_ , A_ : str = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A_ : Optional[Any] = model_class(snake_case ) A_ : Any = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic A_ : List[str] = [*signature.parameters.keys()] A_ : List[Any] = ["pixel_values"] self.assertListEqual(arg_names[:1] , snake_case ) def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' A_ : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case ) def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' A_ : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*snake_case ) @slow def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' for model_name in VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A_ : Optional[Any] = ViTMSNModel.from_pretrained(snake_case ) self.assertIsNotNone(snake_case ) def __snake_case ( ) -> Optional[Any]: A_ : Optional[Any] = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_torch @require_vision class __magic_name__ ( unittest.TestCase ): """simple docstring""" @cached_property def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' return ViTImageProcessor.from_pretrained("facebook/vit-msn-small" ) if is_vision_available() else None @slow def SCREAMING_SNAKE_CASE ( self :List[Any] ): '''simple docstring''' torch.manual_seed(2 ) A_ : Any = ViTMSNForImageClassification.from_pretrained("facebook/vit-msn-small" ).to(snake_case ) A_ : List[str] = self.default_image_processor A_ : int = prepare_img() A_ : List[str] = image_processor(images=snake_case , return_tensors="pt" ).to(snake_case ) # forward pass with torch.no_grad(): A_ : Optional[int] = model(**snake_case ) # verify the logits A_ : List[Any] = torch.Size((1, 1_000) ) self.assertEqual(outputs.logits.shape , snake_case ) A_ : int = torch.tensor([-0.0803, -0.4454, -0.2375] ).to(snake_case ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , snake_case , atol=1e-4 ) )
300
1
from .testing import ( are_the_same_tensors, execute_subprocess_async, require_bnb, require_cpu, require_cuda, require_huggingface_suite, require_mps, require_multi_gpu, require_multi_xpu, require_safetensors, require_single_gpu, require_single_xpu, require_torch_min_version, require_tpu, require_xpu, skip, slow, ) from .training import RegressionDataset, RegressionModel, RegressionModelaXPU from .scripts import test_script, test_sync, test_ops # isort: skip
300
import torch from diffusers import DDPMScheduler from .test_schedulers import SchedulerCommonTest class __magic_name__ ( lowerCamelCase__ ): """simple docstring""" __UpperCamelCase = (DDPMScheduler,) def SCREAMING_SNAKE_CASE ( self :Union[str, Any] , **snake_case :str ): '''simple docstring''' A_ : Dict = { "num_train_timesteps": 1_000, "beta_start": 0.0001, "beta_end": 0.02, "beta_schedule": "linear", "variance_type": "fixed_small", "clip_sample": True, } config.update(**snake_case ) return config def SCREAMING_SNAKE_CASE ( self :int ): '''simple docstring''' for timesteps in [1, 5, 100, 1_000]: self.check_over_configs(num_train_timesteps=snake_case ) def SCREAMING_SNAKE_CASE ( self :Optional[Any] ): '''simple docstring''' for beta_start, beta_end in zip([0.0001, 0.001, 0.01, 0.1] , [0.002, 0.02, 0.2, 2] ): self.check_over_configs(beta_start=snake_case , beta_end=snake_case ) def SCREAMING_SNAKE_CASE ( self :int ): '''simple docstring''' for schedule in ["linear", "squaredcos_cap_v2"]: self.check_over_configs(beta_schedule=snake_case ) def SCREAMING_SNAKE_CASE ( self :List[Any] ): '''simple docstring''' for variance in ["fixed_small", "fixed_large", "other"]: self.check_over_configs(variance_type=snake_case ) def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' for clip_sample in [True, False]: self.check_over_configs(clip_sample=snake_case ) def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' self.check_over_configs(thresholding=snake_case ) for threshold in [0.5, 1.0, 2.0]: for prediction_type in ["epsilon", "sample", "v_prediction"]: self.check_over_configs( thresholding=snake_case , prediction_type=snake_case , sample_max_value=snake_case , ) def SCREAMING_SNAKE_CASE ( self :Optional[int] ): '''simple docstring''' for prediction_type in ["epsilon", "sample", "v_prediction"]: self.check_over_configs(prediction_type=snake_case ) def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' for t in [0, 500, 999]: self.check_over_forward(time_step=snake_case ) def SCREAMING_SNAKE_CASE ( self :Optional[Any] ): '''simple docstring''' A_ : Tuple = self.scheduler_classes[0] A_ : List[str] = self.get_scheduler_config() A_ : List[str] = scheduler_class(**snake_case ) assert torch.sum(torch.abs(scheduler._get_variance(0 ) - 0.0 ) ) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(487 ) - 0.00979 ) ) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(999 ) - 0.02 ) ) < 1e-5 def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' A_ : int = self.scheduler_classes[0] A_ : List[str] = self.get_scheduler_config() A_ : int = scheduler_class(**snake_case ) A_ : Tuple = len(snake_case ) A_ : List[str] = self.dummy_model() A_ : Optional[Any] = self.dummy_sample_deter A_ : List[str] = torch.manual_seed(0 ) for t in reversed(range(snake_case ) ): # 1. predict noise residual A_ : Tuple = model(snake_case , snake_case ) # 2. predict previous mean of sample x_t-1 A_ : Dict = scheduler.step(snake_case , snake_case , snake_case , generator=snake_case ).prev_sample # if t > 0: # noise = self.dummy_sample_deter # variance = scheduler.get_variance(t) ** (0.5) * noise # # sample = pred_prev_sample + variance A_ : Optional[int] = pred_prev_sample A_ : Tuple = torch.sum(torch.abs(snake_case ) ) A_ : str = torch.mean(torch.abs(snake_case ) ) assert abs(result_sum.item() - 258.9606 ) < 1e-2 assert abs(result_mean.item() - 0.3372 ) < 1e-3 def SCREAMING_SNAKE_CASE ( self :Dict ): '''simple docstring''' A_ : Optional[int] = self.scheduler_classes[0] A_ : int = self.get_scheduler_config(prediction_type="v_prediction" ) A_ : List[str] = scheduler_class(**snake_case ) A_ : int = len(snake_case ) A_ : Dict = self.dummy_model() A_ : str = self.dummy_sample_deter A_ : Any = torch.manual_seed(0 ) for t in reversed(range(snake_case ) ): # 1. predict noise residual A_ : Optional[int] = model(snake_case , snake_case ) # 2. predict previous mean of sample x_t-1 A_ : Tuple = scheduler.step(snake_case , snake_case , snake_case , generator=snake_case ).prev_sample # if t > 0: # noise = self.dummy_sample_deter # variance = scheduler.get_variance(t) ** (0.5) * noise # # sample = pred_prev_sample + variance A_ : List[str] = pred_prev_sample A_ : Optional[Any] = torch.sum(torch.abs(snake_case ) ) A_ : List[str] = torch.mean(torch.abs(snake_case ) ) assert abs(result_sum.item() - 202.0296 ) < 1e-2 assert abs(result_mean.item() - 0.2631 ) < 1e-3 def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' A_ : str = self.scheduler_classes[0] A_ : Optional[Any] = self.get_scheduler_config() A_ : Dict = scheduler_class(**snake_case ) A_ : Optional[int] = [100, 87, 50, 1, 0] scheduler.set_timesteps(timesteps=snake_case ) A_ : Optional[int] = scheduler.timesteps for i, timestep in enumerate(snake_case ): if i == len(snake_case ) - 1: A_ : str = -1 else: A_ : List[str] = timesteps[i + 1] A_ : Optional[int] = scheduler.previous_timestep(snake_case ) A_ : List[str] = prev_t.item() self.assertEqual(snake_case , snake_case ) def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' A_ : Optional[Any] = self.scheduler_classes[0] A_ : int = self.get_scheduler_config() A_ : Tuple = scheduler_class(**snake_case ) A_ : List[str] = [100, 87, 50, 51, 0] with self.assertRaises(snake_case , msg="`custom_timesteps` must be in descending order." ): scheduler.set_timesteps(timesteps=snake_case ) def SCREAMING_SNAKE_CASE ( self :List[Any] ): '''simple docstring''' A_ : Any = self.scheduler_classes[0] A_ : Union[str, Any] = self.get_scheduler_config() A_ : Optional[int] = scheduler_class(**snake_case ) A_ : Union[str, Any] = [100, 87, 50, 1, 0] A_ : Optional[int] = len(snake_case ) with self.assertRaises(snake_case , msg="Can only pass one of `num_inference_steps` or `custom_timesteps`." ): scheduler.set_timesteps(num_inference_steps=snake_case , timesteps=snake_case ) def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' A_ : Union[str, Any] = self.scheduler_classes[0] A_ : Optional[Any] = self.get_scheduler_config() A_ : Optional[int] = scheduler_class(**snake_case ) A_ : Optional[int] = [scheduler.config.num_train_timesteps] with self.assertRaises( snake_case , msg="`timesteps` must start before `self.config.train_timesteps`: {scheduler.config.num_train_timesteps}}" , ): scheduler.set_timesteps(timesteps=snake_case )
300
1
import warnings from ...utils import logging from .image_processing_videomae import VideoMAEImageProcessor _lowerCAmelCase : str = logging.get_logger(__name__) class __magic_name__ ( lowerCamelCase__ ): """simple docstring""" def __init__( self :Optional[int] , *snake_case :Dict , **snake_case :Tuple ): '''simple docstring''' warnings.warn( "The class VideoMAEFeatureExtractor is deprecated and will be removed in version 5 of Transformers." " Please use VideoMAEImageProcessor instead." , snake_case , ) super().__init__(*snake_case , **snake_case )
300
import argparse import json import os import fairseq import torch from torch import nn from transformers import ( SpeechaTextaConfig, SpeechaTextaForCausalLM, SpeechaTextaTokenizer, SpeechEncoderDecoderConfig, SpeechEncoderDecoderModel, WavaVecaConfig, WavaVecaFeatureExtractor, WavaVecaModel, logging, ) logging.set_verbosity_info() _lowerCAmelCase : List[str] = logging.get_logger(__name__) _lowerCAmelCase : Optional[int] = { '''post_extract_proj''': '''feature_projection.projection''', '''encoder.pos_conv.0''': '''encoder.pos_conv_embed.conv''', '''self_attn.k_proj''': '''encoder.layers.*.attention.k_proj''', '''self_attn.v_proj''': '''encoder.layers.*.attention.v_proj''', '''self_attn.q_proj''': '''encoder.layers.*.attention.q_proj''', '''self_attn.out_proj''': '''encoder.layers.*.attention.out_proj''', '''self_attn_layer_norm''': '''encoder.layers.*.layer_norm''', '''fc1''': '''encoder.layers.*.feed_forward.intermediate_dense''', '''fc2''': '''encoder.layers.*.feed_forward.output_dense''', '''final_layer_norm''': '''encoder.layers.*.final_layer_norm''', '''encoder.layer_norm''': '''encoder.layer_norm''', '''w2v_model.layer_norm''': '''feature_projection.layer_norm''', '''quantizer.weight_proj''': '''quantizer.weight_proj''', '''quantizer.vars''': '''quantizer.codevectors''', '''project_q''': '''project_q''', '''final_proj''': '''project_hid''', '''w2v_encoder.proj''': '''lm_head''', '''mask_emb''': '''masked_spec_embed''', } _lowerCAmelCase : int = [ '''lm_head''', '''quantizer.weight_proj''', '''quantizer.codevectors''', '''project_q''', '''project_hid''', ] def __snake_case ( _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : List[Any] , _lowerCAmelCase : Any , _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : int ) -> List[Any]: for attribute in key.split("." ): A_ : List[Any] = getattr(_lowerCAmelCase , _lowerCAmelCase ) if weight_type is not None: A_ : List[Any] = getattr(_lowerCAmelCase , _lowerCAmelCase ).shape else: A_ : Tuple = hf_pointer.shape assert hf_shape == value.shape, ( f"Shape of hf {key + '.' + weight_type if weight_type is not None else ''} is {hf_shape}, but should be" f" {value.shape} for {full_name}" ) if weight_type == "weight": A_ : Optional[int] = value elif weight_type == "weight_g": A_ : Optional[int] = value elif weight_type == "weight_v": A_ : Any = value elif weight_type == "bias": A_ : str = value else: A_ : Any = value logger.info(f"{key + '.' + weight_type if weight_type is not None else ''} was initialized from {full_name}." ) def __snake_case ( _lowerCAmelCase : Optional[Any] , _lowerCAmelCase : Dict ) -> List[str]: A_ : Optional[Any] = [] A_ : Any = fairseq_model.state_dict() A_ : Union[str, Any] = hf_model.feature_extractor # if encoder has different dim to decoder -> use proj_weight A_ : str = None for name, value in fairseq_dict.items(): A_ : Tuple = False if "conv_layers" in name: load_conv_layer( _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , hf_model.config.feat_extract_norm == "group" , ) A_ : Optional[Any] = True elif name.split("." )[0] == "proj": A_ : Dict = fairseq_model.proj A_ : List[Any] = True else: for key, mapped_key in MAPPING.items(): if key in name or key.split("w2v_model." )[-1] == name.split("." )[0]: A_ : int = True if "*" in mapped_key: A_ : Optional[Any] = name.split(_lowerCAmelCase )[0].split("." )[-2] A_ : int = mapped_key.replace("*" , _lowerCAmelCase ) if "weight_g" in name: A_ : List[Any] = "weight_g" elif "weight_v" in name: A_ : List[Any] = "weight_v" elif "bias" in name: A_ : Dict = "bias" elif "weight" in name: A_ : List[Any] = "weight" else: A_ : Dict = None set_recursively(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) continue if not is_used: unused_weights.append(_lowerCAmelCase ) logger.warning(f"Unused weights: {unused_weights}" ) return proj_weight def __snake_case ( _lowerCAmelCase : Optional[Any] , _lowerCAmelCase : Tuple , _lowerCAmelCase : List[Any] , _lowerCAmelCase : List[Any] , _lowerCAmelCase : Optional[int] ) -> str: A_ : Any = full_name.split("conv_layers." )[-1] A_ : Optional[int] = name.split("." ) A_ : Optional[Any] = int(items[0] ) A_ : Union[str, Any] = int(items[1] ) if type_id == 0: if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, ( f"{full_name} has size {value.shape}, but" f" {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found." ) A_ : List[Any] = value logger.info(f"Feat extract conv layer {layer_id} was initialized from {full_name}." ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape, ( f"{full_name} has size {value.shape}, but" f" {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found." ) A_ : int = value logger.info(f"Feat extract conv layer {layer_id} was initialized from {full_name}." ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, ( f"{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was" " found." ) A_ : List[Any] = value logger.info(f"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}." ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, ( f"{full_name} has size {value.shape}, but" f" {feature_extractor[layer_id].layer_norm.weight.data.shape} was found." ) A_ : Tuple = value logger.info(f"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}." ) else: unused_weights.append(_lowerCAmelCase ) def __snake_case ( _lowerCAmelCase : Optional[int] ) -> str: A_ , A_ : List[str] = emb.weight.shape A_ : Optional[int] = nn.Linear(_lowerCAmelCase , _lowerCAmelCase , bias=_lowerCAmelCase ) A_ : List[Any] = emb.weight.data return lin_layer def __snake_case ( _lowerCAmelCase : str ) -> Tuple: with open(_lowerCAmelCase , "r" , encoding="utf-8" ) as f: A_ : int = f.readlines() A_ : Dict = [line.split(" " )[0] for line in lines] A_ : Tuple = len(_lowerCAmelCase ) A_ : Union[str, Any] = { "<s>": 0, "<pad>": 1, "</s>": 2, "<unk>": 3, } vocab_dict.update(dict(zip(_lowerCAmelCase , range(4 , num_words + 4 ) ) ) ) return vocab_dict @torch.no_grad() def __snake_case ( _lowerCAmelCase : int , _lowerCAmelCase : Tuple , _lowerCAmelCase : Dict , _lowerCAmelCase : Any , _lowerCAmelCase : Tuple , _lowerCAmelCase : List[str] , _lowerCAmelCase : Dict , ) -> Tuple: A_ : Optional[int] = WavaVecaConfig.from_pretrained(_lowerCAmelCase ) A_ : str = SpeechaTextaConfig.from_pretrained( _lowerCAmelCase , vocab_size=_lowerCAmelCase , decoder_layers=_lowerCAmelCase , do_stable_layer_norm=_lowerCAmelCase ) A_ : int = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=16000 , padding_value=0 , do_normalize=_lowerCAmelCase , return_attention_mask=_lowerCAmelCase , ) A_ , A_ , A_ : List[Any] = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={"data": "/".join(dict_path.split("/" )[:-1] )} ) A_ : Union[str, Any] = model[0].eval() # set weights for wav2vec2 encoder A_ : Tuple = WavaVecaModel(_lowerCAmelCase ) A_ : str = recursively_load_weights_wavaveca(model.encoder , _lowerCAmelCase ) A_ : Tuple = SpeechaTextaForCausalLM(_lowerCAmelCase ) A_ , A_ : List[str] = hf_decoder.model.decoder.load_state_dict(model.decoder.state_dict() , strict=_lowerCAmelCase ) # set output linear layer unexpected_keys.remove("embed_out" ) A_ : Union[str, Any] = nn.Parameter(model.decoder.embed_out.detach() ) # layer norm is init to identity matrix so leaving it is fine logger.warning(f"The following keys are missing when loading the decoder weights: {missing_keys}" ) logger.warning(f"The following keys are unexpected when loading the decoder weights: {unexpected_keys}" ) A_ : str = SpeechEncoderDecoderModel(encoder=_lowerCAmelCase , decoder=_lowerCAmelCase ) A_ : Optional[Any] = False # add projection layer A_ : Optional[Any] = nn.Parameter(projection_layer.weight ) A_ : int = nn.Parameter(projection_layer.bias ) A_ : str = create_vocab_dict(_lowerCAmelCase ) with open(os.path.join(_lowerCAmelCase , "vocab.json" ) , "w" ) as fp: json.dump(_lowerCAmelCase , _lowerCAmelCase ) A_ : Any = SpeechaTextaTokenizer(os.path.join(_lowerCAmelCase , "vocab.json" ) ) tokenizer.save_pretrained(_lowerCAmelCase ) A_ : Optional[int] = hf_wavavec.config.to_dict() A_ : int = tokenizer.pad_token_id A_ : List[str] = tokenizer.bos_token_id A_ : List[str] = tokenizer.eos_token_id A_ : List[str] = "speech_to_text_2" A_ : Tuple = "wav2vec2" A_ : str = SpeechEncoderDecoderConfig.from_dict(_lowerCAmelCase ) hf_wavavec.save_pretrained(_lowerCAmelCase ) feature_extractor.save_pretrained(_lowerCAmelCase ) if __name__ == "__main__": _lowerCAmelCase : Optional[Any] = argparse.ArgumentParser() parser.add_argument('''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''') parser.add_argument('''--checkpoint_path''', default=None, type=str, help='''Path to fairseq checkpoint''') parser.add_argument('''--dict_path''', default=None, type=str, help='''Path to dict of fine-tuned model''') parser.add_argument( '''--encoder_config_path''', default='''facebook/wav2vec2-large-lv60''', type=str, help='''Path to hf encoder wav2vec2 checkpoint config''', ) parser.add_argument( '''--decoder_config_path''', default='''facebook/s2t-small-mustc-en-fr-st''', type=str, help='''Path to hf decoder s2t checkpoint config''', ) parser.add_argument('''--vocab_size''', default=10_224, type=int, help='''Vocab size of decoder''') parser.add_argument('''--num_decoder_layers''', default=7, type=int, help='''Number of decoder layers''') _lowerCAmelCase : Union[str, Any] = parser.parse_args() convert_wavaveca_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.dict_path, encoder_config_path=args.encoder_config_path, decoder_config_path=args.decoder_config_path, vocab_size=args.vocab_size, num_decoder_layers=args.num_decoder_layers, )
300
1
import pyarrow.parquet as pq import pytest from datasets import Audio, Dataset, DatasetDict, Features, NamedSplit, Sequence, Value, config from datasets.features.image import Image from datasets.io.parquet import ParquetDatasetReader, ParquetDatasetWriter, get_writer_batch_size from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases def __snake_case ( _lowerCAmelCase : List[Any] , _lowerCAmelCase : Optional[int] ) -> str: assert isinstance(_lowerCAmelCase , _lowerCAmelCase ) assert dataset.num_rows == 4 assert dataset.num_columns == 3 assert dataset.column_names == ["col_1", "col_2", "col_3"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize("keep_in_memory" , [False, True] ) def __snake_case ( _lowerCAmelCase : Dict , _lowerCAmelCase : Optional[int] , _lowerCAmelCase : List[Any] ) -> Optional[int]: A_ : Tuple = tmp_path / "cache" A_ : Optional[int] = {"col_1": "string", "col_2": "int64", "col_3": "float64"} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): A_ : Optional[Any] = ParquetDatasetReader(_lowerCAmelCase , cache_dir=_lowerCAmelCase , keep_in_memory=_lowerCAmelCase ).read() _check_parquet_dataset(_lowerCAmelCase , _lowerCAmelCase ) @pytest.mark.parametrize( "features" , [ None, {"col_1": "string", "col_2": "int64", "col_3": "float64"}, {"col_1": "string", "col_2": "string", "col_3": "string"}, {"col_1": "int32", "col_2": "int32", "col_3": "int32"}, {"col_1": "float32", "col_2": "float32", "col_3": "float32"}, ] , ) def __snake_case ( _lowerCAmelCase : List[Any] , _lowerCAmelCase : Any , _lowerCAmelCase : List[Any] ) -> str: A_ : List[Any] = tmp_path / "cache" A_ : List[str] = {"col_1": "string", "col_2": "int64", "col_3": "float64"} A_ : int = features.copy() if features else default_expected_features A_ : str = ( Features({feature: Value(_lowerCAmelCase ) for feature, dtype in features.items()} ) if features is not None else None ) A_ : Union[str, Any] = ParquetDatasetReader(_lowerCAmelCase , features=_lowerCAmelCase , cache_dir=_lowerCAmelCase ).read() _check_parquet_dataset(_lowerCAmelCase , _lowerCAmelCase ) @pytest.mark.parametrize("split" , [None, NamedSplit("train" ), "train", "test"] ) def __snake_case ( _lowerCAmelCase : Any , _lowerCAmelCase : Any , _lowerCAmelCase : Any ) -> Optional[Any]: A_ : Dict = tmp_path / "cache" A_ : int = {"col_1": "string", "col_2": "int64", "col_3": "float64"} A_ : Optional[int] = ParquetDatasetReader(_lowerCAmelCase , cache_dir=_lowerCAmelCase , split=_lowerCAmelCase ).read() _check_parquet_dataset(_lowerCAmelCase , _lowerCAmelCase ) assert dataset.split == split if split else "train" @pytest.mark.parametrize("path_type" , [str, list] ) def __snake_case ( _lowerCAmelCase : List[str] , _lowerCAmelCase : int , _lowerCAmelCase : Optional[Any] ) -> List[str]: if issubclass(_lowerCAmelCase , _lowerCAmelCase ): A_ : int = parquet_path elif issubclass(_lowerCAmelCase , _lowerCAmelCase ): A_ : Optional[int] = [parquet_path] A_ : Optional[int] = tmp_path / "cache" A_ : Union[str, Any] = {"col_1": "string", "col_2": "int64", "col_3": "float64"} A_ : Optional[int] = ParquetDatasetReader(_lowerCAmelCase , cache_dir=_lowerCAmelCase ).read() _check_parquet_dataset(_lowerCAmelCase , _lowerCAmelCase ) def __snake_case ( _lowerCAmelCase : Any , _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : Union[str, Any]=("train",) ) -> Tuple: assert isinstance(_lowerCAmelCase , _lowerCAmelCase ) for split in splits: A_ : List[str] = dataset_dict[split] assert dataset.num_rows == 4 assert dataset.num_columns == 3 assert dataset.column_names == ["col_1", "col_2", "col_3"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize("keep_in_memory" , [False, True] ) def __snake_case ( _lowerCAmelCase : Optional[Any] , _lowerCAmelCase : List[Any] , _lowerCAmelCase : Dict ) -> Optional[int]: A_ : Optional[Any] = tmp_path / "cache" A_ : Union[str, Any] = {"col_1": "string", "col_2": "int64", "col_3": "float64"} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): A_ : Union[str, Any] = ParquetDatasetReader( {"train": parquet_path} , cache_dir=_lowerCAmelCase , keep_in_memory=_lowerCAmelCase ).read() _check_parquet_datasetdict(_lowerCAmelCase , _lowerCAmelCase ) @pytest.mark.parametrize( "features" , [ None, {"col_1": "string", "col_2": "int64", "col_3": "float64"}, {"col_1": "string", "col_2": "string", "col_3": "string"}, {"col_1": "int32", "col_2": "int32", "col_3": "int32"}, {"col_1": "float32", "col_2": "float32", "col_3": "float32"}, ] , ) def __snake_case ( _lowerCAmelCase : Tuple , _lowerCAmelCase : Any , _lowerCAmelCase : str ) -> Tuple: A_ : Optional[Any] = tmp_path / "cache" A_ : Any = {"col_1": "string", "col_2": "int64", "col_3": "float64"} A_ : List[str] = features.copy() if features else default_expected_features A_ : Tuple = ( Features({feature: Value(_lowerCAmelCase ) for feature, dtype in features.items()} ) if features is not None else None ) A_ : Optional[int] = ParquetDatasetReader({"train": parquet_path} , features=_lowerCAmelCase , cache_dir=_lowerCAmelCase ).read() _check_parquet_datasetdict(_lowerCAmelCase , _lowerCAmelCase ) @pytest.mark.parametrize("split" , [None, NamedSplit("train" ), "train", "test"] ) def __snake_case ( _lowerCAmelCase : str , _lowerCAmelCase : str , _lowerCAmelCase : Any ) -> Union[str, Any]: if split: A_ : Any = {split: parquet_path} else: A_ : Optional[Any] = "train" A_ : str = {"train": parquet_path, "test": parquet_path} A_ : Any = tmp_path / "cache" A_ : str = {"col_1": "string", "col_2": "int64", "col_3": "float64"} A_ : Dict = ParquetDatasetReader(_lowerCAmelCase , cache_dir=_lowerCAmelCase ).read() _check_parquet_datasetdict(_lowerCAmelCase , _lowerCAmelCase , splits=list(path.keys() ) ) assert all(dataset[split].split == split for split in path.keys() ) def __snake_case ( _lowerCAmelCase : Dict , _lowerCAmelCase : Optional[Any] ) -> Dict: A_ : List[str] = ParquetDatasetWriter(_lowerCAmelCase , tmp_path / "foo.parquet" ) assert writer.write() > 0 A_ : Tuple = pq.ParquetFile(tmp_path / "foo.parquet" ) A_ : Dict = pf.read() assert dataset.data.table == output_table def __snake_case ( _lowerCAmelCase : Optional[Any] , _lowerCAmelCase : int ) -> List[Any]: A_ : Tuple = str(shared_datadir / "test_image_rgb.jpg" ) A_ : int = {"image": [image_path]} A_ : Optional[Any] = Features({"image": Image()} ) A_ : Union[str, Any] = Dataset.from_dict(_lowerCAmelCase , features=_lowerCAmelCase ) A_ : Tuple = ParquetDatasetWriter(_lowerCAmelCase , tmp_path / "foo.parquet" ) assert writer.write() > 0 A_ : str = Dataset.from_parquet(str(tmp_path / "foo.parquet" ) ) assert dataset.features == reloaded_dataset.features A_ : int = ParquetDatasetReader(str(tmp_path / "foo.parquet" ) , streaming=_lowerCAmelCase ).read() assert dataset.features == reloaded_iterable_dataset.features @pytest.mark.parametrize( "feature, expected" , [ (Features({"foo": Value("int32" )} ), None), (Features({"image": Image(), "foo": Value("int32" )} ), config.PARQUET_ROW_GROUP_SIZE_FOR_IMAGE_DATASETS), (Features({"nested": Sequence(Audio() )} ), config.PARQUET_ROW_GROUP_SIZE_FOR_AUDIO_DATASETS), ] , ) def __snake_case ( _lowerCAmelCase : str , _lowerCAmelCase : List[Any] ) -> Any: assert get_writer_batch_size(_lowerCAmelCase ) == expected
300
from __future__ import annotations import unittest import numpy as np from transformers import LayoutLMConfig, 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.models.layoutlm.modeling_tf_layoutlm import ( TF_LAYOUTLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFLayoutLMForMaskedLM, TFLayoutLMForQuestionAnswering, TFLayoutLMForSequenceClassification, TFLayoutLMForTokenClassification, TFLayoutLMModel, ) class __magic_name__ : """simple docstring""" def __init__( self :Tuple , snake_case :Optional[Any] , snake_case :Tuple=13 , snake_case :Dict=7 , snake_case :List[Any]=True , snake_case :List[Any]=True , snake_case :Dict=True , snake_case :Any=True , snake_case :Optional[int]=99 , snake_case :Any=32 , snake_case :Dict=2 , snake_case :int=4 , snake_case :Optional[int]=37 , snake_case :List[str]="gelu" , snake_case :List[Any]=0.1 , snake_case :Optional[Any]=0.1 , snake_case :Tuple=512 , snake_case :Tuple=16 , snake_case :Tuple=2 , snake_case :Optional[int]=0.02 , snake_case :str=3 , snake_case :Optional[int]=4 , snake_case :List[str]=None , snake_case :Tuple=1_000 , ): '''simple docstring''' A_ : str = parent A_ : str = batch_size A_ : str = seq_length A_ : Any = is_training A_ : Any = use_input_mask A_ : str = use_token_type_ids A_ : Tuple = use_labels A_ : Optional[Any] = vocab_size A_ : Dict = hidden_size A_ : str = num_hidden_layers A_ : Dict = num_attention_heads A_ : str = intermediate_size A_ : int = hidden_act A_ : List[Any] = hidden_dropout_prob A_ : Dict = attention_probs_dropout_prob A_ : Optional[Any] = max_position_embeddings A_ : List[Any] = type_vocab_size A_ : Any = type_sequence_label_size A_ : Dict = initializer_range A_ : Any = num_labels A_ : Optional[int] = num_choices A_ : Optional[Any] = scope A_ : Any = range_bbox def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' A_ : List[str] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) # convert bbox to numpy since TF does not support item assignment A_ : Tuple = ids_tensor([self.batch_size, self.seq_length, 4] , self.range_bbox ).numpy() # Ensure that bbox is legal for i in range(bbox.shape[0] ): for j in range(bbox.shape[1] ): if bbox[i, j, 3] < bbox[i, j, 1]: A_ : str = bbox[i, j, 3] A_ : Union[str, Any] = bbox[i, j, 1] A_ : List[Any] = t if bbox[i, j, 2] < bbox[i, j, 0]: A_ : Any = bbox[i, j, 2] A_ : Tuple = bbox[i, j, 0] A_ : int = t A_ : int = tf.convert_to_tensor(snake_case ) A_ : Any = None if self.use_input_mask: A_ : Union[str, Any] = random_attention_mask([self.batch_size, self.seq_length] ) A_ : str = None if self.use_token_type_ids: A_ : Union[str, Any] = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) A_ : Dict = None A_ : List[Any] = None A_ : List[str] = None if self.use_labels: A_ : Dict = ids_tensor([self.batch_size] , self.type_sequence_label_size ) A_ : Optional[int] = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) A_ : str = ids_tensor([self.batch_size] , self.num_choices ) A_ : int = LayoutLMConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , ) return config, input_ids, bbox, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def SCREAMING_SNAKE_CASE ( self :str , snake_case :Dict , snake_case :Union[str, Any] , snake_case :int , snake_case :int , snake_case :Union[str, Any] , snake_case :Tuple , snake_case :Optional[int] , snake_case :List[Any] ): '''simple docstring''' A_ : Any = TFLayoutLMModel(config=snake_case ) A_ : Tuple = model(snake_case , snake_case , attention_mask=snake_case , token_type_ids=snake_case ) A_ : str = model(snake_case , snake_case , token_type_ids=snake_case ) A_ : List[Any] = model(snake_case , snake_case ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def SCREAMING_SNAKE_CASE ( self :Optional[int] , snake_case :Any , snake_case :List[Any] , snake_case :List[str] , snake_case :Optional[Any] , snake_case :Dict , snake_case :Any , snake_case :Union[str, Any] , snake_case :List[Any] ): '''simple docstring''' A_ : Optional[int] = TFLayoutLMForMaskedLM(config=snake_case ) A_ : Tuple = model(snake_case , snake_case , attention_mask=snake_case , token_type_ids=snake_case , labels=snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def SCREAMING_SNAKE_CASE ( self :List[str] , snake_case :Dict , snake_case :Tuple , snake_case :Tuple , snake_case :List[str] , snake_case :Tuple , snake_case :str , snake_case :Optional[int] , snake_case :Any ): '''simple docstring''' A_ : Union[str, Any] = self.num_labels A_ : int = TFLayoutLMForSequenceClassification(config=snake_case ) A_ : Optional[int] = model(snake_case , snake_case , attention_mask=snake_case , token_type_ids=snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def SCREAMING_SNAKE_CASE ( self :Optional[Any] , snake_case :Dict , snake_case :str , snake_case :Optional[Any] , snake_case :int , snake_case :Any , snake_case :Tuple , snake_case :List[str] , snake_case :Union[str, Any] ): '''simple docstring''' A_ : List[Any] = self.num_labels A_ : str = TFLayoutLMForTokenClassification(config=snake_case ) A_ : Union[str, Any] = model(snake_case , snake_case , attention_mask=snake_case , token_type_ids=snake_case , labels=snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def SCREAMING_SNAKE_CASE ( self :int , snake_case :List[str] , snake_case :Optional[int] , snake_case :Union[str, Any] , snake_case :List[Any] , snake_case :int , snake_case :Any , snake_case :Union[str, Any] , snake_case :Any ): '''simple docstring''' A_ : Optional[Any] = TFLayoutLMForQuestionAnswering(config=snake_case ) A_ : List[Any] = model(snake_case , snake_case , attention_mask=snake_case , token_type_ids=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 SCREAMING_SNAKE_CASE ( self :Dict ): '''simple docstring''' A_ : int = self.prepare_config_and_inputs() ( ( A_ ) , ( A_ ) , ( A_ ) , ( A_ ) , ( A_ ) , ( A_ ) , ( A_ ) , ( A_ ) , ) : Union[str, Any] = config_and_inputs A_ : Optional[Any] = { "input_ids": input_ids, "bbox": bbox, "token_type_ids": token_type_ids, "attention_mask": input_mask, } return config, inputs_dict @require_tf class __magic_name__ ( lowerCamelCase__ , lowerCamelCase__ , unittest.TestCase ): """simple docstring""" __UpperCamelCase = ( ( TFLayoutLMModel, TFLayoutLMForMaskedLM, TFLayoutLMForTokenClassification, TFLayoutLMForSequenceClassification, TFLayoutLMForQuestionAnswering, ) if is_tf_available() else () ) __UpperCamelCase = ( { '''feature-extraction''': TFLayoutLMModel, '''fill-mask''': TFLayoutLMForMaskedLM, '''text-classification''': TFLayoutLMForSequenceClassification, '''token-classification''': TFLayoutLMForTokenClassification, '''zero-shot''': TFLayoutLMForSequenceClassification, } if is_tf_available() else {} ) __UpperCamelCase = False __UpperCamelCase = True __UpperCamelCase = 10 def SCREAMING_SNAKE_CASE ( self :Dict ): '''simple docstring''' A_ : Tuple = TFLayoutLMModelTester(self ) A_ : List[Any] = ConfigTester(self , config_class=snake_case , hidden_size=37 ) def SCREAMING_SNAKE_CASE ( self :Tuple ): '''simple docstring''' self.config_tester.run_common_tests() def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' A_ : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case ) def SCREAMING_SNAKE_CASE ( self :Optional[int] ): '''simple docstring''' A_ : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*snake_case ) def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' A_ : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*snake_case ) def SCREAMING_SNAKE_CASE ( self :Tuple ): '''simple docstring''' A_ : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*snake_case ) def SCREAMING_SNAKE_CASE ( self :List[Any] ): '''simple docstring''' A_ : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*snake_case ) @slow def SCREAMING_SNAKE_CASE ( self :Optional[Any] ): '''simple docstring''' for model_name in TF_LAYOUTLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A_ : List[str] = TFLayoutLMModel.from_pretrained(snake_case ) self.assertIsNotNone(snake_case ) @unittest.skip("Onnx compliancy broke with TF 2.10" ) def SCREAMING_SNAKE_CASE ( self :Dict ): '''simple docstring''' pass def __snake_case ( ) -> Optional[Any]: # Here we prepare a batch of 2 sequences to test a LayoutLM forward pass on: # fmt: off A_ : int = tf.convert_to_tensor([[101,1019,1014,1016,1037,12849,4747,1004,14246,2278,5439,4524,5002,2930,2193,2930,4341,3208,1005,1055,2171,2848,11300,3531,102],[101,4070,4034,7020,1024,3058,1015,1013,2861,1013,6070,19274,2772,6205,27814,16147,16147,4343,2047,10283,10969,14389,1012,2338,102]] ) # noqa: E231 A_ : int = tf.convert_to_tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],] ) # noqa: E231 A_ : Union[str, Any] = tf.convert_to_tensor([[[0,0,0,0],[423,237,440,251],[427,272,441,287],[419,115,437,129],[961,885,992,912],[256,38,330,58],[256,38,330,58],[336,42,353,57],[360,39,401,56],[360,39,401,56],[411,39,471,59],[479,41,528,59],[533,39,630,60],[67,113,134,131],[141,115,209,132],[68,149,133,166],[141,149,187,164],[195,148,287,165],[195,148,287,165],[195,148,287,165],[295,148,349,165],[441,149,492,166],[497,149,546,164],[64,201,125,218],[1000,1000,1000,1000]],[[0,0,0,0],[662,150,754,166],[665,199,742,211],[519,213,554,228],[519,213,554,228],[134,433,187,454],[130,467,204,480],[130,467,204,480],[130,467,204,480],[130,467,204,480],[130,467,204,480],[314,469,376,482],[504,684,582,706],[941,825,973,900],[941,825,973,900],[941,825,973,900],[941,825,973,900],[610,749,652,765],[130,659,168,672],[176,657,237,672],[238,657,312,672],[443,653,628,672],[443,653,628,672],[716,301,825,317],[1000,1000,1000,1000]]] ) # noqa: E231 A_ : List[Any] = tf.convert_to_tensor([[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]] ) # noqa: E231 # these are sequence labels (i.e. at the token level) A_ : Tuple = tf.convert_to_tensor([[-100,10,10,10,9,1,-100,7,7,-100,7,7,4,2,5,2,8,8,-100,-100,5,0,3,2,-100],[-100,12,12,12,-100,12,10,-100,-100,-100,-100,10,12,9,-100,-100,-100,10,10,10,9,12,-100,10,-100]] ) # noqa: E231 # fmt: on return input_ids, attention_mask, bbox, token_type_ids, labels @require_tf class __magic_name__ ( unittest.TestCase ): """simple docstring""" @slow def SCREAMING_SNAKE_CASE ( self :Tuple ): '''simple docstring''' A_ : str = TFLayoutLMModel.from_pretrained("microsoft/layoutlm-base-uncased" ) A_ , A_ , A_ , A_ , A_ : Tuple = prepare_layoutlm_batch_inputs() # forward pass A_ : Tuple = model(input_ids=snake_case , bbox=snake_case , attention_mask=snake_case , token_type_ids=snake_case ) # test the sequence output on [0, :3, :3] A_ : List[Any] = tf.convert_to_tensor( [[0.1785, -0.1947, -0.0425], [-0.3254, -0.2807, 0.2553], [-0.5391, -0.3322, 0.3364]] , ) self.assertTrue(np.allclose(outputs.last_hidden_state[0, :3, :3] , snake_case , atol=1e-3 ) ) # test the pooled output on [1, :3] A_ : Optional[Any] = tf.convert_to_tensor([-0.6580, -0.0214, 0.8552] ) self.assertTrue(np.allclose(outputs.pooler_output[1, :3] , snake_case , atol=1e-3 ) ) @slow def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' A_ : Union[str, Any] = TFLayoutLMForSequenceClassification.from_pretrained("microsoft/layoutlm-base-uncased" , num_labels=2 ) A_ , A_ , A_ , A_ , A_ : Any = prepare_layoutlm_batch_inputs() # forward pass A_ : Dict = model( input_ids=snake_case , bbox=snake_case , attention_mask=snake_case , token_type_ids=snake_case , labels=tf.convert_to_tensor([1, 1] ) , ) # test whether we get a loss as a scalar A_ : List[str] = outputs.loss A_ : Union[str, Any] = (2,) self.assertEqual(loss.shape , snake_case ) # test the shape of the logits A_ : Tuple = outputs.logits A_ : Tuple = (2, 2) self.assertEqual(logits.shape , snake_case ) @slow def SCREAMING_SNAKE_CASE ( self :Optional[int] ): '''simple docstring''' A_ : int = TFLayoutLMForTokenClassification.from_pretrained("microsoft/layoutlm-base-uncased" , num_labels=13 ) A_ , A_ , A_ , A_ , A_ : Optional[int] = prepare_layoutlm_batch_inputs() # forward pass A_ : Union[str, Any] = model( input_ids=snake_case , bbox=snake_case , attention_mask=snake_case , token_type_ids=snake_case , labels=snake_case ) # test the shape of the logits A_ : Dict = outputs.logits A_ : List[Any] = tf.convert_to_tensor((2, 25, 13) ) self.assertEqual(logits.shape , snake_case ) @slow def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' A_ : Optional[Any] = TFLayoutLMForQuestionAnswering.from_pretrained("microsoft/layoutlm-base-uncased" ) A_ , A_ , A_ , A_ , A_ : str = prepare_layoutlm_batch_inputs() # forward pass A_ : Union[str, Any] = model(input_ids=snake_case , bbox=snake_case , attention_mask=snake_case , token_type_ids=snake_case ) # test the shape of the logits A_ : Union[str, Any] = tf.convert_to_tensor((2, 25) ) self.assertEqual(outputs.start_logits.shape , snake_case ) self.assertEqual(outputs.end_logits.shape , snake_case )
300
1
import os import tempfile import unittest from transformers.models.marian.convert_marian_tatoeba_to_pytorch import DEFAULT_REPO, TatoebaConverter from transformers.testing_utils import slow from transformers.utils import cached_property @unittest.skipUnless(os.path.exists(lowerCamelCase__ ) , '''Tatoeba directory does not exist.''' ) class __magic_name__ ( unittest.TestCase ): """simple docstring""" @cached_property def SCREAMING_SNAKE_CASE ( self :Dict ): '''simple docstring''' A_ : List[Any] = tempfile.mkdtemp() return TatoebaConverter(save_dir=snake_case ) @slow def SCREAMING_SNAKE_CASE ( self :Optional[int] ): '''simple docstring''' self.resolver.convert_models(["heb-eng"] ) @slow def SCREAMING_SNAKE_CASE ( self :Optional[int] ): '''simple docstring''' A_ , A_ : str = self.resolver.write_model_card("opus-mt-he-en" , dry_run=snake_case ) assert mmeta["long_pair"] == "heb-eng"
300
# Copyright 2023 The HuggingFace Inc. 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. import re from ..utils import cached_file # docstyle-ignore _lowerCAmelCase : Optional[int] = ''' Human: <<task>> Assistant: ''' _lowerCAmelCase : int = '''huggingface-tools/default-prompts''' _lowerCAmelCase : Any = {'''chat''': '''chat_prompt_template.txt''', '''run''': '''run_prompt_template.txt'''} def __snake_case ( _lowerCAmelCase : str , _lowerCAmelCase : List[Any] , _lowerCAmelCase : Dict="run" ) -> List[Any]: if prompt_or_repo_id is None: A_ : Optional[int] = DEFAULT_PROMPTS_REPO # prompt is considered a repo ID when it does not contain any kind of space if re.search("\\s" , _lowerCAmelCase ) is not None: return prompt_or_repo_id A_ : Optional[Any] = cached_file( _lowerCAmelCase , PROMPT_FILES[mode] , repo_type="dataset" , user_agent={"agent": agent_name} ) with open(_lowerCAmelCase , "r" , encoding="utf-8" ) as f: return f.read()
300
1
import copy from ...configuration_utils import PretrainedConfig from ...utils import add_start_docstrings _lowerCAmelCase : Union[str, Any] = r''' [`RagConfig`] stores the configuration of a *RagModel*. Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the documentation from [`PretrainedConfig`] for more information. Args: title_sep (`str`, *optional*, defaults to `" / "`): Separator inserted between the title and the text of the retrieved document when calling [`RagRetriever`]. doc_sep (`str`, *optional*, defaults to `" // "`): Separator inserted between the text of the retrieved document and the original input when calling [`RagRetriever`]. n_docs (`int`, *optional*, defaults to 5): Number of documents to retrieve. max_combined_length (`int`, *optional*, defaults to 300): Max length of contextualized input returned by [`~RagRetriever.__call__`]. retrieval_vector_size (`int`, *optional*, defaults to 768): Dimensionality of the document embeddings indexed by [`RagRetriever`]. retrieval_batch_size (`int`, *optional*, defaults to 8): Retrieval batch size, defined as the number of queries issues concurrently to the faiss index encapsulated [`RagRetriever`]. dataset (`str`, *optional*, defaults to `"wiki_dpr"`): A dataset identifier of the indexed dataset in HuggingFace Datasets (list all available datasets and ids using `datasets.list_datasets()`). dataset_split (`str`, *optional*, defaults to `"train"`) Which split of the `dataset` to load. index_name (`str`, *optional*, defaults to `"compressed"`) The index name of the index associated with the `dataset`. One can choose between `"legacy"`, `"exact"` and `"compressed"`. index_path (`str`, *optional*) The path to the serialized faiss index on disk. passages_path (`str`, *optional*): A path to text passages compatible with the faiss index. Required if using [`~models.rag.retrieval_rag.LegacyIndex`] use_dummy_dataset (`bool`, *optional*, defaults to `False`) Whether to load a "dummy" variant of the dataset specified by `dataset`. label_smoothing (`float`, *optional*, defaults to 0.0): Only relevant if `return_loss` is set to `True`. Controls the `epsilon` parameter value for label smoothing in the loss calculation. If set to 0, no label smoothing is performed. do_marginalize (`bool`, *optional*, defaults to `False`): If `True`, the logits are marginalized over all documents by making use of `torch.nn.functional.log_softmax`. reduce_loss (`bool`, *optional*, defaults to `False`): Whether or not to reduce the NLL loss using the `torch.Tensor.sum` operation. do_deduplication (`bool`, *optional*, defaults to `True`): Whether or not to deduplicate the generations from different context documents for a given input. Has to be set to `False` if used while training with distributed backend. exclude_bos_score (`bool`, *optional*, defaults to `False`): Whether or not to disregard the BOS token when computing the loss. output_retrieved(`bool`, *optional*, defaults to `False`): If set to `True`, `retrieved_doc_embeds`, `retrieved_doc_ids`, `context_input_ids` and `context_attention_mask` are returned. See returned tensors for more detail. use_cache (`bool`, *optional*, defaults to `True`): Whether or not the model should return the last key/values attentions (not used by all models). forced_eos_token_id (`int`, *optional*): The id of the token to force as the last generated token when `max_length` is reached. Usually set to `eos_token_id`. ''' @add_start_docstrings(lowerCamelCase__ ) class __magic_name__ ( lowerCamelCase__ ): """simple docstring""" __UpperCamelCase = '''rag''' __UpperCamelCase = True def __init__( self :Any , snake_case :str=None , snake_case :int=True , snake_case :List[Any]=None , snake_case :Dict=None , snake_case :Any=None , snake_case :Optional[Any]=None , snake_case :str=None , snake_case :List[str]=" / " , snake_case :Any=" // " , snake_case :Union[str, Any]=5 , snake_case :Union[str, Any]=300 , snake_case :List[Any]=768 , snake_case :Optional[Any]=8 , snake_case :int="wiki_dpr" , snake_case :List[str]="train" , snake_case :Union[str, Any]="compressed" , snake_case :List[str]=None , snake_case :Union[str, Any]=None , snake_case :Union[str, Any]=False , snake_case :Optional[int]=False , snake_case :List[str]=0.0 , snake_case :List[str]=True , snake_case :str=False , snake_case :Optional[int]=False , snake_case :str=False , snake_case :List[str]=True , snake_case :List[str]=None , **snake_case :int , ): '''simple docstring''' super().__init__( bos_token_id=snake_case , pad_token_id=snake_case , eos_token_id=snake_case , decoder_start_token_id=snake_case , forced_eos_token_id=snake_case , is_encoder_decoder=snake_case , prefix=snake_case , vocab_size=snake_case , **snake_case , ) assert ( "question_encoder" in kwargs and "generator" in kwargs ), "Config has to be initialized with question_encoder and generator config" A_ : Union[str, Any] = kwargs.pop("question_encoder" ) A_ : Optional[int] = question_encoder_config.pop("model_type" ) A_ : Tuple = kwargs.pop("generator" ) A_ : Dict = decoder_config.pop("model_type" ) from ..auto.configuration_auto import AutoConfig A_ : Tuple = AutoConfig.for_model(snake_case , **snake_case ) A_ : Dict = AutoConfig.for_model(snake_case , **snake_case ) A_ : List[Any] = reduce_loss A_ : List[str] = label_smoothing A_ : List[Any] = exclude_bos_score A_ : Tuple = do_marginalize A_ : str = title_sep A_ : Tuple = doc_sep A_ : str = n_docs A_ : int = max_combined_length A_ : List[str] = dataset A_ : Any = dataset_split A_ : List[str] = index_name A_ : Any = retrieval_vector_size A_ : Union[str, Any] = retrieval_batch_size A_ : Dict = passages_path A_ : Optional[int] = index_path A_ : Union[str, Any] = use_dummy_dataset A_ : Union[str, Any] = output_retrieved A_ : Optional[Any] = do_deduplication A_ : List[Any] = use_cache if self.forced_eos_token_id is None: A_ : Any = getattr(self.generator , "forced_eos_token_id" , snake_case ) @classmethod def SCREAMING_SNAKE_CASE ( cls :Optional[int] , snake_case :PretrainedConfig , snake_case :PretrainedConfig , **snake_case :Union[str, Any] ): '''simple docstring''' return cls(question_encoder=question_encoder_config.to_dict() , generator=generator_config.to_dict() , **snake_case ) def SCREAMING_SNAKE_CASE ( self :Optional[Any] ): '''simple docstring''' A_ : int = copy.deepcopy(self.__dict__ ) A_ : int = self.question_encoder.to_dict() A_ : Optional[Any] = self.generator.to_dict() A_ : str = self.__class__.model_type return output
300
def __snake_case ( _lowerCAmelCase : list ) -> list: if len(_lowerCAmelCase ) <= 1: return [tuple(_lowerCAmelCase )] A_ : Tuple = [] def generate(_lowerCAmelCase : int , _lowerCAmelCase : list ): A_ : List[str] = [0] * n res.append(tuple(_lowerCAmelCase ) ) A_ : int = 0 while i < n: if c[i] < i: if i % 2 == 0: A_ , A_ : str = arr[i], arr[0] else: A_ , A_ : List[str] = arr[i], arr[c[i]] res.append(tuple(_lowerCAmelCase ) ) c[i] += 1 A_ : Tuple = 0 else: A_ : Dict = 0 i += 1 generate(len(_lowerCAmelCase ) , _lowerCAmelCase ) return res if __name__ == "__main__": _lowerCAmelCase : str = input('''Enter numbers separated by a comma:\n''').strip() _lowerCAmelCase : str = [int(item) for item in user_input.split(''',''')] print(heaps(arr))
300
1
import os import socket from contextlib import contextmanager import torch from ..commands.config.default import write_basic_config # noqa: F401 from ..state import PartialState from .dataclasses import DistributedType from .imports import is_deepspeed_available, is_tpu_available from .transformer_engine import convert_model from .versions import is_torch_version if is_deepspeed_available(): from deepspeed import DeepSpeedEngine if is_tpu_available(check_device=False): import torch_xla.core.xla_model as xm def __snake_case ( _lowerCAmelCase : int ) -> Tuple: if is_torch_version("<" , "2.0.0" ) or not hasattr(_lowerCAmelCase , "_dynamo" ): return False return isinstance(_lowerCAmelCase , torch._dynamo.eval_frame.OptimizedModule ) def __snake_case ( _lowerCAmelCase : Dict , _lowerCAmelCase : bool = True ) -> Optional[Any]: A_ : Optional[Any] = (torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel) A_ : Dict = is_compiled_module(_lowerCAmelCase ) if is_compiled: A_ : Tuple = model A_ : Optional[Any] = model._orig_mod if is_deepspeed_available(): options += (DeepSpeedEngine,) while isinstance(_lowerCAmelCase , _lowerCAmelCase ): A_ : Optional[Any] = model.module if not keep_fpaa_wrapper: A_ : Optional[Any] = getattr(_lowerCAmelCase , "forward" ) A_ : Dict = model.__dict__.pop("_original_forward" , _lowerCAmelCase ) if original_forward is not None: while hasattr(_lowerCAmelCase , "__wrapped__" ): A_ : Dict = forward.__wrapped__ if forward == original_forward: break A_ : Union[str, Any] = forward if getattr(_lowerCAmelCase , "_converted_to_transformer_engine" , _lowerCAmelCase ): convert_model(_lowerCAmelCase , to_transformer_engine=_lowerCAmelCase ) if is_compiled: A_ : Dict = model A_ : Tuple = compiled_model return model def __snake_case ( ) -> int: PartialState().wait_for_everyone() def __snake_case ( _lowerCAmelCase : List[Any] , _lowerCAmelCase : Optional[Any] ) -> Union[str, Any]: if PartialState().distributed_type == DistributedType.TPU: xm.save(_lowerCAmelCase , _lowerCAmelCase ) elif PartialState().local_process_index == 0: torch.save(_lowerCAmelCase , _lowerCAmelCase ) @contextmanager def __snake_case ( **_lowerCAmelCase : Union[str, Any] ) -> Optional[int]: for key, value in kwargs.items(): A_ : Optional[int] = str(_lowerCAmelCase ) yield for key in kwargs: if key.upper() in os.environ: del os.environ[key.upper()] def __snake_case ( _lowerCAmelCase : int ) -> Tuple: if not hasattr(_lowerCAmelCase , "__qualname__" ) and not hasattr(_lowerCAmelCase , "__name__" ): A_ : List[str] = getattr(_lowerCAmelCase , "__class__" , _lowerCAmelCase ) if hasattr(_lowerCAmelCase , "__qualname__" ): return obj.__qualname__ if hasattr(_lowerCAmelCase , "__name__" ): return obj.__name__ return str(_lowerCAmelCase ) def __snake_case ( _lowerCAmelCase : str , _lowerCAmelCase : Optional[Any] ) -> List[str]: for key, value in source.items(): if isinstance(_lowerCAmelCase , _lowerCAmelCase ): A_ : List[Any] = destination.setdefault(_lowerCAmelCase , {} ) merge_dicts(_lowerCAmelCase , _lowerCAmelCase ) else: A_ : Tuple = value return destination def __snake_case ( _lowerCAmelCase : int = None ) -> bool: if port is None: A_ : str = 29500 with socket.socket(socket.AF_INET , socket.SOCK_STREAM ) as s: return s.connect_ex(("localhost", port) ) == 0
300
import json from typing import List, Optional, Tuple from tokenizers import pre_tokenizers, processors from ...tokenization_utils_base import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_roberta import RobertaTokenizer _lowerCAmelCase : int = logging.get_logger(__name__) _lowerCAmelCase : Optional[int] = {'''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt''', '''tokenizer_file''': '''tokenizer.json'''} _lowerCAmelCase : List[Any] = { '''vocab_file''': { '''roberta-base''': '''https://huggingface.co/roberta-base/resolve/main/vocab.json''', '''roberta-large''': '''https://huggingface.co/roberta-large/resolve/main/vocab.json''', '''roberta-large-mnli''': '''https://huggingface.co/roberta-large-mnli/resolve/main/vocab.json''', '''distilroberta-base''': '''https://huggingface.co/distilroberta-base/resolve/main/vocab.json''', '''roberta-base-openai-detector''': '''https://huggingface.co/roberta-base-openai-detector/resolve/main/vocab.json''', '''roberta-large-openai-detector''': ( '''https://huggingface.co/roberta-large-openai-detector/resolve/main/vocab.json''' ), }, '''merges_file''': { '''roberta-base''': '''https://huggingface.co/roberta-base/resolve/main/merges.txt''', '''roberta-large''': '''https://huggingface.co/roberta-large/resolve/main/merges.txt''', '''roberta-large-mnli''': '''https://huggingface.co/roberta-large-mnli/resolve/main/merges.txt''', '''distilroberta-base''': '''https://huggingface.co/distilroberta-base/resolve/main/merges.txt''', '''roberta-base-openai-detector''': '''https://huggingface.co/roberta-base-openai-detector/resolve/main/merges.txt''', '''roberta-large-openai-detector''': ( '''https://huggingface.co/roberta-large-openai-detector/resolve/main/merges.txt''' ), }, '''tokenizer_file''': { '''roberta-base''': '''https://huggingface.co/roberta-base/resolve/main/tokenizer.json''', '''roberta-large''': '''https://huggingface.co/roberta-large/resolve/main/tokenizer.json''', '''roberta-large-mnli''': '''https://huggingface.co/roberta-large-mnli/resolve/main/tokenizer.json''', '''distilroberta-base''': '''https://huggingface.co/distilroberta-base/resolve/main/tokenizer.json''', '''roberta-base-openai-detector''': ( '''https://huggingface.co/roberta-base-openai-detector/resolve/main/tokenizer.json''' ), '''roberta-large-openai-detector''': ( '''https://huggingface.co/roberta-large-openai-detector/resolve/main/tokenizer.json''' ), }, } _lowerCAmelCase : Any = { '''roberta-base''': 512, '''roberta-large''': 512, '''roberta-large-mnli''': 512, '''distilroberta-base''': 512, '''roberta-base-openai-detector''': 512, '''roberta-large-openai-detector''': 512, } class __magic_name__ ( lowerCamelCase__ ): """simple docstring""" __UpperCamelCase = VOCAB_FILES_NAMES __UpperCamelCase = PRETRAINED_VOCAB_FILES_MAP __UpperCamelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCamelCase = ['''input_ids''', '''attention_mask'''] __UpperCamelCase = RobertaTokenizer def __init__( self :Dict , snake_case :List[str]=None , snake_case :List[Any]=None , snake_case :Union[str, Any]=None , snake_case :List[str]="replace" , snake_case :Tuple="<s>" , snake_case :Union[str, Any]="</s>" , snake_case :str="</s>" , snake_case :Union[str, Any]="<s>" , snake_case :int="<unk>" , snake_case :Tuple="<pad>" , snake_case :List[str]="<mask>" , snake_case :Any=False , snake_case :Union[str, Any]=True , **snake_case :Optional[int] , ): '''simple docstring''' super().__init__( snake_case , snake_case , tokenizer_file=snake_case , errors=snake_case , bos_token=snake_case , eos_token=snake_case , sep_token=snake_case , cls_token=snake_case , unk_token=snake_case , pad_token=snake_case , mask_token=snake_case , add_prefix_space=snake_case , trim_offsets=snake_case , **snake_case , ) A_ : Optional[Any] = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get("add_prefix_space" , snake_case ) != add_prefix_space: A_ : Dict = getattr(snake_case , pre_tok_state.pop("type" ) ) A_ : Optional[int] = add_prefix_space A_ : int = pre_tok_class(**snake_case ) A_ : Optional[int] = add_prefix_space A_ : Optional[int] = "post_processor" A_ : Dict = getattr(self.backend_tokenizer , snake_case , snake_case ) if tokenizer_component_instance: A_ : Dict = json.loads(tokenizer_component_instance.__getstate__() ) # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class` if "sep" in state: A_ : List[Any] = tuple(state["sep"] ) if "cls" in state: A_ : Optional[Any] = tuple(state["cls"] ) A_ : Tuple = False if state.get("add_prefix_space" , snake_case ) != add_prefix_space: A_ : List[Any] = add_prefix_space A_ : Optional[int] = True if state.get("trim_offsets" , snake_case ) != trim_offsets: A_ : List[str] = trim_offsets A_ : Any = True if changes_to_apply: A_ : Optional[Any] = getattr(snake_case , state.pop("type" ) ) A_ : Any = component_class(**snake_case ) setattr(self.backend_tokenizer , snake_case , snake_case ) @property def SCREAMING_SNAKE_CASE ( self :List[Any] ): '''simple docstring''' if self._mask_token is None: if self.verbose: logger.error("Using mask_token, but it is not set yet." ) return None return str(self._mask_token ) @mask_token.setter def SCREAMING_SNAKE_CASE ( self :Any , snake_case :Dict ): '''simple docstring''' A_ : Dict = AddedToken(snake_case , lstrip=snake_case , rstrip=snake_case ) if isinstance(snake_case , snake_case ) else value A_ : Any = value def SCREAMING_SNAKE_CASE ( self :Dict , *snake_case :Tuple , **snake_case :Union[str, Any] ): '''simple docstring''' A_ : Any = kwargs.get("is_split_into_words" , snake_case ) assert self.add_prefix_space or not is_split_into_words, ( f"You need to instantiate {self.__class__.__name__} with add_prefix_space=True " "to use it with pretokenized inputs." ) return super()._batch_encode_plus(*snake_case , **snake_case ) def SCREAMING_SNAKE_CASE ( self :List[str] , *snake_case :str , **snake_case :Union[str, Any] ): '''simple docstring''' A_ : Any = kwargs.get("is_split_into_words" , snake_case ) assert self.add_prefix_space or not is_split_into_words, ( f"You need to instantiate {self.__class__.__name__} with add_prefix_space=True " "to use it with pretokenized inputs." ) return super()._encode_plus(*snake_case , **snake_case ) def SCREAMING_SNAKE_CASE ( self :Union[str, Any] , snake_case :str , snake_case :Optional[str] = None ): '''simple docstring''' A_ : str = self._tokenizer.model.save(snake_case , name=snake_case ) return tuple(snake_case ) def SCREAMING_SNAKE_CASE ( self :List[str] , snake_case :List[str] , snake_case :Optional[Any]=None ): '''simple docstring''' A_ : int = [self.bos_token_id] + token_ids_a + [self.eos_token_id] if token_ids_a is None: return output return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id] def SCREAMING_SNAKE_CASE ( self :Any , snake_case :List[int] , snake_case :Optional[List[int]] = None ): '''simple docstring''' A_ : Any = [self.sep_token_id] A_ : Optional[Any] = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0]
300
1
import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging _lowerCAmelCase : Tuple = logging.get_logger(__name__) _lowerCAmelCase : Dict = { '''facebook/wav2vec2-base-960h''': '''https://huggingface.co/facebook/wav2vec2-base-960h/resolve/main/config.json''', # See all Wav2Vec2 models at https://huggingface.co/models?filter=wav2vec2 } class __magic_name__ ( lowerCamelCase__ ): """simple docstring""" __UpperCamelCase = '''wav2vec2''' def __init__( self :List[str] , snake_case :Union[str, Any]=32 , snake_case :Optional[Any]=768 , snake_case :Tuple=12 , snake_case :Union[str, Any]=12 , snake_case :Union[str, Any]=3_072 , snake_case :int="gelu" , snake_case :List[str]=0.1 , snake_case :Union[str, Any]=0.1 , snake_case :Optional[Any]=0.1 , snake_case :Any=0.0 , snake_case :Dict=0.0 , snake_case :Dict=0.1 , snake_case :Optional[Any]=0.1 , snake_case :str=0.02 , snake_case :Optional[int]=1e-5 , snake_case :Optional[Any]="group" , snake_case :Any="gelu" , snake_case :Union[str, Any]=(512, 512, 512, 512, 512, 512, 512) , snake_case :List[Any]=(5, 2, 2, 2, 2, 2, 2) , snake_case :Dict=(10, 3, 3, 3, 3, 2, 2) , snake_case :List[str]=False , snake_case :int=128 , snake_case :Union[str, Any]=16 , snake_case :Tuple=False , snake_case :int=True , snake_case :Dict=0.05 , snake_case :Optional[int]=10 , snake_case :Union[str, Any]=2 , snake_case :Any=0.0 , snake_case :Optional[int]=10 , snake_case :List[Any]=0 , snake_case :Dict=320 , snake_case :List[str]=2 , snake_case :Tuple=0.1 , snake_case :Dict=100 , snake_case :Optional[int]=256 , snake_case :Tuple=256 , snake_case :List[str]=0.1 , snake_case :Optional[int]="sum" , snake_case :str=False , snake_case :Tuple=False , snake_case :Any=256 , snake_case :List[str]=(512, 512, 512, 512, 1_500) , snake_case :Dict=(5, 3, 3, 1, 1) , snake_case :List[str]=(1, 2, 3, 1, 1) , snake_case :Optional[Any]=512 , snake_case :Tuple=0 , snake_case :Tuple=1 , snake_case :List[Any]=2 , snake_case :Dict=False , snake_case :List[str]=3 , snake_case :Any=2 , snake_case :Any=3 , snake_case :str=None , snake_case :Optional[int]=None , **snake_case :Optional[int] , ): '''simple docstring''' super().__init__(**snake_case , pad_token_id=snake_case , bos_token_id=snake_case , eos_token_id=snake_case ) A_ : int = hidden_size A_ : Any = feat_extract_norm A_ : List[Any] = feat_extract_activation A_ : Tuple = list(snake_case ) A_ : Optional[Any] = list(snake_case ) A_ : Dict = list(snake_case ) A_ : Dict = conv_bias A_ : str = num_conv_pos_embeddings A_ : Optional[Any] = num_conv_pos_embedding_groups A_ : Any = len(self.conv_dim ) A_ : List[str] = num_hidden_layers A_ : Optional[int] = intermediate_size A_ : Any = hidden_act A_ : Tuple = num_attention_heads A_ : Optional[int] = hidden_dropout A_ : Tuple = attention_dropout A_ : Union[str, Any] = activation_dropout A_ : int = feat_proj_dropout A_ : int = final_dropout A_ : Optional[int] = layerdrop A_ : Union[str, Any] = layer_norm_eps A_ : Dict = initializer_range A_ : Any = vocab_size A_ : List[str] = do_stable_layer_norm A_ : str = 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 A_ : Dict = apply_spec_augment A_ : int = mask_time_prob A_ : str = mask_time_length A_ : Tuple = mask_time_min_masks A_ : Optional[int] = mask_feature_prob A_ : str = mask_feature_length A_ : str = mask_feature_min_masks # parameters for pretraining with codevector quantized representations A_ : List[Any] = num_codevectors_per_group A_ : Optional[Any] = num_codevector_groups A_ : List[str] = contrastive_logits_temperature A_ : Any = feat_quantizer_dropout A_ : Dict = num_negatives A_ : Tuple = codevector_dim A_ : Optional[int] = proj_codevector_dim A_ : int = diversity_loss_weight # ctc loss A_ : str = ctc_loss_reduction A_ : str = ctc_zero_infinity # adapter A_ : Tuple = add_adapter A_ : Optional[Any] = adapter_kernel_size A_ : List[Any] = adapter_stride A_ : List[str] = num_adapter_layers A_ : str = output_hidden_size or hidden_size A_ : str = adapter_attn_dim # SequenceClassification-specific parameter. Feel free to ignore for other classes. A_ : Tuple = classifier_proj_size # XVector-specific parameters. Feel free to ignore for other classes. A_ : List[Any] = list(snake_case ) A_ : str = list(snake_case ) A_ : List[Any] = list(snake_case ) A_ : Tuple = xvector_output_dim @property def SCREAMING_SNAKE_CASE ( self :Optional[int] ): '''simple docstring''' return functools.reduce(operator.mul , self.conv_stride , 1 )
300
from typing import Dict, List from nltk.translate import gleu_score import datasets from datasets import MetricInfo _lowerCAmelCase : int = '''\ @misc{wu2016googles, title={Google\'s Neural Machine Translation System: Bridging the Gap between Human and Machine Translation}, author={Yonghui Wu and Mike Schuster and Zhifeng Chen and Quoc V. Le and Mohammad Norouzi and Wolfgang Macherey and Maxim Krikun and Yuan Cao and Qin Gao and Klaus Macherey and Jeff Klingner and Apurva Shah and Melvin Johnson and Xiaobing Liu and Łukasz Kaiser and Stephan Gouws and Yoshikiyo Kato and Taku Kudo and Hideto Kazawa and Keith Stevens and George Kurian and Nishant Patil and Wei Wang and Cliff Young and Jason Smith and Jason Riesa and Alex Rudnick and Oriol Vinyals and Greg Corrado and Macduff Hughes and Jeffrey Dean}, year={2016}, eprint={1609.08144}, archivePrefix={arXiv}, primaryClass={cs.CL} } ''' _lowerCAmelCase : Tuple = '''\ The BLEU score has some undesirable properties when used for single sentences, as it was designed to be a corpus measure. We therefore use a slightly different score for our RL experiments which we call the \'GLEU score\'. For the GLEU score, we record all sub-sequences of 1, 2, 3 or 4 tokens in output and target sequence (n-grams). We then compute a recall, which is the ratio of the number of matching n-grams to the number of total n-grams in the target (ground truth) sequence, and a precision, which is the ratio of the number of matching n-grams to the number of total n-grams in the generated output sequence. Then GLEU score is simply the minimum of recall and precision. This GLEU score\'s range is always between 0 (no matches) and 1 (all match) and it is symmetrical when switching output and target. According to our experiments, GLEU score correlates quite well with the BLEU metric on a corpus level but does not have its drawbacks for our per sentence reward objective. ''' _lowerCAmelCase : int = '''\ Computes corpus-level Google BLEU (GLEU) score of translated segments against one or more references. Instead of averaging the sentence level GLEU scores (i.e. macro-average precision), Wu et al. (2016) sum up the matching tokens and the max of hypothesis and reference tokens for each sentence, then compute using the aggregate values. Args: predictions (list of str): list of translations to score. Each translation should be tokenized into a list of tokens. references (list of list of str): list of lists of references for each translation. Each reference should be tokenized into a list of tokens. min_len (int): The minimum order of n-gram this function should extract. Defaults to 1. max_len (int): The maximum order of n-gram this function should extract. Defaults to 4. Returns: \'google_bleu\': google_bleu score Examples: Example 1: >>> hyp1 = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'which\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'always\', ... \'disobeys\', \'the\', \'commands\', \'of\', \'the\', \'cat\'] >>> ref1a = [\'It\', \'is\', \'the\', \'guiding\', \'principle\', \'which\', ... \'guarantees\', \'the\', \'rubber\', \'duck\', \'forces\', \'never\', ... \'being\', \'under\', \'the\', \'command\', \'of\', \'the\', \'cat\'] >>> hyp2 = [\'he\', \'read\', \'the\', \'book\', \'because\', \'he\', \'was\', ... \'interested\', \'in\', \'world\', \'history\'] >>> ref2a = [\'he\', \'was\', \'interested\', \'in\', \'world\', \'history\', ... \'because\', \'he\', \'read\', \'the\', \'book\'] >>> list_of_references = [[ref1a], [ref2a]] >>> hypotheses = [hyp1, hyp2] >>> google_bleu = datasets.load_metric("google_bleu") >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references) >>> print(round(results["google_bleu"], 2)) 0.44 Example 2: >>> hyp1 = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'which\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'always\', ... \'disobeys\', \'the\', \'commands\', \'of\', \'the\', \'cat\'] >>> ref1a = [\'It\', \'is\', \'the\', \'guiding\', \'principle\', \'which\', ... \'guarantees\', \'the\', \'rubber\', \'duck\', \'forces\', \'never\', ... \'being\', \'under\', \'the\', \'command\', \'of\', \'the\', \'cat\'] >>> ref1b = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'that\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'will\', \'never\', ... \'heed\', \'the\', \'cat\', \'commands\'] >>> ref1c = [\'It\', \'is\', \'the\', \'practical\', \'guide\', \'for\', \'the\', ... \'rubber\', \'duck\', \'army\', \'never\', \'to\', \'heed\', \'the\', \'directions\', ... \'of\', \'the\', \'cat\'] >>> hyp2 = [\'he\', \'read\', \'the\', \'book\', \'because\', \'he\', \'was\', ... \'interested\', \'in\', \'world\', \'history\'] >>> ref2a = [\'he\', \'was\', \'interested\', \'in\', \'world\', \'history\', ... \'because\', \'he\', \'read\', \'the\', \'book\'] >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]] >>> hypotheses = [hyp1, hyp2] >>> google_bleu = datasets.load_metric("google_bleu") >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references) >>> print(round(results["google_bleu"], 2)) 0.61 Example 3: >>> hyp1 = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'which\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'always\', ... \'disobeys\', \'the\', \'commands\', \'of\', \'the\', \'cat\'] >>> ref1a = [\'It\', \'is\', \'the\', \'guiding\', \'principle\', \'which\', ... \'guarantees\', \'the\', \'rubber\', \'duck\', \'forces\', \'never\', ... \'being\', \'under\', \'the\', \'command\', \'of\', \'the\', \'cat\'] >>> ref1b = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'that\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'will\', \'never\', ... \'heed\', \'the\', \'cat\', \'commands\'] >>> ref1c = [\'It\', \'is\', \'the\', \'practical\', \'guide\', \'for\', \'the\', ... \'rubber\', \'duck\', \'army\', \'never\', \'to\', \'heed\', \'the\', \'directions\', ... \'of\', \'the\', \'cat\'] >>> hyp2 = [\'he\', \'read\', \'the\', \'book\', \'because\', \'he\', \'was\', ... \'interested\', \'in\', \'world\', \'history\'] >>> ref2a = [\'he\', \'was\', \'interested\', \'in\', \'world\', \'history\', ... \'because\', \'he\', \'read\', \'the\', \'book\'] >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]] >>> hypotheses = [hyp1, hyp2] >>> google_bleu = datasets.load_metric("google_bleu") >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references, min_len=2) >>> print(round(results["google_bleu"], 2)) 0.53 Example 4: >>> hyp1 = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'which\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'always\', ... \'disobeys\', \'the\', \'commands\', \'of\', \'the\', \'cat\'] >>> ref1a = [\'It\', \'is\', \'the\', \'guiding\', \'principle\', \'which\', ... \'guarantees\', \'the\', \'rubber\', \'duck\', \'forces\', \'never\', ... \'being\', \'under\', \'the\', \'command\', \'of\', \'the\', \'cat\'] >>> ref1b = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'that\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'will\', \'never\', ... \'heed\', \'the\', \'cat\', \'commands\'] >>> ref1c = [\'It\', \'is\', \'the\', \'practical\', \'guide\', \'for\', \'the\', ... \'rubber\', \'duck\', \'army\', \'never\', \'to\', \'heed\', \'the\', \'directions\', ... \'of\', \'the\', \'cat\'] >>> hyp2 = [\'he\', \'read\', \'the\', \'book\', \'because\', \'he\', \'was\', ... \'interested\', \'in\', \'world\', \'history\'] >>> ref2a = [\'he\', \'was\', \'interested\', \'in\', \'world\', \'history\', ... \'because\', \'he\', \'read\', \'the\', \'book\'] >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]] >>> hypotheses = [hyp1, hyp2] >>> google_bleu = datasets.load_metric("google_bleu") >>> results = google_bleu.compute(predictions=hypotheses,references=list_of_references, min_len=2, max_len=6) >>> print(round(results["google_bleu"], 2)) 0.4 ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __magic_name__ ( datasets.Metric ): """simple docstring""" def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "predictions": datasets.Sequence(datasets.Value("string" , id="token" ) , id="sequence" ), "references": datasets.Sequence( datasets.Sequence(datasets.Value("string" , id="token" ) , id="sequence" ) , id="references" ), } ) , ) def SCREAMING_SNAKE_CASE ( self :int , snake_case :List[List[List[str]]] , snake_case :List[List[str]] , snake_case :int = 1 , snake_case :int = 4 , ): '''simple docstring''' return { "google_bleu": gleu_score.corpus_gleu( list_of_references=snake_case , hypotheses=snake_case , min_len=snake_case , max_len=snake_case ) }
300
1
from collections import namedtuple import requests from lxml import html # type: ignore _lowerCAmelCase : Optional[int] = namedtuple('''covid_data''', '''cases deaths recovered''') def __snake_case ( _lowerCAmelCase : str = "https://www.worldometers.info/coronavirus/" ) -> covid_data: A_ : Any = "//div[@class = \"maincounter-number\"]/span/text()" return covid_data(*html.fromstring(requests.get(_lowerCAmelCase ).content ).xpath(_lowerCAmelCase ) ) _lowerCAmelCase : List[str] = '''Total COVID-19 cases in the world: {} Total deaths due to COVID-19 in the world: {} Total COVID-19 patients recovered in the world: {}''' print(fmt.format(*covid_stats()))
300
import pyarrow.parquet as pq import pytest from datasets import Audio, Dataset, DatasetDict, Features, NamedSplit, Sequence, Value, config from datasets.features.image import Image from datasets.io.parquet import ParquetDatasetReader, ParquetDatasetWriter, get_writer_batch_size from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases def __snake_case ( _lowerCAmelCase : List[Any] , _lowerCAmelCase : Optional[int] ) -> str: assert isinstance(_lowerCAmelCase , _lowerCAmelCase ) assert dataset.num_rows == 4 assert dataset.num_columns == 3 assert dataset.column_names == ["col_1", "col_2", "col_3"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize("keep_in_memory" , [False, True] ) def __snake_case ( _lowerCAmelCase : Dict , _lowerCAmelCase : Optional[int] , _lowerCAmelCase : List[Any] ) -> Optional[int]: A_ : Tuple = tmp_path / "cache" A_ : Optional[int] = {"col_1": "string", "col_2": "int64", "col_3": "float64"} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): A_ : Optional[Any] = ParquetDatasetReader(_lowerCAmelCase , cache_dir=_lowerCAmelCase , keep_in_memory=_lowerCAmelCase ).read() _check_parquet_dataset(_lowerCAmelCase , _lowerCAmelCase ) @pytest.mark.parametrize( "features" , [ None, {"col_1": "string", "col_2": "int64", "col_3": "float64"}, {"col_1": "string", "col_2": "string", "col_3": "string"}, {"col_1": "int32", "col_2": "int32", "col_3": "int32"}, {"col_1": "float32", "col_2": "float32", "col_3": "float32"}, ] , ) def __snake_case ( _lowerCAmelCase : List[Any] , _lowerCAmelCase : Any , _lowerCAmelCase : List[Any] ) -> str: A_ : List[Any] = tmp_path / "cache" A_ : List[str] = {"col_1": "string", "col_2": "int64", "col_3": "float64"} A_ : int = features.copy() if features else default_expected_features A_ : str = ( Features({feature: Value(_lowerCAmelCase ) for feature, dtype in features.items()} ) if features is not None else None ) A_ : Union[str, Any] = ParquetDatasetReader(_lowerCAmelCase , features=_lowerCAmelCase , cache_dir=_lowerCAmelCase ).read() _check_parquet_dataset(_lowerCAmelCase , _lowerCAmelCase ) @pytest.mark.parametrize("split" , [None, NamedSplit("train" ), "train", "test"] ) def __snake_case ( _lowerCAmelCase : Any , _lowerCAmelCase : Any , _lowerCAmelCase : Any ) -> Optional[Any]: A_ : Dict = tmp_path / "cache" A_ : int = {"col_1": "string", "col_2": "int64", "col_3": "float64"} A_ : Optional[int] = ParquetDatasetReader(_lowerCAmelCase , cache_dir=_lowerCAmelCase , split=_lowerCAmelCase ).read() _check_parquet_dataset(_lowerCAmelCase , _lowerCAmelCase ) assert dataset.split == split if split else "train" @pytest.mark.parametrize("path_type" , [str, list] ) def __snake_case ( _lowerCAmelCase : List[str] , _lowerCAmelCase : int , _lowerCAmelCase : Optional[Any] ) -> List[str]: if issubclass(_lowerCAmelCase , _lowerCAmelCase ): A_ : int = parquet_path elif issubclass(_lowerCAmelCase , _lowerCAmelCase ): A_ : Optional[int] = [parquet_path] A_ : Optional[int] = tmp_path / "cache" A_ : Union[str, Any] = {"col_1": "string", "col_2": "int64", "col_3": "float64"} A_ : Optional[int] = ParquetDatasetReader(_lowerCAmelCase , cache_dir=_lowerCAmelCase ).read() _check_parquet_dataset(_lowerCAmelCase , _lowerCAmelCase ) def __snake_case ( _lowerCAmelCase : Any , _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : Union[str, Any]=("train",) ) -> Tuple: assert isinstance(_lowerCAmelCase , _lowerCAmelCase ) for split in splits: A_ : List[str] = dataset_dict[split] assert dataset.num_rows == 4 assert dataset.num_columns == 3 assert dataset.column_names == ["col_1", "col_2", "col_3"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize("keep_in_memory" , [False, True] ) def __snake_case ( _lowerCAmelCase : Optional[Any] , _lowerCAmelCase : List[Any] , _lowerCAmelCase : Dict ) -> Optional[int]: A_ : Optional[Any] = tmp_path / "cache" A_ : Union[str, Any] = {"col_1": "string", "col_2": "int64", "col_3": "float64"} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): A_ : Union[str, Any] = ParquetDatasetReader( {"train": parquet_path} , cache_dir=_lowerCAmelCase , keep_in_memory=_lowerCAmelCase ).read() _check_parquet_datasetdict(_lowerCAmelCase , _lowerCAmelCase ) @pytest.mark.parametrize( "features" , [ None, {"col_1": "string", "col_2": "int64", "col_3": "float64"}, {"col_1": "string", "col_2": "string", "col_3": "string"}, {"col_1": "int32", "col_2": "int32", "col_3": "int32"}, {"col_1": "float32", "col_2": "float32", "col_3": "float32"}, ] , ) def __snake_case ( _lowerCAmelCase : Tuple , _lowerCAmelCase : Any , _lowerCAmelCase : str ) -> Tuple: A_ : Optional[Any] = tmp_path / "cache" A_ : Any = {"col_1": "string", "col_2": "int64", "col_3": "float64"} A_ : List[str] = features.copy() if features else default_expected_features A_ : Tuple = ( Features({feature: Value(_lowerCAmelCase ) for feature, dtype in features.items()} ) if features is not None else None ) A_ : Optional[int] = ParquetDatasetReader({"train": parquet_path} , features=_lowerCAmelCase , cache_dir=_lowerCAmelCase ).read() _check_parquet_datasetdict(_lowerCAmelCase , _lowerCAmelCase ) @pytest.mark.parametrize("split" , [None, NamedSplit("train" ), "train", "test"] ) def __snake_case ( _lowerCAmelCase : str , _lowerCAmelCase : str , _lowerCAmelCase : Any ) -> Union[str, Any]: if split: A_ : Any = {split: parquet_path} else: A_ : Optional[Any] = "train" A_ : str = {"train": parquet_path, "test": parquet_path} A_ : Any = tmp_path / "cache" A_ : str = {"col_1": "string", "col_2": "int64", "col_3": "float64"} A_ : Dict = ParquetDatasetReader(_lowerCAmelCase , cache_dir=_lowerCAmelCase ).read() _check_parquet_datasetdict(_lowerCAmelCase , _lowerCAmelCase , splits=list(path.keys() ) ) assert all(dataset[split].split == split for split in path.keys() ) def __snake_case ( _lowerCAmelCase : Dict , _lowerCAmelCase : Optional[Any] ) -> Dict: A_ : List[str] = ParquetDatasetWriter(_lowerCAmelCase , tmp_path / "foo.parquet" ) assert writer.write() > 0 A_ : Tuple = pq.ParquetFile(tmp_path / "foo.parquet" ) A_ : Dict = pf.read() assert dataset.data.table == output_table def __snake_case ( _lowerCAmelCase : Optional[Any] , _lowerCAmelCase : int ) -> List[Any]: A_ : Tuple = str(shared_datadir / "test_image_rgb.jpg" ) A_ : int = {"image": [image_path]} A_ : Optional[Any] = Features({"image": Image()} ) A_ : Union[str, Any] = Dataset.from_dict(_lowerCAmelCase , features=_lowerCAmelCase ) A_ : Tuple = ParquetDatasetWriter(_lowerCAmelCase , tmp_path / "foo.parquet" ) assert writer.write() > 0 A_ : str = Dataset.from_parquet(str(tmp_path / "foo.parquet" ) ) assert dataset.features == reloaded_dataset.features A_ : int = ParquetDatasetReader(str(tmp_path / "foo.parquet" ) , streaming=_lowerCAmelCase ).read() assert dataset.features == reloaded_iterable_dataset.features @pytest.mark.parametrize( "feature, expected" , [ (Features({"foo": Value("int32" )} ), None), (Features({"image": Image(), "foo": Value("int32" )} ), config.PARQUET_ROW_GROUP_SIZE_FOR_IMAGE_DATASETS), (Features({"nested": Sequence(Audio() )} ), config.PARQUET_ROW_GROUP_SIZE_FOR_AUDIO_DATASETS), ] , ) def __snake_case ( _lowerCAmelCase : str , _lowerCAmelCase : List[Any] ) -> Any: assert get_writer_batch_size(_lowerCAmelCase ) == expected
300
1
import json from typing import List, Optional, Tuple from tokenizers import pre_tokenizers, processors from ...tokenization_utils_base import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_roberta import RobertaTokenizer _lowerCAmelCase : int = logging.get_logger(__name__) _lowerCAmelCase : Optional[int] = {'''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt''', '''tokenizer_file''': '''tokenizer.json'''} _lowerCAmelCase : List[Any] = { '''vocab_file''': { '''roberta-base''': '''https://huggingface.co/roberta-base/resolve/main/vocab.json''', '''roberta-large''': '''https://huggingface.co/roberta-large/resolve/main/vocab.json''', '''roberta-large-mnli''': '''https://huggingface.co/roberta-large-mnli/resolve/main/vocab.json''', '''distilroberta-base''': '''https://huggingface.co/distilroberta-base/resolve/main/vocab.json''', '''roberta-base-openai-detector''': '''https://huggingface.co/roberta-base-openai-detector/resolve/main/vocab.json''', '''roberta-large-openai-detector''': ( '''https://huggingface.co/roberta-large-openai-detector/resolve/main/vocab.json''' ), }, '''merges_file''': { '''roberta-base''': '''https://huggingface.co/roberta-base/resolve/main/merges.txt''', '''roberta-large''': '''https://huggingface.co/roberta-large/resolve/main/merges.txt''', '''roberta-large-mnli''': '''https://huggingface.co/roberta-large-mnli/resolve/main/merges.txt''', '''distilroberta-base''': '''https://huggingface.co/distilroberta-base/resolve/main/merges.txt''', '''roberta-base-openai-detector''': '''https://huggingface.co/roberta-base-openai-detector/resolve/main/merges.txt''', '''roberta-large-openai-detector''': ( '''https://huggingface.co/roberta-large-openai-detector/resolve/main/merges.txt''' ), }, '''tokenizer_file''': { '''roberta-base''': '''https://huggingface.co/roberta-base/resolve/main/tokenizer.json''', '''roberta-large''': '''https://huggingface.co/roberta-large/resolve/main/tokenizer.json''', '''roberta-large-mnli''': '''https://huggingface.co/roberta-large-mnli/resolve/main/tokenizer.json''', '''distilroberta-base''': '''https://huggingface.co/distilroberta-base/resolve/main/tokenizer.json''', '''roberta-base-openai-detector''': ( '''https://huggingface.co/roberta-base-openai-detector/resolve/main/tokenizer.json''' ), '''roberta-large-openai-detector''': ( '''https://huggingface.co/roberta-large-openai-detector/resolve/main/tokenizer.json''' ), }, } _lowerCAmelCase : Any = { '''roberta-base''': 512, '''roberta-large''': 512, '''roberta-large-mnli''': 512, '''distilroberta-base''': 512, '''roberta-base-openai-detector''': 512, '''roberta-large-openai-detector''': 512, } class __magic_name__ ( lowerCamelCase__ ): """simple docstring""" __UpperCamelCase = VOCAB_FILES_NAMES __UpperCamelCase = PRETRAINED_VOCAB_FILES_MAP __UpperCamelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCamelCase = ['''input_ids''', '''attention_mask'''] __UpperCamelCase = RobertaTokenizer def __init__( self :Dict , snake_case :List[str]=None , snake_case :List[Any]=None , snake_case :Union[str, Any]=None , snake_case :List[str]="replace" , snake_case :Tuple="<s>" , snake_case :Union[str, Any]="</s>" , snake_case :str="</s>" , snake_case :Union[str, Any]="<s>" , snake_case :int="<unk>" , snake_case :Tuple="<pad>" , snake_case :List[str]="<mask>" , snake_case :Any=False , snake_case :Union[str, Any]=True , **snake_case :Optional[int] , ): '''simple docstring''' super().__init__( snake_case , snake_case , tokenizer_file=snake_case , errors=snake_case , bos_token=snake_case , eos_token=snake_case , sep_token=snake_case , cls_token=snake_case , unk_token=snake_case , pad_token=snake_case , mask_token=snake_case , add_prefix_space=snake_case , trim_offsets=snake_case , **snake_case , ) A_ : Optional[Any] = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get("add_prefix_space" , snake_case ) != add_prefix_space: A_ : Dict = getattr(snake_case , pre_tok_state.pop("type" ) ) A_ : Optional[int] = add_prefix_space A_ : int = pre_tok_class(**snake_case ) A_ : Optional[int] = add_prefix_space A_ : Optional[int] = "post_processor" A_ : Dict = getattr(self.backend_tokenizer , snake_case , snake_case ) if tokenizer_component_instance: A_ : Dict = json.loads(tokenizer_component_instance.__getstate__() ) # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class` if "sep" in state: A_ : List[Any] = tuple(state["sep"] ) if "cls" in state: A_ : Optional[Any] = tuple(state["cls"] ) A_ : Tuple = False if state.get("add_prefix_space" , snake_case ) != add_prefix_space: A_ : List[Any] = add_prefix_space A_ : Optional[int] = True if state.get("trim_offsets" , snake_case ) != trim_offsets: A_ : List[str] = trim_offsets A_ : Any = True if changes_to_apply: A_ : Optional[Any] = getattr(snake_case , state.pop("type" ) ) A_ : Any = component_class(**snake_case ) setattr(self.backend_tokenizer , snake_case , snake_case ) @property def SCREAMING_SNAKE_CASE ( self :List[Any] ): '''simple docstring''' if self._mask_token is None: if self.verbose: logger.error("Using mask_token, but it is not set yet." ) return None return str(self._mask_token ) @mask_token.setter def SCREAMING_SNAKE_CASE ( self :Any , snake_case :Dict ): '''simple docstring''' A_ : Dict = AddedToken(snake_case , lstrip=snake_case , rstrip=snake_case ) if isinstance(snake_case , snake_case ) else value A_ : Any = value def SCREAMING_SNAKE_CASE ( self :Dict , *snake_case :Tuple , **snake_case :Union[str, Any] ): '''simple docstring''' A_ : Any = kwargs.get("is_split_into_words" , snake_case ) assert self.add_prefix_space or not is_split_into_words, ( f"You need to instantiate {self.__class__.__name__} with add_prefix_space=True " "to use it with pretokenized inputs." ) return super()._batch_encode_plus(*snake_case , **snake_case ) def SCREAMING_SNAKE_CASE ( self :List[str] , *snake_case :str , **snake_case :Union[str, Any] ): '''simple docstring''' A_ : Any = kwargs.get("is_split_into_words" , snake_case ) assert self.add_prefix_space or not is_split_into_words, ( f"You need to instantiate {self.__class__.__name__} with add_prefix_space=True " "to use it with pretokenized inputs." ) return super()._encode_plus(*snake_case , **snake_case ) def SCREAMING_SNAKE_CASE ( self :Union[str, Any] , snake_case :str , snake_case :Optional[str] = None ): '''simple docstring''' A_ : str = self._tokenizer.model.save(snake_case , name=snake_case ) return tuple(snake_case ) def SCREAMING_SNAKE_CASE ( self :List[str] , snake_case :List[str] , snake_case :Optional[Any]=None ): '''simple docstring''' A_ : int = [self.bos_token_id] + token_ids_a + [self.eos_token_id] if token_ids_a is None: return output return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id] def SCREAMING_SNAKE_CASE ( self :Any , snake_case :List[int] , snake_case :Optional[List[int]] = None ): '''simple docstring''' A_ : Any = [self.sep_token_id] A_ : Optional[Any] = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0]
300
import json import unittest import numpy as np from huggingface_hub import hf_hub_download from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from transformers import OneFormerImageProcessor from transformers.models.oneformer.image_processing_oneformer import binary_mask_to_rle from transformers.models.oneformer.modeling_oneformer import OneFormerForUniversalSegmentationOutput if is_vision_available(): from PIL import Image def __snake_case ( _lowerCAmelCase : List[str] , _lowerCAmelCase : List[Any]="shi-labs/oneformer_demo" ) -> int: with open(hf_hub_download(_lowerCAmelCase , _lowerCAmelCase , repo_type="dataset" ) , "r" ) as f: A_ : Optional[int] = json.load(_lowerCAmelCase ) A_ : Union[str, Any] = {} A_ : Tuple = [] A_ : Optional[Any] = [] for key, info in class_info.items(): A_ : Tuple = info["name"] class_names.append(info["name"] ) if info["isthing"]: thing_ids.append(int(_lowerCAmelCase ) ) A_ : Optional[Any] = thing_ids A_ : int = class_names return metadata class __magic_name__ ( unittest.TestCase ): """simple docstring""" def __init__( self :List[Any] , snake_case :List[str] , snake_case :int=7 , snake_case :Optional[int]=3 , snake_case :Union[str, Any]=30 , snake_case :Tuple=400 , snake_case :List[Any]=None , snake_case :Optional[Any]=True , snake_case :Tuple=True , snake_case :Dict=[0.5, 0.5, 0.5] , snake_case :Any=[0.5, 0.5, 0.5] , snake_case :Optional[int]=10 , snake_case :Tuple=False , snake_case :Optional[int]=255 , snake_case :Optional[Any]="shi-labs/oneformer_demo" , snake_case :Optional[Any]="ade20k_panoptic.json" , snake_case :Optional[int]=10 , ): '''simple docstring''' A_ : Tuple = parent A_ : List[str] = batch_size A_ : Optional[int] = num_channels A_ : Tuple = min_resolution A_ : List[Any] = max_resolution A_ : Union[str, Any] = do_resize A_ : Any = {"shortest_edge": 32, "longest_edge": 1_333} if size is None else size A_ : Tuple = do_normalize A_ : List[str] = image_mean A_ : List[Any] = image_std A_ : Union[str, Any] = class_info_file A_ : List[Any] = prepare_metadata(snake_case , snake_case ) A_ : Tuple = num_text A_ : str = repo_path # for the post_process_functions A_ : Any = 2 A_ : int = 10 A_ : Optional[int] = 10 A_ : Tuple = 3 A_ : Tuple = 4 A_ : str = num_labels A_ : int = do_reduce_labels A_ : List[Any] = ignore_index def SCREAMING_SNAKE_CASE ( self :Optional[Any] ): '''simple docstring''' return { "do_resize": self.do_resize, "size": self.size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, "num_labels": self.num_labels, "do_reduce_labels": self.do_reduce_labels, "ignore_index": self.ignore_index, "class_info_file": self.class_info_file, "metadata": self.metadata, "num_text": self.num_text, } def SCREAMING_SNAKE_CASE ( self :List[Any] , snake_case :Any , snake_case :Any=False ): '''simple docstring''' if not batched: A_ : List[str] = image_inputs[0] if isinstance(snake_case , Image.Image ): A_ , A_ : Dict = image.size else: A_ , A_ : Tuple = image.shape[1], image.shape[2] if w < h: A_ : str = int(self.size["shortest_edge"] * h / w ) A_ : Any = self.size["shortest_edge"] elif w > h: A_ : Optional[int] = self.size["shortest_edge"] A_ : List[str] = int(self.size["shortest_edge"] * w / h ) else: A_ : List[str] = self.size["shortest_edge"] A_ : Optional[Any] = self.size["shortest_edge"] else: A_ : Tuple = [] for image in image_inputs: A_ , A_ : Optional[Any] = self.get_expected_values([image] ) expected_values.append((expected_height, expected_width) ) A_ : Tuple = max(snake_case , key=lambda snake_case : item[0] )[0] A_ : Union[str, Any] = max(snake_case , key=lambda snake_case : item[1] )[1] return expected_height, expected_width def SCREAMING_SNAKE_CASE ( self :Tuple ): '''simple docstring''' return OneFormerForUniversalSegmentationOutput( # +1 for null class class_queries_logits=torch.randn((self.batch_size, self.num_queries, self.num_classes + 1) ) , masks_queries_logits=torch.randn((self.batch_size, self.num_queries, self.height, self.width) ) , ) @require_torch @require_vision class __magic_name__ ( lowerCamelCase__ , unittest.TestCase ): """simple docstring""" __UpperCamelCase = OneFormerImageProcessor if (is_vision_available() and is_torch_available()) else None # only for test_image_processing_common.test_image_proc_to_json_string __UpperCamelCase = image_processing_class def SCREAMING_SNAKE_CASE ( self :int ): '''simple docstring''' A_ : Union[str, Any] = OneFormerImageProcessorTester(self ) @property def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' return self.image_processing_tester.prepare_image_processor_dict() def SCREAMING_SNAKE_CASE ( self :List[Any] ): '''simple docstring''' A_ : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(snake_case , "image_mean" ) ) self.assertTrue(hasattr(snake_case , "image_std" ) ) self.assertTrue(hasattr(snake_case , "do_normalize" ) ) self.assertTrue(hasattr(snake_case , "do_resize" ) ) self.assertTrue(hasattr(snake_case , "size" ) ) self.assertTrue(hasattr(snake_case , "ignore_index" ) ) self.assertTrue(hasattr(snake_case , "class_info_file" ) ) self.assertTrue(hasattr(snake_case , "num_text" ) ) self.assertTrue(hasattr(snake_case , "repo_path" ) ) self.assertTrue(hasattr(snake_case , "metadata" ) ) self.assertTrue(hasattr(snake_case , "do_reduce_labels" ) ) def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' pass def SCREAMING_SNAKE_CASE ( self :int ): '''simple docstring''' A_ : Dict = self.image_processing_class(**self.image_processor_dict ) # create random PIL images A_ : Optional[Any] = prepare_image_inputs(self.image_processing_tester , equal_resolution=snake_case ) for image in image_inputs: self.assertIsInstance(snake_case , Image.Image ) # Test not batched input A_ : str = image_processor(image_inputs[0] , ["semantic"] , return_tensors="pt" ).pixel_values A_ , A_ : str = self.image_processing_tester.get_expected_values(snake_case ) self.assertEqual( encoded_images.shape , (1, self.image_processing_tester.num_channels, expected_height, expected_width) , ) # Test batched A_ , A_ : Optional[Any] = self.image_processing_tester.get_expected_values(snake_case , batched=snake_case ) A_ : List[str] = image_processor( snake_case , ["semantic"] * len(snake_case ) , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processing_tester.batch_size, self.image_processing_tester.num_channels, expected_height, expected_width, ) , ) def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' A_ : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors A_ : List[str] = prepare_image_inputs(self.image_processing_tester , equal_resolution=snake_case , numpify=snake_case ) for image in image_inputs: self.assertIsInstance(snake_case , np.ndarray ) # Test not batched input A_ : List[str] = image_processor(image_inputs[0] , ["semantic"] , return_tensors="pt" ).pixel_values A_ , A_ : List[str] = self.image_processing_tester.get_expected_values(snake_case ) self.assertEqual( encoded_images.shape , (1, self.image_processing_tester.num_channels, expected_height, expected_width) , ) # Test batched A_ , A_ : int = self.image_processing_tester.get_expected_values(snake_case , batched=snake_case ) A_ : Optional[Any] = image_processor( snake_case , ["semantic"] * len(snake_case ) , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processing_tester.batch_size, self.image_processing_tester.num_channels, expected_height, expected_width, ) , ) def SCREAMING_SNAKE_CASE ( self :Optional[int] ): '''simple docstring''' A_ : List[str] = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors A_ : List[str] = prepare_image_inputs(self.image_processing_tester , equal_resolution=snake_case , torchify=snake_case ) for image in image_inputs: self.assertIsInstance(snake_case , torch.Tensor ) # Test not batched input A_ : Any = image_processor(image_inputs[0] , ["semantic"] , return_tensors="pt" ).pixel_values A_ , A_ : Tuple = self.image_processing_tester.get_expected_values(snake_case ) self.assertEqual( encoded_images.shape , (1, self.image_processing_tester.num_channels, expected_height, expected_width) , ) # Test batched A_ , A_ : Tuple = self.image_processing_tester.get_expected_values(snake_case , batched=snake_case ) A_ : Any = image_processor( snake_case , ["semantic"] * len(snake_case ) , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processing_tester.batch_size, self.image_processing_tester.num_channels, expected_height, expected_width, ) , ) def SCREAMING_SNAKE_CASE ( self :Optional[Any] , snake_case :Dict=False , snake_case :str=False , snake_case :Dict="np" ): '''simple docstring''' A_ : Tuple = self.image_processing_class(**self.image_processor_dict ) # prepare image and target A_ : Tuple = self.image_processing_tester.num_labels A_ : str = None A_ : Tuple = None A_ : Tuple = prepare_image_inputs(self.image_processing_tester , equal_resolution=snake_case ) if with_segmentation_maps: A_ : List[str] = num_labels if is_instance_map: A_ : List[str] = list(range(snake_case ) ) * 2 A_ : int = dict(enumerate(snake_case ) ) A_ : List[str] = [ np.random.randint(0 , high * 2 , (img.size[1], img.size[0]) ).astype(np.uinta ) for img in image_inputs ] if segmentation_type == "pil": A_ : int = [Image.fromarray(snake_case ) for annotation in annotations] A_ : List[str] = image_processor( snake_case , ["semantic"] * len(snake_case ) , snake_case , return_tensors="pt" , instance_id_to_semantic_id=snake_case , pad_and_return_pixel_mask=snake_case , ) return inputs def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' pass def SCREAMING_SNAKE_CASE ( self :Optional[int] ): '''simple docstring''' def common(snake_case :Dict=False , snake_case :Optional[int]=None ): A_ : Tuple = self.comm_get_image_processor_inputs( with_segmentation_maps=snake_case , is_instance_map=snake_case , segmentation_type=snake_case ) A_ : Optional[Any] = inputs["mask_labels"] A_ : List[Any] = inputs["class_labels"] A_ : Optional[Any] = inputs["pixel_values"] A_ : int = inputs["text_inputs"] # check the batch_size for mask_label, class_label, text_input in zip(snake_case , snake_case , snake_case ): self.assertEqual(mask_label.shape[0] , class_label.shape[0] ) # this ensure padding has happened self.assertEqual(mask_label.shape[1:] , pixel_values.shape[2:] ) self.assertEqual(len(snake_case ) , self.image_processing_tester.num_text ) common() common(is_instance_map=snake_case ) common(is_instance_map=snake_case , segmentation_type="pil" ) common(is_instance_map=snake_case , segmentation_type="pil" ) def SCREAMING_SNAKE_CASE ( self :Optional[Any] ): '''simple docstring''' A_ : Any = np.zeros((20, 50) ) A_ : List[str] = 1 A_ : int = 1 A_ : Optional[Any] = 1 A_ : Any = binary_mask_to_rle(snake_case ) self.assertEqual(len(snake_case ) , 4 ) self.assertEqual(rle[0] , 21 ) self.assertEqual(rle[1] , 45 ) def SCREAMING_SNAKE_CASE ( self :Optional[int] ): '''simple docstring''' A_ : Union[str, Any] = self.image_processing_class( num_labels=self.image_processing_tester.num_classes , max_seq_length=77 , task_seq_length=77 , class_info_file="ade20k_panoptic.json" , num_text=self.image_processing_tester.num_text , repo_path="shi-labs/oneformer_demo" , ) A_ : Any = self.image_processing_tester.get_fake_oneformer_outputs() A_ : int = fature_extractor.post_process_semantic_segmentation(snake_case ) self.assertEqual(len(snake_case ) , self.image_processing_tester.batch_size ) self.assertEqual( segmentation[0].shape , ( self.image_processing_tester.height, self.image_processing_tester.width, ) , ) A_ : Optional[int] = [(1, 4) for i in range(self.image_processing_tester.batch_size )] A_ : List[Any] = fature_extractor.post_process_semantic_segmentation(snake_case , target_sizes=snake_case ) self.assertEqual(segmentation[0].shape , target_sizes[0] ) def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' A_ : List[str] = self.image_processing_class( num_labels=self.image_processing_tester.num_classes , max_seq_length=77 , task_seq_length=77 , class_info_file="ade20k_panoptic.json" , num_text=self.image_processing_tester.num_text , repo_path="shi-labs/oneformer_demo" , ) A_ : str = self.image_processing_tester.get_fake_oneformer_outputs() A_ : Optional[Any] = image_processor.post_process_instance_segmentation(snake_case , threshold=0 ) self.assertTrue(len(snake_case ) == self.image_processing_tester.batch_size ) for el in segmentation: self.assertTrue("segmentation" in el ) self.assertTrue("segments_info" in el ) self.assertEqual(type(el["segments_info"] ) , snake_case ) self.assertEqual( el["segmentation"].shape , (self.image_processing_tester.height, self.image_processing_tester.width) ) def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' A_ : Tuple = self.image_processing_class( num_labels=self.image_processing_tester.num_classes , max_seq_length=77 , task_seq_length=77 , class_info_file="ade20k_panoptic.json" , num_text=self.image_processing_tester.num_text , repo_path="shi-labs/oneformer_demo" , ) A_ : List[Any] = self.image_processing_tester.get_fake_oneformer_outputs() A_ : Optional[Any] = image_processor.post_process_panoptic_segmentation(snake_case , threshold=0 ) self.assertTrue(len(snake_case ) == self.image_processing_tester.batch_size ) for el in segmentation: self.assertTrue("segmentation" in el ) self.assertTrue("segments_info" in el ) self.assertEqual(type(el["segments_info"] ) , snake_case ) self.assertEqual( el["segmentation"].shape , (self.image_processing_tester.height, self.image_processing_tester.width) )
300
1
import unittest import numpy as np from diffusers import OnnxStableDiffusionInpaintPipelineLegacy from diffusers.utils.testing_utils import ( is_onnx_available, load_image, load_numpy, nightly, require_onnxruntime, require_torch_gpu, ) if is_onnx_available(): import onnxruntime as ort @nightly @require_onnxruntime @require_torch_gpu class __magic_name__ ( unittest.TestCase ): """simple docstring""" @property def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' return ( "CUDAExecutionProvider", { "gpu_mem_limit": "15000000000", # 15GB "arena_extend_strategy": "kSameAsRequested", }, ) @property def SCREAMING_SNAKE_CASE ( self :int ): '''simple docstring''' A_ : Tuple = ort.SessionOptions() A_ : Optional[int] = False return options def SCREAMING_SNAKE_CASE ( self :Optional[int] ): '''simple docstring''' A_ : int = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/in_paint/overture-creations-5sI6fQgYIuo.png" ) A_ : List[Any] = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/in_paint/overture-creations-5sI6fQgYIuo_mask.png" ) A_ : Union[str, Any] = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/in_paint/red_cat_sitting_on_a_park_bench_onnx.npy" ) # using the PNDM scheduler by default A_ : List[str] = OnnxStableDiffusionInpaintPipelineLegacy.from_pretrained( "CompVis/stable-diffusion-v1-4" , revision="onnx" , safety_checker=snake_case , feature_extractor=snake_case , provider=self.gpu_provider , sess_options=self.gpu_options , ) pipe.set_progress_bar_config(disable=snake_case ) A_ : int = "A red cat sitting on a park bench" A_ : Any = np.random.RandomState(0 ) A_ : Dict = pipe( prompt=snake_case , image=snake_case , mask_image=snake_case , strength=0.75 , guidance_scale=7.5 , num_inference_steps=15 , generator=snake_case , output_type="np" , ) A_ : str = output.images[0] assert image.shape == (512, 512, 3) assert np.abs(expected_image - image ).max() < 1e-2
300
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 _lowerCAmelCase : List[str] = logging.get_logger(__name__) _lowerCAmelCase : Optional[Any] = { '''facebook/data2vec-vision-base-ft''': ( '''https://huggingface.co/facebook/data2vec-vision-base-ft/resolve/main/config.json''' ), } class __magic_name__ ( lowerCamelCase__ ): """simple docstring""" __UpperCamelCase = '''data2vec-vision''' def __init__( self :int , snake_case :Optional[int]=768 , snake_case :Any=12 , snake_case :Any=12 , snake_case :Tuple=3_072 , snake_case :Any="gelu" , snake_case :Tuple=0.0 , snake_case :int=0.0 , snake_case :Any=0.02 , snake_case :str=1e-12 , snake_case :List[str]=224 , snake_case :Dict=16 , snake_case :int=3 , snake_case :int=False , snake_case :str=False , snake_case :List[Any]=False , snake_case :Optional[Any]=False , snake_case :Tuple=0.1 , snake_case :Optional[Any]=0.1 , snake_case :Any=True , snake_case :Optional[Any]=[3, 5, 7, 11] , snake_case :Dict=[1, 2, 3, 6] , snake_case :int=True , snake_case :List[Any]=0.4 , snake_case :Any=256 , snake_case :Union[str, Any]=1 , snake_case :Union[str, Any]=False , snake_case :Any=255 , **snake_case :int , ): '''simple docstring''' super().__init__(**snake_case ) A_ : Dict = hidden_size A_ : Tuple = num_hidden_layers A_ : List[str] = num_attention_heads A_ : Any = intermediate_size A_ : Optional[Any] = hidden_act A_ : Any = hidden_dropout_prob A_ : List[str] = attention_probs_dropout_prob A_ : Optional[Any] = initializer_range A_ : List[str] = layer_norm_eps A_ : str = image_size A_ : Optional[int] = patch_size A_ : int = num_channels A_ : Optional[Any] = use_mask_token A_ : Optional[Any] = use_absolute_position_embeddings A_ : Optional[int] = use_relative_position_bias A_ : Dict = use_shared_relative_position_bias A_ : Any = layer_scale_init_value A_ : Optional[Any] = drop_path_rate A_ : Dict = use_mean_pooling # decode head attributes (semantic segmentation) A_ : Tuple = out_indices A_ : Optional[Any] = pool_scales # auxiliary head attributes (semantic segmentation) A_ : str = use_auxiliary_head A_ : List[Any] = auxiliary_loss_weight A_ : List[str] = auxiliary_channels A_ : Dict = auxiliary_num_convs A_ : List[str] = auxiliary_concat_input A_ : Optional[int] = semantic_loss_ignore_index class __magic_name__ ( lowerCamelCase__ ): """simple docstring""" __UpperCamelCase = version.parse('''1.11''' ) @property def SCREAMING_SNAKE_CASE ( self :Union[str, Any] ): '''simple docstring''' return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ] ) @property def SCREAMING_SNAKE_CASE ( self :Tuple ): '''simple docstring''' return 1e-4
300
1
from __future__ import annotations def __snake_case ( _lowerCAmelCase : list[float] ) -> bool: if len(_lowerCAmelCase ) < 2: raise ValueError("Monogons and Digons are not polygons in the Euclidean space" ) if any(i <= 0 for i in nums ): raise ValueError("All values must be greater than 0" ) A_ : List[str] = nums.copy() copy_nums.sort() return copy_nums[-1] < sum(copy_nums[:-1] ) if __name__ == "__main__": import doctest doctest.testmod()
300
from typing import List, Optional, Union import numpy as np from ....audio_utils import mel_filter_bank, optimal_fft_length, spectrogram, window_function from ....feature_extraction_sequence_utils import SequenceFeatureExtractor from ....feature_extraction_utils import BatchFeature from ....file_utils import PaddingStrategy, TensorType from ....utils import logging _lowerCAmelCase : str = logging.get_logger(__name__) class __magic_name__ ( lowerCamelCase__ ): """simple docstring""" __UpperCamelCase = ['''input_features''', '''attention_mask'''] def __init__( self :int , snake_case :int=80 , snake_case :Optional[int]=16_000 , snake_case :Tuple=0.0 , snake_case :Optional[int]=10 , snake_case :Optional[Any]=25 , snake_case :Dict="hamming_window" , snake_case :Tuple=32768.0 , snake_case :str=0.97 , snake_case :List[str]=1.0 , snake_case :Dict=True , snake_case :str=True , snake_case :Optional[Any]=False , **snake_case :Union[str, Any] , ): '''simple docstring''' super().__init__(feature_size=snake_case , sampling_rate=snake_case , padding_value=snake_case , **snake_case ) A_ : Union[str, Any] = feature_size A_ : int = sampling_rate A_ : str = padding_value A_ : int = hop_length A_ : List[str] = win_length A_ : Any = frame_signal_scale A_ : str = preemphasis_coeff A_ : List[str] = mel_floor A_ : str = normalize_means A_ : Any = normalize_vars A_ : Optional[Any] = win_function A_ : Dict = return_attention_mask A_ : List[str] = win_length * sampling_rate // 1_000 A_ : List[str] = hop_length * sampling_rate // 1_000 A_ : List[str] = optimal_fft_length(self.sample_size ) A_ : str = (self.n_fft // 2) + 1 def SCREAMING_SNAKE_CASE ( self :Any , snake_case :np.array ): '''simple docstring''' if self.win_function == "hamming_window": A_ : Dict = window_function(window_length=self.sample_size , name=self.win_function , periodic=snake_case ) else: A_ : List[str] = window_function(window_length=self.sample_size , name=self.win_function ) A_ : Optional[int] = mel_filter_bank( num_frequency_bins=self.n_freqs , num_mel_filters=self.feature_size , min_frequency=0.0 , max_frequency=self.sampling_rate / 2.0 , sampling_rate=self.sampling_rate , ) A_ : Tuple = spectrogram( one_waveform * self.frame_signal_scale , window=snake_case , frame_length=self.sample_size , hop_length=self.sample_stride , fft_length=self.n_fft , center=snake_case , preemphasis=self.preemphasis_coeff , mel_filters=snake_case , mel_floor=self.mel_floor , log_mel="log" , ) return msfc_features.T def SCREAMING_SNAKE_CASE ( self :int , snake_case :Any , snake_case :Union[str, Any] , snake_case :str ): '''simple docstring''' if self.normalize_means: A_ : int = x[:input_length].mean(axis=0 ) A_ : Any = np.subtract(snake_case , snake_case ) if self.normalize_vars: A_ : List[Any] = x[:input_length].std(axis=0 ) A_ : Optional[int] = np.divide(snake_case , snake_case ) if input_length < x.shape[0]: A_ : Optional[int] = padding_value # make sure array is in float32 A_ : Union[str, Any] = x.astype(np.floataa ) return x def SCREAMING_SNAKE_CASE ( self :int , snake_case :List[np.ndarray] , snake_case :Optional[np.ndarray] = None ): '''simple docstring''' A_ : str = attention_mask.sum(-1 ) if attention_mask is not None else [x.shape[0] for x in input_features] return [self._normalize_one(snake_case , snake_case , self.padding_value ) for x, n in zip(snake_case , snake_case )] def __call__( self :int , snake_case :Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] , snake_case :Union[bool, str, PaddingStrategy] = False , snake_case :Optional[int] = None , snake_case :bool = False , snake_case :Optional[int] = None , snake_case :Optional[bool] = None , snake_case :Optional[Union[str, TensorType]] = None , snake_case :Optional[int] = None , **snake_case :Dict , ): '''simple docstring''' if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( f"The model corresponding to this feature extractor: {self} was trained using a sampling rate of" f" {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled with" f" {self.sampling_rate} and not {sampling_rate}." ) else: logger.warning( "It is strongly recommended to pass the ``sampling_rate`` argument to this function. " "Failing to do so can result in silent errors that might be hard to debug." ) A_ : Optional[int] = isinstance(snake_case , np.ndarray ) and len(raw_speech.shape ) > 1 if is_batched_numpy and len(raw_speech.shape ) > 2: raise ValueError(f"Only mono-channel audio is supported for input to {self}" ) A_ : Optional[Any] = is_batched_numpy or ( isinstance(snake_case , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: A_ : List[Any] = [np.asarray(snake_case , dtype=np.floataa ) for speech in raw_speech] elif not is_batched and not isinstance(snake_case , np.ndarray ): A_ : int = np.asarray(snake_case , dtype=np.floataa ) elif isinstance(snake_case , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): A_ : Optional[int] = raw_speech.astype(np.floataa ) # always return batch if not is_batched: A_ : Tuple = [raw_speech] # extract fbank features A_ : int = [self._extract_mfsc_features(snake_case ) for one_waveform in raw_speech] # convert into correct format for padding A_ : Union[str, Any] = BatchFeature({"input_features": features} ) A_ : str = self.pad( snake_case , padding=snake_case , max_length=snake_case , truncation=snake_case , pad_to_multiple_of=snake_case , return_attention_mask=snake_case , **snake_case , ) # make sure list is in array format A_ : Optional[int] = padded_inputs.get("input_features" ) if isinstance(input_features[0] , snake_case ): A_ : Union[str, Any] = [np.asarray(snake_case , dtype=np.floataa ) for feature in input_features] A_ : Dict = padded_inputs.get("attention_mask" ) if attention_mask is not None: A_ : Any = [np.asarray(snake_case , dtype=np.intaa ) for array in attention_mask] if self.normalize_means or self.normalize_vars: A_ : Dict = ( np.array(snake_case , dtype=np.intaa ) if self._get_padding_strategies(snake_case , max_length=snake_case ) is not PaddingStrategy.DO_NOT_PAD and padding else None ) A_ : Optional[int] = self.normalize( padded_inputs["input_features"] , attention_mask=snake_case ) if return_tensors is not None: A_ : Dict = padded_inputs.convert_to_tensors(snake_case ) return padded_inputs
300
1
import unittest from transformers import DebertaVaTokenizer, DebertaVaTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin _lowerCAmelCase : Any = get_tests_dir('''fixtures/spiece.model''') @require_sentencepiece @require_tokenizers class __magic_name__ ( lowerCamelCase__ , unittest.TestCase ): """simple docstring""" __UpperCamelCase = DebertaVaTokenizer __UpperCamelCase = DebertaVaTokenizerFast __UpperCamelCase = True __UpperCamelCase = True def SCREAMING_SNAKE_CASE ( self :List[Any] ): '''simple docstring''' super().setUp() # We have a SentencePiece fixture for testing A_ : Dict = DebertaVaTokenizer(snake_case , unk_token="<unk>" ) tokenizer.save_pretrained(self.tmpdirname ) def SCREAMING_SNAKE_CASE ( self :Dict , snake_case :Tuple ): '''simple docstring''' A_ : Optional[Any] = "this is a test" A_ : List[str] = "this is a test" return input_text, output_text def SCREAMING_SNAKE_CASE ( self :Tuple ): '''simple docstring''' A_ : str = "<pad>" A_ : List[Any] = 0 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 SCREAMING_SNAKE_CASE ( self :Dict ): '''simple docstring''' A_ : str = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , "<pad>" ) self.assertEqual(vocab_keys[1] , "<unk>" ) self.assertEqual(vocab_keys[-1] , "[PAD]" ) self.assertEqual(len(snake_case ) , 30_001 ) def SCREAMING_SNAKE_CASE ( self :Dict ): '''simple docstring''' self.assertEqual(self.get_tokenizer().vocab_size , 30_000 ) def SCREAMING_SNAKE_CASE ( self :Union[str, Any] ): '''simple docstring''' A_ : Optional[Any] = " \tHeLLo!how \n Are yoU? " A_ : str = ["▁hello", "!", "how", "▁are", "▁you", "?"] # fmt: on A_ : int = DebertaVaTokenizer(snake_case , do_lower_case=snake_case ) A_ : Optional[int] = tokenizer.convert_ids_to_tokens(tokenizer.encode(snake_case , add_special_tokens=snake_case ) ) self.assertListEqual(snake_case , snake_case ) A_ : Any = DebertaVaTokenizerFast(snake_case , do_lower_case=snake_case ) A_ : Any = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(snake_case , add_special_tokens=snake_case ) ) self.assertListEqual(snake_case , snake_case ) @unittest.skip("There is an inconsistency between slow and fast tokenizer due to a bug in the fast one." ) def SCREAMING_SNAKE_CASE ( self :Tuple ): '''simple docstring''' pass @unittest.skip("There is an inconsistency between slow and fast tokenizer due to a bug in the fast one." ) def SCREAMING_SNAKE_CASE ( self :Tuple ): '''simple docstring''' pass def SCREAMING_SNAKE_CASE ( self :int ): '''simple docstring''' A_ : Dict = "I was born in 92000, and this is falsé." A_ : Optional[Any] = ["▁", "<unk>", "▁was", "▁born", "▁in", "▁9", "2000", "▁", ",", "▁and", "▁this", "▁is", "▁fal", "s", "<unk>", "▁", ".", ] # fmt: on A_ : Optional[Any] = DebertaVaTokenizer(snake_case , split_by_punct=snake_case ) A_ : List[str] = tokenizer.convert_ids_to_tokens(tokenizer.encode(snake_case , add_special_tokens=snake_case ) ) self.assertListEqual(snake_case , snake_case ) A_ : Any = DebertaVaTokenizerFast(snake_case , split_by_punct=snake_case ) A_ : Dict = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(snake_case , add_special_tokens=snake_case ) ) self.assertListEqual(snake_case , snake_case ) def SCREAMING_SNAKE_CASE ( self :Tuple ): '''simple docstring''' A_ : List[str] = "I was born in 92000, and this is falsé." A_ : str = ["▁i", "▁was", "▁born", "▁in", "▁9", "2000", "▁", ",", "▁and", "▁this", "▁is", "▁fal", "s", "<unk>", "▁", ".", ] # fmt: on A_ : List[str] = DebertaVaTokenizer(snake_case , do_lower_case=snake_case , split_by_punct=snake_case ) A_ : List[Any] = tokenizer.convert_ids_to_tokens(tokenizer.encode(snake_case , add_special_tokens=snake_case ) ) self.assertListEqual(snake_case , snake_case ) A_ : int = DebertaVaTokenizerFast(snake_case , do_lower_case=snake_case , split_by_punct=snake_case ) A_ : Dict = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(snake_case , add_special_tokens=snake_case ) ) self.assertListEqual(snake_case , snake_case ) def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' A_ : List[str] = "I was born in 92000, and this is falsé." A_ : int = ["▁i", "▁was", "▁born", "▁in", "▁9", "2000", ",", "▁and", "▁this", "▁is", "▁fal", "s", "<unk>", ".", ] # fmt: on A_ : str = DebertaVaTokenizer(snake_case , do_lower_case=snake_case , split_by_punct=snake_case ) A_ : Any = tokenizer.convert_ids_to_tokens(tokenizer.encode(snake_case , add_special_tokens=snake_case ) ) self.assertListEqual(snake_case , snake_case ) A_ : Dict = DebertaVaTokenizerFast(snake_case , do_lower_case=snake_case , split_by_punct=snake_case ) A_ : Tuple = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(snake_case , add_special_tokens=snake_case ) ) self.assertListEqual(snake_case , snake_case ) def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' A_ : Any = "I was born in 92000, and this is falsé." A_ : Dict = ["▁", "<unk>", "▁was", "▁born", "▁in", "▁9", "2000", "▁", ",", "▁and", "▁this", "▁is", "▁fal", "s", "<unk>", "▁", ".", ] # fmt: on A_ : List[str] = DebertaVaTokenizer(snake_case , do_lower_case=snake_case , split_by_punct=snake_case ) A_ : Any = tokenizer.convert_ids_to_tokens(tokenizer.encode(snake_case , add_special_tokens=snake_case ) ) self.assertListEqual(snake_case , snake_case ) A_ : int = DebertaVaTokenizerFast(snake_case , do_lower_case=snake_case , split_by_punct=snake_case ) A_ : int = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(snake_case , add_special_tokens=snake_case ) ) self.assertListEqual(snake_case , snake_case ) def SCREAMING_SNAKE_CASE ( self :Tuple ): '''simple docstring''' A_ : int = " \tHeLLo!how \n Are yoU? " A_ : Any = ["▁", "<unk>", "e", "<unk>", "o", "!", "how", "▁", "<unk>", "re", "▁yo", "<unk>", "?"] # fmt: on A_ : Dict = DebertaVaTokenizer(snake_case , do_lower_case=snake_case , split_by_punct=snake_case ) A_ : Any = tokenizer.convert_ids_to_tokens(tokenizer.encode(snake_case , add_special_tokens=snake_case ) ) self.assertListEqual(snake_case , snake_case ) A_ : Any = DebertaVaTokenizerFast(snake_case , do_lower_case=snake_case , split_by_punct=snake_case ) A_ : Tuple = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(snake_case , add_special_tokens=snake_case ) ) self.assertListEqual(snake_case , snake_case ) def SCREAMING_SNAKE_CASE ( self :Union[str, Any] ): '''simple docstring''' A_ : Any = self.get_tokenizer() A_ : Dict = self.get_rust_tokenizer() A_ : int = "I was born in 92000, and this is falsé." A_ : int = tokenizer.convert_ids_to_tokens(tokenizer.encode(snake_case , add_special_tokens=snake_case ) ) A_ : Optional[int] = rust_tokenizer.convert_ids_to_tokens(rust_tokenizer.encode(snake_case , add_special_tokens=snake_case ) ) self.assertListEqual(snake_case , snake_case ) A_ : int = tokenizer.encode(snake_case , add_special_tokens=snake_case ) A_ : Optional[int] = rust_tokenizer.encode(snake_case , add_special_tokens=snake_case ) self.assertListEqual(snake_case , snake_case ) A_ : int = self.get_rust_tokenizer() A_ : List[Any] = tokenizer.encode(snake_case ) A_ : int = rust_tokenizer.encode(snake_case ) self.assertListEqual(snake_case , snake_case ) def SCREAMING_SNAKE_CASE ( self :Tuple ): '''simple docstring''' A_ : Dict = "This is a test" A_ : int = [13, 1, 4_398, 25, 21, 1_289] A_ : List[str] = ["▁", "T", "his", "▁is", "▁a", "▁test"] A_ : Tuple = ["▁", "<unk>", "his", "▁is", "▁a", "▁test"] A_ : Tuple = DebertaVaTokenizer(snake_case , keep_accents=snake_case ) A_ : Optional[int] = DebertaVaTokenizerFast(snake_case , keep_accents=snake_case ) A_ : str = tokenizer.encode(snake_case , add_special_tokens=snake_case ) self.assertListEqual(snake_case , snake_case ) A_ : int = tokenizer.tokenize(snake_case ) self.assertListEqual(snake_case , snake_case ) A_ : int = tokenizer.convert_ids_to_tokens(snake_case ) self.assertListEqual(snake_case , snake_case ) A_ : int = rust_tokenizer.encode(snake_case , add_special_tokens=snake_case ) self.assertListEqual(snake_case , snake_case ) A_ : Dict = rust_tokenizer.tokenize(snake_case ) self.assertListEqual(snake_case , snake_case ) A_ : List[str] = rust_tokenizer.convert_ids_to_tokens(snake_case ) self.assertListEqual(snake_case , snake_case ) # fmt: off A_ : int = "I was born in 92000, and this is falsé." A_ : Any = [13, 1, 23, 386, 19, 561, 3_050, 15, 17, 48, 25, 8_256, 18, 1, 9] A_ : int = ["▁", "I", "▁was", "▁born", "▁in", "▁9", "2000", ",", "▁and", "▁this", "▁is", "▁fal", "s", "é", ".", ] A_ : Dict = ["▁", "<unk>", "▁was", "▁born", "▁in", "▁9", "2000", ",", "▁and", "▁this", "▁is", "▁fal", "s", "<unk>", ".", ] # fmt: on A_ : Dict = tokenizer.encode(snake_case , add_special_tokens=snake_case ) self.assertListEqual(snake_case , snake_case ) A_ : str = tokenizer.tokenize(snake_case ) self.assertListEqual(snake_case , snake_case ) A_ : List[str] = tokenizer.convert_ids_to_tokens(snake_case ) self.assertListEqual(snake_case , snake_case ) A_ : int = rust_tokenizer.encode(snake_case , add_special_tokens=snake_case ) self.assertListEqual(snake_case , snake_case ) A_ : Optional[int] = rust_tokenizer.tokenize(snake_case ) self.assertListEqual(snake_case , snake_case ) A_ : Optional[int] = rust_tokenizer.convert_ids_to_tokens(snake_case ) self.assertListEqual(snake_case , snake_case ) def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' A_ : Optional[Any] = DebertaVaTokenizer(snake_case ) A_ : List[Any] = tokenizer.encode("sequence builders" ) A_ : int = tokenizer.encode("multi-sequence build" ) A_ : Optional[int] = tokenizer.build_inputs_with_special_tokens(snake_case ) A_ : Any = tokenizer.build_inputs_with_special_tokens(snake_case , snake_case ) self.assertEqual([tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] , snake_case ) self.assertEqual( [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] + text_a + [tokenizer.sep_token_id] , snake_case , ) @slow def SCREAMING_SNAKE_CASE ( self :Union[str, Any] ): '''simple docstring''' A_ : Optional[int] = {"input_ids": [[1, 39_867, 36, 19_390, 486, 27, 35_052, 81_436, 18, 60_685, 1_225, 7, 35_052, 81_436, 18, 9_367, 16_899, 18, 15_937, 53, 594, 773, 18, 16_287, 30_465, 36, 15_937, 6, 41_139, 38, 36_979, 60_763, 191, 6, 34_132, 99, 6, 50_538, 390, 43_230, 6, 34_132, 2_779, 20_850, 14, 699, 1_072, 1_194, 36, 382, 10_901, 53, 7, 699, 1_072, 2_084, 36, 20_422, 630, 53, 19, 105, 3_049, 1_896, 1_053, 16_899, 1_506, 11, 37_978, 4_243, 7, 1_237, 31_869, 200, 16_566, 654, 6, 35_052, 81_436, 7, 55_630, 13_593, 4, 2], [1, 26, 15_011, 13, 667, 8, 1_053, 18, 23_611, 1_237, 72_356, 12_820, 34, 104_134, 1_209, 35, 13_313, 6_627, 21, 202, 347, 7, 164, 2_399, 11, 46, 4_485, 4, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 5, 1_232, 2_864, 15_785, 14_951, 105, 5, 8_581, 1_250, 4, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], "token_type_ids": [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], "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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=snake_case , model_name="microsoft/deberta-v2-xlarge" , revision="ad6e42c1532ddf3a15c39246b63f5559d558b670" , )
300
from typing import Optional import numpy as np import torch from torch import nn from transformers import GPTaConfig, GPTaLMHeadModel from transformers.modeling_utils import ModuleUtilsMixin from ...configuration_utils import ConfigMixin, register_to_config from ...models import ModelMixin class __magic_name__ ( lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ): """simple docstring""" __UpperCamelCase = [r'''h\.\d+\.attn\.bias''', r'''h\.\d+\.attn\.masked_bias'''] @register_to_config def __init__( self :List[Any] , snake_case :int , snake_case :int , snake_case :Optional[int] = None , snake_case :int = 50_257 , snake_case :int = 1_024 , snake_case :int = 768 , snake_case :int = 12 , snake_case :int = 12 , snake_case :Optional[int] = None , snake_case :str = "gelu_new" , snake_case :float = 0.1 , snake_case :float = 0.1 , snake_case :float = 0.1 , snake_case :float = 1e-5 , snake_case :float = 0.02 , snake_case :bool = True , snake_case :bool = True , snake_case :bool = False , snake_case :bool = False , ): '''simple docstring''' super().__init__() A_ : Tuple = prefix_length if prefix_inner_dim != n_embd and prefix_hidden_dim is None: raise ValueError( f"`prefix_hidden_dim` cannot be `None` when `prefix_inner_dim`: {prefix_hidden_dim} and" f" `n_embd`: {n_embd} are not equal." ) A_ : List[Any] = prefix_inner_dim A_ : Union[str, Any] = prefix_hidden_dim A_ : List[str] = ( nn.Linear(self.prefix_inner_dim , self.prefix_hidden_dim ) if self.prefix_hidden_dim is not None else nn.Identity() ) A_ : List[Any] = ( nn.Linear(self.prefix_hidden_dim , snake_case ) if self.prefix_hidden_dim is not None else nn.Identity() ) A_ : List[Any] = GPTaConfig( vocab_size=snake_case , n_positions=snake_case , n_embd=snake_case , n_layer=snake_case , n_head=snake_case , n_inner=snake_case , activation_function=snake_case , resid_pdrop=snake_case , embd_pdrop=snake_case , attn_pdrop=snake_case , layer_norm_epsilon=snake_case , initializer_range=snake_case , scale_attn_weights=snake_case , use_cache=snake_case , scale_attn_by_inverse_layer_idx=snake_case , reorder_and_upcast_attn=snake_case , ) A_ : Optional[Any] = GPTaLMHeadModel(snake_case ) def SCREAMING_SNAKE_CASE ( self :Tuple , snake_case :torch.Tensor , snake_case :torch.Tensor , snake_case :Optional[torch.Tensor] = None , snake_case :Optional[torch.Tensor] = None , ): '''simple docstring''' A_ : Any = self.transformer.transformer.wte(snake_case ) A_ : str = self.encode_prefix(snake_case ) A_ : Union[str, Any] = self.decode_prefix(snake_case ) A_ : int = torch.cat((prefix_embeds, embedding_text) , dim=1 ) if labels is not None: A_ : Dict = self.get_dummy_token(input_ids.shape[0] , input_ids.device ) A_ : int = torch.cat((dummy_token, input_ids) , dim=1 ) A_ : Union[str, Any] = self.transformer(inputs_embeds=snake_case , labels=snake_case , attention_mask=snake_case ) if self.prefix_hidden_dim is not None: return out, hidden else: return out def SCREAMING_SNAKE_CASE ( self :str , snake_case :int , snake_case :torch.device ): '''simple docstring''' return torch.zeros(snake_case , self.prefix_length , dtype=torch.intaa , device=snake_case ) def SCREAMING_SNAKE_CASE ( self :Optional[int] , snake_case :int ): '''simple docstring''' return self.encode_prefix(snake_case ) @torch.no_grad() def SCREAMING_SNAKE_CASE ( self :List[Any] , snake_case :Dict , snake_case :Optional[int] , snake_case :Any ): '''simple docstring''' A_ : Any = torch.split(snake_case , 1 , dim=0 ) A_ : Optional[int] = [] A_ : Union[str, Any] = [] for feature in features: A_ : Tuple = self.decode_prefix(feature.to(snake_case ) ) # back to the clip feature # Only support beam search for now A_ , A_ : Dict = self.generate_beam( input_embeds=snake_case , device=snake_case , eos_token_id=snake_case ) generated_tokens.append(output_tokens[0] ) generated_seq_lengths.append(seq_lengths[0] ) A_ : int = torch.stack(snake_case ) A_ : int = torch.stack(snake_case ) return generated_tokens, generated_seq_lengths @torch.no_grad() def SCREAMING_SNAKE_CASE ( self :Union[str, Any] , snake_case :int=None , snake_case :str=None , snake_case :int=None , snake_case :int = 5 , snake_case :int = 67 , snake_case :float = 1.0 , snake_case :Optional[int] = None , ): '''simple docstring''' A_ : Optional[Any] = eos_token_id A_ : List[Any] = None A_ : List[Any] = None A_ : str = torch.ones(snake_case , device=snake_case , dtype=torch.int ) A_ : Any = torch.zeros(snake_case , device=snake_case , dtype=torch.bool ) if input_embeds is not None: A_ : Any = input_embeds else: A_ : Optional[Any] = self.transformer.transformer.wte(snake_case ) for i in range(snake_case ): A_ : Optional[Any] = self.transformer(inputs_embeds=snake_case ) A_ : str = outputs.logits A_ : int = logits[:, -1, :] / (temperature if temperature > 0 else 1.0) A_ : List[str] = logits.softmax(-1 ).log() if scores is None: A_ , A_ : Union[str, Any] = logits.topk(snake_case , -1 ) A_ : Tuple = generated.expand(snake_case , *generated.shape[1:] ) A_ , A_ : str = next_tokens.permute(1 , 0 ), scores.squeeze(0 ) if tokens is None: A_ : Union[str, Any] = next_tokens else: A_ : List[str] = tokens.expand(snake_case , *tokens.shape[1:] ) A_ : Union[str, Any] = torch.cat((tokens, next_tokens) , dim=1 ) else: A_ : List[str] = -float(np.inf ) A_ : List[Any] = 0 A_ : Union[str, Any] = scores[:, None] + logits seq_lengths[~is_stopped] += 1 A_ : Optional[Any] = scores_sum / seq_lengths[:, None] A_ , A_ : List[str] = scores_sum_average.view(-1 ).topk(snake_case , -1 ) A_ : str = next_tokens // scores_sum.shape[1] A_ : Union[str, Any] = seq_lengths[next_tokens_source] A_ : Optional[int] = next_tokens % scores_sum.shape[1] A_ : Tuple = next_tokens.unsqueeze(1 ) A_ : Tuple = tokens[next_tokens_source] A_ : Dict = torch.cat((tokens, next_tokens) , dim=1 ) A_ : Dict = generated[next_tokens_source] A_ : Union[str, Any] = scores_sum_average * seq_lengths A_ : Optional[int] = is_stopped[next_tokens_source] A_ : Tuple = self.transformer.transformer.wte(next_tokens.squeeze() ).view(generated.shape[0] , 1 , -1 ) A_ : Union[str, Any] = torch.cat((generated, next_token_embed) , dim=1 ) A_ : Any = is_stopped + next_tokens.eq(snake_case ).squeeze() if is_stopped.all(): break A_ : int = scores / seq_lengths A_ : str = scores.argsort(descending=snake_case ) # tokens tensors are already padded to max_seq_length A_ : Dict = [tokens[i] for i in order] A_ : int = torch.stack(snake_case , dim=0 ) A_ : List[Any] = torch.tensor([seq_lengths[i] for i in order] , dtype=seq_lengths.dtype ) return output_texts, seq_lengths
300
1
import torch from diffusers import DDPMScheduler from .test_schedulers import SchedulerCommonTest class __magic_name__ ( lowerCamelCase__ ): """simple docstring""" __UpperCamelCase = (DDPMScheduler,) def SCREAMING_SNAKE_CASE ( self :Union[str, Any] , **snake_case :str ): '''simple docstring''' A_ : Dict = { "num_train_timesteps": 1_000, "beta_start": 0.0001, "beta_end": 0.02, "beta_schedule": "linear", "variance_type": "fixed_small", "clip_sample": True, } config.update(**snake_case ) return config def SCREAMING_SNAKE_CASE ( self :int ): '''simple docstring''' for timesteps in [1, 5, 100, 1_000]: self.check_over_configs(num_train_timesteps=snake_case ) def SCREAMING_SNAKE_CASE ( self :Optional[Any] ): '''simple docstring''' for beta_start, beta_end in zip([0.0001, 0.001, 0.01, 0.1] , [0.002, 0.02, 0.2, 2] ): self.check_over_configs(beta_start=snake_case , beta_end=snake_case ) def SCREAMING_SNAKE_CASE ( self :int ): '''simple docstring''' for schedule in ["linear", "squaredcos_cap_v2"]: self.check_over_configs(beta_schedule=snake_case ) def SCREAMING_SNAKE_CASE ( self :List[Any] ): '''simple docstring''' for variance in ["fixed_small", "fixed_large", "other"]: self.check_over_configs(variance_type=snake_case ) def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' for clip_sample in [True, False]: self.check_over_configs(clip_sample=snake_case ) def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' self.check_over_configs(thresholding=snake_case ) for threshold in [0.5, 1.0, 2.0]: for prediction_type in ["epsilon", "sample", "v_prediction"]: self.check_over_configs( thresholding=snake_case , prediction_type=snake_case , sample_max_value=snake_case , ) def SCREAMING_SNAKE_CASE ( self :Optional[int] ): '''simple docstring''' for prediction_type in ["epsilon", "sample", "v_prediction"]: self.check_over_configs(prediction_type=snake_case ) def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' for t in [0, 500, 999]: self.check_over_forward(time_step=snake_case ) def SCREAMING_SNAKE_CASE ( self :Optional[Any] ): '''simple docstring''' A_ : Tuple = self.scheduler_classes[0] A_ : List[str] = self.get_scheduler_config() A_ : List[str] = scheduler_class(**snake_case ) assert torch.sum(torch.abs(scheduler._get_variance(0 ) - 0.0 ) ) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(487 ) - 0.00979 ) ) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(999 ) - 0.02 ) ) < 1e-5 def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' A_ : int = self.scheduler_classes[0] A_ : List[str] = self.get_scheduler_config() A_ : int = scheduler_class(**snake_case ) A_ : Tuple = len(snake_case ) A_ : List[str] = self.dummy_model() A_ : Optional[Any] = self.dummy_sample_deter A_ : List[str] = torch.manual_seed(0 ) for t in reversed(range(snake_case ) ): # 1. predict noise residual A_ : Tuple = model(snake_case , snake_case ) # 2. predict previous mean of sample x_t-1 A_ : Dict = scheduler.step(snake_case , snake_case , snake_case , generator=snake_case ).prev_sample # if t > 0: # noise = self.dummy_sample_deter # variance = scheduler.get_variance(t) ** (0.5) * noise # # sample = pred_prev_sample + variance A_ : Optional[int] = pred_prev_sample A_ : Tuple = torch.sum(torch.abs(snake_case ) ) A_ : str = torch.mean(torch.abs(snake_case ) ) assert abs(result_sum.item() - 258.9606 ) < 1e-2 assert abs(result_mean.item() - 0.3372 ) < 1e-3 def SCREAMING_SNAKE_CASE ( self :Dict ): '''simple docstring''' A_ : Optional[int] = self.scheduler_classes[0] A_ : int = self.get_scheduler_config(prediction_type="v_prediction" ) A_ : List[str] = scheduler_class(**snake_case ) A_ : int = len(snake_case ) A_ : Dict = self.dummy_model() A_ : str = self.dummy_sample_deter A_ : Any = torch.manual_seed(0 ) for t in reversed(range(snake_case ) ): # 1. predict noise residual A_ : Optional[int] = model(snake_case , snake_case ) # 2. predict previous mean of sample x_t-1 A_ : Tuple = scheduler.step(snake_case , snake_case , snake_case , generator=snake_case ).prev_sample # if t > 0: # noise = self.dummy_sample_deter # variance = scheduler.get_variance(t) ** (0.5) * noise # # sample = pred_prev_sample + variance A_ : List[str] = pred_prev_sample A_ : Optional[Any] = torch.sum(torch.abs(snake_case ) ) A_ : List[str] = torch.mean(torch.abs(snake_case ) ) assert abs(result_sum.item() - 202.0296 ) < 1e-2 assert abs(result_mean.item() - 0.2631 ) < 1e-3 def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' A_ : str = self.scheduler_classes[0] A_ : Optional[Any] = self.get_scheduler_config() A_ : Dict = scheduler_class(**snake_case ) A_ : Optional[int] = [100, 87, 50, 1, 0] scheduler.set_timesteps(timesteps=snake_case ) A_ : Optional[int] = scheduler.timesteps for i, timestep in enumerate(snake_case ): if i == len(snake_case ) - 1: A_ : str = -1 else: A_ : List[str] = timesteps[i + 1] A_ : Optional[int] = scheduler.previous_timestep(snake_case ) A_ : List[str] = prev_t.item() self.assertEqual(snake_case , snake_case ) def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' A_ : Optional[Any] = self.scheduler_classes[0] A_ : int = self.get_scheduler_config() A_ : Tuple = scheduler_class(**snake_case ) A_ : List[str] = [100, 87, 50, 51, 0] with self.assertRaises(snake_case , msg="`custom_timesteps` must be in descending order." ): scheduler.set_timesteps(timesteps=snake_case ) def SCREAMING_SNAKE_CASE ( self :List[Any] ): '''simple docstring''' A_ : Any = self.scheduler_classes[0] A_ : Union[str, Any] = self.get_scheduler_config() A_ : Optional[int] = scheduler_class(**snake_case ) A_ : Union[str, Any] = [100, 87, 50, 1, 0] A_ : Optional[int] = len(snake_case ) with self.assertRaises(snake_case , msg="Can only pass one of `num_inference_steps` or `custom_timesteps`." ): scheduler.set_timesteps(num_inference_steps=snake_case , timesteps=snake_case ) def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' A_ : Union[str, Any] = self.scheduler_classes[0] A_ : Optional[Any] = self.get_scheduler_config() A_ : Optional[int] = scheduler_class(**snake_case ) A_ : Optional[int] = [scheduler.config.num_train_timesteps] with self.assertRaises( snake_case , msg="`timesteps` must start before `self.config.train_timesteps`: {scheduler.config.num_train_timesteps}}" , ): scheduler.set_timesteps(timesteps=snake_case )
300
import warnings from ...utils import logging from .image_processing_yolos import YolosImageProcessor _lowerCAmelCase : Tuple = logging.get_logger(__name__) class __magic_name__ ( lowerCamelCase__ ): """simple docstring""" def __init__( self :Union[str, Any] , *snake_case :Tuple , **snake_case :Any ): '''simple docstring''' warnings.warn( "The class YolosFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please" " use YolosImageProcessor instead." , snake_case , ) super().__init__(*snake_case , **snake_case )
300
1
import math import numpy as np import qiskit from qiskit import Aer, ClassicalRegister, QuantumCircuit, QuantumRegister, execute def __snake_case ( _lowerCAmelCase : int = 3 ) -> qiskit.result.counts.Counts: if isinstance(_lowerCAmelCase , _lowerCAmelCase ): raise TypeError("number of qubits must be a integer." ) if number_of_qubits <= 0: raise ValueError("number of qubits must be > 0." ) if math.floor(_lowerCAmelCase ) != number_of_qubits: raise ValueError("number of qubits must be exact integer." ) if number_of_qubits > 10: raise ValueError("number of qubits too large to simulate(>10)." ) A_ : str = QuantumRegister(_lowerCAmelCase , "qr" ) A_ : Tuple = ClassicalRegister(_lowerCAmelCase , "cr" ) A_ : List[Any] = QuantumCircuit(_lowerCAmelCase , _lowerCAmelCase ) A_ : List[Any] = number_of_qubits for i in range(_lowerCAmelCase ): quantum_circuit.h(number_of_qubits - i - 1 ) counter -= 1 for j in range(_lowerCAmelCase ): quantum_circuit.cp(np.pi / 2 ** (counter - j) , _lowerCAmelCase , _lowerCAmelCase ) for k in range(number_of_qubits // 2 ): quantum_circuit.swap(_lowerCAmelCase , number_of_qubits - k - 1 ) # measure all the qubits quantum_circuit.measure(_lowerCAmelCase , _lowerCAmelCase ) # simulate with 10000 shots A_ : Dict = Aer.get_backend("qasm_simulator" ) A_ : Dict = execute(_lowerCAmelCase , _lowerCAmelCase , shots=10000 ) return job.result().get_counts(_lowerCAmelCase ) if __name__ == "__main__": print( F'''Total count for quantum fourier transform state is: \ {quantum_fourier_transform(3)}''' )
300
from __future__ import annotations def __snake_case ( _lowerCAmelCase : list[float] ) -> bool: if len(_lowerCAmelCase ) < 2: raise ValueError("Monogons and Digons are not polygons in the Euclidean space" ) if any(i <= 0 for i in nums ): raise ValueError("All values must be greater than 0" ) A_ : List[str] = nums.copy() copy_nums.sort() return copy_nums[-1] < sum(copy_nums[:-1] ) if __name__ == "__main__": import doctest doctest.testmod()
300
1
from itertools import count def __snake_case ( _lowerCAmelCase : int = 50 ) -> int: A_ : Optional[int] = [1] * min_block_length for n in count(_lowerCAmelCase ): fill_count_functions.append(1 ) for block_length in range(_lowerCAmelCase , n + 1 ): for block_start in range(n - block_length ): fill_count_functions[n] += fill_count_functions[ n - block_start - block_length - 1 ] fill_count_functions[n] += 1 if fill_count_functions[n] > 1000000: break return n if __name__ == "__main__": print(F'''{solution() = }''')
300
import inspect from typing import Callable, List, Optional, Union import torch from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer from diffusers import DiffusionPipeline from diffusers.models import AutoencoderKL, UNetaDConditionModel from diffusers.pipelines.stable_diffusion import StableDiffusionPipelineOutput from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker from diffusers.schedulers import DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler from diffusers.utils import logging _lowerCAmelCase : Any = logging.get_logger(__name__) # pylint: disable=invalid-name class __magic_name__ ( lowerCamelCase__ ): """simple docstring""" def __init__( self :Union[str, Any] , snake_case :AutoencoderKL , snake_case :CLIPTextModel , snake_case :CLIPTokenizer , snake_case :UNetaDConditionModel , snake_case :Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler] , snake_case :StableDiffusionSafetyChecker , snake_case :CLIPImageProcessor , ): '''simple docstring''' super().__init__() self.register_modules( vae=snake_case , text_encoder=snake_case , tokenizer=snake_case , unet=snake_case , scheduler=snake_case , safety_checker=snake_case , feature_extractor=snake_case , ) def SCREAMING_SNAKE_CASE ( self :List[Any] , snake_case :Optional[Union[str, int]] = "auto" ): '''simple docstring''' if slice_size == "auto": # half the attention head size is usually a good trade-off between # speed and memory A_ : int = self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(snake_case ) def SCREAMING_SNAKE_CASE ( self :Dict ): '''simple docstring''' self.enable_attention_slicing(snake_case ) @torch.no_grad() def __call__( self :Any , snake_case :Union[str, List[str]] , snake_case :int = 512 , snake_case :int = 512 , snake_case :int = 50 , snake_case :float = 7.5 , snake_case :Optional[Union[str, List[str]]] = None , snake_case :Optional[int] = 1 , snake_case :float = 0.0 , snake_case :Optional[torch.Generator] = None , snake_case :Optional[torch.FloatTensor] = None , snake_case :Optional[str] = "pil" , snake_case :bool = True , snake_case :Optional[Callable[[int, int, torch.FloatTensor], None]] = None , snake_case :int = 1 , snake_case :Optional[torch.FloatTensor] = None , **snake_case :Optional[Any] , ): '''simple docstring''' if isinstance(snake_case , snake_case ): A_ : Dict = 1 elif isinstance(snake_case , snake_case ): A_ : Optional[Any] = len(snake_case ) else: raise ValueError(f"`prompt` has to be of type `str` or `list` but is {type(snake_case )}" ) if height % 8 != 0 or width % 8 != 0: raise ValueError(f"`height` and `width` have to be divisible by 8 but are {height} and {width}." ) if (callback_steps is None) or ( callback_steps is not None and (not isinstance(snake_case , snake_case ) or callback_steps <= 0) ): raise ValueError( f"`callback_steps` has to be a positive integer but is {callback_steps} of type" f" {type(snake_case )}." ) # get prompt text embeddings A_ : int = self.tokenizer( snake_case , padding="max_length" , max_length=self.tokenizer.model_max_length , return_tensors="pt" , ) A_ : Dict = text_inputs.input_ids if text_input_ids.shape[-1] > self.tokenizer.model_max_length: A_ : Optional[int] = 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}" ) A_ : Tuple = text_input_ids[:, : self.tokenizer.model_max_length] if text_embeddings is None: A_ : Union[str, Any] = self.text_encoder(text_input_ids.to(self.device ) )[0] # duplicate text embeddings for each generation per prompt, using mps friendly method A_ , A_ , A_ : int = text_embeddings.shape A_ : List[str] = text_embeddings.repeat(1 , snake_case , 1 ) A_ : List[str] = text_embeddings.view(bs_embed * num_images_per_prompt , snake_case , -1 ) # here `guidance_scale` is defined analog to the guidance weight `w` of equation (2) # of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1` # corresponds to doing no classifier free guidance. A_ : Dict = guidance_scale > 1.0 # get unconditional embeddings for classifier free guidance if do_classifier_free_guidance: A_ : List[str] if negative_prompt is None: A_ : List[str] = [""] elif type(snake_case ) is not type(snake_case ): raise TypeError( f"`negative_prompt` should be the same type to `prompt`, but got {type(snake_case )} !=" f" {type(snake_case )}." ) elif isinstance(snake_case , snake_case ): A_ : Optional[Any] = [negative_prompt] elif batch_size != len(snake_case ): raise ValueError( f"`negative_prompt`: {negative_prompt} has batch size {len(snake_case )}, but `prompt`:" f" {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches" " the batch size of `prompt`." ) else: A_ : Any = negative_prompt A_ : Optional[int] = text_input_ids.shape[-1] A_ : Dict = self.tokenizer( snake_case , padding="max_length" , max_length=snake_case , truncation=snake_case , return_tensors="pt" , ) A_ : Any = self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # duplicate unconditional embeddings for each generation per prompt, using mps friendly method A_ : Tuple = uncond_embeddings.shape[1] A_ : Dict = uncond_embeddings.repeat(snake_case , snake_case , 1 ) A_ : Dict = uncond_embeddings.view(batch_size * num_images_per_prompt , snake_case , -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 A_ : Optional[int] = torch.cat([uncond_embeddings, text_embeddings] ) # get the initial random noise unless the user supplied it # Unlike in other pipelines, latents need to be generated in the target device # for 1-to-1 results reproducibility with the CompVis implementation. # However this currently doesn't work in `mps`. A_ : List[str] = (batch_size * num_images_per_prompt, self.unet.config.in_channels, height // 8, width // 8) A_ : str = (batch_size * num_images_per_prompt, self.unet.config.in_channels, 64, 64) A_ : List[Any] = text_embeddings.dtype if latents is None: if self.device.type == "mps": # randn does not exist on mps A_ : Tuple = torch.randn( snake_case , generator=snake_case , device="cpu" , dtype=snake_case ).to(self.device ) A_ : Optional[Any] = torch.randn(snake_case , generator=snake_case , device="cpu" , dtype=snake_case ).to( self.device ) else: A_ : int = torch.randn( snake_case , generator=snake_case , device=self.device , dtype=snake_case ) A_ : Optional[int] = torch.randn(snake_case , generator=snake_case , device=self.device , dtype=snake_case ) else: if latents_reference.shape != latents_shape: raise ValueError(f"Unexpected latents shape, got {latents.shape}, expected {latents_shape}" ) A_ : Tuple = latents_reference.to(self.device ) A_ : Any = latents.to(self.device ) # This is the key part of the pipeline where we # try to ensure that the generated images w/ the same seed # but different sizes actually result in similar images A_ : List[Any] = (latents_shape[3] - latents_shape_reference[3]) // 2 A_ : Optional[int] = (latents_shape[2] - latents_shape_reference[2]) // 2 A_ : Optional[int] = latents_shape_reference[3] if dx >= 0 else latents_shape_reference[3] + 2 * dx A_ : Dict = latents_shape_reference[2] if dy >= 0 else latents_shape_reference[2] + 2 * dy A_ : Optional[Any] = 0 if dx < 0 else dx A_ : Optional[Any] = 0 if dy < 0 else dy A_ : List[str] = max(-dx , 0 ) A_ : List[Any] = max(-dy , 0 ) # import pdb # pdb.set_trace() A_ : Any = latents_reference[:, :, dy : dy + h, dx : dx + w] # set timesteps self.scheduler.set_timesteps(snake_case ) # Some schedulers like PNDM have timesteps as arrays # It's more optimized to move all timesteps to correct device beforehand A_ : str = self.scheduler.timesteps.to(self.device ) # scale the initial noise by the standard deviation required by the scheduler A_ : Union[str, Any] = latents * self.scheduler.init_noise_sigma # prepare extra kwargs for the scheduler step, since not all schedulers have the same signature # eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers. # eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502 # and should be between [0, 1] A_ : Optional[int] = "eta" in set(inspect.signature(self.scheduler.step ).parameters.keys() ) A_ : List[str] = {} if accepts_eta: A_ : Union[str, Any] = eta for i, t in enumerate(self.progress_bar(snake_case ) ): # expand the latents if we are doing classifier free guidance A_ : Optional[Any] = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents A_ : Any = self.scheduler.scale_model_input(snake_case , snake_case ) # predict the noise residual A_ : List[str] = self.unet(snake_case , snake_case , encoder_hidden_states=snake_case ).sample # perform guidance if do_classifier_free_guidance: A_ , A_ : Dict = noise_pred.chunk(2 ) A_ : List[Any] = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) # compute the previous noisy sample x_t -> x_t-1 A_ : Tuple = self.scheduler.step(snake_case , snake_case , snake_case , **snake_case ).prev_sample # call the callback, if provided if callback is not None and i % callback_steps == 0: callback(snake_case , snake_case , snake_case ) A_ : List[str] = 1 / 0.18215 * latents A_ : Tuple = self.vae.decode(snake_case ).sample A_ : Dict = (image / 2 + 0.5).clamp(0 , 1 ) # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16 A_ : List[str] = image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() if self.safety_checker is not None: A_ : int = self.feature_extractor(self.numpy_to_pil(snake_case ) , return_tensors="pt" ).to( self.device ) A_ , A_ : List[str] = self.safety_checker( images=snake_case , clip_input=safety_checker_input.pixel_values.to(text_embeddings.dtype ) ) else: A_ : List[str] = None if output_type == "pil": A_ : Optional[int] = self.numpy_to_pil(snake_case ) if not return_dict: return (image, has_nsfw_concept) return StableDiffusionPipelineOutput(images=snake_case , nsfw_content_detected=snake_case )
300
1
import argparse import torch from transformers import MobileBertConfig, MobileBertForPreTraining, load_tf_weights_in_mobilebert from transformers.utils import logging logging.set_verbosity_info() def __snake_case ( _lowerCAmelCase : Any , _lowerCAmelCase : Optional[int] , _lowerCAmelCase : Tuple ) -> Tuple: # Initialise PyTorch model A_ : Dict = MobileBertConfig.from_json_file(_lowerCAmelCase ) print(f"Building PyTorch model from configuration: {config}" ) A_ : Dict = MobileBertForPreTraining(_lowerCAmelCase ) # Load weights from tf checkpoint A_ : int = load_tf_weights_in_mobilebert(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) # Save pytorch-model print(f"Save PyTorch model to {pytorch_dump_path}" ) torch.save(model.state_dict() , _lowerCAmelCase ) if __name__ == "__main__": _lowerCAmelCase : List[Any] = 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( '''--mobilebert_config_file''', default=None, type=str, required=True, help=( '''The config json file corresponding to the pre-trained MobileBERT 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 : Tuple = parser.parse_args() convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.mobilebert_config_file, args.pytorch_dump_path)
300
import torch import torch.nn as nn from transformers import CLIPConfig, CLIPVisionModel, PreTrainedModel from ...utils import logging _lowerCAmelCase : List[str] = logging.get_logger(__name__) def __snake_case ( _lowerCAmelCase : int , _lowerCAmelCase : Any ) -> Dict: A_ : Optional[Any] = nn.functional.normalize(_lowerCAmelCase ) A_ : List[str] = nn.functional.normalize(_lowerCAmelCase ) return torch.mm(_lowerCAmelCase , normalized_text_embeds.t() ) class __magic_name__ ( lowerCamelCase__ ): """simple docstring""" __UpperCamelCase = CLIPConfig __UpperCamelCase = ['''CLIPEncoderLayer'''] def __init__( self :int , snake_case :CLIPConfig ): '''simple docstring''' super().__init__(snake_case ) A_ : int = CLIPVisionModel(config.vision_config ) A_ : List[str] = nn.Linear(config.vision_config.hidden_size , config.projection_dim , bias=snake_case ) A_ : Tuple = nn.Parameter(torch.ones(17 , config.projection_dim ) , requires_grad=snake_case ) A_ : str = nn.Parameter(torch.ones(3 , config.projection_dim ) , requires_grad=snake_case ) A_ : List[str] = nn.Parameter(torch.ones(17 ) , requires_grad=snake_case ) A_ : int = nn.Parameter(torch.ones(3 ) , requires_grad=snake_case ) @torch.no_grad() def SCREAMING_SNAKE_CASE ( self :Union[str, Any] , snake_case :Dict , snake_case :Any ): '''simple docstring''' A_ : List[Any] = self.vision_model(snake_case )[1] # pooled_output A_ : List[Any] = self.visual_projection(snake_case ) # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16 A_ : Optional[Any] = cosine_distance(snake_case , self.special_care_embeds ).cpu().float().numpy() A_ : Tuple = cosine_distance(snake_case , self.concept_embeds ).cpu().float().numpy() A_ : Union[str, Any] = [] A_ : Any = image_embeds.shape[0] for i in range(snake_case ): A_ : Optional[int] = {"special_scores": {}, "special_care": [], "concept_scores": {}, "bad_concepts": []} # increase this value to create a stronger `nfsw` filter # at the cost of increasing the possibility of filtering benign images A_ : Optional[Any] = 0.0 for concept_idx in range(len(special_cos_dist[0] ) ): A_ : Optional[Any] = special_cos_dist[i][concept_idx] A_ : Tuple = self.special_care_embeds_weights[concept_idx].item() A_ : Union[str, Any] = round(concept_cos - concept_threshold + adjustment , 3 ) if result_img["special_scores"][concept_idx] > 0: result_img["special_care"].append({concept_idx, result_img["special_scores"][concept_idx]} ) A_ : Any = 0.01 for concept_idx in range(len(cos_dist[0] ) ): A_ : Tuple = cos_dist[i][concept_idx] A_ : Tuple = self.concept_embeds_weights[concept_idx].item() A_ : Tuple = round(concept_cos - concept_threshold + adjustment , 3 ) if result_img["concept_scores"][concept_idx] > 0: result_img["bad_concepts"].append(snake_case ) result.append(snake_case ) A_ : Any = [len(res["bad_concepts"] ) > 0 for res in result] return images, has_nsfw_concepts @torch.no_grad() def SCREAMING_SNAKE_CASE ( self :Union[str, Any] , snake_case :torch.FloatTensor , snake_case :torch.FloatTensor ): '''simple docstring''' A_ : List[str] = self.vision_model(snake_case )[1] # pooled_output A_ : int = self.visual_projection(snake_case ) A_ : Tuple = cosine_distance(snake_case , self.special_care_embeds ) A_ : Tuple = cosine_distance(snake_case , self.concept_embeds ) # increase this value to create a stronger `nsfw` filter # at the cost of increasing the possibility of filtering benign images A_ : Optional[Any] = 0.0 A_ : Tuple = special_cos_dist - self.special_care_embeds_weights + adjustment # special_scores = special_scores.round(decimals=3) A_ : Optional[Any] = torch.any(special_scores > 0 , dim=1 ) A_ : Optional[Any] = special_care * 0.01 A_ : Optional[int] = special_adjustment.unsqueeze(1 ).expand(-1 , cos_dist.shape[1] ) A_ : Union[str, Any] = (cos_dist - self.concept_embeds_weights) + special_adjustment # concept_scores = concept_scores.round(decimals=3) A_ : Union[str, Any] = torch.any(concept_scores > 0 , dim=1 ) return images, has_nsfw_concepts
300
1
import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import CLIPTokenizer, CLIPTokenizerFast from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES from transformers.testing_utils import require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import CLIPSegProcessor, ViTImageProcessor @require_vision class __magic_name__ ( unittest.TestCase ): """simple docstring""" def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' A_ : Optional[Any] = tempfile.mkdtemp() # fmt: off A_ : str = ["l", "o", "w", "e", "r", "s", "t", "i", "d", "n", "lo", "l</w>", "w</w>", "r</w>", "t</w>", "low</w>", "er</w>", "lowest</w>", "newer</w>", "wider", "<unk>", "<|startoftext|>", "<|endoftext|>"] # fmt: on A_ : List[Any] = dict(zip(snake_case , range(len(snake_case ) ) ) ) A_ : Optional[Any] = ["#version: 0.2", "l o", "lo w</w>", "e r</w>", ""] A_ : Dict = {"unk_token": "<unk>"} A_ : Optional[Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["vocab_file"] ) A_ : List[Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["merges_file"] ) with open(self.vocab_file , "w" , encoding="utf-8" ) as fp: fp.write(json.dumps(snake_case ) + "\n" ) with open(self.merges_file , "w" , encoding="utf-8" ) as fp: fp.write("\n".join(snake_case ) ) A_ : Any = { "do_resize": True, "size": 20, "do_center_crop": True, "crop_size": 18, "do_normalize": True, "image_mean": [0.48145466, 0.4578275, 0.40821073], "image_std": [0.26862954, 0.26130258, 0.27577711], } A_ : Optional[Any] = os.path.join(self.tmpdirname , snake_case ) with open(self.image_processor_file , "w" , encoding="utf-8" ) as fp: json.dump(snake_case , snake_case ) def SCREAMING_SNAKE_CASE ( self :Optional[Any] , **snake_case :Tuple ): '''simple docstring''' return CLIPTokenizer.from_pretrained(self.tmpdirname , **snake_case ) def SCREAMING_SNAKE_CASE ( self :Dict , **snake_case :str ): '''simple docstring''' return CLIPTokenizerFast.from_pretrained(self.tmpdirname , **snake_case ) def SCREAMING_SNAKE_CASE ( self :List[Any] , **snake_case :List[Any] ): '''simple docstring''' return ViTImageProcessor.from_pretrained(self.tmpdirname , **snake_case ) def SCREAMING_SNAKE_CASE ( self :Optional[Any] ): '''simple docstring''' shutil.rmtree(self.tmpdirname ) def SCREAMING_SNAKE_CASE ( self :List[Any] ): '''simple docstring''' A_ : Dict = [np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta )] A_ : Optional[Any] = [Image.fromarray(np.moveaxis(snake_case , 0 , -1 ) ) for x in image_inputs] return image_inputs def SCREAMING_SNAKE_CASE ( self :Tuple ): '''simple docstring''' A_ : Optional[Any] = self.get_tokenizer() A_ : Tuple = self.get_rust_tokenizer() A_ : str = self.get_image_processor() A_ : Optional[Any] = CLIPSegProcessor(tokenizer=snake_case , image_processor=snake_case ) processor_slow.save_pretrained(self.tmpdirname ) A_ : str = CLIPSegProcessor.from_pretrained(self.tmpdirname , use_fast=snake_case ) A_ : Optional[Any] = CLIPSegProcessor(tokenizer=snake_case , image_processor=snake_case ) processor_fast.save_pretrained(self.tmpdirname ) A_ : List[Any] = CLIPSegProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor_slow.tokenizer.get_vocab() , tokenizer_slow.get_vocab() ) self.assertEqual(processor_fast.tokenizer.get_vocab() , tokenizer_fast.get_vocab() ) self.assertEqual(tokenizer_slow.get_vocab() , tokenizer_fast.get_vocab() ) self.assertIsInstance(processor_slow.tokenizer , snake_case ) self.assertIsInstance(processor_fast.tokenizer , snake_case ) self.assertEqual(processor_slow.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertEqual(processor_fast.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertIsInstance(processor_slow.image_processor , snake_case ) self.assertIsInstance(processor_fast.image_processor , snake_case ) def SCREAMING_SNAKE_CASE ( self :Union[str, Any] ): '''simple docstring''' A_ : List[Any] = CLIPSegProcessor(tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) A_ : Optional[int] = self.get_tokenizer(bos_token="(BOS)" , eos_token="(EOS)" ) A_ : Union[str, Any] = self.get_image_processor(do_normalize=snake_case , padding_value=1.0 ) A_ : Optional[int] = CLIPSegProcessor.from_pretrained( self.tmpdirname , bos_token="(BOS)" , eos_token="(EOS)" , do_normalize=snake_case , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , snake_case ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , snake_case ) def SCREAMING_SNAKE_CASE ( self :List[Any] ): '''simple docstring''' A_ : Union[str, Any] = self.get_image_processor() A_ : Union[str, Any] = self.get_tokenizer() A_ : Optional[Any] = CLIPSegProcessor(tokenizer=snake_case , image_processor=snake_case ) A_ : List[str] = self.prepare_image_inputs() A_ : str = image_processor(snake_case , return_tensors="np" ) A_ : Optional[int] = processor(images=snake_case , return_tensors="np" ) for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() , input_processor[key].sum() , delta=1e-2 ) def SCREAMING_SNAKE_CASE ( self :int ): '''simple docstring''' A_ : Optional[Any] = self.get_image_processor() A_ : str = self.get_tokenizer() A_ : List[str] = CLIPSegProcessor(tokenizer=snake_case , image_processor=snake_case ) A_ : Optional[int] = "lower newer" A_ : List[Any] = processor(text=snake_case ) A_ : int = tokenizer(snake_case ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' A_ : Optional[Any] = self.get_image_processor() A_ : Dict = self.get_tokenizer() A_ : int = CLIPSegProcessor(tokenizer=snake_case , image_processor=snake_case ) A_ : str = "lower newer" A_ : Optional[Any] = self.prepare_image_inputs() A_ : List[str] = processor(text=snake_case , images=snake_case ) self.assertListEqual(list(inputs.keys() ) , ["input_ids", "attention_mask", "pixel_values"] ) # test if it raises when no input is passed with pytest.raises(snake_case ): processor() def SCREAMING_SNAKE_CASE ( self :Union[str, Any] ): '''simple docstring''' A_ : Any = self.get_image_processor() A_ : int = self.get_tokenizer() A_ : Dict = CLIPSegProcessor(tokenizer=snake_case , image_processor=snake_case ) A_ : List[str] = self.prepare_image_inputs() A_ : Dict = self.prepare_image_inputs() A_ : int = processor(images=snake_case , visual_prompt=snake_case ) self.assertListEqual(list(inputs.keys() ) , ["pixel_values", "conditional_pixel_values"] ) # test if it raises when no input is passed with pytest.raises(snake_case ): processor() def SCREAMING_SNAKE_CASE ( self :Tuple ): '''simple docstring''' A_ : Tuple = self.get_image_processor() A_ : int = self.get_tokenizer() A_ : Dict = CLIPSegProcessor(tokenizer=snake_case , image_processor=snake_case ) A_ : Optional[Any] = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] A_ : Union[str, Any] = processor.batch_decode(snake_case ) A_ : List[str] = tokenizer.batch_decode(snake_case ) self.assertListEqual(snake_case , snake_case )
300
import argparse from pathlib import Path import requests import torch from PIL import Image from transformers import ( RobertaTokenizer, TrOCRConfig, TrOCRForCausalLM, TrOCRProcessor, VisionEncoderDecoderModel, ViTConfig, ViTImageProcessor, ViTModel, ) from transformers.utils import logging logging.set_verbosity_info() _lowerCAmelCase : List[str] = logging.get_logger(__name__) def __snake_case ( _lowerCAmelCase : Optional[int] , _lowerCAmelCase : Union[str, Any] ) -> Optional[int]: A_ : Tuple = [] 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 __snake_case ( _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : Union[str, Any] ) -> Dict: for i in range(encoder_config.num_hidden_layers ): # queries, keys and values (only weights, no biases) A_ : str = state_dict.pop(f"encoder.deit.blocks.{i}.attn.qkv.weight" ) A_ : List[Any] = in_proj_weight[ : encoder_config.hidden_size, : ] A_ : Optional[Any] = in_proj_weight[ encoder_config.hidden_size : encoder_config.hidden_size * 2, : ] A_ : Optional[Any] = in_proj_weight[ -encoder_config.hidden_size :, : ] def __snake_case ( _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : Tuple , _lowerCAmelCase : Dict ) -> Any: A_ : Dict = dct.pop(_lowerCAmelCase ) A_ : List[Any] = val def __snake_case ( _lowerCAmelCase : List[str] ) -> int: if "handwritten" in checkpoint_url: A_ : Any = "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: A_ : Any = "https://www.researchgate.net/profile/Dinh-Sang/publication/338099565/figure/fig8/AS:840413229350922@1577381536857/An-receipt-example-in-the-SROIE-2019-dataset_Q640.jpg" A_ : List[Any] = Image.open(requests.get(_lowerCAmelCase , stream=_lowerCAmelCase ).raw ).convert("RGB" ) return im @torch.no_grad() def __snake_case ( _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : Union[str, Any] ) -> List[Any]: A_ : Optional[Any] = ViTConfig(image_size=384 , qkv_bias=_lowerCAmelCase ) A_ : Tuple = TrOCRConfig() # size of the architecture if "base" in checkpoint_url: A_ : Tuple = 768 elif "large" in checkpoint_url: # use ViT-large encoder A_ : Optional[Any] = 1024 A_ : Union[str, Any] = 4096 A_ : Union[str, Any] = 24 A_ : List[Any] = 16 A_ : List[str] = 1024 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: A_ : Dict = False A_ : int = "relu" A_ : Optional[int] = 1024 A_ : Any = True A_ : List[Any] = False A_ : Optional[int] = False # load HuggingFace model A_ : Union[str, Any] = ViTModel(_lowerCAmelCase , add_pooling_layer=_lowerCAmelCase ) A_ : str = TrOCRForCausalLM(_lowerCAmelCase ) A_ : List[str] = VisionEncoderDecoderModel(encoder=_lowerCAmelCase , decoder=_lowerCAmelCase ) model.eval() # load state_dict of original model, rename some keys A_ : Optional[int] = torch.hub.load_state_dict_from_url(_lowerCAmelCase , map_location="cpu" , check_hash=_lowerCAmelCase )["model"] A_ : Dict = create_rename_keys(_lowerCAmelCase , _lowerCAmelCase ) for src, dest in rename_keys: rename_key(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) read_in_q_k_v(_lowerCAmelCase , _lowerCAmelCase ) # 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(): A_ : Dict = state_dict.pop(_lowerCAmelCase ) if key.startswith("decoder" ) and "output_projection" not in key: A_ : List[str] = val else: A_ : Optional[Any] = val # load state dict model.load_state_dict(_lowerCAmelCase ) # Check outputs on an image A_ : List[Any] = ViTImageProcessor(size=encoder_config.image_size ) A_ : Any = RobertaTokenizer.from_pretrained("roberta-large" ) A_ : Union[str, Any] = TrOCRProcessor(_lowerCAmelCase , _lowerCAmelCase ) A_ : List[str] = processor(images=prepare_img(_lowerCAmelCase ) , return_tensors="pt" ).pixel_values # verify logits A_ : Union[str, Any] = torch.tensor([[model.config.decoder.decoder_start_token_id]] ) A_ : Optional[int] = model(pixel_values=_lowerCAmelCase , decoder_input_ids=_lowerCAmelCase ) A_ : Tuple = outputs.logits A_ : Union[str, Any] = torch.Size([1, 1, 50265] ) if "trocr-base-handwritten" in checkpoint_url: A_ : Union[str, Any] = torch.tensor( [-1.45_02, -4.66_83, -0.53_47, -2.92_91, 9.14_35, -3.05_71, 8.97_64, 1.75_60, 8.73_58, -1.53_11] ) elif "trocr-large-handwritten" in checkpoint_url: A_ : str = torch.tensor( [-2.64_37, -1.31_29, -2.25_96, -5.34_55, 6.35_39, 1.76_04, 5.49_91, 1.47_02, 5.61_13, 2.01_70] ) elif "trocr-base-printed" in checkpoint_url: A_ : Optional[Any] = torch.tensor( [-5.68_16, -5.83_88, 1.13_98, -6.90_34, 6.85_05, -2.43_93, 1.22_84, -1.02_32, -1.96_61, -3.92_10] ) elif "trocr-large-printed" in checkpoint_url: A_ : Optional[int] = torch.tensor( [-6.01_62, -7.09_59, 4.41_55, -5.10_63, 7.04_68, -3.16_31, 2.64_66, -0.30_81, -0.81_06, -1.75_35] ) if "stage1" not in checkpoint_url: assert logits.shape == expected_shape, "Shape of logits not as expected" assert torch.allclose(logits[0, 0, :10] , _lowerCAmelCase , atol=1e-3 ), "First elements of logits not as expected" Path(_lowerCAmelCase ).mkdir(exist_ok=_lowerCAmelCase ) print(f"Saving model to {pytorch_dump_folder_path}" ) model.save_pretrained(_lowerCAmelCase ) print(f"Saving processor to {pytorch_dump_folder_path}" ) processor.save_pretrained(_lowerCAmelCase ) if __name__ == "__main__": _lowerCAmelCase : Dict = argparse.ArgumentParser() parser.add_argument( '''--checkpoint_url''', default='''https://layoutlm.blob.core.windows.net/trocr/model_zoo/fairseq/trocr-base-handwritten.pt''', type=str, help='''URL to the original PyTorch checkpoint (.pth file).''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the folder to output PyTorch model.''' ) _lowerCAmelCase : List[str] = parser.parse_args() convert_tr_ocr_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)
300
1
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 UpperCAmelCase__ = logging.getLogger(__name__) require_version("pytorch_lightning>=1.0.4") UpperCAmelCase__ = { "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 UpperCAmelCase__ = { "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 } UpperCAmelCase__ = sorted(arg_to_scheduler.keys()) UpperCAmelCase__ = "{" + ", ".join(arg_to_scheduler_choices) + "}" class lowercase_ ( pl.LightningModule ): '''simple docstring''' def __init__( self : str , __UpperCAmelCase : argparse.Namespace , __UpperCAmelCase : str=None , __UpperCAmelCase : List[str]="base" , __UpperCAmelCase : str=None , __UpperCAmelCase : int=None , __UpperCAmelCase : Optional[Any]=None , **__UpperCAmelCase : Optional[int] , ) ->Tuple: """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(__UpperCAmelCase ) a = 0 a = Path(self.hparams.output_dir ) a = self.hparams.cache_dir if self.hparams.cache_dir else None if config is None: a = 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=__UpperCAmelCase , **__UpperCAmelCase , ) else: a = config a = ('''encoder_layerdrop''', '''decoder_layerdrop''', '''dropout''', '''attention_dropout''') for p in extra_model_params: if getattr(self.hparams , __UpperCAmelCase , __UpperCAmelCase ): assert hasattr(self.config , __UpperCAmelCase ), F"""model config doesn't have a `{p}` attribute""" setattr(self.config , __UpperCAmelCase , getattr(self.hparams , __UpperCAmelCase ) ) if tokenizer is None: a = AutoTokenizer.from_pretrained( self.hparams.tokenizer_name if self.hparams.tokenizer_name else self.hparams.model_name_or_path , cache_dir=__UpperCAmelCase , ) else: a = tokenizer a = MODEL_MODES[mode] if model is None: a = 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=__UpperCAmelCase , ) else: a = model def __lowerCAmelCase ( self : Any , *__UpperCAmelCase : List[str] , **__UpperCAmelCase : str ) ->str: """simple docstring""" a = self.model_type.from_pretrained(*__UpperCAmelCase , **__UpperCAmelCase ) def __lowerCAmelCase ( self : List[str] ) ->Optional[Any]: """simple docstring""" a = arg_to_scheduler[self.hparams.lr_scheduler] a = get_schedule_func( self.opt , num_warmup_steps=self.hparams.warmup_steps , num_training_steps=self.total_steps() ) a = {'''scheduler''': scheduler, '''interval''': '''step''', '''frequency''': 1} return scheduler def __lowerCAmelCase ( self : List[Any] ) ->List[str]: """simple docstring""" a = self.model a = ['''bias''', '''LayerNorm.weight'''] a = [ { '''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: a = Adafactor( __UpperCAmelCase , lr=self.hparams.learning_rate , scale_parameter=__UpperCAmelCase , relative_step=__UpperCAmelCase ) else: a = AdamW( __UpperCAmelCase , lr=self.hparams.learning_rate , eps=self.hparams.adam_epsilon ) a = optimizer a = self.get_lr_scheduler() return [optimizer], [scheduler] def __lowerCAmelCase ( self : Union[str, Any] , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : Optional[Any] ) ->Dict: """simple docstring""" return self.validation_step(__UpperCAmelCase , __UpperCAmelCase ) def __lowerCAmelCase ( self : Tuple , __UpperCAmelCase : Optional[Any] ) ->Any: """simple docstring""" return self.validation_end(__UpperCAmelCase ) def __lowerCAmelCase ( self : Union[str, Any] ) ->int: """simple docstring""" a = max(1 , self.hparams.gpus ) # TODO: consider num_tpu_cores a = self.hparams.train_batch_size * self.hparams.accumulate_grad_batches * num_devices return (self.dataset_size / effective_batch_size) * self.hparams.max_epochs def __lowerCAmelCase ( self : List[Any] , __UpperCAmelCase : Dict ) ->Optional[int]: """simple docstring""" if stage == "test": a = len(self.test_dataloader().dataset ) else: a = self.get_dataloader('''train''' , self.hparams.train_batch_size , shuffle=__UpperCAmelCase ) a = len(self.train_dataloader().dataset ) def __lowerCAmelCase ( self : List[str] , __UpperCAmelCase : str , __UpperCAmelCase : int , __UpperCAmelCase : bool = False ) ->str: """simple docstring""" raise NotImplementedError('''You must implement this for your task''' ) def __lowerCAmelCase ( self : Union[str, Any] ) ->Optional[Any]: """simple docstring""" return self.train_loader def __lowerCAmelCase ( self : List[Any] ) ->List[Any]: """simple docstring""" return self.get_dataloader('''dev''' , self.hparams.eval_batch_size , shuffle=__UpperCAmelCase ) def __lowerCAmelCase ( self : Union[str, Any] ) ->Tuple: """simple docstring""" return self.get_dataloader('''test''' , self.hparams.eval_batch_size , shuffle=__UpperCAmelCase ) def __lowerCAmelCase ( self : List[Any] , __UpperCAmelCase : Optional[int] ) ->Optional[Any]: """simple docstring""" return os.path.join( self.hparams.data_dir , '''cached_{}_{}_{}'''.format( __UpperCAmelCase , list(filter(__UpperCAmelCase , self.hparams.model_name_or_path.split('''/''' ) ) ).pop() , str(self.hparams.max_seq_length ) , ) , ) @pl.utilities.rank_zero_only def __lowerCAmelCase ( self : Dict , __UpperCAmelCase : Dict[str, Any] ) ->None: """simple docstring""" a = self.output_dir.joinpath('''best_tfmr''' ) a = self.step_count self.model.save_pretrained(__UpperCAmelCase ) self.tokenizer.save_pretrained(__UpperCAmelCase ) @staticmethod def __lowerCAmelCase ( __UpperCAmelCase : Dict , __UpperCAmelCase : int ) ->int: """simple docstring""" parser.add_argument( '''--model_name_or_path''' , default=__UpperCAmelCase , type=__UpperCAmelCase , required=__UpperCAmelCase , help='''Path to pretrained model or model identifier from huggingface.co/models''' , ) parser.add_argument( '''--config_name''' , default='''''' , type=__UpperCAmelCase , help='''Pretrained config name or path if not the same as model_name''' ) parser.add_argument( '''--tokenizer_name''' , default=__UpperCAmelCase , type=__UpperCAmelCase , help='''Pretrained tokenizer name or path if not the same as model_name''' , ) parser.add_argument( '''--cache_dir''' , default=str(Path(__UpperCAmelCase ).parent / '''test_run''' / '''cache''' ) , type=__UpperCAmelCase , help='''Where do you want to store the pre-trained models downloaded from huggingface.co''' , ) parser.add_argument( '''--encoder_layerdrop''' , type=__UpperCAmelCase , help='''Encoder layer dropout probability (Optional). Goes into model.config''' , ) parser.add_argument( '''--decoder_layerdrop''' , type=__UpperCAmelCase , help='''Decoder layer dropout probability (Optional). Goes into model.config''' , ) parser.add_argument( '''--dropout''' , type=__UpperCAmelCase , help='''Dropout probability (Optional). Goes into model.config''' , ) parser.add_argument( '''--attention_dropout''' , type=__UpperCAmelCase , help='''Attention dropout probability (Optional). Goes into model.config''' , ) parser.add_argument('''--learning_rate''' , default=5e-5 , type=__UpperCAmelCase , help='''The initial learning rate for Adam.''' ) parser.add_argument( '''--lr_scheduler''' , default='''linear''' , choices=__UpperCAmelCase , metavar=__UpperCAmelCase , type=__UpperCAmelCase , help='''Learning rate scheduler''' , ) parser.add_argument('''--weight_decay''' , default=0.0 , type=__UpperCAmelCase , help='''Weight decay if we apply some.''' ) parser.add_argument('''--adam_epsilon''' , default=1e-8 , type=__UpperCAmelCase , help='''Epsilon for Adam optimizer.''' ) parser.add_argument('''--warmup_steps''' , default=0 , type=__UpperCAmelCase , help='''Linear warmup over warmup_steps.''' ) parser.add_argument('''--num_workers''' , default=4 , type=__UpperCAmelCase , help='''kwarg passed to DataLoader''' ) parser.add_argument('''--num_train_epochs''' , dest='''max_epochs''' , default=3 , type=__UpperCAmelCase ) parser.add_argument('''--train_batch_size''' , default=32 , type=__UpperCAmelCase ) parser.add_argument('''--eval_batch_size''' , default=32 , type=__UpperCAmelCase ) parser.add_argument('''--adafactor''' , action='''store_true''' ) class lowercase_ ( pl.Callback ): '''simple docstring''' def __lowerCAmelCase ( self : Tuple , __UpperCAmelCase : int , __UpperCAmelCase : Optional[int] ) ->int: """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 lowercase_ ( pl.Callback ): '''simple docstring''' def __lowerCAmelCase ( self : List[str] , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : List[Any] ) ->Union[str, Any]: """simple docstring""" for name, param in pl_module.model.rag.named_parameters(): if param.grad is None: print(__UpperCAmelCase ) class lowercase_ ( pl.Callback ): '''simple docstring''' def __lowerCAmelCase ( self : int , __UpperCAmelCase : Optional[int] , __UpperCAmelCase : Dict ) ->int: """simple docstring""" a = trainer.lr_schedulers[0]['''scheduler'''] a = {F"""lr_group_{i}""": lr for i, lr in enumerate(lr_scheduler.get_lr() )} pl_module.logger.log_metrics(__UpperCAmelCase ) def __lowerCAmelCase ( self : Any , __UpperCAmelCase : pl.Trainer , __UpperCAmelCase : pl.LightningModule ) ->Union[str, Any]: """simple docstring""" rank_zero_info('''***** Validation results *****''' ) a = trainer.callback_metrics # Log results for key in sorted(__UpperCAmelCase ): if key not in ["log", "progress_bar"]: rank_zero_info('''{} = {}\n'''.format(__UpperCAmelCase , str(metrics[key] ) ) ) def __lowerCAmelCase ( self : int , __UpperCAmelCase : pl.Trainer , __UpperCAmelCase : pl.LightningModule ) ->Optional[int]: """simple docstring""" rank_zero_info('''***** Test results *****''' ) a = trainer.callback_metrics # Log and save results to file a = os.path.join(pl_module.hparams.output_dir , '''test_results.txt''' ) with open(__UpperCAmelCase , '''w''' ) as writer: for key in sorted(__UpperCAmelCase ): if key not in ["log", "progress_bar"]: rank_zero_info('''{} = {}\n'''.format(__UpperCAmelCase , str(metrics[key] ) ) ) writer.write('''{} = {}\n'''.format(__UpperCAmelCase , str(metrics[key] ) ) ) def _a ( a :Union[str, Any] , a :int ) -> None: # 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 :Tuple=None , a :Any=True , a :List[str]=[] , a :List[Any]=None , a :Union[str, Any]=None , **a :Optional[Any] , ) -> List[str]: pl.seed_everything(args.seed ) # init model a = Path(model.hparams.output_dir ) odir.mkdir(exist_ok=a ) # add custom checkpoints if checkpoint_callback is None: a = 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: a = LoggingCallback() a = {} if args.fpaa: a = 16 if args.gpus > 1: a = '''auto''' a = '''ddp''' a = args.accumulate_grad_batches a = None a = '''auto''' a = 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
0
# DISCLAIMER: This file is strongly influenced by https://github.com/yang-song/score_sde_pytorch import math from dataclasses import dataclass from typing import Optional, Tuple, Union import torch from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput, randn_tensor from .scheduling_utils import SchedulerMixin, SchedulerOutput @dataclass class __magic_name__ ( lowerCamelCase__ ): """simple docstring""" __UpperCamelCase = 42 __UpperCamelCase = 42 class __magic_name__ ( lowerCamelCase__ , lowerCamelCase__ ): """simple docstring""" __UpperCamelCase = 1 @register_to_config def __init__( self :Union[str, Any] , snake_case :int = 2_000 , snake_case :float = 0.15 , snake_case :float = 0.01 , snake_case :float = 1348.0 , snake_case :float = 1e-5 , snake_case :int = 1 , ): '''simple docstring''' A_ : Dict = sigma_max # setable values A_ : List[Any] = None self.set_sigmas(snake_case , snake_case , snake_case , snake_case ) def SCREAMING_SNAKE_CASE ( self :Any , snake_case :torch.FloatTensor , snake_case :Optional[int] = None ): '''simple docstring''' return sample def SCREAMING_SNAKE_CASE ( self :Optional[Any] , snake_case :int , snake_case :float = None , snake_case :Union[str, torch.device] = None ): '''simple docstring''' A_ : Optional[Any] = sampling_eps if sampling_eps is not None else self.config.sampling_eps A_ : Tuple = torch.linspace(1 , snake_case , snake_case , device=snake_case ) def SCREAMING_SNAKE_CASE ( self :Dict , snake_case :int , snake_case :float = None , snake_case :float = None , snake_case :float = None ): '''simple docstring''' A_ : Union[str, Any] = sigma_min if sigma_min is not None else self.config.sigma_min A_ : Any = sigma_max if sigma_max is not None else self.config.sigma_max A_ : Dict = sampling_eps if sampling_eps is not None else self.config.sampling_eps if self.timesteps is None: self.set_timesteps(snake_case , snake_case ) A_ : str = sigma_min * (sigma_max / sigma_min) ** (self.timesteps / sampling_eps) A_ : Any = torch.exp(torch.linspace(math.log(snake_case ) , math.log(snake_case ) , snake_case ) ) A_ : str = torch.tensor([sigma_min * (sigma_max / sigma_min) ** t for t in self.timesteps] ) def SCREAMING_SNAKE_CASE ( self :List[str] , snake_case :List[str] , snake_case :Dict ): '''simple docstring''' return torch.where( timesteps == 0 , torch.zeros_like(t.to(timesteps.device ) ) , self.discrete_sigmas[timesteps - 1].to(timesteps.device ) , ) def SCREAMING_SNAKE_CASE ( self :Union[str, Any] , snake_case :torch.FloatTensor , snake_case :int , snake_case :torch.FloatTensor , snake_case :Optional[torch.Generator] = None , snake_case :bool = True , ): '''simple docstring''' if self.timesteps is None: raise ValueError( "`self.timesteps` is not set, you need to run 'set_timesteps' after creating the scheduler" ) A_ : int = timestep * torch.ones( sample.shape[0] , device=sample.device ) # torch.repeat_interleave(timestep, sample.shape[0]) A_ : Optional[Any] = (timestep * (len(self.timesteps ) - 1)).long() # mps requires indices to be in the same device, so we use cpu as is the default with cuda A_ : Dict = timesteps.to(self.discrete_sigmas.device ) A_ : Optional[int] = self.discrete_sigmas[timesteps].to(sample.device ) A_ : int = self.get_adjacent_sigma(snake_case , snake_case ).to(sample.device ) A_ : Union[str, Any] = torch.zeros_like(snake_case ) A_ : Tuple = (sigma**2 - adjacent_sigma**2) ** 0.5 # equation 6 in the paper: the model_output modeled by the network is grad_x log pt(x) # also equation 47 shows the analog from SDE models to ancestral sampling methods A_ : Optional[int] = diffusion.flatten() while len(diffusion.shape ) < len(sample.shape ): A_ : Tuple = diffusion.unsqueeze(-1 ) A_ : Optional[Any] = drift - diffusion**2 * model_output # equation 6: sample noise for the diffusion term of A_ : List[Any] = randn_tensor( sample.shape , layout=sample.layout , generator=snake_case , device=sample.device , dtype=sample.dtype ) A_ : List[Any] = sample - drift # subtract because `dt` is a small negative timestep # TODO is the variable diffusion the correct scaling term for the noise? A_ : Any = prev_sample_mean + diffusion * noise # add impact of diffusion field g if not return_dict: return (prev_sample, prev_sample_mean) return SdeVeOutput(prev_sample=snake_case , prev_sample_mean=snake_case ) def SCREAMING_SNAKE_CASE ( self :Tuple , snake_case :torch.FloatTensor , snake_case :torch.FloatTensor , snake_case :Optional[torch.Generator] = None , snake_case :bool = True , ): '''simple docstring''' if self.timesteps is None: raise ValueError( "`self.timesteps` is not set, you need to run 'set_timesteps' after creating the scheduler" ) # For small batch sizes, the paper "suggest replacing norm(z) with sqrt(d), where d is the dim. of z" # sample noise for correction A_ : Dict = randn_tensor(sample.shape , layout=sample.layout , generator=snake_case ).to(sample.device ) # compute step size from the model_output, the noise, and the snr A_ : int = torch.norm(model_output.reshape(model_output.shape[0] , -1 ) , dim=-1 ).mean() A_ : List[Any] = torch.norm(noise.reshape(noise.shape[0] , -1 ) , dim=-1 ).mean() A_ : Dict = (self.config.snr * noise_norm / grad_norm) ** 2 * 2 A_ : Dict = step_size * torch.ones(sample.shape[0] ).to(sample.device ) # self.repeat_scalar(step_size, sample.shape[0]) # compute corrected sample: model_output term and noise term A_ : int = step_size.flatten() while len(step_size.shape ) < len(sample.shape ): A_ : str = step_size.unsqueeze(-1 ) A_ : Optional[Any] = sample + step_size * model_output A_ : Tuple = prev_sample_mean + ((step_size * 2) ** 0.5) * noise if not return_dict: return (prev_sample,) return SchedulerOutput(prev_sample=snake_case ) def SCREAMING_SNAKE_CASE ( self :Tuple , snake_case :torch.FloatTensor , snake_case :torch.FloatTensor , snake_case :torch.FloatTensor , ): '''simple docstring''' A_ : Union[str, Any] = timesteps.to(original_samples.device ) A_ : List[Any] = self.discrete_sigmas.to(original_samples.device )[timesteps] A_ : List[Any] = ( noise * sigmas[:, None, None, None] if noise is not None else torch.randn_like(snake_case ) * sigmas[:, None, None, None] ) A_ : Optional[int] = noise + original_samples return noisy_samples def __len__( self :Union[str, Any] ): '''simple docstring''' return self.config.num_train_timesteps
300
0
'''simple docstring''' from .imports import is_tqdm_available if is_tqdm_available(): from tqdm.auto import tqdm as _tqdm from ..state import PartialState def lowerCAmelCase_ ( snake_case_ : bool = True , *snake_case_ : Union[str, Any] , **snake_case_ : int ) -> Tuple: '''simple docstring''' if not is_tqdm_available(): raise ImportError("Accelerate's `tqdm` module requires `tqdm` to be installed. Please run `pip install tqdm`." ) UpperCAmelCase_ = False if main_process_only: UpperCAmelCase_ = PartialState().local_process_index == 0 return _tqdm(*snake_case_ , **snake_case_ , disable=snake_case_ )
1
from __future__ import annotations from decimal import Decimal from math import * # noqa: F403 from sympy import diff def __snake_case ( _lowerCAmelCase : str , _lowerCAmelCase : float | Decimal , _lowerCAmelCase : float = 10**-10 ) -> float: A_ : Dict = a while True: A_ : Union[str, Any] = Decimal(_lowerCAmelCase ) - ( Decimal(eval(_lowerCAmelCase ) ) / Decimal(eval(str(diff(_lowerCAmelCase ) ) ) ) # noqa: S307 ) # This number dictates the accuracy of the answer if abs(eval(_lowerCAmelCase ) ) < precision: # noqa: S307 return float(_lowerCAmelCase ) # Let's Execute if __name__ == "__main__": # Find root of trigonometric function # Find value of pi print(F'''The root of sin(x) = 0 is {newton_raphson("sin(x)", 2)}''') # Find root of polynomial print(F'''The root of x**2 - 5*x + 2 = 0 is {newton_raphson("x**2 - 5*x + 2", 0.4)}''') # Find Square Root of 5 print(F'''The root of log(x) - 1 = 0 is {newton_raphson("log(x) - 1", 2)}''') # Exponential Roots print(F'''The root of exp(x) - 1 = 0 is {newton_raphson("exp(x) - 1", 0)}''')
300
0
'''simple docstring''' import unittest from transformers import AutoTokenizer, NystromformerConfig, is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( NystromformerForMaskedLM, NystromformerForMultipleChoice, NystromformerForQuestionAnswering, NystromformerForSequenceClassification, NystromformerForTokenClassification, NystromformerModel, ) from transformers.models.nystromformer.modeling_nystromformer import NYSTROMFORMER_PRETRAINED_MODEL_ARCHIVE_LIST class __lowerCAmelCase : '''simple docstring''' def __init__(self : Union[str, Any] , UpperCamelCase : Tuple , UpperCamelCase : List[str]=13 , UpperCamelCase : Optional[Any]=7 , UpperCamelCase : Optional[int]=True , UpperCamelCase : List[str]=True , UpperCamelCase : Optional[Any]=True , UpperCamelCase : str=True , UpperCamelCase : Dict=99 , UpperCamelCase : Optional[Any]=32 , UpperCamelCase : Dict=5 , UpperCamelCase : List[Any]=4 , UpperCamelCase : Optional[int]=37 , UpperCamelCase : str="gelu" , UpperCamelCase : List[Any]=0.1 , UpperCamelCase : Optional[Any]=0.1 , UpperCamelCase : Optional[Any]=512 , UpperCamelCase : Optional[Any]=16 , UpperCamelCase : str=2 , UpperCamelCase : Any=0.02 , UpperCamelCase : Tuple=3 , UpperCamelCase : Optional[Any]=4 , UpperCamelCase : Union[str, Any]=None , ): '''simple docstring''' lowercase__ = parent lowercase__ = batch_size lowercase__ = seq_length lowercase__ = is_training lowercase__ = use_input_mask lowercase__ = use_token_type_ids lowercase__ = use_labels lowercase__ = vocab_size lowercase__ = hidden_size lowercase__ = num_hidden_layers lowercase__ = num_attention_heads lowercase__ = intermediate_size lowercase__ = hidden_act lowercase__ = hidden_dropout_prob lowercase__ = attention_probs_dropout_prob lowercase__ = max_position_embeddings lowercase__ = type_vocab_size lowercase__ = type_sequence_label_size lowercase__ = initializer_range lowercase__ = num_labels lowercase__ = num_choices lowercase__ = scope def UpperCamelCase__ (self : str ): '''simple docstring''' lowercase__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) lowercase__ = None if self.use_input_mask: lowercase__ = random_attention_mask([self.batch_size, self.seq_length] ) lowercase__ = None if self.use_token_type_ids: lowercase__ = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) lowercase__ = None lowercase__ = None lowercase__ = None if self.use_labels: lowercase__ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) lowercase__ = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) lowercase__ = ids_tensor([self.batch_size] , self.num_choices ) lowercase__ = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def UpperCamelCase__ (self : str ): '''simple docstring''' return NystromformerConfig( 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=UpperCamelCase , initializer_range=self.initializer_range , ) def UpperCamelCase__ (self : Optional[int] , UpperCamelCase : List[str] , UpperCamelCase : Tuple , UpperCamelCase : Tuple , UpperCamelCase : Any , UpperCamelCase : Tuple , UpperCamelCase : List[str] , UpperCamelCase : int ): '''simple docstring''' lowercase__ = NystromformerModel(config=UpperCamelCase ) model.to(UpperCamelCase ) model.eval() lowercase__ = model(UpperCamelCase , attention_mask=UpperCamelCase , token_type_ids=UpperCamelCase ) lowercase__ = model(UpperCamelCase , token_type_ids=UpperCamelCase ) lowercase__ = model(UpperCamelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def UpperCamelCase__ (self : Tuple , UpperCamelCase : Any , UpperCamelCase : Optional[Any] , UpperCamelCase : Union[str, Any] , UpperCamelCase : Optional[Any] , UpperCamelCase : Any , UpperCamelCase : Optional[Any] , UpperCamelCase : Tuple ): '''simple docstring''' lowercase__ = NystromformerForMaskedLM(config=UpperCamelCase ) model.to(UpperCamelCase ) model.eval() lowercase__ = model(UpperCamelCase , attention_mask=UpperCamelCase , token_type_ids=UpperCamelCase , labels=UpperCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def UpperCamelCase__ (self : List[str] , UpperCamelCase : Any , UpperCamelCase : str , UpperCamelCase : Optional[int] , UpperCamelCase : str , UpperCamelCase : Union[str, Any] , UpperCamelCase : Optional[int] , UpperCamelCase : Optional[Any] ): '''simple docstring''' lowercase__ = NystromformerForQuestionAnswering(config=UpperCamelCase ) model.to(UpperCamelCase ) model.eval() lowercase__ = model( UpperCamelCase , attention_mask=UpperCamelCase , token_type_ids=UpperCamelCase , start_positions=UpperCamelCase , end_positions=UpperCamelCase , ) 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 UpperCamelCase__ (self : str , UpperCamelCase : str , UpperCamelCase : Union[str, Any] , UpperCamelCase : int , UpperCamelCase : List[Any] , UpperCamelCase : Optional[int] , UpperCamelCase : Union[str, Any] , UpperCamelCase : Optional[Any] ): '''simple docstring''' lowercase__ = self.num_labels lowercase__ = NystromformerForSequenceClassification(UpperCamelCase ) model.to(UpperCamelCase ) model.eval() lowercase__ = model(UpperCamelCase , attention_mask=UpperCamelCase , token_type_ids=UpperCamelCase , labels=UpperCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def UpperCamelCase__ (self : List[str] , UpperCamelCase : Union[str, Any] , UpperCamelCase : Union[str, Any] , UpperCamelCase : List[str] , UpperCamelCase : Union[str, Any] , UpperCamelCase : Union[str, Any] , UpperCamelCase : Tuple , UpperCamelCase : List[Any] ): '''simple docstring''' lowercase__ = self.num_labels lowercase__ = NystromformerForTokenClassification(config=UpperCamelCase ) model.to(UpperCamelCase ) model.eval() lowercase__ = model(UpperCamelCase , attention_mask=UpperCamelCase , token_type_ids=UpperCamelCase , labels=UpperCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def UpperCamelCase__ (self : List[Any] , UpperCamelCase : List[str] , UpperCamelCase : Dict , UpperCamelCase : Tuple , UpperCamelCase : int , UpperCamelCase : Any , UpperCamelCase : Union[str, Any] , UpperCamelCase : int ): '''simple docstring''' lowercase__ = self.num_choices lowercase__ = NystromformerForMultipleChoice(config=UpperCamelCase ) model.to(UpperCamelCase ) model.eval() lowercase__ = input_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() lowercase__ = token_type_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() lowercase__ = input_mask.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() lowercase__ = model( UpperCamelCase , attention_mask=UpperCamelCase , token_type_ids=UpperCamelCase , labels=UpperCamelCase , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def UpperCamelCase__ (self : str ): '''simple docstring''' lowercase__ = self.prepare_config_and_inputs() ( ( lowercase__ ) ,( lowercase__ ) ,( lowercase__ ) ,( lowercase__ ) ,( lowercase__ ) ,( lowercase__ ) ,( lowercase__ ) , ) = config_and_inputs lowercase__ = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_torch class __lowerCAmelCase (lowercase_ , lowercase_ , unittest.TestCase ): '''simple docstring''' lowerCAmelCase__ : Any = ( ( NystromformerModel, NystromformerForMaskedLM, NystromformerForMultipleChoice, NystromformerForQuestionAnswering, NystromformerForSequenceClassification, NystromformerForTokenClassification, ) if is_torch_available() else () ) lowerCAmelCase__ : Dict = ( { """feature-extraction""": NystromformerModel, """fill-mask""": NystromformerForMaskedLM, """question-answering""": NystromformerForQuestionAnswering, """text-classification""": NystromformerForSequenceClassification, """token-classification""": NystromformerForTokenClassification, """zero-shot""": NystromformerForSequenceClassification, } if is_torch_available() else {} ) lowerCAmelCase__ : Tuple = False lowerCAmelCase__ : Dict = False def UpperCamelCase__ (self : List[Any] ): '''simple docstring''' lowercase__ = NystromformerModelTester(self ) lowercase__ = ConfigTester(self , config_class=UpperCamelCase , hidden_size=37 ) def UpperCamelCase__ (self : List[str] ): '''simple docstring''' self.config_tester.run_common_tests() def UpperCamelCase__ (self : Tuple ): '''simple docstring''' lowercase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*UpperCamelCase ) def UpperCamelCase__ (self : int ): '''simple docstring''' lowercase__ = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: lowercase__ = type self.model_tester.create_and_check_model(*UpperCamelCase ) def UpperCamelCase__ (self : int ): '''simple docstring''' lowercase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*UpperCamelCase ) def UpperCamelCase__ (self : Optional[int] ): '''simple docstring''' lowercase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_multiple_choice(*UpperCamelCase ) def UpperCamelCase__ (self : Optional[Any] ): '''simple docstring''' lowercase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*UpperCamelCase ) def UpperCamelCase__ (self : Tuple ): '''simple docstring''' lowercase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*UpperCamelCase ) def UpperCamelCase__ (self : Dict ): '''simple docstring''' lowercase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*UpperCamelCase ) @slow def UpperCamelCase__ (self : List[str] ): '''simple docstring''' for model_name in NYSTROMFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowercase__ = NystromformerModel.from_pretrained(UpperCamelCase ) self.assertIsNotNone(UpperCamelCase ) @require_torch class __lowerCAmelCase (unittest.TestCase ): '''simple docstring''' @slow def UpperCamelCase__ (self : Union[str, Any] ): '''simple docstring''' lowercase__ = NystromformerModel.from_pretrained('''uw-madison/nystromformer-512''' ) lowercase__ = torch.tensor([[0, 1, 2, 3, 4, 5]] ) with torch.no_grad(): lowercase__ = model(UpperCamelCase )[0] lowercase__ = torch.Size((1, 6, 768) ) self.assertEqual(output.shape , UpperCamelCase ) lowercase__ = torch.tensor( [[[-0.45_32, -0.09_36, 0.51_37], [-0.26_76, 0.06_28, 0.61_86], [-0.36_29, -0.17_26, 0.47_16]]] ) self.assertTrue(torch.allclose(output[:, :3, :3] , UpperCamelCase , atol=1E-4 ) ) @slow def UpperCamelCase__ (self : Any ): '''simple docstring''' lowercase__ = '''the [MASK] of Belgium is Brussels''' lowercase__ = AutoTokenizer.from_pretrained('''uw-madison/nystromformer-512''' ) lowercase__ = NystromformerForMaskedLM.from_pretrained('''uw-madison/nystromformer-512''' ) lowercase__ = tokenizer(UpperCamelCase , return_tensors='''pt''' ) with torch.no_grad(): lowercase__ = model(encoding.input_ids ).logits lowercase__ = token_logits[:, 2, :].argmax(-1 )[0] self.assertEqual(tokenizer.decode(UpperCamelCase ) , '''capital''' )
2
import sacrebleu as scb from packaging import version from sacrebleu import TER import datasets _lowerCAmelCase : List[Any] = '''\ @inproceedings{snover-etal-2006-study, title = "A Study of Translation Edit Rate with Targeted Human Annotation", author = "Snover, Matthew and Dorr, Bonnie and Schwartz, Rich and Micciulla, Linnea and Makhoul, John", booktitle = "Proceedings of the 7th Conference of the Association for Machine Translation in the Americas: Technical Papers", month = aug # " 8-12", year = "2006", address = "Cambridge, Massachusetts, USA", publisher = "Association for Machine Translation in the Americas", url = "https://aclanthology.org/2006.amta-papers.25", pages = "223--231", } @inproceedings{post-2018-call, title = "A Call for Clarity in Reporting {BLEU} Scores", author = "Post, Matt", booktitle = "Proceedings of the Third Conference on Machine Translation: Research Papers", month = oct, year = "2018", address = "Belgium, Brussels", publisher = "Association for Computational Linguistics", url = "https://www.aclweb.org/anthology/W18-6319", pages = "186--191", } ''' _lowerCAmelCase : Union[str, Any] = '''\ TER (Translation Edit Rate, also called Translation Error Rate) is a metric to quantify the edit operations that a hypothesis requires to match a reference translation. We use the implementation that is already present in sacrebleu (https://github.com/mjpost/sacreBLEU#ter), which in turn is inspired by the TERCOM implementation, which can be found here: https://github.com/jhclark/tercom. The implementation here is slightly different from sacrebleu in terms of the required input format. The length of the references and hypotheses lists need to be the same, so you may need to transpose your references compared to sacrebleu\'s required input format. See https://github.com/huggingface/datasets/issues/3154#issuecomment-950746534 See the README.md file at https://github.com/mjpost/sacreBLEU#ter for more information. ''' _lowerCAmelCase : Optional[Any] = ''' Produces TER scores alongside the number of edits and reference length. Args: predictions (list of str): The system stream (a sequence of segments). references (list of list of str): A list of one or more reference streams (each a sequence of segments). normalized (boolean): If `True`, applies basic tokenization and normalization to sentences. Defaults to `False`. ignore_punct (boolean): If `True`, applies basic tokenization and normalization to sentences. Defaults to `False`. support_zh_ja_chars (boolean): If `True`, tokenization/normalization supports processing of Chinese characters, as well as Japanese Kanji, Hiragana, Katakana, and Phonetic Extensions of Katakana. Only applies if `normalized = True`. Defaults to `False`. case_sensitive (boolean): If `False`, makes all predictions and references lowercase to ignore differences in case. Defaults to `False`. Returns: \'score\' (float): TER score (num_edits / sum_ref_lengths * 100) \'num_edits\' (int): The cumulative number of edits \'ref_length\' (float): The cumulative average reference length Examples: Example 1: >>> predictions = ["does this sentence match??", ... "what about this sentence?", ... "What did the TER metric user say to the developer?"] >>> references = [["does this sentence match", "does this sentence match!?!"], ... ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"], ... ["Your jokes are...", "...TERrible"]] >>> ter = datasets.load_metric("ter") >>> results = ter.compute(predictions=predictions, ... references=references, ... case_sensitive=True) >>> print(results) {\'score\': 150.0, \'num_edits\': 15, \'ref_length\': 10.0} Example 2: >>> predictions = ["does this sentence match??", ... "what about this sentence?"] >>> references = [["does this sentence match", "does this sentence match!?!"], ... ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"]] >>> ter = datasets.load_metric("ter") >>> results = ter.compute(predictions=predictions, ... references=references, ... case_sensitive=True) >>> print(results) {\'score\': 62.5, \'num_edits\': 5, \'ref_length\': 8.0} Example 3: >>> predictions = ["does this sentence match??", ... "what about this sentence?"] >>> references = [["does this sentence match", "does this sentence match!?!"], ... ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"]] >>> ter = datasets.load_metric("ter") >>> results = ter.compute(predictions=predictions, ... references=references, ... normalized=True, ... case_sensitive=True) >>> print(results) {\'score\': 57.14285714285714, \'num_edits\': 6, \'ref_length\': 10.5} Example 4: >>> predictions = ["does this sentence match??", ... "what about this sentence?"] >>> references = [["does this sentence match", "does this sentence match!?!"], ... ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"]] >>> ter = datasets.load_metric("ter") >>> results = ter.compute(predictions=predictions, ... references=references, ... ignore_punct=True, ... case_sensitive=False) >>> print(results) {\'score\': 0.0, \'num_edits\': 0, \'ref_length\': 8.0} Example 5: >>> predictions = ["does this sentence match??", ... "what about this sentence?", ... "What did the TER metric user say to the developer?"] >>> references = [["does this sentence match", "does this sentence match!?!"], ... ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"], ... ["Your jokes are...", "...TERrible"]] >>> ter = datasets.load_metric("ter") >>> results = ter.compute(predictions=predictions, ... references=references, ... ignore_punct=True, ... case_sensitive=False) >>> print(results) {\'score\': 100.0, \'num_edits\': 10, \'ref_length\': 10.0} ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __magic_name__ ( datasets.Metric ): """simple docstring""" def SCREAMING_SNAKE_CASE ( self :Dict ): '''simple docstring''' if version.parse(scb.__version__ ) < version.parse("1.4.12" ): raise ImportWarning( "To use `sacrebleu`, the module `sacrebleu>=1.4.12` is required, and the current version of `sacrebleu` doesn't match this condition.\n" "You can install it with `pip install \"sacrebleu>=1.4.12\"`." ) return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , homepage="http://www.cs.umd.edu/~snover/tercom/" , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "predictions": datasets.Value("string" , id="sequence" ), "references": datasets.Sequence(datasets.Value("string" , id="sequence" ) , id="references" ), } ) , codebase_urls=["https://github.com/mjpost/sacreBLEU#ter"] , reference_urls=[ "https://github.com/jhclark/tercom", ] , ) def SCREAMING_SNAKE_CASE ( self :Union[str, Any] , snake_case :Optional[int] , snake_case :List[Any] , snake_case :bool = False , snake_case :bool = False , snake_case :bool = False , snake_case :bool = False , ): '''simple docstring''' A_ : List[str] = len(references[0] ) if any(len(snake_case ) != references_per_prediction for refs in references ): raise ValueError("Sacrebleu requires the same number of references for each prediction" ) A_ : int = [[refs[i] for refs in references] for i in range(snake_case )] A_ : Optional[Any] = TER( normalized=snake_case , no_punct=snake_case , asian_support=snake_case , case_sensitive=snake_case , ) A_ : List[Any] = sb_ter.corpus_score(snake_case , snake_case ) return {"score": output.score, "num_edits": output.num_edits, "ref_length": output.ref_length}
300
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) lowercase : Dict = { 'configuration_rembert': ['REMBERT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'RemBertConfig', 'RemBertOnnxConfig'] } try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase : Tuple = ['RemBertTokenizer'] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase : Dict = ['RemBertTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase : List[Any] = [ 'REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST', 'RemBertForCausalLM', 'RemBertForMaskedLM', 'RemBertForMultipleChoice', 'RemBertForQuestionAnswering', 'RemBertForSequenceClassification', 'RemBertForTokenClassification', 'RemBertLayer', 'RemBertModel', 'RemBertPreTrainedModel', 'load_tf_weights_in_rembert', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase : Optional[Any] = [ 'TF_REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFRemBertForCausalLM', 'TFRemBertForMaskedLM', 'TFRemBertForMultipleChoice', 'TFRemBertForQuestionAnswering', 'TFRemBertForSequenceClassification', 'TFRemBertForTokenClassification', 'TFRemBertLayer', 'TFRemBertModel', 'TFRemBertPreTrainedModel', ] if TYPE_CHECKING: from .configuration_rembert import REMBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, RemBertConfig, RemBertOnnxConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_rembert import RemBertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_rembert_fast import RemBertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_rembert import ( REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST, RemBertForCausalLM, RemBertForMaskedLM, RemBertForMultipleChoice, RemBertForQuestionAnswering, RemBertForSequenceClassification, RemBertForTokenClassification, RemBertLayer, RemBertModel, RemBertPreTrainedModel, load_tf_weights_in_rembert, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_rembert import ( TF_REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST, TFRemBertForCausalLM, TFRemBertForMaskedLM, TFRemBertForMultipleChoice, TFRemBertForQuestionAnswering, TFRemBertForSequenceClassification, TFRemBertForTokenClassification, TFRemBertLayer, TFRemBertModel, TFRemBertPreTrainedModel, ) else: import sys lowercase : int = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
3
def __snake_case ( _lowerCAmelCase : List[str] , _lowerCAmelCase : int ) -> str: return (pointa[0] - pointa[0]) ** 2 + (pointa[1] - pointa[1]) ** 2 def __snake_case ( _lowerCAmelCase : int , _lowerCAmelCase : Union[str, Any]=0 ) -> Any: return sorted(_lowerCAmelCase , key=lambda _lowerCAmelCase : x[column] ) def __snake_case ( _lowerCAmelCase : List[str] , _lowerCAmelCase : Any , _lowerCAmelCase : Any=float("inf" ) ) -> int: for i in range(points_counts - 1 ): for j in range(i + 1 , _lowerCAmelCase ): A_ : Tuple = euclidean_distance_sqr(points[i] , points[j] ) if current_dis < min_dis: A_ : Union[str, Any] = current_dis return min_dis def __snake_case ( _lowerCAmelCase : Optional[int] , _lowerCAmelCase : Dict , _lowerCAmelCase : List[str]=float("inf" ) ) -> Dict: for i in range(min(6 , points_counts - 1 ) , _lowerCAmelCase ): for j in range(max(0 , i - 6 ) , _lowerCAmelCase ): A_ : List[Any] = euclidean_distance_sqr(points[i] , points[j] ) if current_dis < min_dis: A_ : Union[str, Any] = current_dis return min_dis def __snake_case ( _lowerCAmelCase : List[Any] , _lowerCAmelCase : str , _lowerCAmelCase : Dict ) -> List[str]: # base case if points_counts <= 3: return dis_between_closest_pair(_lowerCAmelCase , _lowerCAmelCase ) # recursion A_ : Optional[int] = points_counts // 2 A_ : List[Any] = closest_pair_of_points_sqr( _lowerCAmelCase , points_sorted_on_y[:mid] , _lowerCAmelCase ) A_ : List[Any] = closest_pair_of_points_sqr( _lowerCAmelCase , points_sorted_on_y[mid:] , points_counts - mid ) A_ : Tuple = min(_lowerCAmelCase , _lowerCAmelCase ) A_ : Dict = [] for point in points_sorted_on_x: if abs(point[0] - points_sorted_on_x[mid][0] ) < closest_pair_dis: cross_strip.append(_lowerCAmelCase ) A_ : Tuple = dis_between_closest_in_strip( _lowerCAmelCase , len(_lowerCAmelCase ) , _lowerCAmelCase ) return min(_lowerCAmelCase , _lowerCAmelCase ) def __snake_case ( _lowerCAmelCase : str , _lowerCAmelCase : Optional[Any] ) -> Any: A_ : Optional[Any] = column_based_sort(_lowerCAmelCase , column=0 ) A_ : Optional[int] = column_based_sort(_lowerCAmelCase , column=1 ) return ( closest_pair_of_points_sqr( _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) ) ** 0.5 if __name__ == "__main__": _lowerCAmelCase : List[Any] = [(2, 3), (12, 30), (40, 50), (5, 1), (12, 10), (3, 4)] print('''Distance:''', closest_pair_of_points(points, len(points)))
300
0
'''simple docstring''' class UpperCAmelCase_ : def __init__( self : List[str] , UpperCAmelCase__ : list[int] ) -> None: lowerCAmelCase = len(UpperCAmelCase__ ) lowerCAmelCase = [0] * len_array if len_array > 0: lowerCAmelCase = array[0] for i in range(1 , UpperCAmelCase__ ): lowerCAmelCase = self.prefix_sum[i - 1] + array[i] def __UpperCAmelCase ( self : Optional[Any] , UpperCAmelCase__ : int , UpperCAmelCase__ : int ) -> int: if start == 0: return self.prefix_sum[end] return self.prefix_sum[end] - self.prefix_sum[start - 1] def __UpperCAmelCase ( self : int , UpperCAmelCase__ : int ) -> bool: lowerCAmelCase = {0} for sum_item in self.prefix_sum: if sum_item - target_sum in sums: return True sums.add(UpperCAmelCase__ ) return False if __name__ == "__main__": import doctest doctest.testmod()
4
import inspect import unittest from transformers import ViTMSNConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTMSNForImageClassification, ViTMSNModel from transformers.models.vit_msn.modeling_vit_msn import VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class __magic_name__ : """simple docstring""" def __init__( self :Dict , snake_case :Optional[int] , snake_case :Tuple=13 , snake_case :List[Any]=30 , snake_case :Union[str, Any]=2 , snake_case :List[Any]=3 , snake_case :Tuple=True , snake_case :Dict=True , snake_case :Dict=32 , snake_case :List[str]=5 , snake_case :Optional[Any]=4 , snake_case :Any=37 , snake_case :Dict="gelu" , snake_case :List[str]=0.1 , snake_case :str=0.1 , snake_case :Tuple=10 , snake_case :str=0.02 , snake_case :Optional[Any]=None , ): '''simple docstring''' A_ : Tuple = parent A_ : int = batch_size A_ : List[str] = image_size A_ : List[Any] = patch_size A_ : Optional[Any] = num_channels A_ : List[Any] = is_training A_ : Tuple = use_labels A_ : Union[str, Any] = hidden_size A_ : Tuple = num_hidden_layers A_ : Any = num_attention_heads A_ : List[str] = intermediate_size A_ : Optional[int] = hidden_act A_ : List[str] = hidden_dropout_prob A_ : str = attention_probs_dropout_prob A_ : Any = type_sequence_label_size A_ : List[str] = initializer_range A_ : Dict = scope # in ViT MSN, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) A_ : Optional[int] = (image_size // patch_size) ** 2 A_ : List[str] = num_patches + 1 def SCREAMING_SNAKE_CASE ( self :Optional[Any] ): '''simple docstring''' A_ : Any = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) A_ : Tuple = None if self.use_labels: A_ : Dict = ids_tensor([self.batch_size] , self.type_sequence_label_size ) A_ : Dict = self.get_config() return config, pixel_values, labels def SCREAMING_SNAKE_CASE ( self :Union[str, Any] ): '''simple docstring''' return ViTMSNConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , initializer_range=self.initializer_range , ) def SCREAMING_SNAKE_CASE ( self :List[Any] , snake_case :List[Any] , snake_case :str , snake_case :Tuple ): '''simple docstring''' A_ : Optional[Any] = ViTMSNModel(config=snake_case ) model.to(snake_case ) model.eval() A_ : int = model(snake_case ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def SCREAMING_SNAKE_CASE ( self :int , snake_case :Optional[int] , snake_case :List[str] , snake_case :List[str] ): '''simple docstring''' A_ : Dict = self.type_sequence_label_size A_ : Tuple = ViTMSNForImageClassification(snake_case ) model.to(snake_case ) model.eval() A_ : Union[str, Any] = model(snake_case , labels=snake_case ) print("Pixel and labels shape: {pixel_values.shape}, {labels.shape}" ) print("Labels: {labels}" ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images A_ : Union[str, Any] = 1 A_ : int = ViTMSNForImageClassification(snake_case ) model.to(snake_case ) model.eval() A_ : Dict = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) A_ : Optional[Any] = model(snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def SCREAMING_SNAKE_CASE ( self :List[Any] ): '''simple docstring''' A_ : List[str] = self.prepare_config_and_inputs() A_ , A_ , A_ : Optional[int] = config_and_inputs A_ : Dict = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class __magic_name__ ( lowerCamelCase__ , lowerCamelCase__ , unittest.TestCase ): """simple docstring""" __UpperCamelCase = (ViTMSNModel, ViTMSNForImageClassification) if is_torch_available() else () __UpperCamelCase = ( {'''feature-extraction''': ViTMSNModel, '''image-classification''': ViTMSNForImageClassification} if is_torch_available() else {} ) __UpperCamelCase = False __UpperCamelCase = False __UpperCamelCase = False __UpperCamelCase = False def SCREAMING_SNAKE_CASE ( self :Union[str, Any] ): '''simple docstring''' A_ : Tuple = ViTMSNModelTester(self ) A_ : str = ConfigTester(self , config_class=snake_case , has_text_modality=snake_case , hidden_size=37 ) def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' self.config_tester.run_common_tests() @unittest.skip(reason="ViTMSN does not use inputs_embeds" ) def SCREAMING_SNAKE_CASE ( self :Dict ): '''simple docstring''' pass def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' A_ , A_ : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A_ : Optional[int] = model_class(snake_case ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) A_ : Optional[int] = model.get_output_embeddings() self.assertTrue(x is None or isinstance(snake_case , nn.Linear ) ) def SCREAMING_SNAKE_CASE ( self :Optional[Any] ): '''simple docstring''' A_ , A_ : str = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A_ : Optional[Any] = model_class(snake_case ) A_ : Any = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic A_ : List[str] = [*signature.parameters.keys()] A_ : List[Any] = ["pixel_values"] self.assertListEqual(arg_names[:1] , snake_case ) def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' A_ : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case ) def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' A_ : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*snake_case ) @slow def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' for model_name in VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A_ : Optional[Any] = ViTMSNModel.from_pretrained(snake_case ) self.assertIsNotNone(snake_case ) def __snake_case ( ) -> Optional[Any]: A_ : Optional[Any] = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_torch @require_vision class __magic_name__ ( unittest.TestCase ): """simple docstring""" @cached_property def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' return ViTImageProcessor.from_pretrained("facebook/vit-msn-small" ) if is_vision_available() else None @slow def SCREAMING_SNAKE_CASE ( self :List[Any] ): '''simple docstring''' torch.manual_seed(2 ) A_ : Any = ViTMSNForImageClassification.from_pretrained("facebook/vit-msn-small" ).to(snake_case ) A_ : List[str] = self.default_image_processor A_ : int = prepare_img() A_ : List[str] = image_processor(images=snake_case , return_tensors="pt" ).to(snake_case ) # forward pass with torch.no_grad(): A_ : Optional[int] = model(**snake_case ) # verify the logits A_ : List[Any] = torch.Size((1, 1_000) ) self.assertEqual(outputs.logits.shape , snake_case ) A_ : int = torch.tensor([-0.0803, -0.4454, -0.2375] ).to(snake_case ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , snake_case , atol=1e-4 ) )
300
0
from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = { '''alibaba-damo/mgp-str-base''': '''https://huggingface.co/alibaba-damo/mgp-str-base/resolve/main/config.json''', } class lowerCamelCase__ ( lowerCAmelCase): SCREAMING_SNAKE_CASE__ = '''mgp-str''' def __init__(self , UpperCAmelCase=[3_2, 1_2_8] , UpperCAmelCase=4 , UpperCAmelCase=3 , UpperCAmelCase=2_7 , UpperCAmelCase=3_8 , UpperCAmelCase=5_0_2_5_7 , UpperCAmelCase=3_0_5_2_2 , UpperCAmelCase=7_6_8 , UpperCAmelCase=1_2 , UpperCAmelCase=1_2 , UpperCAmelCase=4.0 , UpperCAmelCase=True , UpperCAmelCase=False , UpperCAmelCase=1e-5 , UpperCAmelCase=0.0 , UpperCAmelCase=0.0 , UpperCAmelCase=0.0 , UpperCAmelCase=False , UpperCAmelCase=0.02 , **UpperCAmelCase , ) -> Optional[Any]: super().__init__(**UpperCAmelCase ) _lowercase =image_size _lowercase =patch_size _lowercase =num_channels _lowercase =max_token_length _lowercase =num_character_labels _lowercase =num_bpe_labels _lowercase =num_wordpiece_labels _lowercase =hidden_size _lowercase =num_hidden_layers _lowercase =num_attention_heads _lowercase =mlp_ratio _lowercase =distilled _lowercase =layer_norm_eps _lowercase =drop_rate _lowercase =qkv_bias _lowercase =attn_drop_rate _lowercase =drop_path_rate _lowercase =output_aa_attentions _lowercase =initializer_range
5
import torch from diffusers import DDPMScheduler from .test_schedulers import SchedulerCommonTest class __magic_name__ ( lowerCamelCase__ ): """simple docstring""" __UpperCamelCase = (DDPMScheduler,) def SCREAMING_SNAKE_CASE ( self :Union[str, Any] , **snake_case :str ): '''simple docstring''' A_ : Dict = { "num_train_timesteps": 1_000, "beta_start": 0.0001, "beta_end": 0.02, "beta_schedule": "linear", "variance_type": "fixed_small", "clip_sample": True, } config.update(**snake_case ) return config def SCREAMING_SNAKE_CASE ( self :int ): '''simple docstring''' for timesteps in [1, 5, 100, 1_000]: self.check_over_configs(num_train_timesteps=snake_case ) def SCREAMING_SNAKE_CASE ( self :Optional[Any] ): '''simple docstring''' for beta_start, beta_end in zip([0.0001, 0.001, 0.01, 0.1] , [0.002, 0.02, 0.2, 2] ): self.check_over_configs(beta_start=snake_case , beta_end=snake_case ) def SCREAMING_SNAKE_CASE ( self :int ): '''simple docstring''' for schedule in ["linear", "squaredcos_cap_v2"]: self.check_over_configs(beta_schedule=snake_case ) def SCREAMING_SNAKE_CASE ( self :List[Any] ): '''simple docstring''' for variance in ["fixed_small", "fixed_large", "other"]: self.check_over_configs(variance_type=snake_case ) def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' for clip_sample in [True, False]: self.check_over_configs(clip_sample=snake_case ) def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' self.check_over_configs(thresholding=snake_case ) for threshold in [0.5, 1.0, 2.0]: for prediction_type in ["epsilon", "sample", "v_prediction"]: self.check_over_configs( thresholding=snake_case , prediction_type=snake_case , sample_max_value=snake_case , ) def SCREAMING_SNAKE_CASE ( self :Optional[int] ): '''simple docstring''' for prediction_type in ["epsilon", "sample", "v_prediction"]: self.check_over_configs(prediction_type=snake_case ) def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' for t in [0, 500, 999]: self.check_over_forward(time_step=snake_case ) def SCREAMING_SNAKE_CASE ( self :Optional[Any] ): '''simple docstring''' A_ : Tuple = self.scheduler_classes[0] A_ : List[str] = self.get_scheduler_config() A_ : List[str] = scheduler_class(**snake_case ) assert torch.sum(torch.abs(scheduler._get_variance(0 ) - 0.0 ) ) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(487 ) - 0.00979 ) ) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(999 ) - 0.02 ) ) < 1e-5 def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' A_ : int = self.scheduler_classes[0] A_ : List[str] = self.get_scheduler_config() A_ : int = scheduler_class(**snake_case ) A_ : Tuple = len(snake_case ) A_ : List[str] = self.dummy_model() A_ : Optional[Any] = self.dummy_sample_deter A_ : List[str] = torch.manual_seed(0 ) for t in reversed(range(snake_case ) ): # 1. predict noise residual A_ : Tuple = model(snake_case , snake_case ) # 2. predict previous mean of sample x_t-1 A_ : Dict = scheduler.step(snake_case , snake_case , snake_case , generator=snake_case ).prev_sample # if t > 0: # noise = self.dummy_sample_deter # variance = scheduler.get_variance(t) ** (0.5) * noise # # sample = pred_prev_sample + variance A_ : Optional[int] = pred_prev_sample A_ : Tuple = torch.sum(torch.abs(snake_case ) ) A_ : str = torch.mean(torch.abs(snake_case ) ) assert abs(result_sum.item() - 258.9606 ) < 1e-2 assert abs(result_mean.item() - 0.3372 ) < 1e-3 def SCREAMING_SNAKE_CASE ( self :Dict ): '''simple docstring''' A_ : Optional[int] = self.scheduler_classes[0] A_ : int = self.get_scheduler_config(prediction_type="v_prediction" ) A_ : List[str] = scheduler_class(**snake_case ) A_ : int = len(snake_case ) A_ : Dict = self.dummy_model() A_ : str = self.dummy_sample_deter A_ : Any = torch.manual_seed(0 ) for t in reversed(range(snake_case ) ): # 1. predict noise residual A_ : Optional[int] = model(snake_case , snake_case ) # 2. predict previous mean of sample x_t-1 A_ : Tuple = scheduler.step(snake_case , snake_case , snake_case , generator=snake_case ).prev_sample # if t > 0: # noise = self.dummy_sample_deter # variance = scheduler.get_variance(t) ** (0.5) * noise # # sample = pred_prev_sample + variance A_ : List[str] = pred_prev_sample A_ : Optional[Any] = torch.sum(torch.abs(snake_case ) ) A_ : List[str] = torch.mean(torch.abs(snake_case ) ) assert abs(result_sum.item() - 202.0296 ) < 1e-2 assert abs(result_mean.item() - 0.2631 ) < 1e-3 def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' A_ : str = self.scheduler_classes[0] A_ : Optional[Any] = self.get_scheduler_config() A_ : Dict = scheduler_class(**snake_case ) A_ : Optional[int] = [100, 87, 50, 1, 0] scheduler.set_timesteps(timesteps=snake_case ) A_ : Optional[int] = scheduler.timesteps for i, timestep in enumerate(snake_case ): if i == len(snake_case ) - 1: A_ : str = -1 else: A_ : List[str] = timesteps[i + 1] A_ : Optional[int] = scheduler.previous_timestep(snake_case ) A_ : List[str] = prev_t.item() self.assertEqual(snake_case , snake_case ) def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' A_ : Optional[Any] = self.scheduler_classes[0] A_ : int = self.get_scheduler_config() A_ : Tuple = scheduler_class(**snake_case ) A_ : List[str] = [100, 87, 50, 51, 0] with self.assertRaises(snake_case , msg="`custom_timesteps` must be in descending order." ): scheduler.set_timesteps(timesteps=snake_case ) def SCREAMING_SNAKE_CASE ( self :List[Any] ): '''simple docstring''' A_ : Any = self.scheduler_classes[0] A_ : Union[str, Any] = self.get_scheduler_config() A_ : Optional[int] = scheduler_class(**snake_case ) A_ : Union[str, Any] = [100, 87, 50, 1, 0] A_ : Optional[int] = len(snake_case ) with self.assertRaises(snake_case , msg="Can only pass one of `num_inference_steps` or `custom_timesteps`." ): scheduler.set_timesteps(num_inference_steps=snake_case , timesteps=snake_case ) def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' A_ : Union[str, Any] = self.scheduler_classes[0] A_ : Optional[Any] = self.get_scheduler_config() A_ : Optional[int] = scheduler_class(**snake_case ) A_ : Optional[int] = [scheduler.config.num_train_timesteps] with self.assertRaises( snake_case , msg="`timesteps` must start before `self.config.train_timesteps`: {scheduler.config.num_train_timesteps}}" , ): scheduler.set_timesteps(timesteps=snake_case )
300
0
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) A : str = { 'configuration_blenderbot': [ 'BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'BlenderbotConfig', 'BlenderbotOnnxConfig', ], 'tokenization_blenderbot': ['BlenderbotTokenizer'], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Optional[Any] = ['BlenderbotTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Optional[Any] = [ 'BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST', 'BlenderbotForCausalLM', 'BlenderbotForConditionalGeneration', 'BlenderbotModel', 'BlenderbotPreTrainedModel', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Dict = [ 'TFBlenderbotForConditionalGeneration', 'TFBlenderbotModel', 'TFBlenderbotPreTrainedModel', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Dict = [ 'FlaxBlenderbotForConditionalGeneration', 'FlaxBlenderbotModel', 'FlaxBlenderbotPreTrainedModel', ] if TYPE_CHECKING: from .configuration_blenderbot import ( BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP, BlenderbotConfig, BlenderbotOnnxConfig, ) from .tokenization_blenderbot import BlenderbotTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_blenderbot_fast import BlenderbotTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_blenderbot import ( BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST, BlenderbotForCausalLM, BlenderbotForConditionalGeneration, BlenderbotModel, BlenderbotPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_blenderbot import ( TFBlenderbotForConditionalGeneration, TFBlenderbotModel, TFBlenderbotPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_blenderbot import ( FlaxBlenderbotForConditionalGeneration, FlaxBlenderbotModel, FlaxBlenderbotPreTrainedModel, ) else: import sys A : List[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
6
import argparse import json import os import fairseq import torch from torch import nn from transformers import ( SpeechaTextaConfig, SpeechaTextaForCausalLM, SpeechaTextaTokenizer, SpeechEncoderDecoderConfig, SpeechEncoderDecoderModel, WavaVecaConfig, WavaVecaFeatureExtractor, WavaVecaModel, logging, ) logging.set_verbosity_info() _lowerCAmelCase : List[str] = logging.get_logger(__name__) _lowerCAmelCase : Optional[int] = { '''post_extract_proj''': '''feature_projection.projection''', '''encoder.pos_conv.0''': '''encoder.pos_conv_embed.conv''', '''self_attn.k_proj''': '''encoder.layers.*.attention.k_proj''', '''self_attn.v_proj''': '''encoder.layers.*.attention.v_proj''', '''self_attn.q_proj''': '''encoder.layers.*.attention.q_proj''', '''self_attn.out_proj''': '''encoder.layers.*.attention.out_proj''', '''self_attn_layer_norm''': '''encoder.layers.*.layer_norm''', '''fc1''': '''encoder.layers.*.feed_forward.intermediate_dense''', '''fc2''': '''encoder.layers.*.feed_forward.output_dense''', '''final_layer_norm''': '''encoder.layers.*.final_layer_norm''', '''encoder.layer_norm''': '''encoder.layer_norm''', '''w2v_model.layer_norm''': '''feature_projection.layer_norm''', '''quantizer.weight_proj''': '''quantizer.weight_proj''', '''quantizer.vars''': '''quantizer.codevectors''', '''project_q''': '''project_q''', '''final_proj''': '''project_hid''', '''w2v_encoder.proj''': '''lm_head''', '''mask_emb''': '''masked_spec_embed''', } _lowerCAmelCase : int = [ '''lm_head''', '''quantizer.weight_proj''', '''quantizer.codevectors''', '''project_q''', '''project_hid''', ] def __snake_case ( _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : List[Any] , _lowerCAmelCase : Any , _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : int ) -> List[Any]: for attribute in key.split("." ): A_ : List[Any] = getattr(_lowerCAmelCase , _lowerCAmelCase ) if weight_type is not None: A_ : List[Any] = getattr(_lowerCAmelCase , _lowerCAmelCase ).shape else: A_ : Tuple = hf_pointer.shape assert hf_shape == value.shape, ( f"Shape of hf {key + '.' + weight_type if weight_type is not None else ''} is {hf_shape}, but should be" f" {value.shape} for {full_name}" ) if weight_type == "weight": A_ : Optional[int] = value elif weight_type == "weight_g": A_ : Optional[int] = value elif weight_type == "weight_v": A_ : Any = value elif weight_type == "bias": A_ : str = value else: A_ : Any = value logger.info(f"{key + '.' + weight_type if weight_type is not None else ''} was initialized from {full_name}." ) def __snake_case ( _lowerCAmelCase : Optional[Any] , _lowerCAmelCase : Dict ) -> List[str]: A_ : Optional[Any] = [] A_ : Any = fairseq_model.state_dict() A_ : Union[str, Any] = hf_model.feature_extractor # if encoder has different dim to decoder -> use proj_weight A_ : str = None for name, value in fairseq_dict.items(): A_ : Tuple = False if "conv_layers" in name: load_conv_layer( _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , hf_model.config.feat_extract_norm == "group" , ) A_ : Optional[Any] = True elif name.split("." )[0] == "proj": A_ : Dict = fairseq_model.proj A_ : List[Any] = True else: for key, mapped_key in MAPPING.items(): if key in name or key.split("w2v_model." )[-1] == name.split("." )[0]: A_ : int = True if "*" in mapped_key: A_ : Optional[Any] = name.split(_lowerCAmelCase )[0].split("." )[-2] A_ : int = mapped_key.replace("*" , _lowerCAmelCase ) if "weight_g" in name: A_ : List[Any] = "weight_g" elif "weight_v" in name: A_ : List[Any] = "weight_v" elif "bias" in name: A_ : Dict = "bias" elif "weight" in name: A_ : List[Any] = "weight" else: A_ : Dict = None set_recursively(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) continue if not is_used: unused_weights.append(_lowerCAmelCase ) logger.warning(f"Unused weights: {unused_weights}" ) return proj_weight def __snake_case ( _lowerCAmelCase : Optional[Any] , _lowerCAmelCase : Tuple , _lowerCAmelCase : List[Any] , _lowerCAmelCase : List[Any] , _lowerCAmelCase : Optional[int] ) -> str: A_ : Any = full_name.split("conv_layers." )[-1] A_ : Optional[int] = name.split("." ) A_ : Optional[Any] = int(items[0] ) A_ : Union[str, Any] = int(items[1] ) if type_id == 0: if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, ( f"{full_name} has size {value.shape}, but" f" {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found." ) A_ : List[Any] = value logger.info(f"Feat extract conv layer {layer_id} was initialized from {full_name}." ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape, ( f"{full_name} has size {value.shape}, but" f" {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found." ) A_ : int = value logger.info(f"Feat extract conv layer {layer_id} was initialized from {full_name}." ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, ( f"{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was" " found." ) A_ : List[Any] = value logger.info(f"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}." ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, ( f"{full_name} has size {value.shape}, but" f" {feature_extractor[layer_id].layer_norm.weight.data.shape} was found." ) A_ : Tuple = value logger.info(f"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}." ) else: unused_weights.append(_lowerCAmelCase ) def __snake_case ( _lowerCAmelCase : Optional[int] ) -> str: A_ , A_ : List[str] = emb.weight.shape A_ : Optional[int] = nn.Linear(_lowerCAmelCase , _lowerCAmelCase , bias=_lowerCAmelCase ) A_ : List[Any] = emb.weight.data return lin_layer def __snake_case ( _lowerCAmelCase : str ) -> Tuple: with open(_lowerCAmelCase , "r" , encoding="utf-8" ) as f: A_ : int = f.readlines() A_ : Dict = [line.split(" " )[0] for line in lines] A_ : Tuple = len(_lowerCAmelCase ) A_ : Union[str, Any] = { "<s>": 0, "<pad>": 1, "</s>": 2, "<unk>": 3, } vocab_dict.update(dict(zip(_lowerCAmelCase , range(4 , num_words + 4 ) ) ) ) return vocab_dict @torch.no_grad() def __snake_case ( _lowerCAmelCase : int , _lowerCAmelCase : Tuple , _lowerCAmelCase : Dict , _lowerCAmelCase : Any , _lowerCAmelCase : Tuple , _lowerCAmelCase : List[str] , _lowerCAmelCase : Dict , ) -> Tuple: A_ : Optional[int] = WavaVecaConfig.from_pretrained(_lowerCAmelCase ) A_ : str = SpeechaTextaConfig.from_pretrained( _lowerCAmelCase , vocab_size=_lowerCAmelCase , decoder_layers=_lowerCAmelCase , do_stable_layer_norm=_lowerCAmelCase ) A_ : int = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=16000 , padding_value=0 , do_normalize=_lowerCAmelCase , return_attention_mask=_lowerCAmelCase , ) A_ , A_ , A_ : List[Any] = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={"data": "/".join(dict_path.split("/" )[:-1] )} ) A_ : Union[str, Any] = model[0].eval() # set weights for wav2vec2 encoder A_ : Tuple = WavaVecaModel(_lowerCAmelCase ) A_ : str = recursively_load_weights_wavaveca(model.encoder , _lowerCAmelCase ) A_ : Tuple = SpeechaTextaForCausalLM(_lowerCAmelCase ) A_ , A_ : List[str] = hf_decoder.model.decoder.load_state_dict(model.decoder.state_dict() , strict=_lowerCAmelCase ) # set output linear layer unexpected_keys.remove("embed_out" ) A_ : Union[str, Any] = nn.Parameter(model.decoder.embed_out.detach() ) # layer norm is init to identity matrix so leaving it is fine logger.warning(f"The following keys are missing when loading the decoder weights: {missing_keys}" ) logger.warning(f"The following keys are unexpected when loading the decoder weights: {unexpected_keys}" ) A_ : str = SpeechEncoderDecoderModel(encoder=_lowerCAmelCase , decoder=_lowerCAmelCase ) A_ : Optional[Any] = False # add projection layer A_ : Optional[Any] = nn.Parameter(projection_layer.weight ) A_ : int = nn.Parameter(projection_layer.bias ) A_ : str = create_vocab_dict(_lowerCAmelCase ) with open(os.path.join(_lowerCAmelCase , "vocab.json" ) , "w" ) as fp: json.dump(_lowerCAmelCase , _lowerCAmelCase ) A_ : Any = SpeechaTextaTokenizer(os.path.join(_lowerCAmelCase , "vocab.json" ) ) tokenizer.save_pretrained(_lowerCAmelCase ) A_ : Optional[int] = hf_wavavec.config.to_dict() A_ : int = tokenizer.pad_token_id A_ : List[str] = tokenizer.bos_token_id A_ : List[str] = tokenizer.eos_token_id A_ : List[str] = "speech_to_text_2" A_ : Tuple = "wav2vec2" A_ : str = SpeechEncoderDecoderConfig.from_dict(_lowerCAmelCase ) hf_wavavec.save_pretrained(_lowerCAmelCase ) feature_extractor.save_pretrained(_lowerCAmelCase ) if __name__ == "__main__": _lowerCAmelCase : Optional[Any] = argparse.ArgumentParser() parser.add_argument('''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''') parser.add_argument('''--checkpoint_path''', default=None, type=str, help='''Path to fairseq checkpoint''') parser.add_argument('''--dict_path''', default=None, type=str, help='''Path to dict of fine-tuned model''') parser.add_argument( '''--encoder_config_path''', default='''facebook/wav2vec2-large-lv60''', type=str, help='''Path to hf encoder wav2vec2 checkpoint config''', ) parser.add_argument( '''--decoder_config_path''', default='''facebook/s2t-small-mustc-en-fr-st''', type=str, help='''Path to hf decoder s2t checkpoint config''', ) parser.add_argument('''--vocab_size''', default=10_224, type=int, help='''Vocab size of decoder''') parser.add_argument('''--num_decoder_layers''', default=7, type=int, help='''Number of decoder layers''') _lowerCAmelCase : Union[str, Any] = parser.parse_args() convert_wavaveca_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.dict_path, encoder_config_path=args.encoder_config_path, decoder_config_path=args.decoder_config_path, vocab_size=args.vocab_size, num_decoder_layers=args.num_decoder_layers, )
300
0
from __future__ import annotations import math def _snake_case( SCREAMING_SNAKE_CASE__ : int ) -> bool: '''simple docstring''' if 1 < number < 4: # 2 and 3 are primes return True elif number < 2 or number % 2 == 0 or number % 3 == 0: # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes return False # All primes number are in format of 6k +/- 1 for i in range(5 , int(math.sqrt(SCREAMING_SNAKE_CASE__ ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True def _snake_case( SCREAMING_SNAKE_CASE__ : int ) -> list[int]: '''simple docstring''' A__ = str(SCREAMING_SNAKE_CASE__ ) A__ = [n] for i in range(1 , len(SCREAMING_SNAKE_CASE__ ) ): list_nums.append(int(str_num[i:] ) ) list_nums.append(int(str_num[:-i] ) ) return list_nums def _snake_case( SCREAMING_SNAKE_CASE__ : int ) -> bool: '''simple docstring''' if len(str(SCREAMING_SNAKE_CASE__ ) ) > 3: if not is_prime(int(str(SCREAMING_SNAKE_CASE__ )[-3:] ) ) or not is_prime(int(str(SCREAMING_SNAKE_CASE__ )[:3] ) ): return False return True def _snake_case( SCREAMING_SNAKE_CASE__ : int = 11 ) -> list[int]: '''simple docstring''' A__ = [] A__ = 13 while len(SCREAMING_SNAKE_CASE__ ) != count: if validate(SCREAMING_SNAKE_CASE__ ): A__ = list_truncated_nums(SCREAMING_SNAKE_CASE__ ) if all(is_prime(SCREAMING_SNAKE_CASE__ ) for i in list_nums ): list_truncated_primes.append(SCREAMING_SNAKE_CASE__ ) num += 2 return list_truncated_primes def _snake_case( ) -> int: '''simple docstring''' return sum(compute_truncated_primes(11 ) ) if __name__ == "__main__": print(f"""{sum(compute_truncated_primes(11)) = }""")
7
from __future__ import annotations import unittest import numpy as np from transformers import LayoutLMConfig, 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.models.layoutlm.modeling_tf_layoutlm import ( TF_LAYOUTLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFLayoutLMForMaskedLM, TFLayoutLMForQuestionAnswering, TFLayoutLMForSequenceClassification, TFLayoutLMForTokenClassification, TFLayoutLMModel, ) class __magic_name__ : """simple docstring""" def __init__( self :Tuple , snake_case :Optional[Any] , snake_case :Tuple=13 , snake_case :Dict=7 , snake_case :List[Any]=True , snake_case :List[Any]=True , snake_case :Dict=True , snake_case :Any=True , snake_case :Optional[int]=99 , snake_case :Any=32 , snake_case :Dict=2 , snake_case :int=4 , snake_case :Optional[int]=37 , snake_case :List[str]="gelu" , snake_case :List[Any]=0.1 , snake_case :Optional[Any]=0.1 , snake_case :Tuple=512 , snake_case :Tuple=16 , snake_case :Tuple=2 , snake_case :Optional[int]=0.02 , snake_case :str=3 , snake_case :Optional[int]=4 , snake_case :List[str]=None , snake_case :Tuple=1_000 , ): '''simple docstring''' A_ : str = parent A_ : str = batch_size A_ : str = seq_length A_ : Any = is_training A_ : Any = use_input_mask A_ : str = use_token_type_ids A_ : Tuple = use_labels A_ : Optional[Any] = vocab_size A_ : Dict = hidden_size A_ : str = num_hidden_layers A_ : Dict = num_attention_heads A_ : str = intermediate_size A_ : int = hidden_act A_ : List[Any] = hidden_dropout_prob A_ : Dict = attention_probs_dropout_prob A_ : Optional[Any] = max_position_embeddings A_ : List[Any] = type_vocab_size A_ : Any = type_sequence_label_size A_ : Dict = initializer_range A_ : Any = num_labels A_ : Optional[int] = num_choices A_ : Optional[Any] = scope A_ : Any = range_bbox def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' A_ : List[str] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) # convert bbox to numpy since TF does not support item assignment A_ : Tuple = ids_tensor([self.batch_size, self.seq_length, 4] , self.range_bbox ).numpy() # Ensure that bbox is legal for i in range(bbox.shape[0] ): for j in range(bbox.shape[1] ): if bbox[i, j, 3] < bbox[i, j, 1]: A_ : str = bbox[i, j, 3] A_ : Union[str, Any] = bbox[i, j, 1] A_ : List[Any] = t if bbox[i, j, 2] < bbox[i, j, 0]: A_ : Any = bbox[i, j, 2] A_ : Tuple = bbox[i, j, 0] A_ : int = t A_ : int = tf.convert_to_tensor(snake_case ) A_ : Any = None if self.use_input_mask: A_ : Union[str, Any] = random_attention_mask([self.batch_size, self.seq_length] ) A_ : str = None if self.use_token_type_ids: A_ : Union[str, Any] = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) A_ : Dict = None A_ : List[Any] = None A_ : List[str] = None if self.use_labels: A_ : Dict = ids_tensor([self.batch_size] , self.type_sequence_label_size ) A_ : Optional[int] = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) A_ : str = ids_tensor([self.batch_size] , self.num_choices ) A_ : int = LayoutLMConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , ) return config, input_ids, bbox, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def SCREAMING_SNAKE_CASE ( self :str , snake_case :Dict , snake_case :Union[str, Any] , snake_case :int , snake_case :int , snake_case :Union[str, Any] , snake_case :Tuple , snake_case :Optional[int] , snake_case :List[Any] ): '''simple docstring''' A_ : Any = TFLayoutLMModel(config=snake_case ) A_ : Tuple = model(snake_case , snake_case , attention_mask=snake_case , token_type_ids=snake_case ) A_ : str = model(snake_case , snake_case , token_type_ids=snake_case ) A_ : List[Any] = model(snake_case , snake_case ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def SCREAMING_SNAKE_CASE ( self :Optional[int] , snake_case :Any , snake_case :List[Any] , snake_case :List[str] , snake_case :Optional[Any] , snake_case :Dict , snake_case :Any , snake_case :Union[str, Any] , snake_case :List[Any] ): '''simple docstring''' A_ : Optional[int] = TFLayoutLMForMaskedLM(config=snake_case ) A_ : Tuple = model(snake_case , snake_case , attention_mask=snake_case , token_type_ids=snake_case , labels=snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def SCREAMING_SNAKE_CASE ( self :List[str] , snake_case :Dict , snake_case :Tuple , snake_case :Tuple , snake_case :List[str] , snake_case :Tuple , snake_case :str , snake_case :Optional[int] , snake_case :Any ): '''simple docstring''' A_ : Union[str, Any] = self.num_labels A_ : int = TFLayoutLMForSequenceClassification(config=snake_case ) A_ : Optional[int] = model(snake_case , snake_case , attention_mask=snake_case , token_type_ids=snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def SCREAMING_SNAKE_CASE ( self :Optional[Any] , snake_case :Dict , snake_case :str , snake_case :Optional[Any] , snake_case :int , snake_case :Any , snake_case :Tuple , snake_case :List[str] , snake_case :Union[str, Any] ): '''simple docstring''' A_ : List[Any] = self.num_labels A_ : str = TFLayoutLMForTokenClassification(config=snake_case ) A_ : Union[str, Any] = model(snake_case , snake_case , attention_mask=snake_case , token_type_ids=snake_case , labels=snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def SCREAMING_SNAKE_CASE ( self :int , snake_case :List[str] , snake_case :Optional[int] , snake_case :Union[str, Any] , snake_case :List[Any] , snake_case :int , snake_case :Any , snake_case :Union[str, Any] , snake_case :Any ): '''simple docstring''' A_ : Optional[Any] = TFLayoutLMForQuestionAnswering(config=snake_case ) A_ : List[Any] = model(snake_case , snake_case , attention_mask=snake_case , token_type_ids=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 SCREAMING_SNAKE_CASE ( self :Dict ): '''simple docstring''' A_ : int = self.prepare_config_and_inputs() ( ( A_ ) , ( A_ ) , ( A_ ) , ( A_ ) , ( A_ ) , ( A_ ) , ( A_ ) , ( A_ ) , ) : Union[str, Any] = config_and_inputs A_ : Optional[Any] = { "input_ids": input_ids, "bbox": bbox, "token_type_ids": token_type_ids, "attention_mask": input_mask, } return config, inputs_dict @require_tf class __magic_name__ ( lowerCamelCase__ , lowerCamelCase__ , unittest.TestCase ): """simple docstring""" __UpperCamelCase = ( ( TFLayoutLMModel, TFLayoutLMForMaskedLM, TFLayoutLMForTokenClassification, TFLayoutLMForSequenceClassification, TFLayoutLMForQuestionAnswering, ) if is_tf_available() else () ) __UpperCamelCase = ( { '''feature-extraction''': TFLayoutLMModel, '''fill-mask''': TFLayoutLMForMaskedLM, '''text-classification''': TFLayoutLMForSequenceClassification, '''token-classification''': TFLayoutLMForTokenClassification, '''zero-shot''': TFLayoutLMForSequenceClassification, } if is_tf_available() else {} ) __UpperCamelCase = False __UpperCamelCase = True __UpperCamelCase = 10 def SCREAMING_SNAKE_CASE ( self :Dict ): '''simple docstring''' A_ : Tuple = TFLayoutLMModelTester(self ) A_ : List[Any] = ConfigTester(self , config_class=snake_case , hidden_size=37 ) def SCREAMING_SNAKE_CASE ( self :Tuple ): '''simple docstring''' self.config_tester.run_common_tests() def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' A_ : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case ) def SCREAMING_SNAKE_CASE ( self :Optional[int] ): '''simple docstring''' A_ : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*snake_case ) def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' A_ : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*snake_case ) def SCREAMING_SNAKE_CASE ( self :Tuple ): '''simple docstring''' A_ : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*snake_case ) def SCREAMING_SNAKE_CASE ( self :List[Any] ): '''simple docstring''' A_ : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*snake_case ) @slow def SCREAMING_SNAKE_CASE ( self :Optional[Any] ): '''simple docstring''' for model_name in TF_LAYOUTLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A_ : List[str] = TFLayoutLMModel.from_pretrained(snake_case ) self.assertIsNotNone(snake_case ) @unittest.skip("Onnx compliancy broke with TF 2.10" ) def SCREAMING_SNAKE_CASE ( self :Dict ): '''simple docstring''' pass def __snake_case ( ) -> Optional[Any]: # Here we prepare a batch of 2 sequences to test a LayoutLM forward pass on: # fmt: off A_ : int = tf.convert_to_tensor([[101,1019,1014,1016,1037,12849,4747,1004,14246,2278,5439,4524,5002,2930,2193,2930,4341,3208,1005,1055,2171,2848,11300,3531,102],[101,4070,4034,7020,1024,3058,1015,1013,2861,1013,6070,19274,2772,6205,27814,16147,16147,4343,2047,10283,10969,14389,1012,2338,102]] ) # noqa: E231 A_ : int = tf.convert_to_tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],] ) # noqa: E231 A_ : Union[str, Any] = tf.convert_to_tensor([[[0,0,0,0],[423,237,440,251],[427,272,441,287],[419,115,437,129],[961,885,992,912],[256,38,330,58],[256,38,330,58],[336,42,353,57],[360,39,401,56],[360,39,401,56],[411,39,471,59],[479,41,528,59],[533,39,630,60],[67,113,134,131],[141,115,209,132],[68,149,133,166],[141,149,187,164],[195,148,287,165],[195,148,287,165],[195,148,287,165],[295,148,349,165],[441,149,492,166],[497,149,546,164],[64,201,125,218],[1000,1000,1000,1000]],[[0,0,0,0],[662,150,754,166],[665,199,742,211],[519,213,554,228],[519,213,554,228],[134,433,187,454],[130,467,204,480],[130,467,204,480],[130,467,204,480],[130,467,204,480],[130,467,204,480],[314,469,376,482],[504,684,582,706],[941,825,973,900],[941,825,973,900],[941,825,973,900],[941,825,973,900],[610,749,652,765],[130,659,168,672],[176,657,237,672],[238,657,312,672],[443,653,628,672],[443,653,628,672],[716,301,825,317],[1000,1000,1000,1000]]] ) # noqa: E231 A_ : List[Any] = tf.convert_to_tensor([[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]] ) # noqa: E231 # these are sequence labels (i.e. at the token level) A_ : Tuple = tf.convert_to_tensor([[-100,10,10,10,9,1,-100,7,7,-100,7,7,4,2,5,2,8,8,-100,-100,5,0,3,2,-100],[-100,12,12,12,-100,12,10,-100,-100,-100,-100,10,12,9,-100,-100,-100,10,10,10,9,12,-100,10,-100]] ) # noqa: E231 # fmt: on return input_ids, attention_mask, bbox, token_type_ids, labels @require_tf class __magic_name__ ( unittest.TestCase ): """simple docstring""" @slow def SCREAMING_SNAKE_CASE ( self :Tuple ): '''simple docstring''' A_ : str = TFLayoutLMModel.from_pretrained("microsoft/layoutlm-base-uncased" ) A_ , A_ , A_ , A_ , A_ : Tuple = prepare_layoutlm_batch_inputs() # forward pass A_ : Tuple = model(input_ids=snake_case , bbox=snake_case , attention_mask=snake_case , token_type_ids=snake_case ) # test the sequence output on [0, :3, :3] A_ : List[Any] = tf.convert_to_tensor( [[0.1785, -0.1947, -0.0425], [-0.3254, -0.2807, 0.2553], [-0.5391, -0.3322, 0.3364]] , ) self.assertTrue(np.allclose(outputs.last_hidden_state[0, :3, :3] , snake_case , atol=1e-3 ) ) # test the pooled output on [1, :3] A_ : Optional[Any] = tf.convert_to_tensor([-0.6580, -0.0214, 0.8552] ) self.assertTrue(np.allclose(outputs.pooler_output[1, :3] , snake_case , atol=1e-3 ) ) @slow def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' A_ : Union[str, Any] = TFLayoutLMForSequenceClassification.from_pretrained("microsoft/layoutlm-base-uncased" , num_labels=2 ) A_ , A_ , A_ , A_ , A_ : Any = prepare_layoutlm_batch_inputs() # forward pass A_ : Dict = model( input_ids=snake_case , bbox=snake_case , attention_mask=snake_case , token_type_ids=snake_case , labels=tf.convert_to_tensor([1, 1] ) , ) # test whether we get a loss as a scalar A_ : List[str] = outputs.loss A_ : Union[str, Any] = (2,) self.assertEqual(loss.shape , snake_case ) # test the shape of the logits A_ : Tuple = outputs.logits A_ : Tuple = (2, 2) self.assertEqual(logits.shape , snake_case ) @slow def SCREAMING_SNAKE_CASE ( self :Optional[int] ): '''simple docstring''' A_ : int = TFLayoutLMForTokenClassification.from_pretrained("microsoft/layoutlm-base-uncased" , num_labels=13 ) A_ , A_ , A_ , A_ , A_ : Optional[int] = prepare_layoutlm_batch_inputs() # forward pass A_ : Union[str, Any] = model( input_ids=snake_case , bbox=snake_case , attention_mask=snake_case , token_type_ids=snake_case , labels=snake_case ) # test the shape of the logits A_ : Dict = outputs.logits A_ : List[Any] = tf.convert_to_tensor((2, 25, 13) ) self.assertEqual(logits.shape , snake_case ) @slow def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' A_ : Optional[Any] = TFLayoutLMForQuestionAnswering.from_pretrained("microsoft/layoutlm-base-uncased" ) A_ , A_ , A_ , A_ , A_ : str = prepare_layoutlm_batch_inputs() # forward pass A_ : Union[str, Any] = model(input_ids=snake_case , bbox=snake_case , attention_mask=snake_case , token_type_ids=snake_case ) # test the shape of the logits A_ : Union[str, Any] = tf.convert_to_tensor((2, 25) ) self.assertEqual(outputs.start_logits.shape , snake_case ) self.assertEqual(outputs.end_logits.shape , snake_case )
300
0
import json import sys def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): with open(SCREAMING_SNAKE_CASE__ , encoding='''utf-8''' ) as f: snake_case_ = json.load(SCREAMING_SNAKE_CASE__ ) snake_case_ = ['''<details>''', '''<summary>Show updated benchmarks!</summary>''', ''' '''] for benchmark_name in sorted(SCREAMING_SNAKE_CASE__ ): snake_case_ = results[benchmark_name] snake_case_ = benchmark_name.split('''/''' )[-1] output_md.append(F'''### Benchmark: {benchmark_file_name}''' ) snake_case_ = '''| metric |''' snake_case_ = '''|--------|''' snake_case_ = '''| new / old (diff) |''' for metric_name in sorted(SCREAMING_SNAKE_CASE__ ): snake_case_ = benchmark_res[metric_name] snake_case_ = metric_vals['''new'''] snake_case_ = metric_vals.get('''old''' , SCREAMING_SNAKE_CASE__ ) snake_case_ = metric_vals.get('''diff''' , SCREAMING_SNAKE_CASE__ ) snake_case_ = F''' {new_val:f}''' if isinstance(SCREAMING_SNAKE_CASE__ , (int, float) ) else '''None''' if old_val is not None: val_str += F''' / {old_val:f}''' if isinstance(SCREAMING_SNAKE_CASE__ , (int, float) ) else "None" if dif_val is not None: val_str += F''' ({dif_val:f})''' if isinstance(SCREAMING_SNAKE_CASE__ , (int, float) ) else "None" title += " " + metric_name + " |" lines += "---|" value += val_str + " |" output_md += [title, lines, value, " "] output_md.append('''</details>''' ) with open(SCREAMING_SNAKE_CASE__ , '''w''' , encoding='''utf-8''' ) as f: f.writelines('''\n'''.join(SCREAMING_SNAKE_CASE__ ) ) if __name__ == "__main__": lowerCAmelCase_ = sys.argv[1] lowerCAmelCase_ = sys.argv[2] format_json_to_md(input_json_file, output_md_file)
8
# Copyright 2023 The HuggingFace Inc. 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. import re from ..utils import cached_file # docstyle-ignore _lowerCAmelCase : Optional[int] = ''' Human: <<task>> Assistant: ''' _lowerCAmelCase : int = '''huggingface-tools/default-prompts''' _lowerCAmelCase : Any = {'''chat''': '''chat_prompt_template.txt''', '''run''': '''run_prompt_template.txt'''} def __snake_case ( _lowerCAmelCase : str , _lowerCAmelCase : List[Any] , _lowerCAmelCase : Dict="run" ) -> List[Any]: if prompt_or_repo_id is None: A_ : Optional[int] = DEFAULT_PROMPTS_REPO # prompt is considered a repo ID when it does not contain any kind of space if re.search("\\s" , _lowerCAmelCase ) is not None: return prompt_or_repo_id A_ : Optional[Any] = cached_file( _lowerCAmelCase , PROMPT_FILES[mode] , repo_type="dataset" , user_agent={"agent": agent_name} ) with open(_lowerCAmelCase , "r" , encoding="utf-8" ) as f: return f.read()
300
0
import inspect import unittest from transformers import ViTHybridConfig from transformers.testing_utils import require_accelerate, require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, _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 ViTHybridForImageClassification, ViTHybridImageProcessor, ViTHybridModel from transformers.models.vit_hybrid.modeling_vit_hybrid import VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image class _lowercase : '''simple docstring''' def __init__( self :Optional[int] , lowerCAmelCase__ :Tuple , lowerCAmelCase__ :Optional[int]=13 , lowerCAmelCase__ :List[str]=64 , lowerCAmelCase__ :Union[str, Any]=2 , lowerCAmelCase__ :int=3 , lowerCAmelCase__ :List[Any]=True , lowerCAmelCase__ :Optional[int]=True , lowerCAmelCase__ :int=32 , lowerCAmelCase__ :Optional[Any]=5 , lowerCAmelCase__ :Optional[Any]=4 , lowerCAmelCase__ :Tuple=37 , lowerCAmelCase__ :Optional[int]="gelu" , lowerCAmelCase__ :List[Any]=0.1 , lowerCAmelCase__ :Optional[Any]=0.1 , lowerCAmelCase__ :Any=10 , lowerCAmelCase__ :List[Any]=0.02 , lowerCAmelCase__ :Union[str, Any]=[1, 16, 4, 4] , lowerCAmelCase__ :Tuple=None , ) -> Tuple: __SCREAMING_SNAKE_CASE : Tuple = parent __SCREAMING_SNAKE_CASE : List[str] = batch_size __SCREAMING_SNAKE_CASE : Optional[Any] = image_size __SCREAMING_SNAKE_CASE : List[str] = patch_size __SCREAMING_SNAKE_CASE : str = num_channels __SCREAMING_SNAKE_CASE : Any = is_training __SCREAMING_SNAKE_CASE : List[Any] = use_labels __SCREAMING_SNAKE_CASE : List[str] = hidden_size __SCREAMING_SNAKE_CASE : Optional[int] = num_hidden_layers __SCREAMING_SNAKE_CASE : Tuple = num_attention_heads __SCREAMING_SNAKE_CASE : List[Any] = intermediate_size __SCREAMING_SNAKE_CASE : Tuple = hidden_act __SCREAMING_SNAKE_CASE : Optional[int] = hidden_dropout_prob __SCREAMING_SNAKE_CASE : Union[str, Any] = attention_probs_dropout_prob __SCREAMING_SNAKE_CASE : Optional[int] = type_sequence_label_size __SCREAMING_SNAKE_CASE : Optional[int] = initializer_range __SCREAMING_SNAKE_CASE : Tuple = scope __SCREAMING_SNAKE_CASE : List[Any] = backbone_featmap_shape # in ViT hybrid, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) # the number of patches is based on the feature map of the backbone, which by default uses an output stride # of 32, which means that the feature map has a spatial resolution of 1/32 of the input image size __SCREAMING_SNAKE_CASE : List[str] = (self.image_size // 32) ** 2 __SCREAMING_SNAKE_CASE : Union[str, Any] = num_patches + 1 def __magic_name__( self :str ) -> List[str]: __SCREAMING_SNAKE_CASE : List[Any] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) __SCREAMING_SNAKE_CASE : Optional[int] = None if self.use_labels: __SCREAMING_SNAKE_CASE : Union[str, Any] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __SCREAMING_SNAKE_CASE : str = self.get_config() return config, pixel_values, labels def __magic_name__( self :List[str] ) -> str: __SCREAMING_SNAKE_CASE : List[Any] = { '''global_padding''': '''same''', '''layer_type''': '''bottleneck''', '''depths''': [3, 4, 9], '''out_features''': ['''stage1''', '''stage2''', '''stage3'''], '''embedding_dynamic_padding''': True, '''hidden_sizes''': [4, 8, 16, 32], '''num_groups''': 2, } return ViTHybridConfig( 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=lowerCAmelCase__ , initializer_range=self.initializer_range , backbone_featmap_shape=self.backbone_featmap_shape , backbone_config=lowerCAmelCase__ , ) def __magic_name__( self :List[str] , lowerCAmelCase__ :Tuple , lowerCAmelCase__ :Optional[int] , lowerCAmelCase__ :Optional[Any] ) -> Dict: __SCREAMING_SNAKE_CASE : Union[str, Any] = ViTHybridModel(config=lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() __SCREAMING_SNAKE_CASE : Optional[Any] = model(lowerCAmelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __magic_name__( self :List[Any] , lowerCAmelCase__ :int , lowerCAmelCase__ :Tuple , lowerCAmelCase__ :Union[str, Any] ) -> Dict: __SCREAMING_SNAKE_CASE : Union[str, Any] = self.type_sequence_label_size __SCREAMING_SNAKE_CASE : Optional[Any] = ViTHybridForImageClassification(lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() __SCREAMING_SNAKE_CASE : List[Any] = model(lowerCAmelCase__ , labels=lowerCAmelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def __magic_name__( self :Optional[int] ) -> List[str]: __SCREAMING_SNAKE_CASE : Optional[int] = self.prepare_config_and_inputs() __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Dict = config_and_inputs __SCREAMING_SNAKE_CASE : Union[str, Any] = {'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class _lowercase ( A__ , A__ , unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Optional[Any] = (ViTHybridModel, ViTHybridForImageClassification) if is_torch_available() else () SCREAMING_SNAKE_CASE__ : Union[str, Any] = ( {'''feature-extraction''': ViTHybridModel, '''image-classification''': ViTHybridForImageClassification} if is_torch_available() else {} ) SCREAMING_SNAKE_CASE__ : Dict = False SCREAMING_SNAKE_CASE__ : str = False SCREAMING_SNAKE_CASE__ : Optional[int] = False def __magic_name__( self :Dict ) -> str: __SCREAMING_SNAKE_CASE : List[Any] = ViTHybridModelTester(self ) __SCREAMING_SNAKE_CASE : str = ConfigTester(self , config_class=lowerCAmelCase__ , has_text_modality=lowerCAmelCase__ , hidden_size=37 ) def __magic_name__( self :List[str] ) -> List[Any]: self.config_tester.run_common_tests() @unittest.skip(reason='''ViT does not use inputs_embeds''' ) def __magic_name__( self :Dict ) -> str: pass def __magic_name__( self :Any ) -> Optional[int]: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : str = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __SCREAMING_SNAKE_CASE : List[Any] = model_class(lowerCAmelCase__ ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) __SCREAMING_SNAKE_CASE : int = model.get_output_embeddings() self.assertTrue(x is None or isinstance(lowerCAmelCase__ , nn.Linear ) ) def __magic_name__( self :Any ) -> Optional[int]: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __SCREAMING_SNAKE_CASE : List[str] = model_class(lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Optional[Any] = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic __SCREAMING_SNAKE_CASE : List[str] = [*signature.parameters.keys()] __SCREAMING_SNAKE_CASE : str = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , lowerCAmelCase__ ) def __magic_name__( self :List[Any] ) -> Optional[int]: __SCREAMING_SNAKE_CASE : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCAmelCase__ ) def __magic_name__( self :str ) -> List[str]: __SCREAMING_SNAKE_CASE : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase__ ) def __magic_name__( self :Dict ) -> Optional[Any]: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Dict = self.model_tester.prepare_config_and_inputs_for_common() __SCREAMING_SNAKE_CASE : Tuple = _config_zero_init(lowerCAmelCase__ ) for model_class in self.all_model_classes: __SCREAMING_SNAKE_CASE : Dict = model_class(config=lowerCAmelCase__ ) # Skip the check for the backbone for name, module in model.named_modules(): if module.__class__.__name__ == "ViTHybridPatchEmbeddings": __SCREAMING_SNAKE_CASE : Any = [f'''{name}.{key}''' for key in module.state_dict().keys()] break for name, param in model.named_parameters(): if param.requires_grad: if name in backbone_params: continue 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 __magic_name__( self :Union[str, Any] ) -> int: for model_name in VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __SCREAMING_SNAKE_CASE : List[Any] = ViTHybridModel.from_pretrained(lowerCAmelCase__ ) self.assertIsNotNone(lowerCAmelCase__ ) def _UpperCamelCase ( ): __SCREAMING_SNAKE_CASE : Tuple = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_torch @require_vision class _lowercase ( unittest.TestCase ): '''simple docstring''' @cached_property def __magic_name__( self :List[Any] ) -> str: return ( ViTHybridImageProcessor.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) if is_vision_available() else None ) @slow def __magic_name__( self :List[str] ) -> Union[str, Any]: __SCREAMING_SNAKE_CASE : str = ViTHybridForImageClassification.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0] ).to( lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Tuple = self.default_image_processor __SCREAMING_SNAKE_CASE : Any = prepare_img() __SCREAMING_SNAKE_CASE : Optional[int] = image_processor(images=lowerCAmelCase__ , return_tensors='''pt''' ).to(lowerCAmelCase__ ) # forward pass with torch.no_grad(): __SCREAMING_SNAKE_CASE : Union[str, Any] = model(**lowerCAmelCase__ ) # verify the logits __SCREAMING_SNAKE_CASE : Tuple = torch.Size((1, 1_000) ) self.assertEqual(outputs.logits.shape , lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Tuple = torch.tensor([-1.9090, -0.4993, -0.2389] ).to(lowerCAmelCase__ ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , lowerCAmelCase__ , atol=1E-4 ) ) @slow @require_accelerate def __magic_name__( self :str ) -> str: __SCREAMING_SNAKE_CASE : Any = ViTHybridImageProcessor.from_pretrained('''google/vit-hybrid-base-bit-384''' ) __SCREAMING_SNAKE_CASE : Dict = ViTHybridForImageClassification.from_pretrained('''google/vit-hybrid-base-bit-384''' , device_map='''auto''' ) __SCREAMING_SNAKE_CASE : int = prepare_img() __SCREAMING_SNAKE_CASE : Dict = image_processor(images=lowerCAmelCase__ , return_tensors='''pt''' ) __SCREAMING_SNAKE_CASE : Union[str, Any] = model(**lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Any = outputs.logits # model predicts one of the 1000 ImageNet classes __SCREAMING_SNAKE_CASE : Optional[int] = logits.argmax(-1 ).item() self.assertTrue(model.config.idalabel[predicted_class_idx] , '''tabby, tabby cat''' )
9
def __snake_case ( _lowerCAmelCase : list ) -> list: if len(_lowerCAmelCase ) <= 1: return [tuple(_lowerCAmelCase )] A_ : Tuple = [] def generate(_lowerCAmelCase : int , _lowerCAmelCase : list ): A_ : List[str] = [0] * n res.append(tuple(_lowerCAmelCase ) ) A_ : int = 0 while i < n: if c[i] < i: if i % 2 == 0: A_ , A_ : str = arr[i], arr[0] else: A_ , A_ : List[str] = arr[i], arr[c[i]] res.append(tuple(_lowerCAmelCase ) ) c[i] += 1 A_ : Tuple = 0 else: A_ : Dict = 0 i += 1 generate(len(_lowerCAmelCase ) , _lowerCAmelCase ) return res if __name__ == "__main__": _lowerCAmelCase : str = input('''Enter numbers separated by a comma:\n''').strip() _lowerCAmelCase : str = [int(item) for item in user_input.split(''',''')] print(heaps(arr))
300
0
class _SCREAMING_SNAKE_CASE : '''simple docstring''' def __init__(self : Any , UpperCAmelCase_ : str = "" , UpperCAmelCase_ : bool = False) ->None: '''simple docstring''' lowerCamelCase__: dict[str, RadixNode] ={} # A node will be a leaf if the tree contains its word lowerCamelCase__: Any =is_leaf lowerCamelCase__: List[str] =prefix def SCREAMING_SNAKE_CASE_ (self : List[str] , UpperCAmelCase_ : str) ->tuple[str, str, str]: '''simple docstring''' lowerCamelCase__: Any =0 for q, w in zip(self.prefix , UpperCAmelCase_): if q != w: break x += 1 return self.prefix[:x], self.prefix[x:], word[x:] def SCREAMING_SNAKE_CASE_ (self : Optional[Any] , UpperCAmelCase_ : list[str]) ->None: '''simple docstring''' for word in words: self.insert(UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ (self : List[str] , UpperCAmelCase_ : str) ->None: '''simple docstring''' if self.prefix == word: lowerCamelCase__: Optional[int] =True # Case 2: The node has no edges that have a prefix to the word # Solution: We create an edge from the current node to a new one # containing the word elif word[0] not in self.nodes: lowerCamelCase__: Any =RadixNode(prefix=UpperCAmelCase_ , is_leaf=UpperCAmelCase_) else: lowerCamelCase__: Union[str, Any] =self.nodes[word[0]] lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__: Tuple =incoming_node.match( UpperCAmelCase_) # Case 3: The node prefix is equal to the matching # Solution: We insert remaining word on the next node if remaining_prefix == "": self.nodes[matching_string[0]].insert(UpperCAmelCase_) # Case 4: The word is greater equal to the matching # Solution: Create a node in between both nodes, change # prefixes and add the new node for the remaining word else: lowerCamelCase__: Union[str, Any] =remaining_prefix lowerCamelCase__: Optional[int] =self.nodes[matching_string[0]] lowerCamelCase__: Dict =RadixNode(UpperCAmelCase_ , UpperCAmelCase_) lowerCamelCase__: Tuple =aux_node if remaining_word == "": lowerCamelCase__: Dict =True else: self.nodes[matching_string[0]].insert(UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ (self : int , UpperCAmelCase_ : str) ->bool: '''simple docstring''' lowerCamelCase__: Dict =self.nodes.get(word[0] , UpperCAmelCase_) if not incoming_node: return False else: lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__: str =incoming_node.match( UpperCAmelCase_) # If there is remaining prefix, the word can't be on the tree if remaining_prefix != "": return False # This applies when the word and the prefix are equal elif remaining_word == "": return incoming_node.is_leaf # We have word remaining so we check the next node else: return incoming_node.find(UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ (self : Any , UpperCAmelCase_ : str) ->bool: '''simple docstring''' lowerCamelCase__: Union[str, Any] =self.nodes.get(word[0] , UpperCAmelCase_) if not incoming_node: return False else: lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__: List[Any] =incoming_node.match( UpperCAmelCase_) # If there is remaining prefix, the word can't be on the tree if remaining_prefix != "": return False # We have word remaining so we check the next node elif remaining_word != "": return incoming_node.delete(UpperCAmelCase_) else: # If it is not a leaf, we don't have to delete if not incoming_node.is_leaf: return False else: # We delete the nodes if no edges go from it if len(incoming_node.nodes) == 0: del self.nodes[word[0]] # We merge the current node with its only child if len(self.nodes) == 1 and not self.is_leaf: lowerCamelCase__: int =list(self.nodes.values())[0] lowerCamelCase__: Any =merging_node.is_leaf self.prefix += merging_node.prefix lowerCamelCase__: Tuple =merging_node.nodes # If there is more than 1 edge, we just mark it as non-leaf elif len(incoming_node.nodes) > 1: lowerCamelCase__: Dict =False # If there is 1 edge, we merge it with its child else: lowerCamelCase__: str =list(incoming_node.nodes.values())[0] lowerCamelCase__: Any =merging_node.is_leaf incoming_node.prefix += merging_node.prefix lowerCamelCase__: Optional[Any] =merging_node.nodes return True def SCREAMING_SNAKE_CASE_ (self : List[Any] , UpperCAmelCase_ : int = 0) ->None: '''simple docstring''' if self.prefix != "": print("-" * height , self.prefix , " (leaf)" if self.is_leaf else "") for value in self.nodes.values(): value.print_tree(height + 1) def lowerCAmelCase_ ( ) -> bool: """simple docstring""" lowerCamelCase__: str ="banana bananas bandana band apple all beast".split() lowerCamelCase__: List[Any] =RadixNode() root.insert_many(__a ) assert all(root.find(__a ) for word in words ) assert not root.find("bandanas" ) assert not root.find("apps" ) root.delete("all" ) assert not root.find("all" ) root.delete("banana" ) assert not root.find("banana" ) assert root.find("bananas" ) return True def lowerCAmelCase_ ( ) -> None: """simple docstring""" assert test_trie() def lowerCAmelCase_ ( ) -> None: """simple docstring""" lowerCamelCase__: Optional[int] =RadixNode() lowerCamelCase__: Optional[int] ="banana bananas bandanas bandana band apple all beast".split() root.insert_many(__a ) print("Words:" , __a ) print("Tree:" ) root.print_tree() if __name__ == "__main__": main()
10
import json from typing import List, Optional, Tuple from tokenizers import pre_tokenizers, processors from ...tokenization_utils_base import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_roberta import RobertaTokenizer _lowerCAmelCase : int = logging.get_logger(__name__) _lowerCAmelCase : Optional[int] = {'''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt''', '''tokenizer_file''': '''tokenizer.json'''} _lowerCAmelCase : List[Any] = { '''vocab_file''': { '''roberta-base''': '''https://huggingface.co/roberta-base/resolve/main/vocab.json''', '''roberta-large''': '''https://huggingface.co/roberta-large/resolve/main/vocab.json''', '''roberta-large-mnli''': '''https://huggingface.co/roberta-large-mnli/resolve/main/vocab.json''', '''distilroberta-base''': '''https://huggingface.co/distilroberta-base/resolve/main/vocab.json''', '''roberta-base-openai-detector''': '''https://huggingface.co/roberta-base-openai-detector/resolve/main/vocab.json''', '''roberta-large-openai-detector''': ( '''https://huggingface.co/roberta-large-openai-detector/resolve/main/vocab.json''' ), }, '''merges_file''': { '''roberta-base''': '''https://huggingface.co/roberta-base/resolve/main/merges.txt''', '''roberta-large''': '''https://huggingface.co/roberta-large/resolve/main/merges.txt''', '''roberta-large-mnli''': '''https://huggingface.co/roberta-large-mnli/resolve/main/merges.txt''', '''distilroberta-base''': '''https://huggingface.co/distilroberta-base/resolve/main/merges.txt''', '''roberta-base-openai-detector''': '''https://huggingface.co/roberta-base-openai-detector/resolve/main/merges.txt''', '''roberta-large-openai-detector''': ( '''https://huggingface.co/roberta-large-openai-detector/resolve/main/merges.txt''' ), }, '''tokenizer_file''': { '''roberta-base''': '''https://huggingface.co/roberta-base/resolve/main/tokenizer.json''', '''roberta-large''': '''https://huggingface.co/roberta-large/resolve/main/tokenizer.json''', '''roberta-large-mnli''': '''https://huggingface.co/roberta-large-mnli/resolve/main/tokenizer.json''', '''distilroberta-base''': '''https://huggingface.co/distilroberta-base/resolve/main/tokenizer.json''', '''roberta-base-openai-detector''': ( '''https://huggingface.co/roberta-base-openai-detector/resolve/main/tokenizer.json''' ), '''roberta-large-openai-detector''': ( '''https://huggingface.co/roberta-large-openai-detector/resolve/main/tokenizer.json''' ), }, } _lowerCAmelCase : Any = { '''roberta-base''': 512, '''roberta-large''': 512, '''roberta-large-mnli''': 512, '''distilroberta-base''': 512, '''roberta-base-openai-detector''': 512, '''roberta-large-openai-detector''': 512, } class __magic_name__ ( lowerCamelCase__ ): """simple docstring""" __UpperCamelCase = VOCAB_FILES_NAMES __UpperCamelCase = PRETRAINED_VOCAB_FILES_MAP __UpperCamelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCamelCase = ['''input_ids''', '''attention_mask'''] __UpperCamelCase = RobertaTokenizer def __init__( self :Dict , snake_case :List[str]=None , snake_case :List[Any]=None , snake_case :Union[str, Any]=None , snake_case :List[str]="replace" , snake_case :Tuple="<s>" , snake_case :Union[str, Any]="</s>" , snake_case :str="</s>" , snake_case :Union[str, Any]="<s>" , snake_case :int="<unk>" , snake_case :Tuple="<pad>" , snake_case :List[str]="<mask>" , snake_case :Any=False , snake_case :Union[str, Any]=True , **snake_case :Optional[int] , ): '''simple docstring''' super().__init__( snake_case , snake_case , tokenizer_file=snake_case , errors=snake_case , bos_token=snake_case , eos_token=snake_case , sep_token=snake_case , cls_token=snake_case , unk_token=snake_case , pad_token=snake_case , mask_token=snake_case , add_prefix_space=snake_case , trim_offsets=snake_case , **snake_case , ) A_ : Optional[Any] = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get("add_prefix_space" , snake_case ) != add_prefix_space: A_ : Dict = getattr(snake_case , pre_tok_state.pop("type" ) ) A_ : Optional[int] = add_prefix_space A_ : int = pre_tok_class(**snake_case ) A_ : Optional[int] = add_prefix_space A_ : Optional[int] = "post_processor" A_ : Dict = getattr(self.backend_tokenizer , snake_case , snake_case ) if tokenizer_component_instance: A_ : Dict = json.loads(tokenizer_component_instance.__getstate__() ) # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class` if "sep" in state: A_ : List[Any] = tuple(state["sep"] ) if "cls" in state: A_ : Optional[Any] = tuple(state["cls"] ) A_ : Tuple = False if state.get("add_prefix_space" , snake_case ) != add_prefix_space: A_ : List[Any] = add_prefix_space A_ : Optional[int] = True if state.get("trim_offsets" , snake_case ) != trim_offsets: A_ : List[str] = trim_offsets A_ : Any = True if changes_to_apply: A_ : Optional[Any] = getattr(snake_case , state.pop("type" ) ) A_ : Any = component_class(**snake_case ) setattr(self.backend_tokenizer , snake_case , snake_case ) @property def SCREAMING_SNAKE_CASE ( self :List[Any] ): '''simple docstring''' if self._mask_token is None: if self.verbose: logger.error("Using mask_token, but it is not set yet." ) return None return str(self._mask_token ) @mask_token.setter def SCREAMING_SNAKE_CASE ( self :Any , snake_case :Dict ): '''simple docstring''' A_ : Dict = AddedToken(snake_case , lstrip=snake_case , rstrip=snake_case ) if isinstance(snake_case , snake_case ) else value A_ : Any = value def SCREAMING_SNAKE_CASE ( self :Dict , *snake_case :Tuple , **snake_case :Union[str, Any] ): '''simple docstring''' A_ : Any = kwargs.get("is_split_into_words" , snake_case ) assert self.add_prefix_space or not is_split_into_words, ( f"You need to instantiate {self.__class__.__name__} with add_prefix_space=True " "to use it with pretokenized inputs." ) return super()._batch_encode_plus(*snake_case , **snake_case ) def SCREAMING_SNAKE_CASE ( self :List[str] , *snake_case :str , **snake_case :Union[str, Any] ): '''simple docstring''' A_ : Any = kwargs.get("is_split_into_words" , snake_case ) assert self.add_prefix_space or not is_split_into_words, ( f"You need to instantiate {self.__class__.__name__} with add_prefix_space=True " "to use it with pretokenized inputs." ) return super()._encode_plus(*snake_case , **snake_case ) def SCREAMING_SNAKE_CASE ( self :Union[str, Any] , snake_case :str , snake_case :Optional[str] = None ): '''simple docstring''' A_ : str = self._tokenizer.model.save(snake_case , name=snake_case ) return tuple(snake_case ) def SCREAMING_SNAKE_CASE ( self :List[str] , snake_case :List[str] , snake_case :Optional[Any]=None ): '''simple docstring''' A_ : int = [self.bos_token_id] + token_ids_a + [self.eos_token_id] if token_ids_a is None: return output return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id] def SCREAMING_SNAKE_CASE ( self :Any , snake_case :List[int] , snake_case :Optional[List[int]] = None ): '''simple docstring''' A_ : Any = [self.sep_token_id] A_ : Optional[Any] = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0]
300
0
from __future__ import annotations from collections import deque from collections.abc import Sequence from dataclasses import dataclass from typing import Any @dataclass class lowerCAmelCase__ : '''simple docstring''' __SCREAMING_SNAKE_CASE = 42 __SCREAMING_SNAKE_CASE = None __SCREAMING_SNAKE_CASE = None def _UpperCAmelCase (): _A : List[str] = Node(1 ) _A : Optional[Any] = Node(2 ) _A : Dict = Node(3 ) _A : Optional[Any] = Node(4 ) _A : str = Node(5 ) return tree def _UpperCAmelCase (UpperCamelCase__ : Node | None ): return [root.data, *preorder(root.left ), *preorder(root.right )] if root else [] def _UpperCAmelCase (UpperCamelCase__ : Node | None ): return postorder(root.left ) + postorder(root.right ) + [root.data] if root else [] def _UpperCAmelCase (UpperCamelCase__ : Node | None ): return [*inorder(root.left ), root.data, *inorder(root.right )] if root else [] def _UpperCAmelCase (UpperCamelCase__ : Node | None ): return (max(height(root.left ) , height(root.right ) ) + 1) if root else 0 def _UpperCAmelCase (UpperCamelCase__ : Node | None ): _A : list[Any] = [] if root is None: return output _A : Union[str, Any] = deque([root] ) while process_queue: _A : str = process_queue.popleft() output.append(node.data ) if node.left: process_queue.append(node.left ) if node.right: process_queue.append(node.right ) return output def _UpperCAmelCase (UpperCamelCase__ : Node | None , UpperCamelCase__ : int ): _A : list[Any] = [] def populate_output(UpperCamelCase__ : Node | None , UpperCamelCase__ : int ) -> None: if not root: return if level == 1: output.append(root.data ) elif level > 1: populate_output(root.left , level - 1 ) populate_output(root.right , level - 1 ) populate_output(UpperCamelCase__ , UpperCamelCase__ ) return output def _UpperCAmelCase (UpperCamelCase__ : Node | None , UpperCamelCase__ : int ): _A : list[Any] = [] def populate_output(UpperCamelCase__ : Node | None , UpperCamelCase__ : int ) -> None: if root is None: return if level == 1: output.append(root.data ) elif level > 1: populate_output(root.right , level - 1 ) populate_output(root.left , level - 1 ) populate_output(UpperCamelCase__ , UpperCamelCase__ ) return output def _UpperCAmelCase (UpperCamelCase__ : Node | None ): if root is None: return [] _A : list[Sequence[Node | None]] = [] _A : Dict = 0 _A : str = height(UpperCamelCase__ ) for h in range(1 , height_tree + 1 ): if not flag: output.append(get_nodes_from_left_to_right(UpperCamelCase__ , UpperCamelCase__ ) ) _A : Optional[int] = 1 else: output.append(get_nodes_from_right_to_left(UpperCamelCase__ , UpperCamelCase__ ) ) _A : Tuple = 0 return output def _UpperCAmelCase (): # Main function for testing. _A : str = make_tree() print(f"In-order Traversal: {inorder(UpperCamelCase__ )}" ) print(f"Pre-order Traversal: {preorder(UpperCamelCase__ )}" ) print(f"Post-order Traversal: {postorder(UpperCamelCase__ )}" , "\n" ) print(f"Height of Tree: {height(UpperCamelCase__ )}" , "\n" ) print("Complete Level Order Traversal: " ) print(level_order(UpperCamelCase__ ) , "\n" ) print("Level-wise order Traversal: " ) for level in range(1 , height(UpperCamelCase__ ) + 1 ): print(f"Level {level}:" , get_nodes_from_left_to_right(UpperCamelCase__ , level=UpperCamelCase__ ) ) print("\nZigZag order Traversal: " ) print(zigzag(UpperCamelCase__ ) ) if __name__ == "__main__": import doctest doctest.testmod() main()
11
from typing import Dict, List from nltk.translate import gleu_score import datasets from datasets import MetricInfo _lowerCAmelCase : int = '''\ @misc{wu2016googles, title={Google\'s Neural Machine Translation System: Bridging the Gap between Human and Machine Translation}, author={Yonghui Wu and Mike Schuster and Zhifeng Chen and Quoc V. Le and Mohammad Norouzi and Wolfgang Macherey and Maxim Krikun and Yuan Cao and Qin Gao and Klaus Macherey and Jeff Klingner and Apurva Shah and Melvin Johnson and Xiaobing Liu and Łukasz Kaiser and Stephan Gouws and Yoshikiyo Kato and Taku Kudo and Hideto Kazawa and Keith Stevens and George Kurian and Nishant Patil and Wei Wang and Cliff Young and Jason Smith and Jason Riesa and Alex Rudnick and Oriol Vinyals and Greg Corrado and Macduff Hughes and Jeffrey Dean}, year={2016}, eprint={1609.08144}, archivePrefix={arXiv}, primaryClass={cs.CL} } ''' _lowerCAmelCase : Tuple = '''\ The BLEU score has some undesirable properties when used for single sentences, as it was designed to be a corpus measure. We therefore use a slightly different score for our RL experiments which we call the \'GLEU score\'. For the GLEU score, we record all sub-sequences of 1, 2, 3 or 4 tokens in output and target sequence (n-grams). We then compute a recall, which is the ratio of the number of matching n-grams to the number of total n-grams in the target (ground truth) sequence, and a precision, which is the ratio of the number of matching n-grams to the number of total n-grams in the generated output sequence. Then GLEU score is simply the minimum of recall and precision. This GLEU score\'s range is always between 0 (no matches) and 1 (all match) and it is symmetrical when switching output and target. According to our experiments, GLEU score correlates quite well with the BLEU metric on a corpus level but does not have its drawbacks for our per sentence reward objective. ''' _lowerCAmelCase : int = '''\ Computes corpus-level Google BLEU (GLEU) score of translated segments against one or more references. Instead of averaging the sentence level GLEU scores (i.e. macro-average precision), Wu et al. (2016) sum up the matching tokens and the max of hypothesis and reference tokens for each sentence, then compute using the aggregate values. Args: predictions (list of str): list of translations to score. Each translation should be tokenized into a list of tokens. references (list of list of str): list of lists of references for each translation. Each reference should be tokenized into a list of tokens. min_len (int): The minimum order of n-gram this function should extract. Defaults to 1. max_len (int): The maximum order of n-gram this function should extract. Defaults to 4. Returns: \'google_bleu\': google_bleu score Examples: Example 1: >>> hyp1 = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'which\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'always\', ... \'disobeys\', \'the\', \'commands\', \'of\', \'the\', \'cat\'] >>> ref1a = [\'It\', \'is\', \'the\', \'guiding\', \'principle\', \'which\', ... \'guarantees\', \'the\', \'rubber\', \'duck\', \'forces\', \'never\', ... \'being\', \'under\', \'the\', \'command\', \'of\', \'the\', \'cat\'] >>> hyp2 = [\'he\', \'read\', \'the\', \'book\', \'because\', \'he\', \'was\', ... \'interested\', \'in\', \'world\', \'history\'] >>> ref2a = [\'he\', \'was\', \'interested\', \'in\', \'world\', \'history\', ... \'because\', \'he\', \'read\', \'the\', \'book\'] >>> list_of_references = [[ref1a], [ref2a]] >>> hypotheses = [hyp1, hyp2] >>> google_bleu = datasets.load_metric("google_bleu") >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references) >>> print(round(results["google_bleu"], 2)) 0.44 Example 2: >>> hyp1 = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'which\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'always\', ... \'disobeys\', \'the\', \'commands\', \'of\', \'the\', \'cat\'] >>> ref1a = [\'It\', \'is\', \'the\', \'guiding\', \'principle\', \'which\', ... \'guarantees\', \'the\', \'rubber\', \'duck\', \'forces\', \'never\', ... \'being\', \'under\', \'the\', \'command\', \'of\', \'the\', \'cat\'] >>> ref1b = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'that\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'will\', \'never\', ... \'heed\', \'the\', \'cat\', \'commands\'] >>> ref1c = [\'It\', \'is\', \'the\', \'practical\', \'guide\', \'for\', \'the\', ... \'rubber\', \'duck\', \'army\', \'never\', \'to\', \'heed\', \'the\', \'directions\', ... \'of\', \'the\', \'cat\'] >>> hyp2 = [\'he\', \'read\', \'the\', \'book\', \'because\', \'he\', \'was\', ... \'interested\', \'in\', \'world\', \'history\'] >>> ref2a = [\'he\', \'was\', \'interested\', \'in\', \'world\', \'history\', ... \'because\', \'he\', \'read\', \'the\', \'book\'] >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]] >>> hypotheses = [hyp1, hyp2] >>> google_bleu = datasets.load_metric("google_bleu") >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references) >>> print(round(results["google_bleu"], 2)) 0.61 Example 3: >>> hyp1 = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'which\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'always\', ... \'disobeys\', \'the\', \'commands\', \'of\', \'the\', \'cat\'] >>> ref1a = [\'It\', \'is\', \'the\', \'guiding\', \'principle\', \'which\', ... \'guarantees\', \'the\', \'rubber\', \'duck\', \'forces\', \'never\', ... \'being\', \'under\', \'the\', \'command\', \'of\', \'the\', \'cat\'] >>> ref1b = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'that\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'will\', \'never\', ... \'heed\', \'the\', \'cat\', \'commands\'] >>> ref1c = [\'It\', \'is\', \'the\', \'practical\', \'guide\', \'for\', \'the\', ... \'rubber\', \'duck\', \'army\', \'never\', \'to\', \'heed\', \'the\', \'directions\', ... \'of\', \'the\', \'cat\'] >>> hyp2 = [\'he\', \'read\', \'the\', \'book\', \'because\', \'he\', \'was\', ... \'interested\', \'in\', \'world\', \'history\'] >>> ref2a = [\'he\', \'was\', \'interested\', \'in\', \'world\', \'history\', ... \'because\', \'he\', \'read\', \'the\', \'book\'] >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]] >>> hypotheses = [hyp1, hyp2] >>> google_bleu = datasets.load_metric("google_bleu") >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references, min_len=2) >>> print(round(results["google_bleu"], 2)) 0.53 Example 4: >>> hyp1 = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'which\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'always\', ... \'disobeys\', \'the\', \'commands\', \'of\', \'the\', \'cat\'] >>> ref1a = [\'It\', \'is\', \'the\', \'guiding\', \'principle\', \'which\', ... \'guarantees\', \'the\', \'rubber\', \'duck\', \'forces\', \'never\', ... \'being\', \'under\', \'the\', \'command\', \'of\', \'the\', \'cat\'] >>> ref1b = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'that\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'will\', \'never\', ... \'heed\', \'the\', \'cat\', \'commands\'] >>> ref1c = [\'It\', \'is\', \'the\', \'practical\', \'guide\', \'for\', \'the\', ... \'rubber\', \'duck\', \'army\', \'never\', \'to\', \'heed\', \'the\', \'directions\', ... \'of\', \'the\', \'cat\'] >>> hyp2 = [\'he\', \'read\', \'the\', \'book\', \'because\', \'he\', \'was\', ... \'interested\', \'in\', \'world\', \'history\'] >>> ref2a = [\'he\', \'was\', \'interested\', \'in\', \'world\', \'history\', ... \'because\', \'he\', \'read\', \'the\', \'book\'] >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]] >>> hypotheses = [hyp1, hyp2] >>> google_bleu = datasets.load_metric("google_bleu") >>> results = google_bleu.compute(predictions=hypotheses,references=list_of_references, min_len=2, max_len=6) >>> print(round(results["google_bleu"], 2)) 0.4 ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __magic_name__ ( datasets.Metric ): """simple docstring""" def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "predictions": datasets.Sequence(datasets.Value("string" , id="token" ) , id="sequence" ), "references": datasets.Sequence( datasets.Sequence(datasets.Value("string" , id="token" ) , id="sequence" ) , id="references" ), } ) , ) def SCREAMING_SNAKE_CASE ( self :int , snake_case :List[List[List[str]]] , snake_case :List[List[str]] , snake_case :int = 1 , snake_case :int = 4 , ): '''simple docstring''' return { "google_bleu": gleu_score.corpus_gleu( list_of_references=snake_case , hypotheses=snake_case , min_len=snake_case , max_len=snake_case ) }
300
0
import os import re import shutil import sys import tempfile import unittest import black UpperCAmelCase_ = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) sys.path.append(os.path.join(git_repo_path, 'utils')) import check_copies # noqa: E402 # This is the reference code that will be used in the tests. # If BertLMPredictionHead is changed in modeling_bert.py, this code needs to be manually updated. UpperCAmelCase_ = ' def __init__(self, config):\n super().__init__()\n self.transform = BertPredictionHeadTransform(config)\n\n # The output weights are the same as the input embeddings, but there is\n # an output-only bias for each token.\n self.decoder = nn.Linear(config.hidden_size, config.vocab_size, bias=False)\n\n self.bias = nn.Parameter(torch.zeros(config.vocab_size))\n\n # Need a link between the two variables so that the bias is correctly resized with `resize_token_embeddings`\n self.decoder.bias = self.bias\n\n def forward(self, hidden_states):\n hidden_states = self.transform(hidden_states)\n hidden_states = self.decoder(hidden_states)\n return hidden_states\n' class lowerCamelCase__( unittest.TestCase): def lowerCAmelCase__ ( self: List[str] ): __lowerCamelCase = tempfile.mkdtemp() os.makedirs(os.path.join(self.transformer_dir , """models/bert/""" ) ) __lowerCamelCase = self.transformer_dir shutil.copy( os.path.join(UpperCamelCase_ , """src/transformers/models/bert/modeling_bert.py""" ) , os.path.join(self.transformer_dir , """models/bert/modeling_bert.py""" ) , ) def lowerCAmelCase__ ( self: int ): __lowerCamelCase = """src/transformers""" shutil.rmtree(self.transformer_dir ) def lowerCAmelCase__ ( self: List[Any] , UpperCamelCase_: List[str] , UpperCamelCase_: Any , UpperCamelCase_: List[Any] , UpperCamelCase_: List[str]=None ): __lowerCamelCase = comment + F'\nclass {class_name}(nn.Module):\n' + class_code if overwrite_result is not None: __lowerCamelCase = comment + F'\nclass {class_name}(nn.Module):\n' + overwrite_result __lowerCamelCase = black.Mode(target_versions={black.TargetVersion.PYaa} , line_length=1_19 ) __lowerCamelCase = black.format_str(UpperCamelCase_ , mode=UpperCamelCase_ ) __lowerCamelCase = os.path.join(self.transformer_dir , """new_code.py""" ) with open(UpperCamelCase_ , """w""" , newline="""\n""" ) as f: f.write(UpperCamelCase_ ) if overwrite_result is None: self.assertTrue(len(check_copies.is_copy_consistent(UpperCamelCase_ ) ) == 0 ) else: check_copies.is_copy_consistent(f.name , overwrite=UpperCamelCase_ ) with open(UpperCamelCase_ , """r""" ) as f: self.assertTrue(f.read() , UpperCamelCase_ ) def lowerCAmelCase__ ( self: Union[str, Any] ): __lowerCamelCase = check_copies.find_code_in_transformers("""models.bert.modeling_bert.BertLMPredictionHead""" ) self.assertEqual(UpperCamelCase_ , UpperCamelCase_ ) def lowerCAmelCase__ ( self: Dict ): # Base copy consistency self.check_copy_consistency( """# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead""" , """BertLMPredictionHead""" , REFERENCE_CODE + """\n""" , ) # With no empty line at the end self.check_copy_consistency( """# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead""" , """BertLMPredictionHead""" , UpperCamelCase_ , ) # Copy consistency with rename self.check_copy_consistency( """# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->TestModel""" , """TestModelLMPredictionHead""" , re.sub("""Bert""" , """TestModel""" , UpperCamelCase_ ) , ) # Copy consistency with a really long name __lowerCamelCase = """TestModelWithAReallyLongNameBecauseSomePeopleLikeThatForSomeReason""" self.check_copy_consistency( F'# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->{long_class_name}' , F'{long_class_name}LMPredictionHead' , re.sub("""Bert""" , UpperCamelCase_ , UpperCamelCase_ ) , ) # Copy consistency with overwrite self.check_copy_consistency( """# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->TestModel""" , """TestModelLMPredictionHead""" , UpperCamelCase_ , overwrite_result=re.sub("""Bert""" , """TestModel""" , UpperCamelCase_ ) , ) def lowerCAmelCase__ ( self: Dict ): __lowerCamelCase = check_copies.LOCALIZED_READMES["""README_zh-hans.md"""] __lowerCamelCase = ( """1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (from Google Research and the""" """ Toyota Technological Institute at Chicago) released with the paper [ALBERT: A Lite BERT for""" """ Self-supervised Learning of Language Representations](https://arxiv.org/abs/1909.11942), by Zhenzhong""" """ Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut.\n1.""" """ **[DistilBERT](https://huggingface.co/transformers/model_doc/distilbert.html)** (from HuggingFace),""" """ released together with the paper [DistilBERT, a distilled version of BERT: smaller, faster, cheaper and""" """ lighter](https://arxiv.org/abs/1910.01108) by Victor Sanh, Lysandre Debut and Thomas Wolf. The same""" """ method has been applied to compress GPT2 into""" """ [DistilGPT2](https://github.com/huggingface/transformers/tree/main/examples/distillation), RoBERTa into""" """ [DistilRoBERTa](https://github.com/huggingface/transformers/tree/main/examples/distillation),""" """ Multilingual BERT into""" """ [DistilmBERT](https://github.com/huggingface/transformers/tree/main/examples/distillation) and a German""" """ version of DistilBERT.\n1. **[ELECTRA](https://huggingface.co/transformers/model_doc/electra.html)**""" """ (from Google Research/Stanford University) released with the paper [ELECTRA: Pre-training text encoders""" """ as discriminators rather than generators](https://arxiv.org/abs/2003.10555) by Kevin Clark, Minh-Thang""" """ Luong, Quoc V. Le, Christopher D. Manning.""" ) __lowerCamelCase = ( """1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the""" """ Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of""" """ Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian""" """ Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n""" ) __lowerCamelCase = ( """1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the""" """ Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of""" """ Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian""" """ Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n1.""" """ **[DistilBERT](https://huggingface.co/transformers/model_doc/distilbert.html)** (来自 HuggingFace) 伴随论文""" """ [DistilBERT, a distilled version of BERT: smaller, faster, cheaper and""" """ lighter](https://arxiv.org/abs/1910.01108) 由 Victor Sanh, Lysandre Debut and Thomas Wolf 发布。 The same""" """ method has been applied to compress GPT2 into""" """ [DistilGPT2](https://github.com/huggingface/transformers/tree/main/examples/distillation), RoBERTa into""" """ [DistilRoBERTa](https://github.com/huggingface/transformers/tree/main/examples/distillation),""" """ Multilingual BERT into""" """ [DistilmBERT](https://github.com/huggingface/transformers/tree/main/examples/distillation) and a German""" """ version of DistilBERT.\n1. **[ELECTRA](https://huggingface.co/transformers/model_doc/electra.html)** (来自""" """ Google Research/Stanford University) 伴随论文 [ELECTRA: Pre-training text encoders as discriminators rather""" """ than generators](https://arxiv.org/abs/2003.10555) 由 Kevin Clark, Minh-Thang Luong, Quoc V. Le,""" """ Christopher D. Manning 发布。\n""" ) __lowerCamelCase, __lowerCamelCase = check_copies.convert_to_localized_md( UpperCamelCase_ , UpperCamelCase_ , localized_readme["""format_model_list"""] ) self.assertFalse(UpperCamelCase_ ) self.assertEqual(UpperCamelCase_ , UpperCamelCase_ ) __lowerCamelCase, __lowerCamelCase = check_copies.convert_to_localized_md( UpperCamelCase_ , UpperCamelCase_ , localized_readme["""format_model_list"""] ) # Check whether the number of models is equal to README.md after conversion. self.assertTrue(UpperCamelCase_ ) __lowerCamelCase = ( """1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (from Google Research and the""" """ Toyota Technological Institute at Chicago) released with the paper [ALBERT: A Lite BERT for""" """ Self-supervised Learning of Language Representations](https://arxiv.org/abs/1909.11942), by Zhenzhong""" """ Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut.""" ) __lowerCamelCase = ( """1. **[ALBERT](https://huggingface.co/transformers/main/model_doc/albert.html)** (来自 Google Research and""" """ the Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of""" """ Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian""" """ Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n""" ) __lowerCamelCase = ( """1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the""" """ Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of""" """ Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian""" """ Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n""" ) __lowerCamelCase, __lowerCamelCase = check_copies.convert_to_localized_md( UpperCamelCase_ , UpperCamelCase_ , localized_readme["""format_model_list"""] ) # Check if the model link is synchronized. self.assertEqual(UpperCamelCase_ , UpperCamelCase_ )
12
import pyarrow.parquet as pq import pytest from datasets import Audio, Dataset, DatasetDict, Features, NamedSplit, Sequence, Value, config from datasets.features.image import Image from datasets.io.parquet import ParquetDatasetReader, ParquetDatasetWriter, get_writer_batch_size from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases def __snake_case ( _lowerCAmelCase : List[Any] , _lowerCAmelCase : Optional[int] ) -> str: assert isinstance(_lowerCAmelCase , _lowerCAmelCase ) assert dataset.num_rows == 4 assert dataset.num_columns == 3 assert dataset.column_names == ["col_1", "col_2", "col_3"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize("keep_in_memory" , [False, True] ) def __snake_case ( _lowerCAmelCase : Dict , _lowerCAmelCase : Optional[int] , _lowerCAmelCase : List[Any] ) -> Optional[int]: A_ : Tuple = tmp_path / "cache" A_ : Optional[int] = {"col_1": "string", "col_2": "int64", "col_3": "float64"} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): A_ : Optional[Any] = ParquetDatasetReader(_lowerCAmelCase , cache_dir=_lowerCAmelCase , keep_in_memory=_lowerCAmelCase ).read() _check_parquet_dataset(_lowerCAmelCase , _lowerCAmelCase ) @pytest.mark.parametrize( "features" , [ None, {"col_1": "string", "col_2": "int64", "col_3": "float64"}, {"col_1": "string", "col_2": "string", "col_3": "string"}, {"col_1": "int32", "col_2": "int32", "col_3": "int32"}, {"col_1": "float32", "col_2": "float32", "col_3": "float32"}, ] , ) def __snake_case ( _lowerCAmelCase : List[Any] , _lowerCAmelCase : Any , _lowerCAmelCase : List[Any] ) -> str: A_ : List[Any] = tmp_path / "cache" A_ : List[str] = {"col_1": "string", "col_2": "int64", "col_3": "float64"} A_ : int = features.copy() if features else default_expected_features A_ : str = ( Features({feature: Value(_lowerCAmelCase ) for feature, dtype in features.items()} ) if features is not None else None ) A_ : Union[str, Any] = ParquetDatasetReader(_lowerCAmelCase , features=_lowerCAmelCase , cache_dir=_lowerCAmelCase ).read() _check_parquet_dataset(_lowerCAmelCase , _lowerCAmelCase ) @pytest.mark.parametrize("split" , [None, NamedSplit("train" ), "train", "test"] ) def __snake_case ( _lowerCAmelCase : Any , _lowerCAmelCase : Any , _lowerCAmelCase : Any ) -> Optional[Any]: A_ : Dict = tmp_path / "cache" A_ : int = {"col_1": "string", "col_2": "int64", "col_3": "float64"} A_ : Optional[int] = ParquetDatasetReader(_lowerCAmelCase , cache_dir=_lowerCAmelCase , split=_lowerCAmelCase ).read() _check_parquet_dataset(_lowerCAmelCase , _lowerCAmelCase ) assert dataset.split == split if split else "train" @pytest.mark.parametrize("path_type" , [str, list] ) def __snake_case ( _lowerCAmelCase : List[str] , _lowerCAmelCase : int , _lowerCAmelCase : Optional[Any] ) -> List[str]: if issubclass(_lowerCAmelCase , _lowerCAmelCase ): A_ : int = parquet_path elif issubclass(_lowerCAmelCase , _lowerCAmelCase ): A_ : Optional[int] = [parquet_path] A_ : Optional[int] = tmp_path / "cache" A_ : Union[str, Any] = {"col_1": "string", "col_2": "int64", "col_3": "float64"} A_ : Optional[int] = ParquetDatasetReader(_lowerCAmelCase , cache_dir=_lowerCAmelCase ).read() _check_parquet_dataset(_lowerCAmelCase , _lowerCAmelCase ) def __snake_case ( _lowerCAmelCase : Any , _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : Union[str, Any]=("train",) ) -> Tuple: assert isinstance(_lowerCAmelCase , _lowerCAmelCase ) for split in splits: A_ : List[str] = dataset_dict[split] assert dataset.num_rows == 4 assert dataset.num_columns == 3 assert dataset.column_names == ["col_1", "col_2", "col_3"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize("keep_in_memory" , [False, True] ) def __snake_case ( _lowerCAmelCase : Optional[Any] , _lowerCAmelCase : List[Any] , _lowerCAmelCase : Dict ) -> Optional[int]: A_ : Optional[Any] = tmp_path / "cache" A_ : Union[str, Any] = {"col_1": "string", "col_2": "int64", "col_3": "float64"} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): A_ : Union[str, Any] = ParquetDatasetReader( {"train": parquet_path} , cache_dir=_lowerCAmelCase , keep_in_memory=_lowerCAmelCase ).read() _check_parquet_datasetdict(_lowerCAmelCase , _lowerCAmelCase ) @pytest.mark.parametrize( "features" , [ None, {"col_1": "string", "col_2": "int64", "col_3": "float64"}, {"col_1": "string", "col_2": "string", "col_3": "string"}, {"col_1": "int32", "col_2": "int32", "col_3": "int32"}, {"col_1": "float32", "col_2": "float32", "col_3": "float32"}, ] , ) def __snake_case ( _lowerCAmelCase : Tuple , _lowerCAmelCase : Any , _lowerCAmelCase : str ) -> Tuple: A_ : Optional[Any] = tmp_path / "cache" A_ : Any = {"col_1": "string", "col_2": "int64", "col_3": "float64"} A_ : List[str] = features.copy() if features else default_expected_features A_ : Tuple = ( Features({feature: Value(_lowerCAmelCase ) for feature, dtype in features.items()} ) if features is not None else None ) A_ : Optional[int] = ParquetDatasetReader({"train": parquet_path} , features=_lowerCAmelCase , cache_dir=_lowerCAmelCase ).read() _check_parquet_datasetdict(_lowerCAmelCase , _lowerCAmelCase ) @pytest.mark.parametrize("split" , [None, NamedSplit("train" ), "train", "test"] ) def __snake_case ( _lowerCAmelCase : str , _lowerCAmelCase : str , _lowerCAmelCase : Any ) -> Union[str, Any]: if split: A_ : Any = {split: parquet_path} else: A_ : Optional[Any] = "train" A_ : str = {"train": parquet_path, "test": parquet_path} A_ : Any = tmp_path / "cache" A_ : str = {"col_1": "string", "col_2": "int64", "col_3": "float64"} A_ : Dict = ParquetDatasetReader(_lowerCAmelCase , cache_dir=_lowerCAmelCase ).read() _check_parquet_datasetdict(_lowerCAmelCase , _lowerCAmelCase , splits=list(path.keys() ) ) assert all(dataset[split].split == split for split in path.keys() ) def __snake_case ( _lowerCAmelCase : Dict , _lowerCAmelCase : Optional[Any] ) -> Dict: A_ : List[str] = ParquetDatasetWriter(_lowerCAmelCase , tmp_path / "foo.parquet" ) assert writer.write() > 0 A_ : Tuple = pq.ParquetFile(tmp_path / "foo.parquet" ) A_ : Dict = pf.read() assert dataset.data.table == output_table def __snake_case ( _lowerCAmelCase : Optional[Any] , _lowerCAmelCase : int ) -> List[Any]: A_ : Tuple = str(shared_datadir / "test_image_rgb.jpg" ) A_ : int = {"image": [image_path]} A_ : Optional[Any] = Features({"image": Image()} ) A_ : Union[str, Any] = Dataset.from_dict(_lowerCAmelCase , features=_lowerCAmelCase ) A_ : Tuple = ParquetDatasetWriter(_lowerCAmelCase , tmp_path / "foo.parquet" ) assert writer.write() > 0 A_ : str = Dataset.from_parquet(str(tmp_path / "foo.parquet" ) ) assert dataset.features == reloaded_dataset.features A_ : int = ParquetDatasetReader(str(tmp_path / "foo.parquet" ) , streaming=_lowerCAmelCase ).read() assert dataset.features == reloaded_iterable_dataset.features @pytest.mark.parametrize( "feature, expected" , [ (Features({"foo": Value("int32" )} ), None), (Features({"image": Image(), "foo": Value("int32" )} ), config.PARQUET_ROW_GROUP_SIZE_FOR_IMAGE_DATASETS), (Features({"nested": Sequence(Audio() )} ), config.PARQUET_ROW_GROUP_SIZE_FOR_AUDIO_DATASETS), ] , ) def __snake_case ( _lowerCAmelCase : str , _lowerCAmelCase : List[Any] ) -> Any: assert get_writer_batch_size(_lowerCAmelCase ) == expected
300
0
import gc import random import unittest import torch from diffusers import ( IFImgaImgPipeline, IFImgaImgSuperResolutionPipeline, IFInpaintingPipeline, IFInpaintingSuperResolutionPipeline, IFPipeline, IFSuperResolutionPipeline, ) from diffusers.models.attention_processor import AttnAddedKVProcessor from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import floats_tensor, load_numpy, require_torch_gpu, skip_mps, slow, torch_device from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference from . import IFPipelineTesterMixin @skip_mps class __lowercase ( UpperCAmelCase_ , UpperCAmelCase_ , unittest.TestCase ): """simple docstring""" _UpperCAmelCase : str = IFPipeline _UpperCAmelCase : Tuple = TEXT_TO_IMAGE_PARAMS - {'''width''', '''height''', '''latents'''} _UpperCAmelCase : Any = TEXT_TO_IMAGE_BATCH_PARAMS _UpperCAmelCase : Union[str, Any] = PipelineTesterMixin.required_optional_params - {'''latents'''} def _SCREAMING_SNAKE_CASE ( self : str): return self._get_dummy_components() def _SCREAMING_SNAKE_CASE ( self : Tuple , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : List[str]=0): if str(lowerCAmelCase__).startswith("mps"): SCREAMING_SNAKE_CASE_: int = torch.manual_seed(lowerCAmelCase__) else: SCREAMING_SNAKE_CASE_: Optional[int] = torch.Generator(device=lowerCAmelCase__).manual_seed(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Dict = { "prompt": "A painting of a squirrel eating a burger", "generator": generator, "num_inference_steps": 2, "output_type": "numpy", } return inputs def _SCREAMING_SNAKE_CASE ( self : Dict): self._test_save_load_optional_components() @unittest.skipIf(torch_device != "cuda" , reason="float16 requires CUDA") def _SCREAMING_SNAKE_CASE ( self : Dict): # Due to non-determinism in save load of the hf-internal-testing/tiny-random-t5 text encoder super().test_save_load_floataa(expected_max_diff=1E-1) def _SCREAMING_SNAKE_CASE ( self : Dict): self._test_attention_slicing_forward_pass(expected_max_diff=1E-2) def _SCREAMING_SNAKE_CASE ( self : int): self._test_save_load_local() def _SCREAMING_SNAKE_CASE ( self : List[Any]): self._test_inference_batch_single_identical( expected_max_diff=1E-2 , ) @unittest.skipIf( torch_device != "cuda" or not is_xformers_available() , reason="XFormers attention is only available with CUDA and `xformers` installed" , ) def _SCREAMING_SNAKE_CASE ( self : Dict): self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3) @slow @require_torch_gpu class __lowercase ( unittest.TestCase ): """simple docstring""" def _SCREAMING_SNAKE_CASE ( self : List[Any]): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): # if SCREAMING_SNAKE_CASE_: str = IFPipeline.from_pretrained("DeepFloyd/IF-I-XL-v1.0" , variant="fp16" , torch_dtype=torch.floataa) SCREAMING_SNAKE_CASE_: Optional[int] = IFSuperResolutionPipeline.from_pretrained( "DeepFloyd/IF-II-L-v1.0" , variant="fp16" , torch_dtype=torch.floataa , text_encoder=lowerCAmelCase__ , tokenizer=lowerCAmelCase__) # pre compute text embeddings and remove T5 to save memory pipe_a.text_encoder.to("cuda") SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: int = pipe_a.encode_prompt("anime turtle" , device="cuda") del pipe_a.tokenizer del pipe_a.text_encoder gc.collect() SCREAMING_SNAKE_CASE_: Any = None SCREAMING_SNAKE_CASE_: int = None pipe_a.enable_model_cpu_offload() pipe_a.enable_model_cpu_offload() pipe_a.unet.set_attn_processor(AttnAddedKVProcessor()) pipe_a.unet.set_attn_processor(AttnAddedKVProcessor()) self._test_if(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__) pipe_a.remove_all_hooks() pipe_a.remove_all_hooks() # img2img SCREAMING_SNAKE_CASE_: Any = IFImgaImgPipeline(**pipe_a.components) SCREAMING_SNAKE_CASE_: Optional[int] = IFImgaImgSuperResolutionPipeline(**pipe_a.components) pipe_a.enable_model_cpu_offload() pipe_a.enable_model_cpu_offload() pipe_a.unet.set_attn_processor(AttnAddedKVProcessor()) pipe_a.unet.set_attn_processor(AttnAddedKVProcessor()) self._test_if_imgaimg(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__) pipe_a.remove_all_hooks() pipe_a.remove_all_hooks() # inpainting SCREAMING_SNAKE_CASE_: Optional[int] = IFInpaintingPipeline(**pipe_a.components) SCREAMING_SNAKE_CASE_: Optional[Any] = IFInpaintingSuperResolutionPipeline(**pipe_a.components) pipe_a.enable_model_cpu_offload() pipe_a.enable_model_cpu_offload() pipe_a.unet.set_attn_processor(AttnAddedKVProcessor()) pipe_a.unet.set_attn_processor(AttnAddedKVProcessor()) self._test_if_inpainting(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : int , lowerCAmelCase__ : str , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : Tuple): # pipeline 1 _start_torch_memory_measurement() SCREAMING_SNAKE_CASE_: Union[str, Any] = torch.Generator(device="cpu").manual_seed(0) SCREAMING_SNAKE_CASE_: Optional[int] = pipe_a( prompt_embeds=lowerCAmelCase__ , negative_prompt_embeds=lowerCAmelCase__ , num_inference_steps=2 , generator=lowerCAmelCase__ , output_type="np" , ) SCREAMING_SNAKE_CASE_: Optional[int] = output.images[0] assert image.shape == (64, 64, 3) SCREAMING_SNAKE_CASE_: Any = torch.cuda.max_memory_allocated() assert mem_bytes < 13 * 10**9 SCREAMING_SNAKE_CASE_: Any = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if.npy") assert_mean_pixel_difference(lowerCAmelCase__ , lowerCAmelCase__) # pipeline 2 _start_torch_memory_measurement() SCREAMING_SNAKE_CASE_: List[Any] = torch.Generator(device="cpu").manual_seed(0) SCREAMING_SNAKE_CASE_: Any = floats_tensor((1, 3, 64, 64) , rng=random.Random(0)).to(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[Any] = pipe_a( prompt_embeds=lowerCAmelCase__ , negative_prompt_embeds=lowerCAmelCase__ , image=lowerCAmelCase__ , generator=lowerCAmelCase__ , num_inference_steps=2 , output_type="np" , ) SCREAMING_SNAKE_CASE_: str = output.images[0] assert image.shape == (256, 256, 3) SCREAMING_SNAKE_CASE_: Optional[Any] = torch.cuda.max_memory_allocated() assert mem_bytes < 4 * 10**9 SCREAMING_SNAKE_CASE_: Dict = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_superresolution_stage_II.npy") assert_mean_pixel_difference(lowerCAmelCase__ , lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : Dict , lowerCAmelCase__ : Tuple , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : int , lowerCAmelCase__ : Optional[int]): # pipeline 1 _start_torch_memory_measurement() SCREAMING_SNAKE_CASE_: Optional[int] = floats_tensor((1, 3, 64, 64) , rng=random.Random(0)).to(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Tuple = torch.Generator(device="cpu").manual_seed(0) SCREAMING_SNAKE_CASE_: List[Any] = pipe_a( prompt_embeds=lowerCAmelCase__ , negative_prompt_embeds=lowerCAmelCase__ , image=lowerCAmelCase__ , num_inference_steps=2 , generator=lowerCAmelCase__ , output_type="np" , ) SCREAMING_SNAKE_CASE_: str = output.images[0] assert image.shape == (64, 64, 3) SCREAMING_SNAKE_CASE_: List[str] = torch.cuda.max_memory_allocated() assert mem_bytes < 10 * 10**9 SCREAMING_SNAKE_CASE_: int = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_img2img.npy") assert_mean_pixel_difference(lowerCAmelCase__ , lowerCAmelCase__) # pipeline 2 _start_torch_memory_measurement() SCREAMING_SNAKE_CASE_: Union[str, Any] = torch.Generator(device="cpu").manual_seed(0) SCREAMING_SNAKE_CASE_: Optional[Any] = floats_tensor((1, 3, 256, 256) , rng=random.Random(0)).to(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Dict = floats_tensor((1, 3, 64, 64) , rng=random.Random(0)).to(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: List[Any] = pipe_a( prompt_embeds=lowerCAmelCase__ , negative_prompt_embeds=lowerCAmelCase__ , image=lowerCAmelCase__ , original_image=lowerCAmelCase__ , generator=lowerCAmelCase__ , num_inference_steps=2 , output_type="np" , ) SCREAMING_SNAKE_CASE_: Any = output.images[0] assert image.shape == (256, 256, 3) SCREAMING_SNAKE_CASE_: Dict = torch.cuda.max_memory_allocated() assert mem_bytes < 4 * 10**9 SCREAMING_SNAKE_CASE_: Any = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_img2img_superresolution_stage_II.npy") assert_mean_pixel_difference(lowerCAmelCase__ , lowerCAmelCase__) def _SCREAMING_SNAKE_CASE ( self : str , lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : Any , lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : int): # pipeline 1 _start_torch_memory_measurement() SCREAMING_SNAKE_CASE_: List[Any] = floats_tensor((1, 3, 64, 64) , rng=random.Random(0)).to(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Dict = floats_tensor((1, 3, 64, 64) , rng=random.Random(1)).to(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Optional[int] = torch.Generator(device="cpu").manual_seed(0) SCREAMING_SNAKE_CASE_: Tuple = pipe_a( prompt_embeds=lowerCAmelCase__ , negative_prompt_embeds=lowerCAmelCase__ , image=lowerCAmelCase__ , mask_image=lowerCAmelCase__ , num_inference_steps=2 , generator=lowerCAmelCase__ , output_type="np" , ) SCREAMING_SNAKE_CASE_: int = output.images[0] assert image.shape == (64, 64, 3) SCREAMING_SNAKE_CASE_: Tuple = torch.cuda.max_memory_allocated() assert mem_bytes < 10 * 10**9 SCREAMING_SNAKE_CASE_: Union[str, Any] = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_inpainting.npy") assert_mean_pixel_difference(lowerCAmelCase__ , lowerCAmelCase__) # pipeline 2 _start_torch_memory_measurement() SCREAMING_SNAKE_CASE_: Optional[Any] = torch.Generator(device="cpu").manual_seed(0) SCREAMING_SNAKE_CASE_: List[Any] = floats_tensor((1, 3, 64, 64) , rng=random.Random(0)).to(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Any = floats_tensor((1, 3, 256, 256) , rng=random.Random(0)).to(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Any = floats_tensor((1, 3, 256, 256) , rng=random.Random(1)).to(lowerCAmelCase__) SCREAMING_SNAKE_CASE_: Any = pipe_a( prompt_embeds=lowerCAmelCase__ , negative_prompt_embeds=lowerCAmelCase__ , image=lowerCAmelCase__ , mask_image=lowerCAmelCase__ , original_image=lowerCAmelCase__ , generator=lowerCAmelCase__ , num_inference_steps=2 , output_type="np" , ) SCREAMING_SNAKE_CASE_: Union[str, Any] = output.images[0] assert image.shape == (256, 256, 3) SCREAMING_SNAKE_CASE_: List[str] = torch.cuda.max_memory_allocated() assert mem_bytes < 4 * 10**9 SCREAMING_SNAKE_CASE_: List[Any] = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_inpainting_superresolution_stage_II.npy") assert_mean_pixel_difference(lowerCAmelCase__ , lowerCAmelCase__) def A_ ( ): torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats()
13
import json import unittest import numpy as np from huggingface_hub import hf_hub_download from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from transformers import OneFormerImageProcessor from transformers.models.oneformer.image_processing_oneformer import binary_mask_to_rle from transformers.models.oneformer.modeling_oneformer import OneFormerForUniversalSegmentationOutput if is_vision_available(): from PIL import Image def __snake_case ( _lowerCAmelCase : List[str] , _lowerCAmelCase : List[Any]="shi-labs/oneformer_demo" ) -> int: with open(hf_hub_download(_lowerCAmelCase , _lowerCAmelCase , repo_type="dataset" ) , "r" ) as f: A_ : Optional[int] = json.load(_lowerCAmelCase ) A_ : Union[str, Any] = {} A_ : Tuple = [] A_ : Optional[Any] = [] for key, info in class_info.items(): A_ : Tuple = info["name"] class_names.append(info["name"] ) if info["isthing"]: thing_ids.append(int(_lowerCAmelCase ) ) A_ : Optional[Any] = thing_ids A_ : int = class_names return metadata class __magic_name__ ( unittest.TestCase ): """simple docstring""" def __init__( self :List[Any] , snake_case :List[str] , snake_case :int=7 , snake_case :Optional[int]=3 , snake_case :Union[str, Any]=30 , snake_case :Tuple=400 , snake_case :List[Any]=None , snake_case :Optional[Any]=True , snake_case :Tuple=True , snake_case :Dict=[0.5, 0.5, 0.5] , snake_case :Any=[0.5, 0.5, 0.5] , snake_case :Optional[int]=10 , snake_case :Tuple=False , snake_case :Optional[int]=255 , snake_case :Optional[Any]="shi-labs/oneformer_demo" , snake_case :Optional[Any]="ade20k_panoptic.json" , snake_case :Optional[int]=10 , ): '''simple docstring''' A_ : Tuple = parent A_ : List[str] = batch_size A_ : Optional[int] = num_channels A_ : Tuple = min_resolution A_ : List[Any] = max_resolution A_ : Union[str, Any] = do_resize A_ : Any = {"shortest_edge": 32, "longest_edge": 1_333} if size is None else size A_ : Tuple = do_normalize A_ : List[str] = image_mean A_ : List[Any] = image_std A_ : Union[str, Any] = class_info_file A_ : List[Any] = prepare_metadata(snake_case , snake_case ) A_ : Tuple = num_text A_ : str = repo_path # for the post_process_functions A_ : Any = 2 A_ : int = 10 A_ : Optional[int] = 10 A_ : Tuple = 3 A_ : Tuple = 4 A_ : str = num_labels A_ : int = do_reduce_labels A_ : List[Any] = ignore_index def SCREAMING_SNAKE_CASE ( self :Optional[Any] ): '''simple docstring''' return { "do_resize": self.do_resize, "size": self.size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, "num_labels": self.num_labels, "do_reduce_labels": self.do_reduce_labels, "ignore_index": self.ignore_index, "class_info_file": self.class_info_file, "metadata": self.metadata, "num_text": self.num_text, } def SCREAMING_SNAKE_CASE ( self :List[Any] , snake_case :Any , snake_case :Any=False ): '''simple docstring''' if not batched: A_ : List[str] = image_inputs[0] if isinstance(snake_case , Image.Image ): A_ , A_ : Dict = image.size else: A_ , A_ : Tuple = image.shape[1], image.shape[2] if w < h: A_ : str = int(self.size["shortest_edge"] * h / w ) A_ : Any = self.size["shortest_edge"] elif w > h: A_ : Optional[int] = self.size["shortest_edge"] A_ : List[str] = int(self.size["shortest_edge"] * w / h ) else: A_ : List[str] = self.size["shortest_edge"] A_ : Optional[Any] = self.size["shortest_edge"] else: A_ : Tuple = [] for image in image_inputs: A_ , A_ : Optional[Any] = self.get_expected_values([image] ) expected_values.append((expected_height, expected_width) ) A_ : Tuple = max(snake_case , key=lambda snake_case : item[0] )[0] A_ : Union[str, Any] = max(snake_case , key=lambda snake_case : item[1] )[1] return expected_height, expected_width def SCREAMING_SNAKE_CASE ( self :Tuple ): '''simple docstring''' return OneFormerForUniversalSegmentationOutput( # +1 for null class class_queries_logits=torch.randn((self.batch_size, self.num_queries, self.num_classes + 1) ) , masks_queries_logits=torch.randn((self.batch_size, self.num_queries, self.height, self.width) ) , ) @require_torch @require_vision class __magic_name__ ( lowerCamelCase__ , unittest.TestCase ): """simple docstring""" __UpperCamelCase = OneFormerImageProcessor if (is_vision_available() and is_torch_available()) else None # only for test_image_processing_common.test_image_proc_to_json_string __UpperCamelCase = image_processing_class def SCREAMING_SNAKE_CASE ( self :int ): '''simple docstring''' A_ : Union[str, Any] = OneFormerImageProcessorTester(self ) @property def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' return self.image_processing_tester.prepare_image_processor_dict() def SCREAMING_SNAKE_CASE ( self :List[Any] ): '''simple docstring''' A_ : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(snake_case , "image_mean" ) ) self.assertTrue(hasattr(snake_case , "image_std" ) ) self.assertTrue(hasattr(snake_case , "do_normalize" ) ) self.assertTrue(hasattr(snake_case , "do_resize" ) ) self.assertTrue(hasattr(snake_case , "size" ) ) self.assertTrue(hasattr(snake_case , "ignore_index" ) ) self.assertTrue(hasattr(snake_case , "class_info_file" ) ) self.assertTrue(hasattr(snake_case , "num_text" ) ) self.assertTrue(hasattr(snake_case , "repo_path" ) ) self.assertTrue(hasattr(snake_case , "metadata" ) ) self.assertTrue(hasattr(snake_case , "do_reduce_labels" ) ) def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' pass def SCREAMING_SNAKE_CASE ( self :int ): '''simple docstring''' A_ : Dict = self.image_processing_class(**self.image_processor_dict ) # create random PIL images A_ : Optional[Any] = prepare_image_inputs(self.image_processing_tester , equal_resolution=snake_case ) for image in image_inputs: self.assertIsInstance(snake_case , Image.Image ) # Test not batched input A_ : str = image_processor(image_inputs[0] , ["semantic"] , return_tensors="pt" ).pixel_values A_ , A_ : str = self.image_processing_tester.get_expected_values(snake_case ) self.assertEqual( encoded_images.shape , (1, self.image_processing_tester.num_channels, expected_height, expected_width) , ) # Test batched A_ , A_ : Optional[Any] = self.image_processing_tester.get_expected_values(snake_case , batched=snake_case ) A_ : List[str] = image_processor( snake_case , ["semantic"] * len(snake_case ) , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processing_tester.batch_size, self.image_processing_tester.num_channels, expected_height, expected_width, ) , ) def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' A_ : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors A_ : List[str] = prepare_image_inputs(self.image_processing_tester , equal_resolution=snake_case , numpify=snake_case ) for image in image_inputs: self.assertIsInstance(snake_case , np.ndarray ) # Test not batched input A_ : List[str] = image_processor(image_inputs[0] , ["semantic"] , return_tensors="pt" ).pixel_values A_ , A_ : List[str] = self.image_processing_tester.get_expected_values(snake_case ) self.assertEqual( encoded_images.shape , (1, self.image_processing_tester.num_channels, expected_height, expected_width) , ) # Test batched A_ , A_ : int = self.image_processing_tester.get_expected_values(snake_case , batched=snake_case ) A_ : Optional[Any] = image_processor( snake_case , ["semantic"] * len(snake_case ) , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processing_tester.batch_size, self.image_processing_tester.num_channels, expected_height, expected_width, ) , ) def SCREAMING_SNAKE_CASE ( self :Optional[int] ): '''simple docstring''' A_ : List[str] = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors A_ : List[str] = prepare_image_inputs(self.image_processing_tester , equal_resolution=snake_case , torchify=snake_case ) for image in image_inputs: self.assertIsInstance(snake_case , torch.Tensor ) # Test not batched input A_ : Any = image_processor(image_inputs[0] , ["semantic"] , return_tensors="pt" ).pixel_values A_ , A_ : Tuple = self.image_processing_tester.get_expected_values(snake_case ) self.assertEqual( encoded_images.shape , (1, self.image_processing_tester.num_channels, expected_height, expected_width) , ) # Test batched A_ , A_ : Tuple = self.image_processing_tester.get_expected_values(snake_case , batched=snake_case ) A_ : Any = image_processor( snake_case , ["semantic"] * len(snake_case ) , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processing_tester.batch_size, self.image_processing_tester.num_channels, expected_height, expected_width, ) , ) def SCREAMING_SNAKE_CASE ( self :Optional[Any] , snake_case :Dict=False , snake_case :str=False , snake_case :Dict="np" ): '''simple docstring''' A_ : Tuple = self.image_processing_class(**self.image_processor_dict ) # prepare image and target A_ : Tuple = self.image_processing_tester.num_labels A_ : str = None A_ : Tuple = None A_ : Tuple = prepare_image_inputs(self.image_processing_tester , equal_resolution=snake_case ) if with_segmentation_maps: A_ : List[str] = num_labels if is_instance_map: A_ : List[str] = list(range(snake_case ) ) * 2 A_ : int = dict(enumerate(snake_case ) ) A_ : List[str] = [ np.random.randint(0 , high * 2 , (img.size[1], img.size[0]) ).astype(np.uinta ) for img in image_inputs ] if segmentation_type == "pil": A_ : int = [Image.fromarray(snake_case ) for annotation in annotations] A_ : List[str] = image_processor( snake_case , ["semantic"] * len(snake_case ) , snake_case , return_tensors="pt" , instance_id_to_semantic_id=snake_case , pad_and_return_pixel_mask=snake_case , ) return inputs def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' pass def SCREAMING_SNAKE_CASE ( self :Optional[int] ): '''simple docstring''' def common(snake_case :Dict=False , snake_case :Optional[int]=None ): A_ : Tuple = self.comm_get_image_processor_inputs( with_segmentation_maps=snake_case , is_instance_map=snake_case , segmentation_type=snake_case ) A_ : Optional[Any] = inputs["mask_labels"] A_ : List[Any] = inputs["class_labels"] A_ : Optional[Any] = inputs["pixel_values"] A_ : int = inputs["text_inputs"] # check the batch_size for mask_label, class_label, text_input in zip(snake_case , snake_case , snake_case ): self.assertEqual(mask_label.shape[0] , class_label.shape[0] ) # this ensure padding has happened self.assertEqual(mask_label.shape[1:] , pixel_values.shape[2:] ) self.assertEqual(len(snake_case ) , self.image_processing_tester.num_text ) common() common(is_instance_map=snake_case ) common(is_instance_map=snake_case , segmentation_type="pil" ) common(is_instance_map=snake_case , segmentation_type="pil" ) def SCREAMING_SNAKE_CASE ( self :Optional[Any] ): '''simple docstring''' A_ : Any = np.zeros((20, 50) ) A_ : List[str] = 1 A_ : int = 1 A_ : Optional[Any] = 1 A_ : Any = binary_mask_to_rle(snake_case ) self.assertEqual(len(snake_case ) , 4 ) self.assertEqual(rle[0] , 21 ) self.assertEqual(rle[1] , 45 ) def SCREAMING_SNAKE_CASE ( self :Optional[int] ): '''simple docstring''' A_ : Union[str, Any] = self.image_processing_class( num_labels=self.image_processing_tester.num_classes , max_seq_length=77 , task_seq_length=77 , class_info_file="ade20k_panoptic.json" , num_text=self.image_processing_tester.num_text , repo_path="shi-labs/oneformer_demo" , ) A_ : Any = self.image_processing_tester.get_fake_oneformer_outputs() A_ : int = fature_extractor.post_process_semantic_segmentation(snake_case ) self.assertEqual(len(snake_case ) , self.image_processing_tester.batch_size ) self.assertEqual( segmentation[0].shape , ( self.image_processing_tester.height, self.image_processing_tester.width, ) , ) A_ : Optional[int] = [(1, 4) for i in range(self.image_processing_tester.batch_size )] A_ : List[Any] = fature_extractor.post_process_semantic_segmentation(snake_case , target_sizes=snake_case ) self.assertEqual(segmentation[0].shape , target_sizes[0] ) def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' A_ : List[str] = self.image_processing_class( num_labels=self.image_processing_tester.num_classes , max_seq_length=77 , task_seq_length=77 , class_info_file="ade20k_panoptic.json" , num_text=self.image_processing_tester.num_text , repo_path="shi-labs/oneformer_demo" , ) A_ : str = self.image_processing_tester.get_fake_oneformer_outputs() A_ : Optional[Any] = image_processor.post_process_instance_segmentation(snake_case , threshold=0 ) self.assertTrue(len(snake_case ) == self.image_processing_tester.batch_size ) for el in segmentation: self.assertTrue("segmentation" in el ) self.assertTrue("segments_info" in el ) self.assertEqual(type(el["segments_info"] ) , snake_case ) self.assertEqual( el["segmentation"].shape , (self.image_processing_tester.height, self.image_processing_tester.width) ) def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' A_ : Tuple = self.image_processing_class( num_labels=self.image_processing_tester.num_classes , max_seq_length=77 , task_seq_length=77 , class_info_file="ade20k_panoptic.json" , num_text=self.image_processing_tester.num_text , repo_path="shi-labs/oneformer_demo" , ) A_ : List[Any] = self.image_processing_tester.get_fake_oneformer_outputs() A_ : Optional[Any] = image_processor.post_process_panoptic_segmentation(snake_case , threshold=0 ) self.assertTrue(len(snake_case ) == self.image_processing_tester.batch_size ) for el in segmentation: self.assertTrue("segmentation" in el ) self.assertTrue("segments_info" in el ) self.assertEqual(type(el["segments_info"] ) , snake_case ) self.assertEqual( el["segmentation"].shape , (self.image_processing_tester.height, self.image_processing_tester.width) )
300
0
from ..utils import DummyObject, requires_backends class UpperCamelCase_ ( metaclass=UpperCAmelCase__ ): '''simple docstring''' UpperCAmelCase__ = ['''flax''', '''transformers'''] def __init__( self : List[str] , *UpperCAmelCase__ : str , **UpperCAmelCase__ : Optional[Any]) ->str: '''simple docstring''' requires_backends(self , ['''flax''', '''transformers''']) @classmethod def SCREAMING_SNAKE_CASE ( cls : int , *UpperCAmelCase__ : List[Any] , **UpperCAmelCase__ : Tuple) ->Dict: '''simple docstring''' requires_backends(cls , ['''flax''', '''transformers''']) @classmethod def SCREAMING_SNAKE_CASE ( cls : Optional[Any] , *UpperCAmelCase__ : Any , **UpperCAmelCase__ : List[str]) ->Union[str, Any]: '''simple docstring''' requires_backends(cls , ['''flax''', '''transformers''']) class UpperCamelCase_ ( metaclass=UpperCAmelCase__ ): '''simple docstring''' UpperCAmelCase__ = ['''flax''', '''transformers'''] def __init__( self : Tuple , *UpperCAmelCase__ : Tuple , **UpperCAmelCase__ : Optional[int]) ->List[Any]: '''simple docstring''' requires_backends(self , ['''flax''', '''transformers''']) @classmethod def SCREAMING_SNAKE_CASE ( cls : int , *UpperCAmelCase__ : Dict , **UpperCAmelCase__ : List[Any]) ->Dict: '''simple docstring''' requires_backends(cls , ['''flax''', '''transformers''']) @classmethod def SCREAMING_SNAKE_CASE ( cls : List[str] , *UpperCAmelCase__ : List[Any] , **UpperCAmelCase__ : Union[str, Any]) ->List[Any]: '''simple docstring''' requires_backends(cls , ['''flax''', '''transformers''']) class UpperCamelCase_ ( metaclass=UpperCAmelCase__ ): '''simple docstring''' UpperCAmelCase__ = ['''flax''', '''transformers'''] def __init__( self : Union[str, Any] , *UpperCAmelCase__ : Tuple , **UpperCAmelCase__ : Any) ->Union[str, Any]: '''simple docstring''' requires_backends(self , ['''flax''', '''transformers''']) @classmethod def SCREAMING_SNAKE_CASE ( cls : int , *UpperCAmelCase__ : str , **UpperCAmelCase__ : Optional[int]) ->Any: '''simple docstring''' requires_backends(cls , ['''flax''', '''transformers''']) @classmethod def SCREAMING_SNAKE_CASE ( cls : Optional[Any] , *UpperCAmelCase__ : Tuple , **UpperCAmelCase__ : Union[str, Any]) ->List[Any]: '''simple docstring''' requires_backends(cls , ['''flax''', '''transformers''']) class UpperCamelCase_ ( metaclass=UpperCAmelCase__ ): '''simple docstring''' UpperCAmelCase__ = ['''flax''', '''transformers'''] def __init__( self : int , *UpperCAmelCase__ : Dict , **UpperCAmelCase__ : int) ->Union[str, Any]: '''simple docstring''' requires_backends(self , ['''flax''', '''transformers''']) @classmethod def SCREAMING_SNAKE_CASE ( cls : Any , *UpperCAmelCase__ : List[Any] , **UpperCAmelCase__ : Optional[int]) ->Dict: '''simple docstring''' requires_backends(cls , ['''flax''', '''transformers''']) @classmethod def SCREAMING_SNAKE_CASE ( cls : List[str] , *UpperCAmelCase__ : Optional[int] , **UpperCAmelCase__ : str) ->Optional[Any]: '''simple docstring''' requires_backends(cls , ['''flax''', '''transformers'''])
14
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 _lowerCAmelCase : List[str] = logging.get_logger(__name__) _lowerCAmelCase : Optional[Any] = { '''facebook/data2vec-vision-base-ft''': ( '''https://huggingface.co/facebook/data2vec-vision-base-ft/resolve/main/config.json''' ), } class __magic_name__ ( lowerCamelCase__ ): """simple docstring""" __UpperCamelCase = '''data2vec-vision''' def __init__( self :int , snake_case :Optional[int]=768 , snake_case :Any=12 , snake_case :Any=12 , snake_case :Tuple=3_072 , snake_case :Any="gelu" , snake_case :Tuple=0.0 , snake_case :int=0.0 , snake_case :Any=0.02 , snake_case :str=1e-12 , snake_case :List[str]=224 , snake_case :Dict=16 , snake_case :int=3 , snake_case :int=False , snake_case :str=False , snake_case :List[Any]=False , snake_case :Optional[Any]=False , snake_case :Tuple=0.1 , snake_case :Optional[Any]=0.1 , snake_case :Any=True , snake_case :Optional[Any]=[3, 5, 7, 11] , snake_case :Dict=[1, 2, 3, 6] , snake_case :int=True , snake_case :List[Any]=0.4 , snake_case :Any=256 , snake_case :Union[str, Any]=1 , snake_case :Union[str, Any]=False , snake_case :Any=255 , **snake_case :int , ): '''simple docstring''' super().__init__(**snake_case ) A_ : Dict = hidden_size A_ : Tuple = num_hidden_layers A_ : List[str] = num_attention_heads A_ : Any = intermediate_size A_ : Optional[Any] = hidden_act A_ : Any = hidden_dropout_prob A_ : List[str] = attention_probs_dropout_prob A_ : Optional[Any] = initializer_range A_ : List[str] = layer_norm_eps A_ : str = image_size A_ : Optional[int] = patch_size A_ : int = num_channels A_ : Optional[Any] = use_mask_token A_ : Optional[Any] = use_absolute_position_embeddings A_ : Optional[int] = use_relative_position_bias A_ : Dict = use_shared_relative_position_bias A_ : Any = layer_scale_init_value A_ : Optional[Any] = drop_path_rate A_ : Dict = use_mean_pooling # decode head attributes (semantic segmentation) A_ : Tuple = out_indices A_ : Optional[Any] = pool_scales # auxiliary head attributes (semantic segmentation) A_ : str = use_auxiliary_head A_ : List[Any] = auxiliary_loss_weight A_ : List[str] = auxiliary_channels A_ : Dict = auxiliary_num_convs A_ : List[str] = auxiliary_concat_input A_ : Optional[int] = semantic_loss_ignore_index class __magic_name__ ( lowerCamelCase__ ): """simple docstring""" __UpperCamelCase = version.parse('''1.11''' ) @property def SCREAMING_SNAKE_CASE ( self :Union[str, Any] ): '''simple docstring''' return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ] ) @property def SCREAMING_SNAKE_CASE ( self :Tuple ): '''simple docstring''' return 1e-4
300
0
import argparse from pathlib import Path import torch from packaging import version from torch.onnx import export from diffusers import AutoencoderKL SCREAMING_SNAKE_CASE :str = version.parse(version.parse(torch.__version__).base_version) < version.parse('1.11') def UpperCAmelCase ( a_ , a_ , a_ , a_ , a_ , a_ , a_ , a_=False , ) -> Optional[Any]: """simple docstring""" output_path.parent.mkdir(parents=a_ , exist_ok=a_ ) # PyTorch deprecated the `enable_onnx_checker` and `use_external_data_format` arguments in v1.11, # so we check the torch version for backwards compatibility if is_torch_less_than_1_11: export( a_ , a_ , f=output_path.as_posix() , input_names=a_ , output_names=a_ , dynamic_axes=a_ , do_constant_folding=a_ , use_external_data_format=a_ , enable_onnx_checker=a_ , opset_version=a_ , ) else: export( a_ , a_ , f=output_path.as_posix() , input_names=a_ , output_names=a_ , dynamic_axes=a_ , do_constant_folding=a_ , opset_version=a_ , ) @torch.no_grad() def UpperCAmelCase ( a_ , a_ , a_ , a_ = False ) -> Any: """simple docstring""" __A = torch.floataa if fpaa else torch.floataa if fpaa and torch.cuda.is_available(): __A = "cuda" elif fpaa and not torch.cuda.is_available(): raise ValueError("`float16` model export is only supported on GPUs with CUDA" ) else: __A = "cpu" __A = Path(a_ ) # VAE DECODER __A = AutoencoderKL.from_pretrained(model_path + "/vae" ) __A = vae_decoder.config.latent_channels # forward only through the decoder part __A = vae_decoder.decode onnx_export( a_ , model_args=( torch.randn(1 , a_ , 2_5 , 2_5 ).to(device=a_ , dtype=a_ ), False, ) , output_path=output_path / "vae_decoder" / "model.onnx" , ordered_input_names=["latent_sample", "return_dict"] , output_names=["sample"] , dynamic_axes={ "latent_sample": {0: "batch", 1: "channels", 2: "height", 3: "width"}, } , opset=a_ , ) del vae_decoder if __name__ == "__main__": SCREAMING_SNAKE_CASE :Optional[Any] = argparse.ArgumentParser() parser.add_argument( '--model_path', type=str, required=True, help='Path to the `diffusers` checkpoint to convert (either a local directory or on the Hub).', ) parser.add_argument('--output_path', type=str, required=True, help='Path to the output model.') parser.add_argument( '--opset', default=14, type=int, help='The version of the ONNX operator set to use.', ) parser.add_argument('--fp16', action='store_true', default=False, help='Export the models in `float16` mode') SCREAMING_SNAKE_CASE :int = parser.parse_args() print(args.output_path) convert_models(args.model_path, args.output_path, args.opset, args.fpaa) print('SD: Done: ONNX')
15
from typing import List, Optional, Union import numpy as np from ....audio_utils import mel_filter_bank, optimal_fft_length, spectrogram, window_function from ....feature_extraction_sequence_utils import SequenceFeatureExtractor from ....feature_extraction_utils import BatchFeature from ....file_utils import PaddingStrategy, TensorType from ....utils import logging _lowerCAmelCase : str = logging.get_logger(__name__) class __magic_name__ ( lowerCamelCase__ ): """simple docstring""" __UpperCamelCase = ['''input_features''', '''attention_mask'''] def __init__( self :int , snake_case :int=80 , snake_case :Optional[int]=16_000 , snake_case :Tuple=0.0 , snake_case :Optional[int]=10 , snake_case :Optional[Any]=25 , snake_case :Dict="hamming_window" , snake_case :Tuple=32768.0 , snake_case :str=0.97 , snake_case :List[str]=1.0 , snake_case :Dict=True , snake_case :str=True , snake_case :Optional[Any]=False , **snake_case :Union[str, Any] , ): '''simple docstring''' super().__init__(feature_size=snake_case , sampling_rate=snake_case , padding_value=snake_case , **snake_case ) A_ : Union[str, Any] = feature_size A_ : int = sampling_rate A_ : str = padding_value A_ : int = hop_length A_ : List[str] = win_length A_ : Any = frame_signal_scale A_ : str = preemphasis_coeff A_ : List[str] = mel_floor A_ : str = normalize_means A_ : Any = normalize_vars A_ : Optional[Any] = win_function A_ : Dict = return_attention_mask A_ : List[str] = win_length * sampling_rate // 1_000 A_ : List[str] = hop_length * sampling_rate // 1_000 A_ : List[str] = optimal_fft_length(self.sample_size ) A_ : str = (self.n_fft // 2) + 1 def SCREAMING_SNAKE_CASE ( self :Any , snake_case :np.array ): '''simple docstring''' if self.win_function == "hamming_window": A_ : Dict = window_function(window_length=self.sample_size , name=self.win_function , periodic=snake_case ) else: A_ : List[str] = window_function(window_length=self.sample_size , name=self.win_function ) A_ : Optional[int] = mel_filter_bank( num_frequency_bins=self.n_freqs , num_mel_filters=self.feature_size , min_frequency=0.0 , max_frequency=self.sampling_rate / 2.0 , sampling_rate=self.sampling_rate , ) A_ : Tuple = spectrogram( one_waveform * self.frame_signal_scale , window=snake_case , frame_length=self.sample_size , hop_length=self.sample_stride , fft_length=self.n_fft , center=snake_case , preemphasis=self.preemphasis_coeff , mel_filters=snake_case , mel_floor=self.mel_floor , log_mel="log" , ) return msfc_features.T def SCREAMING_SNAKE_CASE ( self :int , snake_case :Any , snake_case :Union[str, Any] , snake_case :str ): '''simple docstring''' if self.normalize_means: A_ : int = x[:input_length].mean(axis=0 ) A_ : Any = np.subtract(snake_case , snake_case ) if self.normalize_vars: A_ : List[Any] = x[:input_length].std(axis=0 ) A_ : Optional[int] = np.divide(snake_case , snake_case ) if input_length < x.shape[0]: A_ : Optional[int] = padding_value # make sure array is in float32 A_ : Union[str, Any] = x.astype(np.floataa ) return x def SCREAMING_SNAKE_CASE ( self :int , snake_case :List[np.ndarray] , snake_case :Optional[np.ndarray] = None ): '''simple docstring''' A_ : str = attention_mask.sum(-1 ) if attention_mask is not None else [x.shape[0] for x in input_features] return [self._normalize_one(snake_case , snake_case , self.padding_value ) for x, n in zip(snake_case , snake_case )] def __call__( self :int , snake_case :Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] , snake_case :Union[bool, str, PaddingStrategy] = False , snake_case :Optional[int] = None , snake_case :bool = False , snake_case :Optional[int] = None , snake_case :Optional[bool] = None , snake_case :Optional[Union[str, TensorType]] = None , snake_case :Optional[int] = None , **snake_case :Dict , ): '''simple docstring''' if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( f"The model corresponding to this feature extractor: {self} was trained using a sampling rate of" f" {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled with" f" {self.sampling_rate} and not {sampling_rate}." ) else: logger.warning( "It is strongly recommended to pass the ``sampling_rate`` argument to this function. " "Failing to do so can result in silent errors that might be hard to debug." ) A_ : Optional[int] = isinstance(snake_case , np.ndarray ) and len(raw_speech.shape ) > 1 if is_batched_numpy and len(raw_speech.shape ) > 2: raise ValueError(f"Only mono-channel audio is supported for input to {self}" ) A_ : Optional[Any] = is_batched_numpy or ( isinstance(snake_case , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: A_ : List[Any] = [np.asarray(snake_case , dtype=np.floataa ) for speech in raw_speech] elif not is_batched and not isinstance(snake_case , np.ndarray ): A_ : int = np.asarray(snake_case , dtype=np.floataa ) elif isinstance(snake_case , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): A_ : Optional[int] = raw_speech.astype(np.floataa ) # always return batch if not is_batched: A_ : Tuple = [raw_speech] # extract fbank features A_ : int = [self._extract_mfsc_features(snake_case ) for one_waveform in raw_speech] # convert into correct format for padding A_ : Union[str, Any] = BatchFeature({"input_features": features} ) A_ : str = self.pad( snake_case , padding=snake_case , max_length=snake_case , truncation=snake_case , pad_to_multiple_of=snake_case , return_attention_mask=snake_case , **snake_case , ) # make sure list is in array format A_ : Optional[int] = padded_inputs.get("input_features" ) if isinstance(input_features[0] , snake_case ): A_ : Union[str, Any] = [np.asarray(snake_case , dtype=np.floataa ) for feature in input_features] A_ : Dict = padded_inputs.get("attention_mask" ) if attention_mask is not None: A_ : Any = [np.asarray(snake_case , dtype=np.intaa ) for array in attention_mask] if self.normalize_means or self.normalize_vars: A_ : Dict = ( np.array(snake_case , dtype=np.intaa ) if self._get_padding_strategies(snake_case , max_length=snake_case ) is not PaddingStrategy.DO_NOT_PAD and padding else None ) A_ : Optional[int] = self.normalize( padded_inputs["input_features"] , attention_mask=snake_case ) if return_tensors is not None: A_ : Dict = padded_inputs.convert_to_tensors(snake_case ) return padded_inputs
300
0
"""simple docstring""" import inspect import unittest from transformers import BitConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_backbone_common import BackboneTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import BitBackbone, BitForImageClassification, BitImageProcessor, BitModel from transformers.models.bit.modeling_bit import BIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image class __A : '''simple docstring''' def __init__( self : Union[str, Any] ,_snake_case : int ,_snake_case : Any=3 ,_snake_case : List[Any]=32 ,_snake_case : str=3 ,_snake_case : List[Any]=10 ,_snake_case : Any=[8, 16, 32, 64] ,_snake_case : Optional[int]=[1, 1, 2, 1] ,_snake_case : Dict=True ,_snake_case : Dict=True ,_snake_case : List[Any]="relu" ,_snake_case : int=3 ,_snake_case : Dict=None ,_snake_case : List[Any]=["stage2", "stage3", "stage4"] ,_snake_case : List[Any]=[2, 3, 4] ,_snake_case : int=1 ,) -> Optional[int]: """simple docstring""" lowercase__ : Any = parent lowercase__ : Dict = batch_size lowercase__ : Any = image_size lowercase__ : Dict = num_channels lowercase__ : int = embeddings_size lowercase__ : str = hidden_sizes lowercase__ : Tuple = depths lowercase__ : Tuple = is_training lowercase__ : str = use_labels lowercase__ : int = hidden_act lowercase__ : List[Any] = num_labels lowercase__ : Dict = scope lowercase__ : Dict = len(_snake_case ) lowercase__ : Optional[int] = out_features lowercase__ : List[str] = out_indices lowercase__ : Any = num_groups def UpperCAmelCase ( self : Dict ) -> Union[str, Any]: """simple docstring""" lowercase__ : int = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) lowercase__ : Optional[int] = None if self.use_labels: lowercase__ : str = ids_tensor([self.batch_size] ,self.num_labels ) lowercase__ : Optional[int] = self.get_config() return config, pixel_values, labels def UpperCAmelCase ( self : List[str] ) -> Optional[int]: """simple docstring""" return BitConfig( num_channels=self.num_channels ,embeddings_size=self.embeddings_size ,hidden_sizes=self.hidden_sizes ,depths=self.depths ,hidden_act=self.hidden_act ,num_labels=self.num_labels ,out_features=self.out_features ,out_indices=self.out_indices ,num_groups=self.num_groups ,) def UpperCAmelCase ( self : Tuple ,_snake_case : Optional[Any] ,_snake_case : Any ,_snake_case : str ) -> int: """simple docstring""" lowercase__ : Tuple = BitModel(config=_snake_case ) model.to(_snake_case ) model.eval() lowercase__ : Union[str, Any] = model(_snake_case ) self.parent.assertEqual( result.last_hidden_state.shape ,(self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32) ,) def UpperCAmelCase ( self : str ,_snake_case : List[str] ,_snake_case : int ,_snake_case : Optional[int] ) -> Dict: """simple docstring""" lowercase__ : Optional[int] = self.num_labels lowercase__ : List[str] = BitForImageClassification(_snake_case ) model.to(_snake_case ) model.eval() lowercase__ : int = model(_snake_case ,labels=_snake_case ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.num_labels) ) def UpperCAmelCase ( self : Optional[Any] ,_snake_case : List[Any] ,_snake_case : Tuple ,_snake_case : str ) -> Dict: """simple docstring""" lowercase__ : int = BitBackbone(config=_snake_case ) model.to(_snake_case ) model.eval() lowercase__ : str = model(_snake_case ) # verify feature maps self.parent.assertEqual(len(result.feature_maps ) ,len(config.out_features ) ) self.parent.assertListEqual(list(result.feature_maps[0].shape ) ,[self.batch_size, self.hidden_sizes[1], 4, 4] ) # verify channels self.parent.assertEqual(len(model.channels ) ,len(config.out_features ) ) self.parent.assertListEqual(model.channels ,config.hidden_sizes[1:] ) # verify backbone works with out_features=None lowercase__ : Tuple = None lowercase__ : Optional[int] = BitBackbone(config=_snake_case ) model.to(_snake_case ) model.eval() lowercase__ : int = model(_snake_case ) # verify feature maps self.parent.assertEqual(len(result.feature_maps ) ,1 ) self.parent.assertListEqual(list(result.feature_maps[0].shape ) ,[self.batch_size, self.hidden_sizes[-1], 1, 1] ) # verify channels self.parent.assertEqual(len(model.channels ) ,1 ) self.parent.assertListEqual(model.channels ,[config.hidden_sizes[-1]] ) def UpperCAmelCase ( self : Tuple ) -> Union[str, Any]: """simple docstring""" lowercase__ : Optional[Any] = self.prepare_config_and_inputs() lowercase__ , lowercase__ , lowercase__ : Optional[Any] = config_and_inputs lowercase__ : Any = {'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class __A ( A_ ,A_ ,unittest.TestCase ): '''simple docstring''' lowerCAmelCase : List[str] = (BitModel, BitForImageClassification, BitBackbone) if is_torch_available() else () lowerCAmelCase : List[str] = ( {"feature-extraction": BitModel, "image-classification": BitForImageClassification} if is_torch_available() else {} ) lowerCAmelCase : Any = False lowerCAmelCase : str = False lowerCAmelCase : str = False lowerCAmelCase : Optional[int] = False lowerCAmelCase : List[str] = False def UpperCAmelCase ( self : Optional[Any] ) -> int: """simple docstring""" lowercase__ : Optional[Any] = BitModelTester(self ) lowercase__ : Tuple = ConfigTester(self ,config_class=_snake_case ,has_text_modality=_snake_case ) def UpperCAmelCase ( self : Dict ) -> Optional[int]: """simple docstring""" self.create_and_test_config_common_properties() self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() def UpperCAmelCase ( self : int ) -> Tuple: """simple docstring""" return @unittest.skip(reason='''Bit does not output attentions''' ) def UpperCAmelCase ( self : Optional[Any] ) -> Dict: """simple docstring""" pass @unittest.skip(reason='''Bit does not use inputs_embeds''' ) def UpperCAmelCase ( self : Tuple ) -> Optional[Any]: """simple docstring""" pass @unittest.skip(reason='''Bit does not support input and output embeddings''' ) def UpperCAmelCase ( self : Union[str, Any] ) -> Any: """simple docstring""" pass def UpperCAmelCase ( self : Union[str, Any] ) -> Dict: """simple docstring""" lowercase__ , lowercase__ : Dict = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowercase__ : List[str] = model_class(_snake_case ) lowercase__ : List[Any] = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic lowercase__ : List[str] = [*signature.parameters.keys()] lowercase__ : List[Any] = ['''pixel_values'''] self.assertListEqual(arg_names[:1] ,_snake_case ) def UpperCAmelCase ( self : Union[str, Any] ) -> Dict: """simple docstring""" lowercase__ : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_snake_case ) def UpperCAmelCase ( self : Optional[int] ) -> Union[str, Any]: """simple docstring""" lowercase__ : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_backbone(*_snake_case ) def UpperCAmelCase ( self : Optional[int] ) -> Optional[int]: """simple docstring""" lowercase__ , lowercase__ : Any = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowercase__ : List[Any] = model_class(config=_snake_case ) for name, module in model.named_modules(): if isinstance(_snake_case ,(nn.BatchNormad, nn.GroupNorm) ): self.assertTrue( torch.all(module.weight == 1 ) ,msg=f"""Parameter {name} of model {model_class} seems not properly initialized""" ,) self.assertTrue( torch.all(module.bias == 0 ) ,msg=f"""Parameter {name} of model {model_class} seems not properly initialized""" ,) def UpperCAmelCase ( self : List[str] ) -> Optional[Any]: """simple docstring""" def check_hidden_states_output(_snake_case : Union[str, Any] ,_snake_case : int ,_snake_case : Optional[Any] ): lowercase__ : Optional[int] = model_class(_snake_case ) model.to(_snake_case ) model.eval() with torch.no_grad(): lowercase__ : Tuple = model(**self._prepare_for_class(_snake_case ,_snake_case ) ) lowercase__ : Optional[Any] = outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states lowercase__ : int = self.model_tester.num_stages self.assertEqual(len(_snake_case ) ,expected_num_stages + 1 ) # Bit's feature maps are of shape (batch_size, num_channels, height, width) self.assertListEqual( list(hidden_states[0].shape[-2:] ) ,[self.model_tester.image_size // 4, self.model_tester.image_size // 4] ,) lowercase__ , lowercase__ : List[str] = self.model_tester.prepare_config_and_inputs_for_common() lowercase__ : str = ['''preactivation''', '''bottleneck'''] for model_class in self.all_model_classes: for layer_type in layers_type: lowercase__ : Dict = layer_type lowercase__ : Tuple = True check_hidden_states_output(_snake_case ,_snake_case ,_snake_case ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] lowercase__ : List[Any] = True check_hidden_states_output(_snake_case ,_snake_case ,_snake_case ) @unittest.skip(reason='''Bit does not use feedforward chunking''' ) def UpperCAmelCase ( self : str ) -> Optional[int]: """simple docstring""" pass def UpperCAmelCase ( self : List[Any] ) -> List[str]: """simple docstring""" lowercase__ : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_snake_case ) @slow def UpperCAmelCase ( self : List[Any] ) -> List[str]: """simple docstring""" for model_name in BIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowercase__ : Dict = BitModel.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) def __UpperCAmelCase ( ) -> List[str]: lowercase__ : int = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_torch @require_vision class __A ( unittest.TestCase ): '''simple docstring''' @cached_property def UpperCAmelCase ( self : Dict ) -> List[str]: """simple docstring""" return ( BitImageProcessor.from_pretrained(BIT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) if is_vision_available() else None ) @slow def UpperCAmelCase ( self : Dict ) -> Union[str, Any]: """simple docstring""" lowercase__ : Union[str, Any] = BitForImageClassification.from_pretrained(BIT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ).to(_snake_case ) lowercase__ : List[Any] = self.default_image_processor lowercase__ : str = prepare_img() lowercase__ : str = image_processor(images=_snake_case ,return_tensors='''pt''' ).to(_snake_case ) # forward pass with torch.no_grad(): lowercase__ : Optional[int] = model(**_snake_case ) # verify the logits lowercase__ : Tuple = torch.Size((1, 1_000) ) self.assertEqual(outputs.logits.shape ,_snake_case ) lowercase__ : List[str] = torch.tensor([[-0.6526, -0.5263, -1.4398]] ).to(_snake_case ) self.assertTrue(torch.allclose(outputs.logits[0, :3] ,_snake_case ,atol=1e-4 ) ) @require_torch class __A ( A_ ,unittest.TestCase ): '''simple docstring''' lowerCAmelCase : Optional[int] = (BitBackbone,) if is_torch_available() else () lowerCAmelCase : Any = BitConfig lowerCAmelCase : Optional[int] = False def UpperCAmelCase ( self : Any ) -> Any: """simple docstring""" lowercase__ : Tuple = BitModelTester(self )
16
from typing import Optional import numpy as np import torch from torch import nn from transformers import GPTaConfig, GPTaLMHeadModel from transformers.modeling_utils import ModuleUtilsMixin from ...configuration_utils import ConfigMixin, register_to_config from ...models import ModelMixin class __magic_name__ ( lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ): """simple docstring""" __UpperCamelCase = [r'''h\.\d+\.attn\.bias''', r'''h\.\d+\.attn\.masked_bias'''] @register_to_config def __init__( self :List[Any] , snake_case :int , snake_case :int , snake_case :Optional[int] = None , snake_case :int = 50_257 , snake_case :int = 1_024 , snake_case :int = 768 , snake_case :int = 12 , snake_case :int = 12 , snake_case :Optional[int] = None , snake_case :str = "gelu_new" , snake_case :float = 0.1 , snake_case :float = 0.1 , snake_case :float = 0.1 , snake_case :float = 1e-5 , snake_case :float = 0.02 , snake_case :bool = True , snake_case :bool = True , snake_case :bool = False , snake_case :bool = False , ): '''simple docstring''' super().__init__() A_ : Tuple = prefix_length if prefix_inner_dim != n_embd and prefix_hidden_dim is None: raise ValueError( f"`prefix_hidden_dim` cannot be `None` when `prefix_inner_dim`: {prefix_hidden_dim} and" f" `n_embd`: {n_embd} are not equal." ) A_ : List[Any] = prefix_inner_dim A_ : Union[str, Any] = prefix_hidden_dim A_ : List[str] = ( nn.Linear(self.prefix_inner_dim , self.prefix_hidden_dim ) if self.prefix_hidden_dim is not None else nn.Identity() ) A_ : List[Any] = ( nn.Linear(self.prefix_hidden_dim , snake_case ) if self.prefix_hidden_dim is not None else nn.Identity() ) A_ : List[Any] = GPTaConfig( vocab_size=snake_case , n_positions=snake_case , n_embd=snake_case , n_layer=snake_case , n_head=snake_case , n_inner=snake_case , activation_function=snake_case , resid_pdrop=snake_case , embd_pdrop=snake_case , attn_pdrop=snake_case , layer_norm_epsilon=snake_case , initializer_range=snake_case , scale_attn_weights=snake_case , use_cache=snake_case , scale_attn_by_inverse_layer_idx=snake_case , reorder_and_upcast_attn=snake_case , ) A_ : Optional[Any] = GPTaLMHeadModel(snake_case ) def SCREAMING_SNAKE_CASE ( self :Tuple , snake_case :torch.Tensor , snake_case :torch.Tensor , snake_case :Optional[torch.Tensor] = None , snake_case :Optional[torch.Tensor] = None , ): '''simple docstring''' A_ : Any = self.transformer.transformer.wte(snake_case ) A_ : str = self.encode_prefix(snake_case ) A_ : Union[str, Any] = self.decode_prefix(snake_case ) A_ : int = torch.cat((prefix_embeds, embedding_text) , dim=1 ) if labels is not None: A_ : Dict = self.get_dummy_token(input_ids.shape[0] , input_ids.device ) A_ : int = torch.cat((dummy_token, input_ids) , dim=1 ) A_ : Union[str, Any] = self.transformer(inputs_embeds=snake_case , labels=snake_case , attention_mask=snake_case ) if self.prefix_hidden_dim is not None: return out, hidden else: return out def SCREAMING_SNAKE_CASE ( self :str , snake_case :int , snake_case :torch.device ): '''simple docstring''' return torch.zeros(snake_case , self.prefix_length , dtype=torch.intaa , device=snake_case ) def SCREAMING_SNAKE_CASE ( self :Optional[int] , snake_case :int ): '''simple docstring''' return self.encode_prefix(snake_case ) @torch.no_grad() def SCREAMING_SNAKE_CASE ( self :List[Any] , snake_case :Dict , snake_case :Optional[int] , snake_case :Any ): '''simple docstring''' A_ : Any = torch.split(snake_case , 1 , dim=0 ) A_ : Optional[int] = [] A_ : Union[str, Any] = [] for feature in features: A_ : Tuple = self.decode_prefix(feature.to(snake_case ) ) # back to the clip feature # Only support beam search for now A_ , A_ : Dict = self.generate_beam( input_embeds=snake_case , device=snake_case , eos_token_id=snake_case ) generated_tokens.append(output_tokens[0] ) generated_seq_lengths.append(seq_lengths[0] ) A_ : int = torch.stack(snake_case ) A_ : int = torch.stack(snake_case ) return generated_tokens, generated_seq_lengths @torch.no_grad() def SCREAMING_SNAKE_CASE ( self :Union[str, Any] , snake_case :int=None , snake_case :str=None , snake_case :int=None , snake_case :int = 5 , snake_case :int = 67 , snake_case :float = 1.0 , snake_case :Optional[int] = None , ): '''simple docstring''' A_ : Optional[Any] = eos_token_id A_ : List[Any] = None A_ : List[Any] = None A_ : str = torch.ones(snake_case , device=snake_case , dtype=torch.int ) A_ : Any = torch.zeros(snake_case , device=snake_case , dtype=torch.bool ) if input_embeds is not None: A_ : Any = input_embeds else: A_ : Optional[Any] = self.transformer.transformer.wte(snake_case ) for i in range(snake_case ): A_ : Optional[Any] = self.transformer(inputs_embeds=snake_case ) A_ : str = outputs.logits A_ : int = logits[:, -1, :] / (temperature if temperature > 0 else 1.0) A_ : List[str] = logits.softmax(-1 ).log() if scores is None: A_ , A_ : Union[str, Any] = logits.topk(snake_case , -1 ) A_ : Tuple = generated.expand(snake_case , *generated.shape[1:] ) A_ , A_ : str = next_tokens.permute(1 , 0 ), scores.squeeze(0 ) if tokens is None: A_ : Union[str, Any] = next_tokens else: A_ : List[str] = tokens.expand(snake_case , *tokens.shape[1:] ) A_ : Union[str, Any] = torch.cat((tokens, next_tokens) , dim=1 ) else: A_ : List[str] = -float(np.inf ) A_ : List[Any] = 0 A_ : Union[str, Any] = scores[:, None] + logits seq_lengths[~is_stopped] += 1 A_ : Optional[Any] = scores_sum / seq_lengths[:, None] A_ , A_ : List[str] = scores_sum_average.view(-1 ).topk(snake_case , -1 ) A_ : str = next_tokens // scores_sum.shape[1] A_ : Union[str, Any] = seq_lengths[next_tokens_source] A_ : Optional[int] = next_tokens % scores_sum.shape[1] A_ : Tuple = next_tokens.unsqueeze(1 ) A_ : Tuple = tokens[next_tokens_source] A_ : Dict = torch.cat((tokens, next_tokens) , dim=1 ) A_ : Dict = generated[next_tokens_source] A_ : Union[str, Any] = scores_sum_average * seq_lengths A_ : Optional[int] = is_stopped[next_tokens_source] A_ : Tuple = self.transformer.transformer.wte(next_tokens.squeeze() ).view(generated.shape[0] , 1 , -1 ) A_ : Union[str, Any] = torch.cat((generated, next_token_embed) , dim=1 ) A_ : Any = is_stopped + next_tokens.eq(snake_case ).squeeze() if is_stopped.all(): break A_ : int = scores / seq_lengths A_ : str = scores.argsort(descending=snake_case ) # tokens tensors are already padded to max_seq_length A_ : Dict = [tokens[i] for i in order] A_ : int = torch.stack(snake_case , dim=0 ) A_ : List[Any] = torch.tensor([seq_lengths[i] for i in order] , dtype=seq_lengths.dtype ) return output_texts, seq_lengths
300
0
"""simple docstring""" import asyncio import os import shutil import subprocess import sys import tempfile import unittest from distutils.util import strtobool from functools import partial from pathlib import Path from typing import List, Union from unittest import mock import torch from ..state import AcceleratorState, PartialState from ..utils import ( gather, is_bnb_available, is_comet_ml_available, is_datasets_available, is_deepspeed_available, is_mps_available, is_safetensors_available, is_tensorboard_available, is_torch_version, is_tpu_available, is_transformers_available, is_wandb_available, is_xpu_available, ) def _A ( UpperCamelCase_ : List[Any], UpperCamelCase_ : Tuple=False) -> int: '''simple docstring''' try: __lowercase = os.environ[key] except KeyError: # KEY isn't set, default to `default`. __lowercase = default else: # KEY is set, convert it to True or False. try: __lowercase = strtobool(UpperCamelCase_) except ValueError: # More values are supported, but let's keep the message simple. raise ValueError(F"""If set, {key} must be yes or no.""") return _value _a = parse_flag_from_env('RUN_SLOW', default=False) def _A ( UpperCamelCase_ : int) -> Optional[int]: '''simple docstring''' return unittest.skip("Test was skipped")(UpperCamelCase_) def _A ( UpperCamelCase_ : Any) -> Optional[int]: '''simple docstring''' return unittest.skipUnless(_run_slow_tests, "test is slow")(UpperCamelCase_) def _A ( UpperCamelCase_ : Tuple) -> List[Any]: '''simple docstring''' return unittest.skipUnless(not torch.cuda.is_available(), "test requires only a CPU")(UpperCamelCase_) def _A ( UpperCamelCase_ : int) -> Any: '''simple docstring''' return unittest.skipUnless(torch.cuda.is_available(), "test requires a GPU")(UpperCamelCase_) def _A ( UpperCamelCase_ : Optional[int]) -> Tuple: '''simple docstring''' return unittest.skipUnless(is_xpu_available(), "test requires a XPU")(UpperCamelCase_) def _A ( UpperCamelCase_ : Optional[int]) -> Dict: '''simple docstring''' return unittest.skipUnless(is_mps_available(), "test requires a `mps` backend support in `torch`")(UpperCamelCase_) def _A ( UpperCamelCase_ : int) -> List[Any]: '''simple docstring''' return unittest.skipUnless( is_transformers_available() and is_datasets_available(), "test requires the Hugging Face suite")(UpperCamelCase_) def _A ( UpperCamelCase_ : Union[str, Any]) -> Tuple: '''simple docstring''' return unittest.skipUnless(is_bnb_available(), "test requires the bitsandbytes library")(UpperCamelCase_) def _A ( UpperCamelCase_ : Optional[Any]) -> int: '''simple docstring''' return unittest.skipUnless(is_tpu_available(), "test requires TPU")(UpperCamelCase_) def _A ( UpperCamelCase_ : str) -> List[Any]: '''simple docstring''' return unittest.skipUnless(torch.cuda.device_count() == 1, "test requires a GPU")(UpperCamelCase_) def _A ( UpperCamelCase_ : Union[str, Any]) -> List[str]: '''simple docstring''' return unittest.skipUnless(torch.xpu.device_count() == 1, "test requires a XPU")(UpperCamelCase_) def _A ( UpperCamelCase_ : List[Any]) -> str: '''simple docstring''' return unittest.skipUnless(torch.cuda.device_count() > 1, "test requires multiple GPUs")(UpperCamelCase_) def _A ( UpperCamelCase_ : Optional[int]) -> str: '''simple docstring''' return unittest.skipUnless(torch.xpu.device_count() > 1, "test requires multiple XPUs")(UpperCamelCase_) def _A ( UpperCamelCase_ : str) -> int: '''simple docstring''' return unittest.skipUnless(is_safetensors_available(), "test requires safetensors")(UpperCamelCase_) def _A ( UpperCamelCase_ : Dict) -> str: '''simple docstring''' return unittest.skipUnless(is_deepspeed_available(), "test requires DeepSpeed")(UpperCamelCase_) def _A ( UpperCamelCase_ : List[str]) -> Dict: '''simple docstring''' return unittest.skipUnless(is_torch_version(">=", "1.12.0"), "test requires torch version >= 1.12.0")(UpperCamelCase_) def _A ( UpperCamelCase_ : Dict=None, UpperCamelCase_ : int=None) -> int: '''simple docstring''' if test_case is None: return partial(UpperCamelCase_, version=UpperCamelCase_) return unittest.skipUnless(is_torch_version(">=", UpperCamelCase_), F"""test requires torch version >= {version}""")(UpperCamelCase_) def _A ( UpperCamelCase_ : List[Any]) -> Any: '''simple docstring''' return unittest.skipUnless(is_tensorboard_available(), "test requires Tensorboard")(UpperCamelCase_) def _A ( UpperCamelCase_ : Optional[Any]) -> Union[str, Any]: '''simple docstring''' return unittest.skipUnless(is_wandb_available(), "test requires wandb")(UpperCamelCase_) def _A ( UpperCamelCase_ : Optional[int]) -> Tuple: '''simple docstring''' return unittest.skipUnless(is_comet_ml_available(), "test requires comet_ml")(UpperCamelCase_) _a = ( any([is_wandb_available(), is_tensorboard_available()]) and not is_comet_ml_available() ) def _A ( UpperCamelCase_ : List[str]) -> str: '''simple docstring''' return unittest.skipUnless( _atleast_one_tracker_available, "test requires at least one tracker to be available and for `comet_ml` to not be installed", )(UpperCamelCase_) class _lowerCAmelCase ( unittest.TestCase ): """simple docstring""" __UpperCAmelCase : Tuple = True @classmethod def _lowercase ( cls : Dict ): __lowercase = tempfile.mkdtemp() @classmethod def _lowercase ( cls : List[str] ): if os.path.exists(cls.tmpdir ): shutil.rmtree(cls.tmpdir ) def _lowercase ( self : Union[str, Any] ): if self.clear_on_setup: for path in Path(self.tmpdir ).glob("**/*" ): if path.is_file(): path.unlink() elif path.is_dir(): shutil.rmtree(UpperCAmelCase__ ) class _lowerCAmelCase ( unittest.TestCase ): """simple docstring""" def _lowercase ( self : Optional[Any] ): super().tearDown() # Reset the state of the AcceleratorState singleton. AcceleratorState._reset_state() PartialState._reset_state() class _lowerCAmelCase ( unittest.TestCase ): """simple docstring""" def _lowercase ( self : Optional[int], UpperCAmelCase__ : Union[mock.Mock, List[mock.Mock]] ): __lowercase = mocks if isinstance(UpperCAmelCase__, (tuple, list) ) else [mocks] for m in self.mocks: m.start() self.addCleanup(m.stop ) def _A ( UpperCamelCase_ : int) -> int: '''simple docstring''' __lowercase = AcceleratorState() __lowercase = tensor[None].clone().to(state.device) __lowercase = gather(UpperCamelCase_).cpu() __lowercase = tensor[0].cpu() for i in range(tensors.shape[0]): if not torch.equal(tensors[i], UpperCamelCase_): return False return True class _lowerCAmelCase : """simple docstring""" def __init__( self : Union[str, Any], UpperCAmelCase__ : List[Any], UpperCAmelCase__ : List[str], UpperCAmelCase__ : Tuple ): __lowercase = returncode __lowercase = stdout __lowercase = stderr async def _A ( UpperCamelCase_ : int, UpperCamelCase_ : List[str]) -> Any: '''simple docstring''' while True: __lowercase = await stream.readline() if line: callback(UpperCamelCase_) else: break async def _A ( UpperCamelCase_ : Optional[int], UpperCamelCase_ : List[str]=None, UpperCamelCase_ : Optional[Any]=None, UpperCamelCase_ : List[str]=None, UpperCamelCase_ : Dict=False, UpperCamelCase_ : int=False) -> _RunOutput: '''simple docstring''' if echo: print("\nRunning: ", " ".join(UpperCamelCase_)) __lowercase = await asyncio.create_subprocess_exec( cmd[0], *cmd[1:], stdin=UpperCamelCase_, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE, env=UpperCamelCase_, ) # note: there is a warning for a possible deadlock when using `wait` with huge amounts of data in the pipe # https://docs.python.org/3/library/asyncio-subprocess.html#asyncio.asyncio.subprocess.Process.wait # # If it starts hanging, will need to switch to the following code. The problem is that no data # will be seen until it's done and if it hangs for example there will be no debug info. # out, err = await p.communicate() # return _RunOutput(p.returncode, out, err) __lowercase = [] __lowercase = [] def tee(UpperCamelCase_ : Any, UpperCamelCase_ : List[Any], UpperCamelCase_ : Dict, UpperCamelCase_ : int=""): __lowercase = line.decode("utf-8").rstrip() sink.append(UpperCamelCase_) if not quiet: print(UpperCamelCase_, UpperCamelCase_, file=UpperCamelCase_) # XXX: the timeout doesn't seem to make any difference here await asyncio.wait( [ asyncio.create_task(_read_stream(p.stdout, lambda UpperCamelCase_: tee(UpperCamelCase_, UpperCamelCase_, sys.stdout, label="stdout:"))), asyncio.create_task(_read_stream(p.stderr, lambda UpperCamelCase_: tee(UpperCamelCase_, UpperCamelCase_, sys.stderr, label="stderr:"))), ], timeout=UpperCamelCase_, ) return _RunOutput(await p.wait(), UpperCamelCase_, UpperCamelCase_) def _A ( UpperCamelCase_ : str, UpperCamelCase_ : Optional[Any]=None, UpperCamelCase_ : Dict=None, UpperCamelCase_ : str=180, UpperCamelCase_ : Optional[int]=False, UpperCamelCase_ : int=True) -> _RunOutput: '''simple docstring''' __lowercase = asyncio.get_event_loop() __lowercase = loop.run_until_complete( _stream_subprocess(UpperCamelCase_, env=UpperCamelCase_, stdin=UpperCamelCase_, timeout=UpperCamelCase_, quiet=UpperCamelCase_, echo=UpperCamelCase_)) __lowercase = " ".join(UpperCamelCase_) if result.returncode > 0: __lowercase = "\n".join(result.stderr) raise RuntimeError( F"""'{cmd_str}' failed with returncode {result.returncode}\n\n""" F"""The combined stderr from workers follows:\n{stderr}""") return result class _lowerCAmelCase ( lowercase ): """simple docstring""" pass def _A ( UpperCamelCase_ : List[str], UpperCamelCase_ : Optional[int]=False) -> Union[str, Any]: '''simple docstring''' try: __lowercase = subprocess.check_output(UpperCamelCase_, stderr=subprocess.STDOUT) if return_stdout: if hasattr(UpperCamelCase_, "decode"): __lowercase = output.decode("utf-8") return output except subprocess.CalledProcessError as e: raise SubprocessCallException( F"""Command `{" ".join(UpperCamelCase_)}` failed with the following error:\n\n{e.output.decode()}""") from e
17
import warnings from ...utils import logging from .image_processing_yolos import YolosImageProcessor _lowerCAmelCase : Tuple = logging.get_logger(__name__) class __magic_name__ ( lowerCamelCase__ ): """simple docstring""" def __init__( self :Union[str, Any] , *snake_case :Tuple , **snake_case :Any ): '''simple docstring''' warnings.warn( "The class YolosFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please" " use YolosImageProcessor instead." , snake_case , ) super().__init__(*snake_case , **snake_case )
300
0
import contextlib import copy import random from typing import Any, Dict, Iterable, Optional, Union import numpy as np import torch from .utils import deprecate, is_transformers_available if is_transformers_available(): import transformers def _snake_case ( lowerCAmelCase : int ): """simple docstring""" random.seed(lowerCAmelCase ) np.random.seed(lowerCAmelCase ) torch.manual_seed(lowerCAmelCase ) torch.cuda.manual_seed_all(lowerCAmelCase ) # ^^ safe to call this function even if cuda is not available class a__ : def __init__( self : Tuple,_A : Iterable[torch.nn.Parameter],_A : float = 0.9999,_A : float = 0.0,_A : int = 0,_A : bool = False,_A : Union[float, int] = 1.0,_A : Union[float, int] = 2 / 3,_A : Optional[Any] = None,_A : Dict[str, Any] = None,**_A : str,): """simple docstring""" if isinstance(_A,torch.nn.Module ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = ( "Passing a `torch.nn.Module` to `ExponentialMovingAverage` is deprecated. " "Please pass the parameters of the module instead." ) deprecate( "passing a `torch.nn.Module` to `ExponentialMovingAverage`","1.0.0",_A,standard_warn=_A,) SCREAMING_SNAKE_CASE_ : str = parameters.parameters() # set use_ema_warmup to True if a torch.nn.Module is passed for backwards compatibility SCREAMING_SNAKE_CASE_ : str = True if kwargs.get("max_value",_A ) is not None: SCREAMING_SNAKE_CASE_ : str = "The `max_value` argument is deprecated. Please use `decay` instead." deprecate("max_value","1.0.0",_A,standard_warn=_A ) SCREAMING_SNAKE_CASE_ : Optional[int] = kwargs["max_value"] if kwargs.get("min_value",_A ) is not None: SCREAMING_SNAKE_CASE_ : Optional[int] = "The `min_value` argument is deprecated. Please use `min_decay` instead." deprecate("min_value","1.0.0",_A,standard_warn=_A ) SCREAMING_SNAKE_CASE_ : int = kwargs["min_value"] SCREAMING_SNAKE_CASE_ : Dict = list(_A ) SCREAMING_SNAKE_CASE_ : List[str] = [p.clone().detach() for p in parameters] if kwargs.get("device",_A ) is not None: SCREAMING_SNAKE_CASE_ : str = "The `device` argument is deprecated. Please use `to` instead." deprecate("device","1.0.0",_A,standard_warn=_A ) self.to(device=kwargs["device"] ) SCREAMING_SNAKE_CASE_ : Dict = None SCREAMING_SNAKE_CASE_ : Any = decay SCREAMING_SNAKE_CASE_ : List[str] = min_decay SCREAMING_SNAKE_CASE_ : Tuple = update_after_step SCREAMING_SNAKE_CASE_ : List[str] = use_ema_warmup SCREAMING_SNAKE_CASE_ : List[Any] = inv_gamma SCREAMING_SNAKE_CASE_ : List[Any] = power SCREAMING_SNAKE_CASE_ : Union[str, Any] = 0 SCREAMING_SNAKE_CASE_ : Union[str, Any] = None # set in `step()` SCREAMING_SNAKE_CASE_ : Dict = model_cls SCREAMING_SNAKE_CASE_ : Any = model_config @classmethod def __UpperCamelCase ( cls : Dict,_A : Tuple,_A : List[Any] ): """simple docstring""" SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Union[str, Any] = model_cls.load_config(_A,return_unused_kwargs=_A ) SCREAMING_SNAKE_CASE_ : Tuple = model_cls.from_pretrained(_A ) SCREAMING_SNAKE_CASE_ : Optional[Any] = cls(model.parameters(),model_cls=_A,model_config=model.config ) ema_model.load_state_dict(_A ) return ema_model def __UpperCamelCase ( self : Optional[Any],_A : int ): """simple docstring""" if self.model_cls is None: raise ValueError("`save_pretrained` can only be used if `model_cls` was defined at __init__." ) if self.model_config is None: raise ValueError("`save_pretrained` can only be used if `model_config` was defined at __init__." ) SCREAMING_SNAKE_CASE_ : str = self.model_cls.from_config(self.model_config ) SCREAMING_SNAKE_CASE_ : Dict = self.state_dict() state_dict.pop("shadow_params",_A ) model.register_to_config(**_A ) self.copy_to(model.parameters() ) model.save_pretrained(_A ) def __UpperCamelCase ( self : str,_A : int ): """simple docstring""" SCREAMING_SNAKE_CASE_ : List[Any] = max(0,optimization_step - self.update_after_step - 1 ) if step <= 0: return 0.0 if self.use_ema_warmup: SCREAMING_SNAKE_CASE_ : Optional[int] = 1 - (1 + step / self.inv_gamma) ** -self.power else: SCREAMING_SNAKE_CASE_ : List[str] = (1 + step) / (10 + step) SCREAMING_SNAKE_CASE_ : Union[str, Any] = min(_A,self.decay ) # make sure decay is not smaller than min_decay SCREAMING_SNAKE_CASE_ : Any = max(_A,self.min_decay ) return cur_decay_value @torch.no_grad() def __UpperCamelCase ( self : str,_A : Iterable[torch.nn.Parameter] ): """simple docstring""" if isinstance(_A,torch.nn.Module ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = ( "Passing a `torch.nn.Module` to `ExponentialMovingAverage.step` is deprecated. " "Please pass the parameters of the module instead." ) deprecate( "passing a `torch.nn.Module` to `ExponentialMovingAverage.step`","1.0.0",_A,standard_warn=_A,) SCREAMING_SNAKE_CASE_ : Tuple = parameters.parameters() SCREAMING_SNAKE_CASE_ : int = list(_A ) self.optimization_step += 1 # Compute the decay factor for the exponential moving average. SCREAMING_SNAKE_CASE_ : Any = self.get_decay(self.optimization_step ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = decay SCREAMING_SNAKE_CASE_ : Tuple = 1 - decay SCREAMING_SNAKE_CASE_ : str = contextlib.nullcontext if is_transformers_available() and transformers.deepspeed.is_deepspeed_zeroa_enabled(): import deepspeed for s_param, param in zip(self.shadow_params,_A ): if is_transformers_available() and transformers.deepspeed.is_deepspeed_zeroa_enabled(): SCREAMING_SNAKE_CASE_ : Union[str, Any] = deepspeed.zero.GatheredParameters(_A,modifier_rank=_A ) with context_manager(): if param.requires_grad: s_param.sub_(one_minus_decay * (s_param - param) ) else: s_param.copy_(_A ) def __UpperCamelCase ( self : int,_A : Iterable[torch.nn.Parameter] ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Tuple = list(_A ) for s_param, param in zip(self.shadow_params,_A ): param.data.copy_(s_param.to(param.device ).data ) def __UpperCamelCase ( self : Dict,_A : Any=None,_A : Union[str, Any]=None ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Union[str, Any] = [ p.to(device=_A,dtype=_A ) if p.is_floating_point() else p.to(device=_A ) for p in self.shadow_params ] def __UpperCamelCase ( self : Optional[Any] ): """simple docstring""" return { "decay": self.decay, "min_decay": self.min_decay, "optimization_step": self.optimization_step, "update_after_step": self.update_after_step, "use_ema_warmup": self.use_ema_warmup, "inv_gamma": self.inv_gamma, "power": self.power, "shadow_params": self.shadow_params, } def __UpperCamelCase ( self : Optional[Any],_A : Iterable[torch.nn.Parameter] ): """simple docstring""" SCREAMING_SNAKE_CASE_ : List[str] = [param.detach().cpu().clone() for param in parameters] def __UpperCamelCase ( self : int,_A : Iterable[torch.nn.Parameter] ): """simple docstring""" if self.temp_stored_params is None: raise RuntimeError("This ExponentialMovingAverage has no `store()`ed weights " "to `restore()`" ) for c_param, param in zip(self.temp_stored_params,_A ): param.data.copy_(c_param.data ) # Better memory-wise. SCREAMING_SNAKE_CASE_ : Dict = None def __UpperCamelCase ( self : Union[str, Any],_A : dict ): """simple docstring""" SCREAMING_SNAKE_CASE_ : int = copy.deepcopy(_A ) SCREAMING_SNAKE_CASE_ : str = state_dict.get("decay",self.decay ) if self.decay < 0.0 or self.decay > 1.0: raise ValueError("Decay must be between 0 and 1" ) SCREAMING_SNAKE_CASE_ : Dict = state_dict.get("min_decay",self.min_decay ) if not isinstance(self.min_decay,_A ): raise ValueError("Invalid min_decay" ) SCREAMING_SNAKE_CASE_ : Any = state_dict.get("optimization_step",self.optimization_step ) if not isinstance(self.optimization_step,_A ): raise ValueError("Invalid optimization_step" ) SCREAMING_SNAKE_CASE_ : Any = state_dict.get("update_after_step",self.update_after_step ) if not isinstance(self.update_after_step,_A ): raise ValueError("Invalid update_after_step" ) SCREAMING_SNAKE_CASE_ : Optional[Any] = state_dict.get("use_ema_warmup",self.use_ema_warmup ) if not isinstance(self.use_ema_warmup,_A ): raise ValueError("Invalid use_ema_warmup" ) SCREAMING_SNAKE_CASE_ : Optional[Any] = state_dict.get("inv_gamma",self.inv_gamma ) if not isinstance(self.inv_gamma,(float, int) ): raise ValueError("Invalid inv_gamma" ) SCREAMING_SNAKE_CASE_ : int = state_dict.get("power",self.power ) if not isinstance(self.power,(float, int) ): raise ValueError("Invalid power" ) SCREAMING_SNAKE_CASE_ : str = state_dict.get("shadow_params",_A ) if shadow_params is not None: SCREAMING_SNAKE_CASE_ : int = shadow_params if not isinstance(self.shadow_params,_A ): raise ValueError("shadow_params must be a list" ) if not all(isinstance(_A,torch.Tensor ) for p in self.shadow_params ): raise ValueError("shadow_params must all be Tensors" )
18
from __future__ import annotations def __snake_case ( _lowerCAmelCase : list[float] ) -> bool: if len(_lowerCAmelCase ) < 2: raise ValueError("Monogons and Digons are not polygons in the Euclidean space" ) if any(i <= 0 for i in nums ): raise ValueError("All values must be greater than 0" ) A_ : List[str] = nums.copy() copy_nums.sort() return copy_nums[-1] < sum(copy_nums[:-1] ) if __name__ == "__main__": import doctest doctest.testmod()
300
0
import argparse from transformers import TaConfig, TaForConditionalGeneration, load_tf_weights_in_ta from transformers.utils import logging logging.set_verbosity_info() def lowerCamelCase_ ( lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ): # Initialise PyTorch model lowerCamelCase_ = TaConfig.from_json_file(lowerCamelCase__ ) print(F'Building PyTorch model from configuration: {config}' ) lowerCamelCase_ = TaForConditionalGeneration(lowerCamelCase__ ) # Load weights from tf checkpoint load_tf_weights_in_ta(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) # Save pytorch-model print(F'Save PyTorch model to {pytorch_dump_path}' ) model.save_pretrained(lowerCamelCase__ ) if __name__ == "__main__": __A =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( '''--config_file''', default=None, type=str, required=True, help=( '''The config json file corresponding to the pre-trained T5 model. \nThis specifies the model architecture.''' ), ) parser.add_argument( '''--pytorch_dump_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''' ) __A =parser.parse_args() convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.config_file, args.pytorch_dump_path)
19
import inspect from typing import Callable, List, Optional, Union import torch from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer from diffusers import DiffusionPipeline from diffusers.models import AutoencoderKL, UNetaDConditionModel from diffusers.pipelines.stable_diffusion import StableDiffusionPipelineOutput from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker from diffusers.schedulers import DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler from diffusers.utils import logging _lowerCAmelCase : Any = logging.get_logger(__name__) # pylint: disable=invalid-name class __magic_name__ ( lowerCamelCase__ ): """simple docstring""" def __init__( self :Union[str, Any] , snake_case :AutoencoderKL , snake_case :CLIPTextModel , snake_case :CLIPTokenizer , snake_case :UNetaDConditionModel , snake_case :Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler] , snake_case :StableDiffusionSafetyChecker , snake_case :CLIPImageProcessor , ): '''simple docstring''' super().__init__() self.register_modules( vae=snake_case , text_encoder=snake_case , tokenizer=snake_case , unet=snake_case , scheduler=snake_case , safety_checker=snake_case , feature_extractor=snake_case , ) def SCREAMING_SNAKE_CASE ( self :List[Any] , snake_case :Optional[Union[str, int]] = "auto" ): '''simple docstring''' if slice_size == "auto": # half the attention head size is usually a good trade-off between # speed and memory A_ : int = self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(snake_case ) def SCREAMING_SNAKE_CASE ( self :Dict ): '''simple docstring''' self.enable_attention_slicing(snake_case ) @torch.no_grad() def __call__( self :Any , snake_case :Union[str, List[str]] , snake_case :int = 512 , snake_case :int = 512 , snake_case :int = 50 , snake_case :float = 7.5 , snake_case :Optional[Union[str, List[str]]] = None , snake_case :Optional[int] = 1 , snake_case :float = 0.0 , snake_case :Optional[torch.Generator] = None , snake_case :Optional[torch.FloatTensor] = None , snake_case :Optional[str] = "pil" , snake_case :bool = True , snake_case :Optional[Callable[[int, int, torch.FloatTensor], None]] = None , snake_case :int = 1 , snake_case :Optional[torch.FloatTensor] = None , **snake_case :Optional[Any] , ): '''simple docstring''' if isinstance(snake_case , snake_case ): A_ : Dict = 1 elif isinstance(snake_case , snake_case ): A_ : Optional[Any] = len(snake_case ) else: raise ValueError(f"`prompt` has to be of type `str` or `list` but is {type(snake_case )}" ) if height % 8 != 0 or width % 8 != 0: raise ValueError(f"`height` and `width` have to be divisible by 8 but are {height} and {width}." ) if (callback_steps is None) or ( callback_steps is not None and (not isinstance(snake_case , snake_case ) or callback_steps <= 0) ): raise ValueError( f"`callback_steps` has to be a positive integer but is {callback_steps} of type" f" {type(snake_case )}." ) # get prompt text embeddings A_ : int = self.tokenizer( snake_case , padding="max_length" , max_length=self.tokenizer.model_max_length , return_tensors="pt" , ) A_ : Dict = text_inputs.input_ids if text_input_ids.shape[-1] > self.tokenizer.model_max_length: A_ : Optional[int] = 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}" ) A_ : Tuple = text_input_ids[:, : self.tokenizer.model_max_length] if text_embeddings is None: A_ : Union[str, Any] = self.text_encoder(text_input_ids.to(self.device ) )[0] # duplicate text embeddings for each generation per prompt, using mps friendly method A_ , A_ , A_ : int = text_embeddings.shape A_ : List[str] = text_embeddings.repeat(1 , snake_case , 1 ) A_ : List[str] = text_embeddings.view(bs_embed * num_images_per_prompt , snake_case , -1 ) # here `guidance_scale` is defined analog to the guidance weight `w` of equation (2) # of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1` # corresponds to doing no classifier free guidance. A_ : Dict = guidance_scale > 1.0 # get unconditional embeddings for classifier free guidance if do_classifier_free_guidance: A_ : List[str] if negative_prompt is None: A_ : List[str] = [""] elif type(snake_case ) is not type(snake_case ): raise TypeError( f"`negative_prompt` should be the same type to `prompt`, but got {type(snake_case )} !=" f" {type(snake_case )}." ) elif isinstance(snake_case , snake_case ): A_ : Optional[Any] = [negative_prompt] elif batch_size != len(snake_case ): raise ValueError( f"`negative_prompt`: {negative_prompt} has batch size {len(snake_case )}, but `prompt`:" f" {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches" " the batch size of `prompt`." ) else: A_ : Any = negative_prompt A_ : Optional[int] = text_input_ids.shape[-1] A_ : Dict = self.tokenizer( snake_case , padding="max_length" , max_length=snake_case , truncation=snake_case , return_tensors="pt" , ) A_ : Any = self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # duplicate unconditional embeddings for each generation per prompt, using mps friendly method A_ : Tuple = uncond_embeddings.shape[1] A_ : Dict = uncond_embeddings.repeat(snake_case , snake_case , 1 ) A_ : Dict = uncond_embeddings.view(batch_size * num_images_per_prompt , snake_case , -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 A_ : Optional[int] = torch.cat([uncond_embeddings, text_embeddings] ) # get the initial random noise unless the user supplied it # Unlike in other pipelines, latents need to be generated in the target device # for 1-to-1 results reproducibility with the CompVis implementation. # However this currently doesn't work in `mps`. A_ : List[str] = (batch_size * num_images_per_prompt, self.unet.config.in_channels, height // 8, width // 8) A_ : str = (batch_size * num_images_per_prompt, self.unet.config.in_channels, 64, 64) A_ : List[Any] = text_embeddings.dtype if latents is None: if self.device.type == "mps": # randn does not exist on mps A_ : Tuple = torch.randn( snake_case , generator=snake_case , device="cpu" , dtype=snake_case ).to(self.device ) A_ : Optional[Any] = torch.randn(snake_case , generator=snake_case , device="cpu" , dtype=snake_case ).to( self.device ) else: A_ : int = torch.randn( snake_case , generator=snake_case , device=self.device , dtype=snake_case ) A_ : Optional[int] = torch.randn(snake_case , generator=snake_case , device=self.device , dtype=snake_case ) else: if latents_reference.shape != latents_shape: raise ValueError(f"Unexpected latents shape, got {latents.shape}, expected {latents_shape}" ) A_ : Tuple = latents_reference.to(self.device ) A_ : Any = latents.to(self.device ) # This is the key part of the pipeline where we # try to ensure that the generated images w/ the same seed # but different sizes actually result in similar images A_ : List[Any] = (latents_shape[3] - latents_shape_reference[3]) // 2 A_ : Optional[int] = (latents_shape[2] - latents_shape_reference[2]) // 2 A_ : Optional[int] = latents_shape_reference[3] if dx >= 0 else latents_shape_reference[3] + 2 * dx A_ : Dict = latents_shape_reference[2] if dy >= 0 else latents_shape_reference[2] + 2 * dy A_ : Optional[Any] = 0 if dx < 0 else dx A_ : Optional[Any] = 0 if dy < 0 else dy A_ : List[str] = max(-dx , 0 ) A_ : List[Any] = max(-dy , 0 ) # import pdb # pdb.set_trace() A_ : Any = latents_reference[:, :, dy : dy + h, dx : dx + w] # set timesteps self.scheduler.set_timesteps(snake_case ) # Some schedulers like PNDM have timesteps as arrays # It's more optimized to move all timesteps to correct device beforehand A_ : str = self.scheduler.timesteps.to(self.device ) # scale the initial noise by the standard deviation required by the scheduler A_ : Union[str, Any] = latents * self.scheduler.init_noise_sigma # prepare extra kwargs for the scheduler step, since not all schedulers have the same signature # eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers. # eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502 # and should be between [0, 1] A_ : Optional[int] = "eta" in set(inspect.signature(self.scheduler.step ).parameters.keys() ) A_ : List[str] = {} if accepts_eta: A_ : Union[str, Any] = eta for i, t in enumerate(self.progress_bar(snake_case ) ): # expand the latents if we are doing classifier free guidance A_ : Optional[Any] = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents A_ : Any = self.scheduler.scale_model_input(snake_case , snake_case ) # predict the noise residual A_ : List[str] = self.unet(snake_case , snake_case , encoder_hidden_states=snake_case ).sample # perform guidance if do_classifier_free_guidance: A_ , A_ : Dict = noise_pred.chunk(2 ) A_ : List[Any] = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) # compute the previous noisy sample x_t -> x_t-1 A_ : Tuple = self.scheduler.step(snake_case , snake_case , snake_case , **snake_case ).prev_sample # call the callback, if provided if callback is not None and i % callback_steps == 0: callback(snake_case , snake_case , snake_case ) A_ : List[str] = 1 / 0.18215 * latents A_ : Tuple = self.vae.decode(snake_case ).sample A_ : Dict = (image / 2 + 0.5).clamp(0 , 1 ) # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16 A_ : List[str] = image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() if self.safety_checker is not None: A_ : int = self.feature_extractor(self.numpy_to_pil(snake_case ) , return_tensors="pt" ).to( self.device ) A_ , A_ : List[str] = self.safety_checker( images=snake_case , clip_input=safety_checker_input.pixel_values.to(text_embeddings.dtype ) ) else: A_ : List[str] = None if output_type == "pil": A_ : Optional[int] = self.numpy_to_pil(snake_case ) if not return_dict: return (image, has_nsfw_concept) return StableDiffusionPipelineOutput(images=snake_case , nsfw_content_detected=snake_case )
300
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 lowercase : List[Any] = logging.get_logger(__name__) lowercase : int = { """facebook/deit-base-distilled-patch16-224""": ( """https://huggingface.co/facebook/deit-base-patch16-224/resolve/main/config.json""" ), # See all DeiT models at https://huggingface.co/models?filter=deit } class __snake_case ( lowerCAmelCase ): _a : List[Any]= "deit" def __init__( self ,snake_case=768 ,snake_case=12 ,snake_case=12 ,snake_case=3072 ,snake_case="gelu" ,snake_case=0.0 ,snake_case=0.0 ,snake_case=0.02 ,snake_case=1e-12 ,snake_case=224 ,snake_case=16 ,snake_case=3 ,snake_case=True ,snake_case=16 ,**snake_case ,): '''simple docstring''' super().__init__(**snake_case ) lowercase : List[Any] = hidden_size lowercase : Optional[Any] = num_hidden_layers lowercase : Optional[int] = num_attention_heads lowercase : Union[str, Any] = intermediate_size lowercase : int = hidden_act lowercase : Any = hidden_dropout_prob lowercase : List[str] = attention_probs_dropout_prob lowercase : List[str] = initializer_range lowercase : List[Any] = layer_norm_eps lowercase : Union[str, Any] = image_size lowercase : Any = patch_size lowercase : Union[str, Any] = num_channels lowercase : List[Any] = qkv_bias lowercase : Optional[int] = encoder_stride class __snake_case ( lowerCAmelCase ): _a : List[Any]= version.parse("1.11" ) @property def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' return OrderedDict( [ ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}), ] ) @property def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' return 1e-4
20
import torch import torch.nn as nn from transformers import CLIPConfig, CLIPVisionModel, PreTrainedModel from ...utils import logging _lowerCAmelCase : List[str] = logging.get_logger(__name__) def __snake_case ( _lowerCAmelCase : int , _lowerCAmelCase : Any ) -> Dict: A_ : Optional[Any] = nn.functional.normalize(_lowerCAmelCase ) A_ : List[str] = nn.functional.normalize(_lowerCAmelCase ) return torch.mm(_lowerCAmelCase , normalized_text_embeds.t() ) class __magic_name__ ( lowerCamelCase__ ): """simple docstring""" __UpperCamelCase = CLIPConfig __UpperCamelCase = ['''CLIPEncoderLayer'''] def __init__( self :int , snake_case :CLIPConfig ): '''simple docstring''' super().__init__(snake_case ) A_ : int = CLIPVisionModel(config.vision_config ) A_ : List[str] = nn.Linear(config.vision_config.hidden_size , config.projection_dim , bias=snake_case ) A_ : Tuple = nn.Parameter(torch.ones(17 , config.projection_dim ) , requires_grad=snake_case ) A_ : str = nn.Parameter(torch.ones(3 , config.projection_dim ) , requires_grad=snake_case ) A_ : List[str] = nn.Parameter(torch.ones(17 ) , requires_grad=snake_case ) A_ : int = nn.Parameter(torch.ones(3 ) , requires_grad=snake_case ) @torch.no_grad() def SCREAMING_SNAKE_CASE ( self :Union[str, Any] , snake_case :Dict , snake_case :Any ): '''simple docstring''' A_ : List[Any] = self.vision_model(snake_case )[1] # pooled_output A_ : List[Any] = self.visual_projection(snake_case ) # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16 A_ : Optional[Any] = cosine_distance(snake_case , self.special_care_embeds ).cpu().float().numpy() A_ : Tuple = cosine_distance(snake_case , self.concept_embeds ).cpu().float().numpy() A_ : Union[str, Any] = [] A_ : Any = image_embeds.shape[0] for i in range(snake_case ): A_ : Optional[int] = {"special_scores": {}, "special_care": [], "concept_scores": {}, "bad_concepts": []} # increase this value to create a stronger `nfsw` filter # at the cost of increasing the possibility of filtering benign images A_ : Optional[Any] = 0.0 for concept_idx in range(len(special_cos_dist[0] ) ): A_ : Optional[Any] = special_cos_dist[i][concept_idx] A_ : Tuple = self.special_care_embeds_weights[concept_idx].item() A_ : Union[str, Any] = round(concept_cos - concept_threshold + adjustment , 3 ) if result_img["special_scores"][concept_idx] > 0: result_img["special_care"].append({concept_idx, result_img["special_scores"][concept_idx]} ) A_ : Any = 0.01 for concept_idx in range(len(cos_dist[0] ) ): A_ : Tuple = cos_dist[i][concept_idx] A_ : Tuple = self.concept_embeds_weights[concept_idx].item() A_ : Tuple = round(concept_cos - concept_threshold + adjustment , 3 ) if result_img["concept_scores"][concept_idx] > 0: result_img["bad_concepts"].append(snake_case ) result.append(snake_case ) A_ : Any = [len(res["bad_concepts"] ) > 0 for res in result] return images, has_nsfw_concepts @torch.no_grad() def SCREAMING_SNAKE_CASE ( self :Union[str, Any] , snake_case :torch.FloatTensor , snake_case :torch.FloatTensor ): '''simple docstring''' A_ : List[str] = self.vision_model(snake_case )[1] # pooled_output A_ : int = self.visual_projection(snake_case ) A_ : Tuple = cosine_distance(snake_case , self.special_care_embeds ) A_ : Tuple = cosine_distance(snake_case , self.concept_embeds ) # increase this value to create a stronger `nsfw` filter # at the cost of increasing the possibility of filtering benign images A_ : Optional[Any] = 0.0 A_ : Tuple = special_cos_dist - self.special_care_embeds_weights + adjustment # special_scores = special_scores.round(decimals=3) A_ : Optional[Any] = torch.any(special_scores > 0 , dim=1 ) A_ : Optional[Any] = special_care * 0.01 A_ : Optional[int] = special_adjustment.unsqueeze(1 ).expand(-1 , cos_dist.shape[1] ) A_ : Union[str, Any] = (cos_dist - self.concept_embeds_weights) + special_adjustment # concept_scores = concept_scores.round(decimals=3) A_ : Union[str, Any] = torch.any(concept_scores > 0 , dim=1 ) return images, has_nsfw_concepts
300
0
import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_albert import AlbertTokenizer else: SCREAMING_SNAKE_CASE : str = None SCREAMING_SNAKE_CASE : str = logging.get_logger(__name__) SCREAMING_SNAKE_CASE : List[Any] = {"vocab_file": "spiece.model", "tokenizer_file": "tokenizer.json"} SCREAMING_SNAKE_CASE : Optional[int] = { "vocab_file": { "albert-base-v1": "https://huggingface.co/albert-base-v1/resolve/main/spiece.model", "albert-large-v1": "https://huggingface.co/albert-large-v1/resolve/main/spiece.model", "albert-xlarge-v1": "https://huggingface.co/albert-xlarge-v1/resolve/main/spiece.model", "albert-xxlarge-v1": "https://huggingface.co/albert-xxlarge-v1/resolve/main/spiece.model", "albert-base-v2": "https://huggingface.co/albert-base-v2/resolve/main/spiece.model", "albert-large-v2": "https://huggingface.co/albert-large-v2/resolve/main/spiece.model", "albert-xlarge-v2": "https://huggingface.co/albert-xlarge-v2/resolve/main/spiece.model", "albert-xxlarge-v2": "https://huggingface.co/albert-xxlarge-v2/resolve/main/spiece.model", }, "tokenizer_file": { "albert-base-v1": "https://huggingface.co/albert-base-v1/resolve/main/tokenizer.json", "albert-large-v1": "https://huggingface.co/albert-large-v1/resolve/main/tokenizer.json", "albert-xlarge-v1": "https://huggingface.co/albert-xlarge-v1/resolve/main/tokenizer.json", "albert-xxlarge-v1": "https://huggingface.co/albert-xxlarge-v1/resolve/main/tokenizer.json", "albert-base-v2": "https://huggingface.co/albert-base-v2/resolve/main/tokenizer.json", "albert-large-v2": "https://huggingface.co/albert-large-v2/resolve/main/tokenizer.json", "albert-xlarge-v2": "https://huggingface.co/albert-xlarge-v2/resolve/main/tokenizer.json", "albert-xxlarge-v2": "https://huggingface.co/albert-xxlarge-v2/resolve/main/tokenizer.json", }, } SCREAMING_SNAKE_CASE : Optional[int] = { "albert-base-v1": 512, "albert-large-v1": 512, "albert-xlarge-v1": 512, "albert-xxlarge-v1": 512, "albert-base-v2": 512, "albert-large-v2": 512, "albert-xlarge-v2": 512, "albert-xxlarge-v2": 512, } SCREAMING_SNAKE_CASE : Union[str, Any] = "▁" class _lowerCamelCase( _a ): lowercase_ : Union[str, Any] = VOCAB_FILES_NAMES lowercase_ : Dict = PRETRAINED_VOCAB_FILES_MAP lowercase_ : Optional[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase_ : List[Any] = AlbertTokenizer def __init__( self, lowerCamelCase=None, lowerCamelCase=None, lowerCamelCase=True, lowerCamelCase=True, lowerCamelCase=False, lowerCamelCase="[CLS]", lowerCamelCase="[SEP]", lowerCamelCase="<unk>", lowerCamelCase="[SEP]", lowerCamelCase="<pad>", lowerCamelCase="[CLS]", lowerCamelCase="[MASK]", **lowerCamelCase, ) -> Optional[Any]: """simple docstring""" _lowercase : Optional[Any] = ( AddedToken(lowerCamelCase, lstrip=lowerCamelCase, rstrip=lowerCamelCase, normalized=lowerCamelCase) if isinstance(lowerCamelCase, lowerCamelCase) else mask_token ) super().__init__( lowerCamelCase, tokenizer_file=lowerCamelCase, do_lower_case=lowerCamelCase, remove_space=lowerCamelCase, keep_accents=lowerCamelCase, bos_token=lowerCamelCase, eos_token=lowerCamelCase, unk_token=lowerCamelCase, sep_token=lowerCamelCase, pad_token=lowerCamelCase, cls_token=lowerCamelCase, mask_token=lowerCamelCase, **lowerCamelCase, ) _lowercase : List[Any] = do_lower_case _lowercase : List[Any] = remove_space _lowercase : Optional[int] = keep_accents _lowercase : Optional[int] = vocab_file _lowercase : List[Any] = False if not self.vocab_file else True def UpperCamelCase ( self, lowerCamelCase, lowerCamelCase = None) -> List[int]: """simple docstring""" _lowercase : str = [self.sep_token_id] _lowercase : int = [self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def UpperCamelCase ( self, lowerCamelCase, lowerCamelCase = None) -> List[int]: """simple docstring""" _lowercase : Dict = [self.sep_token_id] _lowercase : str = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep) * [0] return len(cls + token_ids_a + sep) * [0] + len(token_ids_a + sep) * [1] def UpperCamelCase ( self, lowerCamelCase, lowerCamelCase = None) -> Tuple[str]: """simple docstring""" if not self.can_save_slow_tokenizer: raise ValueError( 'Your fast tokenizer does not have the necessary information to save the vocabulary for a slow ' 'tokenizer.') if not os.path.isdir(lowerCamelCase): logger.error(F'''Vocabulary path ({save_directory}) should be a directory''') return _lowercase : Optional[Any] = os.path.join( lowerCamelCase, (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file']) if os.path.abspath(self.vocab_file) != os.path.abspath(lowerCamelCase): copyfile(self.vocab_file, lowerCamelCase) return (out_vocab_file,)
21
import argparse from pathlib import Path import requests import torch from PIL import Image from transformers import ( RobertaTokenizer, TrOCRConfig, TrOCRForCausalLM, TrOCRProcessor, VisionEncoderDecoderModel, ViTConfig, ViTImageProcessor, ViTModel, ) from transformers.utils import logging logging.set_verbosity_info() _lowerCAmelCase : List[str] = logging.get_logger(__name__) def __snake_case ( _lowerCAmelCase : Optional[int] , _lowerCAmelCase : Union[str, Any] ) -> Optional[int]: A_ : Tuple = [] 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 __snake_case ( _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : Union[str, Any] ) -> Dict: for i in range(encoder_config.num_hidden_layers ): # queries, keys and values (only weights, no biases) A_ : str = state_dict.pop(f"encoder.deit.blocks.{i}.attn.qkv.weight" ) A_ : List[Any] = in_proj_weight[ : encoder_config.hidden_size, : ] A_ : Optional[Any] = in_proj_weight[ encoder_config.hidden_size : encoder_config.hidden_size * 2, : ] A_ : Optional[Any] = in_proj_weight[ -encoder_config.hidden_size :, : ] def __snake_case ( _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : Tuple , _lowerCAmelCase : Dict ) -> Any: A_ : Dict = dct.pop(_lowerCAmelCase ) A_ : List[Any] = val def __snake_case ( _lowerCAmelCase : List[str] ) -> int: if "handwritten" in checkpoint_url: A_ : Any = "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: A_ : Any = "https://www.researchgate.net/profile/Dinh-Sang/publication/338099565/figure/fig8/AS:840413229350922@1577381536857/An-receipt-example-in-the-SROIE-2019-dataset_Q640.jpg" A_ : List[Any] = Image.open(requests.get(_lowerCAmelCase , stream=_lowerCAmelCase ).raw ).convert("RGB" ) return im @torch.no_grad() def __snake_case ( _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : Union[str, Any] ) -> List[Any]: A_ : Optional[Any] = ViTConfig(image_size=384 , qkv_bias=_lowerCAmelCase ) A_ : Tuple = TrOCRConfig() # size of the architecture if "base" in checkpoint_url: A_ : Tuple = 768 elif "large" in checkpoint_url: # use ViT-large encoder A_ : Optional[Any] = 1024 A_ : Union[str, Any] = 4096 A_ : Union[str, Any] = 24 A_ : List[Any] = 16 A_ : List[str] = 1024 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: A_ : Dict = False A_ : int = "relu" A_ : Optional[int] = 1024 A_ : Any = True A_ : List[Any] = False A_ : Optional[int] = False # load HuggingFace model A_ : Union[str, Any] = ViTModel(_lowerCAmelCase , add_pooling_layer=_lowerCAmelCase ) A_ : str = TrOCRForCausalLM(_lowerCAmelCase ) A_ : List[str] = VisionEncoderDecoderModel(encoder=_lowerCAmelCase , decoder=_lowerCAmelCase ) model.eval() # load state_dict of original model, rename some keys A_ : Optional[int] = torch.hub.load_state_dict_from_url(_lowerCAmelCase , map_location="cpu" , check_hash=_lowerCAmelCase )["model"] A_ : Dict = create_rename_keys(_lowerCAmelCase , _lowerCAmelCase ) for src, dest in rename_keys: rename_key(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) read_in_q_k_v(_lowerCAmelCase , _lowerCAmelCase ) # 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(): A_ : Dict = state_dict.pop(_lowerCAmelCase ) if key.startswith("decoder" ) and "output_projection" not in key: A_ : List[str] = val else: A_ : Optional[Any] = val # load state dict model.load_state_dict(_lowerCAmelCase ) # Check outputs on an image A_ : List[Any] = ViTImageProcessor(size=encoder_config.image_size ) A_ : Any = RobertaTokenizer.from_pretrained("roberta-large" ) A_ : Union[str, Any] = TrOCRProcessor(_lowerCAmelCase , _lowerCAmelCase ) A_ : List[str] = processor(images=prepare_img(_lowerCAmelCase ) , return_tensors="pt" ).pixel_values # verify logits A_ : Union[str, Any] = torch.tensor([[model.config.decoder.decoder_start_token_id]] ) A_ : Optional[int] = model(pixel_values=_lowerCAmelCase , decoder_input_ids=_lowerCAmelCase ) A_ : Tuple = outputs.logits A_ : Union[str, Any] = torch.Size([1, 1, 50265] ) if "trocr-base-handwritten" in checkpoint_url: A_ : Union[str, Any] = torch.tensor( [-1.45_02, -4.66_83, -0.53_47, -2.92_91, 9.14_35, -3.05_71, 8.97_64, 1.75_60, 8.73_58, -1.53_11] ) elif "trocr-large-handwritten" in checkpoint_url: A_ : str = torch.tensor( [-2.64_37, -1.31_29, -2.25_96, -5.34_55, 6.35_39, 1.76_04, 5.49_91, 1.47_02, 5.61_13, 2.01_70] ) elif "trocr-base-printed" in checkpoint_url: A_ : Optional[Any] = torch.tensor( [-5.68_16, -5.83_88, 1.13_98, -6.90_34, 6.85_05, -2.43_93, 1.22_84, -1.02_32, -1.96_61, -3.92_10] ) elif "trocr-large-printed" in checkpoint_url: A_ : Optional[int] = torch.tensor( [-6.01_62, -7.09_59, 4.41_55, -5.10_63, 7.04_68, -3.16_31, 2.64_66, -0.30_81, -0.81_06, -1.75_35] ) if "stage1" not in checkpoint_url: assert logits.shape == expected_shape, "Shape of logits not as expected" assert torch.allclose(logits[0, 0, :10] , _lowerCAmelCase , atol=1e-3 ), "First elements of logits not as expected" Path(_lowerCAmelCase ).mkdir(exist_ok=_lowerCAmelCase ) print(f"Saving model to {pytorch_dump_folder_path}" ) model.save_pretrained(_lowerCAmelCase ) print(f"Saving processor to {pytorch_dump_folder_path}" ) processor.save_pretrained(_lowerCAmelCase ) if __name__ == "__main__": _lowerCAmelCase : Dict = argparse.ArgumentParser() parser.add_argument( '''--checkpoint_url''', default='''https://layoutlm.blob.core.windows.net/trocr/model_zoo/fairseq/trocr-base-handwritten.pt''', type=str, help='''URL to the original PyTorch checkpoint (.pth file).''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the folder to output PyTorch model.''' ) _lowerCAmelCase : List[str] = parser.parse_args() convert_tr_ocr_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)
300
0
'''simple docstring''' import os from shutil import copyfile from typing import List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging __SCREAMING_SNAKE_CASE :Union[str, Any] = logging.get_logger(__name__) __SCREAMING_SNAKE_CASE :Tuple = {'''vocab_file''': '''sentencepiece.model'''} __SCREAMING_SNAKE_CASE :Union[str, Any] = { '''vocab_file''': { '''google/rembert''': '''https://huggingface.co/google/rembert/resolve/main/sentencepiece.model''', }, } __SCREAMING_SNAKE_CASE :List[Any] = { '''google/rembert''': 256, } class A_ ( lowerCAmelCase_ ): _lowerCamelCase : Optional[Any] = VOCAB_FILES_NAMES _lowerCamelCase : Union[str, Any] = PRETRAINED_VOCAB_FILES_MAP _lowerCamelCase : Any = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES def __init__( self : List[str] , snake_case_ : Tuple , snake_case_ : Any=False , snake_case_ : Tuple=True , snake_case_ : Tuple=True , snake_case_ : int="[CLS]" , snake_case_ : Optional[Any]="[SEP]" , snake_case_ : int="[UNK]" , snake_case_ : Optional[int]="[SEP]" , snake_case_ : Any="[PAD]" , snake_case_ : Any="[CLS]" , snake_case_ : int="[MASK]" , **snake_case_ : Optional[int] , ): super().__init__( do_lower_case=snake_case_ , remove_space=snake_case_ , keep_accents=snake_case_ , bos_token=snake_case_ , eos_token=snake_case_ , unk_token=snake_case_ , sep_token=snake_case_ , pad_token=snake_case_ , cls_token=snake_case_ , mask_token=snake_case_ , **snake_case_ , ) _UpperCAmelCase = do_lower_case _UpperCAmelCase = remove_space _UpperCAmelCase = keep_accents _UpperCAmelCase = vocab_file _UpperCAmelCase = spm.SentencePieceProcessor() self.sp_model.Load(snake_case_ ) @property def lowercase ( self : Union[str, Any] ): return len(self.sp_model ) def lowercase ( self : Union[str, Any] ): _UpperCAmelCase = {self.convert_ids_to_tokens(snake_case_ ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self : Tuple ): _UpperCAmelCase = self.__dict__.copy() _UpperCAmelCase = None return state def __setstate__( self : Tuple , snake_case_ : Dict ): _UpperCAmelCase = d _UpperCAmelCase = spm.SentencePieceProcessor() self.sp_model.Load(self.vocab_file ) def lowercase ( self : Union[str, Any] , snake_case_ : Union[str, Any] , snake_case_ : str=False ): _UpperCAmelCase = self.sp_model.EncodeAsPieces(snake_case_ ) return pieces def lowercase ( self : Union[str, Any] , snake_case_ : Optional[Any] ): return self.sp_model.PieceToId(snake_case_ ) def lowercase ( self : Dict , snake_case_ : Tuple ): return self.sp_model.IdToPiece(snake_case_ ) def lowercase ( self : Dict , snake_case_ : Union[str, Any] ): _UpperCAmelCase = self.sp_model.decode_pieces(snake_case_ ) return out_string def lowercase ( self : Union[str, Any] , snake_case_ : List[int] , snake_case_ : Optional[List[int]] = None ): _UpperCAmelCase = [self.sep_token_id] _UpperCAmelCase = [self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def lowercase ( self : Optional[Any] , snake_case_ : List[int] , snake_case_ : Optional[List[int]] = None , snake_case_ : bool = False ): if already_has_special_tokens: if token_ids_a is not None: raise ValueError( "You should not supply a second sequence if the provided sequence of " "ids is already formatted with special tokens for the model." ) return [1 if x in [self.sep_token_id, self.cls_token_id] else 0 for x in token_ids_a] if token_ids_a is not None: return [1] + ([0] * len(snake_case_ )) + [1] + ([0] * len(snake_case_ )) + [1] return [1] + ([0] * len(snake_case_ )) + [1] def lowercase ( self : Tuple , snake_case_ : List[int] , snake_case_ : Optional[List[int]] = None ): _UpperCAmelCase = [self.sep_token_id] _UpperCAmelCase = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def lowercase ( self : Tuple , snake_case_ : str , snake_case_ : Optional[str] = None ): if not os.path.isdir(snake_case_ ): logger.error("Vocabulary path ({}) should be a directory".format(snake_case_ ) ) return _UpperCAmelCase = os.path.join( snake_case_ , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(snake_case_ ): copyfile(self.vocab_file , snake_case_ ) return (out_vocab_file,)
22
# DISCLAIMER: This file is strongly influenced by https://github.com/yang-song/score_sde_pytorch import math from dataclasses import dataclass from typing import Optional, Tuple, Union import torch from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput, randn_tensor from .scheduling_utils import SchedulerMixin, SchedulerOutput @dataclass class __magic_name__ ( lowerCamelCase__ ): """simple docstring""" __UpperCamelCase = 42 __UpperCamelCase = 42 class __magic_name__ ( lowerCamelCase__ , lowerCamelCase__ ): """simple docstring""" __UpperCamelCase = 1 @register_to_config def __init__( self :Union[str, Any] , snake_case :int = 2_000 , snake_case :float = 0.15 , snake_case :float = 0.01 , snake_case :float = 1348.0 , snake_case :float = 1e-5 , snake_case :int = 1 , ): '''simple docstring''' A_ : Dict = sigma_max # setable values A_ : List[Any] = None self.set_sigmas(snake_case , snake_case , snake_case , snake_case ) def SCREAMING_SNAKE_CASE ( self :Any , snake_case :torch.FloatTensor , snake_case :Optional[int] = None ): '''simple docstring''' return sample def SCREAMING_SNAKE_CASE ( self :Optional[Any] , snake_case :int , snake_case :float = None , snake_case :Union[str, torch.device] = None ): '''simple docstring''' A_ : Optional[Any] = sampling_eps if sampling_eps is not None else self.config.sampling_eps A_ : Tuple = torch.linspace(1 , snake_case , snake_case , device=snake_case ) def SCREAMING_SNAKE_CASE ( self :Dict , snake_case :int , snake_case :float = None , snake_case :float = None , snake_case :float = None ): '''simple docstring''' A_ : Union[str, Any] = sigma_min if sigma_min is not None else self.config.sigma_min A_ : Any = sigma_max if sigma_max is not None else self.config.sigma_max A_ : Dict = sampling_eps if sampling_eps is not None else self.config.sampling_eps if self.timesteps is None: self.set_timesteps(snake_case , snake_case ) A_ : str = sigma_min * (sigma_max / sigma_min) ** (self.timesteps / sampling_eps) A_ : Any = torch.exp(torch.linspace(math.log(snake_case ) , math.log(snake_case ) , snake_case ) ) A_ : str = torch.tensor([sigma_min * (sigma_max / sigma_min) ** t for t in self.timesteps] ) def SCREAMING_SNAKE_CASE ( self :List[str] , snake_case :List[str] , snake_case :Dict ): '''simple docstring''' return torch.where( timesteps == 0 , torch.zeros_like(t.to(timesteps.device ) ) , self.discrete_sigmas[timesteps - 1].to(timesteps.device ) , ) def SCREAMING_SNAKE_CASE ( self :Union[str, Any] , snake_case :torch.FloatTensor , snake_case :int , snake_case :torch.FloatTensor , snake_case :Optional[torch.Generator] = None , snake_case :bool = True , ): '''simple docstring''' if self.timesteps is None: raise ValueError( "`self.timesteps` is not set, you need to run 'set_timesteps' after creating the scheduler" ) A_ : int = timestep * torch.ones( sample.shape[0] , device=sample.device ) # torch.repeat_interleave(timestep, sample.shape[0]) A_ : Optional[Any] = (timestep * (len(self.timesteps ) - 1)).long() # mps requires indices to be in the same device, so we use cpu as is the default with cuda A_ : Dict = timesteps.to(self.discrete_sigmas.device ) A_ : Optional[int] = self.discrete_sigmas[timesteps].to(sample.device ) A_ : int = self.get_adjacent_sigma(snake_case , snake_case ).to(sample.device ) A_ : Union[str, Any] = torch.zeros_like(snake_case ) A_ : Tuple = (sigma**2 - adjacent_sigma**2) ** 0.5 # equation 6 in the paper: the model_output modeled by the network is grad_x log pt(x) # also equation 47 shows the analog from SDE models to ancestral sampling methods A_ : Optional[int] = diffusion.flatten() while len(diffusion.shape ) < len(sample.shape ): A_ : Tuple = diffusion.unsqueeze(-1 ) A_ : Optional[Any] = drift - diffusion**2 * model_output # equation 6: sample noise for the diffusion term of A_ : List[Any] = randn_tensor( sample.shape , layout=sample.layout , generator=snake_case , device=sample.device , dtype=sample.dtype ) A_ : List[Any] = sample - drift # subtract because `dt` is a small negative timestep # TODO is the variable diffusion the correct scaling term for the noise? A_ : Any = prev_sample_mean + diffusion * noise # add impact of diffusion field g if not return_dict: return (prev_sample, prev_sample_mean) return SdeVeOutput(prev_sample=snake_case , prev_sample_mean=snake_case ) def SCREAMING_SNAKE_CASE ( self :Tuple , snake_case :torch.FloatTensor , snake_case :torch.FloatTensor , snake_case :Optional[torch.Generator] = None , snake_case :bool = True , ): '''simple docstring''' if self.timesteps is None: raise ValueError( "`self.timesteps` is not set, you need to run 'set_timesteps' after creating the scheduler" ) # For small batch sizes, the paper "suggest replacing norm(z) with sqrt(d), where d is the dim. of z" # sample noise for correction A_ : Dict = randn_tensor(sample.shape , layout=sample.layout , generator=snake_case ).to(sample.device ) # compute step size from the model_output, the noise, and the snr A_ : int = torch.norm(model_output.reshape(model_output.shape[0] , -1 ) , dim=-1 ).mean() A_ : List[Any] = torch.norm(noise.reshape(noise.shape[0] , -1 ) , dim=-1 ).mean() A_ : Dict = (self.config.snr * noise_norm / grad_norm) ** 2 * 2 A_ : Dict = step_size * torch.ones(sample.shape[0] ).to(sample.device ) # self.repeat_scalar(step_size, sample.shape[0]) # compute corrected sample: model_output term and noise term A_ : int = step_size.flatten() while len(step_size.shape ) < len(sample.shape ): A_ : str = step_size.unsqueeze(-1 ) A_ : Optional[Any] = sample + step_size * model_output A_ : Tuple = prev_sample_mean + ((step_size * 2) ** 0.5) * noise if not return_dict: return (prev_sample,) return SchedulerOutput(prev_sample=snake_case ) def SCREAMING_SNAKE_CASE ( self :Tuple , snake_case :torch.FloatTensor , snake_case :torch.FloatTensor , snake_case :torch.FloatTensor , ): '''simple docstring''' A_ : Union[str, Any] = timesteps.to(original_samples.device ) A_ : List[Any] = self.discrete_sigmas.to(original_samples.device )[timesteps] A_ : List[Any] = ( noise * sigmas[:, None, None, None] if noise is not None else torch.randn_like(snake_case ) * sigmas[:, None, None, None] ) A_ : Optional[int] = noise + original_samples return noisy_samples def __len__( self :Union[str, Any] ): '''simple docstring''' return self.config.num_train_timesteps
300
0
'''simple docstring''' from __future__ import annotations def snake_case_ ( _lowerCAmelCase : list[int | float] , _lowerCAmelCase : int , _lowerCAmelCase : int ) -> int | float: if len(_lowerCAmelCase ) == 0: raise ValueError('''find_max() arg is an empty sequence''' ) if ( left >= len(_lowerCAmelCase ) or left < -len(_lowerCAmelCase ) or right >= len(_lowerCAmelCase ) or right < -len(_lowerCAmelCase ) ): raise IndexError('''list index out of range''' ) if left == right: return nums[left] UpperCAmelCase : List[Any] = (left + right) >> 1 # the middle UpperCAmelCase : Optional[Any] = find_max(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) # find max in range[left, mid] UpperCAmelCase : Dict = find_max(_lowerCAmelCase , mid + 1 , _lowerCAmelCase ) # find max in range[mid + 1, right] return left_max if left_max >= right_max else right_max if __name__ == "__main__": import doctest doctest.testmod(verbose=True)
23
from __future__ import annotations from decimal import Decimal from math import * # noqa: F403 from sympy import diff def __snake_case ( _lowerCAmelCase : str , _lowerCAmelCase : float | Decimal , _lowerCAmelCase : float = 10**-10 ) -> float: A_ : Dict = a while True: A_ : Union[str, Any] = Decimal(_lowerCAmelCase ) - ( Decimal(eval(_lowerCAmelCase ) ) / Decimal(eval(str(diff(_lowerCAmelCase ) ) ) ) # noqa: S307 ) # This number dictates the accuracy of the answer if abs(eval(_lowerCAmelCase ) ) < precision: # noqa: S307 return float(_lowerCAmelCase ) # Let's Execute if __name__ == "__main__": # Find root of trigonometric function # Find value of pi print(F'''The root of sin(x) = 0 is {newton_raphson("sin(x)", 2)}''') # Find root of polynomial print(F'''The root of x**2 - 5*x + 2 = 0 is {newton_raphson("x**2 - 5*x + 2", 0.4)}''') # Find Square Root of 5 print(F'''The root of log(x) - 1 = 0 is {newton_raphson("log(x) - 1", 2)}''') # Exponential Roots print(F'''The root of exp(x) - 1 = 0 is {newton_raphson("exp(x) - 1", 0)}''')
300
0
from dataclasses import dataclass, field from typing import Tuple from ..utils import cached_property, is_torch_available, is_torch_tpu_available, logging, requires_backends from .benchmark_args_utils import BenchmarkArguments if is_torch_available(): import torch if is_torch_tpu_available(check_device=False): import torch_xla.core.xla_model as xm snake_case_ = logging.get_logger(__name__) @dataclass class SCREAMING_SNAKE_CASE__ ( _UpperCAmelCase ): A_ : List[str] = [ 'no_inference', 'no_cuda', 'no_tpu', 'no_speed', 'no_memory', 'no_env_print', 'no_multi_process', ] def __init__(self : List[Any] , **a__ : Dict ): """simple docstring""" for deprecated_arg in self.deprecated_args: if deprecated_arg in kwargs: __snake_case = deprecated_arg[3:] setattr(self , a__ , not kwargs.pop(a__ ) ) logger.warning( f"""{deprecated_arg} is depreciated. Please use --no_{positive_arg} or""" f""" {positive_arg}={kwargs[positive_arg]}""" ) __snake_case = kwargs.pop('''torchscript''' , self.torchscript ) __snake_case = kwargs.pop('''torch_xla_tpu_print_metrics''' , self.torch_xla_tpu_print_metrics ) __snake_case = kwargs.pop('''fp16_opt_level''' , self.fpaa_opt_level ) super().__init__(**a__ ) A_ : bool = field(default=_UpperCAmelCase , metadata={'help': 'Trace the models using torchscript'} ) A_ : bool = field(default=_UpperCAmelCase , metadata={'help': 'Print Xla/PyTorch tpu metrics'} ) A_ : str = field( default='O1' , metadata={ 'help': ( 'For fp16: Apex AMP optimization level selected in [\'O0\', \'O1\', \'O2\', and \'O3\']. ' 'See details at https://nvidia.github.io/apex/amp.html' ) } , ) @cached_property def a (self : Tuple ): """simple docstring""" requires_backends(self , ['''torch'''] ) logger.info('''PyTorch: setting up devices''' ) if not self.cuda: __snake_case = torch.device('''cpu''' ) __snake_case = 0 elif is_torch_tpu_available(): __snake_case = xm.xla_device() __snake_case = 0 else: __snake_case = torch.device('''cuda''' if torch.cuda.is_available() else '''cpu''' ) __snake_case = torch.cuda.device_count() return device, n_gpu @property def a (self : Tuple ): """simple docstring""" return is_torch_tpu_available() and self.tpu @property def a (self : Tuple ): """simple docstring""" requires_backends(self , ['''torch'''] ) # TODO(PVP): currently only single GPU is supported return torch.cuda.current_device() @property def a (self : Any ): """simple docstring""" requires_backends(self , ['''torch'''] ) return self._setup_devices[0] @property def a (self : Optional[Any] ): """simple docstring""" requires_backends(self , ['''torch'''] ) return self._setup_devices[1] @property def a (self : List[str] ): """simple docstring""" return self.n_gpu > 0
24
import sacrebleu as scb from packaging import version from sacrebleu import TER import datasets _lowerCAmelCase : List[Any] = '''\ @inproceedings{snover-etal-2006-study, title = "A Study of Translation Edit Rate with Targeted Human Annotation", author = "Snover, Matthew and Dorr, Bonnie and Schwartz, Rich and Micciulla, Linnea and Makhoul, John", booktitle = "Proceedings of the 7th Conference of the Association for Machine Translation in the Americas: Technical Papers", month = aug # " 8-12", year = "2006", address = "Cambridge, Massachusetts, USA", publisher = "Association for Machine Translation in the Americas", url = "https://aclanthology.org/2006.amta-papers.25", pages = "223--231", } @inproceedings{post-2018-call, title = "A Call for Clarity in Reporting {BLEU} Scores", author = "Post, Matt", booktitle = "Proceedings of the Third Conference on Machine Translation: Research Papers", month = oct, year = "2018", address = "Belgium, Brussels", publisher = "Association for Computational Linguistics", url = "https://www.aclweb.org/anthology/W18-6319", pages = "186--191", } ''' _lowerCAmelCase : Union[str, Any] = '''\ TER (Translation Edit Rate, also called Translation Error Rate) is a metric to quantify the edit operations that a hypothesis requires to match a reference translation. We use the implementation that is already present in sacrebleu (https://github.com/mjpost/sacreBLEU#ter), which in turn is inspired by the TERCOM implementation, which can be found here: https://github.com/jhclark/tercom. The implementation here is slightly different from sacrebleu in terms of the required input format. The length of the references and hypotheses lists need to be the same, so you may need to transpose your references compared to sacrebleu\'s required input format. See https://github.com/huggingface/datasets/issues/3154#issuecomment-950746534 See the README.md file at https://github.com/mjpost/sacreBLEU#ter for more information. ''' _lowerCAmelCase : Optional[Any] = ''' Produces TER scores alongside the number of edits and reference length. Args: predictions (list of str): The system stream (a sequence of segments). references (list of list of str): A list of one or more reference streams (each a sequence of segments). normalized (boolean): If `True`, applies basic tokenization and normalization to sentences. Defaults to `False`. ignore_punct (boolean): If `True`, applies basic tokenization and normalization to sentences. Defaults to `False`. support_zh_ja_chars (boolean): If `True`, tokenization/normalization supports processing of Chinese characters, as well as Japanese Kanji, Hiragana, Katakana, and Phonetic Extensions of Katakana. Only applies if `normalized = True`. Defaults to `False`. case_sensitive (boolean): If `False`, makes all predictions and references lowercase to ignore differences in case. Defaults to `False`. Returns: \'score\' (float): TER score (num_edits / sum_ref_lengths * 100) \'num_edits\' (int): The cumulative number of edits \'ref_length\' (float): The cumulative average reference length Examples: Example 1: >>> predictions = ["does this sentence match??", ... "what about this sentence?", ... "What did the TER metric user say to the developer?"] >>> references = [["does this sentence match", "does this sentence match!?!"], ... ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"], ... ["Your jokes are...", "...TERrible"]] >>> ter = datasets.load_metric("ter") >>> results = ter.compute(predictions=predictions, ... references=references, ... case_sensitive=True) >>> print(results) {\'score\': 150.0, \'num_edits\': 15, \'ref_length\': 10.0} Example 2: >>> predictions = ["does this sentence match??", ... "what about this sentence?"] >>> references = [["does this sentence match", "does this sentence match!?!"], ... ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"]] >>> ter = datasets.load_metric("ter") >>> results = ter.compute(predictions=predictions, ... references=references, ... case_sensitive=True) >>> print(results) {\'score\': 62.5, \'num_edits\': 5, \'ref_length\': 8.0} Example 3: >>> predictions = ["does this sentence match??", ... "what about this sentence?"] >>> references = [["does this sentence match", "does this sentence match!?!"], ... ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"]] >>> ter = datasets.load_metric("ter") >>> results = ter.compute(predictions=predictions, ... references=references, ... normalized=True, ... case_sensitive=True) >>> print(results) {\'score\': 57.14285714285714, \'num_edits\': 6, \'ref_length\': 10.5} Example 4: >>> predictions = ["does this sentence match??", ... "what about this sentence?"] >>> references = [["does this sentence match", "does this sentence match!?!"], ... ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"]] >>> ter = datasets.load_metric("ter") >>> results = ter.compute(predictions=predictions, ... references=references, ... ignore_punct=True, ... case_sensitive=False) >>> print(results) {\'score\': 0.0, \'num_edits\': 0, \'ref_length\': 8.0} Example 5: >>> predictions = ["does this sentence match??", ... "what about this sentence?", ... "What did the TER metric user say to the developer?"] >>> references = [["does this sentence match", "does this sentence match!?!"], ... ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"], ... ["Your jokes are...", "...TERrible"]] >>> ter = datasets.load_metric("ter") >>> results = ter.compute(predictions=predictions, ... references=references, ... ignore_punct=True, ... case_sensitive=False) >>> print(results) {\'score\': 100.0, \'num_edits\': 10, \'ref_length\': 10.0} ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __magic_name__ ( datasets.Metric ): """simple docstring""" def SCREAMING_SNAKE_CASE ( self :Dict ): '''simple docstring''' if version.parse(scb.__version__ ) < version.parse("1.4.12" ): raise ImportWarning( "To use `sacrebleu`, the module `sacrebleu>=1.4.12` is required, and the current version of `sacrebleu` doesn't match this condition.\n" "You can install it with `pip install \"sacrebleu>=1.4.12\"`." ) return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , homepage="http://www.cs.umd.edu/~snover/tercom/" , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "predictions": datasets.Value("string" , id="sequence" ), "references": datasets.Sequence(datasets.Value("string" , id="sequence" ) , id="references" ), } ) , codebase_urls=["https://github.com/mjpost/sacreBLEU#ter"] , reference_urls=[ "https://github.com/jhclark/tercom", ] , ) def SCREAMING_SNAKE_CASE ( self :Union[str, Any] , snake_case :Optional[int] , snake_case :List[Any] , snake_case :bool = False , snake_case :bool = False , snake_case :bool = False , snake_case :bool = False , ): '''simple docstring''' A_ : List[str] = len(references[0] ) if any(len(snake_case ) != references_per_prediction for refs in references ): raise ValueError("Sacrebleu requires the same number of references for each prediction" ) A_ : int = [[refs[i] for refs in references] for i in range(snake_case )] A_ : Optional[Any] = TER( normalized=snake_case , no_punct=snake_case , asian_support=snake_case , case_sensitive=snake_case , ) A_ : List[Any] = sb_ter.corpus_score(snake_case , snake_case ) return {"score": output.score, "num_edits": output.num_edits, "ref_length": output.ref_length}
300
0
"""simple docstring""" import unittest import numpy as np import timeout_decorator # noqa from transformers import BlenderbotConfig, is_flax_available from transformers.testing_utils import jax_device, require_flax, slow from ...generation.test_flax_utils import FlaxGenerationTesterMixin from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor if is_flax_available(): import os # The slow tests are often failing with OOM error on GPU # This makes JAX allocate exactly what is needed on demand, and deallocate memory that is no longer needed # but will be slower as stated here https://jax.readthedocs.io/en/latest/gpu_memory_allocation.html UpperCAmelCase__ : Union[str, Any] = 'platform' import jax import jax.numpy as jnp from transformers import BlenderbotTokenizer from transformers.models.blenderbot.modeling_flax_blenderbot import ( FlaxBlenderbotForConditionalGeneration, FlaxBlenderbotModel, shift_tokens_right, ) def lowercase_ ( _snake_case ,_snake_case ,_snake_case=None ,_snake_case=None ,_snake_case=None ,_snake_case=None ,_snake_case=None ,_snake_case=None ,): if attention_mask is None: SCREAMING_SNAKE_CASE__ : str = np.where(input_ids != config.pad_token_id ,1 ,0 ) if decoder_attention_mask is None: SCREAMING_SNAKE_CASE__ : List[str] = np.where(decoder_input_ids != config.pad_token_id ,1 ,0 ) if head_mask is None: SCREAMING_SNAKE_CASE__ : Union[str, Any] = np.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: SCREAMING_SNAKE_CASE__ : Tuple = np.ones((config.decoder_layers, config.decoder_attention_heads) ) if cross_attn_head_mask is None: SCREAMING_SNAKE_CASE__ : Any = np.ones((config.decoder_layers, config.decoder_attention_heads) ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": attention_mask, } class lowerCAmelCase_ : """simple docstring""" def __init__(self , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__=13 , SCREAMING_SNAKE_CASE__=7 , SCREAMING_SNAKE_CASE__=True , SCREAMING_SNAKE_CASE__=False , SCREAMING_SNAKE_CASE__=99 , SCREAMING_SNAKE_CASE__=16 , SCREAMING_SNAKE_CASE__=2 , SCREAMING_SNAKE_CASE__=4 , SCREAMING_SNAKE_CASE__=4 , SCREAMING_SNAKE_CASE__="gelu" , SCREAMING_SNAKE_CASE__=0.1 , SCREAMING_SNAKE_CASE__=0.1 , SCREAMING_SNAKE_CASE__=32 , SCREAMING_SNAKE_CASE__=2 , SCREAMING_SNAKE_CASE__=1 , SCREAMING_SNAKE_CASE__=0 , SCREAMING_SNAKE_CASE__=0.02 , ) -> Any: """simple docstring""" SCREAMING_SNAKE_CASE__ : Union[str, Any] = parent SCREAMING_SNAKE_CASE__ : Tuple = batch_size SCREAMING_SNAKE_CASE__ : Dict = seq_length SCREAMING_SNAKE_CASE__ : List[str] = is_training SCREAMING_SNAKE_CASE__ : List[str] = use_labels SCREAMING_SNAKE_CASE__ : Any = vocab_size SCREAMING_SNAKE_CASE__ : Union[str, Any] = hidden_size SCREAMING_SNAKE_CASE__ : Any = num_hidden_layers SCREAMING_SNAKE_CASE__ : int = num_attention_heads SCREAMING_SNAKE_CASE__ : List[str] = intermediate_size SCREAMING_SNAKE_CASE__ : str = hidden_act SCREAMING_SNAKE_CASE__ : List[Any] = hidden_dropout_prob SCREAMING_SNAKE_CASE__ : Optional[int] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE__ : Tuple = max_position_embeddings SCREAMING_SNAKE_CASE__ : List[Any] = eos_token_id SCREAMING_SNAKE_CASE__ : Optional[Any] = pad_token_id SCREAMING_SNAKE_CASE__ : Optional[Any] = bos_token_id SCREAMING_SNAKE_CASE__ : Any = initializer_range def __magic_name__ (self ) -> List[str]: """simple docstring""" SCREAMING_SNAKE_CASE__ : Optional[int] = np.clip(ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size ) , 3 , self.vocab_size ) SCREAMING_SNAKE_CASE__ : Union[str, Any] = np.concatenate((input_ids, 2 * np.ones((self.batch_size, 1) , dtype=np.intaa )) , -1 ) SCREAMING_SNAKE_CASE__ : Dict = shift_tokens_right(SCREAMING_SNAKE_CASE__ , 1 , 2 ) SCREAMING_SNAKE_CASE__ : Optional[int] = BlenderbotConfig( vocab_size=self.vocab_size , d_model=self.hidden_size , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , eos_token_id=self.eos_token_id , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , initializer_range=self.initializer_range , use_cache=SCREAMING_SNAKE_CASE__ , ) SCREAMING_SNAKE_CASE__ : Dict = prepare_blenderbot_inputs_dict(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) return config, inputs_dict def __magic_name__ (self ) -> int: """simple docstring""" SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ : List[str] = self.prepare_config_and_inputs() return config, inputs_dict def __magic_name__ (self , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Optional[Any]: """simple docstring""" SCREAMING_SNAKE_CASE__ : Tuple = 20 SCREAMING_SNAKE_CASE__ : int = model_class_name(SCREAMING_SNAKE_CASE__ ) SCREAMING_SNAKE_CASE__ : int = model.encode(inputs_dict["""input_ids"""] ) SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ : Tuple = ( inputs_dict["""decoder_input_ids"""], inputs_dict["""decoder_attention_mask"""], ) SCREAMING_SNAKE_CASE__ : Tuple = model.init_cache(decoder_input_ids.shape[0] , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) SCREAMING_SNAKE_CASE__ : str = jnp.ones((decoder_input_ids.shape[0], max_decoder_length) , dtype="""i4""" ) SCREAMING_SNAKE_CASE__ : Dict = jnp.broadcast_to( jnp.arange(decoder_input_ids.shape[-1] - 1 )[None, :] , (decoder_input_ids.shape[0], decoder_input_ids.shape[-1] - 1) , ) SCREAMING_SNAKE_CASE__ : Optional[int] = model.decode( decoder_input_ids[:, :-1] , SCREAMING_SNAKE_CASE__ , decoder_attention_mask=SCREAMING_SNAKE_CASE__ , past_key_values=SCREAMING_SNAKE_CASE__ , decoder_position_ids=SCREAMING_SNAKE_CASE__ , ) SCREAMING_SNAKE_CASE__ : Tuple = jnp.array(decoder_input_ids.shape[0] * [[decoder_input_ids.shape[-1] - 1]] , dtype="""i4""" ) SCREAMING_SNAKE_CASE__ : str = model.decode( decoder_input_ids[:, -1:] , SCREAMING_SNAKE_CASE__ , decoder_attention_mask=SCREAMING_SNAKE_CASE__ , past_key_values=outputs_cache.past_key_values , decoder_position_ids=SCREAMING_SNAKE_CASE__ , ) SCREAMING_SNAKE_CASE__ : Optional[int] = model.decode(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) SCREAMING_SNAKE_CASE__ : Optional[int] = np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1E-3 , msg=F'''Max diff is {diff}''' ) def __magic_name__ (self , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> Optional[int]: """simple docstring""" SCREAMING_SNAKE_CASE__ : Any = 20 SCREAMING_SNAKE_CASE__ : Optional[Any] = model_class_name(SCREAMING_SNAKE_CASE__ ) SCREAMING_SNAKE_CASE__ : Optional[Any] = model.encode(inputs_dict["""input_ids"""] ) SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ : Union[str, Any] = ( inputs_dict["""decoder_input_ids"""], inputs_dict["""decoder_attention_mask"""], ) SCREAMING_SNAKE_CASE__ : Tuple = jnp.concatenate( [ decoder_attention_mask, jnp.zeros((decoder_attention_mask.shape[0], max_decoder_length - decoder_attention_mask.shape[1]) ), ] , axis=-1 , ) SCREAMING_SNAKE_CASE__ : Optional[int] = model.init_cache(decoder_input_ids.shape[0] , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) SCREAMING_SNAKE_CASE__ : str = jnp.broadcast_to( jnp.arange(decoder_input_ids.shape[-1] - 1 )[None, :] , (decoder_input_ids.shape[0], decoder_input_ids.shape[-1] - 1) , ) SCREAMING_SNAKE_CASE__ : Optional[Any] = model.decode( decoder_input_ids[:, :-1] , SCREAMING_SNAKE_CASE__ , decoder_attention_mask=SCREAMING_SNAKE_CASE__ , past_key_values=SCREAMING_SNAKE_CASE__ , decoder_position_ids=SCREAMING_SNAKE_CASE__ , ) SCREAMING_SNAKE_CASE__ : Optional[int] = jnp.array(decoder_input_ids.shape[0] * [[decoder_input_ids.shape[-1] - 1]] , dtype="""i4""" ) SCREAMING_SNAKE_CASE__ : List[str] = model.decode( decoder_input_ids[:, -1:] , SCREAMING_SNAKE_CASE__ , past_key_values=outputs_cache.past_key_values , decoder_attention_mask=SCREAMING_SNAKE_CASE__ , decoder_position_ids=SCREAMING_SNAKE_CASE__ , ) SCREAMING_SNAKE_CASE__ : Optional[Any] = model.decode(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , decoder_attention_mask=SCREAMING_SNAKE_CASE__ ) SCREAMING_SNAKE_CASE__ : List[str] = np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1E-3 , msg=F'''Max diff is {diff}''' ) @require_flax class lowerCAmelCase_ (unittest.TestCase ): """simple docstring""" __UpperCamelCase : Dict = 99 def __magic_name__ (self ) -> Tuple: """simple docstring""" SCREAMING_SNAKE_CASE__ : Optional[int] = np.array( [ [71, 82, 18, 33, 46, 91, 2], [68, 34, 26, 58, 30, 82, 2], [5, 97, 17, 39, 94, 40, 2], [76, 83, 94, 25, 70, 78, 2], [87, 59, 41, 35, 48, 66, 2], [55, 13, 16, 58, 5, 2, 1], # note padding [64, 27, 31, 51, 12, 75, 2], [52, 64, 86, 17, 83, 39, 2], [48, 61, 9, 24, 71, 82, 2], [26, 1, 60, 48, 22, 13, 2], [21, 5, 62, 28, 14, 76, 2], [45, 98, 37, 86, 59, 48, 2], [70, 70, 50, 9, 28, 0, 2], ] , dtype=np.intaa , ) SCREAMING_SNAKE_CASE__ : Optional[Any] = input_ids.shape[0] SCREAMING_SNAKE_CASE__ : List[Any] = BlenderbotConfig( vocab_size=self.vocab_size , d_model=24 , encoder_layers=2 , decoder_layers=2 , encoder_attention_heads=2 , decoder_attention_heads=2 , encoder_ffn_dim=32 , decoder_ffn_dim=32 , max_position_embeddings=48 , eos_token_id=2 , pad_token_id=1 , bos_token_id=0 , ) return config, input_ids, batch_size def __magic_name__ (self ) -> Optional[int]: """simple docstring""" SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ : Optional[int] = self._get_config_and_data() SCREAMING_SNAKE_CASE__ : str = FlaxBlenderbotForConditionalGeneration(SCREAMING_SNAKE_CASE__ ) SCREAMING_SNAKE_CASE__ : Optional[int] = lm_model(input_ids=SCREAMING_SNAKE_CASE__ ) SCREAMING_SNAKE_CASE__ : Dict = (batch_size, input_ids.shape[1], config.vocab_size) self.assertEqual(outputs["""logits"""].shape , SCREAMING_SNAKE_CASE__ ) def __magic_name__ (self ) -> Tuple: """simple docstring""" SCREAMING_SNAKE_CASE__ : Tuple = BlenderbotConfig( vocab_size=self.vocab_size , d_model=14 , encoder_layers=2 , decoder_layers=2 , encoder_attention_heads=2 , decoder_attention_heads=2 , encoder_ffn_dim=8 , decoder_ffn_dim=8 , max_position_embeddings=48 , ) SCREAMING_SNAKE_CASE__ : Dict = FlaxBlenderbotForConditionalGeneration(SCREAMING_SNAKE_CASE__ ) SCREAMING_SNAKE_CASE__ : Optional[Any] = np.array([[71, 82, 18, 33, 46, 91, 2], [68, 34, 26, 58, 30, 2, 1]] , dtype=np.intaa ) SCREAMING_SNAKE_CASE__ : Optional[int] = np.array([[82, 71, 82, 18, 2], [58, 68, 2, 1, 1]] , dtype=np.intaa ) SCREAMING_SNAKE_CASE__ : int = lm_model(input_ids=SCREAMING_SNAKE_CASE__ , decoder_input_ids=SCREAMING_SNAKE_CASE__ ) SCREAMING_SNAKE_CASE__ : Optional[int] = (*summary.shape, config.vocab_size) self.assertEqual(outputs["""logits"""].shape , SCREAMING_SNAKE_CASE__ ) def __magic_name__ (self ) -> Optional[Any]: """simple docstring""" SCREAMING_SNAKE_CASE__ : Optional[Any] = np.array([[71, 82, 18, 33, 2, 1, 1], [68, 34, 26, 58, 30, 82, 2]] , dtype=np.intaa ) SCREAMING_SNAKE_CASE__ : List[Any] = shift_tokens_right(SCREAMING_SNAKE_CASE__ , 1 , 2 ) SCREAMING_SNAKE_CASE__ : List[Any] = np.equal(SCREAMING_SNAKE_CASE__ , 1 ).astype(np.floataa ).sum() SCREAMING_SNAKE_CASE__ : Optional[Any] = np.equal(SCREAMING_SNAKE_CASE__ , 1 ).astype(np.floataa ).sum() self.assertEqual(shifted.shape , input_ids.shape ) self.assertEqual(SCREAMING_SNAKE_CASE__ , n_pad_before - 1 ) self.assertTrue(np.equal(shifted[:, 0] , 2 ).all() ) @require_flax class lowerCAmelCase_ (a__ , unittest.TestCase , a__ ): """simple docstring""" __UpperCamelCase : Any = True __UpperCamelCase : Union[str, Any] = ( ( FlaxBlenderbotModel, FlaxBlenderbotForConditionalGeneration, ) if is_flax_available() else () ) __UpperCamelCase : Union[str, Any] = (FlaxBlenderbotForConditionalGeneration,) if is_flax_available() else () def __magic_name__ (self ) -> Dict: """simple docstring""" SCREAMING_SNAKE_CASE__ : Any = FlaxBlenderbotModelTester(self ) def __magic_name__ (self ) -> Tuple: """simple docstring""" SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ : Union[str, Any] = self.model_tester.prepare_config_and_inputs() for model_class in self.all_model_classes: self.model_tester.check_use_cache_forward(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) def __magic_name__ (self ) -> Union[str, Any]: """simple docstring""" SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ : List[Any] = self.model_tester.prepare_config_and_inputs() for model_class in self.all_model_classes: self.model_tester.check_use_cache_forward_with_attn_mask(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) def __magic_name__ (self ) -> int: """simple docstring""" SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ : Tuple = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): SCREAMING_SNAKE_CASE__ : str = self._prepare_for_class(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) SCREAMING_SNAKE_CASE__ : Union[str, Any] = model_class(SCREAMING_SNAKE_CASE__ ) @jax.jit def encode_jitted(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__=None , **SCREAMING_SNAKE_CASE__ ): return model.encode(input_ids=SCREAMING_SNAKE_CASE__ , attention_mask=SCREAMING_SNAKE_CASE__ ) with self.subTest("""JIT Enabled""" ): SCREAMING_SNAKE_CASE__ : Union[str, Any] = encode_jitted(**SCREAMING_SNAKE_CASE__ ).to_tuple() with self.subTest("""JIT Disabled""" ): with jax.disable_jit(): SCREAMING_SNAKE_CASE__ : Any = encode_jitted(**SCREAMING_SNAKE_CASE__ ).to_tuple() self.assertEqual(len(SCREAMING_SNAKE_CASE__ ) , len(SCREAMING_SNAKE_CASE__ ) ) for jitted_output, output in zip(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): self.assertEqual(jitted_output.shape , output.shape ) def __magic_name__ (self ) -> Any: """simple docstring""" SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ : int = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): SCREAMING_SNAKE_CASE__ : Optional[Any] = model_class(SCREAMING_SNAKE_CASE__ ) SCREAMING_SNAKE_CASE__ : Dict = model.encode(inputs_dict["""input_ids"""] , inputs_dict["""attention_mask"""] ) SCREAMING_SNAKE_CASE__ : Any = { """decoder_input_ids""": inputs_dict["""decoder_input_ids"""], """decoder_attention_mask""": inputs_dict["""decoder_attention_mask"""], """encoder_outputs""": encoder_outputs, } @jax.jit def decode_jitted(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): return model.decode( decoder_input_ids=SCREAMING_SNAKE_CASE__ , decoder_attention_mask=SCREAMING_SNAKE_CASE__ , encoder_outputs=SCREAMING_SNAKE_CASE__ , ) with self.subTest("""JIT Enabled""" ): SCREAMING_SNAKE_CASE__ : List[Any] = decode_jitted(**SCREAMING_SNAKE_CASE__ ).to_tuple() with self.subTest("""JIT Disabled""" ): with jax.disable_jit(): SCREAMING_SNAKE_CASE__ : Optional[int] = decode_jitted(**SCREAMING_SNAKE_CASE__ ).to_tuple() self.assertEqual(len(SCREAMING_SNAKE_CASE__ ) , len(SCREAMING_SNAKE_CASE__ ) ) for jitted_output, output in zip(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): self.assertEqual(jitted_output.shape , output.shape ) @slow def __magic_name__ (self ) -> Optional[int]: """simple docstring""" for model_class_name in self.all_model_classes: SCREAMING_SNAKE_CASE__ : Union[str, Any] = model_class_name.from_pretrained("""facebook/blenderbot-400M-distill""" ) # FlaxBlenderbotForSequenceClassification expects eos token in input_ids SCREAMING_SNAKE_CASE__ : Any = np.ones((1, 1) ) * model.config.eos_token_id SCREAMING_SNAKE_CASE__ : List[str] = model(SCREAMING_SNAKE_CASE__ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE__ ) @unittest.skipUnless(jax_device != """cpu""" , """3B test too slow on CPU.""" ) @slow def __magic_name__ (self ) -> Optional[int]: """simple docstring""" SCREAMING_SNAKE_CASE__ : Dict = {"""num_beams""": 1, """early_stopping""": True, """min_length""": 15, """max_length""": 25} SCREAMING_SNAKE_CASE__ : Any = {"""skip_special_tokens""": True, """clean_up_tokenization_spaces""": True} SCREAMING_SNAKE_CASE__ : Optional[int] = FlaxBlenderbotForConditionalGeneration.from_pretrained("""facebook/blenderbot-3B""" , from_pt=SCREAMING_SNAKE_CASE__ ) SCREAMING_SNAKE_CASE__ : List[Any] = BlenderbotTokenizer.from_pretrained("""facebook/blenderbot-3B""" ) SCREAMING_SNAKE_CASE__ : Union[str, Any] = ["""Sam"""] SCREAMING_SNAKE_CASE__ : Optional[int] = tokenizer(SCREAMING_SNAKE_CASE__ , return_tensors="""jax""" ) SCREAMING_SNAKE_CASE__ : Dict = model.generate(**SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) SCREAMING_SNAKE_CASE__ : int = """Sam is a great name. It means \"sun\" in Gaelic.""" SCREAMING_SNAKE_CASE__ : List[Any] = tokenizer.batch_decode(SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) assert generated_txt[0].strip() == tgt_text
25
def __snake_case ( _lowerCAmelCase : List[str] , _lowerCAmelCase : int ) -> str: return (pointa[0] - pointa[0]) ** 2 + (pointa[1] - pointa[1]) ** 2 def __snake_case ( _lowerCAmelCase : int , _lowerCAmelCase : Union[str, Any]=0 ) -> Any: return sorted(_lowerCAmelCase , key=lambda _lowerCAmelCase : x[column] ) def __snake_case ( _lowerCAmelCase : List[str] , _lowerCAmelCase : Any , _lowerCAmelCase : Any=float("inf" ) ) -> int: for i in range(points_counts - 1 ): for j in range(i + 1 , _lowerCAmelCase ): A_ : Tuple = euclidean_distance_sqr(points[i] , points[j] ) if current_dis < min_dis: A_ : Union[str, Any] = current_dis return min_dis def __snake_case ( _lowerCAmelCase : Optional[int] , _lowerCAmelCase : Dict , _lowerCAmelCase : List[str]=float("inf" ) ) -> Dict: for i in range(min(6 , points_counts - 1 ) , _lowerCAmelCase ): for j in range(max(0 , i - 6 ) , _lowerCAmelCase ): A_ : List[Any] = euclidean_distance_sqr(points[i] , points[j] ) if current_dis < min_dis: A_ : Union[str, Any] = current_dis return min_dis def __snake_case ( _lowerCAmelCase : List[Any] , _lowerCAmelCase : str , _lowerCAmelCase : Dict ) -> List[str]: # base case if points_counts <= 3: return dis_between_closest_pair(_lowerCAmelCase , _lowerCAmelCase ) # recursion A_ : Optional[int] = points_counts // 2 A_ : List[Any] = closest_pair_of_points_sqr( _lowerCAmelCase , points_sorted_on_y[:mid] , _lowerCAmelCase ) A_ : List[Any] = closest_pair_of_points_sqr( _lowerCAmelCase , points_sorted_on_y[mid:] , points_counts - mid ) A_ : Tuple = min(_lowerCAmelCase , _lowerCAmelCase ) A_ : Dict = [] for point in points_sorted_on_x: if abs(point[0] - points_sorted_on_x[mid][0] ) < closest_pair_dis: cross_strip.append(_lowerCAmelCase ) A_ : Tuple = dis_between_closest_in_strip( _lowerCAmelCase , len(_lowerCAmelCase ) , _lowerCAmelCase ) return min(_lowerCAmelCase , _lowerCAmelCase ) def __snake_case ( _lowerCAmelCase : str , _lowerCAmelCase : Optional[Any] ) -> Any: A_ : Optional[Any] = column_based_sort(_lowerCAmelCase , column=0 ) A_ : Optional[int] = column_based_sort(_lowerCAmelCase , column=1 ) return ( closest_pair_of_points_sqr( _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) ) ** 0.5 if __name__ == "__main__": _lowerCAmelCase : List[Any] = [(2, 3), (12, 30), (40, 50), (5, 1), (12, 10), (3, 4)] print('''Distance:''', closest_pair_of_points(points, len(points)))
300
0
# 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 from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available _snake_case = { "configuration_xmod": [ "XMOD_PRETRAINED_CONFIG_ARCHIVE_MAP", "XmodConfig", "XmodOnnxConfig", ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = [ "XMOD_PRETRAINED_MODEL_ARCHIVE_LIST", "XmodForCausalLM", "XmodForMaskedLM", "XmodForMultipleChoice", "XmodForQuestionAnswering", "XmodForSequenceClassification", "XmodForTokenClassification", "XmodModel", "XmodPreTrainedModel", ] if TYPE_CHECKING: from .configuration_xmod import XMOD_PRETRAINED_CONFIG_ARCHIVE_MAP, XmodConfig, XmodOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xmod import ( XMOD_PRETRAINED_MODEL_ARCHIVE_LIST, XmodForCausalLM, XmodForMaskedLM, XmodForMultipleChoice, XmodForQuestionAnswering, XmodForSequenceClassification, XmodForTokenClassification, XmodModel, XmodPreTrainedModel, ) else: import sys _snake_case = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
26
import inspect import unittest from transformers import ViTMSNConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTMSNForImageClassification, ViTMSNModel from transformers.models.vit_msn.modeling_vit_msn import VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class __magic_name__ : """simple docstring""" def __init__( self :Dict , snake_case :Optional[int] , snake_case :Tuple=13 , snake_case :List[Any]=30 , snake_case :Union[str, Any]=2 , snake_case :List[Any]=3 , snake_case :Tuple=True , snake_case :Dict=True , snake_case :Dict=32 , snake_case :List[str]=5 , snake_case :Optional[Any]=4 , snake_case :Any=37 , snake_case :Dict="gelu" , snake_case :List[str]=0.1 , snake_case :str=0.1 , snake_case :Tuple=10 , snake_case :str=0.02 , snake_case :Optional[Any]=None , ): '''simple docstring''' A_ : Tuple = parent A_ : int = batch_size A_ : List[str] = image_size A_ : List[Any] = patch_size A_ : Optional[Any] = num_channels A_ : List[Any] = is_training A_ : Tuple = use_labels A_ : Union[str, Any] = hidden_size A_ : Tuple = num_hidden_layers A_ : Any = num_attention_heads A_ : List[str] = intermediate_size A_ : Optional[int] = hidden_act A_ : List[str] = hidden_dropout_prob A_ : str = attention_probs_dropout_prob A_ : Any = type_sequence_label_size A_ : List[str] = initializer_range A_ : Dict = scope # in ViT MSN, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) A_ : Optional[int] = (image_size // patch_size) ** 2 A_ : List[str] = num_patches + 1 def SCREAMING_SNAKE_CASE ( self :Optional[Any] ): '''simple docstring''' A_ : Any = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) A_ : Tuple = None if self.use_labels: A_ : Dict = ids_tensor([self.batch_size] , self.type_sequence_label_size ) A_ : Dict = self.get_config() return config, pixel_values, labels def SCREAMING_SNAKE_CASE ( self :Union[str, Any] ): '''simple docstring''' return ViTMSNConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , initializer_range=self.initializer_range , ) def SCREAMING_SNAKE_CASE ( self :List[Any] , snake_case :List[Any] , snake_case :str , snake_case :Tuple ): '''simple docstring''' A_ : Optional[Any] = ViTMSNModel(config=snake_case ) model.to(snake_case ) model.eval() A_ : int = model(snake_case ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def SCREAMING_SNAKE_CASE ( self :int , snake_case :Optional[int] , snake_case :List[str] , snake_case :List[str] ): '''simple docstring''' A_ : Dict = self.type_sequence_label_size A_ : Tuple = ViTMSNForImageClassification(snake_case ) model.to(snake_case ) model.eval() A_ : Union[str, Any] = model(snake_case , labels=snake_case ) print("Pixel and labels shape: {pixel_values.shape}, {labels.shape}" ) print("Labels: {labels}" ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images A_ : Union[str, Any] = 1 A_ : int = ViTMSNForImageClassification(snake_case ) model.to(snake_case ) model.eval() A_ : Dict = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) A_ : Optional[Any] = model(snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def SCREAMING_SNAKE_CASE ( self :List[Any] ): '''simple docstring''' A_ : List[str] = self.prepare_config_and_inputs() A_ , A_ , A_ : Optional[int] = config_and_inputs A_ : Dict = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class __magic_name__ ( lowerCamelCase__ , lowerCamelCase__ , unittest.TestCase ): """simple docstring""" __UpperCamelCase = (ViTMSNModel, ViTMSNForImageClassification) if is_torch_available() else () __UpperCamelCase = ( {'''feature-extraction''': ViTMSNModel, '''image-classification''': ViTMSNForImageClassification} if is_torch_available() else {} ) __UpperCamelCase = False __UpperCamelCase = False __UpperCamelCase = False __UpperCamelCase = False def SCREAMING_SNAKE_CASE ( self :Union[str, Any] ): '''simple docstring''' A_ : Tuple = ViTMSNModelTester(self ) A_ : str = ConfigTester(self , config_class=snake_case , has_text_modality=snake_case , hidden_size=37 ) def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' self.config_tester.run_common_tests() @unittest.skip(reason="ViTMSN does not use inputs_embeds" ) def SCREAMING_SNAKE_CASE ( self :Dict ): '''simple docstring''' pass def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' A_ , A_ : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A_ : Optional[int] = model_class(snake_case ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) A_ : Optional[int] = model.get_output_embeddings() self.assertTrue(x is None or isinstance(snake_case , nn.Linear ) ) def SCREAMING_SNAKE_CASE ( self :Optional[Any] ): '''simple docstring''' A_ , A_ : str = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A_ : Optional[Any] = model_class(snake_case ) A_ : Any = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic A_ : List[str] = [*signature.parameters.keys()] A_ : List[Any] = ["pixel_values"] self.assertListEqual(arg_names[:1] , snake_case ) def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' A_ : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case ) def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' A_ : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*snake_case ) @slow def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' for model_name in VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A_ : Optional[Any] = ViTMSNModel.from_pretrained(snake_case ) self.assertIsNotNone(snake_case ) def __snake_case ( ) -> Optional[Any]: A_ : Optional[Any] = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_torch @require_vision class __magic_name__ ( unittest.TestCase ): """simple docstring""" @cached_property def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' return ViTImageProcessor.from_pretrained("facebook/vit-msn-small" ) if is_vision_available() else None @slow def SCREAMING_SNAKE_CASE ( self :List[Any] ): '''simple docstring''' torch.manual_seed(2 ) A_ : Any = ViTMSNForImageClassification.from_pretrained("facebook/vit-msn-small" ).to(snake_case ) A_ : List[str] = self.default_image_processor A_ : int = prepare_img() A_ : List[str] = image_processor(images=snake_case , return_tensors="pt" ).to(snake_case ) # forward pass with torch.no_grad(): A_ : Optional[int] = model(**snake_case ) # verify the logits A_ : List[Any] = torch.Size((1, 1_000) ) self.assertEqual(outputs.logits.shape , snake_case ) A_ : int = torch.tensor([-0.0803, -0.4454, -0.2375] ).to(snake_case ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , snake_case , atol=1e-4 ) )
300
0
'''simple docstring''' import json import unittest import numpy as np from huggingface_hub import hf_hub_download from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from transformers import OneFormerImageProcessor from transformers.models.oneformer.image_processing_oneformer import binary_mask_to_rle from transformers.models.oneformer.modeling_oneformer import OneFormerForUniversalSegmentationOutput if is_vision_available(): from PIL import Image def lowerCamelCase (_SCREAMING_SNAKE_CASE : Any , _SCREAMING_SNAKE_CASE : List[str]="shi-labs/oneformer_demo" ): with open(hf_hub_download(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , repo_type='dataset' ) , 'r' ) as f: __a : List[Any] = json.load(_SCREAMING_SNAKE_CASE ) __a : str = {} __a : List[str] = [] __a : List[Any] = [] for key, info in class_info.items(): __a : List[Any] = info['name'] class_names.append(info['name'] ) if info["isthing"]: thing_ids.append(int(_SCREAMING_SNAKE_CASE ) ) __a : List[str] = thing_ids __a : str = class_names return metadata class __UpperCamelCase ( unittest.TestCase ): def __init__( self , __a , __a=7 , __a=3 , __a=30 , __a=400 , __a=None , __a=True , __a=True , __a=[0.5, 0.5, 0.5] , __a=[0.5, 0.5, 0.5] , __a=10 , __a=False , __a=255 , __a="shi-labs/oneformer_demo" , __a="ade20k_panoptic.json" , __a=10 , ): '''simple docstring''' __a : Union[str, Any] = parent __a : Tuple = batch_size __a : List[Any] = num_channels __a : Tuple = min_resolution __a : Union[str, Any] = max_resolution __a : List[str] = do_resize __a : Union[str, Any] = {'shortest_edge': 32, 'longest_edge': 1333} if size is None else size __a : Optional[Any] = do_normalize __a : Dict = image_mean __a : Union[str, Any] = image_std __a : Union[str, Any] = class_info_file __a : int = prepare_metadata(__a , __a ) __a : List[Any] = num_text __a : List[Any] = repo_path # for the post_process_functions __a : Optional[Any] = 2 __a : Dict = 10 __a : Tuple = 10 __a : int = 3 __a : int = 4 __a : Union[str, Any] = num_labels __a : Any = do_reduce_labels __a : Union[str, Any] = ignore_index def __UpperCAmelCase ( self ): '''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, "num_labels": self.num_labels, "do_reduce_labels": self.do_reduce_labels, "ignore_index": self.ignore_index, "class_info_file": self.class_info_file, "metadata": self.metadata, "num_text": self.num_text, } def __UpperCAmelCase ( self , __a , __a=False ): '''simple docstring''' if not batched: __a : int = image_inputs[0] if isinstance(__a , Image.Image ): __a , __a : List[Any] = image.size else: __a , __a : Optional[Any] = image.shape[1], image.shape[2] if w < h: __a : Optional[Any] = int(self.size['shortest_edge'] * h / w ) __a : str = self.size['shortest_edge'] elif w > h: __a : Any = self.size['shortest_edge'] __a : Dict = int(self.size['shortest_edge'] * w / h ) else: __a : Union[str, Any] = self.size['shortest_edge'] __a : List[Any] = self.size['shortest_edge'] else: __a : Optional[int] = [] for image in image_inputs: __a , __a : Any = self.get_expected_values([image] ) expected_values.append((expected_height, expected_width) ) __a : Union[str, Any] = max(__a , key=lambda __a : item[0] )[0] __a : str = max(__a , key=lambda __a : item[1] )[1] return expected_height, expected_width def __UpperCAmelCase ( self ): '''simple docstring''' return OneFormerForUniversalSegmentationOutput( # +1 for null class class_queries_logits=torch.randn((self.batch_size, self.num_queries, self.num_classes + 1) ) , masks_queries_logits=torch.randn((self.batch_size, self.num_queries, self.height, self.width) ) , ) @require_torch @require_vision class __UpperCamelCase ( lowerCAmelCase_ , unittest.TestCase ): A_ = OneFormerImageProcessor if (is_vision_available() and is_torch_available()) else None # only for test_image_processing_common.test_image_proc_to_json_string A_ = image_processing_class def __UpperCAmelCase ( self ): '''simple docstring''' __a : Optional[int] = OneFormerImageProcessorTester(self ) @property def __UpperCAmelCase ( self ): '''simple docstring''' return self.image_processing_tester.prepare_image_processor_dict() def __UpperCAmelCase ( self ): '''simple docstring''' __a : Union[str, Any] = 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 , 'size' ) ) self.assertTrue(hasattr(__a , 'ignore_index' ) ) self.assertTrue(hasattr(__a , 'class_info_file' ) ) self.assertTrue(hasattr(__a , 'num_text' ) ) self.assertTrue(hasattr(__a , 'repo_path' ) ) self.assertTrue(hasattr(__a , 'metadata' ) ) self.assertTrue(hasattr(__a , 'do_reduce_labels' ) ) def __UpperCAmelCase ( self ): '''simple docstring''' pass def __UpperCAmelCase ( self ): '''simple docstring''' __a : Union[str, Any] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images __a : List[Any] = prepare_image_inputs(self.image_processing_tester , equal_resolution=__a ) for image in image_inputs: self.assertIsInstance(__a , Image.Image ) # Test not batched input __a : List[Any] = image_processor(image_inputs[0] , ['semantic'] , return_tensors='pt' ).pixel_values __a , __a : List[str] = self.image_processing_tester.get_expected_values(__a ) self.assertEqual( encoded_images.shape , (1, self.image_processing_tester.num_channels, expected_height, expected_width) , ) # Test batched __a , __a : Optional[Any] = self.image_processing_tester.get_expected_values(__a , batched=__a ) __a : List[str] = image_processor( __a , ['semantic'] * len(__a ) , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processing_tester.batch_size, self.image_processing_tester.num_channels, expected_height, expected_width, ) , ) def __UpperCAmelCase ( self ): '''simple docstring''' __a : int = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors __a : int = prepare_image_inputs(self.image_processing_tester , equal_resolution=__a , numpify=__a ) for image in image_inputs: self.assertIsInstance(__a , np.ndarray ) # Test not batched input __a : str = image_processor(image_inputs[0] , ['semantic'] , return_tensors='pt' ).pixel_values __a , __a : Tuple = self.image_processing_tester.get_expected_values(__a ) self.assertEqual( encoded_images.shape , (1, self.image_processing_tester.num_channels, expected_height, expected_width) , ) # Test batched __a , __a : Union[str, Any] = self.image_processing_tester.get_expected_values(__a , batched=__a ) __a : Any = image_processor( __a , ['semantic'] * len(__a ) , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processing_tester.batch_size, self.image_processing_tester.num_channels, expected_height, expected_width, ) , ) def __UpperCAmelCase ( self ): '''simple docstring''' __a : int = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors __a : Union[str, Any] = prepare_image_inputs(self.image_processing_tester , equal_resolution=__a , torchify=__a ) for image in image_inputs: self.assertIsInstance(__a , torch.Tensor ) # Test not batched input __a : List[Any] = image_processor(image_inputs[0] , ['semantic'] , return_tensors='pt' ).pixel_values __a , __a : int = self.image_processing_tester.get_expected_values(__a ) self.assertEqual( encoded_images.shape , (1, self.image_processing_tester.num_channels, expected_height, expected_width) , ) # Test batched __a , __a : List[Any] = self.image_processing_tester.get_expected_values(__a , batched=__a ) __a : str = image_processor( __a , ['semantic'] * len(__a ) , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processing_tester.batch_size, self.image_processing_tester.num_channels, expected_height, expected_width, ) , ) def __UpperCAmelCase ( self , __a=False , __a=False , __a="np" ): '''simple docstring''' __a : str = self.image_processing_class(**self.image_processor_dict ) # prepare image and target __a : Dict = self.image_processing_tester.num_labels __a : List[str] = None __a : str = None __a : Optional[int] = prepare_image_inputs(self.image_processing_tester , equal_resolution=__a ) if with_segmentation_maps: __a : List[str] = num_labels if is_instance_map: __a : Optional[int] = list(range(__a ) ) * 2 __a : int = dict(enumerate(__a ) ) __a : List[str] = [ np.random.randint(0 , high * 2 , (img.size[1], img.size[0]) ).astype(np.uinta ) for img in image_inputs ] if segmentation_type == "pil": __a : str = [Image.fromarray(__a ) for annotation in annotations] __a : Optional[Any] = image_processor( __a , ['semantic'] * len(__a ) , __a , return_tensors='pt' , instance_id_to_semantic_id=__a , pad_and_return_pixel_mask=__a , ) return inputs def __UpperCAmelCase ( self ): '''simple docstring''' pass def __UpperCAmelCase ( self ): '''simple docstring''' def common(__a=False , __a=None ): __a : int = self.comm_get_image_processor_inputs( with_segmentation_maps=__a , is_instance_map=__a , segmentation_type=__a ) __a : Optional[Any] = inputs['mask_labels'] __a : Optional[Any] = inputs['class_labels'] __a : Optional[Any] = inputs['pixel_values'] __a : Optional[Any] = inputs['text_inputs'] # check the batch_size for mask_label, class_label, text_input in zip(__a , __a , __a ): self.assertEqual(mask_label.shape[0] , class_label.shape[0] ) # this ensure padding has happened self.assertEqual(mask_label.shape[1:] , pixel_values.shape[2:] ) self.assertEqual(len(__a ) , self.image_processing_tester.num_text ) common() common(is_instance_map=__a ) common(is_instance_map=__a , segmentation_type='pil' ) common(is_instance_map=__a , segmentation_type='pil' ) def __UpperCAmelCase ( self ): '''simple docstring''' __a : Optional[Any] = np.zeros((20, 50) ) __a : List[Any] = 1 __a : List[str] = 1 __a : Optional[Any] = 1 __a : List[str] = binary_mask_to_rle(__a ) self.assertEqual(len(__a ) , 4 ) self.assertEqual(rle[0] , 21 ) self.assertEqual(rle[1] , 45 ) def __UpperCAmelCase ( self ): '''simple docstring''' __a : Tuple = self.image_processing_class( num_labels=self.image_processing_tester.num_classes , max_seq_length=77 , task_seq_length=77 , class_info_file='ade20k_panoptic.json' , num_text=self.image_processing_tester.num_text , repo_path='shi-labs/oneformer_demo' , ) __a : Union[str, Any] = self.image_processing_tester.get_fake_oneformer_outputs() __a : List[Any] = fature_extractor.post_process_semantic_segmentation(__a ) self.assertEqual(len(__a ) , self.image_processing_tester.batch_size ) self.assertEqual( segmentation[0].shape , ( self.image_processing_tester.height, self.image_processing_tester.width, ) , ) __a : str = [(1, 4) for i in range(self.image_processing_tester.batch_size )] __a : Optional[int] = fature_extractor.post_process_semantic_segmentation(__a , target_sizes=__a ) self.assertEqual(segmentation[0].shape , target_sizes[0] ) def __UpperCAmelCase ( self ): '''simple docstring''' __a : Optional[Any] = self.image_processing_class( num_labels=self.image_processing_tester.num_classes , max_seq_length=77 , task_seq_length=77 , class_info_file='ade20k_panoptic.json' , num_text=self.image_processing_tester.num_text , repo_path='shi-labs/oneformer_demo' , ) __a : List[str] = self.image_processing_tester.get_fake_oneformer_outputs() __a : Union[str, Any] = image_processor.post_process_instance_segmentation(__a , threshold=0 ) self.assertTrue(len(__a ) == self.image_processing_tester.batch_size ) for el in segmentation: self.assertTrue('segmentation' in el ) self.assertTrue('segments_info' in el ) self.assertEqual(type(el['segments_info'] ) , __a ) self.assertEqual( el['segmentation'].shape , (self.image_processing_tester.height, self.image_processing_tester.width) ) def __UpperCAmelCase ( self ): '''simple docstring''' __a : Union[str, Any] = self.image_processing_class( num_labels=self.image_processing_tester.num_classes , max_seq_length=77 , task_seq_length=77 , class_info_file='ade20k_panoptic.json' , num_text=self.image_processing_tester.num_text , repo_path='shi-labs/oneformer_demo' , ) __a : List[str] = self.image_processing_tester.get_fake_oneformer_outputs() __a : Optional[int] = image_processor.post_process_panoptic_segmentation(__a , threshold=0 ) self.assertTrue(len(__a ) == self.image_processing_tester.batch_size ) for el in segmentation: self.assertTrue('segmentation' in el ) self.assertTrue('segments_info' in el ) self.assertEqual(type(el['segments_info'] ) , __a ) self.assertEqual( el['segmentation'].shape , (self.image_processing_tester.height, self.image_processing_tester.width) )
27
import torch from diffusers import DDPMScheduler from .test_schedulers import SchedulerCommonTest class __magic_name__ ( lowerCamelCase__ ): """simple docstring""" __UpperCamelCase = (DDPMScheduler,) def SCREAMING_SNAKE_CASE ( self :Union[str, Any] , **snake_case :str ): '''simple docstring''' A_ : Dict = { "num_train_timesteps": 1_000, "beta_start": 0.0001, "beta_end": 0.02, "beta_schedule": "linear", "variance_type": "fixed_small", "clip_sample": True, } config.update(**snake_case ) return config def SCREAMING_SNAKE_CASE ( self :int ): '''simple docstring''' for timesteps in [1, 5, 100, 1_000]: self.check_over_configs(num_train_timesteps=snake_case ) def SCREAMING_SNAKE_CASE ( self :Optional[Any] ): '''simple docstring''' for beta_start, beta_end in zip([0.0001, 0.001, 0.01, 0.1] , [0.002, 0.02, 0.2, 2] ): self.check_over_configs(beta_start=snake_case , beta_end=snake_case ) def SCREAMING_SNAKE_CASE ( self :int ): '''simple docstring''' for schedule in ["linear", "squaredcos_cap_v2"]: self.check_over_configs(beta_schedule=snake_case ) def SCREAMING_SNAKE_CASE ( self :List[Any] ): '''simple docstring''' for variance in ["fixed_small", "fixed_large", "other"]: self.check_over_configs(variance_type=snake_case ) def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' for clip_sample in [True, False]: self.check_over_configs(clip_sample=snake_case ) def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' self.check_over_configs(thresholding=snake_case ) for threshold in [0.5, 1.0, 2.0]: for prediction_type in ["epsilon", "sample", "v_prediction"]: self.check_over_configs( thresholding=snake_case , prediction_type=snake_case , sample_max_value=snake_case , ) def SCREAMING_SNAKE_CASE ( self :Optional[int] ): '''simple docstring''' for prediction_type in ["epsilon", "sample", "v_prediction"]: self.check_over_configs(prediction_type=snake_case ) def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' for t in [0, 500, 999]: self.check_over_forward(time_step=snake_case ) def SCREAMING_SNAKE_CASE ( self :Optional[Any] ): '''simple docstring''' A_ : Tuple = self.scheduler_classes[0] A_ : List[str] = self.get_scheduler_config() A_ : List[str] = scheduler_class(**snake_case ) assert torch.sum(torch.abs(scheduler._get_variance(0 ) - 0.0 ) ) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(487 ) - 0.00979 ) ) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(999 ) - 0.02 ) ) < 1e-5 def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' A_ : int = self.scheduler_classes[0] A_ : List[str] = self.get_scheduler_config() A_ : int = scheduler_class(**snake_case ) A_ : Tuple = len(snake_case ) A_ : List[str] = self.dummy_model() A_ : Optional[Any] = self.dummy_sample_deter A_ : List[str] = torch.manual_seed(0 ) for t in reversed(range(snake_case ) ): # 1. predict noise residual A_ : Tuple = model(snake_case , snake_case ) # 2. predict previous mean of sample x_t-1 A_ : Dict = scheduler.step(snake_case , snake_case , snake_case , generator=snake_case ).prev_sample # if t > 0: # noise = self.dummy_sample_deter # variance = scheduler.get_variance(t) ** (0.5) * noise # # sample = pred_prev_sample + variance A_ : Optional[int] = pred_prev_sample A_ : Tuple = torch.sum(torch.abs(snake_case ) ) A_ : str = torch.mean(torch.abs(snake_case ) ) assert abs(result_sum.item() - 258.9606 ) < 1e-2 assert abs(result_mean.item() - 0.3372 ) < 1e-3 def SCREAMING_SNAKE_CASE ( self :Dict ): '''simple docstring''' A_ : Optional[int] = self.scheduler_classes[0] A_ : int = self.get_scheduler_config(prediction_type="v_prediction" ) A_ : List[str] = scheduler_class(**snake_case ) A_ : int = len(snake_case ) A_ : Dict = self.dummy_model() A_ : str = self.dummy_sample_deter A_ : Any = torch.manual_seed(0 ) for t in reversed(range(snake_case ) ): # 1. predict noise residual A_ : Optional[int] = model(snake_case , snake_case ) # 2. predict previous mean of sample x_t-1 A_ : Tuple = scheduler.step(snake_case , snake_case , snake_case , generator=snake_case ).prev_sample # if t > 0: # noise = self.dummy_sample_deter # variance = scheduler.get_variance(t) ** (0.5) * noise # # sample = pred_prev_sample + variance A_ : List[str] = pred_prev_sample A_ : Optional[Any] = torch.sum(torch.abs(snake_case ) ) A_ : List[str] = torch.mean(torch.abs(snake_case ) ) assert abs(result_sum.item() - 202.0296 ) < 1e-2 assert abs(result_mean.item() - 0.2631 ) < 1e-3 def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' A_ : str = self.scheduler_classes[0] A_ : Optional[Any] = self.get_scheduler_config() A_ : Dict = scheduler_class(**snake_case ) A_ : Optional[int] = [100, 87, 50, 1, 0] scheduler.set_timesteps(timesteps=snake_case ) A_ : Optional[int] = scheduler.timesteps for i, timestep in enumerate(snake_case ): if i == len(snake_case ) - 1: A_ : str = -1 else: A_ : List[str] = timesteps[i + 1] A_ : Optional[int] = scheduler.previous_timestep(snake_case ) A_ : List[str] = prev_t.item() self.assertEqual(snake_case , snake_case ) def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' A_ : Optional[Any] = self.scheduler_classes[0] A_ : int = self.get_scheduler_config() A_ : Tuple = scheduler_class(**snake_case ) A_ : List[str] = [100, 87, 50, 51, 0] with self.assertRaises(snake_case , msg="`custom_timesteps` must be in descending order." ): scheduler.set_timesteps(timesteps=snake_case ) def SCREAMING_SNAKE_CASE ( self :List[Any] ): '''simple docstring''' A_ : Any = self.scheduler_classes[0] A_ : Union[str, Any] = self.get_scheduler_config() A_ : Optional[int] = scheduler_class(**snake_case ) A_ : Union[str, Any] = [100, 87, 50, 1, 0] A_ : Optional[int] = len(snake_case ) with self.assertRaises(snake_case , msg="Can only pass one of `num_inference_steps` or `custom_timesteps`." ): scheduler.set_timesteps(num_inference_steps=snake_case , timesteps=snake_case ) def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' A_ : Union[str, Any] = self.scheduler_classes[0] A_ : Optional[Any] = self.get_scheduler_config() A_ : Optional[int] = scheduler_class(**snake_case ) A_ : Optional[int] = [scheduler.config.num_train_timesteps] with self.assertRaises( snake_case , msg="`timesteps` must start before `self.config.train_timesteps`: {scheduler.config.num_train_timesteps}}" , ): scheduler.set_timesteps(timesteps=snake_case )
300
0
'''simple docstring''' import logging import os from dataclasses import dataclass from typing import List, Optional, Union import tqdm from filelock import FileLock from transformers import ( BartTokenizer, BartTokenizerFast, DataProcessor, PreTrainedTokenizer, RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, is_tf_available, is_torch_available, ) _lowerCamelCase : Tuple = logging.getLogger(__name__) @dataclass(frozen=_a ) class SCREAMING_SNAKE_CASE : """simple docstring""" _SCREAMING_SNAKE_CASE = 42 _SCREAMING_SNAKE_CASE = 42 _SCREAMING_SNAKE_CASE = None _SCREAMING_SNAKE_CASE = None _SCREAMING_SNAKE_CASE = None @dataclass(frozen=_a ) class SCREAMING_SNAKE_CASE : """simple docstring""" _SCREAMING_SNAKE_CASE = 42 _SCREAMING_SNAKE_CASE = None _SCREAMING_SNAKE_CASE = None _SCREAMING_SNAKE_CASE = None _SCREAMING_SNAKE_CASE = None if is_torch_available(): import torch from torch.utils.data import Dataset class SCREAMING_SNAKE_CASE ( _a ): """simple docstring""" _SCREAMING_SNAKE_CASE = 42 def __init__( self : Union[str, Any] , UpperCamelCase__ : str , UpperCamelCase__ : PreTrainedTokenizer , UpperCamelCase__ : str , UpperCamelCase__ : Optional[int] = None , UpperCamelCase__ : Union[str, Any]=False , UpperCamelCase__ : bool = False , ): """simple docstring""" UpperCamelCase = hans_processors[task]() UpperCamelCase = os.path.join( UpperCamelCase__ , 'cached_{}_{}_{}_{}'.format( 'dev' if evaluate else 'train' , tokenizer.__class__.__name__ , str(UpperCamelCase__ ) , UpperCamelCase__ , ) , ) UpperCamelCase = processor.get_labels() if tokenizer.__class__ in ( RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, BartTokenizer, BartTokenizerFast, ): # HACK(label indices are swapped in RoBERTa pretrained model) UpperCamelCase , UpperCamelCase = label_list[2], label_list[1] UpperCamelCase = label_list # Make sure only the first process in distributed training processes the dataset, # and the others will use the cache. UpperCamelCase = cached_features_file + '.lock' with FileLock(UpperCamelCase__ ): if os.path.exists(UpperCamelCase__ ) and not overwrite_cache: logger.info(f"""Loading features from cached file {cached_features_file}""" ) UpperCamelCase = torch.load(UpperCamelCase__ ) else: logger.info(f"""Creating features from dataset file at {data_dir}""" ) UpperCamelCase = ( processor.get_dev_examples(UpperCamelCase__ ) if evaluate else processor.get_train_examples(UpperCamelCase__ ) ) logger.info('Training examples: %s' , len(UpperCamelCase__ ) ) UpperCamelCase = hans_convert_examples_to_features(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) logger.info('Saving features into cached file %s' , UpperCamelCase__ ) torch.save(self.features , UpperCamelCase__ ) def __len__( self : Any ): """simple docstring""" return len(self.features ) def __getitem__( self : Optional[Any] , UpperCamelCase__ : Union[str, Any] ): """simple docstring""" return self.features[i] def A ( self : Any ): """simple docstring""" return self.label_list if is_tf_available(): import tensorflow as tf class SCREAMING_SNAKE_CASE : """simple docstring""" _SCREAMING_SNAKE_CASE = 42 def __init__( self : Optional[int] , UpperCamelCase__ : str , UpperCamelCase__ : PreTrainedTokenizer , UpperCamelCase__ : str , UpperCamelCase__ : Optional[int] = 1_2_8 , UpperCamelCase__ : Any=False , UpperCamelCase__ : bool = False , ): """simple docstring""" UpperCamelCase = hans_processors[task]() UpperCamelCase = processor.get_labels() if tokenizer.__class__ in ( RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, BartTokenizer, BartTokenizerFast, ): # HACK(label indices are swapped in RoBERTa pretrained model) UpperCamelCase , UpperCamelCase = label_list[2], label_list[1] UpperCamelCase = label_list UpperCamelCase = processor.get_dev_examples(UpperCamelCase__ ) if evaluate else processor.get_train_examples(UpperCamelCase__ ) UpperCamelCase = hans_convert_examples_to_features(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) def gen(): for ex_index, ex in tqdm.tqdm(enumerate(self.features ) , desc='convert examples to features' ): if ex_index % 1_0_0_0_0 == 0: logger.info('Writing example %d of %d' % (ex_index, len(UpperCamelCase__ )) ) yield ( { "example_id": 0, "input_ids": ex.input_ids, "attention_mask": ex.attention_mask, "token_type_ids": ex.token_type_ids, }, ex.label, ) UpperCamelCase = tf.data.Dataset.from_generator( UpperCamelCase__ , ( { 'example_id': tf.intaa, 'input_ids': tf.intaa, 'attention_mask': tf.intaa, 'token_type_ids': tf.intaa, }, tf.intaa, ) , ( { 'example_id': tf.TensorShape([] ), 'input_ids': tf.TensorShape([None, None] ), 'attention_mask': tf.TensorShape([None, None] ), 'token_type_ids': tf.TensorShape([None, None] ), }, tf.TensorShape([] ), ) , ) def A ( self : Dict ): """simple docstring""" return self.dataset def __len__( self : Union[str, Any] ): """simple docstring""" return len(self.features ) def __getitem__( self : List[str] , UpperCamelCase__ : str ): """simple docstring""" return self.features[i] def A ( self : Tuple ): """simple docstring""" return self.label_list class SCREAMING_SNAKE_CASE ( _a ): """simple docstring""" def A ( self : List[Any] , UpperCamelCase__ : Optional[int] ): """simple docstring""" return self._create_examples(self._read_tsv(os.path.join(UpperCamelCase__ , 'heuristics_train_set.txt' ) ) , 'train' ) def A ( self : Optional[int] , UpperCamelCase__ : Union[str, Any] ): """simple docstring""" return self._create_examples(self._read_tsv(os.path.join(UpperCamelCase__ , 'heuristics_evaluation_set.txt' ) ) , 'dev' ) def A ( self : Optional[Any] ): """simple docstring""" return ["contradiction", "entailment", "neutral"] def A ( self : Optional[Any] , UpperCamelCase__ : Union[str, Any] , UpperCamelCase__ : str ): """simple docstring""" UpperCamelCase = [] for i, line in enumerate(UpperCamelCase__ ): if i == 0: continue UpperCamelCase = '%s-%s' % (set_type, line[0]) UpperCamelCase = line[5] UpperCamelCase = line[6] UpperCamelCase = line[7][2:] if line[7].startswith('ex' ) else line[7] UpperCamelCase = line[0] examples.append(InputExample(guid=UpperCamelCase__ , text_a=UpperCamelCase__ , text_b=UpperCamelCase__ , label=UpperCamelCase__ , pairID=UpperCamelCase__ ) ) return examples def __lowerCamelCase ( A__ , A__ , A__ , A__ , ) -> Union[str, Any]: """simple docstring""" UpperCamelCase = {label: i for i, label in enumerate(A__ )} UpperCamelCase = [] for ex_index, example in tqdm.tqdm(enumerate(A__ ) , desc='convert examples to features' ): if ex_index % 10_000 == 0: logger.info('Writing example %d' % (ex_index) ) UpperCamelCase = tokenizer( example.text_a , example.text_b , add_special_tokens=A__ , max_length=A__ , padding='max_length' , truncation=A__ , return_overflowing_tokens=A__ , ) UpperCamelCase = label_map[example.label] if example.label in label_map else 0 UpperCamelCase = int(example.pairID ) features.append(InputFeatures(**A__ , label=A__ , pairID=A__ ) ) for i, example in enumerate(examples[:5] ): logger.info('*** Example ***' ) logger.info(F"""guid: {example}""" ) logger.info(F"""features: {features[i]}""" ) return features _lowerCamelCase : Any = { "hans": 3, } _lowerCamelCase : Optional[int] = { "hans": HansProcessor, }
28
import argparse import json import os import fairseq import torch from torch import nn from transformers import ( SpeechaTextaConfig, SpeechaTextaForCausalLM, SpeechaTextaTokenizer, SpeechEncoderDecoderConfig, SpeechEncoderDecoderModel, WavaVecaConfig, WavaVecaFeatureExtractor, WavaVecaModel, logging, ) logging.set_verbosity_info() _lowerCAmelCase : List[str] = logging.get_logger(__name__) _lowerCAmelCase : Optional[int] = { '''post_extract_proj''': '''feature_projection.projection''', '''encoder.pos_conv.0''': '''encoder.pos_conv_embed.conv''', '''self_attn.k_proj''': '''encoder.layers.*.attention.k_proj''', '''self_attn.v_proj''': '''encoder.layers.*.attention.v_proj''', '''self_attn.q_proj''': '''encoder.layers.*.attention.q_proj''', '''self_attn.out_proj''': '''encoder.layers.*.attention.out_proj''', '''self_attn_layer_norm''': '''encoder.layers.*.layer_norm''', '''fc1''': '''encoder.layers.*.feed_forward.intermediate_dense''', '''fc2''': '''encoder.layers.*.feed_forward.output_dense''', '''final_layer_norm''': '''encoder.layers.*.final_layer_norm''', '''encoder.layer_norm''': '''encoder.layer_norm''', '''w2v_model.layer_norm''': '''feature_projection.layer_norm''', '''quantizer.weight_proj''': '''quantizer.weight_proj''', '''quantizer.vars''': '''quantizer.codevectors''', '''project_q''': '''project_q''', '''final_proj''': '''project_hid''', '''w2v_encoder.proj''': '''lm_head''', '''mask_emb''': '''masked_spec_embed''', } _lowerCAmelCase : int = [ '''lm_head''', '''quantizer.weight_proj''', '''quantizer.codevectors''', '''project_q''', '''project_hid''', ] def __snake_case ( _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : List[Any] , _lowerCAmelCase : Any , _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : int ) -> List[Any]: for attribute in key.split("." ): A_ : List[Any] = getattr(_lowerCAmelCase , _lowerCAmelCase ) if weight_type is not None: A_ : List[Any] = getattr(_lowerCAmelCase , _lowerCAmelCase ).shape else: A_ : Tuple = hf_pointer.shape assert hf_shape == value.shape, ( f"Shape of hf {key + '.' + weight_type if weight_type is not None else ''} is {hf_shape}, but should be" f" {value.shape} for {full_name}" ) if weight_type == "weight": A_ : Optional[int] = value elif weight_type == "weight_g": A_ : Optional[int] = value elif weight_type == "weight_v": A_ : Any = value elif weight_type == "bias": A_ : str = value else: A_ : Any = value logger.info(f"{key + '.' + weight_type if weight_type is not None else ''} was initialized from {full_name}." ) def __snake_case ( _lowerCAmelCase : Optional[Any] , _lowerCAmelCase : Dict ) -> List[str]: A_ : Optional[Any] = [] A_ : Any = fairseq_model.state_dict() A_ : Union[str, Any] = hf_model.feature_extractor # if encoder has different dim to decoder -> use proj_weight A_ : str = None for name, value in fairseq_dict.items(): A_ : Tuple = False if "conv_layers" in name: load_conv_layer( _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , hf_model.config.feat_extract_norm == "group" , ) A_ : Optional[Any] = True elif name.split("." )[0] == "proj": A_ : Dict = fairseq_model.proj A_ : List[Any] = True else: for key, mapped_key in MAPPING.items(): if key in name or key.split("w2v_model." )[-1] == name.split("." )[0]: A_ : int = True if "*" in mapped_key: A_ : Optional[Any] = name.split(_lowerCAmelCase )[0].split("." )[-2] A_ : int = mapped_key.replace("*" , _lowerCAmelCase ) if "weight_g" in name: A_ : List[Any] = "weight_g" elif "weight_v" in name: A_ : List[Any] = "weight_v" elif "bias" in name: A_ : Dict = "bias" elif "weight" in name: A_ : List[Any] = "weight" else: A_ : Dict = None set_recursively(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) continue if not is_used: unused_weights.append(_lowerCAmelCase ) logger.warning(f"Unused weights: {unused_weights}" ) return proj_weight def __snake_case ( _lowerCAmelCase : Optional[Any] , _lowerCAmelCase : Tuple , _lowerCAmelCase : List[Any] , _lowerCAmelCase : List[Any] , _lowerCAmelCase : Optional[int] ) -> str: A_ : Any = full_name.split("conv_layers." )[-1] A_ : Optional[int] = name.split("." ) A_ : Optional[Any] = int(items[0] ) A_ : Union[str, Any] = int(items[1] ) if type_id == 0: if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, ( f"{full_name} has size {value.shape}, but" f" {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found." ) A_ : List[Any] = value logger.info(f"Feat extract conv layer {layer_id} was initialized from {full_name}." ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape, ( f"{full_name} has size {value.shape}, but" f" {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found." ) A_ : int = value logger.info(f"Feat extract conv layer {layer_id} was initialized from {full_name}." ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, ( f"{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was" " found." ) A_ : List[Any] = value logger.info(f"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}." ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, ( f"{full_name} has size {value.shape}, but" f" {feature_extractor[layer_id].layer_norm.weight.data.shape} was found." ) A_ : Tuple = value logger.info(f"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}." ) else: unused_weights.append(_lowerCAmelCase ) def __snake_case ( _lowerCAmelCase : Optional[int] ) -> str: A_ , A_ : List[str] = emb.weight.shape A_ : Optional[int] = nn.Linear(_lowerCAmelCase , _lowerCAmelCase , bias=_lowerCAmelCase ) A_ : List[Any] = emb.weight.data return lin_layer def __snake_case ( _lowerCAmelCase : str ) -> Tuple: with open(_lowerCAmelCase , "r" , encoding="utf-8" ) as f: A_ : int = f.readlines() A_ : Dict = [line.split(" " )[0] for line in lines] A_ : Tuple = len(_lowerCAmelCase ) A_ : Union[str, Any] = { "<s>": 0, "<pad>": 1, "</s>": 2, "<unk>": 3, } vocab_dict.update(dict(zip(_lowerCAmelCase , range(4 , num_words + 4 ) ) ) ) return vocab_dict @torch.no_grad() def __snake_case ( _lowerCAmelCase : int , _lowerCAmelCase : Tuple , _lowerCAmelCase : Dict , _lowerCAmelCase : Any , _lowerCAmelCase : Tuple , _lowerCAmelCase : List[str] , _lowerCAmelCase : Dict , ) -> Tuple: A_ : Optional[int] = WavaVecaConfig.from_pretrained(_lowerCAmelCase ) A_ : str = SpeechaTextaConfig.from_pretrained( _lowerCAmelCase , vocab_size=_lowerCAmelCase , decoder_layers=_lowerCAmelCase , do_stable_layer_norm=_lowerCAmelCase ) A_ : int = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=16000 , padding_value=0 , do_normalize=_lowerCAmelCase , return_attention_mask=_lowerCAmelCase , ) A_ , A_ , A_ : List[Any] = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={"data": "/".join(dict_path.split("/" )[:-1] )} ) A_ : Union[str, Any] = model[0].eval() # set weights for wav2vec2 encoder A_ : Tuple = WavaVecaModel(_lowerCAmelCase ) A_ : str = recursively_load_weights_wavaveca(model.encoder , _lowerCAmelCase ) A_ : Tuple = SpeechaTextaForCausalLM(_lowerCAmelCase ) A_ , A_ : List[str] = hf_decoder.model.decoder.load_state_dict(model.decoder.state_dict() , strict=_lowerCAmelCase ) # set output linear layer unexpected_keys.remove("embed_out" ) A_ : Union[str, Any] = nn.Parameter(model.decoder.embed_out.detach() ) # layer norm is init to identity matrix so leaving it is fine logger.warning(f"The following keys are missing when loading the decoder weights: {missing_keys}" ) logger.warning(f"The following keys are unexpected when loading the decoder weights: {unexpected_keys}" ) A_ : str = SpeechEncoderDecoderModel(encoder=_lowerCAmelCase , decoder=_lowerCAmelCase ) A_ : Optional[Any] = False # add projection layer A_ : Optional[Any] = nn.Parameter(projection_layer.weight ) A_ : int = nn.Parameter(projection_layer.bias ) A_ : str = create_vocab_dict(_lowerCAmelCase ) with open(os.path.join(_lowerCAmelCase , "vocab.json" ) , "w" ) as fp: json.dump(_lowerCAmelCase , _lowerCAmelCase ) A_ : Any = SpeechaTextaTokenizer(os.path.join(_lowerCAmelCase , "vocab.json" ) ) tokenizer.save_pretrained(_lowerCAmelCase ) A_ : Optional[int] = hf_wavavec.config.to_dict() A_ : int = tokenizer.pad_token_id A_ : List[str] = tokenizer.bos_token_id A_ : List[str] = tokenizer.eos_token_id A_ : List[str] = "speech_to_text_2" A_ : Tuple = "wav2vec2" A_ : str = SpeechEncoderDecoderConfig.from_dict(_lowerCAmelCase ) hf_wavavec.save_pretrained(_lowerCAmelCase ) feature_extractor.save_pretrained(_lowerCAmelCase ) if __name__ == "__main__": _lowerCAmelCase : Optional[Any] = argparse.ArgumentParser() parser.add_argument('''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''') parser.add_argument('''--checkpoint_path''', default=None, type=str, help='''Path to fairseq checkpoint''') parser.add_argument('''--dict_path''', default=None, type=str, help='''Path to dict of fine-tuned model''') parser.add_argument( '''--encoder_config_path''', default='''facebook/wav2vec2-large-lv60''', type=str, help='''Path to hf encoder wav2vec2 checkpoint config''', ) parser.add_argument( '''--decoder_config_path''', default='''facebook/s2t-small-mustc-en-fr-st''', type=str, help='''Path to hf decoder s2t checkpoint config''', ) parser.add_argument('''--vocab_size''', default=10_224, type=int, help='''Vocab size of decoder''') parser.add_argument('''--num_decoder_layers''', default=7, type=int, help='''Number of decoder layers''') _lowerCAmelCase : Union[str, Any] = parser.parse_args() convert_wavaveca_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.dict_path, encoder_config_path=args.encoder_config_path, decoder_config_path=args.decoder_config_path, vocab_size=args.vocab_size, num_decoder_layers=args.num_decoder_layers, )
300
0
from __future__ import annotations def lowercase__ ( __snake_case : int , __snake_case : int ): '''simple docstring''' UpperCAmelCase_ : list[list[int]] = [] create_all_state(1 , __snake_case , __snake_case , [] , __snake_case ) return result def lowercase__ ( __snake_case : int , __snake_case : int , __snake_case : int , __snake_case : list[int] , __snake_case : list[list[int]] , ): '''simple docstring''' if level == 0: total_list.append(current_list[:] ) return for i in range(__snake_case , total_number - level + 2 ): current_list.append(__snake_case ) create_all_state(i + 1 , __snake_case , level - 1 , __snake_case , __snake_case ) current_list.pop() def lowercase__ ( __snake_case : list[list[int]] ): '''simple docstring''' for i in total_list: print(*__snake_case ) if __name__ == "__main__": __UpperCAmelCase = 4 __UpperCAmelCase = 2 __UpperCAmelCase = generate_all_combinations(n, k) print_all_state(total_list)
29
from __future__ import annotations import unittest import numpy as np from transformers import LayoutLMConfig, 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.models.layoutlm.modeling_tf_layoutlm import ( TF_LAYOUTLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFLayoutLMForMaskedLM, TFLayoutLMForQuestionAnswering, TFLayoutLMForSequenceClassification, TFLayoutLMForTokenClassification, TFLayoutLMModel, ) class __magic_name__ : """simple docstring""" def __init__( self :Tuple , snake_case :Optional[Any] , snake_case :Tuple=13 , snake_case :Dict=7 , snake_case :List[Any]=True , snake_case :List[Any]=True , snake_case :Dict=True , snake_case :Any=True , snake_case :Optional[int]=99 , snake_case :Any=32 , snake_case :Dict=2 , snake_case :int=4 , snake_case :Optional[int]=37 , snake_case :List[str]="gelu" , snake_case :List[Any]=0.1 , snake_case :Optional[Any]=0.1 , snake_case :Tuple=512 , snake_case :Tuple=16 , snake_case :Tuple=2 , snake_case :Optional[int]=0.02 , snake_case :str=3 , snake_case :Optional[int]=4 , snake_case :List[str]=None , snake_case :Tuple=1_000 , ): '''simple docstring''' A_ : str = parent A_ : str = batch_size A_ : str = seq_length A_ : Any = is_training A_ : Any = use_input_mask A_ : str = use_token_type_ids A_ : Tuple = use_labels A_ : Optional[Any] = vocab_size A_ : Dict = hidden_size A_ : str = num_hidden_layers A_ : Dict = num_attention_heads A_ : str = intermediate_size A_ : int = hidden_act A_ : List[Any] = hidden_dropout_prob A_ : Dict = attention_probs_dropout_prob A_ : Optional[Any] = max_position_embeddings A_ : List[Any] = type_vocab_size A_ : Any = type_sequence_label_size A_ : Dict = initializer_range A_ : Any = num_labels A_ : Optional[int] = num_choices A_ : Optional[Any] = scope A_ : Any = range_bbox def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' A_ : List[str] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) # convert bbox to numpy since TF does not support item assignment A_ : Tuple = ids_tensor([self.batch_size, self.seq_length, 4] , self.range_bbox ).numpy() # Ensure that bbox is legal for i in range(bbox.shape[0] ): for j in range(bbox.shape[1] ): if bbox[i, j, 3] < bbox[i, j, 1]: A_ : str = bbox[i, j, 3] A_ : Union[str, Any] = bbox[i, j, 1] A_ : List[Any] = t if bbox[i, j, 2] < bbox[i, j, 0]: A_ : Any = bbox[i, j, 2] A_ : Tuple = bbox[i, j, 0] A_ : int = t A_ : int = tf.convert_to_tensor(snake_case ) A_ : Any = None if self.use_input_mask: A_ : Union[str, Any] = random_attention_mask([self.batch_size, self.seq_length] ) A_ : str = None if self.use_token_type_ids: A_ : Union[str, Any] = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) A_ : Dict = None A_ : List[Any] = None A_ : List[str] = None if self.use_labels: A_ : Dict = ids_tensor([self.batch_size] , self.type_sequence_label_size ) A_ : Optional[int] = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) A_ : str = ids_tensor([self.batch_size] , self.num_choices ) A_ : int = LayoutLMConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , ) return config, input_ids, bbox, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def SCREAMING_SNAKE_CASE ( self :str , snake_case :Dict , snake_case :Union[str, Any] , snake_case :int , snake_case :int , snake_case :Union[str, Any] , snake_case :Tuple , snake_case :Optional[int] , snake_case :List[Any] ): '''simple docstring''' A_ : Any = TFLayoutLMModel(config=snake_case ) A_ : Tuple = model(snake_case , snake_case , attention_mask=snake_case , token_type_ids=snake_case ) A_ : str = model(snake_case , snake_case , token_type_ids=snake_case ) A_ : List[Any] = model(snake_case , snake_case ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def SCREAMING_SNAKE_CASE ( self :Optional[int] , snake_case :Any , snake_case :List[Any] , snake_case :List[str] , snake_case :Optional[Any] , snake_case :Dict , snake_case :Any , snake_case :Union[str, Any] , snake_case :List[Any] ): '''simple docstring''' A_ : Optional[int] = TFLayoutLMForMaskedLM(config=snake_case ) A_ : Tuple = model(snake_case , snake_case , attention_mask=snake_case , token_type_ids=snake_case , labels=snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def SCREAMING_SNAKE_CASE ( self :List[str] , snake_case :Dict , snake_case :Tuple , snake_case :Tuple , snake_case :List[str] , snake_case :Tuple , snake_case :str , snake_case :Optional[int] , snake_case :Any ): '''simple docstring''' A_ : Union[str, Any] = self.num_labels A_ : int = TFLayoutLMForSequenceClassification(config=snake_case ) A_ : Optional[int] = model(snake_case , snake_case , attention_mask=snake_case , token_type_ids=snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def SCREAMING_SNAKE_CASE ( self :Optional[Any] , snake_case :Dict , snake_case :str , snake_case :Optional[Any] , snake_case :int , snake_case :Any , snake_case :Tuple , snake_case :List[str] , snake_case :Union[str, Any] ): '''simple docstring''' A_ : List[Any] = self.num_labels A_ : str = TFLayoutLMForTokenClassification(config=snake_case ) A_ : Union[str, Any] = model(snake_case , snake_case , attention_mask=snake_case , token_type_ids=snake_case , labels=snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def SCREAMING_SNAKE_CASE ( self :int , snake_case :List[str] , snake_case :Optional[int] , snake_case :Union[str, Any] , snake_case :List[Any] , snake_case :int , snake_case :Any , snake_case :Union[str, Any] , snake_case :Any ): '''simple docstring''' A_ : Optional[Any] = TFLayoutLMForQuestionAnswering(config=snake_case ) A_ : List[Any] = model(snake_case , snake_case , attention_mask=snake_case , token_type_ids=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 SCREAMING_SNAKE_CASE ( self :Dict ): '''simple docstring''' A_ : int = self.prepare_config_and_inputs() ( ( A_ ) , ( A_ ) , ( A_ ) , ( A_ ) , ( A_ ) , ( A_ ) , ( A_ ) , ( A_ ) , ) : Union[str, Any] = config_and_inputs A_ : Optional[Any] = { "input_ids": input_ids, "bbox": bbox, "token_type_ids": token_type_ids, "attention_mask": input_mask, } return config, inputs_dict @require_tf class __magic_name__ ( lowerCamelCase__ , lowerCamelCase__ , unittest.TestCase ): """simple docstring""" __UpperCamelCase = ( ( TFLayoutLMModel, TFLayoutLMForMaskedLM, TFLayoutLMForTokenClassification, TFLayoutLMForSequenceClassification, TFLayoutLMForQuestionAnswering, ) if is_tf_available() else () ) __UpperCamelCase = ( { '''feature-extraction''': TFLayoutLMModel, '''fill-mask''': TFLayoutLMForMaskedLM, '''text-classification''': TFLayoutLMForSequenceClassification, '''token-classification''': TFLayoutLMForTokenClassification, '''zero-shot''': TFLayoutLMForSequenceClassification, } if is_tf_available() else {} ) __UpperCamelCase = False __UpperCamelCase = True __UpperCamelCase = 10 def SCREAMING_SNAKE_CASE ( self :Dict ): '''simple docstring''' A_ : Tuple = TFLayoutLMModelTester(self ) A_ : List[Any] = ConfigTester(self , config_class=snake_case , hidden_size=37 ) def SCREAMING_SNAKE_CASE ( self :Tuple ): '''simple docstring''' self.config_tester.run_common_tests() def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' A_ : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case ) def SCREAMING_SNAKE_CASE ( self :Optional[int] ): '''simple docstring''' A_ : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*snake_case ) def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' A_ : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*snake_case ) def SCREAMING_SNAKE_CASE ( self :Tuple ): '''simple docstring''' A_ : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*snake_case ) def SCREAMING_SNAKE_CASE ( self :List[Any] ): '''simple docstring''' A_ : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*snake_case ) @slow def SCREAMING_SNAKE_CASE ( self :Optional[Any] ): '''simple docstring''' for model_name in TF_LAYOUTLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A_ : List[str] = TFLayoutLMModel.from_pretrained(snake_case ) self.assertIsNotNone(snake_case ) @unittest.skip("Onnx compliancy broke with TF 2.10" ) def SCREAMING_SNAKE_CASE ( self :Dict ): '''simple docstring''' pass def __snake_case ( ) -> Optional[Any]: # Here we prepare a batch of 2 sequences to test a LayoutLM forward pass on: # fmt: off A_ : int = tf.convert_to_tensor([[101,1019,1014,1016,1037,12849,4747,1004,14246,2278,5439,4524,5002,2930,2193,2930,4341,3208,1005,1055,2171,2848,11300,3531,102],[101,4070,4034,7020,1024,3058,1015,1013,2861,1013,6070,19274,2772,6205,27814,16147,16147,4343,2047,10283,10969,14389,1012,2338,102]] ) # noqa: E231 A_ : int = tf.convert_to_tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],] ) # noqa: E231 A_ : Union[str, Any] = tf.convert_to_tensor([[[0,0,0,0],[423,237,440,251],[427,272,441,287],[419,115,437,129],[961,885,992,912],[256,38,330,58],[256,38,330,58],[336,42,353,57],[360,39,401,56],[360,39,401,56],[411,39,471,59],[479,41,528,59],[533,39,630,60],[67,113,134,131],[141,115,209,132],[68,149,133,166],[141,149,187,164],[195,148,287,165],[195,148,287,165],[195,148,287,165],[295,148,349,165],[441,149,492,166],[497,149,546,164],[64,201,125,218],[1000,1000,1000,1000]],[[0,0,0,0],[662,150,754,166],[665,199,742,211],[519,213,554,228],[519,213,554,228],[134,433,187,454],[130,467,204,480],[130,467,204,480],[130,467,204,480],[130,467,204,480],[130,467,204,480],[314,469,376,482],[504,684,582,706],[941,825,973,900],[941,825,973,900],[941,825,973,900],[941,825,973,900],[610,749,652,765],[130,659,168,672],[176,657,237,672],[238,657,312,672],[443,653,628,672],[443,653,628,672],[716,301,825,317],[1000,1000,1000,1000]]] ) # noqa: E231 A_ : List[Any] = tf.convert_to_tensor([[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]] ) # noqa: E231 # these are sequence labels (i.e. at the token level) A_ : Tuple = tf.convert_to_tensor([[-100,10,10,10,9,1,-100,7,7,-100,7,7,4,2,5,2,8,8,-100,-100,5,0,3,2,-100],[-100,12,12,12,-100,12,10,-100,-100,-100,-100,10,12,9,-100,-100,-100,10,10,10,9,12,-100,10,-100]] ) # noqa: E231 # fmt: on return input_ids, attention_mask, bbox, token_type_ids, labels @require_tf class __magic_name__ ( unittest.TestCase ): """simple docstring""" @slow def SCREAMING_SNAKE_CASE ( self :Tuple ): '''simple docstring''' A_ : str = TFLayoutLMModel.from_pretrained("microsoft/layoutlm-base-uncased" ) A_ , A_ , A_ , A_ , A_ : Tuple = prepare_layoutlm_batch_inputs() # forward pass A_ : Tuple = model(input_ids=snake_case , bbox=snake_case , attention_mask=snake_case , token_type_ids=snake_case ) # test the sequence output on [0, :3, :3] A_ : List[Any] = tf.convert_to_tensor( [[0.1785, -0.1947, -0.0425], [-0.3254, -0.2807, 0.2553], [-0.5391, -0.3322, 0.3364]] , ) self.assertTrue(np.allclose(outputs.last_hidden_state[0, :3, :3] , snake_case , atol=1e-3 ) ) # test the pooled output on [1, :3] A_ : Optional[Any] = tf.convert_to_tensor([-0.6580, -0.0214, 0.8552] ) self.assertTrue(np.allclose(outputs.pooler_output[1, :3] , snake_case , atol=1e-3 ) ) @slow def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' A_ : Union[str, Any] = TFLayoutLMForSequenceClassification.from_pretrained("microsoft/layoutlm-base-uncased" , num_labels=2 ) A_ , A_ , A_ , A_ , A_ : Any = prepare_layoutlm_batch_inputs() # forward pass A_ : Dict = model( input_ids=snake_case , bbox=snake_case , attention_mask=snake_case , token_type_ids=snake_case , labels=tf.convert_to_tensor([1, 1] ) , ) # test whether we get a loss as a scalar A_ : List[str] = outputs.loss A_ : Union[str, Any] = (2,) self.assertEqual(loss.shape , snake_case ) # test the shape of the logits A_ : Tuple = outputs.logits A_ : Tuple = (2, 2) self.assertEqual(logits.shape , snake_case ) @slow def SCREAMING_SNAKE_CASE ( self :Optional[int] ): '''simple docstring''' A_ : int = TFLayoutLMForTokenClassification.from_pretrained("microsoft/layoutlm-base-uncased" , num_labels=13 ) A_ , A_ , A_ , A_ , A_ : Optional[int] = prepare_layoutlm_batch_inputs() # forward pass A_ : Union[str, Any] = model( input_ids=snake_case , bbox=snake_case , attention_mask=snake_case , token_type_ids=snake_case , labels=snake_case ) # test the shape of the logits A_ : Dict = outputs.logits A_ : List[Any] = tf.convert_to_tensor((2, 25, 13) ) self.assertEqual(logits.shape , snake_case ) @slow def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' A_ : Optional[Any] = TFLayoutLMForQuestionAnswering.from_pretrained("microsoft/layoutlm-base-uncased" ) A_ , A_ , A_ , A_ , A_ : str = prepare_layoutlm_batch_inputs() # forward pass A_ : Union[str, Any] = model(input_ids=snake_case , bbox=snake_case , attention_mask=snake_case , token_type_ids=snake_case ) # test the shape of the logits A_ : Union[str, Any] = tf.convert_to_tensor((2, 25) ) self.assertEqual(outputs.start_logits.shape , snake_case ) self.assertEqual(outputs.end_logits.shape , snake_case )
300
0
import collections import os import re from pathlib import Path __a = 'src/transformers' # Matches is_xxx_available() __a = re.compile(r'is\_([a-z_]*)_available()') # Catches a one-line _import_struct = {xxx} __a = re.compile(r'^_import_structure\s+=\s+\{([^\}]+)\}') # Catches a line with a key-values pattern: "bla": ["foo", "bar"] __a = re.compile(r'\s+"\S*":\s+\[([^\]]*)\]') # Catches a line if not is_foo_available __a = re.compile(r'^\s*if\s+not\s+is\_[a-z_]*\_available\(\)') # Catches a line _import_struct["bla"].append("foo") __a = re.compile(r'^\s*_import_structure\["\S*"\]\.append\("(\S*)"\)') # Catches a line _import_struct["bla"].extend(["foo", "bar"]) or _import_struct["bla"] = ["foo", "bar"] __a = re.compile(r'^\s*_import_structure\[\S*\](?:\.extend\(|\s*=\s+)\[([^\]]*)\]') # Catches a line with an object between quotes and a comma: "MyModel", __a = re.compile(r'^\s+"([^"]+)",') # Catches a line with objects between brackets only: ["foo", "bar"], __a = re.compile(r'^\s+\[([^\]]+)\]') # Catches a line with from foo import bar, bla, boo __a = re.compile(r'\s+from\s+\S*\s+import\s+([^\(\s].*)\n') # Catches a line with try: __a = re.compile(r'^\s*try:') # Catches a line with else: __a = re.compile(r'^\s*else:') def a ( snake_case__: Union[str, Any] ): '''simple docstring''' if _re_test_backend.search(snake_case__ ) is None: return None lowercase_ = [b[0] for b in _re_backend.findall(snake_case__ )] backends.sort() return "_and_".join(snake_case__ ) def a ( snake_case__: Optional[Any] ): '''simple docstring''' with open(snake_case__ , '''r''' , encoding='''utf-8''' , newline='''\n''' ) as f: lowercase_ = f.readlines() lowercase_ = 0 while line_index < len(snake_case__ ) and not lines[line_index].startswith('''_import_structure = {''' ): line_index += 1 # If this is a traditional init, just return. if line_index >= len(snake_case__ ): return None # First grab the objects without a specific backend in _import_structure lowercase_ = [] while not lines[line_index].startswith('''if TYPE_CHECKING''' ) and find_backend(lines[line_index] ) is None: lowercase_ = lines[line_index] # If we have everything on a single line, let's deal with it. if _re_one_line_import_struct.search(snake_case__ ): lowercase_ = _re_one_line_import_struct.search(snake_case__ ).groups()[0] lowercase_ = re.findall(r'''\[([^\]]+)\]''' , snake_case__ ) for imp in imports: objects.extend([obj[1:-1] for obj in imp.split(''', ''' )] ) line_index += 1 continue lowercase_ = _re_import_struct_key_value.search(snake_case__ ) if single_line_import_search is not None: lowercase_ = [obj[1:-1] for obj in single_line_import_search.groups()[0].split(''', ''' ) if len(snake_case__ ) > 0] objects.extend(snake_case__ ) elif line.startswith(''' ''' * 8 + '''"''' ): objects.append(line[9:-3] ) line_index += 1 lowercase_ = {'''none''': objects} # Let's continue with backend-specific objects in _import_structure while not lines[line_index].startswith('''if TYPE_CHECKING''' ): # If the line is an if not is_backend_available, we grab all objects associated. lowercase_ = find_backend(lines[line_index] ) # Check if the backend declaration is inside a try block: if _re_try.search(lines[line_index - 1] ) is None: lowercase_ = None if backend is not None: line_index += 1 # Scroll until we hit the else block of try-except-else while _re_else.search(lines[line_index] ) is None: line_index += 1 line_index += 1 lowercase_ = [] # Until we unindent, add backend objects to the list while len(lines[line_index] ) <= 1 or lines[line_index].startswith(''' ''' * 4 ): lowercase_ = lines[line_index] if _re_import_struct_add_one.search(snake_case__ ) is not None: objects.append(_re_import_struct_add_one.search(snake_case__ ).groups()[0] ) elif _re_import_struct_add_many.search(snake_case__ ) is not None: lowercase_ = _re_import_struct_add_many.search(snake_case__ ).groups()[0].split(''', ''' ) lowercase_ = [obj[1:-1] for obj in imports if len(snake_case__ ) > 0] objects.extend(snake_case__ ) elif _re_between_brackets.search(snake_case__ ) is not None: lowercase_ = _re_between_brackets.search(snake_case__ ).groups()[0].split(''', ''' ) lowercase_ = [obj[1:-1] for obj in imports if len(snake_case__ ) > 0] objects.extend(snake_case__ ) elif _re_quote_object.search(snake_case__ ) is not None: objects.append(_re_quote_object.search(snake_case__ ).groups()[0] ) elif line.startswith(''' ''' * 8 + '''"''' ): objects.append(line[9:-3] ) elif line.startswith(''' ''' * 12 + '''"''' ): objects.append(line[13:-3] ) line_index += 1 lowercase_ = objects else: line_index += 1 # At this stage we are in the TYPE_CHECKING part, first grab the objects without a specific backend lowercase_ = [] while ( line_index < len(snake_case__ ) and find_backend(lines[line_index] ) is None and not lines[line_index].startswith('''else''' ) ): lowercase_ = lines[line_index] lowercase_ = _re_import.search(snake_case__ ) if single_line_import_search is not None: objects.extend(single_line_import_search.groups()[0].split(''', ''' ) ) elif line.startswith(''' ''' * 8 ): objects.append(line[8:-2] ) line_index += 1 lowercase_ = {'''none''': objects} # Let's continue with backend-specific objects while line_index < len(snake_case__ ): # If the line is an if is_backend_available, we grab all objects associated. lowercase_ = find_backend(lines[line_index] ) # Check if the backend declaration is inside a try block: if _re_try.search(lines[line_index - 1] ) is None: lowercase_ = None if backend is not None: line_index += 1 # Scroll until we hit the else block of try-except-else while _re_else.search(lines[line_index] ) is None: line_index += 1 line_index += 1 lowercase_ = [] # Until we unindent, add backend objects to the list while len(lines[line_index] ) <= 1 or lines[line_index].startswith(''' ''' * 8 ): lowercase_ = lines[line_index] lowercase_ = _re_import.search(snake_case__ ) if single_line_import_search is not None: objects.extend(single_line_import_search.groups()[0].split(''', ''' ) ) elif line.startswith(''' ''' * 12 ): objects.append(line[12:-2] ) line_index += 1 lowercase_ = objects else: line_index += 1 return import_dict_objects, type_hint_objects def a ( snake_case__: Any , snake_case__: Tuple ): '''simple docstring''' def find_duplicates(snake_case__: Optional[int] ): return [k for k, v in collections.Counter(snake_case__ ).items() if v > 1] if list(import_dict_objects.keys() ) != list(type_hint_objects.keys() ): return ["Both sides of the init do not have the same backends!"] lowercase_ = [] for key in import_dict_objects.keys(): lowercase_ = find_duplicates(import_dict_objects[key] ) if duplicate_imports: errors.append(F'''Duplicate _import_structure definitions for: {duplicate_imports}''' ) lowercase_ = find_duplicates(type_hint_objects[key] ) if duplicate_type_hints: errors.append(F'''Duplicate TYPE_CHECKING objects for: {duplicate_type_hints}''' ) if sorted(set(import_dict_objects[key] ) ) != sorted(set(type_hint_objects[key] ) ): lowercase_ = '''base imports''' if key == '''none''' else F'''{key} backend''' errors.append(F'''Differences for {name}:''' ) for a in type_hint_objects[key]: if a not in import_dict_objects[key]: errors.append(F''' {a} in TYPE_HINT but not in _import_structure.''' ) for a in import_dict_objects[key]: if a not in type_hint_objects[key]: errors.append(F''' {a} in _import_structure but not in TYPE_HINT.''' ) return errors def a ( ): '''simple docstring''' lowercase_ = [] for root, _, files in os.walk(snake_case__ ): if "__init__.py" in files: lowercase_ = os.path.join(snake_case__ , '''__init__.py''' ) lowercase_ = parse_init(snake_case__ ) if objects is not None: lowercase_ = analyze_results(*snake_case__ ) if len(snake_case__ ) > 0: lowercase_ = F'''Problem in {fname}, both halves do not define the same objects.\n{errors[0]}''' failures.append('''\n'''.join(snake_case__ ) ) if len(snake_case__ ) > 0: raise ValueError('''\n\n'''.join(snake_case__ ) ) def a ( ): '''simple docstring''' lowercase_ = [] for path, directories, files in os.walk(snake_case__ ): for folder in directories: # Ignore private modules if folder.startswith('''_''' ): directories.remove(snake_case__ ) continue # Ignore leftovers from branches (empty folders apart from pycache) if len(list((Path(snake_case__ ) / folder).glob('''*.py''' ) ) ) == 0: continue lowercase_ = str((Path(snake_case__ ) / folder).relative_to(snake_case__ ) ) lowercase_ = short_path.replace(os.path.sep , '''.''' ) submodules.append(snake_case__ ) for fname in files: if fname == "__init__.py": continue lowercase_ = str((Path(snake_case__ ) / fname).relative_to(snake_case__ ) ) lowercase_ = short_path.replace('''.py''' , '''''' ).replace(os.path.sep , '''.''' ) if len(submodule.split('''.''' ) ) == 1: submodules.append(snake_case__ ) return submodules __a = [ 'convert_pytorch_checkpoint_to_tf2', 'modeling_flax_pytorch_utils', 'models.esm.openfold_utils', ] def a ( ): '''simple docstring''' # This is to make sure the transformers module imported is the one in the repo. from transformers.utils import direct_transformers_import lowercase_ = direct_transformers_import(snake_case__ ) lowercase_ = set(transformers._import_structure.keys() ) # This contains all the base keys of the _import_structure object defined in the init, but if the user is missing # some optional dependencies, they may not have all of them. Thus we read the init to read all additions and # (potentiall re-) add them. with open(os.path.join(snake_case__ , '''__init__.py''' ) , '''r''' ) as f: lowercase_ = f.read() import_structure_keys.update(set(re.findall(r'''import_structure\[\"([^\"]*)\"\]''' , snake_case__ ) ) ) lowercase_ = [ module for module in get_transformers_submodules() if module not in IGNORE_SUBMODULES and module not in import_structure_keys ] if len(snake_case__ ) > 0: lowercase_ = '''\n'''.join(F'''- {module}''' for module in module_not_registered ) raise ValueError( '''The following submodules are not properly registed in the main init of Transformers:\n''' F'''{list_of_modules}\n''' '''Make sure they appear somewhere in the keys of `_import_structure` with an empty list as value.''' ) if __name__ == "__main__": check_all_inits() check_submodules()
30
# Copyright 2023 The HuggingFace Inc. 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. import re from ..utils import cached_file # docstyle-ignore _lowerCAmelCase : Optional[int] = ''' Human: <<task>> Assistant: ''' _lowerCAmelCase : int = '''huggingface-tools/default-prompts''' _lowerCAmelCase : Any = {'''chat''': '''chat_prompt_template.txt''', '''run''': '''run_prompt_template.txt'''} def __snake_case ( _lowerCAmelCase : str , _lowerCAmelCase : List[Any] , _lowerCAmelCase : Dict="run" ) -> List[Any]: if prompt_or_repo_id is None: A_ : Optional[int] = DEFAULT_PROMPTS_REPO # prompt is considered a repo ID when it does not contain any kind of space if re.search("\\s" , _lowerCAmelCase ) is not None: return prompt_or_repo_id A_ : Optional[Any] = cached_file( _lowerCAmelCase , PROMPT_FILES[mode] , repo_type="dataset" , user_agent={"agent": agent_name} ) with open(_lowerCAmelCase , "r" , encoding="utf-8" ) as f: return f.read()
300
0
'''simple docstring''' from __future__ import annotations __SCREAMING_SNAKE_CASE : Dict = """#""" class lowerCamelCase_ : '''simple docstring''' def __init__( self : Union[str, Any] ): _UpperCAmelCase : dict = {} def _A ( self : int , A : str ): _UpperCAmelCase : Optional[Any] = self._trie for char in text: if char not in trie: _UpperCAmelCase : Dict = {} _UpperCAmelCase : Tuple = trie[char] _UpperCAmelCase : Tuple = True def _A ( self : Optional[Any] , A : str ): _UpperCAmelCase : str = self._trie for char in prefix: if char in trie: _UpperCAmelCase : Optional[int] = trie[char] else: return [] return self._elements(A ) def _A ( self : str , A : dict ): _UpperCAmelCase : Tuple = [] for c, v in d.items(): _UpperCAmelCase : List[Any] = [" "] if c == END else [(c + s) for s in self._elements(A )] result.extend(A ) return tuple(A ) __SCREAMING_SNAKE_CASE : Any = Trie() __SCREAMING_SNAKE_CASE : Optional[Any] = ("""depart""", """detergent""", """daring""", """dog""", """deer""", """deal""") for word in words: trie.insert_word(word) def UpperCamelCase_ ( _UpperCAmelCase : str ) -> tuple: """simple docstring""" _UpperCAmelCase : List[Any] = trie.find_word(_UpperCAmelCase ) return tuple(string + word for word in suffixes ) def UpperCamelCase_ ( ) -> None: """simple docstring""" print(autocomplete_using_trie("de" ) ) if __name__ == "__main__": import doctest doctest.testmod() main()
31
def __snake_case ( _lowerCAmelCase : list ) -> list: if len(_lowerCAmelCase ) <= 1: return [tuple(_lowerCAmelCase )] A_ : Tuple = [] def generate(_lowerCAmelCase : int , _lowerCAmelCase : list ): A_ : List[str] = [0] * n res.append(tuple(_lowerCAmelCase ) ) A_ : int = 0 while i < n: if c[i] < i: if i % 2 == 0: A_ , A_ : str = arr[i], arr[0] else: A_ , A_ : List[str] = arr[i], arr[c[i]] res.append(tuple(_lowerCAmelCase ) ) c[i] += 1 A_ : Tuple = 0 else: A_ : Dict = 0 i += 1 generate(len(_lowerCAmelCase ) , _lowerCAmelCase ) return res if __name__ == "__main__": _lowerCAmelCase : str = input('''Enter numbers separated by a comma:\n''').strip() _lowerCAmelCase : str = [int(item) for item in user_input.split(''',''')] print(heaps(arr))
300
0
import argparse import pytorch_lightning as pl import torch from torch import nn from transformers import LongformerForQuestionAnswering, LongformerModel class SCREAMING_SNAKE_CASE__ ( pl.LightningModule ): def __init__( self : List[str] , SCREAMING_SNAKE_CASE__ : Optional[Any] ) -> Dict: super().__init__() a_ : Union[str, Any] = model a_ : int = 2 a_ : Optional[int] = nn.Linear(self.model.config.hidden_size , self.num_labels ) def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Dict: pass def SCREAMING_SNAKE_CASE_ ( __A : str , __A : str , __A : str ) -> int: """simple docstring""" a_ : Optional[Any] = LongformerModel.from_pretrained(__A ) a_ : Optional[int] = LightningModel(__A ) a_ : Union[str, Any] = torch.load(__A , map_location=torch.device('cpu' ) ) lightning_model.load_state_dict(ckpt['state_dict'] ) # init longformer question answering model a_ : Optional[Any] = LongformerForQuestionAnswering.from_pretrained(__A ) # transfer weights longformer_for_qa.longformer.load_state_dict(lightning_model.model.state_dict() ) longformer_for_qa.qa_outputs.load_state_dict(lightning_model.qa_outputs.state_dict() ) longformer_for_qa.eval() # save model longformer_for_qa.save_pretrained(__A ) print(F"""Conversion successful. Model saved under {pytorch_dump_folder_path}""" ) if __name__ == "__main__": UpperCAmelCase_ : Optional[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '--longformer_model', default=None, type=str, required=True, help='model identifier of longformer. Should be either `longformer-base-4096` or `longformer-large-4096`.', ) parser.add_argument( '--longformer_question_answering_ckpt_path', default=None, type=str, required=True, help='Path the official PyTorch Lightning Checkpoint.', ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, required=True, help='Path to the output PyTorch model.' ) UpperCAmelCase_ : Optional[int] = parser.parse_args() convert_longformer_qa_checkpoint_to_pytorch( args.longformer_model, args.longformer_question_answering_ckpt_path, args.pytorch_dump_folder_path )
32
import json from typing import List, Optional, Tuple from tokenizers import pre_tokenizers, processors from ...tokenization_utils_base import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_roberta import RobertaTokenizer _lowerCAmelCase : int = logging.get_logger(__name__) _lowerCAmelCase : Optional[int] = {'''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt''', '''tokenizer_file''': '''tokenizer.json'''} _lowerCAmelCase : List[Any] = { '''vocab_file''': { '''roberta-base''': '''https://huggingface.co/roberta-base/resolve/main/vocab.json''', '''roberta-large''': '''https://huggingface.co/roberta-large/resolve/main/vocab.json''', '''roberta-large-mnli''': '''https://huggingface.co/roberta-large-mnli/resolve/main/vocab.json''', '''distilroberta-base''': '''https://huggingface.co/distilroberta-base/resolve/main/vocab.json''', '''roberta-base-openai-detector''': '''https://huggingface.co/roberta-base-openai-detector/resolve/main/vocab.json''', '''roberta-large-openai-detector''': ( '''https://huggingface.co/roberta-large-openai-detector/resolve/main/vocab.json''' ), }, '''merges_file''': { '''roberta-base''': '''https://huggingface.co/roberta-base/resolve/main/merges.txt''', '''roberta-large''': '''https://huggingface.co/roberta-large/resolve/main/merges.txt''', '''roberta-large-mnli''': '''https://huggingface.co/roberta-large-mnli/resolve/main/merges.txt''', '''distilroberta-base''': '''https://huggingface.co/distilroberta-base/resolve/main/merges.txt''', '''roberta-base-openai-detector''': '''https://huggingface.co/roberta-base-openai-detector/resolve/main/merges.txt''', '''roberta-large-openai-detector''': ( '''https://huggingface.co/roberta-large-openai-detector/resolve/main/merges.txt''' ), }, '''tokenizer_file''': { '''roberta-base''': '''https://huggingface.co/roberta-base/resolve/main/tokenizer.json''', '''roberta-large''': '''https://huggingface.co/roberta-large/resolve/main/tokenizer.json''', '''roberta-large-mnli''': '''https://huggingface.co/roberta-large-mnli/resolve/main/tokenizer.json''', '''distilroberta-base''': '''https://huggingface.co/distilroberta-base/resolve/main/tokenizer.json''', '''roberta-base-openai-detector''': ( '''https://huggingface.co/roberta-base-openai-detector/resolve/main/tokenizer.json''' ), '''roberta-large-openai-detector''': ( '''https://huggingface.co/roberta-large-openai-detector/resolve/main/tokenizer.json''' ), }, } _lowerCAmelCase : Any = { '''roberta-base''': 512, '''roberta-large''': 512, '''roberta-large-mnli''': 512, '''distilroberta-base''': 512, '''roberta-base-openai-detector''': 512, '''roberta-large-openai-detector''': 512, } class __magic_name__ ( lowerCamelCase__ ): """simple docstring""" __UpperCamelCase = VOCAB_FILES_NAMES __UpperCamelCase = PRETRAINED_VOCAB_FILES_MAP __UpperCamelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCamelCase = ['''input_ids''', '''attention_mask'''] __UpperCamelCase = RobertaTokenizer def __init__( self :Dict , snake_case :List[str]=None , snake_case :List[Any]=None , snake_case :Union[str, Any]=None , snake_case :List[str]="replace" , snake_case :Tuple="<s>" , snake_case :Union[str, Any]="</s>" , snake_case :str="</s>" , snake_case :Union[str, Any]="<s>" , snake_case :int="<unk>" , snake_case :Tuple="<pad>" , snake_case :List[str]="<mask>" , snake_case :Any=False , snake_case :Union[str, Any]=True , **snake_case :Optional[int] , ): '''simple docstring''' super().__init__( snake_case , snake_case , tokenizer_file=snake_case , errors=snake_case , bos_token=snake_case , eos_token=snake_case , sep_token=snake_case , cls_token=snake_case , unk_token=snake_case , pad_token=snake_case , mask_token=snake_case , add_prefix_space=snake_case , trim_offsets=snake_case , **snake_case , ) A_ : Optional[Any] = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get("add_prefix_space" , snake_case ) != add_prefix_space: A_ : Dict = getattr(snake_case , pre_tok_state.pop("type" ) ) A_ : Optional[int] = add_prefix_space A_ : int = pre_tok_class(**snake_case ) A_ : Optional[int] = add_prefix_space A_ : Optional[int] = "post_processor" A_ : Dict = getattr(self.backend_tokenizer , snake_case , snake_case ) if tokenizer_component_instance: A_ : Dict = json.loads(tokenizer_component_instance.__getstate__() ) # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class` if "sep" in state: A_ : List[Any] = tuple(state["sep"] ) if "cls" in state: A_ : Optional[Any] = tuple(state["cls"] ) A_ : Tuple = False if state.get("add_prefix_space" , snake_case ) != add_prefix_space: A_ : List[Any] = add_prefix_space A_ : Optional[int] = True if state.get("trim_offsets" , snake_case ) != trim_offsets: A_ : List[str] = trim_offsets A_ : Any = True if changes_to_apply: A_ : Optional[Any] = getattr(snake_case , state.pop("type" ) ) A_ : Any = component_class(**snake_case ) setattr(self.backend_tokenizer , snake_case , snake_case ) @property def SCREAMING_SNAKE_CASE ( self :List[Any] ): '''simple docstring''' if self._mask_token is None: if self.verbose: logger.error("Using mask_token, but it is not set yet." ) return None return str(self._mask_token ) @mask_token.setter def SCREAMING_SNAKE_CASE ( self :Any , snake_case :Dict ): '''simple docstring''' A_ : Dict = AddedToken(snake_case , lstrip=snake_case , rstrip=snake_case ) if isinstance(snake_case , snake_case ) else value A_ : Any = value def SCREAMING_SNAKE_CASE ( self :Dict , *snake_case :Tuple , **snake_case :Union[str, Any] ): '''simple docstring''' A_ : Any = kwargs.get("is_split_into_words" , snake_case ) assert self.add_prefix_space or not is_split_into_words, ( f"You need to instantiate {self.__class__.__name__} with add_prefix_space=True " "to use it with pretokenized inputs." ) return super()._batch_encode_plus(*snake_case , **snake_case ) def SCREAMING_SNAKE_CASE ( self :List[str] , *snake_case :str , **snake_case :Union[str, Any] ): '''simple docstring''' A_ : Any = kwargs.get("is_split_into_words" , snake_case ) assert self.add_prefix_space or not is_split_into_words, ( f"You need to instantiate {self.__class__.__name__} with add_prefix_space=True " "to use it with pretokenized inputs." ) return super()._encode_plus(*snake_case , **snake_case ) def SCREAMING_SNAKE_CASE ( self :Union[str, Any] , snake_case :str , snake_case :Optional[str] = None ): '''simple docstring''' A_ : str = self._tokenizer.model.save(snake_case , name=snake_case ) return tuple(snake_case ) def SCREAMING_SNAKE_CASE ( self :List[str] , snake_case :List[str] , snake_case :Optional[Any]=None ): '''simple docstring''' A_ : int = [self.bos_token_id] + token_ids_a + [self.eos_token_id] if token_ids_a is None: return output return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id] def SCREAMING_SNAKE_CASE ( self :Any , snake_case :List[int] , snake_case :Optional[List[int]] = None ): '''simple docstring''' A_ : Any = [self.sep_token_id] A_ : Optional[Any] = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0]
300
0
"""simple docstring""" import logging from transformers.configuration_utils import PretrainedConfig __A : Any = logging.getLogger(__name__) class _UpperCAmelCase ( _A ): SCREAMING_SNAKE_CASE_ : Tuple = "masked_bert" def __init__( self : Optional[int] , A : int=3_05_22 , A : int=7_68 , A : List[Any]=12 , A : Union[str, Any]=12 , A : List[str]=30_72 , A : Dict="gelu" , A : Any=0.1 , A : int=0.1 , A : Optional[Any]=5_12 , A : Union[str, Any]=2 , A : Any=0.02 , A : str=1e-12 , A : Optional[int]=0 , A : Union[str, Any]="topK" , A : Union[str, Any]="constant" , A : Optional[int]=0.0 , **A : List[str] , ) -> int: super().__init__(pad_token_id=A , **A ) lowercase_ : str = vocab_size lowercase_ : List[str] = hidden_size lowercase_ : List[Any] = num_hidden_layers lowercase_ : int = num_attention_heads lowercase_ : Union[str, Any] = hidden_act lowercase_ : Any = intermediate_size lowercase_ : Tuple = hidden_dropout_prob lowercase_ : Optional[Any] = attention_probs_dropout_prob lowercase_ : Tuple = max_position_embeddings lowercase_ : Union[str, Any] = type_vocab_size lowercase_ : Optional[int] = initializer_range lowercase_ : List[Any] = layer_norm_eps lowercase_ : Any = pruning_method lowercase_ : Dict = mask_init lowercase_ : Optional[Any] = mask_scale
33
from typing import Dict, List from nltk.translate import gleu_score import datasets from datasets import MetricInfo _lowerCAmelCase : int = '''\ @misc{wu2016googles, title={Google\'s Neural Machine Translation System: Bridging the Gap between Human and Machine Translation}, author={Yonghui Wu and Mike Schuster and Zhifeng Chen and Quoc V. Le and Mohammad Norouzi and Wolfgang Macherey and Maxim Krikun and Yuan Cao and Qin Gao and Klaus Macherey and Jeff Klingner and Apurva Shah and Melvin Johnson and Xiaobing Liu and Łukasz Kaiser and Stephan Gouws and Yoshikiyo Kato and Taku Kudo and Hideto Kazawa and Keith Stevens and George Kurian and Nishant Patil and Wei Wang and Cliff Young and Jason Smith and Jason Riesa and Alex Rudnick and Oriol Vinyals and Greg Corrado and Macduff Hughes and Jeffrey Dean}, year={2016}, eprint={1609.08144}, archivePrefix={arXiv}, primaryClass={cs.CL} } ''' _lowerCAmelCase : Tuple = '''\ The BLEU score has some undesirable properties when used for single sentences, as it was designed to be a corpus measure. We therefore use a slightly different score for our RL experiments which we call the \'GLEU score\'. For the GLEU score, we record all sub-sequences of 1, 2, 3 or 4 tokens in output and target sequence (n-grams). We then compute a recall, which is the ratio of the number of matching n-grams to the number of total n-grams in the target (ground truth) sequence, and a precision, which is the ratio of the number of matching n-grams to the number of total n-grams in the generated output sequence. Then GLEU score is simply the minimum of recall and precision. This GLEU score\'s range is always between 0 (no matches) and 1 (all match) and it is symmetrical when switching output and target. According to our experiments, GLEU score correlates quite well with the BLEU metric on a corpus level but does not have its drawbacks for our per sentence reward objective. ''' _lowerCAmelCase : int = '''\ Computes corpus-level Google BLEU (GLEU) score of translated segments against one or more references. Instead of averaging the sentence level GLEU scores (i.e. macro-average precision), Wu et al. (2016) sum up the matching tokens and the max of hypothesis and reference tokens for each sentence, then compute using the aggregate values. Args: predictions (list of str): list of translations to score. Each translation should be tokenized into a list of tokens. references (list of list of str): list of lists of references for each translation. Each reference should be tokenized into a list of tokens. min_len (int): The minimum order of n-gram this function should extract. Defaults to 1. max_len (int): The maximum order of n-gram this function should extract. Defaults to 4. Returns: \'google_bleu\': google_bleu score Examples: Example 1: >>> hyp1 = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'which\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'always\', ... \'disobeys\', \'the\', \'commands\', \'of\', \'the\', \'cat\'] >>> ref1a = [\'It\', \'is\', \'the\', \'guiding\', \'principle\', \'which\', ... \'guarantees\', \'the\', \'rubber\', \'duck\', \'forces\', \'never\', ... \'being\', \'under\', \'the\', \'command\', \'of\', \'the\', \'cat\'] >>> hyp2 = [\'he\', \'read\', \'the\', \'book\', \'because\', \'he\', \'was\', ... \'interested\', \'in\', \'world\', \'history\'] >>> ref2a = [\'he\', \'was\', \'interested\', \'in\', \'world\', \'history\', ... \'because\', \'he\', \'read\', \'the\', \'book\'] >>> list_of_references = [[ref1a], [ref2a]] >>> hypotheses = [hyp1, hyp2] >>> google_bleu = datasets.load_metric("google_bleu") >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references) >>> print(round(results["google_bleu"], 2)) 0.44 Example 2: >>> hyp1 = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'which\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'always\', ... \'disobeys\', \'the\', \'commands\', \'of\', \'the\', \'cat\'] >>> ref1a = [\'It\', \'is\', \'the\', \'guiding\', \'principle\', \'which\', ... \'guarantees\', \'the\', \'rubber\', \'duck\', \'forces\', \'never\', ... \'being\', \'under\', \'the\', \'command\', \'of\', \'the\', \'cat\'] >>> ref1b = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'that\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'will\', \'never\', ... \'heed\', \'the\', \'cat\', \'commands\'] >>> ref1c = [\'It\', \'is\', \'the\', \'practical\', \'guide\', \'for\', \'the\', ... \'rubber\', \'duck\', \'army\', \'never\', \'to\', \'heed\', \'the\', \'directions\', ... \'of\', \'the\', \'cat\'] >>> hyp2 = [\'he\', \'read\', \'the\', \'book\', \'because\', \'he\', \'was\', ... \'interested\', \'in\', \'world\', \'history\'] >>> ref2a = [\'he\', \'was\', \'interested\', \'in\', \'world\', \'history\', ... \'because\', \'he\', \'read\', \'the\', \'book\'] >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]] >>> hypotheses = [hyp1, hyp2] >>> google_bleu = datasets.load_metric("google_bleu") >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references) >>> print(round(results["google_bleu"], 2)) 0.61 Example 3: >>> hyp1 = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'which\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'always\', ... \'disobeys\', \'the\', \'commands\', \'of\', \'the\', \'cat\'] >>> ref1a = [\'It\', \'is\', \'the\', \'guiding\', \'principle\', \'which\', ... \'guarantees\', \'the\', \'rubber\', \'duck\', \'forces\', \'never\', ... \'being\', \'under\', \'the\', \'command\', \'of\', \'the\', \'cat\'] >>> ref1b = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'that\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'will\', \'never\', ... \'heed\', \'the\', \'cat\', \'commands\'] >>> ref1c = [\'It\', \'is\', \'the\', \'practical\', \'guide\', \'for\', \'the\', ... \'rubber\', \'duck\', \'army\', \'never\', \'to\', \'heed\', \'the\', \'directions\', ... \'of\', \'the\', \'cat\'] >>> hyp2 = [\'he\', \'read\', \'the\', \'book\', \'because\', \'he\', \'was\', ... \'interested\', \'in\', \'world\', \'history\'] >>> ref2a = [\'he\', \'was\', \'interested\', \'in\', \'world\', \'history\', ... \'because\', \'he\', \'read\', \'the\', \'book\'] >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]] >>> hypotheses = [hyp1, hyp2] >>> google_bleu = datasets.load_metric("google_bleu") >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references, min_len=2) >>> print(round(results["google_bleu"], 2)) 0.53 Example 4: >>> hyp1 = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'which\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'always\', ... \'disobeys\', \'the\', \'commands\', \'of\', \'the\', \'cat\'] >>> ref1a = [\'It\', \'is\', \'the\', \'guiding\', \'principle\', \'which\', ... \'guarantees\', \'the\', \'rubber\', \'duck\', \'forces\', \'never\', ... \'being\', \'under\', \'the\', \'command\', \'of\', \'the\', \'cat\'] >>> ref1b = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'that\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'will\', \'never\', ... \'heed\', \'the\', \'cat\', \'commands\'] >>> ref1c = [\'It\', \'is\', \'the\', \'practical\', \'guide\', \'for\', \'the\', ... \'rubber\', \'duck\', \'army\', \'never\', \'to\', \'heed\', \'the\', \'directions\', ... \'of\', \'the\', \'cat\'] >>> hyp2 = [\'he\', \'read\', \'the\', \'book\', \'because\', \'he\', \'was\', ... \'interested\', \'in\', \'world\', \'history\'] >>> ref2a = [\'he\', \'was\', \'interested\', \'in\', \'world\', \'history\', ... \'because\', \'he\', \'read\', \'the\', \'book\'] >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]] >>> hypotheses = [hyp1, hyp2] >>> google_bleu = datasets.load_metric("google_bleu") >>> results = google_bleu.compute(predictions=hypotheses,references=list_of_references, min_len=2, max_len=6) >>> print(round(results["google_bleu"], 2)) 0.4 ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __magic_name__ ( datasets.Metric ): """simple docstring""" def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "predictions": datasets.Sequence(datasets.Value("string" , id="token" ) , id="sequence" ), "references": datasets.Sequence( datasets.Sequence(datasets.Value("string" , id="token" ) , id="sequence" ) , id="references" ), } ) , ) def SCREAMING_SNAKE_CASE ( self :int , snake_case :List[List[List[str]]] , snake_case :List[List[str]] , snake_case :int = 1 , snake_case :int = 4 , ): '''simple docstring''' return { "google_bleu": gleu_score.corpus_gleu( list_of_references=snake_case , hypotheses=snake_case , min_len=snake_case , max_len=snake_case ) }
300
0
'''simple docstring''' from argparse import ArgumentParser from datasets.commands.convert import ConvertCommand from datasets.commands.dummy_data import DummyDataCommand from datasets.commands.env import EnvironmentCommand from datasets.commands.run_beam import RunBeamCommand from datasets.commands.test import TestCommand from datasets.utils.logging import set_verbosity_info def snake_case_ (_a : Tuple ): return {key.lstrip('''-''' ): value for key, value in zip(unknown_args[::2] , unknown_args[1::2] )} def snake_case_ (): UpperCAmelCase = ArgumentParser( '''HuggingFace Datasets CLI tool''' , usage='''datasets-cli <command> [<args>]''' , allow_abbrev=_a ) UpperCAmelCase = parser.add_subparsers(help='''datasets-cli command helpers''' ) set_verbosity_info() # Register commands ConvertCommand.register_subcommand(_a ) EnvironmentCommand.register_subcommand(_a ) TestCommand.register_subcommand(_a ) RunBeamCommand.register_subcommand(_a ) DummyDataCommand.register_subcommand(_a ) # Parse args UpperCAmelCase , UpperCAmelCase = parser.parse_known_args() if not hasattr(_a , '''func''' ): parser.print_help() exit(1 ) UpperCAmelCase = parse_unknown_args(_a ) # Run UpperCAmelCase = args.func(_a , **_a ) service.run() if __name__ == "__main__": main()
34
import pyarrow.parquet as pq import pytest from datasets import Audio, Dataset, DatasetDict, Features, NamedSplit, Sequence, Value, config from datasets.features.image import Image from datasets.io.parquet import ParquetDatasetReader, ParquetDatasetWriter, get_writer_batch_size from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases def __snake_case ( _lowerCAmelCase : List[Any] , _lowerCAmelCase : Optional[int] ) -> str: assert isinstance(_lowerCAmelCase , _lowerCAmelCase ) assert dataset.num_rows == 4 assert dataset.num_columns == 3 assert dataset.column_names == ["col_1", "col_2", "col_3"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize("keep_in_memory" , [False, True] ) def __snake_case ( _lowerCAmelCase : Dict , _lowerCAmelCase : Optional[int] , _lowerCAmelCase : List[Any] ) -> Optional[int]: A_ : Tuple = tmp_path / "cache" A_ : Optional[int] = {"col_1": "string", "col_2": "int64", "col_3": "float64"} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): A_ : Optional[Any] = ParquetDatasetReader(_lowerCAmelCase , cache_dir=_lowerCAmelCase , keep_in_memory=_lowerCAmelCase ).read() _check_parquet_dataset(_lowerCAmelCase , _lowerCAmelCase ) @pytest.mark.parametrize( "features" , [ None, {"col_1": "string", "col_2": "int64", "col_3": "float64"}, {"col_1": "string", "col_2": "string", "col_3": "string"}, {"col_1": "int32", "col_2": "int32", "col_3": "int32"}, {"col_1": "float32", "col_2": "float32", "col_3": "float32"}, ] , ) def __snake_case ( _lowerCAmelCase : List[Any] , _lowerCAmelCase : Any , _lowerCAmelCase : List[Any] ) -> str: A_ : List[Any] = tmp_path / "cache" A_ : List[str] = {"col_1": "string", "col_2": "int64", "col_3": "float64"} A_ : int = features.copy() if features else default_expected_features A_ : str = ( Features({feature: Value(_lowerCAmelCase ) for feature, dtype in features.items()} ) if features is not None else None ) A_ : Union[str, Any] = ParquetDatasetReader(_lowerCAmelCase , features=_lowerCAmelCase , cache_dir=_lowerCAmelCase ).read() _check_parquet_dataset(_lowerCAmelCase , _lowerCAmelCase ) @pytest.mark.parametrize("split" , [None, NamedSplit("train" ), "train", "test"] ) def __snake_case ( _lowerCAmelCase : Any , _lowerCAmelCase : Any , _lowerCAmelCase : Any ) -> Optional[Any]: A_ : Dict = tmp_path / "cache" A_ : int = {"col_1": "string", "col_2": "int64", "col_3": "float64"} A_ : Optional[int] = ParquetDatasetReader(_lowerCAmelCase , cache_dir=_lowerCAmelCase , split=_lowerCAmelCase ).read() _check_parquet_dataset(_lowerCAmelCase , _lowerCAmelCase ) assert dataset.split == split if split else "train" @pytest.mark.parametrize("path_type" , [str, list] ) def __snake_case ( _lowerCAmelCase : List[str] , _lowerCAmelCase : int , _lowerCAmelCase : Optional[Any] ) -> List[str]: if issubclass(_lowerCAmelCase , _lowerCAmelCase ): A_ : int = parquet_path elif issubclass(_lowerCAmelCase , _lowerCAmelCase ): A_ : Optional[int] = [parquet_path] A_ : Optional[int] = tmp_path / "cache" A_ : Union[str, Any] = {"col_1": "string", "col_2": "int64", "col_3": "float64"} A_ : Optional[int] = ParquetDatasetReader(_lowerCAmelCase , cache_dir=_lowerCAmelCase ).read() _check_parquet_dataset(_lowerCAmelCase , _lowerCAmelCase ) def __snake_case ( _lowerCAmelCase : Any , _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : Union[str, Any]=("train",) ) -> Tuple: assert isinstance(_lowerCAmelCase , _lowerCAmelCase ) for split in splits: A_ : List[str] = dataset_dict[split] assert dataset.num_rows == 4 assert dataset.num_columns == 3 assert dataset.column_names == ["col_1", "col_2", "col_3"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize("keep_in_memory" , [False, True] ) def __snake_case ( _lowerCAmelCase : Optional[Any] , _lowerCAmelCase : List[Any] , _lowerCAmelCase : Dict ) -> Optional[int]: A_ : Optional[Any] = tmp_path / "cache" A_ : Union[str, Any] = {"col_1": "string", "col_2": "int64", "col_3": "float64"} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): A_ : Union[str, Any] = ParquetDatasetReader( {"train": parquet_path} , cache_dir=_lowerCAmelCase , keep_in_memory=_lowerCAmelCase ).read() _check_parquet_datasetdict(_lowerCAmelCase , _lowerCAmelCase ) @pytest.mark.parametrize( "features" , [ None, {"col_1": "string", "col_2": "int64", "col_3": "float64"}, {"col_1": "string", "col_2": "string", "col_3": "string"}, {"col_1": "int32", "col_2": "int32", "col_3": "int32"}, {"col_1": "float32", "col_2": "float32", "col_3": "float32"}, ] , ) def __snake_case ( _lowerCAmelCase : Tuple , _lowerCAmelCase : Any , _lowerCAmelCase : str ) -> Tuple: A_ : Optional[Any] = tmp_path / "cache" A_ : Any = {"col_1": "string", "col_2": "int64", "col_3": "float64"} A_ : List[str] = features.copy() if features else default_expected_features A_ : Tuple = ( Features({feature: Value(_lowerCAmelCase ) for feature, dtype in features.items()} ) if features is not None else None ) A_ : Optional[int] = ParquetDatasetReader({"train": parquet_path} , features=_lowerCAmelCase , cache_dir=_lowerCAmelCase ).read() _check_parquet_datasetdict(_lowerCAmelCase , _lowerCAmelCase ) @pytest.mark.parametrize("split" , [None, NamedSplit("train" ), "train", "test"] ) def __snake_case ( _lowerCAmelCase : str , _lowerCAmelCase : str , _lowerCAmelCase : Any ) -> Union[str, Any]: if split: A_ : Any = {split: parquet_path} else: A_ : Optional[Any] = "train" A_ : str = {"train": parquet_path, "test": parquet_path} A_ : Any = tmp_path / "cache" A_ : str = {"col_1": "string", "col_2": "int64", "col_3": "float64"} A_ : Dict = ParquetDatasetReader(_lowerCAmelCase , cache_dir=_lowerCAmelCase ).read() _check_parquet_datasetdict(_lowerCAmelCase , _lowerCAmelCase , splits=list(path.keys() ) ) assert all(dataset[split].split == split for split in path.keys() ) def __snake_case ( _lowerCAmelCase : Dict , _lowerCAmelCase : Optional[Any] ) -> Dict: A_ : List[str] = ParquetDatasetWriter(_lowerCAmelCase , tmp_path / "foo.parquet" ) assert writer.write() > 0 A_ : Tuple = pq.ParquetFile(tmp_path / "foo.parquet" ) A_ : Dict = pf.read() assert dataset.data.table == output_table def __snake_case ( _lowerCAmelCase : Optional[Any] , _lowerCAmelCase : int ) -> List[Any]: A_ : Tuple = str(shared_datadir / "test_image_rgb.jpg" ) A_ : int = {"image": [image_path]} A_ : Optional[Any] = Features({"image": Image()} ) A_ : Union[str, Any] = Dataset.from_dict(_lowerCAmelCase , features=_lowerCAmelCase ) A_ : Tuple = ParquetDatasetWriter(_lowerCAmelCase , tmp_path / "foo.parquet" ) assert writer.write() > 0 A_ : str = Dataset.from_parquet(str(tmp_path / "foo.parquet" ) ) assert dataset.features == reloaded_dataset.features A_ : int = ParquetDatasetReader(str(tmp_path / "foo.parquet" ) , streaming=_lowerCAmelCase ).read() assert dataset.features == reloaded_iterable_dataset.features @pytest.mark.parametrize( "feature, expected" , [ (Features({"foo": Value("int32" )} ), None), (Features({"image": Image(), "foo": Value("int32" )} ), config.PARQUET_ROW_GROUP_SIZE_FOR_IMAGE_DATASETS), (Features({"nested": Sequence(Audio() )} ), config.PARQUET_ROW_GROUP_SIZE_FOR_AUDIO_DATASETS), ] , ) def __snake_case ( _lowerCAmelCase : str , _lowerCAmelCase : List[Any] ) -> Any: assert get_writer_batch_size(_lowerCAmelCase ) == expected
300
0
'''simple docstring''' from functools import reduce __a = ( "73167176531330624919225119674426574742355349194934" "96983520312774506326239578318016984801869478851843" "85861560789112949495459501737958331952853208805511" "12540698747158523863050715693290963295227443043557" "66896648950445244523161731856403098711121722383113" "62229893423380308135336276614282806444486645238749" "30358907296290491560440772390713810515859307960866" "70172427121883998797908792274921901699720888093776" "65727333001053367881220235421809751254540594752243" "52584907711670556013604839586446706324415722155397" "53697817977846174064955149290862569321978468622482" "83972241375657056057490261407972968652414535100474" "82166370484403199890008895243450658541227588666881" "16427171479924442928230863465674813919123162824586" "17866458359124566529476545682848912883142607690042" "24219022671055626321111109370544217506941658960408" "07198403850962455444362981230987879927244284909188" "84580156166097919133875499200524063689912560717606" "05886116467109405077541002256983155200055935729725" "71636269561882670428252483600823257530420752963450" ) def __snake_case( _lowerCAmelCase = N ) -> int: return max( # mypy cannot properly interpret reduce int(reduce(lambda _lowerCAmelCase , _lowerCAmelCase : str(int(_lowerCAmelCase ) * int(_lowerCAmelCase ) ) , n[i : i + 13] ) ) for i in range(len(_lowerCAmelCase ) - 12 ) ) if __name__ == "__main__": print(F"{solution() = }")
35
import json import unittest import numpy as np from huggingface_hub import hf_hub_download from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from transformers import OneFormerImageProcessor from transformers.models.oneformer.image_processing_oneformer import binary_mask_to_rle from transformers.models.oneformer.modeling_oneformer import OneFormerForUniversalSegmentationOutput if is_vision_available(): from PIL import Image def __snake_case ( _lowerCAmelCase : List[str] , _lowerCAmelCase : List[Any]="shi-labs/oneformer_demo" ) -> int: with open(hf_hub_download(_lowerCAmelCase , _lowerCAmelCase , repo_type="dataset" ) , "r" ) as f: A_ : Optional[int] = json.load(_lowerCAmelCase ) A_ : Union[str, Any] = {} A_ : Tuple = [] A_ : Optional[Any] = [] for key, info in class_info.items(): A_ : Tuple = info["name"] class_names.append(info["name"] ) if info["isthing"]: thing_ids.append(int(_lowerCAmelCase ) ) A_ : Optional[Any] = thing_ids A_ : int = class_names return metadata class __magic_name__ ( unittest.TestCase ): """simple docstring""" def __init__( self :List[Any] , snake_case :List[str] , snake_case :int=7 , snake_case :Optional[int]=3 , snake_case :Union[str, Any]=30 , snake_case :Tuple=400 , snake_case :List[Any]=None , snake_case :Optional[Any]=True , snake_case :Tuple=True , snake_case :Dict=[0.5, 0.5, 0.5] , snake_case :Any=[0.5, 0.5, 0.5] , snake_case :Optional[int]=10 , snake_case :Tuple=False , snake_case :Optional[int]=255 , snake_case :Optional[Any]="shi-labs/oneformer_demo" , snake_case :Optional[Any]="ade20k_panoptic.json" , snake_case :Optional[int]=10 , ): '''simple docstring''' A_ : Tuple = parent A_ : List[str] = batch_size A_ : Optional[int] = num_channels A_ : Tuple = min_resolution A_ : List[Any] = max_resolution A_ : Union[str, Any] = do_resize A_ : Any = {"shortest_edge": 32, "longest_edge": 1_333} if size is None else size A_ : Tuple = do_normalize A_ : List[str] = image_mean A_ : List[Any] = image_std A_ : Union[str, Any] = class_info_file A_ : List[Any] = prepare_metadata(snake_case , snake_case ) A_ : Tuple = num_text A_ : str = repo_path # for the post_process_functions A_ : Any = 2 A_ : int = 10 A_ : Optional[int] = 10 A_ : Tuple = 3 A_ : Tuple = 4 A_ : str = num_labels A_ : int = do_reduce_labels A_ : List[Any] = ignore_index def SCREAMING_SNAKE_CASE ( self :Optional[Any] ): '''simple docstring''' return { "do_resize": self.do_resize, "size": self.size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, "num_labels": self.num_labels, "do_reduce_labels": self.do_reduce_labels, "ignore_index": self.ignore_index, "class_info_file": self.class_info_file, "metadata": self.metadata, "num_text": self.num_text, } def SCREAMING_SNAKE_CASE ( self :List[Any] , snake_case :Any , snake_case :Any=False ): '''simple docstring''' if not batched: A_ : List[str] = image_inputs[0] if isinstance(snake_case , Image.Image ): A_ , A_ : Dict = image.size else: A_ , A_ : Tuple = image.shape[1], image.shape[2] if w < h: A_ : str = int(self.size["shortest_edge"] * h / w ) A_ : Any = self.size["shortest_edge"] elif w > h: A_ : Optional[int] = self.size["shortest_edge"] A_ : List[str] = int(self.size["shortest_edge"] * w / h ) else: A_ : List[str] = self.size["shortest_edge"] A_ : Optional[Any] = self.size["shortest_edge"] else: A_ : Tuple = [] for image in image_inputs: A_ , A_ : Optional[Any] = self.get_expected_values([image] ) expected_values.append((expected_height, expected_width) ) A_ : Tuple = max(snake_case , key=lambda snake_case : item[0] )[0] A_ : Union[str, Any] = max(snake_case , key=lambda snake_case : item[1] )[1] return expected_height, expected_width def SCREAMING_SNAKE_CASE ( self :Tuple ): '''simple docstring''' return OneFormerForUniversalSegmentationOutput( # +1 for null class class_queries_logits=torch.randn((self.batch_size, self.num_queries, self.num_classes + 1) ) , masks_queries_logits=torch.randn((self.batch_size, self.num_queries, self.height, self.width) ) , ) @require_torch @require_vision class __magic_name__ ( lowerCamelCase__ , unittest.TestCase ): """simple docstring""" __UpperCamelCase = OneFormerImageProcessor if (is_vision_available() and is_torch_available()) else None # only for test_image_processing_common.test_image_proc_to_json_string __UpperCamelCase = image_processing_class def SCREAMING_SNAKE_CASE ( self :int ): '''simple docstring''' A_ : Union[str, Any] = OneFormerImageProcessorTester(self ) @property def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' return self.image_processing_tester.prepare_image_processor_dict() def SCREAMING_SNAKE_CASE ( self :List[Any] ): '''simple docstring''' A_ : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(snake_case , "image_mean" ) ) self.assertTrue(hasattr(snake_case , "image_std" ) ) self.assertTrue(hasattr(snake_case , "do_normalize" ) ) self.assertTrue(hasattr(snake_case , "do_resize" ) ) self.assertTrue(hasattr(snake_case , "size" ) ) self.assertTrue(hasattr(snake_case , "ignore_index" ) ) self.assertTrue(hasattr(snake_case , "class_info_file" ) ) self.assertTrue(hasattr(snake_case , "num_text" ) ) self.assertTrue(hasattr(snake_case , "repo_path" ) ) self.assertTrue(hasattr(snake_case , "metadata" ) ) self.assertTrue(hasattr(snake_case , "do_reduce_labels" ) ) def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' pass def SCREAMING_SNAKE_CASE ( self :int ): '''simple docstring''' A_ : Dict = self.image_processing_class(**self.image_processor_dict ) # create random PIL images A_ : Optional[Any] = prepare_image_inputs(self.image_processing_tester , equal_resolution=snake_case ) for image in image_inputs: self.assertIsInstance(snake_case , Image.Image ) # Test not batched input A_ : str = image_processor(image_inputs[0] , ["semantic"] , return_tensors="pt" ).pixel_values A_ , A_ : str = self.image_processing_tester.get_expected_values(snake_case ) self.assertEqual( encoded_images.shape , (1, self.image_processing_tester.num_channels, expected_height, expected_width) , ) # Test batched A_ , A_ : Optional[Any] = self.image_processing_tester.get_expected_values(snake_case , batched=snake_case ) A_ : List[str] = image_processor( snake_case , ["semantic"] * len(snake_case ) , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processing_tester.batch_size, self.image_processing_tester.num_channels, expected_height, expected_width, ) , ) def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' A_ : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors A_ : List[str] = prepare_image_inputs(self.image_processing_tester , equal_resolution=snake_case , numpify=snake_case ) for image in image_inputs: self.assertIsInstance(snake_case , np.ndarray ) # Test not batched input A_ : List[str] = image_processor(image_inputs[0] , ["semantic"] , return_tensors="pt" ).pixel_values A_ , A_ : List[str] = self.image_processing_tester.get_expected_values(snake_case ) self.assertEqual( encoded_images.shape , (1, self.image_processing_tester.num_channels, expected_height, expected_width) , ) # Test batched A_ , A_ : int = self.image_processing_tester.get_expected_values(snake_case , batched=snake_case ) A_ : Optional[Any] = image_processor( snake_case , ["semantic"] * len(snake_case ) , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processing_tester.batch_size, self.image_processing_tester.num_channels, expected_height, expected_width, ) , ) def SCREAMING_SNAKE_CASE ( self :Optional[int] ): '''simple docstring''' A_ : List[str] = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors A_ : List[str] = prepare_image_inputs(self.image_processing_tester , equal_resolution=snake_case , torchify=snake_case ) for image in image_inputs: self.assertIsInstance(snake_case , torch.Tensor ) # Test not batched input A_ : Any = image_processor(image_inputs[0] , ["semantic"] , return_tensors="pt" ).pixel_values A_ , A_ : Tuple = self.image_processing_tester.get_expected_values(snake_case ) self.assertEqual( encoded_images.shape , (1, self.image_processing_tester.num_channels, expected_height, expected_width) , ) # Test batched A_ , A_ : Tuple = self.image_processing_tester.get_expected_values(snake_case , batched=snake_case ) A_ : Any = image_processor( snake_case , ["semantic"] * len(snake_case ) , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processing_tester.batch_size, self.image_processing_tester.num_channels, expected_height, expected_width, ) , ) def SCREAMING_SNAKE_CASE ( self :Optional[Any] , snake_case :Dict=False , snake_case :str=False , snake_case :Dict="np" ): '''simple docstring''' A_ : Tuple = self.image_processing_class(**self.image_processor_dict ) # prepare image and target A_ : Tuple = self.image_processing_tester.num_labels A_ : str = None A_ : Tuple = None A_ : Tuple = prepare_image_inputs(self.image_processing_tester , equal_resolution=snake_case ) if with_segmentation_maps: A_ : List[str] = num_labels if is_instance_map: A_ : List[str] = list(range(snake_case ) ) * 2 A_ : int = dict(enumerate(snake_case ) ) A_ : List[str] = [ np.random.randint(0 , high * 2 , (img.size[1], img.size[0]) ).astype(np.uinta ) for img in image_inputs ] if segmentation_type == "pil": A_ : int = [Image.fromarray(snake_case ) for annotation in annotations] A_ : List[str] = image_processor( snake_case , ["semantic"] * len(snake_case ) , snake_case , return_tensors="pt" , instance_id_to_semantic_id=snake_case , pad_and_return_pixel_mask=snake_case , ) return inputs def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' pass def SCREAMING_SNAKE_CASE ( self :Optional[int] ): '''simple docstring''' def common(snake_case :Dict=False , snake_case :Optional[int]=None ): A_ : Tuple = self.comm_get_image_processor_inputs( with_segmentation_maps=snake_case , is_instance_map=snake_case , segmentation_type=snake_case ) A_ : Optional[Any] = inputs["mask_labels"] A_ : List[Any] = inputs["class_labels"] A_ : Optional[Any] = inputs["pixel_values"] A_ : int = inputs["text_inputs"] # check the batch_size for mask_label, class_label, text_input in zip(snake_case , snake_case , snake_case ): self.assertEqual(mask_label.shape[0] , class_label.shape[0] ) # this ensure padding has happened self.assertEqual(mask_label.shape[1:] , pixel_values.shape[2:] ) self.assertEqual(len(snake_case ) , self.image_processing_tester.num_text ) common() common(is_instance_map=snake_case ) common(is_instance_map=snake_case , segmentation_type="pil" ) common(is_instance_map=snake_case , segmentation_type="pil" ) def SCREAMING_SNAKE_CASE ( self :Optional[Any] ): '''simple docstring''' A_ : Any = np.zeros((20, 50) ) A_ : List[str] = 1 A_ : int = 1 A_ : Optional[Any] = 1 A_ : Any = binary_mask_to_rle(snake_case ) self.assertEqual(len(snake_case ) , 4 ) self.assertEqual(rle[0] , 21 ) self.assertEqual(rle[1] , 45 ) def SCREAMING_SNAKE_CASE ( self :Optional[int] ): '''simple docstring''' A_ : Union[str, Any] = self.image_processing_class( num_labels=self.image_processing_tester.num_classes , max_seq_length=77 , task_seq_length=77 , class_info_file="ade20k_panoptic.json" , num_text=self.image_processing_tester.num_text , repo_path="shi-labs/oneformer_demo" , ) A_ : Any = self.image_processing_tester.get_fake_oneformer_outputs() A_ : int = fature_extractor.post_process_semantic_segmentation(snake_case ) self.assertEqual(len(snake_case ) , self.image_processing_tester.batch_size ) self.assertEqual( segmentation[0].shape , ( self.image_processing_tester.height, self.image_processing_tester.width, ) , ) A_ : Optional[int] = [(1, 4) for i in range(self.image_processing_tester.batch_size )] A_ : List[Any] = fature_extractor.post_process_semantic_segmentation(snake_case , target_sizes=snake_case ) self.assertEqual(segmentation[0].shape , target_sizes[0] ) def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' A_ : List[str] = self.image_processing_class( num_labels=self.image_processing_tester.num_classes , max_seq_length=77 , task_seq_length=77 , class_info_file="ade20k_panoptic.json" , num_text=self.image_processing_tester.num_text , repo_path="shi-labs/oneformer_demo" , ) A_ : str = self.image_processing_tester.get_fake_oneformer_outputs() A_ : Optional[Any] = image_processor.post_process_instance_segmentation(snake_case , threshold=0 ) self.assertTrue(len(snake_case ) == self.image_processing_tester.batch_size ) for el in segmentation: self.assertTrue("segmentation" in el ) self.assertTrue("segments_info" in el ) self.assertEqual(type(el["segments_info"] ) , snake_case ) self.assertEqual( el["segmentation"].shape , (self.image_processing_tester.height, self.image_processing_tester.width) ) def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' A_ : Tuple = self.image_processing_class( num_labels=self.image_processing_tester.num_classes , max_seq_length=77 , task_seq_length=77 , class_info_file="ade20k_panoptic.json" , num_text=self.image_processing_tester.num_text , repo_path="shi-labs/oneformer_demo" , ) A_ : List[Any] = self.image_processing_tester.get_fake_oneformer_outputs() A_ : Optional[Any] = image_processor.post_process_panoptic_segmentation(snake_case , threshold=0 ) self.assertTrue(len(snake_case ) == self.image_processing_tester.batch_size ) for el in segmentation: self.assertTrue("segmentation" in el ) self.assertTrue("segments_info" in el ) self.assertEqual(type(el["segments_info"] ) , snake_case ) self.assertEqual( el["segmentation"].shape , (self.image_processing_tester.height, self.image_processing_tester.width) )
300
0
# Copyright 2021 The HuggingFace Inc. 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 packaging import version from .. import __version__ from .constants import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD from .doc import ( add_code_sample_docstrings, add_end_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, copy_func, replace_return_docstrings, ) from .generic import ( ContextManagers, ExplicitEnum, ModelOutput, PaddingStrategy, TensorType, add_model_info_to_auto_map, cached_property, can_return_loss, expand_dims, find_labels, flatten_dict, infer_framework, is_jax_tensor, is_numpy_array, is_tensor, is_tf_symbolic_tensor, is_tf_tensor, is_torch_device, is_torch_dtype, is_torch_tensor, reshape, squeeze, strtobool, tensor_size, to_numpy, to_py_obj, transpose, working_or_temp_dir, ) from .hub import ( CLOUDFRONT_DISTRIB_PREFIX, DISABLE_TELEMETRY, HF_MODULES_CACHE, HUGGINGFACE_CO_PREFIX, HUGGINGFACE_CO_RESOLVE_ENDPOINT, PYTORCH_PRETRAINED_BERT_CACHE, PYTORCH_TRANSFORMERS_CACHE, S3_BUCKET_PREFIX, TRANSFORMERS_CACHE, TRANSFORMERS_DYNAMIC_MODULE_NAME, EntryNotFoundError, PushToHubMixin, RepositoryNotFoundError, RevisionNotFoundError, cached_file, default_cache_path, define_sagemaker_information, download_url, extract_commit_hash, get_cached_models, get_file_from_repo, get_full_repo_name, has_file, http_user_agent, is_offline_mode, is_remote_url, move_cache, send_example_telemetry, try_to_load_from_cache, ) from .import_utils import ( ENV_VARS_TRUE_AND_AUTO_VALUES, ENV_VARS_TRUE_VALUES, TORCH_FX_REQUIRED_VERSION, USE_JAX, USE_TF, USE_TORCH, DummyObject, OptionalDependencyNotAvailable, _LazyModule, ccl_version, direct_transformers_import, get_torch_version, is_accelerate_available, is_apex_available, is_bitsandbytes_available, is_bsa_available, is_coloredlogs_available, is_cython_available, is_datasets_available, is_decord_available, is_detectrona_available, is_faiss_available, is_flax_available, is_ftfy_available, is_in_notebook, is_ipex_available, is_jieba_available, is_jumanpp_available, is_kenlm_available, is_keras_nlp_available, is_librosa_available, is_natten_available, is_ninja_available, is_onnx_available, is_openai_available, is_optimum_available, is_pandas_available, is_peft_available, is_phonemizer_available, is_protobuf_available, is_psutil_available, is_pyanvml_available, is_pyctcdecode_available, is_pytesseract_available, is_pytest_available, is_pytorch_quantization_available, is_rjieba_available, is_sacremoses_available, is_safetensors_available, is_sagemaker_dp_enabled, is_sagemaker_mp_enabled, is_scipy_available, is_sentencepiece_available, is_seqio_available, is_sklearn_available, is_soundfile_availble, is_spacy_available, is_speech_available, is_sudachi_available, is_tensorflow_probability_available, is_tensorflow_text_available, is_tfaonnx_available, is_tf_available, is_timm_available, is_tokenizers_available, is_torch_available, is_torch_bfaa_available, is_torch_bfaa_cpu_available, is_torch_bfaa_gpu_available, is_torch_compile_available, is_torch_cuda_available, is_torch_fx_available, is_torch_fx_proxy, is_torch_mps_available, is_torch_neuroncore_available, is_torch_tensorrt_fx_available, is_torch_tfaa_available, is_torch_tpu_available, is_torchaudio_available, is_torchdistx_available, is_torchdynamo_available, is_torchvision_available, is_training_run_on_sagemaker, is_vision_available, requires_backends, torch_only_method, ) _snake_case = "pytorch_model.bin" _snake_case = "pytorch_model.bin.index.json" _snake_case = "adapter_config.json" _snake_case = "adapter_model.bin" _snake_case = "adapter_model.safetensors" _snake_case = "tf_model.h5" _snake_case = "tf_model.h5.index.json" _snake_case = "model.ckpt" _snake_case = "flax_model.msgpack" _snake_case = "flax_model.msgpack.index.json" _snake_case = "model.safetensors" _snake_case = "model.safetensors.index.json" _snake_case = "config.json" _snake_case = "preprocessor_config.json" _snake_case = FEATURE_EXTRACTOR_NAME _snake_case = "generation_config.json" _snake_case = "modelcard.json" _snake_case = "▁" _snake_case = SENTENCEPIECE_UNDERLINE # Kept for backward compatibility _snake_case = [ [[0, 1, 0, 1], [1, 0, 0, 1]] ] * 2 # Needs to have 0s and 1s only since XLM uses it for langs too. _snake_case = [[7, 6, 0, 0, 1], [1, 2, 3, 0, 0], [0, 0, 0, 4, 5]] _snake_case = [[1, 1, 1, 1, 1], [1, 1, 1, 0, 0], [0, 0, 0, 1, 1]] def A ( _lowerCamelCase ): '''simple docstring''' if version.parse(_lowerCamelCase ) < version.parse(_lowerCamelCase ): if "dev" in min_version: _lowerCAmelCase : Tuple = ( "This example requires a source install from HuggingFace Transformers (see " "`https://huggingface.co/docs/transformers/installation#install-from-source`)," ) else: _lowerCAmelCase : int = F"This example requires a minimum version of {min_version}," error_message += F" but the version found is {__version__}.\n" raise ImportError( error_message + "Check out https://github.com/huggingface/transformers/tree/main/examples#important-note for the examples corresponding to other " "versions of HuggingFace Transformers." )
36
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 _lowerCAmelCase : List[str] = logging.get_logger(__name__) _lowerCAmelCase : Optional[Any] = { '''facebook/data2vec-vision-base-ft''': ( '''https://huggingface.co/facebook/data2vec-vision-base-ft/resolve/main/config.json''' ), } class __magic_name__ ( lowerCamelCase__ ): """simple docstring""" __UpperCamelCase = '''data2vec-vision''' def __init__( self :int , snake_case :Optional[int]=768 , snake_case :Any=12 , snake_case :Any=12 , snake_case :Tuple=3_072 , snake_case :Any="gelu" , snake_case :Tuple=0.0 , snake_case :int=0.0 , snake_case :Any=0.02 , snake_case :str=1e-12 , snake_case :List[str]=224 , snake_case :Dict=16 , snake_case :int=3 , snake_case :int=False , snake_case :str=False , snake_case :List[Any]=False , snake_case :Optional[Any]=False , snake_case :Tuple=0.1 , snake_case :Optional[Any]=0.1 , snake_case :Any=True , snake_case :Optional[Any]=[3, 5, 7, 11] , snake_case :Dict=[1, 2, 3, 6] , snake_case :int=True , snake_case :List[Any]=0.4 , snake_case :Any=256 , snake_case :Union[str, Any]=1 , snake_case :Union[str, Any]=False , snake_case :Any=255 , **snake_case :int , ): '''simple docstring''' super().__init__(**snake_case ) A_ : Dict = hidden_size A_ : Tuple = num_hidden_layers A_ : List[str] = num_attention_heads A_ : Any = intermediate_size A_ : Optional[Any] = hidden_act A_ : Any = hidden_dropout_prob A_ : List[str] = attention_probs_dropout_prob A_ : Optional[Any] = initializer_range A_ : List[str] = layer_norm_eps A_ : str = image_size A_ : Optional[int] = patch_size A_ : int = num_channels A_ : Optional[Any] = use_mask_token A_ : Optional[Any] = use_absolute_position_embeddings A_ : Optional[int] = use_relative_position_bias A_ : Dict = use_shared_relative_position_bias A_ : Any = layer_scale_init_value A_ : Optional[Any] = drop_path_rate A_ : Dict = use_mean_pooling # decode head attributes (semantic segmentation) A_ : Tuple = out_indices A_ : Optional[Any] = pool_scales # auxiliary head attributes (semantic segmentation) A_ : str = use_auxiliary_head A_ : List[Any] = auxiliary_loss_weight A_ : List[str] = auxiliary_channels A_ : Dict = auxiliary_num_convs A_ : List[str] = auxiliary_concat_input A_ : Optional[int] = semantic_loss_ignore_index class __magic_name__ ( lowerCamelCase__ ): """simple docstring""" __UpperCamelCase = version.parse('''1.11''' ) @property def SCREAMING_SNAKE_CASE ( self :Union[str, Any] ): '''simple docstring''' return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ] ) @property def SCREAMING_SNAKE_CASE ( self :Tuple ): '''simple docstring''' return 1e-4
300
0
'''simple docstring''' import numpy as np import qiskit def _SCREAMING_SNAKE_CASE ( UpperCamelCase = 8 , UpperCamelCase = None ): """simple docstring""" lowerCAmelCase__ : Union[str, Any] = np.random.default_rng(seed=UpperCamelCase ) # Roughly 25% of the qubits will contribute to the key. # So we take more than we need. lowerCAmelCase__ : Tuple = 6 * key_len # Measurement basis for Alice's qubits. lowerCAmelCase__ : List[str] = rng.integers(2 , size=UpperCamelCase ) # The set of states Alice will prepare. lowerCAmelCase__ : Optional[Any] = rng.integers(2 , size=UpperCamelCase ) # Measurement basis for Bob's qubits. lowerCAmelCase__ : List[Any] = rng.integers(2 , size=UpperCamelCase ) # Quantum Circuit to simulate BB84 lowerCAmelCase__ : Any = qiskit.QuantumCircuit(UpperCamelCase , name="""BB84""" ) # Alice prepares her qubits according to rules above. for index, _ in enumerate(UpperCamelCase ): if alice_state[index] == 1: bbaa_circ.x(UpperCamelCase ) if alice_basis[index] == 1: bbaa_circ.h(UpperCamelCase ) bbaa_circ.barrier() # Bob measures the received qubits according to rules above. for index, _ in enumerate(UpperCamelCase ): if bob_basis[index] == 1: bbaa_circ.h(UpperCamelCase ) bbaa_circ.barrier() bbaa_circ.measure_all() # Simulate the quantum circuit. lowerCAmelCase__ : Any = qiskit.Aer.get_backend("""aer_simulator""" ) # We only need to run one shot because the key is unique. # Multiple shots will produce the same key. lowerCAmelCase__ : Optional[int] = qiskit.execute(UpperCamelCase , UpperCamelCase , shots=1 , seed_simulator=UpperCamelCase ) # Returns the result of measurement. lowerCAmelCase__ : Union[str, Any] = job.result().get_counts(UpperCamelCase ).most_frequent() # Extracting the generated key from the simulation results. # Only keep measurement results where Alice and Bob chose the same basis. lowerCAmelCase__ : str = """""".join( [ result_bit for alice_basis_bit, bob_basis_bit, result_bit in zip( UpperCamelCase , UpperCamelCase , UpperCamelCase ) if alice_basis_bit == bob_basis_bit ] ) # Get final key. Pad with 0 if too short, otherwise truncate. lowerCAmelCase__ : Tuple = gen_key[:key_len] if len(UpperCamelCase ) >= key_len else gen_key.ljust(UpperCamelCase , """0""" ) return key if __name__ == "__main__": print(F"""The generated key is : {bbaa(8, seed=0)}""") from doctest import testmod testmod()
37
from typing import List, Optional, Union import numpy as np from ....audio_utils import mel_filter_bank, optimal_fft_length, spectrogram, window_function from ....feature_extraction_sequence_utils import SequenceFeatureExtractor from ....feature_extraction_utils import BatchFeature from ....file_utils import PaddingStrategy, TensorType from ....utils import logging _lowerCAmelCase : str = logging.get_logger(__name__) class __magic_name__ ( lowerCamelCase__ ): """simple docstring""" __UpperCamelCase = ['''input_features''', '''attention_mask'''] def __init__( self :int , snake_case :int=80 , snake_case :Optional[int]=16_000 , snake_case :Tuple=0.0 , snake_case :Optional[int]=10 , snake_case :Optional[Any]=25 , snake_case :Dict="hamming_window" , snake_case :Tuple=32768.0 , snake_case :str=0.97 , snake_case :List[str]=1.0 , snake_case :Dict=True , snake_case :str=True , snake_case :Optional[Any]=False , **snake_case :Union[str, Any] , ): '''simple docstring''' super().__init__(feature_size=snake_case , sampling_rate=snake_case , padding_value=snake_case , **snake_case ) A_ : Union[str, Any] = feature_size A_ : int = sampling_rate A_ : str = padding_value A_ : int = hop_length A_ : List[str] = win_length A_ : Any = frame_signal_scale A_ : str = preemphasis_coeff A_ : List[str] = mel_floor A_ : str = normalize_means A_ : Any = normalize_vars A_ : Optional[Any] = win_function A_ : Dict = return_attention_mask A_ : List[str] = win_length * sampling_rate // 1_000 A_ : List[str] = hop_length * sampling_rate // 1_000 A_ : List[str] = optimal_fft_length(self.sample_size ) A_ : str = (self.n_fft // 2) + 1 def SCREAMING_SNAKE_CASE ( self :Any , snake_case :np.array ): '''simple docstring''' if self.win_function == "hamming_window": A_ : Dict = window_function(window_length=self.sample_size , name=self.win_function , periodic=snake_case ) else: A_ : List[str] = window_function(window_length=self.sample_size , name=self.win_function ) A_ : Optional[int] = mel_filter_bank( num_frequency_bins=self.n_freqs , num_mel_filters=self.feature_size , min_frequency=0.0 , max_frequency=self.sampling_rate / 2.0 , sampling_rate=self.sampling_rate , ) A_ : Tuple = spectrogram( one_waveform * self.frame_signal_scale , window=snake_case , frame_length=self.sample_size , hop_length=self.sample_stride , fft_length=self.n_fft , center=snake_case , preemphasis=self.preemphasis_coeff , mel_filters=snake_case , mel_floor=self.mel_floor , log_mel="log" , ) return msfc_features.T def SCREAMING_SNAKE_CASE ( self :int , snake_case :Any , snake_case :Union[str, Any] , snake_case :str ): '''simple docstring''' if self.normalize_means: A_ : int = x[:input_length].mean(axis=0 ) A_ : Any = np.subtract(snake_case , snake_case ) if self.normalize_vars: A_ : List[Any] = x[:input_length].std(axis=0 ) A_ : Optional[int] = np.divide(snake_case , snake_case ) if input_length < x.shape[0]: A_ : Optional[int] = padding_value # make sure array is in float32 A_ : Union[str, Any] = x.astype(np.floataa ) return x def SCREAMING_SNAKE_CASE ( self :int , snake_case :List[np.ndarray] , snake_case :Optional[np.ndarray] = None ): '''simple docstring''' A_ : str = attention_mask.sum(-1 ) if attention_mask is not None else [x.shape[0] for x in input_features] return [self._normalize_one(snake_case , snake_case , self.padding_value ) for x, n in zip(snake_case , snake_case )] def __call__( self :int , snake_case :Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] , snake_case :Union[bool, str, PaddingStrategy] = False , snake_case :Optional[int] = None , snake_case :bool = False , snake_case :Optional[int] = None , snake_case :Optional[bool] = None , snake_case :Optional[Union[str, TensorType]] = None , snake_case :Optional[int] = None , **snake_case :Dict , ): '''simple docstring''' if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( f"The model corresponding to this feature extractor: {self} was trained using a sampling rate of" f" {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled with" f" {self.sampling_rate} and not {sampling_rate}." ) else: logger.warning( "It is strongly recommended to pass the ``sampling_rate`` argument to this function. " "Failing to do so can result in silent errors that might be hard to debug." ) A_ : Optional[int] = isinstance(snake_case , np.ndarray ) and len(raw_speech.shape ) > 1 if is_batched_numpy and len(raw_speech.shape ) > 2: raise ValueError(f"Only mono-channel audio is supported for input to {self}" ) A_ : Optional[Any] = is_batched_numpy or ( isinstance(snake_case , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: A_ : List[Any] = [np.asarray(snake_case , dtype=np.floataa ) for speech in raw_speech] elif not is_batched and not isinstance(snake_case , np.ndarray ): A_ : int = np.asarray(snake_case , dtype=np.floataa ) elif isinstance(snake_case , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): A_ : Optional[int] = raw_speech.astype(np.floataa ) # always return batch if not is_batched: A_ : Tuple = [raw_speech] # extract fbank features A_ : int = [self._extract_mfsc_features(snake_case ) for one_waveform in raw_speech] # convert into correct format for padding A_ : Union[str, Any] = BatchFeature({"input_features": features} ) A_ : str = self.pad( snake_case , padding=snake_case , max_length=snake_case , truncation=snake_case , pad_to_multiple_of=snake_case , return_attention_mask=snake_case , **snake_case , ) # make sure list is in array format A_ : Optional[int] = padded_inputs.get("input_features" ) if isinstance(input_features[0] , snake_case ): A_ : Union[str, Any] = [np.asarray(snake_case , dtype=np.floataa ) for feature in input_features] A_ : Dict = padded_inputs.get("attention_mask" ) if attention_mask is not None: A_ : Any = [np.asarray(snake_case , dtype=np.intaa ) for array in attention_mask] if self.normalize_means or self.normalize_vars: A_ : Dict = ( np.array(snake_case , dtype=np.intaa ) if self._get_padding_strategies(snake_case , max_length=snake_case ) is not PaddingStrategy.DO_NOT_PAD and padding else None ) A_ : Optional[int] = self.normalize( padded_inputs["input_features"] , attention_mask=snake_case ) if return_tensors is not None: A_ : Dict = padded_inputs.convert_to_tensors(snake_case ) return padded_inputs
300
0
import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import CLIPTokenizer, CLIPTokenizerFast from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES from transformers.testing_utils import require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import OwlViTImageProcessor, OwlViTProcessor @require_vision class _SCREAMING_SNAKE_CASE ( unittest.TestCase ): def _A ( self : List[str] ): UpperCamelCase :int = tempfile.mkdtemp() # fmt: off UpperCamelCase :str = ["""""", """l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """lo""", """l</w>""", """w</w>""", """r</w>""", """t</w>""", """low</w>""", """er</w>""", """lowest</w>""", """newer</w>""", """wider""", """<unk>""", """<|startoftext|>""", """<|endoftext|>"""] # fmt: on UpperCamelCase :Optional[int] = dict(zip(__lowerCamelCase , range(len(__lowerCamelCase ) ) ) ) UpperCamelCase :List[str] = ["""#version: 0.2""", """l o""", """lo w</w>""", """e r</w>""", """"""] UpperCamelCase :List[str] = {"""unk_token""": """<unk>"""} UpperCamelCase :List[Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""vocab_file"""] ) UpperCamelCase :Tuple = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""merges_file"""] ) with open(self.vocab_file , """w""" , encoding="""utf-8""" ) as fp: fp.write(json.dumps(__lowerCamelCase ) + """\n""" ) with open(self.merges_file , """w""" , encoding="""utf-8""" ) as fp: fp.write("""\n""".join(__lowerCamelCase ) ) UpperCamelCase :List[Any] = { """do_resize""": True, """size""": 20, """do_center_crop""": True, """crop_size""": 18, """do_normalize""": True, """image_mean""": [0.48145466, 0.4578275, 0.40821073], """image_std""": [0.26862954, 0.26130258, 0.27577711], } UpperCamelCase :Optional[Any] = os.path.join(self.tmpdirname , __lowerCamelCase ) with open(self.image_processor_file , """w""" , encoding="""utf-8""" ) as fp: json.dump(__lowerCamelCase , __lowerCamelCase ) def _A ( self : Dict , **__lowerCamelCase : Dict ): return CLIPTokenizer.from_pretrained(self.tmpdirname , pad_token="""!""" , **__lowerCamelCase ) def _A ( self : str , **__lowerCamelCase : Any ): return CLIPTokenizerFast.from_pretrained(self.tmpdirname , pad_token="""!""" , **__lowerCamelCase ) def _A ( self : Tuple , **__lowerCamelCase : List[Any] ): return OwlViTImageProcessor.from_pretrained(self.tmpdirname , **__lowerCamelCase ) def _A ( self : str ): shutil.rmtree(self.tmpdirname ) def _A ( self : str ): UpperCamelCase :Any = [np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta )] UpperCamelCase :Any = [Image.fromarray(np.moveaxis(__lowerCamelCase , 0 , -1 ) ) for x in image_inputs] return image_inputs def _A ( self : str ): UpperCamelCase :List[Any] = self.get_tokenizer() UpperCamelCase :List[Any] = self.get_rust_tokenizer() UpperCamelCase :Tuple = self.get_image_processor() UpperCamelCase :Any = OwlViTProcessor(tokenizer=__lowerCamelCase , image_processor=__lowerCamelCase ) processor_slow.save_pretrained(self.tmpdirname ) UpperCamelCase :int = OwlViTProcessor.from_pretrained(self.tmpdirname , use_fast=__lowerCamelCase ) UpperCamelCase :Union[str, Any] = OwlViTProcessor(tokenizer=__lowerCamelCase , image_processor=__lowerCamelCase ) processor_fast.save_pretrained(self.tmpdirname ) UpperCamelCase :str = OwlViTProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor_slow.tokenizer.get_vocab() , tokenizer_slow.get_vocab() ) self.assertEqual(processor_fast.tokenizer.get_vocab() , tokenizer_fast.get_vocab() ) self.assertEqual(tokenizer_slow.get_vocab() , tokenizer_fast.get_vocab() ) self.assertIsInstance(processor_slow.tokenizer , __lowerCamelCase ) self.assertIsInstance(processor_fast.tokenizer , __lowerCamelCase ) self.assertEqual(processor_slow.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertEqual(processor_fast.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertIsInstance(processor_slow.image_processor , __lowerCamelCase ) self.assertIsInstance(processor_fast.image_processor , __lowerCamelCase ) def _A ( self : List[str] ): UpperCamelCase :Optional[int] = OwlViTProcessor(tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) UpperCamelCase :Any = self.get_tokenizer(bos_token="""(BOS)""" , eos_token="""(EOS)""" ) UpperCamelCase :str = self.get_image_processor(do_normalize=__lowerCamelCase ) UpperCamelCase :str = OwlViTProcessor.from_pretrained( self.tmpdirname , bos_token="""(BOS)""" , eos_token="""(EOS)""" , do_normalize=__lowerCamelCase ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , __lowerCamelCase ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , __lowerCamelCase ) def _A ( self : Union[str, Any] ): UpperCamelCase :Union[str, Any] = self.get_image_processor() UpperCamelCase :Dict = self.get_tokenizer() UpperCamelCase :Optional[int] = OwlViTProcessor(tokenizer=__lowerCamelCase , image_processor=__lowerCamelCase ) UpperCamelCase :Dict = self.prepare_image_inputs() UpperCamelCase :List[Any] = image_processor(__lowerCamelCase , return_tensors="""np""" ) UpperCamelCase :Dict = processor(images=__lowerCamelCase , return_tensors="""np""" ) for key in input_image_proc.keys(): self.assertAlmostEqual(input_image_proc[key].sum() , input_processor[key].sum() , delta=1E-2 ) def _A ( self : Dict ): UpperCamelCase :List[Any] = self.get_image_processor() UpperCamelCase :str = self.get_tokenizer() UpperCamelCase :List[Any] = OwlViTProcessor(tokenizer=__lowerCamelCase , image_processor=__lowerCamelCase ) UpperCamelCase :Optional[Any] = """lower newer""" UpperCamelCase :List[str] = processor(text=__lowerCamelCase , return_tensors="""np""" ) UpperCamelCase :List[str] = tokenizer(__lowerCamelCase , return_tensors="""np""" ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key][0].tolist() , encoded_processor[key][0].tolist() ) def _A ( self : Optional[int] ): UpperCamelCase :List[str] = self.get_image_processor() UpperCamelCase :Union[str, Any] = self.get_tokenizer() UpperCamelCase :Any = OwlViTProcessor(tokenizer=__lowerCamelCase , image_processor=__lowerCamelCase ) UpperCamelCase :Optional[int] = """lower newer""" UpperCamelCase :Tuple = self.prepare_image_inputs() UpperCamelCase :int = processor(text=__lowerCamelCase , images=__lowerCamelCase ) self.assertListEqual(list(inputs.keys() ) , ["""input_ids""", """attention_mask""", """pixel_values"""] ) # test if it raises when no input is passed with pytest.raises(__lowerCamelCase ): processor() def _A ( self : Any ): UpperCamelCase :Optional[Any] = """google/owlvit-base-patch32""" UpperCamelCase :List[Any] = OwlViTProcessor.from_pretrained(__lowerCamelCase ) UpperCamelCase :Union[str, Any] = ["""cat""", """nasa badge"""] UpperCamelCase :str = processor(text=__lowerCamelCase ) UpperCamelCase :Optional[Any] = 16 self.assertListEqual(list(inputs.keys() ) , ["""input_ids""", """attention_mask"""] ) self.assertEqual(inputs["""input_ids"""].shape , (2, seq_length) ) # test if it raises when no input is passed with pytest.raises(__lowerCamelCase ): processor() def _A ( self : List[str] ): UpperCamelCase :Dict = """google/owlvit-base-patch32""" UpperCamelCase :Dict = OwlViTProcessor.from_pretrained(__lowerCamelCase ) UpperCamelCase :Tuple = [["""cat""", """nasa badge"""], ["""person"""]] UpperCamelCase :Union[str, Any] = processor(text=__lowerCamelCase ) UpperCamelCase :int = 16 UpperCamelCase :int = len(__lowerCamelCase ) UpperCamelCase :Dict = max([len(__lowerCamelCase ) for texts in input_texts] ) self.assertListEqual(list(inputs.keys() ) , ["""input_ids""", """attention_mask"""] ) self.assertEqual(inputs["""input_ids"""].shape , (batch_size * num_max_text_queries, seq_length) ) # test if it raises when no input is passed with pytest.raises(__lowerCamelCase ): processor() def _A ( self : Any ): UpperCamelCase :Optional[Any] = """google/owlvit-base-patch32""" UpperCamelCase :List[Any] = OwlViTProcessor.from_pretrained(__lowerCamelCase ) UpperCamelCase :Optional[int] = ["""cat""", """nasa badge"""] UpperCamelCase :Union[str, Any] = processor(text=__lowerCamelCase ) UpperCamelCase :str = 16 UpperCamelCase :Optional[int] = inputs["""input_ids"""] UpperCamelCase :str = [ [49_406, 2_368, 49_407, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [49_406, 6_841, 11_301, 49_407, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], ] self.assertListEqual(list(inputs.keys() ) , ["""input_ids""", """attention_mask"""] ) self.assertEqual(inputs["""input_ids"""].shape , (2, seq_length) ) self.assertListEqual(list(input_ids[0] ) , predicted_ids[0] ) self.assertListEqual(list(input_ids[1] ) , predicted_ids[1] ) def _A ( self : str ): UpperCamelCase :Union[str, Any] = self.get_image_processor() UpperCamelCase :str = self.get_tokenizer() UpperCamelCase :Optional[int] = OwlViTProcessor(tokenizer=__lowerCamelCase , image_processor=__lowerCamelCase ) UpperCamelCase :List[Any] = self.prepare_image_inputs() UpperCamelCase :List[str] = self.prepare_image_inputs() UpperCamelCase :Union[str, Any] = processor(images=__lowerCamelCase , query_images=__lowerCamelCase ) self.assertListEqual(list(inputs.keys() ) , ["""query_pixel_values""", """pixel_values"""] ) # test if it raises when no input is passed with pytest.raises(__lowerCamelCase ): processor() def _A ( self : Dict ): UpperCamelCase :Dict = self.get_image_processor() UpperCamelCase :str = self.get_tokenizer() UpperCamelCase :str = OwlViTProcessor(tokenizer=__lowerCamelCase , image_processor=__lowerCamelCase ) UpperCamelCase :str = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] UpperCamelCase :Any = processor.batch_decode(__lowerCamelCase ) UpperCamelCase :Optional[Any] = tokenizer.batch_decode(__lowerCamelCase ) self.assertListEqual(__lowerCamelCase , __lowerCamelCase )
38
from typing import Optional import numpy as np import torch from torch import nn from transformers import GPTaConfig, GPTaLMHeadModel from transformers.modeling_utils import ModuleUtilsMixin from ...configuration_utils import ConfigMixin, register_to_config from ...models import ModelMixin class __magic_name__ ( lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ): """simple docstring""" __UpperCamelCase = [r'''h\.\d+\.attn\.bias''', r'''h\.\d+\.attn\.masked_bias'''] @register_to_config def __init__( self :List[Any] , snake_case :int , snake_case :int , snake_case :Optional[int] = None , snake_case :int = 50_257 , snake_case :int = 1_024 , snake_case :int = 768 , snake_case :int = 12 , snake_case :int = 12 , snake_case :Optional[int] = None , snake_case :str = "gelu_new" , snake_case :float = 0.1 , snake_case :float = 0.1 , snake_case :float = 0.1 , snake_case :float = 1e-5 , snake_case :float = 0.02 , snake_case :bool = True , snake_case :bool = True , snake_case :bool = False , snake_case :bool = False , ): '''simple docstring''' super().__init__() A_ : Tuple = prefix_length if prefix_inner_dim != n_embd and prefix_hidden_dim is None: raise ValueError( f"`prefix_hidden_dim` cannot be `None` when `prefix_inner_dim`: {prefix_hidden_dim} and" f" `n_embd`: {n_embd} are not equal." ) A_ : List[Any] = prefix_inner_dim A_ : Union[str, Any] = prefix_hidden_dim A_ : List[str] = ( nn.Linear(self.prefix_inner_dim , self.prefix_hidden_dim ) if self.prefix_hidden_dim is not None else nn.Identity() ) A_ : List[Any] = ( nn.Linear(self.prefix_hidden_dim , snake_case ) if self.prefix_hidden_dim is not None else nn.Identity() ) A_ : List[Any] = GPTaConfig( vocab_size=snake_case , n_positions=snake_case , n_embd=snake_case , n_layer=snake_case , n_head=snake_case , n_inner=snake_case , activation_function=snake_case , resid_pdrop=snake_case , embd_pdrop=snake_case , attn_pdrop=snake_case , layer_norm_epsilon=snake_case , initializer_range=snake_case , scale_attn_weights=snake_case , use_cache=snake_case , scale_attn_by_inverse_layer_idx=snake_case , reorder_and_upcast_attn=snake_case , ) A_ : Optional[Any] = GPTaLMHeadModel(snake_case ) def SCREAMING_SNAKE_CASE ( self :Tuple , snake_case :torch.Tensor , snake_case :torch.Tensor , snake_case :Optional[torch.Tensor] = None , snake_case :Optional[torch.Tensor] = None , ): '''simple docstring''' A_ : Any = self.transformer.transformer.wte(snake_case ) A_ : str = self.encode_prefix(snake_case ) A_ : Union[str, Any] = self.decode_prefix(snake_case ) A_ : int = torch.cat((prefix_embeds, embedding_text) , dim=1 ) if labels is not None: A_ : Dict = self.get_dummy_token(input_ids.shape[0] , input_ids.device ) A_ : int = torch.cat((dummy_token, input_ids) , dim=1 ) A_ : Union[str, Any] = self.transformer(inputs_embeds=snake_case , labels=snake_case , attention_mask=snake_case ) if self.prefix_hidden_dim is not None: return out, hidden else: return out def SCREAMING_SNAKE_CASE ( self :str , snake_case :int , snake_case :torch.device ): '''simple docstring''' return torch.zeros(snake_case , self.prefix_length , dtype=torch.intaa , device=snake_case ) def SCREAMING_SNAKE_CASE ( self :Optional[int] , snake_case :int ): '''simple docstring''' return self.encode_prefix(snake_case ) @torch.no_grad() def SCREAMING_SNAKE_CASE ( self :List[Any] , snake_case :Dict , snake_case :Optional[int] , snake_case :Any ): '''simple docstring''' A_ : Any = torch.split(snake_case , 1 , dim=0 ) A_ : Optional[int] = [] A_ : Union[str, Any] = [] for feature in features: A_ : Tuple = self.decode_prefix(feature.to(snake_case ) ) # back to the clip feature # Only support beam search for now A_ , A_ : Dict = self.generate_beam( input_embeds=snake_case , device=snake_case , eos_token_id=snake_case ) generated_tokens.append(output_tokens[0] ) generated_seq_lengths.append(seq_lengths[0] ) A_ : int = torch.stack(snake_case ) A_ : int = torch.stack(snake_case ) return generated_tokens, generated_seq_lengths @torch.no_grad() def SCREAMING_SNAKE_CASE ( self :Union[str, Any] , snake_case :int=None , snake_case :str=None , snake_case :int=None , snake_case :int = 5 , snake_case :int = 67 , snake_case :float = 1.0 , snake_case :Optional[int] = None , ): '''simple docstring''' A_ : Optional[Any] = eos_token_id A_ : List[Any] = None A_ : List[Any] = None A_ : str = torch.ones(snake_case , device=snake_case , dtype=torch.int ) A_ : Any = torch.zeros(snake_case , device=snake_case , dtype=torch.bool ) if input_embeds is not None: A_ : Any = input_embeds else: A_ : Optional[Any] = self.transformer.transformer.wte(snake_case ) for i in range(snake_case ): A_ : Optional[Any] = self.transformer(inputs_embeds=snake_case ) A_ : str = outputs.logits A_ : int = logits[:, -1, :] / (temperature if temperature > 0 else 1.0) A_ : List[str] = logits.softmax(-1 ).log() if scores is None: A_ , A_ : Union[str, Any] = logits.topk(snake_case , -1 ) A_ : Tuple = generated.expand(snake_case , *generated.shape[1:] ) A_ , A_ : str = next_tokens.permute(1 , 0 ), scores.squeeze(0 ) if tokens is None: A_ : Union[str, Any] = next_tokens else: A_ : List[str] = tokens.expand(snake_case , *tokens.shape[1:] ) A_ : Union[str, Any] = torch.cat((tokens, next_tokens) , dim=1 ) else: A_ : List[str] = -float(np.inf ) A_ : List[Any] = 0 A_ : Union[str, Any] = scores[:, None] + logits seq_lengths[~is_stopped] += 1 A_ : Optional[Any] = scores_sum / seq_lengths[:, None] A_ , A_ : List[str] = scores_sum_average.view(-1 ).topk(snake_case , -1 ) A_ : str = next_tokens // scores_sum.shape[1] A_ : Union[str, Any] = seq_lengths[next_tokens_source] A_ : Optional[int] = next_tokens % scores_sum.shape[1] A_ : Tuple = next_tokens.unsqueeze(1 ) A_ : Tuple = tokens[next_tokens_source] A_ : Dict = torch.cat((tokens, next_tokens) , dim=1 ) A_ : Dict = generated[next_tokens_source] A_ : Union[str, Any] = scores_sum_average * seq_lengths A_ : Optional[int] = is_stopped[next_tokens_source] A_ : Tuple = self.transformer.transformer.wte(next_tokens.squeeze() ).view(generated.shape[0] , 1 , -1 ) A_ : Union[str, Any] = torch.cat((generated, next_token_embed) , dim=1 ) A_ : Any = is_stopped + next_tokens.eq(snake_case ).squeeze() if is_stopped.all(): break A_ : int = scores / seq_lengths A_ : str = scores.argsort(descending=snake_case ) # tokens tensors are already padded to max_seq_length A_ : Dict = [tokens[i] for i in order] A_ : int = torch.stack(snake_case , dim=0 ) A_ : List[Any] = torch.tensor([seq_lengths[i] for i in order] , dtype=seq_lengths.dtype ) return output_texts, seq_lengths
300
0
import pytest import requests from datasets.utils.file_utils import http_head from .utils import OfflineSimulationMode, RequestWouldHangIndefinitelyError, offline @pytest.mark.integration def __A ( )-> str: """simple docstring""" with offline(OfflineSimulationMode.CONNECTION_TIMES_OUT ): with pytest.raises(__lowerCAmelCase ): requests.request('GET' , 'https://huggingface.co' ) with pytest.raises(requests.exceptions.ConnectTimeout ): requests.request('GET' , 'https://huggingface.co' , timeout=1.0 ) @pytest.mark.integration def __A ( )-> List[str]: """simple docstring""" with offline(OfflineSimulationMode.CONNECTION_FAILS ): with pytest.raises(requests.exceptions.ConnectionError ): requests.request('GET' , 'https://huggingface.co' ) def __A ( )-> Tuple: """simple docstring""" with offline(OfflineSimulationMode.HF_DATASETS_OFFLINE_SET_TO_1 ): with pytest.raises(__lowerCAmelCase ): http_head('https://huggingface.co' )
39
import warnings from ...utils import logging from .image_processing_yolos import YolosImageProcessor _lowerCAmelCase : Tuple = logging.get_logger(__name__) class __magic_name__ ( lowerCamelCase__ ): """simple docstring""" def __init__( self :Union[str, Any] , *snake_case :Tuple , **snake_case :Any ): '''simple docstring''' warnings.warn( "The class YolosFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please" " use YolosImageProcessor instead." , snake_case , ) super().__init__(*snake_case , **snake_case )
300
0
"""simple docstring""" # Copyright 2023 The HuggingFace Inc. 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. import re from ..models.auto import AutoProcessor from ..models.vision_encoder_decoder import VisionEncoderDecoderModel from ..utils import is_vision_available from .base import PipelineTool if is_vision_available(): from PIL import Image class _A ( _a ): """simple docstring""" UpperCAmelCase : str = """naver-clova-ix/donut-base-finetuned-docvqa""" UpperCAmelCase : Tuple = ( """This is a tool that answers a question about an document (pdf). It takes an input named `document` which """ """should be the document containing the information, as well as a `question` that is the question about the """ """document. It returns a text that contains the answer to the question.""" ) UpperCAmelCase : List[str] = """document_qa""" UpperCAmelCase : str = AutoProcessor UpperCAmelCase : Optional[int] = VisionEncoderDecoderModel UpperCAmelCase : int = ["""image""", """text"""] UpperCAmelCase : int = ["""text"""] def __init__( self : Tuple , *__UpperCAmelCase : Union[str, Any] , **__UpperCAmelCase : Any): if not is_vision_available(): raise ValueError("Pillow must be installed to use the DocumentQuestionAnsweringTool.") super().__init__(*__UpperCAmelCase , **__UpperCAmelCase) def __snake_case ( self : Tuple , __UpperCAmelCase : "Image" , __UpperCAmelCase : str): a : Any = "<s_docvqa><s_question>{user_input}</s_question><s_answer>" a : Union[str, Any] = task_prompt.replace("{user_input}" , __UpperCAmelCase) a : Optional[Any] = self.pre_processor.tokenizer( __UpperCAmelCase , add_special_tokens=__UpperCAmelCase , return_tensors="pt").input_ids a : Any = self.pre_processor(__UpperCAmelCase , return_tensors="pt").pixel_values return {"decoder_input_ids": decoder_input_ids, "pixel_values": pixel_values} def __snake_case ( self : int , __UpperCAmelCase : int): return self.model.generate( inputs["pixel_values"].to(self.device) , decoder_input_ids=inputs["decoder_input_ids"].to(self.device) , max_length=self.model.decoder.config.max_position_embeddings , early_stopping=__UpperCAmelCase , pad_token_id=self.pre_processor.tokenizer.pad_token_id , eos_token_id=self.pre_processor.tokenizer.eos_token_id , use_cache=__UpperCAmelCase , num_beams=1 , bad_words_ids=[[self.pre_processor.tokenizer.unk_token_id]] , return_dict_in_generate=__UpperCAmelCase , ).sequences def __snake_case ( self : str , __UpperCAmelCase : List[Any]): a : Union[str, Any] = self.pre_processor.batch_decode(__UpperCAmelCase)[0] a : Optional[Any] = sequence.replace(self.pre_processor.tokenizer.eos_token , "") a : Any = sequence.replace(self.pre_processor.tokenizer.pad_token , "") a : Optional[Any] = re.sub(r"<.*?>" , "" , __UpperCAmelCase , count=1).strip() # remove first task start token a : List[str] = self.pre_processor.tokenajson(__UpperCAmelCase) return sequence["answer"]
40
from __future__ import annotations def __snake_case ( _lowerCAmelCase : list[float] ) -> bool: if len(_lowerCAmelCase ) < 2: raise ValueError("Monogons and Digons are not polygons in the Euclidean space" ) if any(i <= 0 for i in nums ): raise ValueError("All values must be greater than 0" ) A_ : List[str] = nums.copy() copy_nums.sort() return copy_nums[-1] < sum(copy_nums[:-1] ) if __name__ == "__main__": import doctest doctest.testmod()
300
0
'''simple docstring''' import random import sys import numpy as np from matplotlib import pyplot as plt from matplotlib.colors import ListedColormap _A : List[str] ='''Usage of script: script_name <size_of_canvas:int>''' _A : List[str] =[0] * 100 + [1] * 10 random.shuffle(choice) def SCREAMING_SNAKE_CASE_ (UpperCamelCase ) -> list[list[bool]]: lowerCamelCase__ : int = [[False for i in range(UpperCamelCase )] for j in range(UpperCamelCase )] return canvas def SCREAMING_SNAKE_CASE_ (UpperCamelCase ) -> None: for i, row in enumerate(UpperCamelCase ): for j, _ in enumerate(UpperCamelCase ): lowerCamelCase__ : List[str] = bool(random.getrandbits(1 ) ) def SCREAMING_SNAKE_CASE_ (UpperCamelCase ) -> list[list[bool]]: lowerCamelCase__ : Any = np.array(UpperCamelCase ) lowerCamelCase__ : Optional[int] = np.array(create_canvas(current_canvas.shape[0] ) ) for r, row in enumerate(UpperCamelCase ): for c, pt in enumerate(UpperCamelCase ): lowerCamelCase__ : List[str] = __judge_point( UpperCamelCase , current_canvas[r - 1 : r + 2, c - 1 : c + 2] ) lowerCamelCase__ : Optional[Any] = next_gen_canvas del next_gen_canvas # cleaning memory as we move on. lowerCamelCase__ : list[list[bool]] = current_canvas.tolist() return return_canvas def SCREAMING_SNAKE_CASE_ (UpperCamelCase , UpperCamelCase ) -> bool: lowerCamelCase__ : Tuple = 0 lowerCamelCase__ : Any = 0 # finding dead or alive neighbours count. for i in neighbours: for status in i: if status: alive += 1 else: dead += 1 # handling duplicate entry for focus pt. if pt: alive -= 1 else: dead -= 1 # running the rules of game here. lowerCamelCase__ : Tuple = pt if pt: if alive < 2: lowerCamelCase__ : Union[str, Any] = False elif alive == 2 or alive == 3: lowerCamelCase__ : str = True elif alive > 3: lowerCamelCase__ : List[str] = False else: if alive == 3: lowerCamelCase__ : Any = True return state if __name__ == "__main__": if len(sys.argv) != 2: raise Exception(usage_doc) _A : Optional[int] =int(sys.argv[1]) # main working structure of this module. _A : Dict =create_canvas(canvas_size) seed(c) _A , _A : List[Any] =plt.subplots() fig.show() _A : Dict =ListedColormap(['''w''', '''k''']) try: while True: _A : Optional[int] =run(c) ax.matshow(c, cmap=cmap) fig.canvas.draw() ax.cla() except KeyboardInterrupt: # do nothing. pass
41
import inspect from typing import Callable, List, Optional, Union import torch from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer from diffusers import DiffusionPipeline from diffusers.models import AutoencoderKL, UNetaDConditionModel from diffusers.pipelines.stable_diffusion import StableDiffusionPipelineOutput from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker from diffusers.schedulers import DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler from diffusers.utils import logging _lowerCAmelCase : Any = logging.get_logger(__name__) # pylint: disable=invalid-name class __magic_name__ ( lowerCamelCase__ ): """simple docstring""" def __init__( self :Union[str, Any] , snake_case :AutoencoderKL , snake_case :CLIPTextModel , snake_case :CLIPTokenizer , snake_case :UNetaDConditionModel , snake_case :Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler] , snake_case :StableDiffusionSafetyChecker , snake_case :CLIPImageProcessor , ): '''simple docstring''' super().__init__() self.register_modules( vae=snake_case , text_encoder=snake_case , tokenizer=snake_case , unet=snake_case , scheduler=snake_case , safety_checker=snake_case , feature_extractor=snake_case , ) def SCREAMING_SNAKE_CASE ( self :List[Any] , snake_case :Optional[Union[str, int]] = "auto" ): '''simple docstring''' if slice_size == "auto": # half the attention head size is usually a good trade-off between # speed and memory A_ : int = self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(snake_case ) def SCREAMING_SNAKE_CASE ( self :Dict ): '''simple docstring''' self.enable_attention_slicing(snake_case ) @torch.no_grad() def __call__( self :Any , snake_case :Union[str, List[str]] , snake_case :int = 512 , snake_case :int = 512 , snake_case :int = 50 , snake_case :float = 7.5 , snake_case :Optional[Union[str, List[str]]] = None , snake_case :Optional[int] = 1 , snake_case :float = 0.0 , snake_case :Optional[torch.Generator] = None , snake_case :Optional[torch.FloatTensor] = None , snake_case :Optional[str] = "pil" , snake_case :bool = True , snake_case :Optional[Callable[[int, int, torch.FloatTensor], None]] = None , snake_case :int = 1 , snake_case :Optional[torch.FloatTensor] = None , **snake_case :Optional[Any] , ): '''simple docstring''' if isinstance(snake_case , snake_case ): A_ : Dict = 1 elif isinstance(snake_case , snake_case ): A_ : Optional[Any] = len(snake_case ) else: raise ValueError(f"`prompt` has to be of type `str` or `list` but is {type(snake_case )}" ) if height % 8 != 0 or width % 8 != 0: raise ValueError(f"`height` and `width` have to be divisible by 8 but are {height} and {width}." ) if (callback_steps is None) or ( callback_steps is not None and (not isinstance(snake_case , snake_case ) or callback_steps <= 0) ): raise ValueError( f"`callback_steps` has to be a positive integer but is {callback_steps} of type" f" {type(snake_case )}." ) # get prompt text embeddings A_ : int = self.tokenizer( snake_case , padding="max_length" , max_length=self.tokenizer.model_max_length , return_tensors="pt" , ) A_ : Dict = text_inputs.input_ids if text_input_ids.shape[-1] > self.tokenizer.model_max_length: A_ : Optional[int] = 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}" ) A_ : Tuple = text_input_ids[:, : self.tokenizer.model_max_length] if text_embeddings is None: A_ : Union[str, Any] = self.text_encoder(text_input_ids.to(self.device ) )[0] # duplicate text embeddings for each generation per prompt, using mps friendly method A_ , A_ , A_ : int = text_embeddings.shape A_ : List[str] = text_embeddings.repeat(1 , snake_case , 1 ) A_ : List[str] = text_embeddings.view(bs_embed * num_images_per_prompt , snake_case , -1 ) # here `guidance_scale` is defined analog to the guidance weight `w` of equation (2) # of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1` # corresponds to doing no classifier free guidance. A_ : Dict = guidance_scale > 1.0 # get unconditional embeddings for classifier free guidance if do_classifier_free_guidance: A_ : List[str] if negative_prompt is None: A_ : List[str] = [""] elif type(snake_case ) is not type(snake_case ): raise TypeError( f"`negative_prompt` should be the same type to `prompt`, but got {type(snake_case )} !=" f" {type(snake_case )}." ) elif isinstance(snake_case , snake_case ): A_ : Optional[Any] = [negative_prompt] elif batch_size != len(snake_case ): raise ValueError( f"`negative_prompt`: {negative_prompt} has batch size {len(snake_case )}, but `prompt`:" f" {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches" " the batch size of `prompt`." ) else: A_ : Any = negative_prompt A_ : Optional[int] = text_input_ids.shape[-1] A_ : Dict = self.tokenizer( snake_case , padding="max_length" , max_length=snake_case , truncation=snake_case , return_tensors="pt" , ) A_ : Any = self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # duplicate unconditional embeddings for each generation per prompt, using mps friendly method A_ : Tuple = uncond_embeddings.shape[1] A_ : Dict = uncond_embeddings.repeat(snake_case , snake_case , 1 ) A_ : Dict = uncond_embeddings.view(batch_size * num_images_per_prompt , snake_case , -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 A_ : Optional[int] = torch.cat([uncond_embeddings, text_embeddings] ) # get the initial random noise unless the user supplied it # Unlike in other pipelines, latents need to be generated in the target device # for 1-to-1 results reproducibility with the CompVis implementation. # However this currently doesn't work in `mps`. A_ : List[str] = (batch_size * num_images_per_prompt, self.unet.config.in_channels, height // 8, width // 8) A_ : str = (batch_size * num_images_per_prompt, self.unet.config.in_channels, 64, 64) A_ : List[Any] = text_embeddings.dtype if latents is None: if self.device.type == "mps": # randn does not exist on mps A_ : Tuple = torch.randn( snake_case , generator=snake_case , device="cpu" , dtype=snake_case ).to(self.device ) A_ : Optional[Any] = torch.randn(snake_case , generator=snake_case , device="cpu" , dtype=snake_case ).to( self.device ) else: A_ : int = torch.randn( snake_case , generator=snake_case , device=self.device , dtype=snake_case ) A_ : Optional[int] = torch.randn(snake_case , generator=snake_case , device=self.device , dtype=snake_case ) else: if latents_reference.shape != latents_shape: raise ValueError(f"Unexpected latents shape, got {latents.shape}, expected {latents_shape}" ) A_ : Tuple = latents_reference.to(self.device ) A_ : Any = latents.to(self.device ) # This is the key part of the pipeline where we # try to ensure that the generated images w/ the same seed # but different sizes actually result in similar images A_ : List[Any] = (latents_shape[3] - latents_shape_reference[3]) // 2 A_ : Optional[int] = (latents_shape[2] - latents_shape_reference[2]) // 2 A_ : Optional[int] = latents_shape_reference[3] if dx >= 0 else latents_shape_reference[3] + 2 * dx A_ : Dict = latents_shape_reference[2] if dy >= 0 else latents_shape_reference[2] + 2 * dy A_ : Optional[Any] = 0 if dx < 0 else dx A_ : Optional[Any] = 0 if dy < 0 else dy A_ : List[str] = max(-dx , 0 ) A_ : List[Any] = max(-dy , 0 ) # import pdb # pdb.set_trace() A_ : Any = latents_reference[:, :, dy : dy + h, dx : dx + w] # set timesteps self.scheduler.set_timesteps(snake_case ) # Some schedulers like PNDM have timesteps as arrays # It's more optimized to move all timesteps to correct device beforehand A_ : str = self.scheduler.timesteps.to(self.device ) # scale the initial noise by the standard deviation required by the scheduler A_ : Union[str, Any] = latents * self.scheduler.init_noise_sigma # prepare extra kwargs for the scheduler step, since not all schedulers have the same signature # eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers. # eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502 # and should be between [0, 1] A_ : Optional[int] = "eta" in set(inspect.signature(self.scheduler.step ).parameters.keys() ) A_ : List[str] = {} if accepts_eta: A_ : Union[str, Any] = eta for i, t in enumerate(self.progress_bar(snake_case ) ): # expand the latents if we are doing classifier free guidance A_ : Optional[Any] = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents A_ : Any = self.scheduler.scale_model_input(snake_case , snake_case ) # predict the noise residual A_ : List[str] = self.unet(snake_case , snake_case , encoder_hidden_states=snake_case ).sample # perform guidance if do_classifier_free_guidance: A_ , A_ : Dict = noise_pred.chunk(2 ) A_ : List[Any] = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) # compute the previous noisy sample x_t -> x_t-1 A_ : Tuple = self.scheduler.step(snake_case , snake_case , snake_case , **snake_case ).prev_sample # call the callback, if provided if callback is not None and i % callback_steps == 0: callback(snake_case , snake_case , snake_case ) A_ : List[str] = 1 / 0.18215 * latents A_ : Tuple = self.vae.decode(snake_case ).sample A_ : Dict = (image / 2 + 0.5).clamp(0 , 1 ) # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16 A_ : List[str] = image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() if self.safety_checker is not None: A_ : int = self.feature_extractor(self.numpy_to_pil(snake_case ) , return_tensors="pt" ).to( self.device ) A_ , A_ : List[str] = self.safety_checker( images=snake_case , clip_input=safety_checker_input.pixel_values.to(text_embeddings.dtype ) ) else: A_ : List[str] = None if output_type == "pil": A_ : Optional[int] = self.numpy_to_pil(snake_case ) if not return_dict: return (image, has_nsfw_concept) return StableDiffusionPipelineOutput(images=snake_case , nsfw_content_detected=snake_case )
300
0
'''simple docstring''' import gc import math import unittest import torch from diffusers import UNetaDModel from diffusers.utils import floats_tensor, logging, slow, torch_all_close, torch_device from diffusers.utils.testing_utils import enable_full_determinism from .test_modeling_common import ModelTesterMixin, UNetTesterMixin lowercase : Dict = logging.get_logger(__name__) enable_full_determinism() class __UpperCAmelCase ( _lowerCamelCase , _lowerCamelCase , unittest.TestCase ): __lowercase = UNetaDModel __lowercase = """sample""" @property def lowerCamelCase ( self ): """simple docstring""" _snake_case = 4 _snake_case = 3 _snake_case = (32, 32) _snake_case = floats_tensor((batch_size, num_channels) + sizes ).to(lowerCAmelCase_ ) _snake_case = torch.tensor([10] ).to(lowerCAmelCase_ ) return {"sample": noise, "timestep": time_step} @property def lowerCamelCase ( self ): """simple docstring""" return (3, 32, 32) @property def lowerCamelCase ( self ): """simple docstring""" return (3, 32, 32) def lowerCamelCase ( self ): """simple docstring""" _snake_case = { 'block_out_channels': (32, 64), 'down_block_types': ('DownBlock2D', 'AttnDownBlock2D'), 'up_block_types': ('AttnUpBlock2D', 'UpBlock2D'), 'attention_head_dim': 3, 'out_channels': 3, 'in_channels': 3, 'layers_per_block': 2, 'sample_size': 32, } _snake_case = self.dummy_input return init_dict, inputs_dict class __UpperCAmelCase ( _lowerCamelCase , _lowerCamelCase , unittest.TestCase ): __lowercase = UNetaDModel __lowercase = """sample""" @property def lowerCamelCase ( self ): """simple docstring""" _snake_case = 4 _snake_case = 4 _snake_case = (32, 32) _snake_case = floats_tensor((batch_size, num_channels) + sizes ).to(lowerCAmelCase_ ) _snake_case = torch.tensor([10] ).to(lowerCAmelCase_ ) return {"sample": noise, "timestep": time_step} @property def lowerCamelCase ( self ): """simple docstring""" return (4, 32, 32) @property def lowerCamelCase ( self ): """simple docstring""" return (4, 32, 32) def lowerCamelCase ( self ): """simple docstring""" _snake_case = { 'sample_size': 32, 'in_channels': 4, 'out_channels': 4, 'layers_per_block': 2, 'block_out_channels': (32, 64), 'attention_head_dim': 32, 'down_block_types': ('DownBlock2D', 'DownBlock2D'), 'up_block_types': ('UpBlock2D', 'UpBlock2D'), } _snake_case = self.dummy_input return init_dict, inputs_dict def lowerCamelCase ( self ): """simple docstring""" _snake_case , _snake_case = UNetaDModel.from_pretrained('fusing/unet-ldm-dummy-update' , output_loading_info=lowerCAmelCase_ ) self.assertIsNotNone(lowerCAmelCase_ ) self.assertEqual(len(loading_info['missing_keys'] ) , 0 ) model.to(lowerCAmelCase_ ) _snake_case = model(**self.dummy_input ).sample assert image is not None, "Make sure output is not None" @unittest.skipIf(torch_device != 'cuda' , 'This test is supposed to run on GPU' ) def lowerCamelCase ( self ): """simple docstring""" _snake_case , _snake_case = UNetaDModel.from_pretrained('fusing/unet-ldm-dummy-update' , output_loading_info=lowerCAmelCase_ ) model.to(lowerCAmelCase_ ) _snake_case = model(**self.dummy_input ).sample assert image is not None, "Make sure output is not None" @unittest.skipIf(torch_device != 'cuda' , 'This test is supposed to run on GPU' ) def lowerCamelCase ( self ): """simple docstring""" _snake_case , _snake_case = UNetaDModel.from_pretrained('fusing/unet-ldm-dummy-update' , output_loading_info=lowerCAmelCase_ ) model_accelerate.to(lowerCAmelCase_ ) model_accelerate.eval() _snake_case = torch.randn( 1 , model_accelerate.config.in_channels , model_accelerate.config.sample_size , model_accelerate.config.sample_size , generator=torch.manual_seed(0 ) , ) _snake_case = noise.to(lowerCAmelCase_ ) _snake_case = torch.tensor([10] * noise.shape[0] ).to(lowerCAmelCase_ ) _snake_case = model_accelerate(lowerCAmelCase_ , lowerCAmelCase_ )['sample'] # two models don't need to stay in the device at the same time del model_accelerate torch.cuda.empty_cache() gc.collect() _snake_case , _snake_case = UNetaDModel.from_pretrained( 'fusing/unet-ldm-dummy-update' , output_loading_info=lowerCAmelCase_ , low_cpu_mem_usage=lowerCAmelCase_ ) model_normal_load.to(lowerCAmelCase_ ) model_normal_load.eval() _snake_case = model_normal_load(lowerCAmelCase_ , lowerCAmelCase_ )['sample'] assert torch_all_close(lowerCAmelCase_ , lowerCAmelCase_ , rtol=1E-3 ) def lowerCamelCase ( self ): """simple docstring""" _snake_case = UNetaDModel.from_pretrained('fusing/unet-ldm-dummy-update' ) model.eval() model.to(lowerCAmelCase_ ) _snake_case = torch.randn( 1 , model.config.in_channels , model.config.sample_size , model.config.sample_size , generator=torch.manual_seed(0 ) , ) _snake_case = noise.to(lowerCAmelCase_ ) _snake_case = torch.tensor([10] * noise.shape[0] ).to(lowerCAmelCase_ ) with torch.no_grad(): _snake_case = model(lowerCAmelCase_ , lowerCAmelCase_ ).sample _snake_case = output[0, -1, -3:, -3:].flatten().cpu() # fmt: off _snake_case = torch.tensor([-13.3258, -20.1100, -15.9873, -17.6617, -23.0596, -17.9419, -13.3675, -16.1889, -12.3800] ) # fmt: on self.assertTrue(torch_all_close(lowerCAmelCase_ , lowerCAmelCase_ , rtol=1E-3 ) ) class __UpperCAmelCase ( _lowerCamelCase , _lowerCamelCase , unittest.TestCase ): __lowercase = UNetaDModel __lowercase = """sample""" @property def lowerCamelCase ( self , lowerCAmelCase_=(32, 32) ): """simple docstring""" _snake_case = 4 _snake_case = 3 _snake_case = floats_tensor((batch_size, num_channels) + sizes ).to(lowerCAmelCase_ ) _snake_case = torch.tensor(batch_size * [10] ).to(dtype=torch.intaa , device=lowerCAmelCase_ ) return {"sample": noise, "timestep": time_step} @property def lowerCamelCase ( self ): """simple docstring""" return (3, 32, 32) @property def lowerCamelCase ( self ): """simple docstring""" return (3, 32, 32) def lowerCamelCase ( self ): """simple docstring""" _snake_case = { 'block_out_channels': [32, 64, 64, 64], 'in_channels': 3, 'layers_per_block': 1, 'out_channels': 3, 'time_embedding_type': 'fourier', 'norm_eps': 1E-6, 'mid_block_scale_factor': math.sqrt(2.0 ), 'norm_num_groups': None, 'down_block_types': [ 'SkipDownBlock2D', 'AttnSkipDownBlock2D', 'SkipDownBlock2D', 'SkipDownBlock2D', ], 'up_block_types': [ 'SkipUpBlock2D', 'SkipUpBlock2D', 'AttnSkipUpBlock2D', 'SkipUpBlock2D', ], } _snake_case = self.dummy_input return init_dict, inputs_dict @slow def lowerCamelCase ( self ): """simple docstring""" _snake_case , _snake_case = UNetaDModel.from_pretrained('google/ncsnpp-celebahq-256' , output_loading_info=lowerCAmelCase_ ) self.assertIsNotNone(lowerCAmelCase_ ) self.assertEqual(len(loading_info['missing_keys'] ) , 0 ) model.to(lowerCAmelCase_ ) _snake_case = self.dummy_input _snake_case = floats_tensor((4, 3) + (2_56, 2_56) ).to(lowerCAmelCase_ ) _snake_case = noise _snake_case = model(**lowerCAmelCase_ ) assert image is not None, "Make sure output is not None" @slow def lowerCamelCase ( self ): """simple docstring""" _snake_case = UNetaDModel.from_pretrained('google/ncsnpp-celebahq-256' ) model.to(lowerCAmelCase_ ) _snake_case = 4 _snake_case = 3 _snake_case = (2_56, 2_56) _snake_case = torch.ones((batch_size, num_channels) + sizes ).to(lowerCAmelCase_ ) _snake_case = torch.tensor(batch_size * [1E-4] ).to(lowerCAmelCase_ ) with torch.no_grad(): _snake_case = model(lowerCAmelCase_ , lowerCAmelCase_ ).sample _snake_case = output[0, -3:, -3:, -1].flatten().cpu() # fmt: off _snake_case = torch.tensor([-4842.8691, -6499.6631, -3800.1953, -7978.2686, -10980.7129, -20028.8535, 8148.2822, 2342.2905, 567.7608] ) # fmt: on self.assertTrue(torch_all_close(lowerCAmelCase_ , lowerCAmelCase_ , rtol=1E-2 ) ) def lowerCamelCase ( self ): """simple docstring""" _snake_case = UNetaDModel.from_pretrained('fusing/ncsnpp-ffhq-ve-dummy-update' ) model.to(lowerCAmelCase_ ) _snake_case = 4 _snake_case = 3 _snake_case = (32, 32) _snake_case = torch.ones((batch_size, num_channels) + sizes ).to(lowerCAmelCase_ ) _snake_case = torch.tensor(batch_size * [1E-4] ).to(lowerCAmelCase_ ) with torch.no_grad(): _snake_case = model(lowerCAmelCase_ , lowerCAmelCase_ ).sample _snake_case = output[0, -3:, -3:, -1].flatten().cpu() # fmt: off _snake_case = torch.tensor([-0.0325, -0.0900, -0.0869, -0.0332, -0.0725, -0.0270, -0.0101, 0.0227, 0.0256] ) # fmt: on self.assertTrue(torch_all_close(lowerCAmelCase_ , lowerCAmelCase_ , rtol=1E-2 ) ) def lowerCamelCase ( self ): """simple docstring""" pass
42
import torch import torch.nn as nn from transformers import CLIPConfig, CLIPVisionModel, PreTrainedModel from ...utils import logging _lowerCAmelCase : List[str] = logging.get_logger(__name__) def __snake_case ( _lowerCAmelCase : int , _lowerCAmelCase : Any ) -> Dict: A_ : Optional[Any] = nn.functional.normalize(_lowerCAmelCase ) A_ : List[str] = nn.functional.normalize(_lowerCAmelCase ) return torch.mm(_lowerCAmelCase , normalized_text_embeds.t() ) class __magic_name__ ( lowerCamelCase__ ): """simple docstring""" __UpperCamelCase = CLIPConfig __UpperCamelCase = ['''CLIPEncoderLayer'''] def __init__( self :int , snake_case :CLIPConfig ): '''simple docstring''' super().__init__(snake_case ) A_ : int = CLIPVisionModel(config.vision_config ) A_ : List[str] = nn.Linear(config.vision_config.hidden_size , config.projection_dim , bias=snake_case ) A_ : Tuple = nn.Parameter(torch.ones(17 , config.projection_dim ) , requires_grad=snake_case ) A_ : str = nn.Parameter(torch.ones(3 , config.projection_dim ) , requires_grad=snake_case ) A_ : List[str] = nn.Parameter(torch.ones(17 ) , requires_grad=snake_case ) A_ : int = nn.Parameter(torch.ones(3 ) , requires_grad=snake_case ) @torch.no_grad() def SCREAMING_SNAKE_CASE ( self :Union[str, Any] , snake_case :Dict , snake_case :Any ): '''simple docstring''' A_ : List[Any] = self.vision_model(snake_case )[1] # pooled_output A_ : List[Any] = self.visual_projection(snake_case ) # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16 A_ : Optional[Any] = cosine_distance(snake_case , self.special_care_embeds ).cpu().float().numpy() A_ : Tuple = cosine_distance(snake_case , self.concept_embeds ).cpu().float().numpy() A_ : Union[str, Any] = [] A_ : Any = image_embeds.shape[0] for i in range(snake_case ): A_ : Optional[int] = {"special_scores": {}, "special_care": [], "concept_scores": {}, "bad_concepts": []} # increase this value to create a stronger `nfsw` filter # at the cost of increasing the possibility of filtering benign images A_ : Optional[Any] = 0.0 for concept_idx in range(len(special_cos_dist[0] ) ): A_ : Optional[Any] = special_cos_dist[i][concept_idx] A_ : Tuple = self.special_care_embeds_weights[concept_idx].item() A_ : Union[str, Any] = round(concept_cos - concept_threshold + adjustment , 3 ) if result_img["special_scores"][concept_idx] > 0: result_img["special_care"].append({concept_idx, result_img["special_scores"][concept_idx]} ) A_ : Any = 0.01 for concept_idx in range(len(cos_dist[0] ) ): A_ : Tuple = cos_dist[i][concept_idx] A_ : Tuple = self.concept_embeds_weights[concept_idx].item() A_ : Tuple = round(concept_cos - concept_threshold + adjustment , 3 ) if result_img["concept_scores"][concept_idx] > 0: result_img["bad_concepts"].append(snake_case ) result.append(snake_case ) A_ : Any = [len(res["bad_concepts"] ) > 0 for res in result] return images, has_nsfw_concepts @torch.no_grad() def SCREAMING_SNAKE_CASE ( self :Union[str, Any] , snake_case :torch.FloatTensor , snake_case :torch.FloatTensor ): '''simple docstring''' A_ : List[str] = self.vision_model(snake_case )[1] # pooled_output A_ : int = self.visual_projection(snake_case ) A_ : Tuple = cosine_distance(snake_case , self.special_care_embeds ) A_ : Tuple = cosine_distance(snake_case , self.concept_embeds ) # increase this value to create a stronger `nsfw` filter # at the cost of increasing the possibility of filtering benign images A_ : Optional[Any] = 0.0 A_ : Tuple = special_cos_dist - self.special_care_embeds_weights + adjustment # special_scores = special_scores.round(decimals=3) A_ : Optional[Any] = torch.any(special_scores > 0 , dim=1 ) A_ : Optional[Any] = special_care * 0.01 A_ : Optional[int] = special_adjustment.unsqueeze(1 ).expand(-1 , cos_dist.shape[1] ) A_ : Union[str, Any] = (cos_dist - self.concept_embeds_weights) + special_adjustment # concept_scores = concept_scores.round(decimals=3) A_ : Union[str, Any] = torch.any(concept_scores > 0 , dim=1 ) return images, has_nsfw_concepts
300
0
def lowerCamelCase ( SCREAMING_SNAKE_CASE ): '''simple docstring''' if not isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): raise TypeError('''Input value must be an \'int\' type''' ) __UpperCamelCase :List[str] = 0 while number: position += 1 number >>= 1 return position if __name__ == "__main__": import doctest doctest.testmod()
43
import argparse from pathlib import Path import requests import torch from PIL import Image from transformers import ( RobertaTokenizer, TrOCRConfig, TrOCRForCausalLM, TrOCRProcessor, VisionEncoderDecoderModel, ViTConfig, ViTImageProcessor, ViTModel, ) from transformers.utils import logging logging.set_verbosity_info() _lowerCAmelCase : List[str] = logging.get_logger(__name__) def __snake_case ( _lowerCAmelCase : Optional[int] , _lowerCAmelCase : Union[str, Any] ) -> Optional[int]: A_ : Tuple = [] 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 __snake_case ( _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : Union[str, Any] ) -> Dict: for i in range(encoder_config.num_hidden_layers ): # queries, keys and values (only weights, no biases) A_ : str = state_dict.pop(f"encoder.deit.blocks.{i}.attn.qkv.weight" ) A_ : List[Any] = in_proj_weight[ : encoder_config.hidden_size, : ] A_ : Optional[Any] = in_proj_weight[ encoder_config.hidden_size : encoder_config.hidden_size * 2, : ] A_ : Optional[Any] = in_proj_weight[ -encoder_config.hidden_size :, : ] def __snake_case ( _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : Tuple , _lowerCAmelCase : Dict ) -> Any: A_ : Dict = dct.pop(_lowerCAmelCase ) A_ : List[Any] = val def __snake_case ( _lowerCAmelCase : List[str] ) -> int: if "handwritten" in checkpoint_url: A_ : Any = "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: A_ : Any = "https://www.researchgate.net/profile/Dinh-Sang/publication/338099565/figure/fig8/AS:840413229350922@1577381536857/An-receipt-example-in-the-SROIE-2019-dataset_Q640.jpg" A_ : List[Any] = Image.open(requests.get(_lowerCAmelCase , stream=_lowerCAmelCase ).raw ).convert("RGB" ) return im @torch.no_grad() def __snake_case ( _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : Union[str, Any] ) -> List[Any]: A_ : Optional[Any] = ViTConfig(image_size=384 , qkv_bias=_lowerCAmelCase ) A_ : Tuple = TrOCRConfig() # size of the architecture if "base" in checkpoint_url: A_ : Tuple = 768 elif "large" in checkpoint_url: # use ViT-large encoder A_ : Optional[Any] = 1024 A_ : Union[str, Any] = 4096 A_ : Union[str, Any] = 24 A_ : List[Any] = 16 A_ : List[str] = 1024 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: A_ : Dict = False A_ : int = "relu" A_ : Optional[int] = 1024 A_ : Any = True A_ : List[Any] = False A_ : Optional[int] = False # load HuggingFace model A_ : Union[str, Any] = ViTModel(_lowerCAmelCase , add_pooling_layer=_lowerCAmelCase ) A_ : str = TrOCRForCausalLM(_lowerCAmelCase ) A_ : List[str] = VisionEncoderDecoderModel(encoder=_lowerCAmelCase , decoder=_lowerCAmelCase ) model.eval() # load state_dict of original model, rename some keys A_ : Optional[int] = torch.hub.load_state_dict_from_url(_lowerCAmelCase , map_location="cpu" , check_hash=_lowerCAmelCase )["model"] A_ : Dict = create_rename_keys(_lowerCAmelCase , _lowerCAmelCase ) for src, dest in rename_keys: rename_key(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) read_in_q_k_v(_lowerCAmelCase , _lowerCAmelCase ) # 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(): A_ : Dict = state_dict.pop(_lowerCAmelCase ) if key.startswith("decoder" ) and "output_projection" not in key: A_ : List[str] = val else: A_ : Optional[Any] = val # load state dict model.load_state_dict(_lowerCAmelCase ) # Check outputs on an image A_ : List[Any] = ViTImageProcessor(size=encoder_config.image_size ) A_ : Any = RobertaTokenizer.from_pretrained("roberta-large" ) A_ : Union[str, Any] = TrOCRProcessor(_lowerCAmelCase , _lowerCAmelCase ) A_ : List[str] = processor(images=prepare_img(_lowerCAmelCase ) , return_tensors="pt" ).pixel_values # verify logits A_ : Union[str, Any] = torch.tensor([[model.config.decoder.decoder_start_token_id]] ) A_ : Optional[int] = model(pixel_values=_lowerCAmelCase , decoder_input_ids=_lowerCAmelCase ) A_ : Tuple = outputs.logits A_ : Union[str, Any] = torch.Size([1, 1, 50265] ) if "trocr-base-handwritten" in checkpoint_url: A_ : Union[str, Any] = torch.tensor( [-1.45_02, -4.66_83, -0.53_47, -2.92_91, 9.14_35, -3.05_71, 8.97_64, 1.75_60, 8.73_58, -1.53_11] ) elif "trocr-large-handwritten" in checkpoint_url: A_ : str = torch.tensor( [-2.64_37, -1.31_29, -2.25_96, -5.34_55, 6.35_39, 1.76_04, 5.49_91, 1.47_02, 5.61_13, 2.01_70] ) elif "trocr-base-printed" in checkpoint_url: A_ : Optional[Any] = torch.tensor( [-5.68_16, -5.83_88, 1.13_98, -6.90_34, 6.85_05, -2.43_93, 1.22_84, -1.02_32, -1.96_61, -3.92_10] ) elif "trocr-large-printed" in checkpoint_url: A_ : Optional[int] = torch.tensor( [-6.01_62, -7.09_59, 4.41_55, -5.10_63, 7.04_68, -3.16_31, 2.64_66, -0.30_81, -0.81_06, -1.75_35] ) if "stage1" not in checkpoint_url: assert logits.shape == expected_shape, "Shape of logits not as expected" assert torch.allclose(logits[0, 0, :10] , _lowerCAmelCase , atol=1e-3 ), "First elements of logits not as expected" Path(_lowerCAmelCase ).mkdir(exist_ok=_lowerCAmelCase ) print(f"Saving model to {pytorch_dump_folder_path}" ) model.save_pretrained(_lowerCAmelCase ) print(f"Saving processor to {pytorch_dump_folder_path}" ) processor.save_pretrained(_lowerCAmelCase ) if __name__ == "__main__": _lowerCAmelCase : Dict = argparse.ArgumentParser() parser.add_argument( '''--checkpoint_url''', default='''https://layoutlm.blob.core.windows.net/trocr/model_zoo/fairseq/trocr-base-handwritten.pt''', type=str, help='''URL to the original PyTorch checkpoint (.pth file).''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the folder to output PyTorch model.''' ) _lowerCAmelCase : List[str] = parser.parse_args() convert_tr_ocr_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)
300
0
"""simple docstring""" def SCREAMING_SNAKE_CASE ( _lowerCamelCase : Tuple ) -> Any: _lowerCAmelCase : Optional[int] = len(_lowerCamelCase ) while cur > 1: # Find the maximum number in arr _lowerCAmelCase : Any = arr.index(max(arr[0:cur] ) ) # Reverse from 0 to mi _lowerCAmelCase : Union[str, Any] = arr[mi::-1] + arr[mi + 1 : len(_lowerCamelCase )] # Reverse whole list _lowerCAmelCase : Dict = arr[cur - 1 :: -1] + arr[cur : len(_lowerCamelCase )] cur -= 1 return arr if __name__ == "__main__": _a : Union[str, Any] = input('Enter numbers separated by a comma:\n').strip() _a : Dict = [int(item) for item in user_input.split(',')] print(pancake_sort(unsorted))
44
# DISCLAIMER: This file is strongly influenced by https://github.com/yang-song/score_sde_pytorch import math from dataclasses import dataclass from typing import Optional, Tuple, Union import torch from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput, randn_tensor from .scheduling_utils import SchedulerMixin, SchedulerOutput @dataclass class __magic_name__ ( lowerCamelCase__ ): """simple docstring""" __UpperCamelCase = 42 __UpperCamelCase = 42 class __magic_name__ ( lowerCamelCase__ , lowerCamelCase__ ): """simple docstring""" __UpperCamelCase = 1 @register_to_config def __init__( self :Union[str, Any] , snake_case :int = 2_000 , snake_case :float = 0.15 , snake_case :float = 0.01 , snake_case :float = 1348.0 , snake_case :float = 1e-5 , snake_case :int = 1 , ): '''simple docstring''' A_ : Dict = sigma_max # setable values A_ : List[Any] = None self.set_sigmas(snake_case , snake_case , snake_case , snake_case ) def SCREAMING_SNAKE_CASE ( self :Any , snake_case :torch.FloatTensor , snake_case :Optional[int] = None ): '''simple docstring''' return sample def SCREAMING_SNAKE_CASE ( self :Optional[Any] , snake_case :int , snake_case :float = None , snake_case :Union[str, torch.device] = None ): '''simple docstring''' A_ : Optional[Any] = sampling_eps if sampling_eps is not None else self.config.sampling_eps A_ : Tuple = torch.linspace(1 , snake_case , snake_case , device=snake_case ) def SCREAMING_SNAKE_CASE ( self :Dict , snake_case :int , snake_case :float = None , snake_case :float = None , snake_case :float = None ): '''simple docstring''' A_ : Union[str, Any] = sigma_min if sigma_min is not None else self.config.sigma_min A_ : Any = sigma_max if sigma_max is not None else self.config.sigma_max A_ : Dict = sampling_eps if sampling_eps is not None else self.config.sampling_eps if self.timesteps is None: self.set_timesteps(snake_case , snake_case ) A_ : str = sigma_min * (sigma_max / sigma_min) ** (self.timesteps / sampling_eps) A_ : Any = torch.exp(torch.linspace(math.log(snake_case ) , math.log(snake_case ) , snake_case ) ) A_ : str = torch.tensor([sigma_min * (sigma_max / sigma_min) ** t for t in self.timesteps] ) def SCREAMING_SNAKE_CASE ( self :List[str] , snake_case :List[str] , snake_case :Dict ): '''simple docstring''' return torch.where( timesteps == 0 , torch.zeros_like(t.to(timesteps.device ) ) , self.discrete_sigmas[timesteps - 1].to(timesteps.device ) , ) def SCREAMING_SNAKE_CASE ( self :Union[str, Any] , snake_case :torch.FloatTensor , snake_case :int , snake_case :torch.FloatTensor , snake_case :Optional[torch.Generator] = None , snake_case :bool = True , ): '''simple docstring''' if self.timesteps is None: raise ValueError( "`self.timesteps` is not set, you need to run 'set_timesteps' after creating the scheduler" ) A_ : int = timestep * torch.ones( sample.shape[0] , device=sample.device ) # torch.repeat_interleave(timestep, sample.shape[0]) A_ : Optional[Any] = (timestep * (len(self.timesteps ) - 1)).long() # mps requires indices to be in the same device, so we use cpu as is the default with cuda A_ : Dict = timesteps.to(self.discrete_sigmas.device ) A_ : Optional[int] = self.discrete_sigmas[timesteps].to(sample.device ) A_ : int = self.get_adjacent_sigma(snake_case , snake_case ).to(sample.device ) A_ : Union[str, Any] = torch.zeros_like(snake_case ) A_ : Tuple = (sigma**2 - adjacent_sigma**2) ** 0.5 # equation 6 in the paper: the model_output modeled by the network is grad_x log pt(x) # also equation 47 shows the analog from SDE models to ancestral sampling methods A_ : Optional[int] = diffusion.flatten() while len(diffusion.shape ) < len(sample.shape ): A_ : Tuple = diffusion.unsqueeze(-1 ) A_ : Optional[Any] = drift - diffusion**2 * model_output # equation 6: sample noise for the diffusion term of A_ : List[Any] = randn_tensor( sample.shape , layout=sample.layout , generator=snake_case , device=sample.device , dtype=sample.dtype ) A_ : List[Any] = sample - drift # subtract because `dt` is a small negative timestep # TODO is the variable diffusion the correct scaling term for the noise? A_ : Any = prev_sample_mean + diffusion * noise # add impact of diffusion field g if not return_dict: return (prev_sample, prev_sample_mean) return SdeVeOutput(prev_sample=snake_case , prev_sample_mean=snake_case ) def SCREAMING_SNAKE_CASE ( self :Tuple , snake_case :torch.FloatTensor , snake_case :torch.FloatTensor , snake_case :Optional[torch.Generator] = None , snake_case :bool = True , ): '''simple docstring''' if self.timesteps is None: raise ValueError( "`self.timesteps` is not set, you need to run 'set_timesteps' after creating the scheduler" ) # For small batch sizes, the paper "suggest replacing norm(z) with sqrt(d), where d is the dim. of z" # sample noise for correction A_ : Dict = randn_tensor(sample.shape , layout=sample.layout , generator=snake_case ).to(sample.device ) # compute step size from the model_output, the noise, and the snr A_ : int = torch.norm(model_output.reshape(model_output.shape[0] , -1 ) , dim=-1 ).mean() A_ : List[Any] = torch.norm(noise.reshape(noise.shape[0] , -1 ) , dim=-1 ).mean() A_ : Dict = (self.config.snr * noise_norm / grad_norm) ** 2 * 2 A_ : Dict = step_size * torch.ones(sample.shape[0] ).to(sample.device ) # self.repeat_scalar(step_size, sample.shape[0]) # compute corrected sample: model_output term and noise term A_ : int = step_size.flatten() while len(step_size.shape ) < len(sample.shape ): A_ : str = step_size.unsqueeze(-1 ) A_ : Optional[Any] = sample + step_size * model_output A_ : Tuple = prev_sample_mean + ((step_size * 2) ** 0.5) * noise if not return_dict: return (prev_sample,) return SchedulerOutput(prev_sample=snake_case ) def SCREAMING_SNAKE_CASE ( self :Tuple , snake_case :torch.FloatTensor , snake_case :torch.FloatTensor , snake_case :torch.FloatTensor , ): '''simple docstring''' A_ : Union[str, Any] = timesteps.to(original_samples.device ) A_ : List[Any] = self.discrete_sigmas.to(original_samples.device )[timesteps] A_ : List[Any] = ( noise * sigmas[:, None, None, None] if noise is not None else torch.randn_like(snake_case ) * sigmas[:, None, None, None] ) A_ : Optional[int] = noise + original_samples return noisy_samples def __len__( self :Union[str, Any] ): '''simple docstring''' return self.config.num_train_timesteps
300
0
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available, ) lowercase_ = {"configuration_unispeech": ["UNISPEECH_PRETRAINED_CONFIG_ARCHIVE_MAP", "UniSpeechConfig"]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "UNISPEECH_PRETRAINED_MODEL_ARCHIVE_LIST", "UniSpeechForCTC", "UniSpeechForPreTraining", "UniSpeechForSequenceClassification", "UniSpeechModel", "UniSpeechPreTrainedModel", ] if TYPE_CHECKING: from .configuration_unispeech import UNISPEECH_PRETRAINED_CONFIG_ARCHIVE_MAP, UniSpeechConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_unispeech import ( UNISPEECH_PRETRAINED_MODEL_ARCHIVE_LIST, UniSpeechForCTC, UniSpeechForPreTraining, UniSpeechForSequenceClassification, UniSpeechModel, UniSpeechPreTrainedModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
45
from __future__ import annotations from decimal import Decimal from math import * # noqa: F403 from sympy import diff def __snake_case ( _lowerCAmelCase : str , _lowerCAmelCase : float | Decimal , _lowerCAmelCase : float = 10**-10 ) -> float: A_ : Dict = a while True: A_ : Union[str, Any] = Decimal(_lowerCAmelCase ) - ( Decimal(eval(_lowerCAmelCase ) ) / Decimal(eval(str(diff(_lowerCAmelCase ) ) ) ) # noqa: S307 ) # This number dictates the accuracy of the answer if abs(eval(_lowerCAmelCase ) ) < precision: # noqa: S307 return float(_lowerCAmelCase ) # Let's Execute if __name__ == "__main__": # Find root of trigonometric function # Find value of pi print(F'''The root of sin(x) = 0 is {newton_raphson("sin(x)", 2)}''') # Find root of polynomial print(F'''The root of x**2 - 5*x + 2 = 0 is {newton_raphson("x**2 - 5*x + 2", 0.4)}''') # Find Square Root of 5 print(F'''The root of log(x) - 1 = 0 is {newton_raphson("log(x) - 1", 2)}''') # Exponential Roots print(F'''The root of exp(x) - 1 = 0 is {newton_raphson("exp(x) - 1", 0)}''')
300
0
"""simple docstring""" def UpperCAmelCase__ ( SCREAMING_SNAKE_CASE : int = 1 , SCREAMING_SNAKE_CASE : int = 10_00 ): '''simple docstring''' lowerCAmelCase = 1 lowerCAmelCase = 0 for divide_by_number in range(SCREAMING_SNAKE_CASE , digit + 1 ): lowerCAmelCase = [] lowerCAmelCase = numerator for _ in range(1 , digit + 1 ): if now_divide in has_been_divided: if longest_list_length < len(SCREAMING_SNAKE_CASE ): lowerCAmelCase = len(SCREAMING_SNAKE_CASE ) lowerCAmelCase = divide_by_number else: has_been_divided.append(SCREAMING_SNAKE_CASE ) lowerCAmelCase = now_divide * 10 % divide_by_number return the_digit # Tests if __name__ == "__main__": import doctest doctest.testmod()
46
import sacrebleu as scb from packaging import version from sacrebleu import TER import datasets _lowerCAmelCase : List[Any] = '''\ @inproceedings{snover-etal-2006-study, title = "A Study of Translation Edit Rate with Targeted Human Annotation", author = "Snover, Matthew and Dorr, Bonnie and Schwartz, Rich and Micciulla, Linnea and Makhoul, John", booktitle = "Proceedings of the 7th Conference of the Association for Machine Translation in the Americas: Technical Papers", month = aug # " 8-12", year = "2006", address = "Cambridge, Massachusetts, USA", publisher = "Association for Machine Translation in the Americas", url = "https://aclanthology.org/2006.amta-papers.25", pages = "223--231", } @inproceedings{post-2018-call, title = "A Call for Clarity in Reporting {BLEU} Scores", author = "Post, Matt", booktitle = "Proceedings of the Third Conference on Machine Translation: Research Papers", month = oct, year = "2018", address = "Belgium, Brussels", publisher = "Association for Computational Linguistics", url = "https://www.aclweb.org/anthology/W18-6319", pages = "186--191", } ''' _lowerCAmelCase : Union[str, Any] = '''\ TER (Translation Edit Rate, also called Translation Error Rate) is a metric to quantify the edit operations that a hypothesis requires to match a reference translation. We use the implementation that is already present in sacrebleu (https://github.com/mjpost/sacreBLEU#ter), which in turn is inspired by the TERCOM implementation, which can be found here: https://github.com/jhclark/tercom. The implementation here is slightly different from sacrebleu in terms of the required input format. The length of the references and hypotheses lists need to be the same, so you may need to transpose your references compared to sacrebleu\'s required input format. See https://github.com/huggingface/datasets/issues/3154#issuecomment-950746534 See the README.md file at https://github.com/mjpost/sacreBLEU#ter for more information. ''' _lowerCAmelCase : Optional[Any] = ''' Produces TER scores alongside the number of edits and reference length. Args: predictions (list of str): The system stream (a sequence of segments). references (list of list of str): A list of one or more reference streams (each a sequence of segments). normalized (boolean): If `True`, applies basic tokenization and normalization to sentences. Defaults to `False`. ignore_punct (boolean): If `True`, applies basic tokenization and normalization to sentences. Defaults to `False`. support_zh_ja_chars (boolean): If `True`, tokenization/normalization supports processing of Chinese characters, as well as Japanese Kanji, Hiragana, Katakana, and Phonetic Extensions of Katakana. Only applies if `normalized = True`. Defaults to `False`. case_sensitive (boolean): If `False`, makes all predictions and references lowercase to ignore differences in case. Defaults to `False`. Returns: \'score\' (float): TER score (num_edits / sum_ref_lengths * 100) \'num_edits\' (int): The cumulative number of edits \'ref_length\' (float): The cumulative average reference length Examples: Example 1: >>> predictions = ["does this sentence match??", ... "what about this sentence?", ... "What did the TER metric user say to the developer?"] >>> references = [["does this sentence match", "does this sentence match!?!"], ... ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"], ... ["Your jokes are...", "...TERrible"]] >>> ter = datasets.load_metric("ter") >>> results = ter.compute(predictions=predictions, ... references=references, ... case_sensitive=True) >>> print(results) {\'score\': 150.0, \'num_edits\': 15, \'ref_length\': 10.0} Example 2: >>> predictions = ["does this sentence match??", ... "what about this sentence?"] >>> references = [["does this sentence match", "does this sentence match!?!"], ... ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"]] >>> ter = datasets.load_metric("ter") >>> results = ter.compute(predictions=predictions, ... references=references, ... case_sensitive=True) >>> print(results) {\'score\': 62.5, \'num_edits\': 5, \'ref_length\': 8.0} Example 3: >>> predictions = ["does this sentence match??", ... "what about this sentence?"] >>> references = [["does this sentence match", "does this sentence match!?!"], ... ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"]] >>> ter = datasets.load_metric("ter") >>> results = ter.compute(predictions=predictions, ... references=references, ... normalized=True, ... case_sensitive=True) >>> print(results) {\'score\': 57.14285714285714, \'num_edits\': 6, \'ref_length\': 10.5} Example 4: >>> predictions = ["does this sentence match??", ... "what about this sentence?"] >>> references = [["does this sentence match", "does this sentence match!?!"], ... ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"]] >>> ter = datasets.load_metric("ter") >>> results = ter.compute(predictions=predictions, ... references=references, ... ignore_punct=True, ... case_sensitive=False) >>> print(results) {\'score\': 0.0, \'num_edits\': 0, \'ref_length\': 8.0} Example 5: >>> predictions = ["does this sentence match??", ... "what about this sentence?", ... "What did the TER metric user say to the developer?"] >>> references = [["does this sentence match", "does this sentence match!?!"], ... ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"], ... ["Your jokes are...", "...TERrible"]] >>> ter = datasets.load_metric("ter") >>> results = ter.compute(predictions=predictions, ... references=references, ... ignore_punct=True, ... case_sensitive=False) >>> print(results) {\'score\': 100.0, \'num_edits\': 10, \'ref_length\': 10.0} ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __magic_name__ ( datasets.Metric ): """simple docstring""" def SCREAMING_SNAKE_CASE ( self :Dict ): '''simple docstring''' if version.parse(scb.__version__ ) < version.parse("1.4.12" ): raise ImportWarning( "To use `sacrebleu`, the module `sacrebleu>=1.4.12` is required, and the current version of `sacrebleu` doesn't match this condition.\n" "You can install it with `pip install \"sacrebleu>=1.4.12\"`." ) return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , homepage="http://www.cs.umd.edu/~snover/tercom/" , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "predictions": datasets.Value("string" , id="sequence" ), "references": datasets.Sequence(datasets.Value("string" , id="sequence" ) , id="references" ), } ) , codebase_urls=["https://github.com/mjpost/sacreBLEU#ter"] , reference_urls=[ "https://github.com/jhclark/tercom", ] , ) def SCREAMING_SNAKE_CASE ( self :Union[str, Any] , snake_case :Optional[int] , snake_case :List[Any] , snake_case :bool = False , snake_case :bool = False , snake_case :bool = False , snake_case :bool = False , ): '''simple docstring''' A_ : List[str] = len(references[0] ) if any(len(snake_case ) != references_per_prediction for refs in references ): raise ValueError("Sacrebleu requires the same number of references for each prediction" ) A_ : int = [[refs[i] for refs in references] for i in range(snake_case )] A_ : Optional[Any] = TER( normalized=snake_case , no_punct=snake_case , asian_support=snake_case , case_sensitive=snake_case , ) A_ : List[Any] = sb_ter.corpus_score(snake_case , snake_case ) return {"score": output.score, "num_edits": output.num_edits, "ref_length": output.ref_length}
300
0
'''simple docstring''' def _lowerCAmelCase ( _UpperCamelCase : int ) -> bool: """simple docstring""" return sum(i for i in range(1 , number // 2 + 1 ) if number % i == 0 ) == number if __name__ == "__main__": print("Program to check whether a number is a Perfect number or not...") lowerCamelCase : Optional[Any] = int(input("Enter number: ").strip()) print(f'''{number} is {"" if perfect(number) else "not "}a Perfect Number.''')
47
def __snake_case ( _lowerCAmelCase : List[str] , _lowerCAmelCase : int ) -> str: return (pointa[0] - pointa[0]) ** 2 + (pointa[1] - pointa[1]) ** 2 def __snake_case ( _lowerCAmelCase : int , _lowerCAmelCase : Union[str, Any]=0 ) -> Any: return sorted(_lowerCAmelCase , key=lambda _lowerCAmelCase : x[column] ) def __snake_case ( _lowerCAmelCase : List[str] , _lowerCAmelCase : Any , _lowerCAmelCase : Any=float("inf" ) ) -> int: for i in range(points_counts - 1 ): for j in range(i + 1 , _lowerCAmelCase ): A_ : Tuple = euclidean_distance_sqr(points[i] , points[j] ) if current_dis < min_dis: A_ : Union[str, Any] = current_dis return min_dis def __snake_case ( _lowerCAmelCase : Optional[int] , _lowerCAmelCase : Dict , _lowerCAmelCase : List[str]=float("inf" ) ) -> Dict: for i in range(min(6 , points_counts - 1 ) , _lowerCAmelCase ): for j in range(max(0 , i - 6 ) , _lowerCAmelCase ): A_ : List[Any] = euclidean_distance_sqr(points[i] , points[j] ) if current_dis < min_dis: A_ : Union[str, Any] = current_dis return min_dis def __snake_case ( _lowerCAmelCase : List[Any] , _lowerCAmelCase : str , _lowerCAmelCase : Dict ) -> List[str]: # base case if points_counts <= 3: return dis_between_closest_pair(_lowerCAmelCase , _lowerCAmelCase ) # recursion A_ : Optional[int] = points_counts // 2 A_ : List[Any] = closest_pair_of_points_sqr( _lowerCAmelCase , points_sorted_on_y[:mid] , _lowerCAmelCase ) A_ : List[Any] = closest_pair_of_points_sqr( _lowerCAmelCase , points_sorted_on_y[mid:] , points_counts - mid ) A_ : Tuple = min(_lowerCAmelCase , _lowerCAmelCase ) A_ : Dict = [] for point in points_sorted_on_x: if abs(point[0] - points_sorted_on_x[mid][0] ) < closest_pair_dis: cross_strip.append(_lowerCAmelCase ) A_ : Tuple = dis_between_closest_in_strip( _lowerCAmelCase , len(_lowerCAmelCase ) , _lowerCAmelCase ) return min(_lowerCAmelCase , _lowerCAmelCase ) def __snake_case ( _lowerCAmelCase : str , _lowerCAmelCase : Optional[Any] ) -> Any: A_ : Optional[Any] = column_based_sort(_lowerCAmelCase , column=0 ) A_ : Optional[int] = column_based_sort(_lowerCAmelCase , column=1 ) return ( closest_pair_of_points_sqr( _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) ) ** 0.5 if __name__ == "__main__": _lowerCAmelCase : List[Any] = [(2, 3), (12, 30), (40, 50), (5, 1), (12, 10), (3, 4)] print('''Distance:''', closest_pair_of_points(points, len(points)))
300
0
import tempfile import unittest from pathlib import Path from shutil import copyfile from transformers import BatchEncoding, MarianTokenizer from transformers.testing_utils import get_tests_dir, require_sentencepiece, slow from transformers.utils import is_sentencepiece_available, is_tf_available, is_torch_available if is_sentencepiece_available(): from transformers.models.marian.tokenization_marian import VOCAB_FILES_NAMES, save_json from ...test_tokenization_common import TokenizerTesterMixin SCREAMING_SNAKE_CASE__ : Optional[Any] = get_tests_dir('fixtures/test_sentencepiece.model') SCREAMING_SNAKE_CASE__ : Dict = {'target_lang': 'fi', 'source_lang': 'en'} SCREAMING_SNAKE_CASE__ : Any = '>>zh<<' SCREAMING_SNAKE_CASE__ : int = 'Helsinki-NLP/' if is_torch_available(): SCREAMING_SNAKE_CASE__ : Optional[int] = 'pt' elif is_tf_available(): SCREAMING_SNAKE_CASE__ : str = 'tf' else: SCREAMING_SNAKE_CASE__ : Dict = 'jax' @require_sentencepiece class UpperCamelCase__ (lowerCAmelCase__ , unittest.TestCase ): '''simple docstring''' lowerCamelCase_ : Any = MarianTokenizer lowerCamelCase_ : Tuple = False lowerCamelCase_ : List[str] = True def _lowercase ( self ) -> Union[str, Any]: super().setUp() lowerCamelCase : Tuple = ["</s>", "<unk>", "▁This", "▁is", "▁a", "▁t", "est", "\u0120", "<pad>"] lowerCamelCase : Optional[int] = dict(zip(UpperCamelCase__ , range(len(UpperCamelCase__ ) ) ) ) lowerCamelCase : int = Path(self.tmpdirname ) save_json(UpperCamelCase__ , save_dir / VOCAB_FILES_NAMES["vocab"] ) save_json(UpperCamelCase__ , save_dir / VOCAB_FILES_NAMES["tokenizer_config_file"] ) if not (save_dir / VOCAB_FILES_NAMES["source_spm"]).exists(): copyfile(UpperCamelCase__ , save_dir / VOCAB_FILES_NAMES["source_spm"] ) copyfile(UpperCamelCase__ , save_dir / VOCAB_FILES_NAMES["target_spm"] ) lowerCamelCase : Optional[int] = MarianTokenizer.from_pretrained(self.tmpdirname ) tokenizer.save_pretrained(self.tmpdirname ) def _lowercase ( self , **UpperCamelCase__ ) -> MarianTokenizer: return MarianTokenizer.from_pretrained(self.tmpdirname , **UpperCamelCase__ ) def _lowercase ( self , UpperCamelCase__ ) -> Optional[Any]: return ( "This is a test", "This is a test", ) def _lowercase ( self ) -> Any: lowerCamelCase : Dict = "</s>" lowerCamelCase : str = 0 self.assertEqual(self.get_tokenizer()._convert_token_to_id(UpperCamelCase__ ) , UpperCamelCase__ ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(UpperCamelCase__ ) , UpperCamelCase__ ) def _lowercase ( self ) -> List[str]: lowerCamelCase : Union[str, Any] = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , "</s>" ) self.assertEqual(vocab_keys[1] , "<unk>" ) self.assertEqual(vocab_keys[-1] , "<pad>" ) self.assertEqual(len(UpperCamelCase__ ) , 9 ) def _lowercase ( self ) -> List[Any]: self.assertEqual(self.get_tokenizer().vocab_size , 9 ) def _lowercase ( self ) -> str: lowerCamelCase : Any = MarianTokenizer.from_pretrained(F'''{ORG_NAME}opus-mt-en-de''' ) lowerCamelCase : Union[str, Any] = en_de_tokenizer(["I am a small frog"] , return_tensors=UpperCamelCase__ ) self.assertIsInstance(UpperCamelCase__ , UpperCamelCase__ ) lowerCamelCase : Union[str, Any] = [38, 121, 14, 697, 3_8848, 0] self.assertListEqual(UpperCamelCase__ , batch.input_ids[0] ) lowerCamelCase : Union[str, Any] = tempfile.mkdtemp() en_de_tokenizer.save_pretrained(UpperCamelCase__ ) lowerCamelCase : Tuple = [x.name for x in Path(UpperCamelCase__ ).glob("*" )] self.assertIn("source.spm" , UpperCamelCase__ ) MarianTokenizer.from_pretrained(UpperCamelCase__ ) def _lowercase ( self ) -> Any: lowerCamelCase : Dict = self.get_tokenizer() lowerCamelCase : Optional[Any] = tok( ["I am a small frog" * 1000, "I am a small frog"] , padding=UpperCamelCase__ , truncation=UpperCamelCase__ , return_tensors=UpperCamelCase__ ) self.assertIsInstance(UpperCamelCase__ , UpperCamelCase__ ) self.assertEqual(batch.input_ids.shape , (2, 512) ) def _lowercase ( self ) -> List[Any]: lowerCamelCase : List[Any] = self.get_tokenizer() lowerCamelCase : Any = tok(["I am a tiny frog", "I am a small frog"] , padding=UpperCamelCase__ , return_tensors=UpperCamelCase__ ) self.assertIsInstance(UpperCamelCase__ , UpperCamelCase__ ) self.assertEqual(batch_smaller.input_ids.shape , (2, 10) ) @slow def _lowercase ( self ) -> List[str]: # fmt: off lowerCamelCase : Dict = {"input_ids": [[4_3495, 462, 20, 4_2164, 1369, 52, 464, 132, 1703, 492, 13, 7491, 3_8999, 6, 8, 464, 132, 1703, 492, 13, 4669, 3_7867, 13, 7525, 27, 1593, 988, 13, 3_3972, 7029, 6, 20, 8251, 383, 2, 270, 5866, 3788, 2, 2353, 8251, 1_2338, 2, 1_3958, 387, 2, 3629, 6953, 188, 2900, 2, 1_3958, 8011, 1_1501, 23, 8460, 4073, 3_4009, 20, 435, 1_1439, 27, 8, 8460, 4073, 6004, 20, 9988, 375, 27, 33, 266, 1945, 1076, 1350, 3_7867, 3288, 5, 577, 1076, 4374, 8, 5082, 5, 2_6453, 257, 556, 403, 2, 242, 132, 383, 316, 492, 8, 1_0767, 6, 316, 304, 4239, 3, 0], [148, 1_5722, 19, 1839, 12, 1350, 13, 2_2327, 5082, 5418, 4_7567, 3_5938, 59, 318, 1_9552, 108, 2183, 54, 1_4976, 4835, 32, 547, 1114, 8, 315, 2417, 5, 92, 1_9088, 3, 0, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100], [36, 6395, 1_2570, 3_9147, 1_1597, 6, 266, 4, 4_5405, 7296, 3, 0, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100]], "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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=UpperCamelCase__ , model_name="Helsinki-NLP/opus-mt-en-de" , revision="1a8c2263da11e68e50938f97e10cd57820bd504c" , decode_kwargs={"use_source_tokenizer": True} , ) def _lowercase ( self ) -> int: lowerCamelCase : Optional[int] = MarianTokenizer.from_pretrained("hf-internal-testing/test-marian-two-vocabs" ) lowerCamelCase : int = "Tämä on testi" lowerCamelCase : List[str] = "This is a test" lowerCamelCase : Optional[int] = [76, 7, 2047, 2] lowerCamelCase : List[str] = [69, 12, 11, 940, 2] lowerCamelCase : Optional[int] = tokenizer(UpperCamelCase__ ).input_ids self.assertListEqual(UpperCamelCase__ , UpperCamelCase__ ) lowerCamelCase : int = tokenizer(text_target=UpperCamelCase__ ).input_ids self.assertListEqual(UpperCamelCase__ , UpperCamelCase__ ) lowerCamelCase : List[str] = tokenizer.decode(UpperCamelCase__ , skip_special_tokens=UpperCamelCase__ ) self.assertEqual(UpperCamelCase__ , UpperCamelCase__ )
48
import inspect import unittest from transformers import ViTMSNConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTMSNForImageClassification, ViTMSNModel from transformers.models.vit_msn.modeling_vit_msn import VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class __magic_name__ : """simple docstring""" def __init__( self :Dict , snake_case :Optional[int] , snake_case :Tuple=13 , snake_case :List[Any]=30 , snake_case :Union[str, Any]=2 , snake_case :List[Any]=3 , snake_case :Tuple=True , snake_case :Dict=True , snake_case :Dict=32 , snake_case :List[str]=5 , snake_case :Optional[Any]=4 , snake_case :Any=37 , snake_case :Dict="gelu" , snake_case :List[str]=0.1 , snake_case :str=0.1 , snake_case :Tuple=10 , snake_case :str=0.02 , snake_case :Optional[Any]=None , ): '''simple docstring''' A_ : Tuple = parent A_ : int = batch_size A_ : List[str] = image_size A_ : List[Any] = patch_size A_ : Optional[Any] = num_channels A_ : List[Any] = is_training A_ : Tuple = use_labels A_ : Union[str, Any] = hidden_size A_ : Tuple = num_hidden_layers A_ : Any = num_attention_heads A_ : List[str] = intermediate_size A_ : Optional[int] = hidden_act A_ : List[str] = hidden_dropout_prob A_ : str = attention_probs_dropout_prob A_ : Any = type_sequence_label_size A_ : List[str] = initializer_range A_ : Dict = scope # in ViT MSN, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) A_ : Optional[int] = (image_size // patch_size) ** 2 A_ : List[str] = num_patches + 1 def SCREAMING_SNAKE_CASE ( self :Optional[Any] ): '''simple docstring''' A_ : Any = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) A_ : Tuple = None if self.use_labels: A_ : Dict = ids_tensor([self.batch_size] , self.type_sequence_label_size ) A_ : Dict = self.get_config() return config, pixel_values, labels def SCREAMING_SNAKE_CASE ( self :Union[str, Any] ): '''simple docstring''' return ViTMSNConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , initializer_range=self.initializer_range , ) def SCREAMING_SNAKE_CASE ( self :List[Any] , snake_case :List[Any] , snake_case :str , snake_case :Tuple ): '''simple docstring''' A_ : Optional[Any] = ViTMSNModel(config=snake_case ) model.to(snake_case ) model.eval() A_ : int = model(snake_case ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def SCREAMING_SNAKE_CASE ( self :int , snake_case :Optional[int] , snake_case :List[str] , snake_case :List[str] ): '''simple docstring''' A_ : Dict = self.type_sequence_label_size A_ : Tuple = ViTMSNForImageClassification(snake_case ) model.to(snake_case ) model.eval() A_ : Union[str, Any] = model(snake_case , labels=snake_case ) print("Pixel and labels shape: {pixel_values.shape}, {labels.shape}" ) print("Labels: {labels}" ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images A_ : Union[str, Any] = 1 A_ : int = ViTMSNForImageClassification(snake_case ) model.to(snake_case ) model.eval() A_ : Dict = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) A_ : Optional[Any] = model(snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def SCREAMING_SNAKE_CASE ( self :List[Any] ): '''simple docstring''' A_ : List[str] = self.prepare_config_and_inputs() A_ , A_ , A_ : Optional[int] = config_and_inputs A_ : Dict = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class __magic_name__ ( lowerCamelCase__ , lowerCamelCase__ , unittest.TestCase ): """simple docstring""" __UpperCamelCase = (ViTMSNModel, ViTMSNForImageClassification) if is_torch_available() else () __UpperCamelCase = ( {'''feature-extraction''': ViTMSNModel, '''image-classification''': ViTMSNForImageClassification} if is_torch_available() else {} ) __UpperCamelCase = False __UpperCamelCase = False __UpperCamelCase = False __UpperCamelCase = False def SCREAMING_SNAKE_CASE ( self :Union[str, Any] ): '''simple docstring''' A_ : Tuple = ViTMSNModelTester(self ) A_ : str = ConfigTester(self , config_class=snake_case , has_text_modality=snake_case , hidden_size=37 ) def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' self.config_tester.run_common_tests() @unittest.skip(reason="ViTMSN does not use inputs_embeds" ) def SCREAMING_SNAKE_CASE ( self :Dict ): '''simple docstring''' pass def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' A_ , A_ : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A_ : Optional[int] = model_class(snake_case ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) A_ : Optional[int] = model.get_output_embeddings() self.assertTrue(x is None or isinstance(snake_case , nn.Linear ) ) def SCREAMING_SNAKE_CASE ( self :Optional[Any] ): '''simple docstring''' A_ , A_ : str = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A_ : Optional[Any] = model_class(snake_case ) A_ : Any = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic A_ : List[str] = [*signature.parameters.keys()] A_ : List[Any] = ["pixel_values"] self.assertListEqual(arg_names[:1] , snake_case ) def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' A_ : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case ) def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' A_ : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*snake_case ) @slow def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' for model_name in VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A_ : Optional[Any] = ViTMSNModel.from_pretrained(snake_case ) self.assertIsNotNone(snake_case ) def __snake_case ( ) -> Optional[Any]: A_ : Optional[Any] = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_torch @require_vision class __magic_name__ ( unittest.TestCase ): """simple docstring""" @cached_property def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' return ViTImageProcessor.from_pretrained("facebook/vit-msn-small" ) if is_vision_available() else None @slow def SCREAMING_SNAKE_CASE ( self :List[Any] ): '''simple docstring''' torch.manual_seed(2 ) A_ : Any = ViTMSNForImageClassification.from_pretrained("facebook/vit-msn-small" ).to(snake_case ) A_ : List[str] = self.default_image_processor A_ : int = prepare_img() A_ : List[str] = image_processor(images=snake_case , return_tensors="pt" ).to(snake_case ) # forward pass with torch.no_grad(): A_ : Optional[int] = model(**snake_case ) # verify the logits A_ : List[Any] = torch.Size((1, 1_000) ) self.assertEqual(outputs.logits.shape , snake_case ) A_ : int = torch.tensor([-0.0803, -0.4454, -0.2375] ).to(snake_case ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , snake_case , atol=1e-4 ) )
300
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 : def __init__( self : int , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : Optional[int]=13 , __SCREAMING_SNAKE_CASE : Union[str, Any]=7 , __SCREAMING_SNAKE_CASE : int=True , __SCREAMING_SNAKE_CASE : Union[str, Any]=True , __SCREAMING_SNAKE_CASE : Tuple=True , __SCREAMING_SNAKE_CASE : Optional[int]=True , __SCREAMING_SNAKE_CASE : Optional[int]=99 , __SCREAMING_SNAKE_CASE : List[str]=32 , __SCREAMING_SNAKE_CASE : Dict=2 , __SCREAMING_SNAKE_CASE : Optional[Any]=4 , __SCREAMING_SNAKE_CASE : Any=37 , __SCREAMING_SNAKE_CASE : Any="gelu" , __SCREAMING_SNAKE_CASE : int=0.1 , __SCREAMING_SNAKE_CASE : Tuple=0.1 , __SCREAMING_SNAKE_CASE : Optional[Any]=512 , __SCREAMING_SNAKE_CASE : Dict=16 , __SCREAMING_SNAKE_CASE : Union[str, Any]=2 , __SCREAMING_SNAKE_CASE : int=0.02 , __SCREAMING_SNAKE_CASE : Dict=False , __SCREAMING_SNAKE_CASE : int=True , __SCREAMING_SNAKE_CASE : str="None" , __SCREAMING_SNAKE_CASE : str=3 , __SCREAMING_SNAKE_CASE : Optional[int]=4 , __SCREAMING_SNAKE_CASE : List[str]=None , ): '''simple docstring''' __a = parent __a = batch_size __a = seq_length __a = is_training __a = use_input_mask __a = use_token_type_ids __a = use_labels __a = vocab_size __a = hidden_size __a = num_hidden_layers __a = num_attention_heads __a = intermediate_size __a = hidden_act __a = hidden_dropout_prob __a = attention_probs_dropout_prob __a = max_position_embeddings __a = type_vocab_size __a = type_sequence_label_size __a = initializer_range __a = num_labels __a = num_choices __a = relative_attention __a = position_biased_input __a = pos_att_type __a = scope def _lowerCamelCase ( self : Union[str, Any]): '''simple docstring''' __a = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size) __a = None if self.use_input_mask: __a = random_attention_mask([self.batch_size, self.seq_length]) __a = None if self.use_token_type_ids: __a = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size) __a = None __a = None __a = None if self.use_labels: __a = ids_tensor([self.batch_size] , self.type_sequence_label_size) __a = ids_tensor([self.batch_size, self.seq_length] , self.num_labels) __a = 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 _lowerCamelCase ( self : List[Any] , __SCREAMING_SNAKE_CASE : List[Any] , __SCREAMING_SNAKE_CASE : Tuple , __SCREAMING_SNAKE_CASE : List[Any] , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : Dict , __SCREAMING_SNAKE_CASE : Tuple): '''simple docstring''' __a = TFDebertaVaModel(config=__SCREAMING_SNAKE_CASE) __a = {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids} __a = [input_ids, input_mask] __a = model(__SCREAMING_SNAKE_CASE) __a = model(__SCREAMING_SNAKE_CASE) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size)) def _lowerCamelCase ( self : List[Any] , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : Dict , __SCREAMING_SNAKE_CASE : List[str] , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : Any , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : Dict): '''simple docstring''' __a = TFDebertaVaForMaskedLM(config=__SCREAMING_SNAKE_CASE) __a = { '''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids, } __a = model(__SCREAMING_SNAKE_CASE) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size)) def _lowerCamelCase ( self : Tuple , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : Union[str, Any] , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : Tuple , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : int): '''simple docstring''' __a = self.num_labels __a = TFDebertaVaForSequenceClassification(config=__SCREAMING_SNAKE_CASE) __a = { '''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids, } __a = model(__SCREAMING_SNAKE_CASE) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels)) def _lowerCamelCase ( self : Dict , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : Optional[int] , __SCREAMING_SNAKE_CASE : Union[str, Any] , __SCREAMING_SNAKE_CASE : List[str] , __SCREAMING_SNAKE_CASE : List[str] , __SCREAMING_SNAKE_CASE : str): '''simple docstring''' __a = self.num_labels __a = TFDebertaVaForTokenClassification(config=__SCREAMING_SNAKE_CASE) __a = { '''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids, } __a = model(__SCREAMING_SNAKE_CASE) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels)) def _lowerCamelCase ( self : Tuple , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : List[str] , __SCREAMING_SNAKE_CASE : Union[str, Any] , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : Any , __SCREAMING_SNAKE_CASE : Dict , __SCREAMING_SNAKE_CASE : Optional[int]): '''simple docstring''' __a = TFDebertaVaForQuestionAnswering(config=__SCREAMING_SNAKE_CASE) __a = { '''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids, } __a = 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 _lowerCamelCase ( self : Dict): '''simple docstring''' __a = self.prepare_config_and_inputs() ( ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ) = config_and_inputs __a = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_tf class _A ( __UpperCAmelCase ,__UpperCAmelCase ,unittest.TestCase ): UpperCamelCase__ : Dict = ( ( TFDebertaVaModel, TFDebertaVaForMaskedLM, TFDebertaVaForQuestionAnswering, TFDebertaVaForSequenceClassification, TFDebertaVaForTokenClassification, ) if is_tf_available() else () ) UpperCamelCase__ : Optional[int] = ( { '''feature-extraction''': TFDebertaVaModel, '''fill-mask''': TFDebertaVaForMaskedLM, '''question-answering''': TFDebertaVaForQuestionAnswering, '''text-classification''': TFDebertaVaForSequenceClassification, '''token-classification''': TFDebertaVaForTokenClassification, '''zero-shot''': TFDebertaVaForSequenceClassification, } if is_tf_available() else {} ) UpperCamelCase__ : int = False UpperCamelCase__ : Optional[int] = False def _lowerCamelCase ( self : Optional[Any]): '''simple docstring''' __a = TFDebertaVaModelTester(self) __a = ConfigTester(self , config_class=__SCREAMING_SNAKE_CASE , hidden_size=37) def _lowerCamelCase ( self : List[str]): '''simple docstring''' self.config_tester.run_common_tests() def _lowerCamelCase ( self : Optional[int]): '''simple docstring''' __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE) def _lowerCamelCase ( self : List[Any]): '''simple docstring''' __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*__SCREAMING_SNAKE_CASE) def _lowerCamelCase ( self : List[Any]): '''simple docstring''' __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*__SCREAMING_SNAKE_CASE) def _lowerCamelCase ( self : List[str]): '''simple docstring''' __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*__SCREAMING_SNAKE_CASE) def _lowerCamelCase ( self : int): '''simple docstring''' __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*__SCREAMING_SNAKE_CASE) @slow def _lowerCamelCase ( self : List[str]): '''simple docstring''' __a = TFDebertaVaModel.from_pretrained('''kamalkraj/deberta-v2-xlarge''') self.assertIsNotNone(__SCREAMING_SNAKE_CASE) @require_tf class _A ( unittest.TestCase ): @unittest.skip(reason='''Model not available yet''') def _lowerCamelCase ( self : Optional[Any]): '''simple docstring''' pass @slow def _lowerCamelCase ( self : Any): '''simple docstring''' __a = TFDebertaVaModel.from_pretrained('''kamalkraj/deberta-v2-xlarge''') __a = tf.constant([[0, 31_414, 232, 328, 740, 1_140, 12_695, 69, 46_078, 1_588, 2]]) __a = tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]) __a = model(__SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE)[0] __a = 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)
49
import torch from diffusers import DDPMScheduler from .test_schedulers import SchedulerCommonTest class __magic_name__ ( lowerCamelCase__ ): """simple docstring""" __UpperCamelCase = (DDPMScheduler,) def SCREAMING_SNAKE_CASE ( self :Union[str, Any] , **snake_case :str ): '''simple docstring''' A_ : Dict = { "num_train_timesteps": 1_000, "beta_start": 0.0001, "beta_end": 0.02, "beta_schedule": "linear", "variance_type": "fixed_small", "clip_sample": True, } config.update(**snake_case ) return config def SCREAMING_SNAKE_CASE ( self :int ): '''simple docstring''' for timesteps in [1, 5, 100, 1_000]: self.check_over_configs(num_train_timesteps=snake_case ) def SCREAMING_SNAKE_CASE ( self :Optional[Any] ): '''simple docstring''' for beta_start, beta_end in zip([0.0001, 0.001, 0.01, 0.1] , [0.002, 0.02, 0.2, 2] ): self.check_over_configs(beta_start=snake_case , beta_end=snake_case ) def SCREAMING_SNAKE_CASE ( self :int ): '''simple docstring''' for schedule in ["linear", "squaredcos_cap_v2"]: self.check_over_configs(beta_schedule=snake_case ) def SCREAMING_SNAKE_CASE ( self :List[Any] ): '''simple docstring''' for variance in ["fixed_small", "fixed_large", "other"]: self.check_over_configs(variance_type=snake_case ) def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' for clip_sample in [True, False]: self.check_over_configs(clip_sample=snake_case ) def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' self.check_over_configs(thresholding=snake_case ) for threshold in [0.5, 1.0, 2.0]: for prediction_type in ["epsilon", "sample", "v_prediction"]: self.check_over_configs( thresholding=snake_case , prediction_type=snake_case , sample_max_value=snake_case , ) def SCREAMING_SNAKE_CASE ( self :Optional[int] ): '''simple docstring''' for prediction_type in ["epsilon", "sample", "v_prediction"]: self.check_over_configs(prediction_type=snake_case ) def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' for t in [0, 500, 999]: self.check_over_forward(time_step=snake_case ) def SCREAMING_SNAKE_CASE ( self :Optional[Any] ): '''simple docstring''' A_ : Tuple = self.scheduler_classes[0] A_ : List[str] = self.get_scheduler_config() A_ : List[str] = scheduler_class(**snake_case ) assert torch.sum(torch.abs(scheduler._get_variance(0 ) - 0.0 ) ) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(487 ) - 0.00979 ) ) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(999 ) - 0.02 ) ) < 1e-5 def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' A_ : int = self.scheduler_classes[0] A_ : List[str] = self.get_scheduler_config() A_ : int = scheduler_class(**snake_case ) A_ : Tuple = len(snake_case ) A_ : List[str] = self.dummy_model() A_ : Optional[Any] = self.dummy_sample_deter A_ : List[str] = torch.manual_seed(0 ) for t in reversed(range(snake_case ) ): # 1. predict noise residual A_ : Tuple = model(snake_case , snake_case ) # 2. predict previous mean of sample x_t-1 A_ : Dict = scheduler.step(snake_case , snake_case , snake_case , generator=snake_case ).prev_sample # if t > 0: # noise = self.dummy_sample_deter # variance = scheduler.get_variance(t) ** (0.5) * noise # # sample = pred_prev_sample + variance A_ : Optional[int] = pred_prev_sample A_ : Tuple = torch.sum(torch.abs(snake_case ) ) A_ : str = torch.mean(torch.abs(snake_case ) ) assert abs(result_sum.item() - 258.9606 ) < 1e-2 assert abs(result_mean.item() - 0.3372 ) < 1e-3 def SCREAMING_SNAKE_CASE ( self :Dict ): '''simple docstring''' A_ : Optional[int] = self.scheduler_classes[0] A_ : int = self.get_scheduler_config(prediction_type="v_prediction" ) A_ : List[str] = scheduler_class(**snake_case ) A_ : int = len(snake_case ) A_ : Dict = self.dummy_model() A_ : str = self.dummy_sample_deter A_ : Any = torch.manual_seed(0 ) for t in reversed(range(snake_case ) ): # 1. predict noise residual A_ : Optional[int] = model(snake_case , snake_case ) # 2. predict previous mean of sample x_t-1 A_ : Tuple = scheduler.step(snake_case , snake_case , snake_case , generator=snake_case ).prev_sample # if t > 0: # noise = self.dummy_sample_deter # variance = scheduler.get_variance(t) ** (0.5) * noise # # sample = pred_prev_sample + variance A_ : List[str] = pred_prev_sample A_ : Optional[Any] = torch.sum(torch.abs(snake_case ) ) A_ : List[str] = torch.mean(torch.abs(snake_case ) ) assert abs(result_sum.item() - 202.0296 ) < 1e-2 assert abs(result_mean.item() - 0.2631 ) < 1e-3 def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' A_ : str = self.scheduler_classes[0] A_ : Optional[Any] = self.get_scheduler_config() A_ : Dict = scheduler_class(**snake_case ) A_ : Optional[int] = [100, 87, 50, 1, 0] scheduler.set_timesteps(timesteps=snake_case ) A_ : Optional[int] = scheduler.timesteps for i, timestep in enumerate(snake_case ): if i == len(snake_case ) - 1: A_ : str = -1 else: A_ : List[str] = timesteps[i + 1] A_ : Optional[int] = scheduler.previous_timestep(snake_case ) A_ : List[str] = prev_t.item() self.assertEqual(snake_case , snake_case ) def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' A_ : Optional[Any] = self.scheduler_classes[0] A_ : int = self.get_scheduler_config() A_ : Tuple = scheduler_class(**snake_case ) A_ : List[str] = [100, 87, 50, 51, 0] with self.assertRaises(snake_case , msg="`custom_timesteps` must be in descending order." ): scheduler.set_timesteps(timesteps=snake_case ) def SCREAMING_SNAKE_CASE ( self :List[Any] ): '''simple docstring''' A_ : Any = self.scheduler_classes[0] A_ : Union[str, Any] = self.get_scheduler_config() A_ : Optional[int] = scheduler_class(**snake_case ) A_ : Union[str, Any] = [100, 87, 50, 1, 0] A_ : Optional[int] = len(snake_case ) with self.assertRaises(snake_case , msg="Can only pass one of `num_inference_steps` or `custom_timesteps`." ): scheduler.set_timesteps(num_inference_steps=snake_case , timesteps=snake_case ) def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' A_ : Union[str, Any] = self.scheduler_classes[0] A_ : Optional[Any] = self.get_scheduler_config() A_ : Optional[int] = scheduler_class(**snake_case ) A_ : Optional[int] = [scheduler.config.num_train_timesteps] with self.assertRaises( snake_case , msg="`timesteps` must start before `self.config.train_timesteps`: {scheduler.config.num_train_timesteps}}" , ): scheduler.set_timesteps(timesteps=snake_case )
300
0
import unittest from transformers import BertGenerationTokenizer from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_torch, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin _UpperCAmelCase : Optional[Any] = """▁""" _UpperCAmelCase : Optional[Any] = get_tests_dir("""fixtures/test_sentencepiece.model""") @require_sentencepiece class lowerCAmelCase ( __UpperCamelCase, unittest.TestCase ): UpperCAmelCase__ = BertGenerationTokenizer UpperCAmelCase__ = False UpperCAmelCase__ = True def A_ ( self : List[Any] ) -> List[str]: super().setUp() lowerCamelCase__ : Dict = BertGenerationTokenizer(UpperCAmelCase , keep_accents=UpperCAmelCase ) tokenizer.save_pretrained(self.tmpdirname ) def A_ ( self : Optional[Any] ) -> Dict: lowerCamelCase__ : List[str] = '<s>' lowerCamelCase__ : Optional[int] = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(UpperCAmelCase ) , UpperCAmelCase ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(UpperCAmelCase ) , UpperCAmelCase ) def A_ ( self : List[str] ) -> Optional[int]: lowerCamelCase__ : Optional[Any] = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '<unk>' ) self.assertEqual(vocab_keys[1] , '<s>' ) self.assertEqual(vocab_keys[-1] , '<pad>' ) self.assertEqual(len(UpperCAmelCase ) , 1002 ) def A_ ( self : List[Any] ) -> Optional[Any]: self.assertEqual(self.get_tokenizer().vocab_size , 1000 ) def A_ ( self : Union[str, Any] ) -> List[Any]: lowerCamelCase__ : Union[str, Any] = BertGenerationTokenizer(UpperCAmelCase , keep_accents=UpperCAmelCase ) lowerCamelCase__ : List[str] = tokenizer.tokenize('This is a test' ) self.assertListEqual(UpperCAmelCase , ['▁This', '▁is', '▁a', '▁t', 'est'] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(UpperCAmelCase ) , [285, 46, 10, 170, 382] , ) lowerCamelCase__ : List[str] = tokenizer.tokenize('I was born in 92000, and this is falsé.' ) self.assertListEqual( UpperCAmelCase , [ SPIECE_UNDERLINE + 'I', SPIECE_UNDERLINE + 'was', SPIECE_UNDERLINE + 'b', 'or', 'n', SPIECE_UNDERLINE + 'in', SPIECE_UNDERLINE + '', '9', '2', '0', '0', '0', ',', SPIECE_UNDERLINE + 'and', SPIECE_UNDERLINE + 'this', SPIECE_UNDERLINE + 'is', SPIECE_UNDERLINE + 'f', 'al', 's', 'é', '.', ] , ) lowerCamelCase__ : Optional[int] = tokenizer.convert_tokens_to_ids(UpperCAmelCase ) self.assertListEqual( UpperCAmelCase , [8, 21, 84, 55, 24, 19, 7, 0, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 0, 4] , ) lowerCamelCase__ : Optional[Any] = tokenizer.convert_ids_to_tokens(UpperCAmelCase ) self.assertListEqual( UpperCAmelCase , [ SPIECE_UNDERLINE + 'I', SPIECE_UNDERLINE + 'was', SPIECE_UNDERLINE + 'b', 'or', 'n', SPIECE_UNDERLINE + 'in', SPIECE_UNDERLINE + '', '<unk>', '2', '0', '0', '0', ',', SPIECE_UNDERLINE + 'and', SPIECE_UNDERLINE + 'this', SPIECE_UNDERLINE + 'is', SPIECE_UNDERLINE + 'f', 'al', 's', '<unk>', '.', ] , ) @cached_property def A_ ( self : Dict ) -> Tuple: return BertGenerationTokenizer.from_pretrained('google/bert_for_seq_generation_L-24_bbc_encoder' ) @slow def A_ ( self : Optional[int] ) -> List[str]: lowerCamelCase__ : Union[str, Any] = 'Hello World!' lowerCamelCase__ : Dict = [18536, 2260, 101] self.assertListEqual(UpperCAmelCase , self.big_tokenizer.encode(UpperCAmelCase ) ) @slow def A_ ( self : Optional[Any] ) -> str: lowerCamelCase__ : List[Any] = ( 'This is a very long text with a lot of weird characters, such as: . , ~ ? ( ) " [ ] ! : - . Also we will' ' add words that should not exsist and be tokenized to <unk>, such as saoneuhaoesuth' ) lowerCamelCase__ : Any = [ 871, 419, 358, 946, 991, 2521, 452, 358, 1357, 387, 7751, 3536, 112, 985, 456, 126, 865, 938, 5400, 5734, 458, 1368, 467, 786, 2462, 5246, 1159, 633, 865, 4519, 457, 582, 852, 2557, 427, 916, 508, 405, 34324, 497, 391, 408, 11342, 1244, 385, 100, 938, 985, 456, 574, 362, 12597, 3200, 3129, 1172, ] self.assertListEqual(UpperCAmelCase , self.big_tokenizer.encode(UpperCAmelCase ) ) @require_torch @slow def A_ ( self : int ) -> Optional[Any]: import torch from transformers import BertGenerationConfig, BertGenerationEncoder # Build sequence lowerCamelCase__ : str = list(self.big_tokenizer.get_vocab().keys() )[:10] lowerCamelCase__ : int = ' '.join(UpperCAmelCase ) lowerCamelCase__ : Union[str, Any] = self.big_tokenizer.encode_plus(UpperCAmelCase , return_tensors='pt' , return_token_type_ids=UpperCAmelCase ) lowerCamelCase__ : Optional[Any] = self.big_tokenizer.batch_encode_plus( [sequence + ' ' + sequence] , return_tensors='pt' , return_token_type_ids=UpperCAmelCase ) lowerCamelCase__ : Tuple = BertGenerationConfig() lowerCamelCase__ : Optional[Any] = BertGenerationEncoder(UpperCAmelCase ) assert model.get_input_embeddings().weight.shape[0] >= self.big_tokenizer.vocab_size with torch.no_grad(): model(**UpperCAmelCase ) model(**UpperCAmelCase ) @slow def A_ ( self : Optional[int] ) -> List[Any]: # fmt: off lowerCamelCase__ : Any = {'input_ids': [[39286, 458, 36335, 2001, 456, 13073, 13266, 455, 113, 7746, 1741, 11157, 391, 13073, 13266, 455, 113, 3967, 35412, 113, 4936, 109, 3870, 2377, 113, 30084, 45720, 458, 134, 17496, 112, 503, 11672, 113, 118, 112, 5665, 13347, 38687, 112, 1496, 31389, 112, 3268, 47264, 134, 962, 112, 16377, 8035, 23130, 430, 12169, 15518, 28592, 458, 146, 41697, 109, 391, 12169, 15518, 16689, 458, 146, 41358, 109, 452, 726, 4034, 111, 763, 35412, 5082, 388, 1903, 111, 9051, 391, 2870, 48918, 1900, 1123, 550, 998, 112, 9586, 15985, 455, 391, 410, 22955, 37636, 114], [448, 17496, 419, 3663, 385, 763, 113, 27533, 2870, 3283, 13043, 1639, 24713, 523, 656, 24013, 18550, 2521, 517, 27014, 21244, 420, 1212, 1465, 391, 927, 4833, 388, 578, 11786, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [484, 2169, 7687, 21932, 18146, 726, 363, 17032, 3391, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], 'attention_mask': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 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], [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]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=UpperCAmelCase , model_name='google/bert_for_seq_generation_L-24_bbc_encoder' , revision='c817d1fd1be2ffa69431227a1fe320544943d4db' , )
50
import argparse import json import os import fairseq import torch from torch import nn from transformers import ( SpeechaTextaConfig, SpeechaTextaForCausalLM, SpeechaTextaTokenizer, SpeechEncoderDecoderConfig, SpeechEncoderDecoderModel, WavaVecaConfig, WavaVecaFeatureExtractor, WavaVecaModel, logging, ) logging.set_verbosity_info() _lowerCAmelCase : List[str] = logging.get_logger(__name__) _lowerCAmelCase : Optional[int] = { '''post_extract_proj''': '''feature_projection.projection''', '''encoder.pos_conv.0''': '''encoder.pos_conv_embed.conv''', '''self_attn.k_proj''': '''encoder.layers.*.attention.k_proj''', '''self_attn.v_proj''': '''encoder.layers.*.attention.v_proj''', '''self_attn.q_proj''': '''encoder.layers.*.attention.q_proj''', '''self_attn.out_proj''': '''encoder.layers.*.attention.out_proj''', '''self_attn_layer_norm''': '''encoder.layers.*.layer_norm''', '''fc1''': '''encoder.layers.*.feed_forward.intermediate_dense''', '''fc2''': '''encoder.layers.*.feed_forward.output_dense''', '''final_layer_norm''': '''encoder.layers.*.final_layer_norm''', '''encoder.layer_norm''': '''encoder.layer_norm''', '''w2v_model.layer_norm''': '''feature_projection.layer_norm''', '''quantizer.weight_proj''': '''quantizer.weight_proj''', '''quantizer.vars''': '''quantizer.codevectors''', '''project_q''': '''project_q''', '''final_proj''': '''project_hid''', '''w2v_encoder.proj''': '''lm_head''', '''mask_emb''': '''masked_spec_embed''', } _lowerCAmelCase : int = [ '''lm_head''', '''quantizer.weight_proj''', '''quantizer.codevectors''', '''project_q''', '''project_hid''', ] def __snake_case ( _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : List[Any] , _lowerCAmelCase : Any , _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : int ) -> List[Any]: for attribute in key.split("." ): A_ : List[Any] = getattr(_lowerCAmelCase , _lowerCAmelCase ) if weight_type is not None: A_ : List[Any] = getattr(_lowerCAmelCase , _lowerCAmelCase ).shape else: A_ : Tuple = hf_pointer.shape assert hf_shape == value.shape, ( f"Shape of hf {key + '.' + weight_type if weight_type is not None else ''} is {hf_shape}, but should be" f" {value.shape} for {full_name}" ) if weight_type == "weight": A_ : Optional[int] = value elif weight_type == "weight_g": A_ : Optional[int] = value elif weight_type == "weight_v": A_ : Any = value elif weight_type == "bias": A_ : str = value else: A_ : Any = value logger.info(f"{key + '.' + weight_type if weight_type is not None else ''} was initialized from {full_name}." ) def __snake_case ( _lowerCAmelCase : Optional[Any] , _lowerCAmelCase : Dict ) -> List[str]: A_ : Optional[Any] = [] A_ : Any = fairseq_model.state_dict() A_ : Union[str, Any] = hf_model.feature_extractor # if encoder has different dim to decoder -> use proj_weight A_ : str = None for name, value in fairseq_dict.items(): A_ : Tuple = False if "conv_layers" in name: load_conv_layer( _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , hf_model.config.feat_extract_norm == "group" , ) A_ : Optional[Any] = True elif name.split("." )[0] == "proj": A_ : Dict = fairseq_model.proj A_ : List[Any] = True else: for key, mapped_key in MAPPING.items(): if key in name or key.split("w2v_model." )[-1] == name.split("." )[0]: A_ : int = True if "*" in mapped_key: A_ : Optional[Any] = name.split(_lowerCAmelCase )[0].split("." )[-2] A_ : int = mapped_key.replace("*" , _lowerCAmelCase ) if "weight_g" in name: A_ : List[Any] = "weight_g" elif "weight_v" in name: A_ : List[Any] = "weight_v" elif "bias" in name: A_ : Dict = "bias" elif "weight" in name: A_ : List[Any] = "weight" else: A_ : Dict = None set_recursively(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) continue if not is_used: unused_weights.append(_lowerCAmelCase ) logger.warning(f"Unused weights: {unused_weights}" ) return proj_weight def __snake_case ( _lowerCAmelCase : Optional[Any] , _lowerCAmelCase : Tuple , _lowerCAmelCase : List[Any] , _lowerCAmelCase : List[Any] , _lowerCAmelCase : Optional[int] ) -> str: A_ : Any = full_name.split("conv_layers." )[-1] A_ : Optional[int] = name.split("." ) A_ : Optional[Any] = int(items[0] ) A_ : Union[str, Any] = int(items[1] ) if type_id == 0: if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, ( f"{full_name} has size {value.shape}, but" f" {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found." ) A_ : List[Any] = value logger.info(f"Feat extract conv layer {layer_id} was initialized from {full_name}." ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape, ( f"{full_name} has size {value.shape}, but" f" {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found." ) A_ : int = value logger.info(f"Feat extract conv layer {layer_id} was initialized from {full_name}." ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, ( f"{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was" " found." ) A_ : List[Any] = value logger.info(f"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}." ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, ( f"{full_name} has size {value.shape}, but" f" {feature_extractor[layer_id].layer_norm.weight.data.shape} was found." ) A_ : Tuple = value logger.info(f"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}." ) else: unused_weights.append(_lowerCAmelCase ) def __snake_case ( _lowerCAmelCase : Optional[int] ) -> str: A_ , A_ : List[str] = emb.weight.shape A_ : Optional[int] = nn.Linear(_lowerCAmelCase , _lowerCAmelCase , bias=_lowerCAmelCase ) A_ : List[Any] = emb.weight.data return lin_layer def __snake_case ( _lowerCAmelCase : str ) -> Tuple: with open(_lowerCAmelCase , "r" , encoding="utf-8" ) as f: A_ : int = f.readlines() A_ : Dict = [line.split(" " )[0] for line in lines] A_ : Tuple = len(_lowerCAmelCase ) A_ : Union[str, Any] = { "<s>": 0, "<pad>": 1, "</s>": 2, "<unk>": 3, } vocab_dict.update(dict(zip(_lowerCAmelCase , range(4 , num_words + 4 ) ) ) ) return vocab_dict @torch.no_grad() def __snake_case ( _lowerCAmelCase : int , _lowerCAmelCase : Tuple , _lowerCAmelCase : Dict , _lowerCAmelCase : Any , _lowerCAmelCase : Tuple , _lowerCAmelCase : List[str] , _lowerCAmelCase : Dict , ) -> Tuple: A_ : Optional[int] = WavaVecaConfig.from_pretrained(_lowerCAmelCase ) A_ : str = SpeechaTextaConfig.from_pretrained( _lowerCAmelCase , vocab_size=_lowerCAmelCase , decoder_layers=_lowerCAmelCase , do_stable_layer_norm=_lowerCAmelCase ) A_ : int = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=16000 , padding_value=0 , do_normalize=_lowerCAmelCase , return_attention_mask=_lowerCAmelCase , ) A_ , A_ , A_ : List[Any] = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={"data": "/".join(dict_path.split("/" )[:-1] )} ) A_ : Union[str, Any] = model[0].eval() # set weights for wav2vec2 encoder A_ : Tuple = WavaVecaModel(_lowerCAmelCase ) A_ : str = recursively_load_weights_wavaveca(model.encoder , _lowerCAmelCase ) A_ : Tuple = SpeechaTextaForCausalLM(_lowerCAmelCase ) A_ , A_ : List[str] = hf_decoder.model.decoder.load_state_dict(model.decoder.state_dict() , strict=_lowerCAmelCase ) # set output linear layer unexpected_keys.remove("embed_out" ) A_ : Union[str, Any] = nn.Parameter(model.decoder.embed_out.detach() ) # layer norm is init to identity matrix so leaving it is fine logger.warning(f"The following keys are missing when loading the decoder weights: {missing_keys}" ) logger.warning(f"The following keys are unexpected when loading the decoder weights: {unexpected_keys}" ) A_ : str = SpeechEncoderDecoderModel(encoder=_lowerCAmelCase , decoder=_lowerCAmelCase ) A_ : Optional[Any] = False # add projection layer A_ : Optional[Any] = nn.Parameter(projection_layer.weight ) A_ : int = nn.Parameter(projection_layer.bias ) A_ : str = create_vocab_dict(_lowerCAmelCase ) with open(os.path.join(_lowerCAmelCase , "vocab.json" ) , "w" ) as fp: json.dump(_lowerCAmelCase , _lowerCAmelCase ) A_ : Any = SpeechaTextaTokenizer(os.path.join(_lowerCAmelCase , "vocab.json" ) ) tokenizer.save_pretrained(_lowerCAmelCase ) A_ : Optional[int] = hf_wavavec.config.to_dict() A_ : int = tokenizer.pad_token_id A_ : List[str] = tokenizer.bos_token_id A_ : List[str] = tokenizer.eos_token_id A_ : List[str] = "speech_to_text_2" A_ : Tuple = "wav2vec2" A_ : str = SpeechEncoderDecoderConfig.from_dict(_lowerCAmelCase ) hf_wavavec.save_pretrained(_lowerCAmelCase ) feature_extractor.save_pretrained(_lowerCAmelCase ) if __name__ == "__main__": _lowerCAmelCase : Optional[Any] = argparse.ArgumentParser() parser.add_argument('''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''') parser.add_argument('''--checkpoint_path''', default=None, type=str, help='''Path to fairseq checkpoint''') parser.add_argument('''--dict_path''', default=None, type=str, help='''Path to dict of fine-tuned model''') parser.add_argument( '''--encoder_config_path''', default='''facebook/wav2vec2-large-lv60''', type=str, help='''Path to hf encoder wav2vec2 checkpoint config''', ) parser.add_argument( '''--decoder_config_path''', default='''facebook/s2t-small-mustc-en-fr-st''', type=str, help='''Path to hf decoder s2t checkpoint config''', ) parser.add_argument('''--vocab_size''', default=10_224, type=int, help='''Vocab size of decoder''') parser.add_argument('''--num_decoder_layers''', default=7, type=int, help='''Number of decoder layers''') _lowerCAmelCase : Union[str, Any] = parser.parse_args() convert_wavaveca_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.dict_path, encoder_config_path=args.encoder_config_path, decoder_config_path=args.decoder_config_path, vocab_size=args.vocab_size, num_decoder_layers=args.num_decoder_layers, )
300
0
import gc import inspect import unittest import torch from parameterized import parameterized from diffusers import PriorTransformer from diffusers.utils import floats_tensor, slow, torch_all_close, torch_device from diffusers.utils.testing_utils import enable_full_determinism from .test_modeling_common import ModelTesterMixin enable_full_determinism() class __snake_case ( a , unittest.TestCase ): UpperCAmelCase__ : Tuple = PriorTransformer UpperCAmelCase__ : Tuple = '''hidden_states''' @property def lowerCamelCase ( self : Any): """simple docstring""" UpperCAmelCase_ = 4 UpperCAmelCase_ = 8 UpperCAmelCase_ = 7 UpperCAmelCase_ = floats_tensor((batch_size, embedding_dim)).to(_snake_case) UpperCAmelCase_ = floats_tensor((batch_size, embedding_dim)).to(_snake_case) UpperCAmelCase_ = floats_tensor((batch_size, num_embeddings, embedding_dim)).to(_snake_case) return { "hidden_states": hidden_states, "timestep": 2, "proj_embedding": proj_embedding, "encoder_hidden_states": encoder_hidden_states, } def lowerCamelCase ( self : int , _snake_case : List[str]=0): """simple docstring""" torch.manual_seed(_snake_case) UpperCAmelCase_ = 4 UpperCAmelCase_ = 8 UpperCAmelCase_ = 7 UpperCAmelCase_ = torch.randn((batch_size, embedding_dim)).to(_snake_case) UpperCAmelCase_ = torch.randn((batch_size, embedding_dim)).to(_snake_case) UpperCAmelCase_ = torch.randn((batch_size, num_embeddings, embedding_dim)).to(_snake_case) return { "hidden_states": hidden_states, "timestep": 2, "proj_embedding": proj_embedding, "encoder_hidden_states": encoder_hidden_states, } @property def lowerCamelCase ( self : List[Any]): """simple docstring""" return (4, 8) @property def lowerCamelCase ( self : str): """simple docstring""" return (4, 8) def lowerCamelCase ( self : List[Any]): """simple docstring""" UpperCAmelCase_ = { '''num_attention_heads''': 2, '''attention_head_dim''': 4, '''num_layers''': 2, '''embedding_dim''': 8, '''num_embeddings''': 7, '''additional_embeddings''': 4, } UpperCAmelCase_ = self.dummy_input return init_dict, inputs_dict def lowerCamelCase ( self : int): """simple docstring""" UpperCAmelCase_ , UpperCAmelCase_ = PriorTransformer.from_pretrained( '''hf-internal-testing/prior-dummy''' , output_loading_info=_snake_case) self.assertIsNotNone(_snake_case) self.assertEqual(len(loading_info['''missing_keys''']) , 0) model.to(_snake_case) UpperCAmelCase_ = model(**self.dummy_input)[0] assert hidden_states is not None, "Make sure output is not None" def lowerCamelCase ( self : List[str]): """simple docstring""" UpperCAmelCase_ , UpperCAmelCase_ = self.prepare_init_args_and_inputs_for_common() UpperCAmelCase_ = self.model_class(**_snake_case) UpperCAmelCase_ = inspect.signature(model.forward) # signature.parameters is an OrderedDict => so arg_names order is deterministic UpperCAmelCase_ = [*signature.parameters.keys()] UpperCAmelCase_ = ['''hidden_states''', '''timestep'''] self.assertListEqual(arg_names[:2] , _snake_case) def lowerCamelCase ( self : str): """simple docstring""" UpperCAmelCase_ = PriorTransformer.from_pretrained('''hf-internal-testing/prior-dummy''') UpperCAmelCase_ = model.to(_snake_case) if hasattr(_snake_case , '''set_default_attn_processor'''): model.set_default_attn_processor() UpperCAmelCase_ = self.get_dummy_seed_input() with torch.no_grad(): UpperCAmelCase_ = model(**_snake_case)[0] UpperCAmelCase_ = output[0, :5].flatten().cpu() print(_snake_case) # Since the VAE Gaussian prior's generator is seeded on the appropriate device, # the expected output slices are not the same for CPU and GPU. UpperCAmelCase_ = torch.tensor([-1.3_4_3_6, -0.2_8_7_0, 0.7_5_3_8, 0.4_3_6_8, -0.0_2_3_9]) self.assertTrue(torch_all_close(_snake_case , _snake_case , rtol=1e-2)) @slow class __snake_case ( unittest.TestCase ): def lowerCamelCase ( self : Union[str, Any] , _snake_case : Optional[Any]=1 , _snake_case : Any=768 , _snake_case : Optional[Any]=77 , _snake_case : Optional[int]=0): """simple docstring""" torch.manual_seed(_snake_case) UpperCAmelCase_ = batch_size UpperCAmelCase_ = embedding_dim UpperCAmelCase_ = num_embeddings UpperCAmelCase_ = torch.randn((batch_size, embedding_dim)).to(_snake_case) UpperCAmelCase_ = torch.randn((batch_size, embedding_dim)).to(_snake_case) UpperCAmelCase_ = torch.randn((batch_size, num_embeddings, embedding_dim)).to(_snake_case) return { "hidden_states": hidden_states, "timestep": 2, "proj_embedding": proj_embedding, "encoder_hidden_states": encoder_hidden_states, } def lowerCamelCase ( self : str): """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() @parameterized.expand( [ # fmt: off [13, [-0.5_8_6_1, 0.1_2_8_3, -0.0_9_3_1, 0.0_8_8_2, 0.4_4_7_6, 0.1_3_2_9, -0.0_4_9_8, 0.0_6_4_0]], [37, [-0.4_9_1_3, 0.0_1_1_0, -0.0_4_8_3, 0.0_5_4_1, 0.4_9_5_4, -0.0_1_7_0, 0.0_3_5_4, 0.1_6_5_1]], # fmt: on ]) def lowerCamelCase ( self : List[str] , _snake_case : List[Any] , _snake_case : List[Any]): """simple docstring""" UpperCAmelCase_ = PriorTransformer.from_pretrained('''kandinsky-community/kandinsky-2-1-prior''' , subfolder='''prior''') model.to(_snake_case) UpperCAmelCase_ = self.get_dummy_seed_input(seed=_snake_case) with torch.no_grad(): UpperCAmelCase_ = model(**_snake_case)[0] assert list(sample.shape) == [1, 768] UpperCAmelCase_ = sample[0, :8].flatten().cpu() print(_snake_case) UpperCAmelCase_ = torch.tensor(_snake_case) assert torch_all_close(_snake_case , _snake_case , atol=1e-3)
51
from __future__ import annotations import unittest import numpy as np from transformers import LayoutLMConfig, 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.models.layoutlm.modeling_tf_layoutlm import ( TF_LAYOUTLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFLayoutLMForMaskedLM, TFLayoutLMForQuestionAnswering, TFLayoutLMForSequenceClassification, TFLayoutLMForTokenClassification, TFLayoutLMModel, ) class __magic_name__ : """simple docstring""" def __init__( self :Tuple , snake_case :Optional[Any] , snake_case :Tuple=13 , snake_case :Dict=7 , snake_case :List[Any]=True , snake_case :List[Any]=True , snake_case :Dict=True , snake_case :Any=True , snake_case :Optional[int]=99 , snake_case :Any=32 , snake_case :Dict=2 , snake_case :int=4 , snake_case :Optional[int]=37 , snake_case :List[str]="gelu" , snake_case :List[Any]=0.1 , snake_case :Optional[Any]=0.1 , snake_case :Tuple=512 , snake_case :Tuple=16 , snake_case :Tuple=2 , snake_case :Optional[int]=0.02 , snake_case :str=3 , snake_case :Optional[int]=4 , snake_case :List[str]=None , snake_case :Tuple=1_000 , ): '''simple docstring''' A_ : str = parent A_ : str = batch_size A_ : str = seq_length A_ : Any = is_training A_ : Any = use_input_mask A_ : str = use_token_type_ids A_ : Tuple = use_labels A_ : Optional[Any] = vocab_size A_ : Dict = hidden_size A_ : str = num_hidden_layers A_ : Dict = num_attention_heads A_ : str = intermediate_size A_ : int = hidden_act A_ : List[Any] = hidden_dropout_prob A_ : Dict = attention_probs_dropout_prob A_ : Optional[Any] = max_position_embeddings A_ : List[Any] = type_vocab_size A_ : Any = type_sequence_label_size A_ : Dict = initializer_range A_ : Any = num_labels A_ : Optional[int] = num_choices A_ : Optional[Any] = scope A_ : Any = range_bbox def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' A_ : List[str] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) # convert bbox to numpy since TF does not support item assignment A_ : Tuple = ids_tensor([self.batch_size, self.seq_length, 4] , self.range_bbox ).numpy() # Ensure that bbox is legal for i in range(bbox.shape[0] ): for j in range(bbox.shape[1] ): if bbox[i, j, 3] < bbox[i, j, 1]: A_ : str = bbox[i, j, 3] A_ : Union[str, Any] = bbox[i, j, 1] A_ : List[Any] = t if bbox[i, j, 2] < bbox[i, j, 0]: A_ : Any = bbox[i, j, 2] A_ : Tuple = bbox[i, j, 0] A_ : int = t A_ : int = tf.convert_to_tensor(snake_case ) A_ : Any = None if self.use_input_mask: A_ : Union[str, Any] = random_attention_mask([self.batch_size, self.seq_length] ) A_ : str = None if self.use_token_type_ids: A_ : Union[str, Any] = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) A_ : Dict = None A_ : List[Any] = None A_ : List[str] = None if self.use_labels: A_ : Dict = ids_tensor([self.batch_size] , self.type_sequence_label_size ) A_ : Optional[int] = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) A_ : str = ids_tensor([self.batch_size] , self.num_choices ) A_ : int = LayoutLMConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , ) return config, input_ids, bbox, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def SCREAMING_SNAKE_CASE ( self :str , snake_case :Dict , snake_case :Union[str, Any] , snake_case :int , snake_case :int , snake_case :Union[str, Any] , snake_case :Tuple , snake_case :Optional[int] , snake_case :List[Any] ): '''simple docstring''' A_ : Any = TFLayoutLMModel(config=snake_case ) A_ : Tuple = model(snake_case , snake_case , attention_mask=snake_case , token_type_ids=snake_case ) A_ : str = model(snake_case , snake_case , token_type_ids=snake_case ) A_ : List[Any] = model(snake_case , snake_case ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def SCREAMING_SNAKE_CASE ( self :Optional[int] , snake_case :Any , snake_case :List[Any] , snake_case :List[str] , snake_case :Optional[Any] , snake_case :Dict , snake_case :Any , snake_case :Union[str, Any] , snake_case :List[Any] ): '''simple docstring''' A_ : Optional[int] = TFLayoutLMForMaskedLM(config=snake_case ) A_ : Tuple = model(snake_case , snake_case , attention_mask=snake_case , token_type_ids=snake_case , labels=snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def SCREAMING_SNAKE_CASE ( self :List[str] , snake_case :Dict , snake_case :Tuple , snake_case :Tuple , snake_case :List[str] , snake_case :Tuple , snake_case :str , snake_case :Optional[int] , snake_case :Any ): '''simple docstring''' A_ : Union[str, Any] = self.num_labels A_ : int = TFLayoutLMForSequenceClassification(config=snake_case ) A_ : Optional[int] = model(snake_case , snake_case , attention_mask=snake_case , token_type_ids=snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def SCREAMING_SNAKE_CASE ( self :Optional[Any] , snake_case :Dict , snake_case :str , snake_case :Optional[Any] , snake_case :int , snake_case :Any , snake_case :Tuple , snake_case :List[str] , snake_case :Union[str, Any] ): '''simple docstring''' A_ : List[Any] = self.num_labels A_ : str = TFLayoutLMForTokenClassification(config=snake_case ) A_ : Union[str, Any] = model(snake_case , snake_case , attention_mask=snake_case , token_type_ids=snake_case , labels=snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def SCREAMING_SNAKE_CASE ( self :int , snake_case :List[str] , snake_case :Optional[int] , snake_case :Union[str, Any] , snake_case :List[Any] , snake_case :int , snake_case :Any , snake_case :Union[str, Any] , snake_case :Any ): '''simple docstring''' A_ : Optional[Any] = TFLayoutLMForQuestionAnswering(config=snake_case ) A_ : List[Any] = model(snake_case , snake_case , attention_mask=snake_case , token_type_ids=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 SCREAMING_SNAKE_CASE ( self :Dict ): '''simple docstring''' A_ : int = self.prepare_config_and_inputs() ( ( A_ ) , ( A_ ) , ( A_ ) , ( A_ ) , ( A_ ) , ( A_ ) , ( A_ ) , ( A_ ) , ) : Union[str, Any] = config_and_inputs A_ : Optional[Any] = { "input_ids": input_ids, "bbox": bbox, "token_type_ids": token_type_ids, "attention_mask": input_mask, } return config, inputs_dict @require_tf class __magic_name__ ( lowerCamelCase__ , lowerCamelCase__ , unittest.TestCase ): """simple docstring""" __UpperCamelCase = ( ( TFLayoutLMModel, TFLayoutLMForMaskedLM, TFLayoutLMForTokenClassification, TFLayoutLMForSequenceClassification, TFLayoutLMForQuestionAnswering, ) if is_tf_available() else () ) __UpperCamelCase = ( { '''feature-extraction''': TFLayoutLMModel, '''fill-mask''': TFLayoutLMForMaskedLM, '''text-classification''': TFLayoutLMForSequenceClassification, '''token-classification''': TFLayoutLMForTokenClassification, '''zero-shot''': TFLayoutLMForSequenceClassification, } if is_tf_available() else {} ) __UpperCamelCase = False __UpperCamelCase = True __UpperCamelCase = 10 def SCREAMING_SNAKE_CASE ( self :Dict ): '''simple docstring''' A_ : Tuple = TFLayoutLMModelTester(self ) A_ : List[Any] = ConfigTester(self , config_class=snake_case , hidden_size=37 ) def SCREAMING_SNAKE_CASE ( self :Tuple ): '''simple docstring''' self.config_tester.run_common_tests() def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' A_ : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case ) def SCREAMING_SNAKE_CASE ( self :Optional[int] ): '''simple docstring''' A_ : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*snake_case ) def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' A_ : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*snake_case ) def SCREAMING_SNAKE_CASE ( self :Tuple ): '''simple docstring''' A_ : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*snake_case ) def SCREAMING_SNAKE_CASE ( self :List[Any] ): '''simple docstring''' A_ : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*snake_case ) @slow def SCREAMING_SNAKE_CASE ( self :Optional[Any] ): '''simple docstring''' for model_name in TF_LAYOUTLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A_ : List[str] = TFLayoutLMModel.from_pretrained(snake_case ) self.assertIsNotNone(snake_case ) @unittest.skip("Onnx compliancy broke with TF 2.10" ) def SCREAMING_SNAKE_CASE ( self :Dict ): '''simple docstring''' pass def __snake_case ( ) -> Optional[Any]: # Here we prepare a batch of 2 sequences to test a LayoutLM forward pass on: # fmt: off A_ : int = tf.convert_to_tensor([[101,1019,1014,1016,1037,12849,4747,1004,14246,2278,5439,4524,5002,2930,2193,2930,4341,3208,1005,1055,2171,2848,11300,3531,102],[101,4070,4034,7020,1024,3058,1015,1013,2861,1013,6070,19274,2772,6205,27814,16147,16147,4343,2047,10283,10969,14389,1012,2338,102]] ) # noqa: E231 A_ : int = tf.convert_to_tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],] ) # noqa: E231 A_ : Union[str, Any] = tf.convert_to_tensor([[[0,0,0,0],[423,237,440,251],[427,272,441,287],[419,115,437,129],[961,885,992,912],[256,38,330,58],[256,38,330,58],[336,42,353,57],[360,39,401,56],[360,39,401,56],[411,39,471,59],[479,41,528,59],[533,39,630,60],[67,113,134,131],[141,115,209,132],[68,149,133,166],[141,149,187,164],[195,148,287,165],[195,148,287,165],[195,148,287,165],[295,148,349,165],[441,149,492,166],[497,149,546,164],[64,201,125,218],[1000,1000,1000,1000]],[[0,0,0,0],[662,150,754,166],[665,199,742,211],[519,213,554,228],[519,213,554,228],[134,433,187,454],[130,467,204,480],[130,467,204,480],[130,467,204,480],[130,467,204,480],[130,467,204,480],[314,469,376,482],[504,684,582,706],[941,825,973,900],[941,825,973,900],[941,825,973,900],[941,825,973,900],[610,749,652,765],[130,659,168,672],[176,657,237,672],[238,657,312,672],[443,653,628,672],[443,653,628,672],[716,301,825,317],[1000,1000,1000,1000]]] ) # noqa: E231 A_ : List[Any] = tf.convert_to_tensor([[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]] ) # noqa: E231 # these are sequence labels (i.e. at the token level) A_ : Tuple = tf.convert_to_tensor([[-100,10,10,10,9,1,-100,7,7,-100,7,7,4,2,5,2,8,8,-100,-100,5,0,3,2,-100],[-100,12,12,12,-100,12,10,-100,-100,-100,-100,10,12,9,-100,-100,-100,10,10,10,9,12,-100,10,-100]] ) # noqa: E231 # fmt: on return input_ids, attention_mask, bbox, token_type_ids, labels @require_tf class __magic_name__ ( unittest.TestCase ): """simple docstring""" @slow def SCREAMING_SNAKE_CASE ( self :Tuple ): '''simple docstring''' A_ : str = TFLayoutLMModel.from_pretrained("microsoft/layoutlm-base-uncased" ) A_ , A_ , A_ , A_ , A_ : Tuple = prepare_layoutlm_batch_inputs() # forward pass A_ : Tuple = model(input_ids=snake_case , bbox=snake_case , attention_mask=snake_case , token_type_ids=snake_case ) # test the sequence output on [0, :3, :3] A_ : List[Any] = tf.convert_to_tensor( [[0.1785, -0.1947, -0.0425], [-0.3254, -0.2807, 0.2553], [-0.5391, -0.3322, 0.3364]] , ) self.assertTrue(np.allclose(outputs.last_hidden_state[0, :3, :3] , snake_case , atol=1e-3 ) ) # test the pooled output on [1, :3] A_ : Optional[Any] = tf.convert_to_tensor([-0.6580, -0.0214, 0.8552] ) self.assertTrue(np.allclose(outputs.pooler_output[1, :3] , snake_case , atol=1e-3 ) ) @slow def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' A_ : Union[str, Any] = TFLayoutLMForSequenceClassification.from_pretrained("microsoft/layoutlm-base-uncased" , num_labels=2 ) A_ , A_ , A_ , A_ , A_ : Any = prepare_layoutlm_batch_inputs() # forward pass A_ : Dict = model( input_ids=snake_case , bbox=snake_case , attention_mask=snake_case , token_type_ids=snake_case , labels=tf.convert_to_tensor([1, 1] ) , ) # test whether we get a loss as a scalar A_ : List[str] = outputs.loss A_ : Union[str, Any] = (2,) self.assertEqual(loss.shape , snake_case ) # test the shape of the logits A_ : Tuple = outputs.logits A_ : Tuple = (2, 2) self.assertEqual(logits.shape , snake_case ) @slow def SCREAMING_SNAKE_CASE ( self :Optional[int] ): '''simple docstring''' A_ : int = TFLayoutLMForTokenClassification.from_pretrained("microsoft/layoutlm-base-uncased" , num_labels=13 ) A_ , A_ , A_ , A_ , A_ : Optional[int] = prepare_layoutlm_batch_inputs() # forward pass A_ : Union[str, Any] = model( input_ids=snake_case , bbox=snake_case , attention_mask=snake_case , token_type_ids=snake_case , labels=snake_case ) # test the shape of the logits A_ : Dict = outputs.logits A_ : List[Any] = tf.convert_to_tensor((2, 25, 13) ) self.assertEqual(logits.shape , snake_case ) @slow def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' A_ : Optional[Any] = TFLayoutLMForQuestionAnswering.from_pretrained("microsoft/layoutlm-base-uncased" ) A_ , A_ , A_ , A_ , A_ : str = prepare_layoutlm_batch_inputs() # forward pass A_ : Union[str, Any] = model(input_ids=snake_case , bbox=snake_case , attention_mask=snake_case , token_type_ids=snake_case ) # test the shape of the logits A_ : Union[str, Any] = tf.convert_to_tensor((2, 25) ) self.assertEqual(outputs.start_logits.shape , snake_case ) self.assertEqual(outputs.end_logits.shape , snake_case )
300
0
from argparse import ArgumentParser from .env import EnvironmentCommand def A_ ( ) -> str: UpperCamelCase : List[str] = ArgumentParser("Diffusers CLI tool" , usage="diffusers-cli <command> [<args>]" ) UpperCamelCase : Optional[Any] = parser.add_subparsers(help="diffusers-cli command helpers" ) # Register commands EnvironmentCommand.register_subcommand(_lowerCAmelCase ) # Let's go UpperCamelCase : Optional[int] = parser.parse_args() if not hasattr(_lowerCAmelCase , "func" ): parser.print_help() exit(1 ) # Run UpperCamelCase : str = args.func(_lowerCAmelCase ) service.run() if __name__ == "__main__": main()
52
# Copyright 2023 The HuggingFace Inc. 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. import re from ..utils import cached_file # docstyle-ignore _lowerCAmelCase : Optional[int] = ''' Human: <<task>> Assistant: ''' _lowerCAmelCase : int = '''huggingface-tools/default-prompts''' _lowerCAmelCase : Any = {'''chat''': '''chat_prompt_template.txt''', '''run''': '''run_prompt_template.txt'''} def __snake_case ( _lowerCAmelCase : str , _lowerCAmelCase : List[Any] , _lowerCAmelCase : Dict="run" ) -> List[Any]: if prompt_or_repo_id is None: A_ : Optional[int] = DEFAULT_PROMPTS_REPO # prompt is considered a repo ID when it does not contain any kind of space if re.search("\\s" , _lowerCAmelCase ) is not None: return prompt_or_repo_id A_ : Optional[Any] = cached_file( _lowerCAmelCase , PROMPT_FILES[mode] , repo_type="dataset" , user_agent={"agent": agent_name} ) with open(_lowerCAmelCase , "r" , encoding="utf-8" ) as f: return f.read()
300
0
'''simple docstring''' import os import shutil import sys import tempfile import unittest from pathlib import Path import pytest import transformers from transformers import ( BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, GPT2_PRETRAINED_CONFIG_ARCHIVE_MAP, AutoTokenizer, BertConfig, BertTokenizer, BertTokenizerFast, CTRLTokenizer, GPTaTokenizer, GPTaTokenizerFast, PreTrainedTokenizerFast, RobertaTokenizer, RobertaTokenizerFast, is_tokenizers_available, ) from transformers.models.auto.configuration_auto import CONFIG_MAPPING, AutoConfig from transformers.models.auto.tokenization_auto import ( TOKENIZER_MAPPING, get_tokenizer_config, tokenizer_class_from_name, ) from transformers.models.roberta.configuration_roberta import RobertaConfig from transformers.testing_utils import ( DUMMY_DIFF_TOKENIZER_IDENTIFIER, DUMMY_UNKNOWN_IDENTIFIER, SMALL_MODEL_IDENTIFIER, RequestCounter, require_tokenizers, slow, ) sys.path.append(str(Path(__file__).parent.parent.parent.parent / '''utils''')) from test_module.custom_configuration import CustomConfig # noqa E402 from test_module.custom_tokenization import CustomTokenizer # noqa E402 if is_tokenizers_available(): from test_module.custom_tokenization_fast import CustomTokenizerFast class snake_case ( unittest.TestCase ): """simple docstring""" def _lowerCamelCase ( self : Any ): __UpperCamelCase = 0 @slow def _lowerCamelCase ( self : Dict ): for model_name in (x for x in BERT_PRETRAINED_CONFIG_ARCHIVE_MAP.keys() if "japanese" not in x): __UpperCamelCase = AutoTokenizer.from_pretrained(__A ) self.assertIsNotNone(__A ) self.assertIsInstance(__A , (BertTokenizer, BertTokenizerFast) ) self.assertGreater(len(__A ) , 0 ) for model_name in GPT2_PRETRAINED_CONFIG_ARCHIVE_MAP.keys(): __UpperCamelCase = AutoTokenizer.from_pretrained(__A ) self.assertIsNotNone(__A ) self.assertIsInstance(__A , (GPTaTokenizer, GPTaTokenizerFast) ) self.assertGreater(len(__A ) , 0 ) def _lowerCamelCase ( self : Dict ): __UpperCamelCase = AutoTokenizer.from_pretrained(__A ) self.assertIsInstance(__A , (BertTokenizer, BertTokenizerFast) ) self.assertEqual(tokenizer.vocab_size , 1_2 ) def _lowerCamelCase ( self : Union[str, Any] ): __UpperCamelCase = AutoTokenizer.from_pretrained(__A ) self.assertIsInstance(__A , (RobertaTokenizer, RobertaTokenizerFast) ) self.assertEqual(tokenizer.vocab_size , 2_0 ) def _lowerCamelCase ( self : Dict ): __UpperCamelCase = AutoConfig.from_pretrained(__A ) self.assertIsInstance(__A , __A ) # Check that tokenizer_type ≠ model_type __UpperCamelCase = AutoTokenizer.from_pretrained(__A , config=__A ) self.assertIsInstance(__A , (BertTokenizer, BertTokenizerFast) ) self.assertEqual(tokenizer.vocab_size , 1_2 ) def _lowerCamelCase ( self : Optional[Any] ): with tempfile.TemporaryDirectory() as tmp_dir: shutil.copy('./tests/fixtures/vocab.txt' , os.path.join(__A , 'vocab.txt' ) ) __UpperCamelCase = AutoTokenizer.from_pretrained(__A , tokenizer_type='bert' , use_fast=__A ) self.assertIsInstance(__A , __A ) with tempfile.TemporaryDirectory() as tmp_dir: shutil.copy('./tests/fixtures/vocab.json' , os.path.join(__A , 'vocab.json' ) ) shutil.copy('./tests/fixtures/merges.txt' , os.path.join(__A , 'merges.txt' ) ) __UpperCamelCase = AutoTokenizer.from_pretrained(__A , tokenizer_type='gpt2' , use_fast=__A ) self.assertIsInstance(__A , __A ) @require_tokenizers def _lowerCamelCase ( self : Optional[int] ): with tempfile.TemporaryDirectory() as tmp_dir: shutil.copy('./tests/fixtures/vocab.txt' , os.path.join(__A , 'vocab.txt' ) ) __UpperCamelCase = AutoTokenizer.from_pretrained(__A , tokenizer_type='bert' ) self.assertIsInstance(__A , __A ) with tempfile.TemporaryDirectory() as tmp_dir: shutil.copy('./tests/fixtures/vocab.json' , os.path.join(__A , 'vocab.json' ) ) shutil.copy('./tests/fixtures/merges.txt' , os.path.join(__A , 'merges.txt' ) ) __UpperCamelCase = AutoTokenizer.from_pretrained(__A , tokenizer_type='gpt2' ) self.assertIsInstance(__A , __A ) def _lowerCamelCase ( self : int ): with pytest.raises(__A ): AutoTokenizer.from_pretrained('./' , tokenizer_type='xxx' ) @require_tokenizers def _lowerCamelCase ( self : Optional[Any] ): for tokenizer_class in [BertTokenizer, BertTokenizerFast, AutoTokenizer]: __UpperCamelCase = tokenizer_class.from_pretrained('wietsedv/bert-base-dutch-cased' ) self.assertIsInstance(__A , (BertTokenizer, BertTokenizerFast) ) if isinstance(__A , __A ): self.assertEqual(tokenizer.basic_tokenizer.do_lower_case , __A ) else: self.assertEqual(tokenizer.do_lower_case , __A ) self.assertEqual(tokenizer.model_max_length , 5_1_2 ) @require_tokenizers def _lowerCamelCase ( self : List[str] ): for tokenizer_class in [BertTokenizer, BertTokenizerFast, AutoTokenizer]: with self.assertRaisesRegex( __A , 'julien-c/herlolip-not-exists is not a local folder and is not a valid model identifier' , ): __UpperCamelCase = tokenizer_class.from_pretrained('julien-c/herlolip-not-exists' ) def _lowerCamelCase ( self : List[Any] ): # tests: https://github.com/huggingface/transformers/pull/13251 # 1. models with `-`, e.g. xlm-roberta -> xlm_roberta # 2. models that don't remap 1-1 from model-name to model file, e.g., openai-gpt -> openai __UpperCamelCase = TOKENIZER_MAPPING.values() __UpperCamelCase = [] for slow_tok, fast_tok in tokenizers: if slow_tok is not None: tokenizer_names.append(slow_tok.__name__ ) if fast_tok is not None: tokenizer_names.append(fast_tok.__name__ ) for tokenizer_name in tokenizer_names: # must find the right class tokenizer_class_from_name(__A ) @require_tokenizers def _lowerCamelCase ( self : Optional[int] ): self.assertIsInstance(AutoTokenizer.from_pretrained('bert-base-cased' , use_fast=__A ) , __A ) self.assertIsInstance(AutoTokenizer.from_pretrained('bert-base-cased' ) , __A ) @require_tokenizers def _lowerCamelCase ( self : Optional[int] ): __UpperCamelCase = AutoTokenizer.from_pretrained('distilbert-base-uncased' , do_lower_case=__A ) __UpperCamelCase = 'Hello, world. How are you?' __UpperCamelCase = tokenizer.tokenize(__A ) self.assertEqual('[UNK]' , tokens[0] ) __UpperCamelCase = AutoTokenizer.from_pretrained('microsoft/mpnet-base' , do_lower_case=__A ) __UpperCamelCase = tokenizer.tokenize(__A ) self.assertEqual('[UNK]' , tokens[0] ) @require_tokenizers def _lowerCamelCase ( self : List[Any] ): __UpperCamelCase = AutoTokenizer.from_pretrained('robot-test/dummy-tokenizer-fast-with-model-config' ) self.assertEqual(type(__A ) , __A ) self.assertEqual(tokenizer.model_max_length , 5_1_2 ) self.assertEqual(tokenizer.vocab_size , 3_0_0_0_0 ) self.assertEqual(tokenizer.unk_token , '[UNK]' ) self.assertEqual(tokenizer.padding_side , 'right' ) self.assertEqual(tokenizer.truncation_side , 'right' ) def _lowerCamelCase ( self : int ): __UpperCamelCase = AutoTokenizer.from_pretrained(__A ) self.assertIsInstance(__A , (BertTokenizer, BertTokenizerFast) ) with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(__A ) __UpperCamelCase = AutoTokenizer.from_pretrained(__A ) self.assertIsInstance(__A , tokenizer.__class__ ) self.assertEqual(tokenizera.vocab_size , 1_2 ) def _lowerCamelCase ( self : int ): __UpperCamelCase = AutoTokenizer.from_pretrained('ctrl' ) # There is no fast CTRL so this always gives us a slow tokenizer. self.assertIsInstance(__A , __A ) def _lowerCamelCase ( self : Tuple ): # Check we can load the tokenizer config of an online model. __UpperCamelCase = get_tokenizer_config('bert-base-cased' ) __UpperCamelCase = config.pop('_commit_hash' , __A ) # If we ever update bert-base-cased tokenizer config, this dict here will need to be updated. self.assertEqual(__A , {'do_lower_case': False} ) # This model does not have a tokenizer_config so we get back an empty dict. __UpperCamelCase = get_tokenizer_config(__A ) self.assertDictEqual(__A , {} ) # A tokenizer saved with `save_pretrained` always creates a tokenizer config. __UpperCamelCase = AutoTokenizer.from_pretrained(__A ) with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(__A ) __UpperCamelCase = get_tokenizer_config(__A ) # Check the class of the tokenizer was properly saved (note that it always saves the slow class). self.assertEqual(config['tokenizer_class'] , 'BertTokenizer' ) def _lowerCamelCase ( self : List[str] ): try: AutoConfig.register('custom' , __A ) AutoTokenizer.register(__A , slow_tokenizer_class=__A ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(__A ): AutoTokenizer.register(__A , slow_tokenizer_class=__A ) __UpperCamelCase = CustomTokenizer.from_pretrained(__A ) with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(__A ) __UpperCamelCase = AutoTokenizer.from_pretrained(__A ) self.assertIsInstance(__A , __A ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in TOKENIZER_MAPPING._extra_content: del TOKENIZER_MAPPING._extra_content[CustomConfig] @require_tokenizers def _lowerCamelCase ( self : Optional[int] ): try: AutoConfig.register('custom' , __A ) # Can register in two steps AutoTokenizer.register(__A , slow_tokenizer_class=__A ) self.assertEqual(TOKENIZER_MAPPING[CustomConfig] , (CustomTokenizer, None) ) AutoTokenizer.register(__A , fast_tokenizer_class=__A ) self.assertEqual(TOKENIZER_MAPPING[CustomConfig] , (CustomTokenizer, CustomTokenizerFast) ) del TOKENIZER_MAPPING._extra_content[CustomConfig] # Can register in one step AutoTokenizer.register( __A , slow_tokenizer_class=__A , fast_tokenizer_class=__A ) self.assertEqual(TOKENIZER_MAPPING[CustomConfig] , (CustomTokenizer, CustomTokenizerFast) ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(__A ): AutoTokenizer.register(__A , fast_tokenizer_class=__A ) # We pass through a bert tokenizer fast cause there is no converter slow to fast for our new toknizer # and that model does not have a tokenizer.json with tempfile.TemporaryDirectory() as tmp_dir: __UpperCamelCase = BertTokenizerFast.from_pretrained(__A ) bert_tokenizer.save_pretrained(__A ) __UpperCamelCase = CustomTokenizerFast.from_pretrained(__A ) with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(__A ) __UpperCamelCase = AutoTokenizer.from_pretrained(__A ) self.assertIsInstance(__A , __A ) __UpperCamelCase = AutoTokenizer.from_pretrained(__A , use_fast=__A ) self.assertIsInstance(__A , __A ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in TOKENIZER_MAPPING._extra_content: del TOKENIZER_MAPPING._extra_content[CustomConfig] def _lowerCamelCase ( self : List[Any] ): # If remote code is not set, we will time out when asking whether to load the model. with self.assertRaises(__A ): __UpperCamelCase = AutoTokenizer.from_pretrained('hf-internal-testing/test_dynamic_tokenizer' ) # If remote code is disabled, we can't load this config. with self.assertRaises(__A ): __UpperCamelCase = AutoTokenizer.from_pretrained( 'hf-internal-testing/test_dynamic_tokenizer' , trust_remote_code=__A ) __UpperCamelCase = AutoTokenizer.from_pretrained('hf-internal-testing/test_dynamic_tokenizer' , trust_remote_code=__A ) self.assertTrue(tokenizer.special_attribute_present ) # Test tokenizer can be reloaded. with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(__A ) __UpperCamelCase = AutoTokenizer.from_pretrained(__A , trust_remote_code=__A ) self.assertTrue(reloaded_tokenizer.special_attribute_present ) if is_tokenizers_available(): self.assertEqual(tokenizer.__class__.__name__ , 'NewTokenizerFast' ) self.assertEqual(reloaded_tokenizer.__class__.__name__ , 'NewTokenizerFast' ) # Test we can also load the slow version __UpperCamelCase = AutoTokenizer.from_pretrained( 'hf-internal-testing/test_dynamic_tokenizer' , trust_remote_code=__A , use_fast=__A ) self.assertTrue(tokenizer.special_attribute_present ) self.assertEqual(tokenizer.__class__.__name__ , 'NewTokenizer' ) # Test tokenizer can be reloaded. with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(__A ) __UpperCamelCase = AutoTokenizer.from_pretrained(__A , trust_remote_code=__A , use_fast=__A ) self.assertEqual(reloaded_tokenizer.__class__.__name__ , 'NewTokenizer' ) self.assertTrue(reloaded_tokenizer.special_attribute_present ) else: self.assertEqual(tokenizer.__class__.__name__ , 'NewTokenizer' ) self.assertEqual(reloaded_tokenizer.__class__.__name__ , 'NewTokenizer' ) @require_tokenizers def _lowerCamelCase ( self : Any ): class snake_case ( __lowerCamelCase ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Any =False class snake_case ( __lowerCamelCase ): """simple docstring""" SCREAMING_SNAKE_CASE_ : str =NewTokenizer SCREAMING_SNAKE_CASE_ : str =False try: AutoConfig.register('custom' , __A ) AutoTokenizer.register(__A , slow_tokenizer_class=__A ) AutoTokenizer.register(__A , fast_tokenizer_class=__A ) # If remote code is not set, the default is to use local __UpperCamelCase = AutoTokenizer.from_pretrained('hf-internal-testing/test_dynamic_tokenizer' ) self.assertEqual(tokenizer.__class__.__name__ , 'NewTokenizerFast' ) self.assertFalse(tokenizer.special_attribute_present ) __UpperCamelCase = AutoTokenizer.from_pretrained('hf-internal-testing/test_dynamic_tokenizer' , use_fast=__A ) self.assertEqual(tokenizer.__class__.__name__ , 'NewTokenizer' ) self.assertFalse(tokenizer.special_attribute_present ) # If remote code is disabled, we load the local one. __UpperCamelCase = AutoTokenizer.from_pretrained( 'hf-internal-testing/test_dynamic_tokenizer' , trust_remote_code=__A ) self.assertEqual(tokenizer.__class__.__name__ , 'NewTokenizerFast' ) self.assertFalse(tokenizer.special_attribute_present ) __UpperCamelCase = AutoTokenizer.from_pretrained( 'hf-internal-testing/test_dynamic_tokenizer' , trust_remote_code=__A , use_fast=__A ) self.assertEqual(tokenizer.__class__.__name__ , 'NewTokenizer' ) self.assertFalse(tokenizer.special_attribute_present ) # If remote is enabled, we load from the Hub __UpperCamelCase = AutoTokenizer.from_pretrained( 'hf-internal-testing/test_dynamic_tokenizer' , trust_remote_code=__A ) self.assertEqual(tokenizer.__class__.__name__ , 'NewTokenizerFast' ) self.assertTrue(tokenizer.special_attribute_present ) __UpperCamelCase = AutoTokenizer.from_pretrained( 'hf-internal-testing/test_dynamic_tokenizer' , trust_remote_code=__A , use_fast=__A ) self.assertEqual(tokenizer.__class__.__name__ , 'NewTokenizer' ) self.assertTrue(tokenizer.special_attribute_present ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in TOKENIZER_MAPPING._extra_content: del TOKENIZER_MAPPING._extra_content[CustomConfig] def _lowerCamelCase ( self : Dict ): __UpperCamelCase = AutoTokenizer.from_pretrained( 'hf-internal-testing/test_dynamic_tokenizer_legacy' , trust_remote_code=__A ) self.assertTrue(tokenizer.special_attribute_present ) if is_tokenizers_available(): self.assertEqual(tokenizer.__class__.__name__ , 'NewTokenizerFast' ) # Test we can also load the slow version __UpperCamelCase = AutoTokenizer.from_pretrained( 'hf-internal-testing/test_dynamic_tokenizer_legacy' , trust_remote_code=__A , use_fast=__A ) self.assertTrue(tokenizer.special_attribute_present ) self.assertEqual(tokenizer.__class__.__name__ , 'NewTokenizer' ) else: self.assertEqual(tokenizer.__class__.__name__ , 'NewTokenizer' ) def _lowerCamelCase ( self : int ): with self.assertRaisesRegex( __A , 'bert-base is not a local folder and is not a valid model identifier' ): __UpperCamelCase = AutoTokenizer.from_pretrained('bert-base' ) def _lowerCamelCase ( self : str ): with self.assertRaisesRegex( __A , R'aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)' ): __UpperCamelCase = AutoTokenizer.from_pretrained(__A , revision='aaaaaa' ) def _lowerCamelCase ( self : Dict ): # Make sure we have cached the tokenizer. __UpperCamelCase = AutoTokenizer.from_pretrained('hf-internal-testing/tiny-random-bert' ) with RequestCounter() as counter: __UpperCamelCase = AutoTokenizer.from_pretrained('hf-internal-testing/tiny-random-bert' ) self.assertEqual(counter.get_request_count , 0 ) self.assertEqual(counter.head_request_count , 1 ) self.assertEqual(counter.other_request_count , 0 )
53
def __snake_case ( _lowerCAmelCase : list ) -> list: if len(_lowerCAmelCase ) <= 1: return [tuple(_lowerCAmelCase )] A_ : Tuple = [] def generate(_lowerCAmelCase : int , _lowerCAmelCase : list ): A_ : List[str] = [0] * n res.append(tuple(_lowerCAmelCase ) ) A_ : int = 0 while i < n: if c[i] < i: if i % 2 == 0: A_ , A_ : str = arr[i], arr[0] else: A_ , A_ : List[str] = arr[i], arr[c[i]] res.append(tuple(_lowerCAmelCase ) ) c[i] += 1 A_ : Tuple = 0 else: A_ : Dict = 0 i += 1 generate(len(_lowerCAmelCase ) , _lowerCAmelCase ) return res if __name__ == "__main__": _lowerCAmelCase : str = input('''Enter numbers separated by a comma:\n''').strip() _lowerCAmelCase : str = [int(item) for item in user_input.split(''',''')] print(heaps(arr))
300
0
"""simple docstring""" from typing import Callable, List, Optional, Union import PIL import torch from transformers import ( CLIPImageProcessor, CLIPSegForImageSegmentation, CLIPSegProcessor, CLIPTextModel, CLIPTokenizer, ) from diffusers import DiffusionPipeline from diffusers.configuration_utils import FrozenDict from diffusers.models import AutoencoderKL, UNetaDConditionModel from diffusers.pipelines.stable_diffusion import StableDiffusionInpaintPipeline from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker from diffusers.schedulers import DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler from diffusers.utils import deprecate, is_accelerate_available, logging a__ : str = logging.get_logger(__name__) # pylint: disable=invalid-name class UpperCamelCase_ ( UpperCamelCase): """simple docstring""" def __init__( self : List[Any] , UpperCAmelCase__ : CLIPSegForImageSegmentation , UpperCAmelCase__ : CLIPSegProcessor , UpperCAmelCase__ : AutoencoderKL , UpperCAmelCase__ : CLIPTextModel , UpperCAmelCase__ : CLIPTokenizer , UpperCAmelCase__ : UNetaDConditionModel , UpperCAmelCase__ : Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler] , UpperCAmelCase__ : StableDiffusionSafetyChecker , UpperCAmelCase__ : CLIPImageProcessor , ) -> List[Any]: super().__init__() if hasattr(scheduler.config , "steps_offset" ) and scheduler.config.steps_offset != 1: __SCREAMING_SNAKE_CASE = ( F"""The configuration file of this scheduler: {scheduler} is outdated. `steps_offset`""" F""" should be set to 1 instead of {scheduler.config.steps_offset}. Please make sure """ "to update the config accordingly as leaving `steps_offset` might led to incorrect results" " in future versions. If you have downloaded this checkpoint from the Hugging Face Hub," " it would be very nice if you could open a Pull request for the `scheduler/scheduler_config.json`" " file" ) deprecate("steps_offset!=1" , "1.0.0" , UpperCAmelCase__ , standard_warn=UpperCAmelCase__ ) __SCREAMING_SNAKE_CASE = dict(scheduler.config ) __SCREAMING_SNAKE_CASE = 1 __SCREAMING_SNAKE_CASE = FrozenDict(UpperCAmelCase__ ) if hasattr(scheduler.config , "skip_prk_steps" ) and scheduler.config.skip_prk_steps is False: __SCREAMING_SNAKE_CASE = ( F"""The configuration file of this scheduler: {scheduler} has not set the configuration""" " `skip_prk_steps`. `skip_prk_steps` should be set to True in the configuration file. Please make" " sure to update the config accordingly as not setting `skip_prk_steps` in the config might lead to" " incorrect results in future versions. If you have downloaded this checkpoint from the Hugging Face" " Hub, it would be very nice if you could open a Pull request for the" " `scheduler/scheduler_config.json` file" ) deprecate("skip_prk_steps not set" , "1.0.0" , UpperCAmelCase__ , standard_warn=UpperCAmelCase__ ) __SCREAMING_SNAKE_CASE = dict(scheduler.config ) __SCREAMING_SNAKE_CASE = True __SCREAMING_SNAKE_CASE = FrozenDict(UpperCAmelCase__ ) if safety_checker is None: logger.warning( F"""You have disabled the safety checker for {self.__class__} by passing `safety_checker=None`. Ensure""" " that you abide to the conditions of the Stable Diffusion license and do not expose unfiltered" " results in services or applications open to the public. Both the diffusers team and Hugging Face" " strongly recommend to keep the safety filter enabled in all public facing circumstances, disabling" " it only for use-cases that involve analyzing network behavior or auditing its results. For more" " information, please have a look at https://github.com/huggingface/diffusers/pull/254 ." ) self.register_modules( segmentation_model=UpperCAmelCase__ , segmentation_processor=UpperCAmelCase__ , vae=UpperCAmelCase__ , text_encoder=UpperCAmelCase__ , tokenizer=UpperCAmelCase__ , unet=UpperCAmelCase__ , scheduler=UpperCAmelCase__ , safety_checker=UpperCAmelCase__ , feature_extractor=UpperCAmelCase__ , ) def UpperCAmelCase_ ( self : Optional[Any] , UpperCAmelCase__ : Optional[Union[str, int]] = "auto" ) -> List[str]: if slice_size == "auto": # half the attention head size is usually a good trade-off between # speed and memory __SCREAMING_SNAKE_CASE = self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(UpperCAmelCase__ ) def UpperCAmelCase_ ( self : Tuple ) -> List[Any]: self.enable_attention_slicing(UpperCAmelCase__ ) def UpperCAmelCase_ ( self : Dict ) -> Dict: if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError("Please install accelerate via `pip install accelerate`" ) __SCREAMING_SNAKE_CASE = torch.device("cuda" ) for cpu_offloaded_model in [self.unet, self.text_encoder, self.vae, self.safety_checker]: if cpu_offloaded_model is not None: cpu_offload(UpperCAmelCase__ , UpperCAmelCase__ ) @property # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device def UpperCAmelCase_ ( self : Union[str, Any] ) -> Any: if self.device != torch.device("meta" ) or not hasattr(self.unet , "_hf_hook" ): return self.device for module in self.unet.modules(): if ( hasattr(UpperCAmelCase__ , "_hf_hook" ) and hasattr(module._hf_hook , "execution_device" ) and module._hf_hook.execution_device is not None ): return torch.device(module._hf_hook.execution_device ) return self.device @torch.no_grad() def __call__( self : List[Any] , UpperCAmelCase__ : Union[str, List[str]] , UpperCAmelCase__ : Union[torch.FloatTensor, PIL.Image.Image] , UpperCAmelCase__ : str , UpperCAmelCase__ : int = 5_1_2 , UpperCAmelCase__ : int = 5_1_2 , UpperCAmelCase__ : int = 5_0 , UpperCAmelCase__ : float = 7.5 , UpperCAmelCase__ : Optional[Union[str, List[str]]] = None , UpperCAmelCase__ : Optional[int] = 1 , UpperCAmelCase__ : float = 0.0 , UpperCAmelCase__ : Optional[torch.Generator] = None , UpperCAmelCase__ : Optional[torch.FloatTensor] = None , UpperCAmelCase__ : Optional[str] = "pil" , UpperCAmelCase__ : bool = True , UpperCAmelCase__ : Optional[Callable[[int, int, torch.FloatTensor], None]] = None , UpperCAmelCase__ : int = 1 , **UpperCAmelCase__ : Optional[Any] , ) -> Any: __SCREAMING_SNAKE_CASE = self.segmentation_processor( text=[text] , images=[image] , padding="max_length" , return_tensors="pt" ).to(self.device ) __SCREAMING_SNAKE_CASE = self.segmentation_model(**UpperCAmelCase__ ) __SCREAMING_SNAKE_CASE = torch.sigmoid(outputs.logits ).cpu().detach().unsqueeze(-1 ).numpy() __SCREAMING_SNAKE_CASE = self.numpy_to_pil(UpperCAmelCase__ )[0].resize(image.size ) # Run inpainting pipeline with the generated mask __SCREAMING_SNAKE_CASE = StableDiffusionInpaintPipeline( vae=self.vae , text_encoder=self.text_encoder , tokenizer=self.tokenizer , unet=self.unet , scheduler=self.scheduler , safety_checker=self.safety_checker , feature_extractor=self.feature_extractor , ) return inpainting_pipeline( prompt=UpperCAmelCase__ , image=UpperCAmelCase__ , mask_image=UpperCAmelCase__ , height=UpperCAmelCase__ , width=UpperCAmelCase__ , num_inference_steps=UpperCAmelCase__ , guidance_scale=UpperCAmelCase__ , negative_prompt=UpperCAmelCase__ , num_images_per_prompt=UpperCAmelCase__ , eta=UpperCAmelCase__ , generator=UpperCAmelCase__ , latents=UpperCAmelCase__ , output_type=UpperCAmelCase__ , return_dict=UpperCAmelCase__ , callback=UpperCAmelCase__ , callback_steps=UpperCAmelCase__ , )
54
import json from typing import List, Optional, Tuple from tokenizers import pre_tokenizers, processors from ...tokenization_utils_base import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_roberta import RobertaTokenizer _lowerCAmelCase : int = logging.get_logger(__name__) _lowerCAmelCase : Optional[int] = {'''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt''', '''tokenizer_file''': '''tokenizer.json'''} _lowerCAmelCase : List[Any] = { '''vocab_file''': { '''roberta-base''': '''https://huggingface.co/roberta-base/resolve/main/vocab.json''', '''roberta-large''': '''https://huggingface.co/roberta-large/resolve/main/vocab.json''', '''roberta-large-mnli''': '''https://huggingface.co/roberta-large-mnli/resolve/main/vocab.json''', '''distilroberta-base''': '''https://huggingface.co/distilroberta-base/resolve/main/vocab.json''', '''roberta-base-openai-detector''': '''https://huggingface.co/roberta-base-openai-detector/resolve/main/vocab.json''', '''roberta-large-openai-detector''': ( '''https://huggingface.co/roberta-large-openai-detector/resolve/main/vocab.json''' ), }, '''merges_file''': { '''roberta-base''': '''https://huggingface.co/roberta-base/resolve/main/merges.txt''', '''roberta-large''': '''https://huggingface.co/roberta-large/resolve/main/merges.txt''', '''roberta-large-mnli''': '''https://huggingface.co/roberta-large-mnli/resolve/main/merges.txt''', '''distilroberta-base''': '''https://huggingface.co/distilroberta-base/resolve/main/merges.txt''', '''roberta-base-openai-detector''': '''https://huggingface.co/roberta-base-openai-detector/resolve/main/merges.txt''', '''roberta-large-openai-detector''': ( '''https://huggingface.co/roberta-large-openai-detector/resolve/main/merges.txt''' ), }, '''tokenizer_file''': { '''roberta-base''': '''https://huggingface.co/roberta-base/resolve/main/tokenizer.json''', '''roberta-large''': '''https://huggingface.co/roberta-large/resolve/main/tokenizer.json''', '''roberta-large-mnli''': '''https://huggingface.co/roberta-large-mnli/resolve/main/tokenizer.json''', '''distilroberta-base''': '''https://huggingface.co/distilroberta-base/resolve/main/tokenizer.json''', '''roberta-base-openai-detector''': ( '''https://huggingface.co/roberta-base-openai-detector/resolve/main/tokenizer.json''' ), '''roberta-large-openai-detector''': ( '''https://huggingface.co/roberta-large-openai-detector/resolve/main/tokenizer.json''' ), }, } _lowerCAmelCase : Any = { '''roberta-base''': 512, '''roberta-large''': 512, '''roberta-large-mnli''': 512, '''distilroberta-base''': 512, '''roberta-base-openai-detector''': 512, '''roberta-large-openai-detector''': 512, } class __magic_name__ ( lowerCamelCase__ ): """simple docstring""" __UpperCamelCase = VOCAB_FILES_NAMES __UpperCamelCase = PRETRAINED_VOCAB_FILES_MAP __UpperCamelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCamelCase = ['''input_ids''', '''attention_mask'''] __UpperCamelCase = RobertaTokenizer def __init__( self :Dict , snake_case :List[str]=None , snake_case :List[Any]=None , snake_case :Union[str, Any]=None , snake_case :List[str]="replace" , snake_case :Tuple="<s>" , snake_case :Union[str, Any]="</s>" , snake_case :str="</s>" , snake_case :Union[str, Any]="<s>" , snake_case :int="<unk>" , snake_case :Tuple="<pad>" , snake_case :List[str]="<mask>" , snake_case :Any=False , snake_case :Union[str, Any]=True , **snake_case :Optional[int] , ): '''simple docstring''' super().__init__( snake_case , snake_case , tokenizer_file=snake_case , errors=snake_case , bos_token=snake_case , eos_token=snake_case , sep_token=snake_case , cls_token=snake_case , unk_token=snake_case , pad_token=snake_case , mask_token=snake_case , add_prefix_space=snake_case , trim_offsets=snake_case , **snake_case , ) A_ : Optional[Any] = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get("add_prefix_space" , snake_case ) != add_prefix_space: A_ : Dict = getattr(snake_case , pre_tok_state.pop("type" ) ) A_ : Optional[int] = add_prefix_space A_ : int = pre_tok_class(**snake_case ) A_ : Optional[int] = add_prefix_space A_ : Optional[int] = "post_processor" A_ : Dict = getattr(self.backend_tokenizer , snake_case , snake_case ) if tokenizer_component_instance: A_ : Dict = json.loads(tokenizer_component_instance.__getstate__() ) # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class` if "sep" in state: A_ : List[Any] = tuple(state["sep"] ) if "cls" in state: A_ : Optional[Any] = tuple(state["cls"] ) A_ : Tuple = False if state.get("add_prefix_space" , snake_case ) != add_prefix_space: A_ : List[Any] = add_prefix_space A_ : Optional[int] = True if state.get("trim_offsets" , snake_case ) != trim_offsets: A_ : List[str] = trim_offsets A_ : Any = True if changes_to_apply: A_ : Optional[Any] = getattr(snake_case , state.pop("type" ) ) A_ : Any = component_class(**snake_case ) setattr(self.backend_tokenizer , snake_case , snake_case ) @property def SCREAMING_SNAKE_CASE ( self :List[Any] ): '''simple docstring''' if self._mask_token is None: if self.verbose: logger.error("Using mask_token, but it is not set yet." ) return None return str(self._mask_token ) @mask_token.setter def SCREAMING_SNAKE_CASE ( self :Any , snake_case :Dict ): '''simple docstring''' A_ : Dict = AddedToken(snake_case , lstrip=snake_case , rstrip=snake_case ) if isinstance(snake_case , snake_case ) else value A_ : Any = value def SCREAMING_SNAKE_CASE ( self :Dict , *snake_case :Tuple , **snake_case :Union[str, Any] ): '''simple docstring''' A_ : Any = kwargs.get("is_split_into_words" , snake_case ) assert self.add_prefix_space or not is_split_into_words, ( f"You need to instantiate {self.__class__.__name__} with add_prefix_space=True " "to use it with pretokenized inputs." ) return super()._batch_encode_plus(*snake_case , **snake_case ) def SCREAMING_SNAKE_CASE ( self :List[str] , *snake_case :str , **snake_case :Union[str, Any] ): '''simple docstring''' A_ : Any = kwargs.get("is_split_into_words" , snake_case ) assert self.add_prefix_space or not is_split_into_words, ( f"You need to instantiate {self.__class__.__name__} with add_prefix_space=True " "to use it with pretokenized inputs." ) return super()._encode_plus(*snake_case , **snake_case ) def SCREAMING_SNAKE_CASE ( self :Union[str, Any] , snake_case :str , snake_case :Optional[str] = None ): '''simple docstring''' A_ : str = self._tokenizer.model.save(snake_case , name=snake_case ) return tuple(snake_case ) def SCREAMING_SNAKE_CASE ( self :List[str] , snake_case :List[str] , snake_case :Optional[Any]=None ): '''simple docstring''' A_ : int = [self.bos_token_id] + token_ids_a + [self.eos_token_id] if token_ids_a is None: return output return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id] def SCREAMING_SNAKE_CASE ( self :Any , snake_case :List[int] , snake_case :Optional[List[int]] = None ): '''simple docstring''' A_ : Any = [self.sep_token_id] A_ : Optional[Any] = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0]
300
0
'''simple docstring''' import tempfile import unittest from make_student import create_student_by_copying_alternating_layers from transformers import AutoConfig from transformers.file_utils import cached_property from transformers.testing_utils import require_torch a_ : List[str] = """sshleifer/bart-tiny-random""" a_ : Union[str, Any] = """patrickvonplaten/t5-tiny-random""" @require_torch class snake_case ( unittest.TestCase ): """simple docstring""" @cached_property def snake_case ( self ): """simple docstring""" return AutoConfig.from_pretrained(UpperCamelCase ) def snake_case ( self ): """simple docstring""" lowerCamelCase_ ,*lowerCamelCase_ = create_student_by_copying_alternating_layers(UpperCamelCase , tempfile.mkdtemp() , e=1 , d=1 ) self.assertEqual(student.config.num_hidden_layers , 1 ) def snake_case ( self ): """simple docstring""" lowerCamelCase_ ,*lowerCamelCase_ = create_student_by_copying_alternating_layers(UpperCamelCase , tempfile.mkdtemp() , e=1 , d=UpperCamelCase ) def snake_case ( self ): """simple docstring""" lowerCamelCase_ ,*lowerCamelCase_ = create_student_by_copying_alternating_layers(UpperCamelCase , tempfile.mkdtemp() , e=1 , d=UpperCamelCase ) self.assertEqual(student.config.encoder_layers , 1 ) self.assertEqual(student.config.decoder_layers , self.teacher_config.encoder_layers ) def snake_case ( self ): """simple docstring""" lowerCamelCase_ ,*lowerCamelCase_ = create_student_by_copying_alternating_layers(UpperCamelCase , tempfile.mkdtemp() , e=1 , d=1 ) self.assertEqual(student.config.encoder_layers , 1 ) self.assertEqual(student.config.decoder_layers , 1 ) def snake_case ( self ): """simple docstring""" with self.assertRaises(UpperCamelCase ): create_student_by_copying_alternating_layers(UpperCamelCase , tempfile.mkdtemp() , e=UpperCamelCase , d=UpperCamelCase )
55
from typing import Dict, List from nltk.translate import gleu_score import datasets from datasets import MetricInfo _lowerCAmelCase : int = '''\ @misc{wu2016googles, title={Google\'s Neural Machine Translation System: Bridging the Gap between Human and Machine Translation}, author={Yonghui Wu and Mike Schuster and Zhifeng Chen and Quoc V. Le and Mohammad Norouzi and Wolfgang Macherey and Maxim Krikun and Yuan Cao and Qin Gao and Klaus Macherey and Jeff Klingner and Apurva Shah and Melvin Johnson and Xiaobing Liu and Łukasz Kaiser and Stephan Gouws and Yoshikiyo Kato and Taku Kudo and Hideto Kazawa and Keith Stevens and George Kurian and Nishant Patil and Wei Wang and Cliff Young and Jason Smith and Jason Riesa and Alex Rudnick and Oriol Vinyals and Greg Corrado and Macduff Hughes and Jeffrey Dean}, year={2016}, eprint={1609.08144}, archivePrefix={arXiv}, primaryClass={cs.CL} } ''' _lowerCAmelCase : Tuple = '''\ The BLEU score has some undesirable properties when used for single sentences, as it was designed to be a corpus measure. We therefore use a slightly different score for our RL experiments which we call the \'GLEU score\'. For the GLEU score, we record all sub-sequences of 1, 2, 3 or 4 tokens in output and target sequence (n-grams). We then compute a recall, which is the ratio of the number of matching n-grams to the number of total n-grams in the target (ground truth) sequence, and a precision, which is the ratio of the number of matching n-grams to the number of total n-grams in the generated output sequence. Then GLEU score is simply the minimum of recall and precision. This GLEU score\'s range is always between 0 (no matches) and 1 (all match) and it is symmetrical when switching output and target. According to our experiments, GLEU score correlates quite well with the BLEU metric on a corpus level but does not have its drawbacks for our per sentence reward objective. ''' _lowerCAmelCase : int = '''\ Computes corpus-level Google BLEU (GLEU) score of translated segments against one or more references. Instead of averaging the sentence level GLEU scores (i.e. macro-average precision), Wu et al. (2016) sum up the matching tokens and the max of hypothesis and reference tokens for each sentence, then compute using the aggregate values. Args: predictions (list of str): list of translations to score. Each translation should be tokenized into a list of tokens. references (list of list of str): list of lists of references for each translation. Each reference should be tokenized into a list of tokens. min_len (int): The minimum order of n-gram this function should extract. Defaults to 1. max_len (int): The maximum order of n-gram this function should extract. Defaults to 4. Returns: \'google_bleu\': google_bleu score Examples: Example 1: >>> hyp1 = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'which\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'always\', ... \'disobeys\', \'the\', \'commands\', \'of\', \'the\', \'cat\'] >>> ref1a = [\'It\', \'is\', \'the\', \'guiding\', \'principle\', \'which\', ... \'guarantees\', \'the\', \'rubber\', \'duck\', \'forces\', \'never\', ... \'being\', \'under\', \'the\', \'command\', \'of\', \'the\', \'cat\'] >>> hyp2 = [\'he\', \'read\', \'the\', \'book\', \'because\', \'he\', \'was\', ... \'interested\', \'in\', \'world\', \'history\'] >>> ref2a = [\'he\', \'was\', \'interested\', \'in\', \'world\', \'history\', ... \'because\', \'he\', \'read\', \'the\', \'book\'] >>> list_of_references = [[ref1a], [ref2a]] >>> hypotheses = [hyp1, hyp2] >>> google_bleu = datasets.load_metric("google_bleu") >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references) >>> print(round(results["google_bleu"], 2)) 0.44 Example 2: >>> hyp1 = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'which\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'always\', ... \'disobeys\', \'the\', \'commands\', \'of\', \'the\', \'cat\'] >>> ref1a = [\'It\', \'is\', \'the\', \'guiding\', \'principle\', \'which\', ... \'guarantees\', \'the\', \'rubber\', \'duck\', \'forces\', \'never\', ... \'being\', \'under\', \'the\', \'command\', \'of\', \'the\', \'cat\'] >>> ref1b = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'that\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'will\', \'never\', ... \'heed\', \'the\', \'cat\', \'commands\'] >>> ref1c = [\'It\', \'is\', \'the\', \'practical\', \'guide\', \'for\', \'the\', ... \'rubber\', \'duck\', \'army\', \'never\', \'to\', \'heed\', \'the\', \'directions\', ... \'of\', \'the\', \'cat\'] >>> hyp2 = [\'he\', \'read\', \'the\', \'book\', \'because\', \'he\', \'was\', ... \'interested\', \'in\', \'world\', \'history\'] >>> ref2a = [\'he\', \'was\', \'interested\', \'in\', \'world\', \'history\', ... \'because\', \'he\', \'read\', \'the\', \'book\'] >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]] >>> hypotheses = [hyp1, hyp2] >>> google_bleu = datasets.load_metric("google_bleu") >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references) >>> print(round(results["google_bleu"], 2)) 0.61 Example 3: >>> hyp1 = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'which\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'always\', ... \'disobeys\', \'the\', \'commands\', \'of\', \'the\', \'cat\'] >>> ref1a = [\'It\', \'is\', \'the\', \'guiding\', \'principle\', \'which\', ... \'guarantees\', \'the\', \'rubber\', \'duck\', \'forces\', \'never\', ... \'being\', \'under\', \'the\', \'command\', \'of\', \'the\', \'cat\'] >>> ref1b = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'that\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'will\', \'never\', ... \'heed\', \'the\', \'cat\', \'commands\'] >>> ref1c = [\'It\', \'is\', \'the\', \'practical\', \'guide\', \'for\', \'the\', ... \'rubber\', \'duck\', \'army\', \'never\', \'to\', \'heed\', \'the\', \'directions\', ... \'of\', \'the\', \'cat\'] >>> hyp2 = [\'he\', \'read\', \'the\', \'book\', \'because\', \'he\', \'was\', ... \'interested\', \'in\', \'world\', \'history\'] >>> ref2a = [\'he\', \'was\', \'interested\', \'in\', \'world\', \'history\', ... \'because\', \'he\', \'read\', \'the\', \'book\'] >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]] >>> hypotheses = [hyp1, hyp2] >>> google_bleu = datasets.load_metric("google_bleu") >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references, min_len=2) >>> print(round(results["google_bleu"], 2)) 0.53 Example 4: >>> hyp1 = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'which\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'always\', ... \'disobeys\', \'the\', \'commands\', \'of\', \'the\', \'cat\'] >>> ref1a = [\'It\', \'is\', \'the\', \'guiding\', \'principle\', \'which\', ... \'guarantees\', \'the\', \'rubber\', \'duck\', \'forces\', \'never\', ... \'being\', \'under\', \'the\', \'command\', \'of\', \'the\', \'cat\'] >>> ref1b = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'that\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'will\', \'never\', ... \'heed\', \'the\', \'cat\', \'commands\'] >>> ref1c = [\'It\', \'is\', \'the\', \'practical\', \'guide\', \'for\', \'the\', ... \'rubber\', \'duck\', \'army\', \'never\', \'to\', \'heed\', \'the\', \'directions\', ... \'of\', \'the\', \'cat\'] >>> hyp2 = [\'he\', \'read\', \'the\', \'book\', \'because\', \'he\', \'was\', ... \'interested\', \'in\', \'world\', \'history\'] >>> ref2a = [\'he\', \'was\', \'interested\', \'in\', \'world\', \'history\', ... \'because\', \'he\', \'read\', \'the\', \'book\'] >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]] >>> hypotheses = [hyp1, hyp2] >>> google_bleu = datasets.load_metric("google_bleu") >>> results = google_bleu.compute(predictions=hypotheses,references=list_of_references, min_len=2, max_len=6) >>> print(round(results["google_bleu"], 2)) 0.4 ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __magic_name__ ( datasets.Metric ): """simple docstring""" def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "predictions": datasets.Sequence(datasets.Value("string" , id="token" ) , id="sequence" ), "references": datasets.Sequence( datasets.Sequence(datasets.Value("string" , id="token" ) , id="sequence" ) , id="references" ), } ) , ) def SCREAMING_SNAKE_CASE ( self :int , snake_case :List[List[List[str]]] , snake_case :List[List[str]] , snake_case :int = 1 , snake_case :int = 4 , ): '''simple docstring''' return { "google_bleu": gleu_score.corpus_gleu( list_of_references=snake_case , hypotheses=snake_case , min_len=snake_case , max_len=snake_case ) }
300
0
'''simple docstring''' from transformers import HfArgumentParser, TensorFlowBenchmark, TensorFlowBenchmarkArguments def __magic_name__ ( ) -> List[Any]: '''simple docstring''' snake_case_ = HfArgumentParser(__UpperCAmelCase ) snake_case_ = parser.parse_args_into_dataclasses()[0] snake_case_ = TensorFlowBenchmark(args=__UpperCAmelCase ) try: snake_case_ = parser.parse_args_into_dataclasses()[0] except ValueError as e: snake_case_ = '''Arg --no_{0} is no longer used, please use --no-{0} instead.''' snake_case_ = ''' '''.join(str(__UpperCAmelCase ).split(''' ''' )[:-1] ) snake_case_ = '''''' snake_case_ = eval(str(__UpperCAmelCase ).split(''' ''' )[-1] ) snake_case_ = [] for arg in depreciated_args: # arg[2:] removes '--' if arg[2:] in TensorFlowBenchmark.deprecated_args: # arg[5:] removes '--no_' full_error_msg += arg_error_msg.format(arg[5:] ) else: wrong_args.append(__UpperCAmelCase ) if len(__UpperCAmelCase ) > 0: snake_case_ = full_error_msg + begin_error_msg + str(__UpperCAmelCase ) raise ValueError(__UpperCAmelCase ) benchmark.run() if __name__ == "__main__": main()
56
import pyarrow.parquet as pq import pytest from datasets import Audio, Dataset, DatasetDict, Features, NamedSplit, Sequence, Value, config from datasets.features.image import Image from datasets.io.parquet import ParquetDatasetReader, ParquetDatasetWriter, get_writer_batch_size from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases def __snake_case ( _lowerCAmelCase : List[Any] , _lowerCAmelCase : Optional[int] ) -> str: assert isinstance(_lowerCAmelCase , _lowerCAmelCase ) assert dataset.num_rows == 4 assert dataset.num_columns == 3 assert dataset.column_names == ["col_1", "col_2", "col_3"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize("keep_in_memory" , [False, True] ) def __snake_case ( _lowerCAmelCase : Dict , _lowerCAmelCase : Optional[int] , _lowerCAmelCase : List[Any] ) -> Optional[int]: A_ : Tuple = tmp_path / "cache" A_ : Optional[int] = {"col_1": "string", "col_2": "int64", "col_3": "float64"} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): A_ : Optional[Any] = ParquetDatasetReader(_lowerCAmelCase , cache_dir=_lowerCAmelCase , keep_in_memory=_lowerCAmelCase ).read() _check_parquet_dataset(_lowerCAmelCase , _lowerCAmelCase ) @pytest.mark.parametrize( "features" , [ None, {"col_1": "string", "col_2": "int64", "col_3": "float64"}, {"col_1": "string", "col_2": "string", "col_3": "string"}, {"col_1": "int32", "col_2": "int32", "col_3": "int32"}, {"col_1": "float32", "col_2": "float32", "col_3": "float32"}, ] , ) def __snake_case ( _lowerCAmelCase : List[Any] , _lowerCAmelCase : Any , _lowerCAmelCase : List[Any] ) -> str: A_ : List[Any] = tmp_path / "cache" A_ : List[str] = {"col_1": "string", "col_2": "int64", "col_3": "float64"} A_ : int = features.copy() if features else default_expected_features A_ : str = ( Features({feature: Value(_lowerCAmelCase ) for feature, dtype in features.items()} ) if features is not None else None ) A_ : Union[str, Any] = ParquetDatasetReader(_lowerCAmelCase , features=_lowerCAmelCase , cache_dir=_lowerCAmelCase ).read() _check_parquet_dataset(_lowerCAmelCase , _lowerCAmelCase ) @pytest.mark.parametrize("split" , [None, NamedSplit("train" ), "train", "test"] ) def __snake_case ( _lowerCAmelCase : Any , _lowerCAmelCase : Any , _lowerCAmelCase : Any ) -> Optional[Any]: A_ : Dict = tmp_path / "cache" A_ : int = {"col_1": "string", "col_2": "int64", "col_3": "float64"} A_ : Optional[int] = ParquetDatasetReader(_lowerCAmelCase , cache_dir=_lowerCAmelCase , split=_lowerCAmelCase ).read() _check_parquet_dataset(_lowerCAmelCase , _lowerCAmelCase ) assert dataset.split == split if split else "train" @pytest.mark.parametrize("path_type" , [str, list] ) def __snake_case ( _lowerCAmelCase : List[str] , _lowerCAmelCase : int , _lowerCAmelCase : Optional[Any] ) -> List[str]: if issubclass(_lowerCAmelCase , _lowerCAmelCase ): A_ : int = parquet_path elif issubclass(_lowerCAmelCase , _lowerCAmelCase ): A_ : Optional[int] = [parquet_path] A_ : Optional[int] = tmp_path / "cache" A_ : Union[str, Any] = {"col_1": "string", "col_2": "int64", "col_3": "float64"} A_ : Optional[int] = ParquetDatasetReader(_lowerCAmelCase , cache_dir=_lowerCAmelCase ).read() _check_parquet_dataset(_lowerCAmelCase , _lowerCAmelCase ) def __snake_case ( _lowerCAmelCase : Any , _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : Union[str, Any]=("train",) ) -> Tuple: assert isinstance(_lowerCAmelCase , _lowerCAmelCase ) for split in splits: A_ : List[str] = dataset_dict[split] assert dataset.num_rows == 4 assert dataset.num_columns == 3 assert dataset.column_names == ["col_1", "col_2", "col_3"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize("keep_in_memory" , [False, True] ) def __snake_case ( _lowerCAmelCase : Optional[Any] , _lowerCAmelCase : List[Any] , _lowerCAmelCase : Dict ) -> Optional[int]: A_ : Optional[Any] = tmp_path / "cache" A_ : Union[str, Any] = {"col_1": "string", "col_2": "int64", "col_3": "float64"} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): A_ : Union[str, Any] = ParquetDatasetReader( {"train": parquet_path} , cache_dir=_lowerCAmelCase , keep_in_memory=_lowerCAmelCase ).read() _check_parquet_datasetdict(_lowerCAmelCase , _lowerCAmelCase ) @pytest.mark.parametrize( "features" , [ None, {"col_1": "string", "col_2": "int64", "col_3": "float64"}, {"col_1": "string", "col_2": "string", "col_3": "string"}, {"col_1": "int32", "col_2": "int32", "col_3": "int32"}, {"col_1": "float32", "col_2": "float32", "col_3": "float32"}, ] , ) def __snake_case ( _lowerCAmelCase : Tuple , _lowerCAmelCase : Any , _lowerCAmelCase : str ) -> Tuple: A_ : Optional[Any] = tmp_path / "cache" A_ : Any = {"col_1": "string", "col_2": "int64", "col_3": "float64"} A_ : List[str] = features.copy() if features else default_expected_features A_ : Tuple = ( Features({feature: Value(_lowerCAmelCase ) for feature, dtype in features.items()} ) if features is not None else None ) A_ : Optional[int] = ParquetDatasetReader({"train": parquet_path} , features=_lowerCAmelCase , cache_dir=_lowerCAmelCase ).read() _check_parquet_datasetdict(_lowerCAmelCase , _lowerCAmelCase ) @pytest.mark.parametrize("split" , [None, NamedSplit("train" ), "train", "test"] ) def __snake_case ( _lowerCAmelCase : str , _lowerCAmelCase : str , _lowerCAmelCase : Any ) -> Union[str, Any]: if split: A_ : Any = {split: parquet_path} else: A_ : Optional[Any] = "train" A_ : str = {"train": parquet_path, "test": parquet_path} A_ : Any = tmp_path / "cache" A_ : str = {"col_1": "string", "col_2": "int64", "col_3": "float64"} A_ : Dict = ParquetDatasetReader(_lowerCAmelCase , cache_dir=_lowerCAmelCase ).read() _check_parquet_datasetdict(_lowerCAmelCase , _lowerCAmelCase , splits=list(path.keys() ) ) assert all(dataset[split].split == split for split in path.keys() ) def __snake_case ( _lowerCAmelCase : Dict , _lowerCAmelCase : Optional[Any] ) -> Dict: A_ : List[str] = ParquetDatasetWriter(_lowerCAmelCase , tmp_path / "foo.parquet" ) assert writer.write() > 0 A_ : Tuple = pq.ParquetFile(tmp_path / "foo.parquet" ) A_ : Dict = pf.read() assert dataset.data.table == output_table def __snake_case ( _lowerCAmelCase : Optional[Any] , _lowerCAmelCase : int ) -> List[Any]: A_ : Tuple = str(shared_datadir / "test_image_rgb.jpg" ) A_ : int = {"image": [image_path]} A_ : Optional[Any] = Features({"image": Image()} ) A_ : Union[str, Any] = Dataset.from_dict(_lowerCAmelCase , features=_lowerCAmelCase ) A_ : Tuple = ParquetDatasetWriter(_lowerCAmelCase , tmp_path / "foo.parquet" ) assert writer.write() > 0 A_ : str = Dataset.from_parquet(str(tmp_path / "foo.parquet" ) ) assert dataset.features == reloaded_dataset.features A_ : int = ParquetDatasetReader(str(tmp_path / "foo.parquet" ) , streaming=_lowerCAmelCase ).read() assert dataset.features == reloaded_iterable_dataset.features @pytest.mark.parametrize( "feature, expected" , [ (Features({"foo": Value("int32" )} ), None), (Features({"image": Image(), "foo": Value("int32" )} ), config.PARQUET_ROW_GROUP_SIZE_FOR_IMAGE_DATASETS), (Features({"nested": Sequence(Audio() )} ), config.PARQUET_ROW_GROUP_SIZE_FOR_AUDIO_DATASETS), ] , ) def __snake_case ( _lowerCAmelCase : str , _lowerCAmelCase : List[Any] ) -> Any: assert get_writer_batch_size(_lowerCAmelCase ) == expected
300
0
"""simple docstring""" import os import unittest from transformers import LxmertTokenizer, LxmertTokenizerFast from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class _UpperCamelCase ( lowerCAmelCase__ ,unittest.TestCase ): '''simple docstring''' __UpperCAmelCase : Any =LxmertTokenizer __UpperCAmelCase : Optional[int] =LxmertTokenizerFast __UpperCAmelCase : int =True __UpperCAmelCase : Dict =True def snake_case ( self ): super().setUp() __lowerCAmelCase = [ "[UNK]", "[CLS]", "[SEP]", "want", "##want", "##ed", "wa", "un", "runn", "##ing", ",", "low", "lowest", ] __lowerCAmelCase = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["vocab_file"] ) with open(self.vocab_file , "w" , encoding="utf-8" ) as vocab_writer: vocab_writer.write("".join([x + "\n" for x in vocab_tokens] ) ) def snake_case ( self , __a ): __lowerCAmelCase = "UNwant\u00E9d,running" __lowerCAmelCase = "unwanted, running" return input_text, output_text def snake_case ( self ): __lowerCAmelCase = self.tokenizer_class(self.vocab_file ) __lowerCAmelCase = tokenizer.tokenize("UNwant\u00E9d,running" ) self.assertListEqual(__a , ["un", "##want", "##ed", ",", "runn", "##ing"] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(__a ) , [7, 4, 5, 10, 8, 9] ) def snake_case ( self ): if not self.test_rust_tokenizer: return __lowerCAmelCase = self.get_tokenizer() __lowerCAmelCase = self.get_rust_tokenizer() __lowerCAmelCase = "I was born in 92000, and this is falsé." __lowerCAmelCase = tokenizer.tokenize(__a ) __lowerCAmelCase = rust_tokenizer.tokenize(__a ) self.assertListEqual(__a , __a ) __lowerCAmelCase = tokenizer.encode(__a , add_special_tokens=__a ) __lowerCAmelCase = rust_tokenizer.encode(__a , add_special_tokens=__a ) self.assertListEqual(__a , __a ) __lowerCAmelCase = self.get_rust_tokenizer() __lowerCAmelCase = tokenizer.encode(__a ) __lowerCAmelCase = rust_tokenizer.encode(__a ) self.assertListEqual(__a , __a )
57
import json import unittest import numpy as np from huggingface_hub import hf_hub_download from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from transformers import OneFormerImageProcessor from transformers.models.oneformer.image_processing_oneformer import binary_mask_to_rle from transformers.models.oneformer.modeling_oneformer import OneFormerForUniversalSegmentationOutput if is_vision_available(): from PIL import Image def __snake_case ( _lowerCAmelCase : List[str] , _lowerCAmelCase : List[Any]="shi-labs/oneformer_demo" ) -> int: with open(hf_hub_download(_lowerCAmelCase , _lowerCAmelCase , repo_type="dataset" ) , "r" ) as f: A_ : Optional[int] = json.load(_lowerCAmelCase ) A_ : Union[str, Any] = {} A_ : Tuple = [] A_ : Optional[Any] = [] for key, info in class_info.items(): A_ : Tuple = info["name"] class_names.append(info["name"] ) if info["isthing"]: thing_ids.append(int(_lowerCAmelCase ) ) A_ : Optional[Any] = thing_ids A_ : int = class_names return metadata class __magic_name__ ( unittest.TestCase ): """simple docstring""" def __init__( self :List[Any] , snake_case :List[str] , snake_case :int=7 , snake_case :Optional[int]=3 , snake_case :Union[str, Any]=30 , snake_case :Tuple=400 , snake_case :List[Any]=None , snake_case :Optional[Any]=True , snake_case :Tuple=True , snake_case :Dict=[0.5, 0.5, 0.5] , snake_case :Any=[0.5, 0.5, 0.5] , snake_case :Optional[int]=10 , snake_case :Tuple=False , snake_case :Optional[int]=255 , snake_case :Optional[Any]="shi-labs/oneformer_demo" , snake_case :Optional[Any]="ade20k_panoptic.json" , snake_case :Optional[int]=10 , ): '''simple docstring''' A_ : Tuple = parent A_ : List[str] = batch_size A_ : Optional[int] = num_channels A_ : Tuple = min_resolution A_ : List[Any] = max_resolution A_ : Union[str, Any] = do_resize A_ : Any = {"shortest_edge": 32, "longest_edge": 1_333} if size is None else size A_ : Tuple = do_normalize A_ : List[str] = image_mean A_ : List[Any] = image_std A_ : Union[str, Any] = class_info_file A_ : List[Any] = prepare_metadata(snake_case , snake_case ) A_ : Tuple = num_text A_ : str = repo_path # for the post_process_functions A_ : Any = 2 A_ : int = 10 A_ : Optional[int] = 10 A_ : Tuple = 3 A_ : Tuple = 4 A_ : str = num_labels A_ : int = do_reduce_labels A_ : List[Any] = ignore_index def SCREAMING_SNAKE_CASE ( self :Optional[Any] ): '''simple docstring''' return { "do_resize": self.do_resize, "size": self.size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, "num_labels": self.num_labels, "do_reduce_labels": self.do_reduce_labels, "ignore_index": self.ignore_index, "class_info_file": self.class_info_file, "metadata": self.metadata, "num_text": self.num_text, } def SCREAMING_SNAKE_CASE ( self :List[Any] , snake_case :Any , snake_case :Any=False ): '''simple docstring''' if not batched: A_ : List[str] = image_inputs[0] if isinstance(snake_case , Image.Image ): A_ , A_ : Dict = image.size else: A_ , A_ : Tuple = image.shape[1], image.shape[2] if w < h: A_ : str = int(self.size["shortest_edge"] * h / w ) A_ : Any = self.size["shortest_edge"] elif w > h: A_ : Optional[int] = self.size["shortest_edge"] A_ : List[str] = int(self.size["shortest_edge"] * w / h ) else: A_ : List[str] = self.size["shortest_edge"] A_ : Optional[Any] = self.size["shortest_edge"] else: A_ : Tuple = [] for image in image_inputs: A_ , A_ : Optional[Any] = self.get_expected_values([image] ) expected_values.append((expected_height, expected_width) ) A_ : Tuple = max(snake_case , key=lambda snake_case : item[0] )[0] A_ : Union[str, Any] = max(snake_case , key=lambda snake_case : item[1] )[1] return expected_height, expected_width def SCREAMING_SNAKE_CASE ( self :Tuple ): '''simple docstring''' return OneFormerForUniversalSegmentationOutput( # +1 for null class class_queries_logits=torch.randn((self.batch_size, self.num_queries, self.num_classes + 1) ) , masks_queries_logits=torch.randn((self.batch_size, self.num_queries, self.height, self.width) ) , ) @require_torch @require_vision class __magic_name__ ( lowerCamelCase__ , unittest.TestCase ): """simple docstring""" __UpperCamelCase = OneFormerImageProcessor if (is_vision_available() and is_torch_available()) else None # only for test_image_processing_common.test_image_proc_to_json_string __UpperCamelCase = image_processing_class def SCREAMING_SNAKE_CASE ( self :int ): '''simple docstring''' A_ : Union[str, Any] = OneFormerImageProcessorTester(self ) @property def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' return self.image_processing_tester.prepare_image_processor_dict() def SCREAMING_SNAKE_CASE ( self :List[Any] ): '''simple docstring''' A_ : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(snake_case , "image_mean" ) ) self.assertTrue(hasattr(snake_case , "image_std" ) ) self.assertTrue(hasattr(snake_case , "do_normalize" ) ) self.assertTrue(hasattr(snake_case , "do_resize" ) ) self.assertTrue(hasattr(snake_case , "size" ) ) self.assertTrue(hasattr(snake_case , "ignore_index" ) ) self.assertTrue(hasattr(snake_case , "class_info_file" ) ) self.assertTrue(hasattr(snake_case , "num_text" ) ) self.assertTrue(hasattr(snake_case , "repo_path" ) ) self.assertTrue(hasattr(snake_case , "metadata" ) ) self.assertTrue(hasattr(snake_case , "do_reduce_labels" ) ) def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' pass def SCREAMING_SNAKE_CASE ( self :int ): '''simple docstring''' A_ : Dict = self.image_processing_class(**self.image_processor_dict ) # create random PIL images A_ : Optional[Any] = prepare_image_inputs(self.image_processing_tester , equal_resolution=snake_case ) for image in image_inputs: self.assertIsInstance(snake_case , Image.Image ) # Test not batched input A_ : str = image_processor(image_inputs[0] , ["semantic"] , return_tensors="pt" ).pixel_values A_ , A_ : str = self.image_processing_tester.get_expected_values(snake_case ) self.assertEqual( encoded_images.shape , (1, self.image_processing_tester.num_channels, expected_height, expected_width) , ) # Test batched A_ , A_ : Optional[Any] = self.image_processing_tester.get_expected_values(snake_case , batched=snake_case ) A_ : List[str] = image_processor( snake_case , ["semantic"] * len(snake_case ) , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processing_tester.batch_size, self.image_processing_tester.num_channels, expected_height, expected_width, ) , ) def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' A_ : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors A_ : List[str] = prepare_image_inputs(self.image_processing_tester , equal_resolution=snake_case , numpify=snake_case ) for image in image_inputs: self.assertIsInstance(snake_case , np.ndarray ) # Test not batched input A_ : List[str] = image_processor(image_inputs[0] , ["semantic"] , return_tensors="pt" ).pixel_values A_ , A_ : List[str] = self.image_processing_tester.get_expected_values(snake_case ) self.assertEqual( encoded_images.shape , (1, self.image_processing_tester.num_channels, expected_height, expected_width) , ) # Test batched A_ , A_ : int = self.image_processing_tester.get_expected_values(snake_case , batched=snake_case ) A_ : Optional[Any] = image_processor( snake_case , ["semantic"] * len(snake_case ) , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processing_tester.batch_size, self.image_processing_tester.num_channels, expected_height, expected_width, ) , ) def SCREAMING_SNAKE_CASE ( self :Optional[int] ): '''simple docstring''' A_ : List[str] = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors A_ : List[str] = prepare_image_inputs(self.image_processing_tester , equal_resolution=snake_case , torchify=snake_case ) for image in image_inputs: self.assertIsInstance(snake_case , torch.Tensor ) # Test not batched input A_ : Any = image_processor(image_inputs[0] , ["semantic"] , return_tensors="pt" ).pixel_values A_ , A_ : Tuple = self.image_processing_tester.get_expected_values(snake_case ) self.assertEqual( encoded_images.shape , (1, self.image_processing_tester.num_channels, expected_height, expected_width) , ) # Test batched A_ , A_ : Tuple = self.image_processing_tester.get_expected_values(snake_case , batched=snake_case ) A_ : Any = image_processor( snake_case , ["semantic"] * len(snake_case ) , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processing_tester.batch_size, self.image_processing_tester.num_channels, expected_height, expected_width, ) , ) def SCREAMING_SNAKE_CASE ( self :Optional[Any] , snake_case :Dict=False , snake_case :str=False , snake_case :Dict="np" ): '''simple docstring''' A_ : Tuple = self.image_processing_class(**self.image_processor_dict ) # prepare image and target A_ : Tuple = self.image_processing_tester.num_labels A_ : str = None A_ : Tuple = None A_ : Tuple = prepare_image_inputs(self.image_processing_tester , equal_resolution=snake_case ) if with_segmentation_maps: A_ : List[str] = num_labels if is_instance_map: A_ : List[str] = list(range(snake_case ) ) * 2 A_ : int = dict(enumerate(snake_case ) ) A_ : List[str] = [ np.random.randint(0 , high * 2 , (img.size[1], img.size[0]) ).astype(np.uinta ) for img in image_inputs ] if segmentation_type == "pil": A_ : int = [Image.fromarray(snake_case ) for annotation in annotations] A_ : List[str] = image_processor( snake_case , ["semantic"] * len(snake_case ) , snake_case , return_tensors="pt" , instance_id_to_semantic_id=snake_case , pad_and_return_pixel_mask=snake_case , ) return inputs def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' pass def SCREAMING_SNAKE_CASE ( self :Optional[int] ): '''simple docstring''' def common(snake_case :Dict=False , snake_case :Optional[int]=None ): A_ : Tuple = self.comm_get_image_processor_inputs( with_segmentation_maps=snake_case , is_instance_map=snake_case , segmentation_type=snake_case ) A_ : Optional[Any] = inputs["mask_labels"] A_ : List[Any] = inputs["class_labels"] A_ : Optional[Any] = inputs["pixel_values"] A_ : int = inputs["text_inputs"] # check the batch_size for mask_label, class_label, text_input in zip(snake_case , snake_case , snake_case ): self.assertEqual(mask_label.shape[0] , class_label.shape[0] ) # this ensure padding has happened self.assertEqual(mask_label.shape[1:] , pixel_values.shape[2:] ) self.assertEqual(len(snake_case ) , self.image_processing_tester.num_text ) common() common(is_instance_map=snake_case ) common(is_instance_map=snake_case , segmentation_type="pil" ) common(is_instance_map=snake_case , segmentation_type="pil" ) def SCREAMING_SNAKE_CASE ( self :Optional[Any] ): '''simple docstring''' A_ : Any = np.zeros((20, 50) ) A_ : List[str] = 1 A_ : int = 1 A_ : Optional[Any] = 1 A_ : Any = binary_mask_to_rle(snake_case ) self.assertEqual(len(snake_case ) , 4 ) self.assertEqual(rle[0] , 21 ) self.assertEqual(rle[1] , 45 ) def SCREAMING_SNAKE_CASE ( self :Optional[int] ): '''simple docstring''' A_ : Union[str, Any] = self.image_processing_class( num_labels=self.image_processing_tester.num_classes , max_seq_length=77 , task_seq_length=77 , class_info_file="ade20k_panoptic.json" , num_text=self.image_processing_tester.num_text , repo_path="shi-labs/oneformer_demo" , ) A_ : Any = self.image_processing_tester.get_fake_oneformer_outputs() A_ : int = fature_extractor.post_process_semantic_segmentation(snake_case ) self.assertEqual(len(snake_case ) , self.image_processing_tester.batch_size ) self.assertEqual( segmentation[0].shape , ( self.image_processing_tester.height, self.image_processing_tester.width, ) , ) A_ : Optional[int] = [(1, 4) for i in range(self.image_processing_tester.batch_size )] A_ : List[Any] = fature_extractor.post_process_semantic_segmentation(snake_case , target_sizes=snake_case ) self.assertEqual(segmentation[0].shape , target_sizes[0] ) def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' A_ : List[str] = self.image_processing_class( num_labels=self.image_processing_tester.num_classes , max_seq_length=77 , task_seq_length=77 , class_info_file="ade20k_panoptic.json" , num_text=self.image_processing_tester.num_text , repo_path="shi-labs/oneformer_demo" , ) A_ : str = self.image_processing_tester.get_fake_oneformer_outputs() A_ : Optional[Any] = image_processor.post_process_instance_segmentation(snake_case , threshold=0 ) self.assertTrue(len(snake_case ) == self.image_processing_tester.batch_size ) for el in segmentation: self.assertTrue("segmentation" in el ) self.assertTrue("segments_info" in el ) self.assertEqual(type(el["segments_info"] ) , snake_case ) self.assertEqual( el["segmentation"].shape , (self.image_processing_tester.height, self.image_processing_tester.width) ) def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' A_ : Tuple = self.image_processing_class( num_labels=self.image_processing_tester.num_classes , max_seq_length=77 , task_seq_length=77 , class_info_file="ade20k_panoptic.json" , num_text=self.image_processing_tester.num_text , repo_path="shi-labs/oneformer_demo" , ) A_ : List[Any] = self.image_processing_tester.get_fake_oneformer_outputs() A_ : Optional[Any] = image_processor.post_process_panoptic_segmentation(snake_case , threshold=0 ) self.assertTrue(len(snake_case ) == self.image_processing_tester.batch_size ) for el in segmentation: self.assertTrue("segmentation" in el ) self.assertTrue("segments_info" in el ) self.assertEqual(type(el["segments_info"] ) , snake_case ) self.assertEqual( el["segmentation"].shape , (self.image_processing_tester.height, self.image_processing_tester.width) )
300
0
'''simple docstring''' from ....configuration_utils import PretrainedConfig from ....utils import logging lowercase_ = logging.get_logger(__name__) # TODO: upload to AWS lowercase_ = { """yjernite/retribert-base-uncased""": ( """https://huggingface.co/yjernite/retribert-base-uncased/resolve/main/config.json""" ), } class a_ ( snake_case_ ): '''simple docstring''' UpperCamelCase = '''retribert''' def __init__( self , A=3_0522 , A=768 , A=8 , A=12 , A=3072 , A="gelu" , A=0.1 , A=0.1 , A=512 , A=2 , A=0.02 , A=1e-12 , A=True , A=128 , A=0 , **A , ) -> Union[str, Any]: super().__init__(pad_token_id=A , **A ) _SCREAMING_SNAKE_CASE = vocab_size _SCREAMING_SNAKE_CASE = hidden_size _SCREAMING_SNAKE_CASE = num_hidden_layers _SCREAMING_SNAKE_CASE = num_attention_heads _SCREAMING_SNAKE_CASE = hidden_act _SCREAMING_SNAKE_CASE = intermediate_size _SCREAMING_SNAKE_CASE = hidden_dropout_prob _SCREAMING_SNAKE_CASE = attention_probs_dropout_prob _SCREAMING_SNAKE_CASE = max_position_embeddings _SCREAMING_SNAKE_CASE = type_vocab_size _SCREAMING_SNAKE_CASE = initializer_range _SCREAMING_SNAKE_CASE = layer_norm_eps _SCREAMING_SNAKE_CASE = share_encoders _SCREAMING_SNAKE_CASE = projection_dim
58
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 _lowerCAmelCase : List[str] = logging.get_logger(__name__) _lowerCAmelCase : Optional[Any] = { '''facebook/data2vec-vision-base-ft''': ( '''https://huggingface.co/facebook/data2vec-vision-base-ft/resolve/main/config.json''' ), } class __magic_name__ ( lowerCamelCase__ ): """simple docstring""" __UpperCamelCase = '''data2vec-vision''' def __init__( self :int , snake_case :Optional[int]=768 , snake_case :Any=12 , snake_case :Any=12 , snake_case :Tuple=3_072 , snake_case :Any="gelu" , snake_case :Tuple=0.0 , snake_case :int=0.0 , snake_case :Any=0.02 , snake_case :str=1e-12 , snake_case :List[str]=224 , snake_case :Dict=16 , snake_case :int=3 , snake_case :int=False , snake_case :str=False , snake_case :List[Any]=False , snake_case :Optional[Any]=False , snake_case :Tuple=0.1 , snake_case :Optional[Any]=0.1 , snake_case :Any=True , snake_case :Optional[Any]=[3, 5, 7, 11] , snake_case :Dict=[1, 2, 3, 6] , snake_case :int=True , snake_case :List[Any]=0.4 , snake_case :Any=256 , snake_case :Union[str, Any]=1 , snake_case :Union[str, Any]=False , snake_case :Any=255 , **snake_case :int , ): '''simple docstring''' super().__init__(**snake_case ) A_ : Dict = hidden_size A_ : Tuple = num_hidden_layers A_ : List[str] = num_attention_heads A_ : Any = intermediate_size A_ : Optional[Any] = hidden_act A_ : Any = hidden_dropout_prob A_ : List[str] = attention_probs_dropout_prob A_ : Optional[Any] = initializer_range A_ : List[str] = layer_norm_eps A_ : str = image_size A_ : Optional[int] = patch_size A_ : int = num_channels A_ : Optional[Any] = use_mask_token A_ : Optional[Any] = use_absolute_position_embeddings A_ : Optional[int] = use_relative_position_bias A_ : Dict = use_shared_relative_position_bias A_ : Any = layer_scale_init_value A_ : Optional[Any] = drop_path_rate A_ : Dict = use_mean_pooling # decode head attributes (semantic segmentation) A_ : Tuple = out_indices A_ : Optional[Any] = pool_scales # auxiliary head attributes (semantic segmentation) A_ : str = use_auxiliary_head A_ : List[Any] = auxiliary_loss_weight A_ : List[str] = auxiliary_channels A_ : Dict = auxiliary_num_convs A_ : List[str] = auxiliary_concat_input A_ : Optional[int] = semantic_loss_ignore_index class __magic_name__ ( lowerCamelCase__ ): """simple docstring""" __UpperCamelCase = version.parse('''1.11''' ) @property def SCREAMING_SNAKE_CASE ( self :Union[str, Any] ): '''simple docstring''' return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ] ) @property def SCREAMING_SNAKE_CASE ( self :Tuple ): '''simple docstring''' return 1e-4
300
0
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available, is_vision_available, ) __lowerCamelCase = { """configuration_perceiver""": ["""PERCEIVER_PRETRAINED_CONFIG_ARCHIVE_MAP""", """PerceiverConfig""", """PerceiverOnnxConfig"""], """tokenization_perceiver""": ["""PerceiverTokenizer"""], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCamelCase = ["""PerceiverFeatureExtractor"""] __lowerCamelCase = ["""PerceiverImageProcessor"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCamelCase = [ """PERCEIVER_PRETRAINED_MODEL_ARCHIVE_LIST""", """PerceiverForImageClassificationConvProcessing""", """PerceiverForImageClassificationFourier""", """PerceiverForImageClassificationLearned""", """PerceiverForMaskedLM""", """PerceiverForMultimodalAutoencoding""", """PerceiverForOpticalFlow""", """PerceiverForSequenceClassification""", """PerceiverLayer""", """PerceiverModel""", """PerceiverPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_perceiver import PERCEIVER_PRETRAINED_CONFIG_ARCHIVE_MAP, PerceiverConfig, PerceiverOnnxConfig from .tokenization_perceiver import PerceiverTokenizer try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_perceiver import PerceiverFeatureExtractor from .image_processing_perceiver import PerceiverImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_perceiver import ( PERCEIVER_PRETRAINED_MODEL_ARCHIVE_LIST, PerceiverForImageClassificationConvProcessing, PerceiverForImageClassificationFourier, PerceiverForImageClassificationLearned, PerceiverForMaskedLM, PerceiverForMultimodalAutoencoding, PerceiverForOpticalFlow, PerceiverForSequenceClassification, PerceiverLayer, PerceiverModel, PerceiverPreTrainedModel, ) else: import sys __lowerCamelCase = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
59
from typing import List, Optional, Union import numpy as np from ....audio_utils import mel_filter_bank, optimal_fft_length, spectrogram, window_function from ....feature_extraction_sequence_utils import SequenceFeatureExtractor from ....feature_extraction_utils import BatchFeature from ....file_utils import PaddingStrategy, TensorType from ....utils import logging _lowerCAmelCase : str = logging.get_logger(__name__) class __magic_name__ ( lowerCamelCase__ ): """simple docstring""" __UpperCamelCase = ['''input_features''', '''attention_mask'''] def __init__( self :int , snake_case :int=80 , snake_case :Optional[int]=16_000 , snake_case :Tuple=0.0 , snake_case :Optional[int]=10 , snake_case :Optional[Any]=25 , snake_case :Dict="hamming_window" , snake_case :Tuple=32768.0 , snake_case :str=0.97 , snake_case :List[str]=1.0 , snake_case :Dict=True , snake_case :str=True , snake_case :Optional[Any]=False , **snake_case :Union[str, Any] , ): '''simple docstring''' super().__init__(feature_size=snake_case , sampling_rate=snake_case , padding_value=snake_case , **snake_case ) A_ : Union[str, Any] = feature_size A_ : int = sampling_rate A_ : str = padding_value A_ : int = hop_length A_ : List[str] = win_length A_ : Any = frame_signal_scale A_ : str = preemphasis_coeff A_ : List[str] = mel_floor A_ : str = normalize_means A_ : Any = normalize_vars A_ : Optional[Any] = win_function A_ : Dict = return_attention_mask A_ : List[str] = win_length * sampling_rate // 1_000 A_ : List[str] = hop_length * sampling_rate // 1_000 A_ : List[str] = optimal_fft_length(self.sample_size ) A_ : str = (self.n_fft // 2) + 1 def SCREAMING_SNAKE_CASE ( self :Any , snake_case :np.array ): '''simple docstring''' if self.win_function == "hamming_window": A_ : Dict = window_function(window_length=self.sample_size , name=self.win_function , periodic=snake_case ) else: A_ : List[str] = window_function(window_length=self.sample_size , name=self.win_function ) A_ : Optional[int] = mel_filter_bank( num_frequency_bins=self.n_freqs , num_mel_filters=self.feature_size , min_frequency=0.0 , max_frequency=self.sampling_rate / 2.0 , sampling_rate=self.sampling_rate , ) A_ : Tuple = spectrogram( one_waveform * self.frame_signal_scale , window=snake_case , frame_length=self.sample_size , hop_length=self.sample_stride , fft_length=self.n_fft , center=snake_case , preemphasis=self.preemphasis_coeff , mel_filters=snake_case , mel_floor=self.mel_floor , log_mel="log" , ) return msfc_features.T def SCREAMING_SNAKE_CASE ( self :int , snake_case :Any , snake_case :Union[str, Any] , snake_case :str ): '''simple docstring''' if self.normalize_means: A_ : int = x[:input_length].mean(axis=0 ) A_ : Any = np.subtract(snake_case , snake_case ) if self.normalize_vars: A_ : List[Any] = x[:input_length].std(axis=0 ) A_ : Optional[int] = np.divide(snake_case , snake_case ) if input_length < x.shape[0]: A_ : Optional[int] = padding_value # make sure array is in float32 A_ : Union[str, Any] = x.astype(np.floataa ) return x def SCREAMING_SNAKE_CASE ( self :int , snake_case :List[np.ndarray] , snake_case :Optional[np.ndarray] = None ): '''simple docstring''' A_ : str = attention_mask.sum(-1 ) if attention_mask is not None else [x.shape[0] for x in input_features] return [self._normalize_one(snake_case , snake_case , self.padding_value ) for x, n in zip(snake_case , snake_case )] def __call__( self :int , snake_case :Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] , snake_case :Union[bool, str, PaddingStrategy] = False , snake_case :Optional[int] = None , snake_case :bool = False , snake_case :Optional[int] = None , snake_case :Optional[bool] = None , snake_case :Optional[Union[str, TensorType]] = None , snake_case :Optional[int] = None , **snake_case :Dict , ): '''simple docstring''' if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( f"The model corresponding to this feature extractor: {self} was trained using a sampling rate of" f" {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled with" f" {self.sampling_rate} and not {sampling_rate}." ) else: logger.warning( "It is strongly recommended to pass the ``sampling_rate`` argument to this function. " "Failing to do so can result in silent errors that might be hard to debug." ) A_ : Optional[int] = isinstance(snake_case , np.ndarray ) and len(raw_speech.shape ) > 1 if is_batched_numpy and len(raw_speech.shape ) > 2: raise ValueError(f"Only mono-channel audio is supported for input to {self}" ) A_ : Optional[Any] = is_batched_numpy or ( isinstance(snake_case , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: A_ : List[Any] = [np.asarray(snake_case , dtype=np.floataa ) for speech in raw_speech] elif not is_batched and not isinstance(snake_case , np.ndarray ): A_ : int = np.asarray(snake_case , dtype=np.floataa ) elif isinstance(snake_case , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): A_ : Optional[int] = raw_speech.astype(np.floataa ) # always return batch if not is_batched: A_ : Tuple = [raw_speech] # extract fbank features A_ : int = [self._extract_mfsc_features(snake_case ) for one_waveform in raw_speech] # convert into correct format for padding A_ : Union[str, Any] = BatchFeature({"input_features": features} ) A_ : str = self.pad( snake_case , padding=snake_case , max_length=snake_case , truncation=snake_case , pad_to_multiple_of=snake_case , return_attention_mask=snake_case , **snake_case , ) # make sure list is in array format A_ : Optional[int] = padded_inputs.get("input_features" ) if isinstance(input_features[0] , snake_case ): A_ : Union[str, Any] = [np.asarray(snake_case , dtype=np.floataa ) for feature in input_features] A_ : Dict = padded_inputs.get("attention_mask" ) if attention_mask is not None: A_ : Any = [np.asarray(snake_case , dtype=np.intaa ) for array in attention_mask] if self.normalize_means or self.normalize_vars: A_ : Dict = ( np.array(snake_case , dtype=np.intaa ) if self._get_padding_strategies(snake_case , max_length=snake_case ) is not PaddingStrategy.DO_NOT_PAD and padding else None ) A_ : Optional[int] = self.normalize( padded_inputs["input_features"] , attention_mask=snake_case ) if return_tensors is not None: A_ : Dict = padded_inputs.convert_to_tensors(snake_case ) return padded_inputs
300
0
"""simple docstring""" import logging import os from typing import Dict, List, Optional, Union import torch import torch.nn as nn from accelerate.utils.imports import ( is_abit_bnb_available, is_abit_bnb_available, is_bnb_available, ) from ..big_modeling import dispatch_model, init_empty_weights from .dataclasses import BnbQuantizationConfig from .modeling import ( find_tied_parameters, get_balanced_memory, infer_auto_device_map, load_checkpoint_in_model, offload_weight, set_module_tensor_to_device, ) if is_bnb_available(): import bitsandbytes as bnb from copy import deepcopy snake_case__ : Dict = logging.getLogger(__name__) def _snake_case ( _snake_case : torch.nn.Module , _snake_case : BnbQuantizationConfig , _snake_case : Union[str, os.PathLike] = None , _snake_case : Optional[Dict[str, Union[int, str, torch.device]]] = None , _snake_case : Optional[List[str]] = None , _snake_case : Optional[Dict[Union[int, str], Union[int, str]]] = None , _snake_case : Optional[Union[str, os.PathLike]] = None , _snake_case : bool = False , ): lowerCAmelCase : Any = bnb_quantization_config.load_in_abit lowerCAmelCase : List[str] = bnb_quantization_config.load_in_abit if load_in_abit and not is_abit_bnb_available(): raise ImportError( '''You have a version of `bitsandbytes` that is not compatible with 8bit quantization,''' ''' make sure you have the latest version of `bitsandbytes` installed.''' ) if load_in_abit and not is_abit_bnb_available(): raise ValueError( '''You have a version of `bitsandbytes` that is not compatible with 4bit quantization,''' '''make sure you have the latest version of `bitsandbytes` installed.''' ) lowerCAmelCase : Dict = [] # custom device map if isinstance(_snake_case , _snake_case ) and len(device_map.keys() ) > 1: lowerCAmelCase : Any = [key for key, value in device_map.items() if value in ['''disk''', '''cpu''']] # We keep some modules such as the lm_head in their original dtype for numerical stability reasons if bnb_quantization_config.skip_modules is None: lowerCAmelCase : List[Any] = get_keys_to_not_convert(_snake_case ) # add cpu modules to skip modules only for 4-bit modules if load_in_abit: bnb_quantization_config.skip_modules.extend(_snake_case ) lowerCAmelCase : str = bnb_quantization_config.skip_modules # We add the modules we want to keep in full precision if bnb_quantization_config.keep_in_fpaa_modules is None: lowerCAmelCase : Optional[Any] = [] lowerCAmelCase : int = bnb_quantization_config.keep_in_fpaa_modules modules_to_not_convert.extend(_snake_case ) # compatibility with peft lowerCAmelCase : str = load_in_abit lowerCAmelCase : str = load_in_abit lowerCAmelCase : str = get_parameter_device(_snake_case ) if model_device.type != "meta": # quantization of an already loaded model logger.warning( '''It is not recommended to quantize a loaded model. ''' '''The model should be instantiated under the `init_empty_weights` context manager.''' ) lowerCAmelCase : Optional[int] = replace_with_bnb_layers(_snake_case , _snake_case , modules_to_not_convert=_snake_case ) # convert param to the right dtype lowerCAmelCase : Any = bnb_quantization_config.torch_dtype for name, param in model.state_dict().items(): if any(module_to_keep_in_fpaa in name for module_to_keep_in_fpaa in keep_in_fpaa_modules ): param.to(torch.floataa ) if param.dtype != torch.floataa: lowerCAmelCase : Any = name.replace('''.weight''' , '''''' ).replace('''.bias''' , '''''' ) lowerCAmelCase : Optional[Any] = getattr(_snake_case , _snake_case , _snake_case ) if param is not None: param.to(torch.floataa ) elif torch.is_floating_point(_snake_case ): param.to(_snake_case ) if model_device.type == "cuda": # move everything to cpu in the first place because we can't do quantization if the weights are already on cuda model.cuda(torch.cuda.current_device() ) torch.cuda.empty_cache() elif torch.cuda.is_available(): model.to(torch.cuda.current_device() ) else: raise RuntimeError('''No GPU found. A GPU is needed for quantization.''' ) logger.info( f'''The model device type is {model_device.type}. However, cuda is needed for quantization.''' '''We move the model to cuda.''' ) return model elif weights_location is None: raise RuntimeError( f'''`weights_location` needs to be the folder path containing the weights of the model, but we found {weights_location} ''' ) else: with init_empty_weights(): lowerCAmelCase : List[str] = replace_with_bnb_layers( _snake_case , _snake_case , modules_to_not_convert=_snake_case ) lowerCAmelCase : List[str] = get_quantized_model_device_map( _snake_case , _snake_case , _snake_case , max_memory=_snake_case , no_split_module_classes=_snake_case , ) if offload_state_dict is None and device_map is not None and "disk" in device_map.values(): lowerCAmelCase : List[Any] = True lowerCAmelCase : List[str] = any(x in list(device_map.values() ) for x in ['''cpu''', '''disk'''] ) load_checkpoint_in_model( _snake_case , _snake_case , _snake_case , dtype=bnb_quantization_config.torch_dtype , offload_folder=_snake_case , offload_state_dict=_snake_case , keep_in_fpaa_modules=bnb_quantization_config.keep_in_fpaa_modules , offload_abit_bnb=load_in_abit and offload , ) return dispatch_model(_snake_case , device_map=_snake_case , offload_dir=_snake_case ) def _snake_case ( _snake_case : str , _snake_case : List[str] , _snake_case : Tuple=None , _snake_case : Any=None , _snake_case : Dict=None ): if device_map is None: if torch.cuda.is_available(): lowerCAmelCase : int = {'''''': torch.cuda.current_device()} else: raise RuntimeError('''No GPU found. A GPU is needed for quantization.''' ) logger.info('''The device_map was not initialized.''' '''Setting device_map to `{\'\':torch.cuda.current_device()}`.''' ) if isinstance(_snake_case , _snake_case ): if device_map not in ["auto", "balanced", "balanced_low_0", "sequential"]: raise ValueError( '''If passing a string for `device_map`, please choose \'auto\', \'balanced\', \'balanced_low_0\' or ''' '''\'sequential\'.''' ) lowerCAmelCase : int = {} special_dtypes.update( { name: bnb_quantization_config.torch_dtype for name, _ in model.named_parameters() if any(m in name for m in bnb_quantization_config.skip_modules ) } ) special_dtypes.update( { name: torch.floataa for name, _ in model.named_parameters() if any(m in name for m in bnb_quantization_config.keep_in_fpaa_modules ) } ) lowerCAmelCase : Tuple = {} lowerCAmelCase : List[Any] = special_dtypes lowerCAmelCase : List[str] = no_split_module_classes lowerCAmelCase : Optional[int] = bnb_quantization_config.target_dtype # get max_memory for each device. if device_map != "sequential": lowerCAmelCase : str = get_balanced_memory( _snake_case , low_zero=(device_map == '''balanced_low_0''') , max_memory=_snake_case , **_snake_case , ) lowerCAmelCase : Tuple = max_memory lowerCAmelCase : int = infer_auto_device_map(_snake_case , **_snake_case ) if isinstance(_snake_case , _snake_case ): # check if don't have any quantized module on the cpu lowerCAmelCase : Dict = bnb_quantization_config.skip_modules + bnb_quantization_config.keep_in_fpaa_modules lowerCAmelCase : Tuple = { key: device_map[key] for key in device_map.keys() if key not in modules_not_to_convert } for device in ["cpu", "disk"]: if device in device_map_without_some_modules.values(): if bnb_quantization_config.load_in_abit: raise ValueError( ''' Some modules are dispatched on the CPU or the disk. Make sure you have enough GPU RAM to fit the quantized model. If you want to dispatch the model on the CPU or the disk while keeping these modules in `torch_dtype`, you need to pass a custom `device_map` to `load_and_quantize_model`. Check https://huggingface.co/docs/accelerate/main/en/usage_guides/quantization#offload-modules-to-cpu-and-disk for more details. ''' ) else: logger.info( '''Some modules are are offloaded to the CPU or the disk. Note that these modules will be converted to 8-bit''' ) del device_map_without_some_modules return device_map def _snake_case ( _snake_case : Tuple , _snake_case : List[Any] , _snake_case : int=None , _snake_case : Any=None ): if modules_to_not_convert is None: lowerCAmelCase : str = [] lowerCAmelCase, lowerCAmelCase : List[str] = _replace_with_bnb_layers( _snake_case , _snake_case , _snake_case , _snake_case ) if not has_been_replaced: logger.warning( '''You are loading your model in 8bit or 4bit but no linear modules were found in your model.''' ''' this can happen for some architectures such as gpt2 that uses Conv1D instead of Linear layers.''' ''' Please double check your model architecture, or submit an issue on github if you think this is''' ''' a bug.''' ) return model def _snake_case ( _snake_case : Dict , _snake_case : Optional[Any] , _snake_case : List[Any]=None , _snake_case : Dict=None , ): lowerCAmelCase : List[str] = False for name, module in model.named_children(): if current_key_name is None: lowerCAmelCase : List[Any] = [] current_key_name.append(_snake_case ) if isinstance(_snake_case , nn.Linear ) and name not in modules_to_not_convert: # Check if the current key is not in the `modules_to_not_convert` lowerCAmelCase : Optional[int] = '''.'''.join(_snake_case ) lowerCAmelCase : Optional[int] = True for key in modules_to_not_convert: if ( (key in current_key_name_str) and (key + "." in current_key_name_str) ) or key == current_key_name_str: lowerCAmelCase : List[str] = False break if proceed: # Load bnb module with empty weight and replace ``nn.Linear` module if bnb_quantization_config.load_in_abit: lowerCAmelCase : Any = bnb.nn.LinearabitLt( module.in_features , module.out_features , module.bias is not None , has_fpaa_weights=_snake_case , threshold=bnb_quantization_config.llm_inta_threshold , ) elif bnb_quantization_config.load_in_abit: lowerCAmelCase : Dict = bnb.nn.Linearabit( module.in_features , module.out_features , module.bias is not None , bnb_quantization_config.bnb_abit_compute_dtype , compress_statistics=bnb_quantization_config.bnb_abit_use_double_quant , quant_type=bnb_quantization_config.bnb_abit_quant_type , ) else: raise ValueError('''load_in_8bit and load_in_4bit can\'t be both False''' ) lowerCAmelCase : Dict = module.weight.data if module.bias is not None: lowerCAmelCase : Union[str, Any] = module.bias.data bnb_module.requires_grad_(_snake_case ) setattr(_snake_case , _snake_case , _snake_case ) lowerCAmelCase : Any = True if len(list(module.children() ) ) > 0: lowerCAmelCase, lowerCAmelCase : Any = _replace_with_bnb_layers( _snake_case , _snake_case , _snake_case , _snake_case ) lowerCAmelCase : Optional[Any] = has_been_replaced | _has_been_replaced # Remove the last key for recursion current_key_name.pop(-1 ) return model, has_been_replaced def _snake_case ( _snake_case : List[str] ): # Create a copy of the model with init_empty_weights(): lowerCAmelCase : str = deepcopy(_snake_case ) # this has 0 cost since it is done inside `init_empty_weights` context manager` lowerCAmelCase : List[Any] = find_tied_parameters(_snake_case ) # For compatibility with Accelerate < 0.18 if isinstance(_snake_case , _snake_case ): lowerCAmelCase : List[Any] = sum(list(tied_params.values() ) , [] ) + list(tied_params.keys() ) else: lowerCAmelCase : Tuple = sum(_snake_case , [] ) lowerCAmelCase : Optional[Any] = len(_snake_case ) > 0 # Check if it is a base model lowerCAmelCase : Union[str, Any] = False if hasattr(_snake_case , '''base_model_prefix''' ): lowerCAmelCase : int = not hasattr(_snake_case , model.base_model_prefix ) # Ignore this for base models (BertModel, GPT2Model, etc.) if (not has_tied_params) and is_base_model: return [] # otherwise they have an attached head lowerCAmelCase : Union[str, Any] = list(model.named_children() ) lowerCAmelCase : Dict = [list_modules[-1][0]] # add last module together with tied weights lowerCAmelCase : Optional[Any] = set(_snake_case ) - set(_snake_case ) lowerCAmelCase : Dict = list(set(_snake_case ) ) + list(_snake_case ) # remove ".weight" from the keys lowerCAmelCase : Dict = ['''.weight''', '''.bias'''] lowerCAmelCase : List[str] = [] for name in list_untouched: for name_to_remove in names_to_remove: if name_to_remove in name: lowerCAmelCase : Optional[int] = name.replace(_snake_case , '''''' ) filtered_module_names.append(_snake_case ) return filtered_module_names def _snake_case ( _snake_case : Any ): for m in model.modules(): if isinstance(_snake_case , bnb.nn.Linearabit ): return True return False def _snake_case ( _snake_case : nn.Module ): return next(parameter.parameters() ).device def _snake_case ( _snake_case : Dict , _snake_case : str , _snake_case : List[str] , _snake_case : List[str] , _snake_case : List[str] , _snake_case : str , _snake_case : Tuple ): # if it is not quantized, we quantize and offload the quantized weights and the SCB stats if fpaa_statistics is None: set_module_tensor_to_device(_snake_case , _snake_case , 0 , dtype=_snake_case , value=_snake_case ) lowerCAmelCase : List[str] = param_name lowerCAmelCase : Union[str, Any] = model if "." in tensor_name: lowerCAmelCase : int = tensor_name.split('''.''' ) for split in splits[:-1]: lowerCAmelCase : str = getattr(_snake_case , _snake_case ) if new_module is None: raise ValueError(f'''{module} has no attribute {split}.''' ) lowerCAmelCase : List[str] = new_module lowerCAmelCase : int = splits[-1] # offload weights lowerCAmelCase : str = False offload_weight(module._parameters[tensor_name] , _snake_case , _snake_case , index=_snake_case ) if hasattr(module._parameters[tensor_name] , '''SCB''' ): offload_weight( module._parameters[tensor_name].SCB , param_name.replace('''weight''' , '''SCB''' ) , _snake_case , index=_snake_case , ) else: offload_weight(_snake_case , _snake_case , _snake_case , index=_snake_case ) offload_weight(_snake_case , param_name.replace('''weight''' , '''SCB''' ) , _snake_case , index=_snake_case ) set_module_tensor_to_device(_snake_case , _snake_case , '''meta''' , dtype=_snake_case , value=torch.empty(*param.size() ) )
60
from typing import Optional import numpy as np import torch from torch import nn from transformers import GPTaConfig, GPTaLMHeadModel from transformers.modeling_utils import ModuleUtilsMixin from ...configuration_utils import ConfigMixin, register_to_config from ...models import ModelMixin class __magic_name__ ( lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ): """simple docstring""" __UpperCamelCase = [r'''h\.\d+\.attn\.bias''', r'''h\.\d+\.attn\.masked_bias'''] @register_to_config def __init__( self :List[Any] , snake_case :int , snake_case :int , snake_case :Optional[int] = None , snake_case :int = 50_257 , snake_case :int = 1_024 , snake_case :int = 768 , snake_case :int = 12 , snake_case :int = 12 , snake_case :Optional[int] = None , snake_case :str = "gelu_new" , snake_case :float = 0.1 , snake_case :float = 0.1 , snake_case :float = 0.1 , snake_case :float = 1e-5 , snake_case :float = 0.02 , snake_case :bool = True , snake_case :bool = True , snake_case :bool = False , snake_case :bool = False , ): '''simple docstring''' super().__init__() A_ : Tuple = prefix_length if prefix_inner_dim != n_embd and prefix_hidden_dim is None: raise ValueError( f"`prefix_hidden_dim` cannot be `None` when `prefix_inner_dim`: {prefix_hidden_dim} and" f" `n_embd`: {n_embd} are not equal." ) A_ : List[Any] = prefix_inner_dim A_ : Union[str, Any] = prefix_hidden_dim A_ : List[str] = ( nn.Linear(self.prefix_inner_dim , self.prefix_hidden_dim ) if self.prefix_hidden_dim is not None else nn.Identity() ) A_ : List[Any] = ( nn.Linear(self.prefix_hidden_dim , snake_case ) if self.prefix_hidden_dim is not None else nn.Identity() ) A_ : List[Any] = GPTaConfig( vocab_size=snake_case , n_positions=snake_case , n_embd=snake_case , n_layer=snake_case , n_head=snake_case , n_inner=snake_case , activation_function=snake_case , resid_pdrop=snake_case , embd_pdrop=snake_case , attn_pdrop=snake_case , layer_norm_epsilon=snake_case , initializer_range=snake_case , scale_attn_weights=snake_case , use_cache=snake_case , scale_attn_by_inverse_layer_idx=snake_case , reorder_and_upcast_attn=snake_case , ) A_ : Optional[Any] = GPTaLMHeadModel(snake_case ) def SCREAMING_SNAKE_CASE ( self :Tuple , snake_case :torch.Tensor , snake_case :torch.Tensor , snake_case :Optional[torch.Tensor] = None , snake_case :Optional[torch.Tensor] = None , ): '''simple docstring''' A_ : Any = self.transformer.transformer.wte(snake_case ) A_ : str = self.encode_prefix(snake_case ) A_ : Union[str, Any] = self.decode_prefix(snake_case ) A_ : int = torch.cat((prefix_embeds, embedding_text) , dim=1 ) if labels is not None: A_ : Dict = self.get_dummy_token(input_ids.shape[0] , input_ids.device ) A_ : int = torch.cat((dummy_token, input_ids) , dim=1 ) A_ : Union[str, Any] = self.transformer(inputs_embeds=snake_case , labels=snake_case , attention_mask=snake_case ) if self.prefix_hidden_dim is not None: return out, hidden else: return out def SCREAMING_SNAKE_CASE ( self :str , snake_case :int , snake_case :torch.device ): '''simple docstring''' return torch.zeros(snake_case , self.prefix_length , dtype=torch.intaa , device=snake_case ) def SCREAMING_SNAKE_CASE ( self :Optional[int] , snake_case :int ): '''simple docstring''' return self.encode_prefix(snake_case ) @torch.no_grad() def SCREAMING_SNAKE_CASE ( self :List[Any] , snake_case :Dict , snake_case :Optional[int] , snake_case :Any ): '''simple docstring''' A_ : Any = torch.split(snake_case , 1 , dim=0 ) A_ : Optional[int] = [] A_ : Union[str, Any] = [] for feature in features: A_ : Tuple = self.decode_prefix(feature.to(snake_case ) ) # back to the clip feature # Only support beam search for now A_ , A_ : Dict = self.generate_beam( input_embeds=snake_case , device=snake_case , eos_token_id=snake_case ) generated_tokens.append(output_tokens[0] ) generated_seq_lengths.append(seq_lengths[0] ) A_ : int = torch.stack(snake_case ) A_ : int = torch.stack(snake_case ) return generated_tokens, generated_seq_lengths @torch.no_grad() def SCREAMING_SNAKE_CASE ( self :Union[str, Any] , snake_case :int=None , snake_case :str=None , snake_case :int=None , snake_case :int = 5 , snake_case :int = 67 , snake_case :float = 1.0 , snake_case :Optional[int] = None , ): '''simple docstring''' A_ : Optional[Any] = eos_token_id A_ : List[Any] = None A_ : List[Any] = None A_ : str = torch.ones(snake_case , device=snake_case , dtype=torch.int ) A_ : Any = torch.zeros(snake_case , device=snake_case , dtype=torch.bool ) if input_embeds is not None: A_ : Any = input_embeds else: A_ : Optional[Any] = self.transformer.transformer.wte(snake_case ) for i in range(snake_case ): A_ : Optional[Any] = self.transformer(inputs_embeds=snake_case ) A_ : str = outputs.logits A_ : int = logits[:, -1, :] / (temperature if temperature > 0 else 1.0) A_ : List[str] = logits.softmax(-1 ).log() if scores is None: A_ , A_ : Union[str, Any] = logits.topk(snake_case , -1 ) A_ : Tuple = generated.expand(snake_case , *generated.shape[1:] ) A_ , A_ : str = next_tokens.permute(1 , 0 ), scores.squeeze(0 ) if tokens is None: A_ : Union[str, Any] = next_tokens else: A_ : List[str] = tokens.expand(snake_case , *tokens.shape[1:] ) A_ : Union[str, Any] = torch.cat((tokens, next_tokens) , dim=1 ) else: A_ : List[str] = -float(np.inf ) A_ : List[Any] = 0 A_ : Union[str, Any] = scores[:, None] + logits seq_lengths[~is_stopped] += 1 A_ : Optional[Any] = scores_sum / seq_lengths[:, None] A_ , A_ : List[str] = scores_sum_average.view(-1 ).topk(snake_case , -1 ) A_ : str = next_tokens // scores_sum.shape[1] A_ : Union[str, Any] = seq_lengths[next_tokens_source] A_ : Optional[int] = next_tokens % scores_sum.shape[1] A_ : Tuple = next_tokens.unsqueeze(1 ) A_ : Tuple = tokens[next_tokens_source] A_ : Dict = torch.cat((tokens, next_tokens) , dim=1 ) A_ : Dict = generated[next_tokens_source] A_ : Union[str, Any] = scores_sum_average * seq_lengths A_ : Optional[int] = is_stopped[next_tokens_source] A_ : Tuple = self.transformer.transformer.wte(next_tokens.squeeze() ).view(generated.shape[0] , 1 , -1 ) A_ : Union[str, Any] = torch.cat((generated, next_token_embed) , dim=1 ) A_ : Any = is_stopped + next_tokens.eq(snake_case ).squeeze() if is_stopped.all(): break A_ : int = scores / seq_lengths A_ : str = scores.argsort(descending=snake_case ) # tokens tensors are already padded to max_seq_length A_ : Dict = [tokens[i] for i in order] A_ : int = torch.stack(snake_case , dim=0 ) A_ : List[Any] = torch.tensor([seq_lengths[i] for i in order] , dtype=seq_lengths.dtype ) return output_texts, seq_lengths
300
0
"""simple docstring""" from collections import defaultdict from math import gcd def __a ( __lowerCamelCase = 150_0000 ): UpperCAmelCase_ : defaultdict = defaultdict(__lowerCamelCase ) UpperCAmelCase_ : List[str] = 2 while 2 * euclid_m * (euclid_m + 1) <= limit: for euclid_n in range((euclid_m % 2) + 1, __lowerCamelCase, 2 ): if gcd(__lowerCamelCase, __lowerCamelCase ) > 1: continue UpperCAmelCase_ : str = 2 * euclid_m * (euclid_m + euclid_n) for perimeter in range(__lowerCamelCase, limit + 1, __lowerCamelCase ): frequencies[perimeter] += 1 euclid_m += 1 return sum(1 for frequency in frequencies.values() if frequency == 1 ) if __name__ == "__main__": print(f"""{solution() = }""")
61
import warnings from ...utils import logging from .image_processing_yolos import YolosImageProcessor _lowerCAmelCase : Tuple = logging.get_logger(__name__) class __magic_name__ ( lowerCamelCase__ ): """simple docstring""" def __init__( self :Union[str, Any] , *snake_case :Tuple , **snake_case :Any ): '''simple docstring''' warnings.warn( "The class YolosFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please" " use YolosImageProcessor instead." , snake_case , ) super().__init__(*snake_case , **snake_case )
300
0
import os import time import numpy as np import onnxruntime as ort _A = '1' _A = '0' _A = '1' _A = ort.SessionOptions() _A = ort.GraphOptimizationLevel.ORT_DISABLE_ALL print('Create inference session...') _A = ['TensorrtExecutionProvider', 'CUDAExecutionProvider'] _A = ort.InferenceSession('model.onnx', sess_options=sess_opt, providers=execution_provider) _A = ort.RunOptions() _A = 128 _A = 1 _A = np.ones((batch, sequence), dtype=np.intaa) _A = np.ones((batch, sequence), dtype=np.intaa) _A = np.ones((batch, sequence), dtype=np.intaa) print('Warm up phase...') sess.run( None, { sess.get_inputs()[0].name: input_ids, sess.get_inputs()[1].name: attention_mask, sess.get_inputs()[2].name: token_type_ids, }, run_options=run_opt, ) print('Start inference...') _A = time.time() _A = 2000 _A = {} for iter in range(max_iters): _A = sess.run( None, { sess.get_inputs()[0].name: input_ids, sess.get_inputs()[1].name: attention_mask, sess.get_inputs()[2].name: token_type_ids, }, run_options=run_opt, ) print('Average Inference Time = {:.3f} ms'.format((time.time() - start_time) * 1000 / max_iters))
62
from __future__ import annotations def __snake_case ( _lowerCAmelCase : list[float] ) -> bool: if len(_lowerCAmelCase ) < 2: raise ValueError("Monogons and Digons are not polygons in the Euclidean space" ) if any(i <= 0 for i in nums ): raise ValueError("All values must be greater than 0" ) A_ : List[str] = nums.copy() copy_nums.sort() return copy_nums[-1] < sum(copy_nums[:-1] ) if __name__ == "__main__": import doctest doctest.testmod()
300
0
'''simple docstring''' import io import json import unittest from parameterized import parameterized from transformers import FSMTForConditionalGeneration, FSMTTokenizer from transformers.testing_utils import get_tests_dir, require_torch, slow, torch_device from utils import calculate_bleu lowerCAmelCase_ : Tuple = get_tests_dir() + '/test_data/fsmt/fsmt_val_data.json' with io.open(filename, 'r', encoding='utf-8') as f: lowerCAmelCase_ : Dict = json.load(f) @require_torch class __SCREAMING_SNAKE_CASE (unittest.TestCase ): """simple docstring""" def UpperCamelCase__ ( self : List[str] , __a : Optional[Any] ): return FSMTTokenizer.from_pretrained(__a ) def UpperCamelCase__ ( self : Union[str, Any] , __a : List[str] ): _a = FSMTForConditionalGeneration.from_pretrained(__a ).to(__a ) if torch_device == "cuda": model.half() return model @parameterized.expand( [ ["en-ru", 26.0], ["ru-en", 22.0], ["en-de", 22.0], ["de-en", 29.0], ] ) @slow def UpperCamelCase__ ( self : Optional[int] , __a : Union[str, Any] , __a : Tuple ): # note: this test is not testing the best performance since it only evals a small batch # but it should be enough to detect a regression in the output quality _a = f'facebook/wmt19-{pair}' _a = self.get_tokenizer(__a ) _a = self.get_model(__a ) _a = bleu_data[pair]["src"] _a = bleu_data[pair]["tgt"] _a = tokenizer(__a , return_tensors="pt" , truncation=__a , padding="longest" ).to(__a ) _a = model.generate( input_ids=batch.input_ids , num_beams=8 , ) _a = tokenizer.batch_decode( __a , skip_special_tokens=__a , clean_up_tokenization_spaces=__a ) _a = calculate_bleu(__a , __a ) print(__a ) self.assertGreaterEqual(scores["bleu"] , __a )
63
import inspect from typing import Callable, List, Optional, Union import torch from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer from diffusers import DiffusionPipeline from diffusers.models import AutoencoderKL, UNetaDConditionModel from diffusers.pipelines.stable_diffusion import StableDiffusionPipelineOutput from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker from diffusers.schedulers import DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler from diffusers.utils import logging _lowerCAmelCase : Any = logging.get_logger(__name__) # pylint: disable=invalid-name class __magic_name__ ( lowerCamelCase__ ): """simple docstring""" def __init__( self :Union[str, Any] , snake_case :AutoencoderKL , snake_case :CLIPTextModel , snake_case :CLIPTokenizer , snake_case :UNetaDConditionModel , snake_case :Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler] , snake_case :StableDiffusionSafetyChecker , snake_case :CLIPImageProcessor , ): '''simple docstring''' super().__init__() self.register_modules( vae=snake_case , text_encoder=snake_case , tokenizer=snake_case , unet=snake_case , scheduler=snake_case , safety_checker=snake_case , feature_extractor=snake_case , ) def SCREAMING_SNAKE_CASE ( self :List[Any] , snake_case :Optional[Union[str, int]] = "auto" ): '''simple docstring''' if slice_size == "auto": # half the attention head size is usually a good trade-off between # speed and memory A_ : int = self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(snake_case ) def SCREAMING_SNAKE_CASE ( self :Dict ): '''simple docstring''' self.enable_attention_slicing(snake_case ) @torch.no_grad() def __call__( self :Any , snake_case :Union[str, List[str]] , snake_case :int = 512 , snake_case :int = 512 , snake_case :int = 50 , snake_case :float = 7.5 , snake_case :Optional[Union[str, List[str]]] = None , snake_case :Optional[int] = 1 , snake_case :float = 0.0 , snake_case :Optional[torch.Generator] = None , snake_case :Optional[torch.FloatTensor] = None , snake_case :Optional[str] = "pil" , snake_case :bool = True , snake_case :Optional[Callable[[int, int, torch.FloatTensor], None]] = None , snake_case :int = 1 , snake_case :Optional[torch.FloatTensor] = None , **snake_case :Optional[Any] , ): '''simple docstring''' if isinstance(snake_case , snake_case ): A_ : Dict = 1 elif isinstance(snake_case , snake_case ): A_ : Optional[Any] = len(snake_case ) else: raise ValueError(f"`prompt` has to be of type `str` or `list` but is {type(snake_case )}" ) if height % 8 != 0 or width % 8 != 0: raise ValueError(f"`height` and `width` have to be divisible by 8 but are {height} and {width}." ) if (callback_steps is None) or ( callback_steps is not None and (not isinstance(snake_case , snake_case ) or callback_steps <= 0) ): raise ValueError( f"`callback_steps` has to be a positive integer but is {callback_steps} of type" f" {type(snake_case )}." ) # get prompt text embeddings A_ : int = self.tokenizer( snake_case , padding="max_length" , max_length=self.tokenizer.model_max_length , return_tensors="pt" , ) A_ : Dict = text_inputs.input_ids if text_input_ids.shape[-1] > self.tokenizer.model_max_length: A_ : Optional[int] = 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}" ) A_ : Tuple = text_input_ids[:, : self.tokenizer.model_max_length] if text_embeddings is None: A_ : Union[str, Any] = self.text_encoder(text_input_ids.to(self.device ) )[0] # duplicate text embeddings for each generation per prompt, using mps friendly method A_ , A_ , A_ : int = text_embeddings.shape A_ : List[str] = text_embeddings.repeat(1 , snake_case , 1 ) A_ : List[str] = text_embeddings.view(bs_embed * num_images_per_prompt , snake_case , -1 ) # here `guidance_scale` is defined analog to the guidance weight `w` of equation (2) # of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1` # corresponds to doing no classifier free guidance. A_ : Dict = guidance_scale > 1.0 # get unconditional embeddings for classifier free guidance if do_classifier_free_guidance: A_ : List[str] if negative_prompt is None: A_ : List[str] = [""] elif type(snake_case ) is not type(snake_case ): raise TypeError( f"`negative_prompt` should be the same type to `prompt`, but got {type(snake_case )} !=" f" {type(snake_case )}." ) elif isinstance(snake_case , snake_case ): A_ : Optional[Any] = [negative_prompt] elif batch_size != len(snake_case ): raise ValueError( f"`negative_prompt`: {negative_prompt} has batch size {len(snake_case )}, but `prompt`:" f" {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches" " the batch size of `prompt`." ) else: A_ : Any = negative_prompt A_ : Optional[int] = text_input_ids.shape[-1] A_ : Dict = self.tokenizer( snake_case , padding="max_length" , max_length=snake_case , truncation=snake_case , return_tensors="pt" , ) A_ : Any = self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # duplicate unconditional embeddings for each generation per prompt, using mps friendly method A_ : Tuple = uncond_embeddings.shape[1] A_ : Dict = uncond_embeddings.repeat(snake_case , snake_case , 1 ) A_ : Dict = uncond_embeddings.view(batch_size * num_images_per_prompt , snake_case , -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 A_ : Optional[int] = torch.cat([uncond_embeddings, text_embeddings] ) # get the initial random noise unless the user supplied it # Unlike in other pipelines, latents need to be generated in the target device # for 1-to-1 results reproducibility with the CompVis implementation. # However this currently doesn't work in `mps`. A_ : List[str] = (batch_size * num_images_per_prompt, self.unet.config.in_channels, height // 8, width // 8) A_ : str = (batch_size * num_images_per_prompt, self.unet.config.in_channels, 64, 64) A_ : List[Any] = text_embeddings.dtype if latents is None: if self.device.type == "mps": # randn does not exist on mps A_ : Tuple = torch.randn( snake_case , generator=snake_case , device="cpu" , dtype=snake_case ).to(self.device ) A_ : Optional[Any] = torch.randn(snake_case , generator=snake_case , device="cpu" , dtype=snake_case ).to( self.device ) else: A_ : int = torch.randn( snake_case , generator=snake_case , device=self.device , dtype=snake_case ) A_ : Optional[int] = torch.randn(snake_case , generator=snake_case , device=self.device , dtype=snake_case ) else: if latents_reference.shape != latents_shape: raise ValueError(f"Unexpected latents shape, got {latents.shape}, expected {latents_shape}" ) A_ : Tuple = latents_reference.to(self.device ) A_ : Any = latents.to(self.device ) # This is the key part of the pipeline where we # try to ensure that the generated images w/ the same seed # but different sizes actually result in similar images A_ : List[Any] = (latents_shape[3] - latents_shape_reference[3]) // 2 A_ : Optional[int] = (latents_shape[2] - latents_shape_reference[2]) // 2 A_ : Optional[int] = latents_shape_reference[3] if dx >= 0 else latents_shape_reference[3] + 2 * dx A_ : Dict = latents_shape_reference[2] if dy >= 0 else latents_shape_reference[2] + 2 * dy A_ : Optional[Any] = 0 if dx < 0 else dx A_ : Optional[Any] = 0 if dy < 0 else dy A_ : List[str] = max(-dx , 0 ) A_ : List[Any] = max(-dy , 0 ) # import pdb # pdb.set_trace() A_ : Any = latents_reference[:, :, dy : dy + h, dx : dx + w] # set timesteps self.scheduler.set_timesteps(snake_case ) # Some schedulers like PNDM have timesteps as arrays # It's more optimized to move all timesteps to correct device beforehand A_ : str = self.scheduler.timesteps.to(self.device ) # scale the initial noise by the standard deviation required by the scheduler A_ : Union[str, Any] = latents * self.scheduler.init_noise_sigma # prepare extra kwargs for the scheduler step, since not all schedulers have the same signature # eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers. # eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502 # and should be between [0, 1] A_ : Optional[int] = "eta" in set(inspect.signature(self.scheduler.step ).parameters.keys() ) A_ : List[str] = {} if accepts_eta: A_ : Union[str, Any] = eta for i, t in enumerate(self.progress_bar(snake_case ) ): # expand the latents if we are doing classifier free guidance A_ : Optional[Any] = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents A_ : Any = self.scheduler.scale_model_input(snake_case , snake_case ) # predict the noise residual A_ : List[str] = self.unet(snake_case , snake_case , encoder_hidden_states=snake_case ).sample # perform guidance if do_classifier_free_guidance: A_ , A_ : Dict = noise_pred.chunk(2 ) A_ : List[Any] = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) # compute the previous noisy sample x_t -> x_t-1 A_ : Tuple = self.scheduler.step(snake_case , snake_case , snake_case , **snake_case ).prev_sample # call the callback, if provided if callback is not None and i % callback_steps == 0: callback(snake_case , snake_case , snake_case ) A_ : List[str] = 1 / 0.18215 * latents A_ : Tuple = self.vae.decode(snake_case ).sample A_ : Dict = (image / 2 + 0.5).clamp(0 , 1 ) # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16 A_ : List[str] = image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() if self.safety_checker is not None: A_ : int = self.feature_extractor(self.numpy_to_pil(snake_case ) , return_tensors="pt" ).to( self.device ) A_ , A_ : List[str] = self.safety_checker( images=snake_case , clip_input=safety_checker_input.pixel_values.to(text_embeddings.dtype ) ) else: A_ : List[str] = None if output_type == "pil": A_ : Optional[int] = self.numpy_to_pil(snake_case ) if not return_dict: return (image, has_nsfw_concept) return StableDiffusionPipelineOutput(images=snake_case , nsfw_content_detected=snake_case )
300
0
"""simple docstring""" from __future__ import annotations def UpperCAmelCase__ (snake_case__ : list[int] , snake_case__ : int ): """simple docstring""" _snake_case : list[list[int]] = [] _snake_case : list[int] = [] _snake_case : Tuple = 0 _snake_case : Optional[Any] = sum(snake_case__ ) create_state_space_tree(snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ ) return result def UpperCAmelCase__ (snake_case__ : list[int] , snake_case__ : int , snake_case__ : int , snake_case__ : list[int] , snake_case__ : list[list[int]] , snake_case__ : int , ): """simple docstring""" if sum(snake_case__ ) > max_sum or (remaining_nums_sum + sum(snake_case__ )) < max_sum: return if sum(snake_case__ ) == max_sum: result.append(snake_case__ ) return for index in range(snake_case__ , len(snake_case__ ) ): create_state_space_tree( snake_case__ , snake_case__ , index + 1 , [*path, nums[index]] , snake_case__ , remaining_nums_sum - nums[index] , ) A_ = [3, 34, 4, 12, 5, 2] A_ = 9 A_ = generate_sum_of_subsets_soln(nums, max_sum) print(*result)
64
import torch import torch.nn as nn from transformers import CLIPConfig, CLIPVisionModel, PreTrainedModel from ...utils import logging _lowerCAmelCase : List[str] = logging.get_logger(__name__) def __snake_case ( _lowerCAmelCase : int , _lowerCAmelCase : Any ) -> Dict: A_ : Optional[Any] = nn.functional.normalize(_lowerCAmelCase ) A_ : List[str] = nn.functional.normalize(_lowerCAmelCase ) return torch.mm(_lowerCAmelCase , normalized_text_embeds.t() ) class __magic_name__ ( lowerCamelCase__ ): """simple docstring""" __UpperCamelCase = CLIPConfig __UpperCamelCase = ['''CLIPEncoderLayer'''] def __init__( self :int , snake_case :CLIPConfig ): '''simple docstring''' super().__init__(snake_case ) A_ : int = CLIPVisionModel(config.vision_config ) A_ : List[str] = nn.Linear(config.vision_config.hidden_size , config.projection_dim , bias=snake_case ) A_ : Tuple = nn.Parameter(torch.ones(17 , config.projection_dim ) , requires_grad=snake_case ) A_ : str = nn.Parameter(torch.ones(3 , config.projection_dim ) , requires_grad=snake_case ) A_ : List[str] = nn.Parameter(torch.ones(17 ) , requires_grad=snake_case ) A_ : int = nn.Parameter(torch.ones(3 ) , requires_grad=snake_case ) @torch.no_grad() def SCREAMING_SNAKE_CASE ( self :Union[str, Any] , snake_case :Dict , snake_case :Any ): '''simple docstring''' A_ : List[Any] = self.vision_model(snake_case )[1] # pooled_output A_ : List[Any] = self.visual_projection(snake_case ) # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16 A_ : Optional[Any] = cosine_distance(snake_case , self.special_care_embeds ).cpu().float().numpy() A_ : Tuple = cosine_distance(snake_case , self.concept_embeds ).cpu().float().numpy() A_ : Union[str, Any] = [] A_ : Any = image_embeds.shape[0] for i in range(snake_case ): A_ : Optional[int] = {"special_scores": {}, "special_care": [], "concept_scores": {}, "bad_concepts": []} # increase this value to create a stronger `nfsw` filter # at the cost of increasing the possibility of filtering benign images A_ : Optional[Any] = 0.0 for concept_idx in range(len(special_cos_dist[0] ) ): A_ : Optional[Any] = special_cos_dist[i][concept_idx] A_ : Tuple = self.special_care_embeds_weights[concept_idx].item() A_ : Union[str, Any] = round(concept_cos - concept_threshold + adjustment , 3 ) if result_img["special_scores"][concept_idx] > 0: result_img["special_care"].append({concept_idx, result_img["special_scores"][concept_idx]} ) A_ : Any = 0.01 for concept_idx in range(len(cos_dist[0] ) ): A_ : Tuple = cos_dist[i][concept_idx] A_ : Tuple = self.concept_embeds_weights[concept_idx].item() A_ : Tuple = round(concept_cos - concept_threshold + adjustment , 3 ) if result_img["concept_scores"][concept_idx] > 0: result_img["bad_concepts"].append(snake_case ) result.append(snake_case ) A_ : Any = [len(res["bad_concepts"] ) > 0 for res in result] return images, has_nsfw_concepts @torch.no_grad() def SCREAMING_SNAKE_CASE ( self :Union[str, Any] , snake_case :torch.FloatTensor , snake_case :torch.FloatTensor ): '''simple docstring''' A_ : List[str] = self.vision_model(snake_case )[1] # pooled_output A_ : int = self.visual_projection(snake_case ) A_ : Tuple = cosine_distance(snake_case , self.special_care_embeds ) A_ : Tuple = cosine_distance(snake_case , self.concept_embeds ) # increase this value to create a stronger `nsfw` filter # at the cost of increasing the possibility of filtering benign images A_ : Optional[Any] = 0.0 A_ : Tuple = special_cos_dist - self.special_care_embeds_weights + adjustment # special_scores = special_scores.round(decimals=3) A_ : Optional[Any] = torch.any(special_scores > 0 , dim=1 ) A_ : Optional[Any] = special_care * 0.01 A_ : Optional[int] = special_adjustment.unsqueeze(1 ).expand(-1 , cos_dist.shape[1] ) A_ : Union[str, Any] = (cos_dist - self.concept_embeds_weights) + special_adjustment # concept_scores = concept_scores.round(decimals=3) A_ : Union[str, Any] = torch.any(concept_scores > 0 , dim=1 ) return images, has_nsfw_concepts
300
0
import argparse import logging import pickle from collections import Counter logging.basicConfig( format='%(asctime)s - %(levelname)s - %(name)s - %(message)s', datefmt='%m/%d/%Y %H:%M:%S', level=logging.INFO ) UpperCamelCase__ = logging.getLogger(__name__) if __name__ == "__main__": UpperCamelCase__ = argparse.ArgumentParser( description='Token Counts for smoothing the masking probabilities in MLM (cf XLM/word2vec)' ) parser.add_argument( '--data_file', type=str, default='data/dump.bert-base-uncased.pickle', help='The binarized dataset.' ) parser.add_argument( '--token_counts_dump', type=str, default='data/token_counts.bert-base-uncased.pickle', help='The dump file.' ) parser.add_argument('--vocab_size', default=3_0_5_2_2, type=int) UpperCamelCase__ = parser.parse_args() logger.info(f'''Loading data from {args.data_file}''') with open(args.data_file, 'rb') as fp: UpperCamelCase__ = pickle.load(fp) logger.info('Counting occurrences for MLM.') UpperCamelCase__ = Counter() for tk_ids in data: counter.update(tk_ids) UpperCamelCase__ = [0] * args.vocab_size for k, v in counter.items(): UpperCamelCase__ = v logger.info(f'''Dump to {args.token_counts_dump}''') with open(args.token_counts_dump, 'wb') as handle: pickle.dump(counts, handle, protocol=pickle.HIGHEST_PROTOCOL)
65
import argparse from pathlib import Path import requests import torch from PIL import Image from transformers import ( RobertaTokenizer, TrOCRConfig, TrOCRForCausalLM, TrOCRProcessor, VisionEncoderDecoderModel, ViTConfig, ViTImageProcessor, ViTModel, ) from transformers.utils import logging logging.set_verbosity_info() _lowerCAmelCase : List[str] = logging.get_logger(__name__) def __snake_case ( _lowerCAmelCase : Optional[int] , _lowerCAmelCase : Union[str, Any] ) -> Optional[int]: A_ : Tuple = [] 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 __snake_case ( _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : Union[str, Any] ) -> Dict: for i in range(encoder_config.num_hidden_layers ): # queries, keys and values (only weights, no biases) A_ : str = state_dict.pop(f"encoder.deit.blocks.{i}.attn.qkv.weight" ) A_ : List[Any] = in_proj_weight[ : encoder_config.hidden_size, : ] A_ : Optional[Any] = in_proj_weight[ encoder_config.hidden_size : encoder_config.hidden_size * 2, : ] A_ : Optional[Any] = in_proj_weight[ -encoder_config.hidden_size :, : ] def __snake_case ( _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : Tuple , _lowerCAmelCase : Dict ) -> Any: A_ : Dict = dct.pop(_lowerCAmelCase ) A_ : List[Any] = val def __snake_case ( _lowerCAmelCase : List[str] ) -> int: if "handwritten" in checkpoint_url: A_ : Any = "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: A_ : Any = "https://www.researchgate.net/profile/Dinh-Sang/publication/338099565/figure/fig8/AS:840413229350922@1577381536857/An-receipt-example-in-the-SROIE-2019-dataset_Q640.jpg" A_ : List[Any] = Image.open(requests.get(_lowerCAmelCase , stream=_lowerCAmelCase ).raw ).convert("RGB" ) return im @torch.no_grad() def __snake_case ( _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : Union[str, Any] ) -> List[Any]: A_ : Optional[Any] = ViTConfig(image_size=384 , qkv_bias=_lowerCAmelCase ) A_ : Tuple = TrOCRConfig() # size of the architecture if "base" in checkpoint_url: A_ : Tuple = 768 elif "large" in checkpoint_url: # use ViT-large encoder A_ : Optional[Any] = 1024 A_ : Union[str, Any] = 4096 A_ : Union[str, Any] = 24 A_ : List[Any] = 16 A_ : List[str] = 1024 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: A_ : Dict = False A_ : int = "relu" A_ : Optional[int] = 1024 A_ : Any = True A_ : List[Any] = False A_ : Optional[int] = False # load HuggingFace model A_ : Union[str, Any] = ViTModel(_lowerCAmelCase , add_pooling_layer=_lowerCAmelCase ) A_ : str = TrOCRForCausalLM(_lowerCAmelCase ) A_ : List[str] = VisionEncoderDecoderModel(encoder=_lowerCAmelCase , decoder=_lowerCAmelCase ) model.eval() # load state_dict of original model, rename some keys A_ : Optional[int] = torch.hub.load_state_dict_from_url(_lowerCAmelCase , map_location="cpu" , check_hash=_lowerCAmelCase )["model"] A_ : Dict = create_rename_keys(_lowerCAmelCase , _lowerCAmelCase ) for src, dest in rename_keys: rename_key(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) read_in_q_k_v(_lowerCAmelCase , _lowerCAmelCase ) # 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(): A_ : Dict = state_dict.pop(_lowerCAmelCase ) if key.startswith("decoder" ) and "output_projection" not in key: A_ : List[str] = val else: A_ : Optional[Any] = val # load state dict model.load_state_dict(_lowerCAmelCase ) # Check outputs on an image A_ : List[Any] = ViTImageProcessor(size=encoder_config.image_size ) A_ : Any = RobertaTokenizer.from_pretrained("roberta-large" ) A_ : Union[str, Any] = TrOCRProcessor(_lowerCAmelCase , _lowerCAmelCase ) A_ : List[str] = processor(images=prepare_img(_lowerCAmelCase ) , return_tensors="pt" ).pixel_values # verify logits A_ : Union[str, Any] = torch.tensor([[model.config.decoder.decoder_start_token_id]] ) A_ : Optional[int] = model(pixel_values=_lowerCAmelCase , decoder_input_ids=_lowerCAmelCase ) A_ : Tuple = outputs.logits A_ : Union[str, Any] = torch.Size([1, 1, 50265] ) if "trocr-base-handwritten" in checkpoint_url: A_ : Union[str, Any] = torch.tensor( [-1.45_02, -4.66_83, -0.53_47, -2.92_91, 9.14_35, -3.05_71, 8.97_64, 1.75_60, 8.73_58, -1.53_11] ) elif "trocr-large-handwritten" in checkpoint_url: A_ : str = torch.tensor( [-2.64_37, -1.31_29, -2.25_96, -5.34_55, 6.35_39, 1.76_04, 5.49_91, 1.47_02, 5.61_13, 2.01_70] ) elif "trocr-base-printed" in checkpoint_url: A_ : Optional[Any] = torch.tensor( [-5.68_16, -5.83_88, 1.13_98, -6.90_34, 6.85_05, -2.43_93, 1.22_84, -1.02_32, -1.96_61, -3.92_10] ) elif "trocr-large-printed" in checkpoint_url: A_ : Optional[int] = torch.tensor( [-6.01_62, -7.09_59, 4.41_55, -5.10_63, 7.04_68, -3.16_31, 2.64_66, -0.30_81, -0.81_06, -1.75_35] ) if "stage1" not in checkpoint_url: assert logits.shape == expected_shape, "Shape of logits not as expected" assert torch.allclose(logits[0, 0, :10] , _lowerCAmelCase , atol=1e-3 ), "First elements of logits not as expected" Path(_lowerCAmelCase ).mkdir(exist_ok=_lowerCAmelCase ) print(f"Saving model to {pytorch_dump_folder_path}" ) model.save_pretrained(_lowerCAmelCase ) print(f"Saving processor to {pytorch_dump_folder_path}" ) processor.save_pretrained(_lowerCAmelCase ) if __name__ == "__main__": _lowerCAmelCase : Dict = argparse.ArgumentParser() parser.add_argument( '''--checkpoint_url''', default='''https://layoutlm.blob.core.windows.net/trocr/model_zoo/fairseq/trocr-base-handwritten.pt''', type=str, help='''URL to the original PyTorch checkpoint (.pth file).''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the folder to output PyTorch model.''' ) _lowerCAmelCase : List[str] = parser.parse_args() convert_tr_ocr_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)
300
0
"""simple docstring""" import collections import inspect import unittest from typing import Dict, List, Tuple from transformers import MaskFormerSwinConfig from transformers.testing_utils import require_torch, require_torch_multi_gpu, torch_device from transformers.utils import is_torch_available from ...test_backbone_common import BackboneTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import MaskFormerSwinBackbone from transformers.models.maskformer import MaskFormerSwinModel class lowerCamelCase : '''simple docstring''' def __init__( self: Dict , snake_case: Optional[Any] , snake_case: Tuple=13 , snake_case: Any=32 , snake_case: Union[str, Any]=2 , snake_case: Tuple=3 , snake_case: Union[str, Any]=16 , snake_case: Union[str, Any]=[1, 2, 1] , snake_case: Optional[Any]=[2, 2, 4] , snake_case: str=2 , snake_case: List[str]=2.0 , snake_case: Optional[int]=True , snake_case: Union[str, Any]=0.0 , snake_case: Optional[int]=0.0 , snake_case: Optional[Any]=0.1 , snake_case: List[str]="gelu" , snake_case: Any=False , snake_case: Optional[Any]=True , snake_case: Optional[int]=0.0_2 , snake_case: Any=1E-5 , snake_case: Optional[int]=True , snake_case: int=None , snake_case: Any=True , snake_case: str=10 , snake_case: Optional[Any]=8 , snake_case: Union[str, Any]=["stage1", "stage2", "stage3"] , snake_case: Tuple=[1, 2, 3] , ) -> Dict: snake_case_ :Dict = parent snake_case_ :List[Any] = batch_size snake_case_ :Dict = image_size snake_case_ :Dict = patch_size snake_case_ :Tuple = num_channels snake_case_ :List[Any] = embed_dim snake_case_ :List[str] = depths snake_case_ :str = num_heads snake_case_ :Tuple = window_size snake_case_ :Tuple = mlp_ratio snake_case_ :int = qkv_bias snake_case_ :Tuple = hidden_dropout_prob snake_case_ :Optional[Any] = attention_probs_dropout_prob snake_case_ :Dict = drop_path_rate snake_case_ :Any = hidden_act snake_case_ :Any = use_absolute_embeddings snake_case_ :int = patch_norm snake_case_ :List[Any] = layer_norm_eps snake_case_ :Tuple = initializer_range snake_case_ :str = is_training snake_case_ :int = scope snake_case_ :Tuple = use_labels snake_case_ :Tuple = type_sequence_label_size snake_case_ :str = encoder_stride snake_case_ :List[Any] = out_features snake_case_ :str = out_indices def lowerCAmelCase_ ( self: Tuple ) -> Dict: snake_case_ :Optional[int] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) snake_case_ :str = None if self.use_labels: snake_case_ :Optional[int] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) snake_case_ :Union[str, Any] = self.get_config() return config, pixel_values, labels def lowerCAmelCase_ ( self: int ) -> Optional[Any]: return MaskFormerSwinConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , embed_dim=self.embed_dim , depths=self.depths , num_heads=self.num_heads , window_size=self.window_size , mlp_ratio=self.mlp_ratio , qkv_bias=self.qkv_bias , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , drop_path_rate=self.drop_path_rate , hidden_act=self.hidden_act , use_absolute_embeddings=self.use_absolute_embeddings , path_norm=self.patch_norm , layer_norm_eps=self.layer_norm_eps , initializer_range=self.initializer_range , encoder_stride=self.encoder_stride , out_features=self.out_features , out_indices=self.out_indices , ) def lowerCAmelCase_ ( self: List[Any] , snake_case: str , snake_case: int , snake_case: List[str] ) -> Any: snake_case_ :Dict = MaskFormerSwinModel(config=snake_case ) model.to(snake_case ) model.eval() snake_case_ :Tuple = model(snake_case ) snake_case_ :Dict = ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths ) - 1)) snake_case_ :Any = int(config.embed_dim * 2 ** (len(config.depths ) - 1) ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, expected_seq_len, expected_dim) ) def lowerCAmelCase_ ( self: Optional[Any] , snake_case: int , snake_case: List[str] , snake_case: Tuple ) -> Union[str, Any]: snake_case_ :Any = MaskFormerSwinBackbone(config=snake_case ) model.to(snake_case ) model.eval() snake_case_ :Optional[Any] = model(snake_case ) # verify feature maps self.parent.assertEqual(len(result.feature_maps ) , len(config.out_features ) ) self.parent.assertListEqual(list(result.feature_maps[0].shape ) , [13, 16, 16, 16] ) # verify channels self.parent.assertEqual(len(model.channels ) , len(config.out_features ) ) self.parent.assertListEqual(model.channels , [16, 32, 64] ) # verify ValueError with self.parent.assertRaises(snake_case ): snake_case_ :Optional[Any] = ["""stem"""] snake_case_ :str = MaskFormerSwinBackbone(config=snake_case ) def lowerCAmelCase_ ( self: List[str] ) -> Optional[Any]: snake_case_ :Optional[int] = self.prepare_config_and_inputs() snake_case_, snake_case_, snake_case_ :str = config_and_inputs snake_case_ :Tuple = {"""pixel_values""": pixel_values} return config, inputs_dict @require_torch class lowerCamelCase ( _lowerCAmelCase , _lowerCAmelCase , unittest.TestCase ): '''simple docstring''' _A : Union[str, Any] = ( ( MaskFormerSwinModel, MaskFormerSwinBackbone, ) if is_torch_available() else () ) _A : str = {"""feature-extraction""": MaskFormerSwinModel} if is_torch_available() else {} _A : List[str] = False _A : Any = False _A : Dict = False _A : List[Any] = False _A : Optional[int] = False def lowerCAmelCase_ ( self: Dict ) -> Any: snake_case_ :str = MaskFormerSwinModelTester(self ) snake_case_ :Optional[Any] = ConfigTester(self , config_class=snake_case , embed_dim=37 ) @require_torch_multi_gpu @unittest.skip( reason=( """`MaskFormerSwinModel` outputs `hidden_states_spatial_dimensions` which doesn't work well with""" """ `nn.DataParallel`""" ) ) def lowerCAmelCase_ ( self: List[str] ) -> Optional[int]: pass def lowerCAmelCase_ ( self: Union[str, Any] ) -> Dict: self.create_and_test_config_common_properties() self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() def lowerCAmelCase_ ( self: Any ) -> Tuple: return def lowerCAmelCase_ ( self: Any ) -> Any: snake_case_ :List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case ) def lowerCAmelCase_ ( self: Union[str, Any] ) -> int: snake_case_ :Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_backbone(*snake_case ) @unittest.skip("""Swin does not use inputs_embeds""" ) def lowerCAmelCase_ ( self: str ) -> List[str]: pass @unittest.skip("""Swin does not support feedforward chunking""" ) def lowerCAmelCase_ ( self: int ) -> Optional[int]: pass def lowerCAmelCase_ ( self: List[str] ) -> List[Any]: snake_case_, snake_case_ :List[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: snake_case_ :str = model_class(snake_case ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) snake_case_ :Dict = model.get_output_embeddings() self.assertTrue(x is None or isinstance(snake_case , nn.Linear ) ) def lowerCAmelCase_ ( self: Tuple ) -> Dict: snake_case_, snake_case_ :int = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: snake_case_ :Optional[int] = model_class(snake_case ) snake_case_ :str = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic snake_case_ :str = [*signature.parameters.keys()] snake_case_ :str = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , snake_case ) @unittest.skip(reason="""MaskFormerSwin is only used as backbone and doesn't support output_attentions""" ) def lowerCAmelCase_ ( self: List[Any] ) -> List[Any]: pass @unittest.skip(reason="""MaskFormerSwin is only used as an internal backbone""" ) def lowerCAmelCase_ ( self: Dict ) -> List[Any]: pass def lowerCAmelCase_ ( self: Union[str, Any] , snake_case: Union[str, Any] , snake_case: int , snake_case: Any , snake_case: List[str] ) -> str: snake_case_ :List[str] = model_class(snake_case ) model.to(snake_case ) model.eval() with torch.no_grad(): snake_case_ :List[Any] = model(**self._prepare_for_class(snake_case , snake_case ) ) snake_case_ :Any = outputs.hidden_states snake_case_ :Optional[int] = getattr( self.model_tester , """expected_num_hidden_layers""" , len(self.model_tester.depths ) + 1 ) self.assertEqual(len(snake_case ) , snake_case ) # Swin has a different seq_length snake_case_ :str = ( config.patch_size if isinstance(config.patch_size , collections.abc.Iterable ) else (config.patch_size, config.patch_size) ) snake_case_ :int = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0]) self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [num_patches, self.model_tester.embed_dim] , ) def lowerCAmelCase_ ( self: List[Any] ) -> Optional[int]: snake_case_, snake_case_ :Any = self.model_tester.prepare_config_and_inputs_for_common() snake_case_ :List[Any] = ( self.model_tester.image_size if isinstance(self.model_tester.image_size , collections.abc.Iterable ) else (self.model_tester.image_size, self.model_tester.image_size) ) for model_class in self.all_model_classes: snake_case_ :Tuple = True self.check_hidden_states_output(snake_case , snake_case , snake_case , snake_case ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] snake_case_ :List[Any] = True self.check_hidden_states_output(snake_case , snake_case , snake_case , snake_case ) def lowerCAmelCase_ ( self: Optional[Any] ) -> Tuple: snake_case_, snake_case_ :int = self.model_tester.prepare_config_and_inputs_for_common() snake_case_ :List[Any] = 3 snake_case_ :List[Any] = ( self.model_tester.image_size if isinstance(self.model_tester.image_size , collections.abc.Iterable ) else (self.model_tester.image_size, self.model_tester.image_size) ) snake_case_ :Any = ( config.patch_size if isinstance(config.patch_size , collections.abc.Iterable ) else (config.patch_size, config.patch_size) ) snake_case_ :Tuple = image_size[0] + patch_size[0] - (image_size[0] % patch_size[0]) snake_case_ :List[str] = image_size[1] + patch_size[1] - (image_size[1] % patch_size[1]) for model_class in self.all_model_classes: snake_case_ :str = True self.check_hidden_states_output(snake_case , snake_case , snake_case , (padded_height, padded_width) ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] snake_case_ :Any = True self.check_hidden_states_output(snake_case , snake_case , snake_case , (padded_height, padded_width) ) @unittest.skip(reason="""MaskFormerSwin doesn't have pretrained checkpoints""" ) def lowerCAmelCase_ ( self: Union[str, Any] ) -> List[str]: pass @unittest.skip(reason="""This will be fixed once MaskFormerSwin is replaced by native Swin""" ) def lowerCAmelCase_ ( self: List[str] ) -> str: pass @unittest.skip(reason="""This will be fixed once MaskFormerSwin is replaced by native Swin""" ) def lowerCAmelCase_ ( self: str ) -> List[Any]: pass def lowerCAmelCase_ ( self: Union[str, Any] ) -> Optional[Any]: snake_case_, snake_case_ :Dict = self.model_tester.prepare_config_and_inputs_for_common() def set_nan_tensor_to_zero(snake_case: str ): snake_case_ :Optional[int] = 0 return t def check_equivalence(snake_case: List[Any] , snake_case: Union[str, Any] , snake_case: int , snake_case: Tuple={} ): with torch.no_grad(): snake_case_ :List[Any] = model(**snake_case , return_dict=snake_case , **snake_case ) snake_case_ :Any = model(**snake_case , return_dict=snake_case , **snake_case ).to_tuple() def recursive_check(snake_case: List[Any] , snake_case: int ): if isinstance(snake_case , (List, Tuple) ): for tuple_iterable_value, dict_iterable_value in zip(snake_case , snake_case ): recursive_check(snake_case , snake_case ) elif isinstance(snake_case , snake_case ): for tuple_iterable_value, dict_iterable_value in zip( tuple_object.values() , dict_object.values() ): recursive_check(snake_case , snake_case ) elif tuple_object is None: return else: self.assertTrue( torch.allclose( set_nan_tensor_to_zero(snake_case ) , set_nan_tensor_to_zero(snake_case ) , atol=1E-5 ) , msg=( """Tuple and dict output are not equal. Difference:""" f""" {torch.max(torch.abs(tuple_object - dict_object ) )}. Tuple has `nan`:""" f""" {torch.isnan(snake_case ).any()} and `inf`: {torch.isinf(snake_case )}. Dict has""" f""" `nan`: {torch.isnan(snake_case ).any()} and `inf`: {torch.isinf(snake_case )}.""" ) , ) recursive_check(snake_case , snake_case ) for model_class in self.all_model_classes: snake_case_ :int = model_class(snake_case ) model.to(snake_case ) model.eval() snake_case_ :Any = self._prepare_for_class(snake_case , snake_case ) snake_case_ :List[Any] = self._prepare_for_class(snake_case , snake_case ) check_equivalence(snake_case , snake_case , snake_case ) snake_case_ :Tuple = self._prepare_for_class(snake_case , snake_case , return_labels=snake_case ) snake_case_ :Dict = self._prepare_for_class(snake_case , snake_case , return_labels=snake_case ) check_equivalence(snake_case , snake_case , snake_case ) snake_case_ :Tuple = self._prepare_for_class(snake_case , snake_case ) snake_case_ :Any = self._prepare_for_class(snake_case , snake_case ) check_equivalence(snake_case , snake_case , snake_case , {"""output_hidden_states""": True} ) snake_case_ :Dict = self._prepare_for_class(snake_case , snake_case , return_labels=snake_case ) snake_case_ :List[str] = self._prepare_for_class(snake_case , snake_case , return_labels=snake_case ) check_equivalence(snake_case , snake_case , snake_case , {"""output_hidden_states""": True} ) @require_torch class lowerCamelCase ( unittest.TestCase , _lowerCAmelCase ): '''simple docstring''' _A : int = (MaskFormerSwinBackbone,) if is_torch_available() else () _A : Tuple = MaskFormerSwinConfig def lowerCAmelCase_ ( self: List[str] ) -> Optional[int]: snake_case_ :Optional[Any] = MaskFormerSwinModelTester(self ) def lowerCAmelCase_ ( self: int ) -> Optional[int]: snake_case_, snake_case_ :Any = self.model_tester.prepare_config_and_inputs_for_common() snake_case_ :Tuple = inputs_dict["""pixel_values"""].shape[0] for backbone_class in self.all_model_classes: snake_case_ :List[str] = backbone_class(snake_case ) backbone.to(snake_case ) backbone.eval() snake_case_ :List[Any] = backbone(**snake_case ) # Test default outputs and verify feature maps self.assertIsInstance(outputs.feature_maps , snake_case ) self.assertTrue(len(outputs.feature_maps ) == len(backbone.channels ) ) for feature_map, n_channels in zip(outputs.feature_maps , backbone.channels ): self.assertTrue(feature_map.shape[:2] , (batch_size, n_channels) ) self.assertIsNone(outputs.hidden_states ) self.assertIsNone(outputs.attentions ) # Test output_hidden_states=True snake_case_ :Union[str, Any] = backbone(**snake_case , output_hidden_states=snake_case ) self.assertIsNotNone(outputs.hidden_states ) self.assertTrue(len(outputs.hidden_states ) , len(backbone.stage_names ) ) # We skip the stem layer for hidden_states, n_channels in zip(outputs.hidden_states[1:] , backbone.channels ): for hidden_state in hidden_states: # Hidden states are in the format (batch_size, (height * width), n_channels) snake_case_, snake_case_, snake_case_ :List[Any] = hidden_state.shape self.assertTrue((h_batch_size, h_n_channels) , (batch_size, n_channels) ) # Test output_attentions=True if self.has_attentions: snake_case_ :List[Any] = backbone(**snake_case , output_attentions=snake_case ) self.assertIsNotNone(outputs.attentions )
66
# DISCLAIMER: This file is strongly influenced by https://github.com/yang-song/score_sde_pytorch import math from dataclasses import dataclass from typing import Optional, Tuple, Union import torch from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput, randn_tensor from .scheduling_utils import SchedulerMixin, SchedulerOutput @dataclass class __magic_name__ ( lowerCamelCase__ ): """simple docstring""" __UpperCamelCase = 42 __UpperCamelCase = 42 class __magic_name__ ( lowerCamelCase__ , lowerCamelCase__ ): """simple docstring""" __UpperCamelCase = 1 @register_to_config def __init__( self :Union[str, Any] , snake_case :int = 2_000 , snake_case :float = 0.15 , snake_case :float = 0.01 , snake_case :float = 1348.0 , snake_case :float = 1e-5 , snake_case :int = 1 , ): '''simple docstring''' A_ : Dict = sigma_max # setable values A_ : List[Any] = None self.set_sigmas(snake_case , snake_case , snake_case , snake_case ) def SCREAMING_SNAKE_CASE ( self :Any , snake_case :torch.FloatTensor , snake_case :Optional[int] = None ): '''simple docstring''' return sample def SCREAMING_SNAKE_CASE ( self :Optional[Any] , snake_case :int , snake_case :float = None , snake_case :Union[str, torch.device] = None ): '''simple docstring''' A_ : Optional[Any] = sampling_eps if sampling_eps is not None else self.config.sampling_eps A_ : Tuple = torch.linspace(1 , snake_case , snake_case , device=snake_case ) def SCREAMING_SNAKE_CASE ( self :Dict , snake_case :int , snake_case :float = None , snake_case :float = None , snake_case :float = None ): '''simple docstring''' A_ : Union[str, Any] = sigma_min if sigma_min is not None else self.config.sigma_min A_ : Any = sigma_max if sigma_max is not None else self.config.sigma_max A_ : Dict = sampling_eps if sampling_eps is not None else self.config.sampling_eps if self.timesteps is None: self.set_timesteps(snake_case , snake_case ) A_ : str = sigma_min * (sigma_max / sigma_min) ** (self.timesteps / sampling_eps) A_ : Any = torch.exp(torch.linspace(math.log(snake_case ) , math.log(snake_case ) , snake_case ) ) A_ : str = torch.tensor([sigma_min * (sigma_max / sigma_min) ** t for t in self.timesteps] ) def SCREAMING_SNAKE_CASE ( self :List[str] , snake_case :List[str] , snake_case :Dict ): '''simple docstring''' return torch.where( timesteps == 0 , torch.zeros_like(t.to(timesteps.device ) ) , self.discrete_sigmas[timesteps - 1].to(timesteps.device ) , ) def SCREAMING_SNAKE_CASE ( self :Union[str, Any] , snake_case :torch.FloatTensor , snake_case :int , snake_case :torch.FloatTensor , snake_case :Optional[torch.Generator] = None , snake_case :bool = True , ): '''simple docstring''' if self.timesteps is None: raise ValueError( "`self.timesteps` is not set, you need to run 'set_timesteps' after creating the scheduler" ) A_ : int = timestep * torch.ones( sample.shape[0] , device=sample.device ) # torch.repeat_interleave(timestep, sample.shape[0]) A_ : Optional[Any] = (timestep * (len(self.timesteps ) - 1)).long() # mps requires indices to be in the same device, so we use cpu as is the default with cuda A_ : Dict = timesteps.to(self.discrete_sigmas.device ) A_ : Optional[int] = self.discrete_sigmas[timesteps].to(sample.device ) A_ : int = self.get_adjacent_sigma(snake_case , snake_case ).to(sample.device ) A_ : Union[str, Any] = torch.zeros_like(snake_case ) A_ : Tuple = (sigma**2 - adjacent_sigma**2) ** 0.5 # equation 6 in the paper: the model_output modeled by the network is grad_x log pt(x) # also equation 47 shows the analog from SDE models to ancestral sampling methods A_ : Optional[int] = diffusion.flatten() while len(diffusion.shape ) < len(sample.shape ): A_ : Tuple = diffusion.unsqueeze(-1 ) A_ : Optional[Any] = drift - diffusion**2 * model_output # equation 6: sample noise for the diffusion term of A_ : List[Any] = randn_tensor( sample.shape , layout=sample.layout , generator=snake_case , device=sample.device , dtype=sample.dtype ) A_ : List[Any] = sample - drift # subtract because `dt` is a small negative timestep # TODO is the variable diffusion the correct scaling term for the noise? A_ : Any = prev_sample_mean + diffusion * noise # add impact of diffusion field g if not return_dict: return (prev_sample, prev_sample_mean) return SdeVeOutput(prev_sample=snake_case , prev_sample_mean=snake_case ) def SCREAMING_SNAKE_CASE ( self :Tuple , snake_case :torch.FloatTensor , snake_case :torch.FloatTensor , snake_case :Optional[torch.Generator] = None , snake_case :bool = True , ): '''simple docstring''' if self.timesteps is None: raise ValueError( "`self.timesteps` is not set, you need to run 'set_timesteps' after creating the scheduler" ) # For small batch sizes, the paper "suggest replacing norm(z) with sqrt(d), where d is the dim. of z" # sample noise for correction A_ : Dict = randn_tensor(sample.shape , layout=sample.layout , generator=snake_case ).to(sample.device ) # compute step size from the model_output, the noise, and the snr A_ : int = torch.norm(model_output.reshape(model_output.shape[0] , -1 ) , dim=-1 ).mean() A_ : List[Any] = torch.norm(noise.reshape(noise.shape[0] , -1 ) , dim=-1 ).mean() A_ : Dict = (self.config.snr * noise_norm / grad_norm) ** 2 * 2 A_ : Dict = step_size * torch.ones(sample.shape[0] ).to(sample.device ) # self.repeat_scalar(step_size, sample.shape[0]) # compute corrected sample: model_output term and noise term A_ : int = step_size.flatten() while len(step_size.shape ) < len(sample.shape ): A_ : str = step_size.unsqueeze(-1 ) A_ : Optional[Any] = sample + step_size * model_output A_ : Tuple = prev_sample_mean + ((step_size * 2) ** 0.5) * noise if not return_dict: return (prev_sample,) return SchedulerOutput(prev_sample=snake_case ) def SCREAMING_SNAKE_CASE ( self :Tuple , snake_case :torch.FloatTensor , snake_case :torch.FloatTensor , snake_case :torch.FloatTensor , ): '''simple docstring''' A_ : Union[str, Any] = timesteps.to(original_samples.device ) A_ : List[Any] = self.discrete_sigmas.to(original_samples.device )[timesteps] A_ : List[Any] = ( noise * sigmas[:, None, None, None] if noise is not None else torch.randn_like(snake_case ) * sigmas[:, None, None, None] ) A_ : Optional[int] = noise + original_samples return noisy_samples def __len__( self :Union[str, Any] ): '''simple docstring''' return self.config.num_train_timesteps
300
0
'''simple docstring''' # Copyright 2022 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. import argparse import os import platform import numpy as np import psutil import torch from accelerate import __version__ as version from accelerate.commands.config import default_config_file, load_config_from_file from ..utils import is_npu_available, is_xpu_available def __lowerCAmelCase ( UpperCamelCase__=None ) -> Tuple: if subparsers is not None: __lowerCamelCase = subparsers.add_parser('''env''' ) else: __lowerCamelCase = argparse.ArgumentParser('''Accelerate env command''' ) parser.add_argument( '''--config_file''' , default=UpperCamelCase__ , help='''The config file to use for the default values in the launching script.''' ) if subparsers is not None: parser.set_defaults(func=UpperCamelCase__ ) return parser def __lowerCAmelCase ( UpperCamelCase__ ) -> Union[str, Any]: __lowerCamelCase = torch.__version__ __lowerCamelCase = torch.cuda.is_available() __lowerCamelCase = is_xpu_available() __lowerCamelCase = is_npu_available() __lowerCamelCase = '''Not found''' # Get the default from the config file. if args.config_file is not None or os.path.isfile(UpperCamelCase__ ): __lowerCamelCase = load_config_from_file(args.config_file ).to_dict() __lowerCamelCase = { '''`Accelerate` version''': version, '''Platform''': platform.platform(), '''Python version''': platform.python_version(), '''Numpy version''': np.__version__, '''PyTorch version (GPU?)''': f"""{pt_version} ({pt_cuda_available})""", '''PyTorch XPU available''': str(UpperCamelCase__ ), '''PyTorch NPU available''': str(UpperCamelCase__ ), '''System RAM''': f"""{psutil.virtual_memory().total / 10_24 ** 3:.2f} GB""", } if pt_cuda_available: __lowerCamelCase = torch.cuda.get_device_name() print('''\nCopy-and-paste the text below in your GitHub issue\n''' ) print('''\n'''.join([f"""- {prop}: {val}""" for prop, val in info.items()] ) ) print('''- `Accelerate` default config:''' if args.config_file is None else '''- `Accelerate` config passed:''' ) __lowerCamelCase = ( '''\n'''.join([f"""\t- {prop}: {val}""" for prop, val in accelerate_config.items()] ) if isinstance(UpperCamelCase__ , UpperCamelCase__ ) else f"""\t{accelerate_config}""" ) print(UpperCamelCase__ ) __lowerCamelCase = accelerate_config return info def __lowerCAmelCase ( ) -> int: __lowerCamelCase = env_command_parser() __lowerCamelCase = parser.parse_args() env_command(UpperCamelCase__ ) return 0 if __name__ == "__main__": raise SystemExit(main())
67
from __future__ import annotations from decimal import Decimal from math import * # noqa: F403 from sympy import diff def __snake_case ( _lowerCAmelCase : str , _lowerCAmelCase : float | Decimal , _lowerCAmelCase : float = 10**-10 ) -> float: A_ : Dict = a while True: A_ : Union[str, Any] = Decimal(_lowerCAmelCase ) - ( Decimal(eval(_lowerCAmelCase ) ) / Decimal(eval(str(diff(_lowerCAmelCase ) ) ) ) # noqa: S307 ) # This number dictates the accuracy of the answer if abs(eval(_lowerCAmelCase ) ) < precision: # noqa: S307 return float(_lowerCAmelCase ) # Let's Execute if __name__ == "__main__": # Find root of trigonometric function # Find value of pi print(F'''The root of sin(x) = 0 is {newton_raphson("sin(x)", 2)}''') # Find root of polynomial print(F'''The root of x**2 - 5*x + 2 = 0 is {newton_raphson("x**2 - 5*x + 2", 0.4)}''') # Find Square Root of 5 print(F'''The root of log(x) - 1 = 0 is {newton_raphson("log(x) - 1", 2)}''') # Exponential Roots print(F'''The root of exp(x) - 1 = 0 is {newton_raphson("exp(x) - 1", 0)}''')
300
0
import warnings from typing import Dict import numpy as np from ..utils import ExplicitEnum, add_end_docstrings, is_tf_available, is_torch_available from .base import PIPELINE_INIT_ARGS, GenericTensor, Pipeline if is_tf_available(): from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING if is_torch_available(): from ..models.auto.modeling_auto import MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_: int ) -> Any: '''simple docstring''' return 1.0 / (1.0 + np.exp(-_outputs )) def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_: List[Any] ) -> int: '''simple docstring''' A__ = np.max(_outputs , axis=-1 , keepdims=SCREAMING_SNAKE_CASE_ ) A__ = np.exp(_outputs - maxes ) return shifted_exp / shifted_exp.sum(axis=-1 , keepdims=SCREAMING_SNAKE_CASE_ ) class a__ ( snake_case ): """simple docstring""" __lowerCamelCase = 'sigmoid' __lowerCamelCase = 'softmax' __lowerCamelCase = 'none' @add_end_docstrings( snake_case , r'\n return_all_scores (`bool`, *optional*, defaults to `False`):\n Whether to return all prediction scores or just the one of the predicted class.\n function_to_apply (`str`, *optional*, defaults to `"default"`):\n The function to apply to the model outputs in order to retrieve the scores. Accepts four different values:\n\n - `"default"`: if the model has a single label, will apply the sigmoid function on the output. If the model\n has several labels, will apply the softmax function on the output.\n - `"sigmoid"`: Applies the sigmoid function on the output.\n - `"softmax"`: Applies the softmax function on the output.\n - `"none"`: Does not apply any function on the output.\n ' , ) class a__ ( snake_case ): """simple docstring""" __lowerCamelCase = False __lowerCamelCase = ClassificationFunction.NONE def __init__( self , **lowercase ) -> Optional[int]: '''simple docstring''' super().__init__(**lowercase ) self.check_model_type( TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING if self.framework == "tf" else MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING ) def UpperCamelCase ( self , lowercase=None , lowercase=None , lowercase="" , **lowercase ) -> Optional[int]: '''simple docstring''' A__ = tokenizer_kwargs A__ = {} if hasattr(self.model.config , "return_all_scores" ) and return_all_scores is None: A__ = self.model.config.return_all_scores if isinstance(lowercase , lowercase ) or top_k is None: A__ = top_k A__ = False elif return_all_scores is not None: warnings.warn( "`return_all_scores` is now deprecated, if want a similar functionality use `top_k=None` instead of" " `return_all_scores=True` or `top_k=1` instead of `return_all_scores=False`." , lowercase , ) if return_all_scores: A__ = None else: A__ = 1 if isinstance(lowercase , lowercase ): A__ = ClassificationFunction[function_to_apply.upper()] if function_to_apply is not None: A__ = function_to_apply return preprocess_params, {}, postprocess_params def __call__( self , *lowercase , **lowercase ) -> Any: '''simple docstring''' A__ = super().__call__(*lowercase , **lowercase ) # TODO try and retrieve it in a nicer way from _sanitize_parameters. A__ = "top_k" not in kwargs if isinstance(args[0] , lowercase ) and _legacy: # This pipeline is odd, and return a list when single item is run return [result] else: return result def UpperCamelCase ( self , lowercase , **lowercase ) -> Dict[str, GenericTensor]: '''simple docstring''' A__ = self.framework if isinstance(lowercase , lowercase ): return self.tokenizer(**lowercase , return_tensors=lowercase , **lowercase ) elif isinstance(lowercase , lowercase ) and len(lowercase ) == 1 and isinstance(inputs[0] , lowercase ) and len(inputs[0] ) == 2: # It used to be valid to use a list of list of list for text pairs, keeping this path for BC return self.tokenizer( text=inputs[0][0] , text_pair=inputs[0][1] , return_tensors=lowercase , **lowercase ) elif isinstance(lowercase , lowercase ): # This is likely an invalid usage of the pipeline attempting to pass text pairs. raise ValueError( "The pipeline received invalid inputs, if you are trying to send text pairs, you can try to send a" " dictionary `{\"text\": \"My text\", \"text_pair\": \"My pair\"}` in order to send a text pair." ) return self.tokenizer(lowercase , return_tensors=lowercase , **lowercase ) def UpperCamelCase ( self , lowercase ) -> Dict: '''simple docstring''' return self.model(**lowercase ) def UpperCamelCase ( self , lowercase , lowercase=None , lowercase=1 , lowercase=True ) -> Tuple: '''simple docstring''' if function_to_apply is None: if self.model.config.problem_type == "multi_label_classification" or self.model.config.num_labels == 1: A__ = ClassificationFunction.SIGMOID elif self.model.config.problem_type == "single_label_classification" or self.model.config.num_labels > 1: A__ = ClassificationFunction.SOFTMAX elif hasattr(self.model.config , "function_to_apply" ) and function_to_apply is None: A__ = self.model.config.function_to_apply else: A__ = ClassificationFunction.NONE A__ = model_outputs["logits"][0] A__ = outputs.numpy() if function_to_apply == ClassificationFunction.SIGMOID: A__ = sigmoid(lowercase ) elif function_to_apply == ClassificationFunction.SOFTMAX: A__ = softmax(lowercase ) elif function_to_apply == ClassificationFunction.NONE: A__ = outputs else: raise ValueError(F'Unrecognized `function_to_apply` argument: {function_to_apply}' ) if top_k == 1 and _legacy: return {"label": self.model.config.idalabel[scores.argmax().item()], "score": scores.max().item()} A__ = [ {"label": self.model.config.idalabel[i], "score": score.item()} for i, score in enumerate(lowercase ) ] if not _legacy: dict_scores.sort(key=lambda lowercase : x["score"] , reverse=lowercase ) if top_k is not None: A__ = dict_scores[:top_k] return dict_scores
68
import sacrebleu as scb from packaging import version from sacrebleu import TER import datasets _lowerCAmelCase : List[Any] = '''\ @inproceedings{snover-etal-2006-study, title = "A Study of Translation Edit Rate with Targeted Human Annotation", author = "Snover, Matthew and Dorr, Bonnie and Schwartz, Rich and Micciulla, Linnea and Makhoul, John", booktitle = "Proceedings of the 7th Conference of the Association for Machine Translation in the Americas: Technical Papers", month = aug # " 8-12", year = "2006", address = "Cambridge, Massachusetts, USA", publisher = "Association for Machine Translation in the Americas", url = "https://aclanthology.org/2006.amta-papers.25", pages = "223--231", } @inproceedings{post-2018-call, title = "A Call for Clarity in Reporting {BLEU} Scores", author = "Post, Matt", booktitle = "Proceedings of the Third Conference on Machine Translation: Research Papers", month = oct, year = "2018", address = "Belgium, Brussels", publisher = "Association for Computational Linguistics", url = "https://www.aclweb.org/anthology/W18-6319", pages = "186--191", } ''' _lowerCAmelCase : Union[str, Any] = '''\ TER (Translation Edit Rate, also called Translation Error Rate) is a metric to quantify the edit operations that a hypothesis requires to match a reference translation. We use the implementation that is already present in sacrebleu (https://github.com/mjpost/sacreBLEU#ter), which in turn is inspired by the TERCOM implementation, which can be found here: https://github.com/jhclark/tercom. The implementation here is slightly different from sacrebleu in terms of the required input format. The length of the references and hypotheses lists need to be the same, so you may need to transpose your references compared to sacrebleu\'s required input format. See https://github.com/huggingface/datasets/issues/3154#issuecomment-950746534 See the README.md file at https://github.com/mjpost/sacreBLEU#ter for more information. ''' _lowerCAmelCase : Optional[Any] = ''' Produces TER scores alongside the number of edits and reference length. Args: predictions (list of str): The system stream (a sequence of segments). references (list of list of str): A list of one or more reference streams (each a sequence of segments). normalized (boolean): If `True`, applies basic tokenization and normalization to sentences. Defaults to `False`. ignore_punct (boolean): If `True`, applies basic tokenization and normalization to sentences. Defaults to `False`. support_zh_ja_chars (boolean): If `True`, tokenization/normalization supports processing of Chinese characters, as well as Japanese Kanji, Hiragana, Katakana, and Phonetic Extensions of Katakana. Only applies if `normalized = True`. Defaults to `False`. case_sensitive (boolean): If `False`, makes all predictions and references lowercase to ignore differences in case. Defaults to `False`. Returns: \'score\' (float): TER score (num_edits / sum_ref_lengths * 100) \'num_edits\' (int): The cumulative number of edits \'ref_length\' (float): The cumulative average reference length Examples: Example 1: >>> predictions = ["does this sentence match??", ... "what about this sentence?", ... "What did the TER metric user say to the developer?"] >>> references = [["does this sentence match", "does this sentence match!?!"], ... ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"], ... ["Your jokes are...", "...TERrible"]] >>> ter = datasets.load_metric("ter") >>> results = ter.compute(predictions=predictions, ... references=references, ... case_sensitive=True) >>> print(results) {\'score\': 150.0, \'num_edits\': 15, \'ref_length\': 10.0} Example 2: >>> predictions = ["does this sentence match??", ... "what about this sentence?"] >>> references = [["does this sentence match", "does this sentence match!?!"], ... ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"]] >>> ter = datasets.load_metric("ter") >>> results = ter.compute(predictions=predictions, ... references=references, ... case_sensitive=True) >>> print(results) {\'score\': 62.5, \'num_edits\': 5, \'ref_length\': 8.0} Example 3: >>> predictions = ["does this sentence match??", ... "what about this sentence?"] >>> references = [["does this sentence match", "does this sentence match!?!"], ... ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"]] >>> ter = datasets.load_metric("ter") >>> results = ter.compute(predictions=predictions, ... references=references, ... normalized=True, ... case_sensitive=True) >>> print(results) {\'score\': 57.14285714285714, \'num_edits\': 6, \'ref_length\': 10.5} Example 4: >>> predictions = ["does this sentence match??", ... "what about this sentence?"] >>> references = [["does this sentence match", "does this sentence match!?!"], ... ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"]] >>> ter = datasets.load_metric("ter") >>> results = ter.compute(predictions=predictions, ... references=references, ... ignore_punct=True, ... case_sensitive=False) >>> print(results) {\'score\': 0.0, \'num_edits\': 0, \'ref_length\': 8.0} Example 5: >>> predictions = ["does this sentence match??", ... "what about this sentence?", ... "What did the TER metric user say to the developer?"] >>> references = [["does this sentence match", "does this sentence match!?!"], ... ["wHaT aBoUt ThIs SeNtEnCe?", "wHaT aBoUt ThIs SeNtEnCe?"], ... ["Your jokes are...", "...TERrible"]] >>> ter = datasets.load_metric("ter") >>> results = ter.compute(predictions=predictions, ... references=references, ... ignore_punct=True, ... case_sensitive=False) >>> print(results) {\'score\': 100.0, \'num_edits\': 10, \'ref_length\': 10.0} ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __magic_name__ ( datasets.Metric ): """simple docstring""" def SCREAMING_SNAKE_CASE ( self :Dict ): '''simple docstring''' if version.parse(scb.__version__ ) < version.parse("1.4.12" ): raise ImportWarning( "To use `sacrebleu`, the module `sacrebleu>=1.4.12` is required, and the current version of `sacrebleu` doesn't match this condition.\n" "You can install it with `pip install \"sacrebleu>=1.4.12\"`." ) return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , homepage="http://www.cs.umd.edu/~snover/tercom/" , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "predictions": datasets.Value("string" , id="sequence" ), "references": datasets.Sequence(datasets.Value("string" , id="sequence" ) , id="references" ), } ) , codebase_urls=["https://github.com/mjpost/sacreBLEU#ter"] , reference_urls=[ "https://github.com/jhclark/tercom", ] , ) def SCREAMING_SNAKE_CASE ( self :Union[str, Any] , snake_case :Optional[int] , snake_case :List[Any] , snake_case :bool = False , snake_case :bool = False , snake_case :bool = False , snake_case :bool = False , ): '''simple docstring''' A_ : List[str] = len(references[0] ) if any(len(snake_case ) != references_per_prediction for refs in references ): raise ValueError("Sacrebleu requires the same number of references for each prediction" ) A_ : int = [[refs[i] for refs in references] for i in range(snake_case )] A_ : Optional[Any] = TER( normalized=snake_case , no_punct=snake_case , asian_support=snake_case , case_sensitive=snake_case , ) A_ : List[Any] = sb_ter.corpus_score(snake_case , snake_case ) return {"score": output.score, "num_edits": output.num_edits, "ref_length": output.ref_length}
300
0
"""simple docstring""" from __future__ import annotations import numpy as np def UpperCAmelCase ( UpperCAmelCase ) -> tuple[np.ndarray, np.ndarray]: snake_case_ , snake_case_ = np.shape(UpperCAmelCase ) if rows != columns: snake_case_ = ( '\'table\' has to be of square shaped array but got a ' f'{rows}x{columns} array:\n{table}' ) raise ValueError(UpperCAmelCase ) snake_case_ = np.zeros((rows, columns) ) snake_case_ = np.zeros((rows, columns) ) for i in range(UpperCAmelCase ): for j in range(UpperCAmelCase ): snake_case_ = sum(lower[i][k] * upper[k][j] for k in range(UpperCAmelCase ) ) if upper[j][j] == 0: raise ArithmeticError('No LU decomposition exists' ) snake_case_ = (table[i][j] - total) / upper[j][j] snake_case_ = 1 for j in range(UpperCAmelCase , UpperCAmelCase ): snake_case_ = sum(lower[i][k] * upper[k][j] for k in range(UpperCAmelCase ) ) snake_case_ = table[i][j] - total return lower, upper if __name__ == "__main__": import doctest doctest.testmod()
69
def __snake_case ( _lowerCAmelCase : List[str] , _lowerCAmelCase : int ) -> str: return (pointa[0] - pointa[0]) ** 2 + (pointa[1] - pointa[1]) ** 2 def __snake_case ( _lowerCAmelCase : int , _lowerCAmelCase : Union[str, Any]=0 ) -> Any: return sorted(_lowerCAmelCase , key=lambda _lowerCAmelCase : x[column] ) def __snake_case ( _lowerCAmelCase : List[str] , _lowerCAmelCase : Any , _lowerCAmelCase : Any=float("inf" ) ) -> int: for i in range(points_counts - 1 ): for j in range(i + 1 , _lowerCAmelCase ): A_ : Tuple = euclidean_distance_sqr(points[i] , points[j] ) if current_dis < min_dis: A_ : Union[str, Any] = current_dis return min_dis def __snake_case ( _lowerCAmelCase : Optional[int] , _lowerCAmelCase : Dict , _lowerCAmelCase : List[str]=float("inf" ) ) -> Dict: for i in range(min(6 , points_counts - 1 ) , _lowerCAmelCase ): for j in range(max(0 , i - 6 ) , _lowerCAmelCase ): A_ : List[Any] = euclidean_distance_sqr(points[i] , points[j] ) if current_dis < min_dis: A_ : Union[str, Any] = current_dis return min_dis def __snake_case ( _lowerCAmelCase : List[Any] , _lowerCAmelCase : str , _lowerCAmelCase : Dict ) -> List[str]: # base case if points_counts <= 3: return dis_between_closest_pair(_lowerCAmelCase , _lowerCAmelCase ) # recursion A_ : Optional[int] = points_counts // 2 A_ : List[Any] = closest_pair_of_points_sqr( _lowerCAmelCase , points_sorted_on_y[:mid] , _lowerCAmelCase ) A_ : List[Any] = closest_pair_of_points_sqr( _lowerCAmelCase , points_sorted_on_y[mid:] , points_counts - mid ) A_ : Tuple = min(_lowerCAmelCase , _lowerCAmelCase ) A_ : Dict = [] for point in points_sorted_on_x: if abs(point[0] - points_sorted_on_x[mid][0] ) < closest_pair_dis: cross_strip.append(_lowerCAmelCase ) A_ : Tuple = dis_between_closest_in_strip( _lowerCAmelCase , len(_lowerCAmelCase ) , _lowerCAmelCase ) return min(_lowerCAmelCase , _lowerCAmelCase ) def __snake_case ( _lowerCAmelCase : str , _lowerCAmelCase : Optional[Any] ) -> Any: A_ : Optional[Any] = column_based_sort(_lowerCAmelCase , column=0 ) A_ : Optional[int] = column_based_sort(_lowerCAmelCase , column=1 ) return ( closest_pair_of_points_sqr( _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) ) ** 0.5 if __name__ == "__main__": _lowerCAmelCase : List[Any] = [(2, 3), (12, 30), (40, 50), (5, 1), (12, 10), (3, 4)] print('''Distance:''', closest_pair_of_points(points, len(points)))
300
0
'''simple docstring''' def UpperCamelCase__ ( lowerCAmelCase = 10 , lowerCAmelCase = 22 ): """simple docstring""" _lowerCAmelCase = range(1 , lowerCAmelCase ) _lowerCAmelCase = range(1 , lowerCAmelCase ) return sum( 1 for power in powers for base in bases if len(str(base**power ) ) == power ) if __name__ == "__main__": print(F"""{solution(10, 22) = }""")
70
import inspect import unittest from transformers import ViTMSNConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTMSNForImageClassification, ViTMSNModel from transformers.models.vit_msn.modeling_vit_msn import VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class __magic_name__ : """simple docstring""" def __init__( self :Dict , snake_case :Optional[int] , snake_case :Tuple=13 , snake_case :List[Any]=30 , snake_case :Union[str, Any]=2 , snake_case :List[Any]=3 , snake_case :Tuple=True , snake_case :Dict=True , snake_case :Dict=32 , snake_case :List[str]=5 , snake_case :Optional[Any]=4 , snake_case :Any=37 , snake_case :Dict="gelu" , snake_case :List[str]=0.1 , snake_case :str=0.1 , snake_case :Tuple=10 , snake_case :str=0.02 , snake_case :Optional[Any]=None , ): '''simple docstring''' A_ : Tuple = parent A_ : int = batch_size A_ : List[str] = image_size A_ : List[Any] = patch_size A_ : Optional[Any] = num_channels A_ : List[Any] = is_training A_ : Tuple = use_labels A_ : Union[str, Any] = hidden_size A_ : Tuple = num_hidden_layers A_ : Any = num_attention_heads A_ : List[str] = intermediate_size A_ : Optional[int] = hidden_act A_ : List[str] = hidden_dropout_prob A_ : str = attention_probs_dropout_prob A_ : Any = type_sequence_label_size A_ : List[str] = initializer_range A_ : Dict = scope # in ViT MSN, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) A_ : Optional[int] = (image_size // patch_size) ** 2 A_ : List[str] = num_patches + 1 def SCREAMING_SNAKE_CASE ( self :Optional[Any] ): '''simple docstring''' A_ : Any = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) A_ : Tuple = None if self.use_labels: A_ : Dict = ids_tensor([self.batch_size] , self.type_sequence_label_size ) A_ : Dict = self.get_config() return config, pixel_values, labels def SCREAMING_SNAKE_CASE ( self :Union[str, Any] ): '''simple docstring''' return ViTMSNConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , initializer_range=self.initializer_range , ) def SCREAMING_SNAKE_CASE ( self :List[Any] , snake_case :List[Any] , snake_case :str , snake_case :Tuple ): '''simple docstring''' A_ : Optional[Any] = ViTMSNModel(config=snake_case ) model.to(snake_case ) model.eval() A_ : int = model(snake_case ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def SCREAMING_SNAKE_CASE ( self :int , snake_case :Optional[int] , snake_case :List[str] , snake_case :List[str] ): '''simple docstring''' A_ : Dict = self.type_sequence_label_size A_ : Tuple = ViTMSNForImageClassification(snake_case ) model.to(snake_case ) model.eval() A_ : Union[str, Any] = model(snake_case , labels=snake_case ) print("Pixel and labels shape: {pixel_values.shape}, {labels.shape}" ) print("Labels: {labels}" ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images A_ : Union[str, Any] = 1 A_ : int = ViTMSNForImageClassification(snake_case ) model.to(snake_case ) model.eval() A_ : Dict = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) A_ : Optional[Any] = model(snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def SCREAMING_SNAKE_CASE ( self :List[Any] ): '''simple docstring''' A_ : List[str] = self.prepare_config_and_inputs() A_ , A_ , A_ : Optional[int] = config_and_inputs A_ : Dict = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class __magic_name__ ( lowerCamelCase__ , lowerCamelCase__ , unittest.TestCase ): """simple docstring""" __UpperCamelCase = (ViTMSNModel, ViTMSNForImageClassification) if is_torch_available() else () __UpperCamelCase = ( {'''feature-extraction''': ViTMSNModel, '''image-classification''': ViTMSNForImageClassification} if is_torch_available() else {} ) __UpperCamelCase = False __UpperCamelCase = False __UpperCamelCase = False __UpperCamelCase = False def SCREAMING_SNAKE_CASE ( self :Union[str, Any] ): '''simple docstring''' A_ : Tuple = ViTMSNModelTester(self ) A_ : str = ConfigTester(self , config_class=snake_case , has_text_modality=snake_case , hidden_size=37 ) def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' self.config_tester.run_common_tests() @unittest.skip(reason="ViTMSN does not use inputs_embeds" ) def SCREAMING_SNAKE_CASE ( self :Dict ): '''simple docstring''' pass def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' A_ , A_ : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A_ : Optional[int] = model_class(snake_case ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) A_ : Optional[int] = model.get_output_embeddings() self.assertTrue(x is None or isinstance(snake_case , nn.Linear ) ) def SCREAMING_SNAKE_CASE ( self :Optional[Any] ): '''simple docstring''' A_ , A_ : str = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A_ : Optional[Any] = model_class(snake_case ) A_ : Any = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic A_ : List[str] = [*signature.parameters.keys()] A_ : List[Any] = ["pixel_values"] self.assertListEqual(arg_names[:1] , snake_case ) def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' A_ : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case ) def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' A_ : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*snake_case ) @slow def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' for model_name in VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A_ : Optional[Any] = ViTMSNModel.from_pretrained(snake_case ) self.assertIsNotNone(snake_case ) def __snake_case ( ) -> Optional[Any]: A_ : Optional[Any] = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_torch @require_vision class __magic_name__ ( unittest.TestCase ): """simple docstring""" @cached_property def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' return ViTImageProcessor.from_pretrained("facebook/vit-msn-small" ) if is_vision_available() else None @slow def SCREAMING_SNAKE_CASE ( self :List[Any] ): '''simple docstring''' torch.manual_seed(2 ) A_ : Any = ViTMSNForImageClassification.from_pretrained("facebook/vit-msn-small" ).to(snake_case ) A_ : List[str] = self.default_image_processor A_ : int = prepare_img() A_ : List[str] = image_processor(images=snake_case , return_tensors="pt" ).to(snake_case ) # forward pass with torch.no_grad(): A_ : Optional[int] = model(**snake_case ) # verify the logits A_ : List[Any] = torch.Size((1, 1_000) ) self.assertEqual(outputs.logits.shape , snake_case ) A_ : int = torch.tensor([-0.0803, -0.4454, -0.2375] ).to(snake_case ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , snake_case , atol=1e-4 ) )
300
0
from __future__ import annotations from math import pi # Define the Reduced Planck Constant ℏ (H bar), speed of light C, value of # Pi and the function A_ :List[str] = 1.0_5457_1817E-34 # unit of ℏ : J * s A_ :int = 3E8 # unit of c : m * s^-1 def A ( a_ ,a_ ,a_ ) -> dict[str, float]: if (force, area, distance).count(0 ) != 1: raise ValueError('One and only one argument must be 0' ) if force < 0: raise ValueError('Magnitude of force can not be negative' ) if distance < 0: raise ValueError('Distance can not be negative' ) if area < 0: raise ValueError('Area can not be negative' ) if force == 0: __UpperCamelCase : Optional[int] =(REDUCED_PLANCK_CONSTANT * SPEED_OF_LIGHT * pi**2 * area) / ( 240 * (distance) ** 4 ) return {"force": force} elif area == 0: __UpperCamelCase : str =(240 * force * (distance) ** 4) / ( REDUCED_PLANCK_CONSTANT * SPEED_OF_LIGHT * pi**2 ) return {"area": area} elif distance == 0: __UpperCamelCase : List[str] =( (REDUCED_PLANCK_CONSTANT * SPEED_OF_LIGHT * pi**2 * area) / (240 * force) ) ** (1 / 4) return {"distance": distance} raise ValueError('One and only one argument must be 0' ) # Run doctest if __name__ == "__main__": import doctest doctest.testmod()
71
import torch from diffusers import DDPMScheduler from .test_schedulers import SchedulerCommonTest class __magic_name__ ( lowerCamelCase__ ): """simple docstring""" __UpperCamelCase = (DDPMScheduler,) def SCREAMING_SNAKE_CASE ( self :Union[str, Any] , **snake_case :str ): '''simple docstring''' A_ : Dict = { "num_train_timesteps": 1_000, "beta_start": 0.0001, "beta_end": 0.02, "beta_schedule": "linear", "variance_type": "fixed_small", "clip_sample": True, } config.update(**snake_case ) return config def SCREAMING_SNAKE_CASE ( self :int ): '''simple docstring''' for timesteps in [1, 5, 100, 1_000]: self.check_over_configs(num_train_timesteps=snake_case ) def SCREAMING_SNAKE_CASE ( self :Optional[Any] ): '''simple docstring''' for beta_start, beta_end in zip([0.0001, 0.001, 0.01, 0.1] , [0.002, 0.02, 0.2, 2] ): self.check_over_configs(beta_start=snake_case , beta_end=snake_case ) def SCREAMING_SNAKE_CASE ( self :int ): '''simple docstring''' for schedule in ["linear", "squaredcos_cap_v2"]: self.check_over_configs(beta_schedule=snake_case ) def SCREAMING_SNAKE_CASE ( self :List[Any] ): '''simple docstring''' for variance in ["fixed_small", "fixed_large", "other"]: self.check_over_configs(variance_type=snake_case ) def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' for clip_sample in [True, False]: self.check_over_configs(clip_sample=snake_case ) def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' self.check_over_configs(thresholding=snake_case ) for threshold in [0.5, 1.0, 2.0]: for prediction_type in ["epsilon", "sample", "v_prediction"]: self.check_over_configs( thresholding=snake_case , prediction_type=snake_case , sample_max_value=snake_case , ) def SCREAMING_SNAKE_CASE ( self :Optional[int] ): '''simple docstring''' for prediction_type in ["epsilon", "sample", "v_prediction"]: self.check_over_configs(prediction_type=snake_case ) def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' for t in [0, 500, 999]: self.check_over_forward(time_step=snake_case ) def SCREAMING_SNAKE_CASE ( self :Optional[Any] ): '''simple docstring''' A_ : Tuple = self.scheduler_classes[0] A_ : List[str] = self.get_scheduler_config() A_ : List[str] = scheduler_class(**snake_case ) assert torch.sum(torch.abs(scheduler._get_variance(0 ) - 0.0 ) ) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(487 ) - 0.00979 ) ) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(999 ) - 0.02 ) ) < 1e-5 def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' A_ : int = self.scheduler_classes[0] A_ : List[str] = self.get_scheduler_config() A_ : int = scheduler_class(**snake_case ) A_ : Tuple = len(snake_case ) A_ : List[str] = self.dummy_model() A_ : Optional[Any] = self.dummy_sample_deter A_ : List[str] = torch.manual_seed(0 ) for t in reversed(range(snake_case ) ): # 1. predict noise residual A_ : Tuple = model(snake_case , snake_case ) # 2. predict previous mean of sample x_t-1 A_ : Dict = scheduler.step(snake_case , snake_case , snake_case , generator=snake_case ).prev_sample # if t > 0: # noise = self.dummy_sample_deter # variance = scheduler.get_variance(t) ** (0.5) * noise # # sample = pred_prev_sample + variance A_ : Optional[int] = pred_prev_sample A_ : Tuple = torch.sum(torch.abs(snake_case ) ) A_ : str = torch.mean(torch.abs(snake_case ) ) assert abs(result_sum.item() - 258.9606 ) < 1e-2 assert abs(result_mean.item() - 0.3372 ) < 1e-3 def SCREAMING_SNAKE_CASE ( self :Dict ): '''simple docstring''' A_ : Optional[int] = self.scheduler_classes[0] A_ : int = self.get_scheduler_config(prediction_type="v_prediction" ) A_ : List[str] = scheduler_class(**snake_case ) A_ : int = len(snake_case ) A_ : Dict = self.dummy_model() A_ : str = self.dummy_sample_deter A_ : Any = torch.manual_seed(0 ) for t in reversed(range(snake_case ) ): # 1. predict noise residual A_ : Optional[int] = model(snake_case , snake_case ) # 2. predict previous mean of sample x_t-1 A_ : Tuple = scheduler.step(snake_case , snake_case , snake_case , generator=snake_case ).prev_sample # if t > 0: # noise = self.dummy_sample_deter # variance = scheduler.get_variance(t) ** (0.5) * noise # # sample = pred_prev_sample + variance A_ : List[str] = pred_prev_sample A_ : Optional[Any] = torch.sum(torch.abs(snake_case ) ) A_ : List[str] = torch.mean(torch.abs(snake_case ) ) assert abs(result_sum.item() - 202.0296 ) < 1e-2 assert abs(result_mean.item() - 0.2631 ) < 1e-3 def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' A_ : str = self.scheduler_classes[0] A_ : Optional[Any] = self.get_scheduler_config() A_ : Dict = scheduler_class(**snake_case ) A_ : Optional[int] = [100, 87, 50, 1, 0] scheduler.set_timesteps(timesteps=snake_case ) A_ : Optional[int] = scheduler.timesteps for i, timestep in enumerate(snake_case ): if i == len(snake_case ) - 1: A_ : str = -1 else: A_ : List[str] = timesteps[i + 1] A_ : Optional[int] = scheduler.previous_timestep(snake_case ) A_ : List[str] = prev_t.item() self.assertEqual(snake_case , snake_case ) def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' A_ : Optional[Any] = self.scheduler_classes[0] A_ : int = self.get_scheduler_config() A_ : Tuple = scheduler_class(**snake_case ) A_ : List[str] = [100, 87, 50, 51, 0] with self.assertRaises(snake_case , msg="`custom_timesteps` must be in descending order." ): scheduler.set_timesteps(timesteps=snake_case ) def SCREAMING_SNAKE_CASE ( self :List[Any] ): '''simple docstring''' A_ : Any = self.scheduler_classes[0] A_ : Union[str, Any] = self.get_scheduler_config() A_ : Optional[int] = scheduler_class(**snake_case ) A_ : Union[str, Any] = [100, 87, 50, 1, 0] A_ : Optional[int] = len(snake_case ) with self.assertRaises(snake_case , msg="Can only pass one of `num_inference_steps` or `custom_timesteps`." ): scheduler.set_timesteps(num_inference_steps=snake_case , timesteps=snake_case ) def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' A_ : Union[str, Any] = self.scheduler_classes[0] A_ : Optional[Any] = self.get_scheduler_config() A_ : Optional[int] = scheduler_class(**snake_case ) A_ : Optional[int] = [scheduler.config.num_train_timesteps] with self.assertRaises( snake_case , msg="`timesteps` must start before `self.config.train_timesteps`: {scheduler.config.num_train_timesteps}}" , ): scheduler.set_timesteps(timesteps=snake_case )
300
0
"""simple docstring""" import os from pathlib import Path import numpy as np import pytest from pack_dataset import pack_data_dir from parameterized import parameterized from save_len_file import save_len_file from torch.utils.data import DataLoader from transformers import AutoTokenizer from transformers.models.mbart.modeling_mbart import shift_tokens_right from transformers.testing_utils import TestCasePlus, slow from utils import FAIRSEQ_AVAILABLE, DistributedSortishSampler, LegacySeqaSeqDataset, SeqaSeqDataset lowerCAmelCase__ = '''bert-base-cased''' lowerCAmelCase__ = '''google/pegasus-xsum''' lowerCAmelCase__ = [''' Sam ate lunch today.''', '''Sams lunch ingredients.'''] lowerCAmelCase__ = ['''A very interesting story about what I ate for lunch.''', '''Avocado, celery, turkey, coffee'''] lowerCAmelCase__ = '''patrickvonplaten/t5-tiny-random''' lowerCAmelCase__ = '''sshleifer/bart-tiny-random''' lowerCAmelCase__ = '''sshleifer/tiny-mbart''' lowerCAmelCase__ = '''sshleifer/tiny-marian-en-de''' def snake_case_ ( A_ : Path, A_ : list ): '''simple docstring''' _lowerCamelCase : List[Any] = '''\n'''.join(A_ ) Path(A_ ).open('''w''' ).writelines(A_ ) def snake_case_ ( A_ : Tuple ): '''simple docstring''' for split in ["train", "val", "test"]: _dump_articles(os.path.join(A_, F'''{split}.source''' ), A_ ) _dump_articles(os.path.join(A_, F'''{split}.target''' ), A_ ) return tmp_dir class __snake_case ( _lowercase): @parameterized.expand( [ MBART_TINY, MARIAN_TINY, T5_TINY, BART_TINY, PEGASUS_XSUM, ] , ) @slow def SCREAMING_SNAKE_CASE ( self : Optional[Any] , __lowerCAmelCase : Optional[int] ): """simple docstring""" _lowerCamelCase : Any = AutoTokenizer.from_pretrained(__lowerCAmelCase ) _lowerCamelCase : Union[str, Any] = make_test_data_dir(tmp_dir=self.get_auto_remove_tmp_dir() ) _lowerCamelCase : str = max(len(tokenizer.encode(__lowerCAmelCase ) ) for a in ARTICLES ) _lowerCamelCase : List[Any] = max(len(tokenizer.encode(__lowerCAmelCase ) ) for a in SUMMARIES ) _lowerCamelCase : Union[str, Any] = 4 _lowerCamelCase : Dict = 8 assert max_len_target > max_src_len # Will be truncated assert max_len_source > max_src_len # Will be truncated _lowerCamelCase , _lowerCamelCase : Tuple = '''ro_RO''', '''de_DE''' # ignored for all but mbart, but never causes error. _lowerCamelCase : int = SeqaSeqDataset( __lowerCAmelCase , data_dir=__lowerCAmelCase , type_path='''train''' , max_source_length=__lowerCAmelCase , max_target_length=__lowerCAmelCase , src_lang=__lowerCAmelCase , tgt_lang=__lowerCAmelCase , ) _lowerCamelCase : Any = DataLoader(__lowerCAmelCase , batch_size=2 , collate_fn=train_dataset.collate_fn ) for batch in dataloader: assert isinstance(__lowerCAmelCase , __lowerCAmelCase ) assert batch["attention_mask"].shape == batch["input_ids"].shape # show that articles were trimmed. assert batch["input_ids"].shape[1] == max_src_len # show that targets are the same len assert batch["labels"].shape[1] == max_tgt_len if tok_name != MBART_TINY: continue # check language codes in correct place _lowerCamelCase : List[Any] = shift_tokens_right(batch['''labels'''] , tokenizer.pad_token_id ) assert batch["decoder_input_ids"][0, 0].item() == tokenizer.lang_code_to_id[tgt_lang] assert batch["decoder_input_ids"][0, -1].item() == tokenizer.eos_token_id assert batch["input_ids"][0, -2].item() == tokenizer.eos_token_id assert batch["input_ids"][0, -1].item() == tokenizer.lang_code_to_id[src_lang] break # No need to test every batch @parameterized.expand([BART_TINY, BERT_BASE_CASED] ) def SCREAMING_SNAKE_CASE ( self : int , __lowerCAmelCase : Tuple ): """simple docstring""" _lowerCamelCase : Union[str, Any] = AutoTokenizer.from_pretrained(__lowerCAmelCase ) _lowerCamelCase : List[Any] = make_test_data_dir(tmp_dir=self.get_auto_remove_tmp_dir() ) _lowerCamelCase : List[str] = max(len(tokenizer.encode(__lowerCAmelCase ) ) for a in ARTICLES ) _lowerCamelCase : Any = max(len(tokenizer.encode(__lowerCAmelCase ) ) for a in SUMMARIES ) _lowerCamelCase : List[str] = 4 _lowerCamelCase : Tuple = LegacySeqaSeqDataset( __lowerCAmelCase , data_dir=__lowerCAmelCase , type_path='''train''' , max_source_length=2_0 , max_target_length=__lowerCAmelCase , ) _lowerCamelCase : Dict = DataLoader(__lowerCAmelCase , batch_size=2 , collate_fn=train_dataset.collate_fn ) for batch in dataloader: assert batch["attention_mask"].shape == batch["input_ids"].shape # show that articles were trimmed. assert batch["input_ids"].shape[1] == max_len_source assert 2_0 >= batch["input_ids"].shape[1] # trimmed significantly # show that targets were truncated assert batch["labels"].shape[1] == trunc_target # Truncated assert max_len_target > trunc_target # Truncated break # No need to test every batch def SCREAMING_SNAKE_CASE ( self : Optional[Any] ): """simple docstring""" _lowerCamelCase : Dict = AutoTokenizer.from_pretrained('''facebook/mbart-large-cc25''' ) _lowerCamelCase : int = Path(make_test_data_dir(tmp_dir=self.get_auto_remove_tmp_dir() ) ) _lowerCamelCase : str = tmp_dir.joinpath('''train.source''' ).open().readlines() _lowerCamelCase : Union[str, Any] = Path(make_test_data_dir(tmp_dir=self.get_auto_remove_tmp_dir() ) ) pack_data_dir(__lowerCAmelCase , __lowerCAmelCase , 1_2_8 , __lowerCAmelCase ) _lowerCamelCase : Optional[int] = {x.name for x in tmp_dir.iterdir()} _lowerCamelCase : Dict = {x.name for x in save_dir.iterdir()} _lowerCamelCase : Optional[Any] = save_dir.joinpath('''train.source''' ).open().readlines() # orig: [' Sam ate lunch today.\n', 'Sams lunch ingredients.'] # desired_packed: [' Sam ate lunch today.\n Sams lunch ingredients.'] assert len(__lowerCAmelCase ) < len(__lowerCAmelCase ) assert len(__lowerCAmelCase ) == 1 assert len(packed_examples[0] ) == sum(len(__lowerCAmelCase ) for x in orig_examples ) assert orig_paths == new_paths @pytest.mark.skipif(not FAIRSEQ_AVAILABLE , reason='''This test requires fairseq''' ) def SCREAMING_SNAKE_CASE ( self : Dict ): """simple docstring""" if not FAIRSEQ_AVAILABLE: return _lowerCamelCase , _lowerCamelCase , _lowerCamelCase : List[Any] = self._get_dataset(max_len=6_4 ) _lowerCamelCase : Any = 6_4 _lowerCamelCase : str = ds.make_dynamic_sampler(__lowerCAmelCase , required_batch_size_multiple=__lowerCAmelCase ) _lowerCamelCase : List[str] = [len(__lowerCAmelCase ) for x in batch_sampler] assert len(set(__lowerCAmelCase ) ) > 1 # it's not dynamic batch size if every batch is the same length assert sum(__lowerCAmelCase ) == len(__lowerCAmelCase ) # no dropped or added examples _lowerCamelCase : Optional[int] = DataLoader(__lowerCAmelCase , batch_sampler=__lowerCAmelCase , collate_fn=ds.collate_fn , num_workers=2 ) _lowerCamelCase : Optional[Any] = [] _lowerCamelCase : List[Any] = [] for batch in data_loader: _lowerCamelCase : Union[str, Any] = batch['''input_ids'''].shape _lowerCamelCase : Optional[Any] = src_shape[0] assert bs % required_batch_size_multiple == 0 or bs < required_batch_size_multiple _lowerCamelCase : str = np.product(batch['''input_ids'''].shape ) num_src_per_batch.append(__lowerCAmelCase ) if num_src_tokens > (max_tokens * 1.1): failures.append(__lowerCAmelCase ) assert num_src_per_batch[0] == max(__lowerCAmelCase ) if failures: raise AssertionError(f'''too many tokens in {len(__lowerCAmelCase )} batches''' ) def SCREAMING_SNAKE_CASE ( self : int ): """simple docstring""" _lowerCamelCase , _lowerCamelCase , _lowerCamelCase : List[str] = self._get_dataset(max_len=5_1_2 ) _lowerCamelCase : Optional[int] = 2 _lowerCamelCase : List[Any] = ds.make_sortish_sampler(__lowerCAmelCase , shuffle=__lowerCAmelCase ) _lowerCamelCase : Optional[int] = DataLoader(__lowerCAmelCase , batch_size=__lowerCAmelCase , collate_fn=ds.collate_fn , num_workers=2 ) _lowerCamelCase : Optional[int] = DataLoader(__lowerCAmelCase , batch_size=__lowerCAmelCase , collate_fn=ds.collate_fn , num_workers=2 , sampler=__lowerCAmelCase ) _lowerCamelCase : Tuple = tokenizer.pad_token_id def count_pad_tokens(__lowerCAmelCase : Dict , __lowerCAmelCase : str="input_ids" ): return [batch[k].eq(__lowerCAmelCase ).sum().item() for batch in data_loader] assert sum(count_pad_tokens(__lowerCAmelCase , k='''labels''' ) ) < sum(count_pad_tokens(__lowerCAmelCase , k='''labels''' ) ) assert sum(count_pad_tokens(__lowerCAmelCase ) ) < sum(count_pad_tokens(__lowerCAmelCase ) ) assert len(__lowerCAmelCase ) == len(__lowerCAmelCase ) def SCREAMING_SNAKE_CASE ( self : str , __lowerCAmelCase : List[Any]=1_0_0_0 , __lowerCAmelCase : Dict=1_2_8 ): """simple docstring""" if os.getenv('''USE_REAL_DATA''' , __lowerCAmelCase ): _lowerCamelCase : Union[str, Any] = '''examples/seq2seq/wmt_en_ro''' _lowerCamelCase : Dict = max_len * 2 * 6_4 if not Path(__lowerCAmelCase ).joinpath('''train.len''' ).exists(): save_len_file(__lowerCAmelCase , __lowerCAmelCase ) else: _lowerCamelCase : int = '''examples/seq2seq/test_data/wmt_en_ro''' _lowerCamelCase : Optional[int] = max_len * 4 save_len_file(__lowerCAmelCase , __lowerCAmelCase ) _lowerCamelCase : Optional[Any] = AutoTokenizer.from_pretrained(__lowerCAmelCase ) _lowerCamelCase : Dict = SeqaSeqDataset( __lowerCAmelCase , data_dir=__lowerCAmelCase , type_path='''train''' , max_source_length=__lowerCAmelCase , max_target_length=__lowerCAmelCase , n_obs=__lowerCAmelCase , ) return ds, max_tokens, tokenizer def SCREAMING_SNAKE_CASE ( self : int ): """simple docstring""" _lowerCamelCase , _lowerCamelCase , _lowerCamelCase : Dict = self._get_dataset() _lowerCamelCase : Optional[int] = set(DistributedSortishSampler(__lowerCAmelCase , 2_5_6 , num_replicas=2 , rank=0 , add_extra_examples=__lowerCAmelCase ) ) _lowerCamelCase : Tuple = set(DistributedSortishSampler(__lowerCAmelCase , 2_5_6 , num_replicas=2 , rank=1 , add_extra_examples=__lowerCAmelCase ) ) assert idsa.intersection(__lowerCAmelCase ) == set() @parameterized.expand( [ MBART_TINY, MARIAN_TINY, T5_TINY, BART_TINY, PEGASUS_XSUM, ] , ) def SCREAMING_SNAKE_CASE ( self : Dict , __lowerCAmelCase : Optional[Any] ): """simple docstring""" _lowerCamelCase : Any = AutoTokenizer.from_pretrained(__lowerCAmelCase , use_fast=__lowerCAmelCase ) if tok_name == MBART_TINY: _lowerCamelCase : str = SeqaSeqDataset( __lowerCAmelCase , data_dir=make_test_data_dir(tmp_dir=self.get_auto_remove_tmp_dir() ) , type_path='''train''' , max_source_length=4 , max_target_length=8 , src_lang='''EN''' , tgt_lang='''FR''' , ) _lowerCamelCase : Union[str, Any] = train_dataset.dataset_kwargs assert "src_lang" in kwargs and "tgt_lang" in kwargs else: _lowerCamelCase : str = SeqaSeqDataset( __lowerCAmelCase , data_dir=make_test_data_dir(tmp_dir=self.get_auto_remove_tmp_dir() ) , type_path='''train''' , max_source_length=4 , max_target_length=8 , ) _lowerCamelCase : Optional[int] = train_dataset.dataset_kwargs assert "add_prefix_space" not in kwargs if tok_name != BART_TINY else "add_prefix_space" in kwargs assert len(__lowerCAmelCase ) == 1 if tok_name == BART_TINY else len(__lowerCAmelCase ) == 0
72
import argparse import json import os import fairseq import torch from torch import nn from transformers import ( SpeechaTextaConfig, SpeechaTextaForCausalLM, SpeechaTextaTokenizer, SpeechEncoderDecoderConfig, SpeechEncoderDecoderModel, WavaVecaConfig, WavaVecaFeatureExtractor, WavaVecaModel, logging, ) logging.set_verbosity_info() _lowerCAmelCase : List[str] = logging.get_logger(__name__) _lowerCAmelCase : Optional[int] = { '''post_extract_proj''': '''feature_projection.projection''', '''encoder.pos_conv.0''': '''encoder.pos_conv_embed.conv''', '''self_attn.k_proj''': '''encoder.layers.*.attention.k_proj''', '''self_attn.v_proj''': '''encoder.layers.*.attention.v_proj''', '''self_attn.q_proj''': '''encoder.layers.*.attention.q_proj''', '''self_attn.out_proj''': '''encoder.layers.*.attention.out_proj''', '''self_attn_layer_norm''': '''encoder.layers.*.layer_norm''', '''fc1''': '''encoder.layers.*.feed_forward.intermediate_dense''', '''fc2''': '''encoder.layers.*.feed_forward.output_dense''', '''final_layer_norm''': '''encoder.layers.*.final_layer_norm''', '''encoder.layer_norm''': '''encoder.layer_norm''', '''w2v_model.layer_norm''': '''feature_projection.layer_norm''', '''quantizer.weight_proj''': '''quantizer.weight_proj''', '''quantizer.vars''': '''quantizer.codevectors''', '''project_q''': '''project_q''', '''final_proj''': '''project_hid''', '''w2v_encoder.proj''': '''lm_head''', '''mask_emb''': '''masked_spec_embed''', } _lowerCAmelCase : int = [ '''lm_head''', '''quantizer.weight_proj''', '''quantizer.codevectors''', '''project_q''', '''project_hid''', ] def __snake_case ( _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : List[Any] , _lowerCAmelCase : Any , _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : int ) -> List[Any]: for attribute in key.split("." ): A_ : List[Any] = getattr(_lowerCAmelCase , _lowerCAmelCase ) if weight_type is not None: A_ : List[Any] = getattr(_lowerCAmelCase , _lowerCAmelCase ).shape else: A_ : Tuple = hf_pointer.shape assert hf_shape == value.shape, ( f"Shape of hf {key + '.' + weight_type if weight_type is not None else ''} is {hf_shape}, but should be" f" {value.shape} for {full_name}" ) if weight_type == "weight": A_ : Optional[int] = value elif weight_type == "weight_g": A_ : Optional[int] = value elif weight_type == "weight_v": A_ : Any = value elif weight_type == "bias": A_ : str = value else: A_ : Any = value logger.info(f"{key + '.' + weight_type if weight_type is not None else ''} was initialized from {full_name}." ) def __snake_case ( _lowerCAmelCase : Optional[Any] , _lowerCAmelCase : Dict ) -> List[str]: A_ : Optional[Any] = [] A_ : Any = fairseq_model.state_dict() A_ : Union[str, Any] = hf_model.feature_extractor # if encoder has different dim to decoder -> use proj_weight A_ : str = None for name, value in fairseq_dict.items(): A_ : Tuple = False if "conv_layers" in name: load_conv_layer( _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , hf_model.config.feat_extract_norm == "group" , ) A_ : Optional[Any] = True elif name.split("." )[0] == "proj": A_ : Dict = fairseq_model.proj A_ : List[Any] = True else: for key, mapped_key in MAPPING.items(): if key in name or key.split("w2v_model." )[-1] == name.split("." )[0]: A_ : int = True if "*" in mapped_key: A_ : Optional[Any] = name.split(_lowerCAmelCase )[0].split("." )[-2] A_ : int = mapped_key.replace("*" , _lowerCAmelCase ) if "weight_g" in name: A_ : List[Any] = "weight_g" elif "weight_v" in name: A_ : List[Any] = "weight_v" elif "bias" in name: A_ : Dict = "bias" elif "weight" in name: A_ : List[Any] = "weight" else: A_ : Dict = None set_recursively(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) continue if not is_used: unused_weights.append(_lowerCAmelCase ) logger.warning(f"Unused weights: {unused_weights}" ) return proj_weight def __snake_case ( _lowerCAmelCase : Optional[Any] , _lowerCAmelCase : Tuple , _lowerCAmelCase : List[Any] , _lowerCAmelCase : List[Any] , _lowerCAmelCase : Optional[int] ) -> str: A_ : Any = full_name.split("conv_layers." )[-1] A_ : Optional[int] = name.split("." ) A_ : Optional[Any] = int(items[0] ) A_ : Union[str, Any] = int(items[1] ) if type_id == 0: if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, ( f"{full_name} has size {value.shape}, but" f" {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found." ) A_ : List[Any] = value logger.info(f"Feat extract conv layer {layer_id} was initialized from {full_name}." ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape, ( f"{full_name} has size {value.shape}, but" f" {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found." ) A_ : int = value logger.info(f"Feat extract conv layer {layer_id} was initialized from {full_name}." ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, ( f"{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was" " found." ) A_ : List[Any] = value logger.info(f"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}." ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, ( f"{full_name} has size {value.shape}, but" f" {feature_extractor[layer_id].layer_norm.weight.data.shape} was found." ) A_ : Tuple = value logger.info(f"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}." ) else: unused_weights.append(_lowerCAmelCase ) def __snake_case ( _lowerCAmelCase : Optional[int] ) -> str: A_ , A_ : List[str] = emb.weight.shape A_ : Optional[int] = nn.Linear(_lowerCAmelCase , _lowerCAmelCase , bias=_lowerCAmelCase ) A_ : List[Any] = emb.weight.data return lin_layer def __snake_case ( _lowerCAmelCase : str ) -> Tuple: with open(_lowerCAmelCase , "r" , encoding="utf-8" ) as f: A_ : int = f.readlines() A_ : Dict = [line.split(" " )[0] for line in lines] A_ : Tuple = len(_lowerCAmelCase ) A_ : Union[str, Any] = { "<s>": 0, "<pad>": 1, "</s>": 2, "<unk>": 3, } vocab_dict.update(dict(zip(_lowerCAmelCase , range(4 , num_words + 4 ) ) ) ) return vocab_dict @torch.no_grad() def __snake_case ( _lowerCAmelCase : int , _lowerCAmelCase : Tuple , _lowerCAmelCase : Dict , _lowerCAmelCase : Any , _lowerCAmelCase : Tuple , _lowerCAmelCase : List[str] , _lowerCAmelCase : Dict , ) -> Tuple: A_ : Optional[int] = WavaVecaConfig.from_pretrained(_lowerCAmelCase ) A_ : str = SpeechaTextaConfig.from_pretrained( _lowerCAmelCase , vocab_size=_lowerCAmelCase , decoder_layers=_lowerCAmelCase , do_stable_layer_norm=_lowerCAmelCase ) A_ : int = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=16000 , padding_value=0 , do_normalize=_lowerCAmelCase , return_attention_mask=_lowerCAmelCase , ) A_ , A_ , A_ : List[Any] = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={"data": "/".join(dict_path.split("/" )[:-1] )} ) A_ : Union[str, Any] = model[0].eval() # set weights for wav2vec2 encoder A_ : Tuple = WavaVecaModel(_lowerCAmelCase ) A_ : str = recursively_load_weights_wavaveca(model.encoder , _lowerCAmelCase ) A_ : Tuple = SpeechaTextaForCausalLM(_lowerCAmelCase ) A_ , A_ : List[str] = hf_decoder.model.decoder.load_state_dict(model.decoder.state_dict() , strict=_lowerCAmelCase ) # set output linear layer unexpected_keys.remove("embed_out" ) A_ : Union[str, Any] = nn.Parameter(model.decoder.embed_out.detach() ) # layer norm is init to identity matrix so leaving it is fine logger.warning(f"The following keys are missing when loading the decoder weights: {missing_keys}" ) logger.warning(f"The following keys are unexpected when loading the decoder weights: {unexpected_keys}" ) A_ : str = SpeechEncoderDecoderModel(encoder=_lowerCAmelCase , decoder=_lowerCAmelCase ) A_ : Optional[Any] = False # add projection layer A_ : Optional[Any] = nn.Parameter(projection_layer.weight ) A_ : int = nn.Parameter(projection_layer.bias ) A_ : str = create_vocab_dict(_lowerCAmelCase ) with open(os.path.join(_lowerCAmelCase , "vocab.json" ) , "w" ) as fp: json.dump(_lowerCAmelCase , _lowerCAmelCase ) A_ : Any = SpeechaTextaTokenizer(os.path.join(_lowerCAmelCase , "vocab.json" ) ) tokenizer.save_pretrained(_lowerCAmelCase ) A_ : Optional[int] = hf_wavavec.config.to_dict() A_ : int = tokenizer.pad_token_id A_ : List[str] = tokenizer.bos_token_id A_ : List[str] = tokenizer.eos_token_id A_ : List[str] = "speech_to_text_2" A_ : Tuple = "wav2vec2" A_ : str = SpeechEncoderDecoderConfig.from_dict(_lowerCAmelCase ) hf_wavavec.save_pretrained(_lowerCAmelCase ) feature_extractor.save_pretrained(_lowerCAmelCase ) if __name__ == "__main__": _lowerCAmelCase : Optional[Any] = argparse.ArgumentParser() parser.add_argument('''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''') parser.add_argument('''--checkpoint_path''', default=None, type=str, help='''Path to fairseq checkpoint''') parser.add_argument('''--dict_path''', default=None, type=str, help='''Path to dict of fine-tuned model''') parser.add_argument( '''--encoder_config_path''', default='''facebook/wav2vec2-large-lv60''', type=str, help='''Path to hf encoder wav2vec2 checkpoint config''', ) parser.add_argument( '''--decoder_config_path''', default='''facebook/s2t-small-mustc-en-fr-st''', type=str, help='''Path to hf decoder s2t checkpoint config''', ) parser.add_argument('''--vocab_size''', default=10_224, type=int, help='''Vocab size of decoder''') parser.add_argument('''--num_decoder_layers''', default=7, type=int, help='''Number of decoder layers''') _lowerCAmelCase : Union[str, Any] = parser.parse_args() convert_wavaveca_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.dict_path, encoder_config_path=args.encoder_config_path, decoder_config_path=args.decoder_config_path, vocab_size=args.vocab_size, num_decoder_layers=args.num_decoder_layers, )
300
0
from __future__ import annotations from numpy import array, cos, cross, floataa, radians, sin from numpy.typing import NDArray def SCREAMING_SNAKE_CASE__ ( lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ = False ) -> list[float]: if radian_mode: return [magnitude * cos(lowerCamelCase__ ), magnitude * sin(lowerCamelCase__ )] return [magnitude * cos(radians(lowerCamelCase__ ) ), magnitude * sin(radians(lowerCamelCase__ ) )] def SCREAMING_SNAKE_CASE__ ( lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ = 1_0**-1 ) -> bool: __lowerCamelCase : NDArray[floataa] = cross(lowerCamelCase__ , lowerCamelCase__ ) __lowerCamelCase : float = sum(lowerCamelCase__ ) return abs(lowerCamelCase__ ) < eps if __name__ == "__main__": # Test to check if it works a =array( [ polar_force(7_18.4, 180 - 30), polar_force(8_79.54, 45), polar_force(100, -90), ] ) a =array([[0, 0], [0, 0], [0, 0]]) assert in_static_equilibrium(forces, location) # Problem 1 in image_data/2D_problems.jpg a =array( [ polar_force(30 * 9.81, 15), polar_force(215, 180 - 45), polar_force(264, 90 - 30), ] ) a =array([[0, 0], [0, 0], [0, 0]]) assert in_static_equilibrium(forces, location) # Problem in image_data/2D_problems_1.jpg a =array([[0, -2000], [0, -1200], [0, 15600], [0, -12400]]) a =array([[0, 0], [6, 0], [10, 0], [12, 0]]) assert in_static_equilibrium(forces, location) import doctest doctest.testmod()
73
from __future__ import annotations import unittest import numpy as np from transformers import LayoutLMConfig, 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.models.layoutlm.modeling_tf_layoutlm import ( TF_LAYOUTLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFLayoutLMForMaskedLM, TFLayoutLMForQuestionAnswering, TFLayoutLMForSequenceClassification, TFLayoutLMForTokenClassification, TFLayoutLMModel, ) class __magic_name__ : """simple docstring""" def __init__( self :Tuple , snake_case :Optional[Any] , snake_case :Tuple=13 , snake_case :Dict=7 , snake_case :List[Any]=True , snake_case :List[Any]=True , snake_case :Dict=True , snake_case :Any=True , snake_case :Optional[int]=99 , snake_case :Any=32 , snake_case :Dict=2 , snake_case :int=4 , snake_case :Optional[int]=37 , snake_case :List[str]="gelu" , snake_case :List[Any]=0.1 , snake_case :Optional[Any]=0.1 , snake_case :Tuple=512 , snake_case :Tuple=16 , snake_case :Tuple=2 , snake_case :Optional[int]=0.02 , snake_case :str=3 , snake_case :Optional[int]=4 , snake_case :List[str]=None , snake_case :Tuple=1_000 , ): '''simple docstring''' A_ : str = parent A_ : str = batch_size A_ : str = seq_length A_ : Any = is_training A_ : Any = use_input_mask A_ : str = use_token_type_ids A_ : Tuple = use_labels A_ : Optional[Any] = vocab_size A_ : Dict = hidden_size A_ : str = num_hidden_layers A_ : Dict = num_attention_heads A_ : str = intermediate_size A_ : int = hidden_act A_ : List[Any] = hidden_dropout_prob A_ : Dict = attention_probs_dropout_prob A_ : Optional[Any] = max_position_embeddings A_ : List[Any] = type_vocab_size A_ : Any = type_sequence_label_size A_ : Dict = initializer_range A_ : Any = num_labels A_ : Optional[int] = num_choices A_ : Optional[Any] = scope A_ : Any = range_bbox def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' A_ : List[str] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) # convert bbox to numpy since TF does not support item assignment A_ : Tuple = ids_tensor([self.batch_size, self.seq_length, 4] , self.range_bbox ).numpy() # Ensure that bbox is legal for i in range(bbox.shape[0] ): for j in range(bbox.shape[1] ): if bbox[i, j, 3] < bbox[i, j, 1]: A_ : str = bbox[i, j, 3] A_ : Union[str, Any] = bbox[i, j, 1] A_ : List[Any] = t if bbox[i, j, 2] < bbox[i, j, 0]: A_ : Any = bbox[i, j, 2] A_ : Tuple = bbox[i, j, 0] A_ : int = t A_ : int = tf.convert_to_tensor(snake_case ) A_ : Any = None if self.use_input_mask: A_ : Union[str, Any] = random_attention_mask([self.batch_size, self.seq_length] ) A_ : str = None if self.use_token_type_ids: A_ : Union[str, Any] = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) A_ : Dict = None A_ : List[Any] = None A_ : List[str] = None if self.use_labels: A_ : Dict = ids_tensor([self.batch_size] , self.type_sequence_label_size ) A_ : Optional[int] = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) A_ : str = ids_tensor([self.batch_size] , self.num_choices ) A_ : int = LayoutLMConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , ) return config, input_ids, bbox, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def SCREAMING_SNAKE_CASE ( self :str , snake_case :Dict , snake_case :Union[str, Any] , snake_case :int , snake_case :int , snake_case :Union[str, Any] , snake_case :Tuple , snake_case :Optional[int] , snake_case :List[Any] ): '''simple docstring''' A_ : Any = TFLayoutLMModel(config=snake_case ) A_ : Tuple = model(snake_case , snake_case , attention_mask=snake_case , token_type_ids=snake_case ) A_ : str = model(snake_case , snake_case , token_type_ids=snake_case ) A_ : List[Any] = model(snake_case , snake_case ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def SCREAMING_SNAKE_CASE ( self :Optional[int] , snake_case :Any , snake_case :List[Any] , snake_case :List[str] , snake_case :Optional[Any] , snake_case :Dict , snake_case :Any , snake_case :Union[str, Any] , snake_case :List[Any] ): '''simple docstring''' A_ : Optional[int] = TFLayoutLMForMaskedLM(config=snake_case ) A_ : Tuple = model(snake_case , snake_case , attention_mask=snake_case , token_type_ids=snake_case , labels=snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def SCREAMING_SNAKE_CASE ( self :List[str] , snake_case :Dict , snake_case :Tuple , snake_case :Tuple , snake_case :List[str] , snake_case :Tuple , snake_case :str , snake_case :Optional[int] , snake_case :Any ): '''simple docstring''' A_ : Union[str, Any] = self.num_labels A_ : int = TFLayoutLMForSequenceClassification(config=snake_case ) A_ : Optional[int] = model(snake_case , snake_case , attention_mask=snake_case , token_type_ids=snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def SCREAMING_SNAKE_CASE ( self :Optional[Any] , snake_case :Dict , snake_case :str , snake_case :Optional[Any] , snake_case :int , snake_case :Any , snake_case :Tuple , snake_case :List[str] , snake_case :Union[str, Any] ): '''simple docstring''' A_ : List[Any] = self.num_labels A_ : str = TFLayoutLMForTokenClassification(config=snake_case ) A_ : Union[str, Any] = model(snake_case , snake_case , attention_mask=snake_case , token_type_ids=snake_case , labels=snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def SCREAMING_SNAKE_CASE ( self :int , snake_case :List[str] , snake_case :Optional[int] , snake_case :Union[str, Any] , snake_case :List[Any] , snake_case :int , snake_case :Any , snake_case :Union[str, Any] , snake_case :Any ): '''simple docstring''' A_ : Optional[Any] = TFLayoutLMForQuestionAnswering(config=snake_case ) A_ : List[Any] = model(snake_case , snake_case , attention_mask=snake_case , token_type_ids=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 SCREAMING_SNAKE_CASE ( self :Dict ): '''simple docstring''' A_ : int = self.prepare_config_and_inputs() ( ( A_ ) , ( A_ ) , ( A_ ) , ( A_ ) , ( A_ ) , ( A_ ) , ( A_ ) , ( A_ ) , ) : Union[str, Any] = config_and_inputs A_ : Optional[Any] = { "input_ids": input_ids, "bbox": bbox, "token_type_ids": token_type_ids, "attention_mask": input_mask, } return config, inputs_dict @require_tf class __magic_name__ ( lowerCamelCase__ , lowerCamelCase__ , unittest.TestCase ): """simple docstring""" __UpperCamelCase = ( ( TFLayoutLMModel, TFLayoutLMForMaskedLM, TFLayoutLMForTokenClassification, TFLayoutLMForSequenceClassification, TFLayoutLMForQuestionAnswering, ) if is_tf_available() else () ) __UpperCamelCase = ( { '''feature-extraction''': TFLayoutLMModel, '''fill-mask''': TFLayoutLMForMaskedLM, '''text-classification''': TFLayoutLMForSequenceClassification, '''token-classification''': TFLayoutLMForTokenClassification, '''zero-shot''': TFLayoutLMForSequenceClassification, } if is_tf_available() else {} ) __UpperCamelCase = False __UpperCamelCase = True __UpperCamelCase = 10 def SCREAMING_SNAKE_CASE ( self :Dict ): '''simple docstring''' A_ : Tuple = TFLayoutLMModelTester(self ) A_ : List[Any] = ConfigTester(self , config_class=snake_case , hidden_size=37 ) def SCREAMING_SNAKE_CASE ( self :Tuple ): '''simple docstring''' self.config_tester.run_common_tests() def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' A_ : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case ) def SCREAMING_SNAKE_CASE ( self :Optional[int] ): '''simple docstring''' A_ : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*snake_case ) def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' A_ : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*snake_case ) def SCREAMING_SNAKE_CASE ( self :Tuple ): '''simple docstring''' A_ : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*snake_case ) def SCREAMING_SNAKE_CASE ( self :List[Any] ): '''simple docstring''' A_ : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*snake_case ) @slow def SCREAMING_SNAKE_CASE ( self :Optional[Any] ): '''simple docstring''' for model_name in TF_LAYOUTLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A_ : List[str] = TFLayoutLMModel.from_pretrained(snake_case ) self.assertIsNotNone(snake_case ) @unittest.skip("Onnx compliancy broke with TF 2.10" ) def SCREAMING_SNAKE_CASE ( self :Dict ): '''simple docstring''' pass def __snake_case ( ) -> Optional[Any]: # Here we prepare a batch of 2 sequences to test a LayoutLM forward pass on: # fmt: off A_ : int = tf.convert_to_tensor([[101,1019,1014,1016,1037,12849,4747,1004,14246,2278,5439,4524,5002,2930,2193,2930,4341,3208,1005,1055,2171,2848,11300,3531,102],[101,4070,4034,7020,1024,3058,1015,1013,2861,1013,6070,19274,2772,6205,27814,16147,16147,4343,2047,10283,10969,14389,1012,2338,102]] ) # noqa: E231 A_ : int = tf.convert_to_tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],] ) # noqa: E231 A_ : Union[str, Any] = tf.convert_to_tensor([[[0,0,0,0],[423,237,440,251],[427,272,441,287],[419,115,437,129],[961,885,992,912],[256,38,330,58],[256,38,330,58],[336,42,353,57],[360,39,401,56],[360,39,401,56],[411,39,471,59],[479,41,528,59],[533,39,630,60],[67,113,134,131],[141,115,209,132],[68,149,133,166],[141,149,187,164],[195,148,287,165],[195,148,287,165],[195,148,287,165],[295,148,349,165],[441,149,492,166],[497,149,546,164],[64,201,125,218],[1000,1000,1000,1000]],[[0,0,0,0],[662,150,754,166],[665,199,742,211],[519,213,554,228],[519,213,554,228],[134,433,187,454],[130,467,204,480],[130,467,204,480],[130,467,204,480],[130,467,204,480],[130,467,204,480],[314,469,376,482],[504,684,582,706],[941,825,973,900],[941,825,973,900],[941,825,973,900],[941,825,973,900],[610,749,652,765],[130,659,168,672],[176,657,237,672],[238,657,312,672],[443,653,628,672],[443,653,628,672],[716,301,825,317],[1000,1000,1000,1000]]] ) # noqa: E231 A_ : List[Any] = tf.convert_to_tensor([[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]] ) # noqa: E231 # these are sequence labels (i.e. at the token level) A_ : Tuple = tf.convert_to_tensor([[-100,10,10,10,9,1,-100,7,7,-100,7,7,4,2,5,2,8,8,-100,-100,5,0,3,2,-100],[-100,12,12,12,-100,12,10,-100,-100,-100,-100,10,12,9,-100,-100,-100,10,10,10,9,12,-100,10,-100]] ) # noqa: E231 # fmt: on return input_ids, attention_mask, bbox, token_type_ids, labels @require_tf class __magic_name__ ( unittest.TestCase ): """simple docstring""" @slow def SCREAMING_SNAKE_CASE ( self :Tuple ): '''simple docstring''' A_ : str = TFLayoutLMModel.from_pretrained("microsoft/layoutlm-base-uncased" ) A_ , A_ , A_ , A_ , A_ : Tuple = prepare_layoutlm_batch_inputs() # forward pass A_ : Tuple = model(input_ids=snake_case , bbox=snake_case , attention_mask=snake_case , token_type_ids=snake_case ) # test the sequence output on [0, :3, :3] A_ : List[Any] = tf.convert_to_tensor( [[0.1785, -0.1947, -0.0425], [-0.3254, -0.2807, 0.2553], [-0.5391, -0.3322, 0.3364]] , ) self.assertTrue(np.allclose(outputs.last_hidden_state[0, :3, :3] , snake_case , atol=1e-3 ) ) # test the pooled output on [1, :3] A_ : Optional[Any] = tf.convert_to_tensor([-0.6580, -0.0214, 0.8552] ) self.assertTrue(np.allclose(outputs.pooler_output[1, :3] , snake_case , atol=1e-3 ) ) @slow def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' A_ : Union[str, Any] = TFLayoutLMForSequenceClassification.from_pretrained("microsoft/layoutlm-base-uncased" , num_labels=2 ) A_ , A_ , A_ , A_ , A_ : Any = prepare_layoutlm_batch_inputs() # forward pass A_ : Dict = model( input_ids=snake_case , bbox=snake_case , attention_mask=snake_case , token_type_ids=snake_case , labels=tf.convert_to_tensor([1, 1] ) , ) # test whether we get a loss as a scalar A_ : List[str] = outputs.loss A_ : Union[str, Any] = (2,) self.assertEqual(loss.shape , snake_case ) # test the shape of the logits A_ : Tuple = outputs.logits A_ : Tuple = (2, 2) self.assertEqual(logits.shape , snake_case ) @slow def SCREAMING_SNAKE_CASE ( self :Optional[int] ): '''simple docstring''' A_ : int = TFLayoutLMForTokenClassification.from_pretrained("microsoft/layoutlm-base-uncased" , num_labels=13 ) A_ , A_ , A_ , A_ , A_ : Optional[int] = prepare_layoutlm_batch_inputs() # forward pass A_ : Union[str, Any] = model( input_ids=snake_case , bbox=snake_case , attention_mask=snake_case , token_type_ids=snake_case , labels=snake_case ) # test the shape of the logits A_ : Dict = outputs.logits A_ : List[Any] = tf.convert_to_tensor((2, 25, 13) ) self.assertEqual(logits.shape , snake_case ) @slow def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' A_ : Optional[Any] = TFLayoutLMForQuestionAnswering.from_pretrained("microsoft/layoutlm-base-uncased" ) A_ , A_ , A_ , A_ , A_ : str = prepare_layoutlm_batch_inputs() # forward pass A_ : Union[str, Any] = model(input_ids=snake_case , bbox=snake_case , attention_mask=snake_case , token_type_ids=snake_case ) # test the shape of the logits A_ : Union[str, Any] = tf.convert_to_tensor((2, 25) ) self.assertEqual(outputs.start_logits.shape , snake_case ) self.assertEqual(outputs.end_logits.shape , snake_case )
300
0
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available _lowercase = { '''configuration_biogpt''': ['''BIOGPT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''BioGptConfig'''], '''tokenization_biogpt''': ['''BioGptTokenizer'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowercase = [ '''BIOGPT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''BioGptForCausalLM''', '''BioGptForTokenClassification''', '''BioGptForSequenceClassification''', '''BioGptModel''', '''BioGptPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_biogpt import BIOGPT_PRETRAINED_CONFIG_ARCHIVE_MAP, BioGptConfig from .tokenization_biogpt import BioGptTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_biogpt import ( BIOGPT_PRETRAINED_MODEL_ARCHIVE_LIST, BioGptForCausalLM, BioGptForSequenceClassification, BioGptForTokenClassification, BioGptModel, BioGptPreTrainedModel, ) else: import sys _lowercase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
74
# Copyright 2023 The HuggingFace Inc. 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. import re from ..utils import cached_file # docstyle-ignore _lowerCAmelCase : Optional[int] = ''' Human: <<task>> Assistant: ''' _lowerCAmelCase : int = '''huggingface-tools/default-prompts''' _lowerCAmelCase : Any = {'''chat''': '''chat_prompt_template.txt''', '''run''': '''run_prompt_template.txt'''} def __snake_case ( _lowerCAmelCase : str , _lowerCAmelCase : List[Any] , _lowerCAmelCase : Dict="run" ) -> List[Any]: if prompt_or_repo_id is None: A_ : Optional[int] = DEFAULT_PROMPTS_REPO # prompt is considered a repo ID when it does not contain any kind of space if re.search("\\s" , _lowerCAmelCase ) is not None: return prompt_or_repo_id A_ : Optional[Any] = cached_file( _lowerCAmelCase , PROMPT_FILES[mode] , repo_type="dataset" , user_agent={"agent": agent_name} ) with open(_lowerCAmelCase , "r" , encoding="utf-8" ) as f: return f.read()
300
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available a_ : str = {"""tokenization_herbert""": ["""HerbertTokenizer"""]} try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ : Optional[int] = ["""HerbertTokenizerFast"""] if TYPE_CHECKING: from .tokenization_herbert import HerbertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_herbert_fast import HerbertTokenizerFast else: import sys a_ : List[str] = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
75
def __snake_case ( _lowerCAmelCase : list ) -> list: if len(_lowerCAmelCase ) <= 1: return [tuple(_lowerCAmelCase )] A_ : Tuple = [] def generate(_lowerCAmelCase : int , _lowerCAmelCase : list ): A_ : List[str] = [0] * n res.append(tuple(_lowerCAmelCase ) ) A_ : int = 0 while i < n: if c[i] < i: if i % 2 == 0: A_ , A_ : str = arr[i], arr[0] else: A_ , A_ : List[str] = arr[i], arr[c[i]] res.append(tuple(_lowerCAmelCase ) ) c[i] += 1 A_ : Tuple = 0 else: A_ : Dict = 0 i += 1 generate(len(_lowerCAmelCase ) , _lowerCAmelCase ) return res if __name__ == "__main__": _lowerCAmelCase : str = input('''Enter numbers separated by a comma:\n''').strip() _lowerCAmelCase : str = [int(item) for item in user_input.split(''',''')] print(heaps(arr))
300
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available a_ = { 'configuration_clipseg': [ 'CLIPSEG_PRETRAINED_CONFIG_ARCHIVE_MAP', 'CLIPSegConfig', 'CLIPSegTextConfig', 'CLIPSegVisionConfig', ], 'processing_clipseg': ['CLIPSegProcessor'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ = [ 'CLIPSEG_PRETRAINED_MODEL_ARCHIVE_LIST', 'CLIPSegModel', 'CLIPSegPreTrainedModel', 'CLIPSegTextModel', 'CLIPSegVisionModel', 'CLIPSegForImageSegmentation', ] if TYPE_CHECKING: from .configuration_clipseg import ( CLIPSEG_PRETRAINED_CONFIG_ARCHIVE_MAP, CLIPSegConfig, CLIPSegTextConfig, CLIPSegVisionConfig, ) from .processing_clipseg import CLIPSegProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_clipseg import ( CLIPSEG_PRETRAINED_MODEL_ARCHIVE_LIST, CLIPSegForImageSegmentation, CLIPSegModel, CLIPSegPreTrainedModel, CLIPSegTextModel, CLIPSegVisionModel, ) else: import sys a_ = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
76
import json from typing import List, Optional, Tuple from tokenizers import pre_tokenizers, processors from ...tokenization_utils_base import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_roberta import RobertaTokenizer _lowerCAmelCase : int = logging.get_logger(__name__) _lowerCAmelCase : Optional[int] = {'''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt''', '''tokenizer_file''': '''tokenizer.json'''} _lowerCAmelCase : List[Any] = { '''vocab_file''': { '''roberta-base''': '''https://huggingface.co/roberta-base/resolve/main/vocab.json''', '''roberta-large''': '''https://huggingface.co/roberta-large/resolve/main/vocab.json''', '''roberta-large-mnli''': '''https://huggingface.co/roberta-large-mnli/resolve/main/vocab.json''', '''distilroberta-base''': '''https://huggingface.co/distilroberta-base/resolve/main/vocab.json''', '''roberta-base-openai-detector''': '''https://huggingface.co/roberta-base-openai-detector/resolve/main/vocab.json''', '''roberta-large-openai-detector''': ( '''https://huggingface.co/roberta-large-openai-detector/resolve/main/vocab.json''' ), }, '''merges_file''': { '''roberta-base''': '''https://huggingface.co/roberta-base/resolve/main/merges.txt''', '''roberta-large''': '''https://huggingface.co/roberta-large/resolve/main/merges.txt''', '''roberta-large-mnli''': '''https://huggingface.co/roberta-large-mnli/resolve/main/merges.txt''', '''distilroberta-base''': '''https://huggingface.co/distilroberta-base/resolve/main/merges.txt''', '''roberta-base-openai-detector''': '''https://huggingface.co/roberta-base-openai-detector/resolve/main/merges.txt''', '''roberta-large-openai-detector''': ( '''https://huggingface.co/roberta-large-openai-detector/resolve/main/merges.txt''' ), }, '''tokenizer_file''': { '''roberta-base''': '''https://huggingface.co/roberta-base/resolve/main/tokenizer.json''', '''roberta-large''': '''https://huggingface.co/roberta-large/resolve/main/tokenizer.json''', '''roberta-large-mnli''': '''https://huggingface.co/roberta-large-mnli/resolve/main/tokenizer.json''', '''distilroberta-base''': '''https://huggingface.co/distilroberta-base/resolve/main/tokenizer.json''', '''roberta-base-openai-detector''': ( '''https://huggingface.co/roberta-base-openai-detector/resolve/main/tokenizer.json''' ), '''roberta-large-openai-detector''': ( '''https://huggingface.co/roberta-large-openai-detector/resolve/main/tokenizer.json''' ), }, } _lowerCAmelCase : Any = { '''roberta-base''': 512, '''roberta-large''': 512, '''roberta-large-mnli''': 512, '''distilroberta-base''': 512, '''roberta-base-openai-detector''': 512, '''roberta-large-openai-detector''': 512, } class __magic_name__ ( lowerCamelCase__ ): """simple docstring""" __UpperCamelCase = VOCAB_FILES_NAMES __UpperCamelCase = PRETRAINED_VOCAB_FILES_MAP __UpperCamelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCamelCase = ['''input_ids''', '''attention_mask'''] __UpperCamelCase = RobertaTokenizer def __init__( self :Dict , snake_case :List[str]=None , snake_case :List[Any]=None , snake_case :Union[str, Any]=None , snake_case :List[str]="replace" , snake_case :Tuple="<s>" , snake_case :Union[str, Any]="</s>" , snake_case :str="</s>" , snake_case :Union[str, Any]="<s>" , snake_case :int="<unk>" , snake_case :Tuple="<pad>" , snake_case :List[str]="<mask>" , snake_case :Any=False , snake_case :Union[str, Any]=True , **snake_case :Optional[int] , ): '''simple docstring''' super().__init__( snake_case , snake_case , tokenizer_file=snake_case , errors=snake_case , bos_token=snake_case , eos_token=snake_case , sep_token=snake_case , cls_token=snake_case , unk_token=snake_case , pad_token=snake_case , mask_token=snake_case , add_prefix_space=snake_case , trim_offsets=snake_case , **snake_case , ) A_ : Optional[Any] = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get("add_prefix_space" , snake_case ) != add_prefix_space: A_ : Dict = getattr(snake_case , pre_tok_state.pop("type" ) ) A_ : Optional[int] = add_prefix_space A_ : int = pre_tok_class(**snake_case ) A_ : Optional[int] = add_prefix_space A_ : Optional[int] = "post_processor" A_ : Dict = getattr(self.backend_tokenizer , snake_case , snake_case ) if tokenizer_component_instance: A_ : Dict = json.loads(tokenizer_component_instance.__getstate__() ) # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class` if "sep" in state: A_ : List[Any] = tuple(state["sep"] ) if "cls" in state: A_ : Optional[Any] = tuple(state["cls"] ) A_ : Tuple = False if state.get("add_prefix_space" , snake_case ) != add_prefix_space: A_ : List[Any] = add_prefix_space A_ : Optional[int] = True if state.get("trim_offsets" , snake_case ) != trim_offsets: A_ : List[str] = trim_offsets A_ : Any = True if changes_to_apply: A_ : Optional[Any] = getattr(snake_case , state.pop("type" ) ) A_ : Any = component_class(**snake_case ) setattr(self.backend_tokenizer , snake_case , snake_case ) @property def SCREAMING_SNAKE_CASE ( self :List[Any] ): '''simple docstring''' if self._mask_token is None: if self.verbose: logger.error("Using mask_token, but it is not set yet." ) return None return str(self._mask_token ) @mask_token.setter def SCREAMING_SNAKE_CASE ( self :Any , snake_case :Dict ): '''simple docstring''' A_ : Dict = AddedToken(snake_case , lstrip=snake_case , rstrip=snake_case ) if isinstance(snake_case , snake_case ) else value A_ : Any = value def SCREAMING_SNAKE_CASE ( self :Dict , *snake_case :Tuple , **snake_case :Union[str, Any] ): '''simple docstring''' A_ : Any = kwargs.get("is_split_into_words" , snake_case ) assert self.add_prefix_space or not is_split_into_words, ( f"You need to instantiate {self.__class__.__name__} with add_prefix_space=True " "to use it with pretokenized inputs." ) return super()._batch_encode_plus(*snake_case , **snake_case ) def SCREAMING_SNAKE_CASE ( self :List[str] , *snake_case :str , **snake_case :Union[str, Any] ): '''simple docstring''' A_ : Any = kwargs.get("is_split_into_words" , snake_case ) assert self.add_prefix_space or not is_split_into_words, ( f"You need to instantiate {self.__class__.__name__} with add_prefix_space=True " "to use it with pretokenized inputs." ) return super()._encode_plus(*snake_case , **snake_case ) def SCREAMING_SNAKE_CASE ( self :Union[str, Any] , snake_case :str , snake_case :Optional[str] = None ): '''simple docstring''' A_ : str = self._tokenizer.model.save(snake_case , name=snake_case ) return tuple(snake_case ) def SCREAMING_SNAKE_CASE ( self :List[str] , snake_case :List[str] , snake_case :Optional[Any]=None ): '''simple docstring''' A_ : int = [self.bos_token_id] + token_ids_a + [self.eos_token_id] if token_ids_a is None: return output return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id] def SCREAMING_SNAKE_CASE ( self :Any , snake_case :List[int] , snake_case :Optional[List[int]] = None ): '''simple docstring''' A_ : Any = [self.sep_token_id] A_ : Optional[Any] = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0]
300
0
"""simple docstring""" def a_ ( _lowerCAmelCase : int ): '''simple docstring''' if divisor % 5 == 0 or divisor % 2 == 0: return 0 lowercase__ : Dict = 1 lowercase__ : Dict = 1 while repunit: lowercase__ : List[Any] = (10 * repunit + 1) % divisor repunit_index += 1 return repunit_index def a_ ( _lowerCAmelCase : int = 100_0000 ): '''simple docstring''' lowercase__ : str = limit - 1 if divisor % 2 == 0: divisor += 1 while least_divisible_repunit(_lowerCAmelCase ) <= limit: divisor += 2 return divisor if __name__ == "__main__": print(f'''{solution() = }''')
77
from typing import Dict, List from nltk.translate import gleu_score import datasets from datasets import MetricInfo _lowerCAmelCase : int = '''\ @misc{wu2016googles, title={Google\'s Neural Machine Translation System: Bridging the Gap between Human and Machine Translation}, author={Yonghui Wu and Mike Schuster and Zhifeng Chen and Quoc V. Le and Mohammad Norouzi and Wolfgang Macherey and Maxim Krikun and Yuan Cao and Qin Gao and Klaus Macherey and Jeff Klingner and Apurva Shah and Melvin Johnson and Xiaobing Liu and Łukasz Kaiser and Stephan Gouws and Yoshikiyo Kato and Taku Kudo and Hideto Kazawa and Keith Stevens and George Kurian and Nishant Patil and Wei Wang and Cliff Young and Jason Smith and Jason Riesa and Alex Rudnick and Oriol Vinyals and Greg Corrado and Macduff Hughes and Jeffrey Dean}, year={2016}, eprint={1609.08144}, archivePrefix={arXiv}, primaryClass={cs.CL} } ''' _lowerCAmelCase : Tuple = '''\ The BLEU score has some undesirable properties when used for single sentences, as it was designed to be a corpus measure. We therefore use a slightly different score for our RL experiments which we call the \'GLEU score\'. For the GLEU score, we record all sub-sequences of 1, 2, 3 or 4 tokens in output and target sequence (n-grams). We then compute a recall, which is the ratio of the number of matching n-grams to the number of total n-grams in the target (ground truth) sequence, and a precision, which is the ratio of the number of matching n-grams to the number of total n-grams in the generated output sequence. Then GLEU score is simply the minimum of recall and precision. This GLEU score\'s range is always between 0 (no matches) and 1 (all match) and it is symmetrical when switching output and target. According to our experiments, GLEU score correlates quite well with the BLEU metric on a corpus level but does not have its drawbacks for our per sentence reward objective. ''' _lowerCAmelCase : int = '''\ Computes corpus-level Google BLEU (GLEU) score of translated segments against one or more references. Instead of averaging the sentence level GLEU scores (i.e. macro-average precision), Wu et al. (2016) sum up the matching tokens and the max of hypothesis and reference tokens for each sentence, then compute using the aggregate values. Args: predictions (list of str): list of translations to score. Each translation should be tokenized into a list of tokens. references (list of list of str): list of lists of references for each translation. Each reference should be tokenized into a list of tokens. min_len (int): The minimum order of n-gram this function should extract. Defaults to 1. max_len (int): The maximum order of n-gram this function should extract. Defaults to 4. Returns: \'google_bleu\': google_bleu score Examples: Example 1: >>> hyp1 = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'which\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'always\', ... \'disobeys\', \'the\', \'commands\', \'of\', \'the\', \'cat\'] >>> ref1a = [\'It\', \'is\', \'the\', \'guiding\', \'principle\', \'which\', ... \'guarantees\', \'the\', \'rubber\', \'duck\', \'forces\', \'never\', ... \'being\', \'under\', \'the\', \'command\', \'of\', \'the\', \'cat\'] >>> hyp2 = [\'he\', \'read\', \'the\', \'book\', \'because\', \'he\', \'was\', ... \'interested\', \'in\', \'world\', \'history\'] >>> ref2a = [\'he\', \'was\', \'interested\', \'in\', \'world\', \'history\', ... \'because\', \'he\', \'read\', \'the\', \'book\'] >>> list_of_references = [[ref1a], [ref2a]] >>> hypotheses = [hyp1, hyp2] >>> google_bleu = datasets.load_metric("google_bleu") >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references) >>> print(round(results["google_bleu"], 2)) 0.44 Example 2: >>> hyp1 = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'which\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'always\', ... \'disobeys\', \'the\', \'commands\', \'of\', \'the\', \'cat\'] >>> ref1a = [\'It\', \'is\', \'the\', \'guiding\', \'principle\', \'which\', ... \'guarantees\', \'the\', \'rubber\', \'duck\', \'forces\', \'never\', ... \'being\', \'under\', \'the\', \'command\', \'of\', \'the\', \'cat\'] >>> ref1b = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'that\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'will\', \'never\', ... \'heed\', \'the\', \'cat\', \'commands\'] >>> ref1c = [\'It\', \'is\', \'the\', \'practical\', \'guide\', \'for\', \'the\', ... \'rubber\', \'duck\', \'army\', \'never\', \'to\', \'heed\', \'the\', \'directions\', ... \'of\', \'the\', \'cat\'] >>> hyp2 = [\'he\', \'read\', \'the\', \'book\', \'because\', \'he\', \'was\', ... \'interested\', \'in\', \'world\', \'history\'] >>> ref2a = [\'he\', \'was\', \'interested\', \'in\', \'world\', \'history\', ... \'because\', \'he\', \'read\', \'the\', \'book\'] >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]] >>> hypotheses = [hyp1, hyp2] >>> google_bleu = datasets.load_metric("google_bleu") >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references) >>> print(round(results["google_bleu"], 2)) 0.61 Example 3: >>> hyp1 = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'which\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'always\', ... \'disobeys\', \'the\', \'commands\', \'of\', \'the\', \'cat\'] >>> ref1a = [\'It\', \'is\', \'the\', \'guiding\', \'principle\', \'which\', ... \'guarantees\', \'the\', \'rubber\', \'duck\', \'forces\', \'never\', ... \'being\', \'under\', \'the\', \'command\', \'of\', \'the\', \'cat\'] >>> ref1b = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'that\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'will\', \'never\', ... \'heed\', \'the\', \'cat\', \'commands\'] >>> ref1c = [\'It\', \'is\', \'the\', \'practical\', \'guide\', \'for\', \'the\', ... \'rubber\', \'duck\', \'army\', \'never\', \'to\', \'heed\', \'the\', \'directions\', ... \'of\', \'the\', \'cat\'] >>> hyp2 = [\'he\', \'read\', \'the\', \'book\', \'because\', \'he\', \'was\', ... \'interested\', \'in\', \'world\', \'history\'] >>> ref2a = [\'he\', \'was\', \'interested\', \'in\', \'world\', \'history\', ... \'because\', \'he\', \'read\', \'the\', \'book\'] >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]] >>> hypotheses = [hyp1, hyp2] >>> google_bleu = datasets.load_metric("google_bleu") >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references, min_len=2) >>> print(round(results["google_bleu"], 2)) 0.53 Example 4: >>> hyp1 = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'which\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'always\', ... \'disobeys\', \'the\', \'commands\', \'of\', \'the\', \'cat\'] >>> ref1a = [\'It\', \'is\', \'the\', \'guiding\', \'principle\', \'which\', ... \'guarantees\', \'the\', \'rubber\', \'duck\', \'forces\', \'never\', ... \'being\', \'under\', \'the\', \'command\', \'of\', \'the\', \'cat\'] >>> ref1b = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'that\', ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'will\', \'never\', ... \'heed\', \'the\', \'cat\', \'commands\'] >>> ref1c = [\'It\', \'is\', \'the\', \'practical\', \'guide\', \'for\', \'the\', ... \'rubber\', \'duck\', \'army\', \'never\', \'to\', \'heed\', \'the\', \'directions\', ... \'of\', \'the\', \'cat\'] >>> hyp2 = [\'he\', \'read\', \'the\', \'book\', \'because\', \'he\', \'was\', ... \'interested\', \'in\', \'world\', \'history\'] >>> ref2a = [\'he\', \'was\', \'interested\', \'in\', \'world\', \'history\', ... \'because\', \'he\', \'read\', \'the\', \'book\'] >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]] >>> hypotheses = [hyp1, hyp2] >>> google_bleu = datasets.load_metric("google_bleu") >>> results = google_bleu.compute(predictions=hypotheses,references=list_of_references, min_len=2, max_len=6) >>> print(round(results["google_bleu"], 2)) 0.4 ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __magic_name__ ( datasets.Metric ): """simple docstring""" def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "predictions": datasets.Sequence(datasets.Value("string" , id="token" ) , id="sequence" ), "references": datasets.Sequence( datasets.Sequence(datasets.Value("string" , id="token" ) , id="sequence" ) , id="references" ), } ) , ) def SCREAMING_SNAKE_CASE ( self :int , snake_case :List[List[List[str]]] , snake_case :List[List[str]] , snake_case :int = 1 , snake_case :int = 4 , ): '''simple docstring''' return { "google_bleu": gleu_score.corpus_gleu( list_of_references=snake_case , hypotheses=snake_case , min_len=snake_case , max_len=snake_case ) }
300
0
"""simple docstring""" def _lowerCAmelCase ( lowercase_ ): return number & 1 == 0 if __name__ == "__main__": import doctest doctest.testmod()
78
import pyarrow.parquet as pq import pytest from datasets import Audio, Dataset, DatasetDict, Features, NamedSplit, Sequence, Value, config from datasets.features.image import Image from datasets.io.parquet import ParquetDatasetReader, ParquetDatasetWriter, get_writer_batch_size from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases def __snake_case ( _lowerCAmelCase : List[Any] , _lowerCAmelCase : Optional[int] ) -> str: assert isinstance(_lowerCAmelCase , _lowerCAmelCase ) assert dataset.num_rows == 4 assert dataset.num_columns == 3 assert dataset.column_names == ["col_1", "col_2", "col_3"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize("keep_in_memory" , [False, True] ) def __snake_case ( _lowerCAmelCase : Dict , _lowerCAmelCase : Optional[int] , _lowerCAmelCase : List[Any] ) -> Optional[int]: A_ : Tuple = tmp_path / "cache" A_ : Optional[int] = {"col_1": "string", "col_2": "int64", "col_3": "float64"} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): A_ : Optional[Any] = ParquetDatasetReader(_lowerCAmelCase , cache_dir=_lowerCAmelCase , keep_in_memory=_lowerCAmelCase ).read() _check_parquet_dataset(_lowerCAmelCase , _lowerCAmelCase ) @pytest.mark.parametrize( "features" , [ None, {"col_1": "string", "col_2": "int64", "col_3": "float64"}, {"col_1": "string", "col_2": "string", "col_3": "string"}, {"col_1": "int32", "col_2": "int32", "col_3": "int32"}, {"col_1": "float32", "col_2": "float32", "col_3": "float32"}, ] , ) def __snake_case ( _lowerCAmelCase : List[Any] , _lowerCAmelCase : Any , _lowerCAmelCase : List[Any] ) -> str: A_ : List[Any] = tmp_path / "cache" A_ : List[str] = {"col_1": "string", "col_2": "int64", "col_3": "float64"} A_ : int = features.copy() if features else default_expected_features A_ : str = ( Features({feature: Value(_lowerCAmelCase ) for feature, dtype in features.items()} ) if features is not None else None ) A_ : Union[str, Any] = ParquetDatasetReader(_lowerCAmelCase , features=_lowerCAmelCase , cache_dir=_lowerCAmelCase ).read() _check_parquet_dataset(_lowerCAmelCase , _lowerCAmelCase ) @pytest.mark.parametrize("split" , [None, NamedSplit("train" ), "train", "test"] ) def __snake_case ( _lowerCAmelCase : Any , _lowerCAmelCase : Any , _lowerCAmelCase : Any ) -> Optional[Any]: A_ : Dict = tmp_path / "cache" A_ : int = {"col_1": "string", "col_2": "int64", "col_3": "float64"} A_ : Optional[int] = ParquetDatasetReader(_lowerCAmelCase , cache_dir=_lowerCAmelCase , split=_lowerCAmelCase ).read() _check_parquet_dataset(_lowerCAmelCase , _lowerCAmelCase ) assert dataset.split == split if split else "train" @pytest.mark.parametrize("path_type" , [str, list] ) def __snake_case ( _lowerCAmelCase : List[str] , _lowerCAmelCase : int , _lowerCAmelCase : Optional[Any] ) -> List[str]: if issubclass(_lowerCAmelCase , _lowerCAmelCase ): A_ : int = parquet_path elif issubclass(_lowerCAmelCase , _lowerCAmelCase ): A_ : Optional[int] = [parquet_path] A_ : Optional[int] = tmp_path / "cache" A_ : Union[str, Any] = {"col_1": "string", "col_2": "int64", "col_3": "float64"} A_ : Optional[int] = ParquetDatasetReader(_lowerCAmelCase , cache_dir=_lowerCAmelCase ).read() _check_parquet_dataset(_lowerCAmelCase , _lowerCAmelCase ) def __snake_case ( _lowerCAmelCase : Any , _lowerCAmelCase : Union[str, Any] , _lowerCAmelCase : Union[str, Any]=("train",) ) -> Tuple: assert isinstance(_lowerCAmelCase , _lowerCAmelCase ) for split in splits: A_ : List[str] = dataset_dict[split] assert dataset.num_rows == 4 assert dataset.num_columns == 3 assert dataset.column_names == ["col_1", "col_2", "col_3"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize("keep_in_memory" , [False, True] ) def __snake_case ( _lowerCAmelCase : Optional[Any] , _lowerCAmelCase : List[Any] , _lowerCAmelCase : Dict ) -> Optional[int]: A_ : Optional[Any] = tmp_path / "cache" A_ : Union[str, Any] = {"col_1": "string", "col_2": "int64", "col_3": "float64"} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): A_ : Union[str, Any] = ParquetDatasetReader( {"train": parquet_path} , cache_dir=_lowerCAmelCase , keep_in_memory=_lowerCAmelCase ).read() _check_parquet_datasetdict(_lowerCAmelCase , _lowerCAmelCase ) @pytest.mark.parametrize( "features" , [ None, {"col_1": "string", "col_2": "int64", "col_3": "float64"}, {"col_1": "string", "col_2": "string", "col_3": "string"}, {"col_1": "int32", "col_2": "int32", "col_3": "int32"}, {"col_1": "float32", "col_2": "float32", "col_3": "float32"}, ] , ) def __snake_case ( _lowerCAmelCase : Tuple , _lowerCAmelCase : Any , _lowerCAmelCase : str ) -> Tuple: A_ : Optional[Any] = tmp_path / "cache" A_ : Any = {"col_1": "string", "col_2": "int64", "col_3": "float64"} A_ : List[str] = features.copy() if features else default_expected_features A_ : Tuple = ( Features({feature: Value(_lowerCAmelCase ) for feature, dtype in features.items()} ) if features is not None else None ) A_ : Optional[int] = ParquetDatasetReader({"train": parquet_path} , features=_lowerCAmelCase , cache_dir=_lowerCAmelCase ).read() _check_parquet_datasetdict(_lowerCAmelCase , _lowerCAmelCase ) @pytest.mark.parametrize("split" , [None, NamedSplit("train" ), "train", "test"] ) def __snake_case ( _lowerCAmelCase : str , _lowerCAmelCase : str , _lowerCAmelCase : Any ) -> Union[str, Any]: if split: A_ : Any = {split: parquet_path} else: A_ : Optional[Any] = "train" A_ : str = {"train": parquet_path, "test": parquet_path} A_ : Any = tmp_path / "cache" A_ : str = {"col_1": "string", "col_2": "int64", "col_3": "float64"} A_ : Dict = ParquetDatasetReader(_lowerCAmelCase , cache_dir=_lowerCAmelCase ).read() _check_parquet_datasetdict(_lowerCAmelCase , _lowerCAmelCase , splits=list(path.keys() ) ) assert all(dataset[split].split == split for split in path.keys() ) def __snake_case ( _lowerCAmelCase : Dict , _lowerCAmelCase : Optional[Any] ) -> Dict: A_ : List[str] = ParquetDatasetWriter(_lowerCAmelCase , tmp_path / "foo.parquet" ) assert writer.write() > 0 A_ : Tuple = pq.ParquetFile(tmp_path / "foo.parquet" ) A_ : Dict = pf.read() assert dataset.data.table == output_table def __snake_case ( _lowerCAmelCase : Optional[Any] , _lowerCAmelCase : int ) -> List[Any]: A_ : Tuple = str(shared_datadir / "test_image_rgb.jpg" ) A_ : int = {"image": [image_path]} A_ : Optional[Any] = Features({"image": Image()} ) A_ : Union[str, Any] = Dataset.from_dict(_lowerCAmelCase , features=_lowerCAmelCase ) A_ : Tuple = ParquetDatasetWriter(_lowerCAmelCase , tmp_path / "foo.parquet" ) assert writer.write() > 0 A_ : str = Dataset.from_parquet(str(tmp_path / "foo.parquet" ) ) assert dataset.features == reloaded_dataset.features A_ : int = ParquetDatasetReader(str(tmp_path / "foo.parquet" ) , streaming=_lowerCAmelCase ).read() assert dataset.features == reloaded_iterable_dataset.features @pytest.mark.parametrize( "feature, expected" , [ (Features({"foo": Value("int32" )} ), None), (Features({"image": Image(), "foo": Value("int32" )} ), config.PARQUET_ROW_GROUP_SIZE_FOR_IMAGE_DATASETS), (Features({"nested": Sequence(Audio() )} ), config.PARQUET_ROW_GROUP_SIZE_FOR_AUDIO_DATASETS), ] , ) def __snake_case ( _lowerCAmelCase : str , _lowerCAmelCase : List[Any] ) -> Any: assert get_writer_batch_size(_lowerCAmelCase ) == expected
300
0
'''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_ = logging.getLogger(__name__) require_version('''pytorch_lightning>=1.0.4''') lowerCamelCase_ = { '''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_ = { '''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_ = sorted(arg_to_scheduler.keys()) lowerCamelCase_ = '''{''' + ''', '''.join(arg_to_scheduler_choices) + '''}''' class _UpperCAmelCase ( pl.LightningModule ): """simple docstring""" def __init__( self : str , __UpperCAmelCase : argparse.Namespace , __UpperCAmelCase : Tuple=None , __UpperCAmelCase : Dict="base" , __UpperCAmelCase : Union[str, Any]=None , __UpperCAmelCase : Dict=None , __UpperCAmelCase : Union[str, Any]=None , **__UpperCAmelCase : Optional[Any] , ): '''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(__UpperCAmelCase ) _A = 0 _A = Path(self.hparams.output_dir ) _A = self.hparams.cache_dir if self.hparams.cache_dir else None if config is None: _A = 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=__UpperCAmelCase , **__UpperCAmelCase , ) else: _A = config _A = ("encoder_layerdrop", "decoder_layerdrop", "dropout", "attention_dropout") for p in extra_model_params: if getattr(self.hparams , __UpperCAmelCase , __UpperCAmelCase ): assert hasattr(self.config , __UpperCAmelCase ), f'''model config doesn\'t have a `{p}` attribute''' setattr(self.config , __UpperCAmelCase , getattr(self.hparams , __UpperCAmelCase ) ) if tokenizer is None: _A = AutoTokenizer.from_pretrained( self.hparams.tokenizer_name if self.hparams.tokenizer_name else self.hparams.model_name_or_path , cache_dir=__UpperCAmelCase , ) else: _A = tokenizer _A = MODEL_MODES[mode] if model is None: _A = 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=__UpperCAmelCase , ) else: _A = model def lowerCAmelCase ( self : Any , *__UpperCAmelCase : Optional[Any] , **__UpperCAmelCase : Union[str, Any] ): '''simple docstring''' _A = self.model_type.from_pretrained(*__UpperCAmelCase , **__UpperCAmelCase ) def lowerCAmelCase ( self : Dict ): '''simple docstring''' _A = arg_to_scheduler[self.hparams.lr_scheduler] _A = get_schedule_func( self.opt , num_warmup_steps=self.hparams.warmup_steps , num_training_steps=self.total_steps() ) _A = {"scheduler": scheduler, "interval": "step", "frequency": 1} return scheduler def lowerCAmelCase ( self : Union[str, Any] ): '''simple docstring''' _A = self.model _A = ["bias", "LayerNorm.weight"] _A = [ { "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: _A = Adafactor( __UpperCAmelCase , lr=self.hparams.learning_rate , scale_parameter=__UpperCAmelCase , relative_step=__UpperCAmelCase ) else: _A = AdamW( __UpperCAmelCase , lr=self.hparams.learning_rate , eps=self.hparams.adam_epsilon ) _A = optimizer _A = self.get_lr_scheduler() return [optimizer], [scheduler] def lowerCAmelCase ( self : List[str] , __UpperCAmelCase : int , __UpperCAmelCase : Dict ): '''simple docstring''' return self.validation_step(__UpperCAmelCase , __UpperCAmelCase ) def lowerCAmelCase ( self : List[str] , __UpperCAmelCase : Optional[Any] ): '''simple docstring''' return self.validation_end(__UpperCAmelCase ) def lowerCAmelCase ( self : List[Any] ): '''simple docstring''' _A = max(1 , self.hparams.gpus ) # TODO: consider num_tpu_cores _A = self.hparams.train_batch_size * self.hparams.accumulate_grad_batches * num_devices return (self.dataset_size / effective_batch_size) * self.hparams.max_epochs def lowerCAmelCase ( self : int , __UpperCAmelCase : Dict ): '''simple docstring''' if stage == "test": _A = len(self.test_dataloader().dataset ) else: _A = self.get_dataloader("train" , self.hparams.train_batch_size , shuffle=__UpperCAmelCase ) _A = len(self.train_dataloader().dataset ) def lowerCAmelCase ( self : Tuple , __UpperCAmelCase : str , __UpperCAmelCase : int , __UpperCAmelCase : bool = False ): '''simple docstring''' raise NotImplementedError("You must implement this for your task" ) def lowerCAmelCase ( self : List[str] ): '''simple docstring''' return self.train_loader def lowerCAmelCase ( self : str ): '''simple docstring''' return self.get_dataloader("dev" , self.hparams.eval_batch_size , shuffle=__UpperCAmelCase ) def lowerCAmelCase ( self : List[Any] ): '''simple docstring''' return self.get_dataloader("test" , self.hparams.eval_batch_size , shuffle=__UpperCAmelCase ) def lowerCAmelCase ( self : Optional[int] , __UpperCAmelCase : Tuple ): '''simple docstring''' return os.path.join( self.hparams.data_dir , "cached_{}_{}_{}".format( __UpperCAmelCase , list(filter(__UpperCAmelCase , self.hparams.model_name_or_path.split("/" ) ) ).pop() , str(self.hparams.max_seq_length ) , ) , ) @pl.utilities.rank_zero_only def lowerCAmelCase ( self : int , __UpperCAmelCase : Dict[str, Any] ): '''simple docstring''' _A = self.output_dir.joinpath("best_tfmr" ) _A = self.step_count self.model.save_pretrained(__UpperCAmelCase ) self.tokenizer.save_pretrained(__UpperCAmelCase ) @staticmethod def lowerCAmelCase ( __UpperCAmelCase : List[str] , __UpperCAmelCase : Union[str, Any] ): '''simple docstring''' parser.add_argument( "--model_name_or_path" , default=__UpperCAmelCase , type=__UpperCAmelCase , required=__UpperCAmelCase , help="Path to pretrained model or model identifier from huggingface.co/models" , ) parser.add_argument( "--config_name" , default="" , type=__UpperCAmelCase , help="Pretrained config name or path if not the same as model_name" ) parser.add_argument( "--tokenizer_name" , default=__UpperCAmelCase , type=__UpperCAmelCase , help="Pretrained tokenizer name or path if not the same as model_name" , ) parser.add_argument( "--cache_dir" , default=str(Path(__UpperCAmelCase ).parent / "test_run" / "cache" ) , type=__UpperCAmelCase , help="Where do you want to store the pre-trained models downloaded from huggingface.co" , ) parser.add_argument( "--encoder_layerdrop" , type=__UpperCAmelCase , help="Encoder layer dropout probability (Optional). Goes into model.config" , ) parser.add_argument( "--decoder_layerdrop" , type=__UpperCAmelCase , help="Decoder layer dropout probability (Optional). Goes into model.config" , ) parser.add_argument( "--dropout" , type=__UpperCAmelCase , help="Dropout probability (Optional). Goes into model.config" , ) parser.add_argument( "--attention_dropout" , type=__UpperCAmelCase , help="Attention dropout probability (Optional). Goes into model.config" , ) parser.add_argument("--learning_rate" , default=5E-5 , type=__UpperCAmelCase , help="The initial learning rate for Adam." ) parser.add_argument( "--lr_scheduler" , default="linear" , choices=__UpperCAmelCase , metavar=__UpperCAmelCase , type=__UpperCAmelCase , help="Learning rate scheduler" , ) parser.add_argument("--weight_decay" , default=0.0 , type=__UpperCAmelCase , help="Weight decay if we apply some." ) parser.add_argument("--adam_epsilon" , default=1E-8 , type=__UpperCAmelCase , help="Epsilon for Adam optimizer." ) parser.add_argument("--warmup_steps" , default=0 , type=__UpperCAmelCase , help="Linear warmup over warmup_steps." ) parser.add_argument("--num_workers" , default=4 , type=__UpperCAmelCase , help="kwarg passed to DataLoader" ) parser.add_argument("--num_train_epochs" , dest="max_epochs" , default=3 , type=__UpperCAmelCase ) parser.add_argument("--train_batch_size" , default=32 , type=__UpperCAmelCase ) parser.add_argument("--eval_batch_size" , default=32 , type=__UpperCAmelCase ) parser.add_argument("--adafactor" , action="store_true" ) class _UpperCAmelCase ( pl.Callback ): """simple docstring""" def lowerCAmelCase ( self : str , __UpperCAmelCase : List[str] , __UpperCAmelCase : Union[str, Any] ): '''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 _UpperCAmelCase ( pl.Callback ): """simple docstring""" def lowerCAmelCase ( self : Any , __UpperCAmelCase : Optional[Any] , __UpperCAmelCase : List[Any] ): '''simple docstring''' for name, param in pl_module.model.rag.named_parameters(): if param.grad is None: print(__UpperCAmelCase ) class _UpperCAmelCase ( pl.Callback ): """simple docstring""" def lowerCAmelCase ( self : Union[str, Any] , __UpperCAmelCase : List[str] , __UpperCAmelCase : str ): '''simple docstring''' _A = trainer.lr_schedulers[0]["scheduler"] _A = {f'''lr_group_{i}''': lr for i, lr in enumerate(lr_scheduler.get_lr() )} pl_module.logger.log_metrics(__UpperCAmelCase ) def lowerCAmelCase ( self : Optional[int] , __UpperCAmelCase : pl.Trainer , __UpperCAmelCase : pl.LightningModule ): '''simple docstring''' rank_zero_info("***** Validation results *****" ) _A = trainer.callback_metrics # Log results for key in sorted(__UpperCAmelCase ): if key not in ["log", "progress_bar"]: rank_zero_info("{} = {}\n".format(__UpperCAmelCase , str(metrics[key] ) ) ) def lowerCAmelCase ( self : Any , __UpperCAmelCase : pl.Trainer , __UpperCAmelCase : pl.LightningModule ): '''simple docstring''' rank_zero_info("***** Test results *****" ) _A = trainer.callback_metrics # Log and save results to file _A = os.path.join(pl_module.hparams.output_dir , "test_results.txt" ) with open(__UpperCAmelCase , "w" ) as writer: for key in sorted(__UpperCAmelCase ): if key not in ["log", "progress_bar"]: rank_zero_info("{} = {}\n".format(__UpperCAmelCase , str(metrics[key] ) ) ) writer.write("{} = {}\n".format(__UpperCAmelCase , str(metrics[key] ) ) ) def __lowercase ( __lowercase , __lowercase ) -> None: '''simple docstring''' parser.add_argument( "--output_dir" , default=str(Path(__lowercase ).parent / "test_run" / "model_checkpoints" ) , type=__lowercase , 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=__lowercase , 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=__lowercase ) parser.add_argument("--max_grad_norm" , dest="gradient_clip_val" , default=1.0 , type=__lowercase , 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=__lowercase , default=1 , help="Number of updates steps to accumulate before performing a backward/update pass." , ) parser.add_argument("--seed" , type=__lowercase , default=42 , help="random seed for initialization" ) parser.add_argument( "--data_dir" , default=str(Path(__lowercase ).parent / "test_run" / "dummy-train-data" ) , type=__lowercase , help="The input data dir. Should contain the training files for the CoNLL-2003 NER task." , ) def __lowercase ( __lowercase , __lowercase , __lowercase=None , __lowercase=True , __lowercase=[] , __lowercase=None , __lowercase=None , **__lowercase , ) -> Any: '''simple docstring''' pl.seed_everything(args.seed ) # init model _A = Path(model.hparams.output_dir ) odir.mkdir(exist_ok=__lowercase ) # add custom checkpoints if checkpoint_callback is None: _A = 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(__lowercase ) if logging_callback is None: _A = LoggingCallback() _A = {} if args.fpaa: _A = 16 if args.gpus > 1: _A = "auto" _A = "ddp" _A = args.accumulate_grad_batches _A = None _A = "auto" _A = pl.Trainer.from_argparse_args( __lowercase , weights_summary=__lowercase , callbacks=[logging_callback] + extra_callbacks + [InitCallback()] + [checkpoint_callback] , logger=__lowercase , val_check_interval=1 , num_sanity_val_steps=2 , **__lowercase , ) if args.do_train: trainer.fit(__lowercase ) else: print("RAG modeling tests with new set functions successfuly executed!" ) return trainer
79
import json import unittest import numpy as np from huggingface_hub import hf_hub_download from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from transformers import OneFormerImageProcessor from transformers.models.oneformer.image_processing_oneformer import binary_mask_to_rle from transformers.models.oneformer.modeling_oneformer import OneFormerForUniversalSegmentationOutput if is_vision_available(): from PIL import Image def __snake_case ( _lowerCAmelCase : List[str] , _lowerCAmelCase : List[Any]="shi-labs/oneformer_demo" ) -> int: with open(hf_hub_download(_lowerCAmelCase , _lowerCAmelCase , repo_type="dataset" ) , "r" ) as f: A_ : Optional[int] = json.load(_lowerCAmelCase ) A_ : Union[str, Any] = {} A_ : Tuple = [] A_ : Optional[Any] = [] for key, info in class_info.items(): A_ : Tuple = info["name"] class_names.append(info["name"] ) if info["isthing"]: thing_ids.append(int(_lowerCAmelCase ) ) A_ : Optional[Any] = thing_ids A_ : int = class_names return metadata class __magic_name__ ( unittest.TestCase ): """simple docstring""" def __init__( self :List[Any] , snake_case :List[str] , snake_case :int=7 , snake_case :Optional[int]=3 , snake_case :Union[str, Any]=30 , snake_case :Tuple=400 , snake_case :List[Any]=None , snake_case :Optional[Any]=True , snake_case :Tuple=True , snake_case :Dict=[0.5, 0.5, 0.5] , snake_case :Any=[0.5, 0.5, 0.5] , snake_case :Optional[int]=10 , snake_case :Tuple=False , snake_case :Optional[int]=255 , snake_case :Optional[Any]="shi-labs/oneformer_demo" , snake_case :Optional[Any]="ade20k_panoptic.json" , snake_case :Optional[int]=10 , ): '''simple docstring''' A_ : Tuple = parent A_ : List[str] = batch_size A_ : Optional[int] = num_channels A_ : Tuple = min_resolution A_ : List[Any] = max_resolution A_ : Union[str, Any] = do_resize A_ : Any = {"shortest_edge": 32, "longest_edge": 1_333} if size is None else size A_ : Tuple = do_normalize A_ : List[str] = image_mean A_ : List[Any] = image_std A_ : Union[str, Any] = class_info_file A_ : List[Any] = prepare_metadata(snake_case , snake_case ) A_ : Tuple = num_text A_ : str = repo_path # for the post_process_functions A_ : Any = 2 A_ : int = 10 A_ : Optional[int] = 10 A_ : Tuple = 3 A_ : Tuple = 4 A_ : str = num_labels A_ : int = do_reduce_labels A_ : List[Any] = ignore_index def SCREAMING_SNAKE_CASE ( self :Optional[Any] ): '''simple docstring''' return { "do_resize": self.do_resize, "size": self.size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, "num_labels": self.num_labels, "do_reduce_labels": self.do_reduce_labels, "ignore_index": self.ignore_index, "class_info_file": self.class_info_file, "metadata": self.metadata, "num_text": self.num_text, } def SCREAMING_SNAKE_CASE ( self :List[Any] , snake_case :Any , snake_case :Any=False ): '''simple docstring''' if not batched: A_ : List[str] = image_inputs[0] if isinstance(snake_case , Image.Image ): A_ , A_ : Dict = image.size else: A_ , A_ : Tuple = image.shape[1], image.shape[2] if w < h: A_ : str = int(self.size["shortest_edge"] * h / w ) A_ : Any = self.size["shortest_edge"] elif w > h: A_ : Optional[int] = self.size["shortest_edge"] A_ : List[str] = int(self.size["shortest_edge"] * w / h ) else: A_ : List[str] = self.size["shortest_edge"] A_ : Optional[Any] = self.size["shortest_edge"] else: A_ : Tuple = [] for image in image_inputs: A_ , A_ : Optional[Any] = self.get_expected_values([image] ) expected_values.append((expected_height, expected_width) ) A_ : Tuple = max(snake_case , key=lambda snake_case : item[0] )[0] A_ : Union[str, Any] = max(snake_case , key=lambda snake_case : item[1] )[1] return expected_height, expected_width def SCREAMING_SNAKE_CASE ( self :Tuple ): '''simple docstring''' return OneFormerForUniversalSegmentationOutput( # +1 for null class class_queries_logits=torch.randn((self.batch_size, self.num_queries, self.num_classes + 1) ) , masks_queries_logits=torch.randn((self.batch_size, self.num_queries, self.height, self.width) ) , ) @require_torch @require_vision class __magic_name__ ( lowerCamelCase__ , unittest.TestCase ): """simple docstring""" __UpperCamelCase = OneFormerImageProcessor if (is_vision_available() and is_torch_available()) else None # only for test_image_processing_common.test_image_proc_to_json_string __UpperCamelCase = image_processing_class def SCREAMING_SNAKE_CASE ( self :int ): '''simple docstring''' A_ : Union[str, Any] = OneFormerImageProcessorTester(self ) @property def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' return self.image_processing_tester.prepare_image_processor_dict() def SCREAMING_SNAKE_CASE ( self :List[Any] ): '''simple docstring''' A_ : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(snake_case , "image_mean" ) ) self.assertTrue(hasattr(snake_case , "image_std" ) ) self.assertTrue(hasattr(snake_case , "do_normalize" ) ) self.assertTrue(hasattr(snake_case , "do_resize" ) ) self.assertTrue(hasattr(snake_case , "size" ) ) self.assertTrue(hasattr(snake_case , "ignore_index" ) ) self.assertTrue(hasattr(snake_case , "class_info_file" ) ) self.assertTrue(hasattr(snake_case , "num_text" ) ) self.assertTrue(hasattr(snake_case , "repo_path" ) ) self.assertTrue(hasattr(snake_case , "metadata" ) ) self.assertTrue(hasattr(snake_case , "do_reduce_labels" ) ) def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' pass def SCREAMING_SNAKE_CASE ( self :int ): '''simple docstring''' A_ : Dict = self.image_processing_class(**self.image_processor_dict ) # create random PIL images A_ : Optional[Any] = prepare_image_inputs(self.image_processing_tester , equal_resolution=snake_case ) for image in image_inputs: self.assertIsInstance(snake_case , Image.Image ) # Test not batched input A_ : str = image_processor(image_inputs[0] , ["semantic"] , return_tensors="pt" ).pixel_values A_ , A_ : str = self.image_processing_tester.get_expected_values(snake_case ) self.assertEqual( encoded_images.shape , (1, self.image_processing_tester.num_channels, expected_height, expected_width) , ) # Test batched A_ , A_ : Optional[Any] = self.image_processing_tester.get_expected_values(snake_case , batched=snake_case ) A_ : List[str] = image_processor( snake_case , ["semantic"] * len(snake_case ) , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processing_tester.batch_size, self.image_processing_tester.num_channels, expected_height, expected_width, ) , ) def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' A_ : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors A_ : List[str] = prepare_image_inputs(self.image_processing_tester , equal_resolution=snake_case , numpify=snake_case ) for image in image_inputs: self.assertIsInstance(snake_case , np.ndarray ) # Test not batched input A_ : List[str] = image_processor(image_inputs[0] , ["semantic"] , return_tensors="pt" ).pixel_values A_ , A_ : List[str] = self.image_processing_tester.get_expected_values(snake_case ) self.assertEqual( encoded_images.shape , (1, self.image_processing_tester.num_channels, expected_height, expected_width) , ) # Test batched A_ , A_ : int = self.image_processing_tester.get_expected_values(snake_case , batched=snake_case ) A_ : Optional[Any] = image_processor( snake_case , ["semantic"] * len(snake_case ) , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processing_tester.batch_size, self.image_processing_tester.num_channels, expected_height, expected_width, ) , ) def SCREAMING_SNAKE_CASE ( self :Optional[int] ): '''simple docstring''' A_ : List[str] = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors A_ : List[str] = prepare_image_inputs(self.image_processing_tester , equal_resolution=snake_case , torchify=snake_case ) for image in image_inputs: self.assertIsInstance(snake_case , torch.Tensor ) # Test not batched input A_ : Any = image_processor(image_inputs[0] , ["semantic"] , return_tensors="pt" ).pixel_values A_ , A_ : Tuple = self.image_processing_tester.get_expected_values(snake_case ) self.assertEqual( encoded_images.shape , (1, self.image_processing_tester.num_channels, expected_height, expected_width) , ) # Test batched A_ , A_ : Tuple = self.image_processing_tester.get_expected_values(snake_case , batched=snake_case ) A_ : Any = image_processor( snake_case , ["semantic"] * len(snake_case ) , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processing_tester.batch_size, self.image_processing_tester.num_channels, expected_height, expected_width, ) , ) def SCREAMING_SNAKE_CASE ( self :Optional[Any] , snake_case :Dict=False , snake_case :str=False , snake_case :Dict="np" ): '''simple docstring''' A_ : Tuple = self.image_processing_class(**self.image_processor_dict ) # prepare image and target A_ : Tuple = self.image_processing_tester.num_labels A_ : str = None A_ : Tuple = None A_ : Tuple = prepare_image_inputs(self.image_processing_tester , equal_resolution=snake_case ) if with_segmentation_maps: A_ : List[str] = num_labels if is_instance_map: A_ : List[str] = list(range(snake_case ) ) * 2 A_ : int = dict(enumerate(snake_case ) ) A_ : List[str] = [ np.random.randint(0 , high * 2 , (img.size[1], img.size[0]) ).astype(np.uinta ) for img in image_inputs ] if segmentation_type == "pil": A_ : int = [Image.fromarray(snake_case ) for annotation in annotations] A_ : List[str] = image_processor( snake_case , ["semantic"] * len(snake_case ) , snake_case , return_tensors="pt" , instance_id_to_semantic_id=snake_case , pad_and_return_pixel_mask=snake_case , ) return inputs def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' pass def SCREAMING_SNAKE_CASE ( self :Optional[int] ): '''simple docstring''' def common(snake_case :Dict=False , snake_case :Optional[int]=None ): A_ : Tuple = self.comm_get_image_processor_inputs( with_segmentation_maps=snake_case , is_instance_map=snake_case , segmentation_type=snake_case ) A_ : Optional[Any] = inputs["mask_labels"] A_ : List[Any] = inputs["class_labels"] A_ : Optional[Any] = inputs["pixel_values"] A_ : int = inputs["text_inputs"] # check the batch_size for mask_label, class_label, text_input in zip(snake_case , snake_case , snake_case ): self.assertEqual(mask_label.shape[0] , class_label.shape[0] ) # this ensure padding has happened self.assertEqual(mask_label.shape[1:] , pixel_values.shape[2:] ) self.assertEqual(len(snake_case ) , self.image_processing_tester.num_text ) common() common(is_instance_map=snake_case ) common(is_instance_map=snake_case , segmentation_type="pil" ) common(is_instance_map=snake_case , segmentation_type="pil" ) def SCREAMING_SNAKE_CASE ( self :Optional[Any] ): '''simple docstring''' A_ : Any = np.zeros((20, 50) ) A_ : List[str] = 1 A_ : int = 1 A_ : Optional[Any] = 1 A_ : Any = binary_mask_to_rle(snake_case ) self.assertEqual(len(snake_case ) , 4 ) self.assertEqual(rle[0] , 21 ) self.assertEqual(rle[1] , 45 ) def SCREAMING_SNAKE_CASE ( self :Optional[int] ): '''simple docstring''' A_ : Union[str, Any] = self.image_processing_class( num_labels=self.image_processing_tester.num_classes , max_seq_length=77 , task_seq_length=77 , class_info_file="ade20k_panoptic.json" , num_text=self.image_processing_tester.num_text , repo_path="shi-labs/oneformer_demo" , ) A_ : Any = self.image_processing_tester.get_fake_oneformer_outputs() A_ : int = fature_extractor.post_process_semantic_segmentation(snake_case ) self.assertEqual(len(snake_case ) , self.image_processing_tester.batch_size ) self.assertEqual( segmentation[0].shape , ( self.image_processing_tester.height, self.image_processing_tester.width, ) , ) A_ : Optional[int] = [(1, 4) for i in range(self.image_processing_tester.batch_size )] A_ : List[Any] = fature_extractor.post_process_semantic_segmentation(snake_case , target_sizes=snake_case ) self.assertEqual(segmentation[0].shape , target_sizes[0] ) def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' A_ : List[str] = self.image_processing_class( num_labels=self.image_processing_tester.num_classes , max_seq_length=77 , task_seq_length=77 , class_info_file="ade20k_panoptic.json" , num_text=self.image_processing_tester.num_text , repo_path="shi-labs/oneformer_demo" , ) A_ : str = self.image_processing_tester.get_fake_oneformer_outputs() A_ : Optional[Any] = image_processor.post_process_instance_segmentation(snake_case , threshold=0 ) self.assertTrue(len(snake_case ) == self.image_processing_tester.batch_size ) for el in segmentation: self.assertTrue("segmentation" in el ) self.assertTrue("segments_info" in el ) self.assertEqual(type(el["segments_info"] ) , snake_case ) self.assertEqual( el["segmentation"].shape , (self.image_processing_tester.height, self.image_processing_tester.width) ) def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' A_ : Tuple = self.image_processing_class( num_labels=self.image_processing_tester.num_classes , max_seq_length=77 , task_seq_length=77 , class_info_file="ade20k_panoptic.json" , num_text=self.image_processing_tester.num_text , repo_path="shi-labs/oneformer_demo" , ) A_ : List[Any] = self.image_processing_tester.get_fake_oneformer_outputs() A_ : Optional[Any] = image_processor.post_process_panoptic_segmentation(snake_case , threshold=0 ) self.assertTrue(len(snake_case ) == self.image_processing_tester.batch_size ) for el in segmentation: self.assertTrue("segmentation" in el ) self.assertTrue("segments_info" in el ) self.assertEqual(type(el["segments_info"] ) , snake_case ) self.assertEqual( el["segmentation"].shape , (self.image_processing_tester.height, self.image_processing_tester.width) )
300
0
'''simple docstring''' import json import os import re import shutil import tempfile import unittest from typing import Tuple from transformers import AddedToken, BatchEncoding, ByTaTokenizer from transformers.utils import cached_property, is_tf_available, is_torch_available from ...test_tokenization_common import TokenizerTesterMixin if is_torch_available(): a__ : List[str] = 'pt' elif is_tf_available(): a__ : Union[str, Any] = 'tf' else: a__ : Dict = 'jax' class lowercase_ ( a__ , unittest.TestCase ): __UpperCAmelCase = ByTaTokenizer __UpperCAmelCase = False def __a ( self ): super().setUp() UpperCamelCase__ = ByTaTokenizer() tokenizer.save_pretrained(self.tmpdirname ) @cached_property def __a ( self ): return ByTaTokenizer.from_pretrained("google/byt5-small" ) def __a ( self , **a ): return self.tokenizer_class.from_pretrained(self.tmpdirname , **a ) def __a ( self , a , a=False , a=20 , a=5 ): # XXX The default common tokenizer tests assume that every ID is decodable on its own. # This assumption is invalid for ByT5 because single bytes might not be # valid utf-8 (byte 128 for instance). # Here we're overriding the smallest possible method to provide # a clean sequence without making the same assumption. UpperCamelCase__ = [] for i in range(len(a ) ): try: UpperCamelCase__ = tokenizer.decode([i] , clean_up_tokenization_spaces=a ) except UnicodeDecodeError: pass toks.append((i, tok) ) UpperCamelCase__ = list(filter(lambda a : re.match(r"^[ a-zA-Z]+$" , t[1] ) , a ) ) UpperCamelCase__ = list(filter(lambda a : [t[0]] == tokenizer.encode(t[1] , add_special_tokens=a ) , a ) ) if max_length is not None and len(a ) > max_length: UpperCamelCase__ = toks[:max_length] if min_length is not None and len(a ) < min_length and len(a ) > 0: while len(a ) < min_length: UpperCamelCase__ = toks + toks # toks_str = [t[1] for t in toks] UpperCamelCase__ = [t[0] for t in toks] # Ensure consistency UpperCamelCase__ = tokenizer.decode(a , clean_up_tokenization_spaces=a ) if " " not in output_txt and len(a ) > 1: UpperCamelCase__ = ( tokenizer.decode([toks_ids[0]] , clean_up_tokenization_spaces=a ) + " " + tokenizer.decode(toks_ids[1:] , clean_up_tokenization_spaces=a ) ) if with_prefix_space: UpperCamelCase__ = " " + output_txt UpperCamelCase__ = tokenizer.encode(a , add_special_tokens=a ) return output_txt, output_ids def __a ( self ): UpperCamelCase__ = self.ta_base_tokenizer UpperCamelCase__ = tokenizer(["hi</s>", "I went to the gym</s>", "</s>"] ) UpperCamelCase__ = tokenizer(["hi", "I went to the gym", ""] ) self.assertListEqual(batch_with_eos_added["input_ids"] , batch_without_eos_added["input_ids"] ) def __a ( self ): UpperCamelCase__ = self.ta_base_tokenizer UpperCamelCase__ = "Unicode €." UpperCamelCase__ = tokenizer(a ) UpperCamelCase__ = [88, 1_13, 1_08, 1_02, 1_14, 1_03, 1_04, 35, 2_29, 1_33, 1_75, 49, 1] self.assertEqual(encoded["input_ids"] , a ) # decoding UpperCamelCase__ = tokenizer.decode(a ) self.assertEqual(a , "Unicode €.</s>" ) UpperCamelCase__ = tokenizer("e è é ê ë" ) UpperCamelCase__ = [1_04, 35, 1_98, 1_71, 35, 1_98, 1_72, 35, 1_98, 1_73, 35, 1_98, 1_74, 1] self.assertEqual(encoded["input_ids"] , a ) # decoding UpperCamelCase__ = tokenizer.decode(a ) self.assertEqual(a , "e è é ê ë</s>" ) # encode/decode, but with `encode` instead of `__call__` self.assertEqual(tokenizer.decode(tokenizer.encode("e è é ê ë" ) ) , "e è é ê ë</s>" ) def __a ( self ): UpperCamelCase__ = self.ta_base_tokenizer UpperCamelCase__ = ["A long paragraph for summarization.", "Another paragraph for summarization."] # fmt: off UpperCamelCase__ = [68, 35, 1_11, 1_14, 1_13, 1_06, 35, 1_15, 1_00, 1_17, 1_00, 1_06, 1_17, 1_00, 1_15, 1_07, 35, 1_05, 1_14, 1_17, 35, 1_18, 1_20, 1_12, 1_12, 1_00, 1_17, 1_08, 1_25, 1_00, 1_19, 1_08, 1_14, 1_13, 49, 1, 0] # fmt: on UpperCamelCase__ = tokenizer(a , padding=a , return_tensors=a ) self.assertIsInstance(a , a ) if FRAMEWORK != "jax": UpperCamelCase__ = list(batch.input_ids.numpy()[0] ) else: UpperCamelCase__ = list(batch.input_ids.tolist()[0] ) self.assertListEqual(a , a ) self.assertEqual((2, 37) , batch.input_ids.shape ) self.assertEqual((2, 37) , batch.attention_mask.shape ) def __a ( self ): UpperCamelCase__ = self.ta_base_tokenizer UpperCamelCase__ = ["A long paragraph for summarization.", "Another paragraph for summarization."] UpperCamelCase__ = tokenizer(a , padding=a , return_tensors=a ) # check if input_ids are returned and no decoder_input_ids self.assertIn("input_ids" , a ) self.assertIn("attention_mask" , a ) self.assertNotIn("decoder_input_ids" , a ) self.assertNotIn("decoder_attention_mask" , a ) def __a ( self ): UpperCamelCase__ = self.ta_base_tokenizer UpperCamelCase__ = [ "Summary of the text.", "Another summary.", ] UpperCamelCase__ = tokenizer( text_target=a , max_length=32 , padding="max_length" , truncation=a , return_tensors=a ) self.assertEqual(32 , targets["input_ids"].shape[1] ) def __a ( self ): UpperCamelCase__ = self.ta_base_tokenizer UpperCamelCase__ = ["A long paragraph for summarization. </s>"] UpperCamelCase__ = ["Summary of the text. </s>"] # fmt: off UpperCamelCase__ = [68, 35, 1_11, 1_14, 1_13, 1_06, 35, 1_15, 1_00, 1_17, 1_00, 1_06, 1_17, 1_00, 1_15, 1_07, 35, 1_05, 1_14, 1_17, 35, 1_18, 1_20, 1_12, 1_12, 1_00, 1_17, 1_08, 1_25, 1_00, 1_19, 1_08, 1_14, 1_13, 49, 35, 1] UpperCamelCase__ = [86, 1_20, 1_12, 1_12, 1_00, 1_17, 1_24, 35, 1_14, 1_05, 35, 1_19, 1_07, 1_04, 35, 1_19, 1_04, 1_23, 1_19, 49, 35, 1] # fmt: on UpperCamelCase__ = tokenizer(a , text_target=a ) self.assertEqual(a , batch["input_ids"][0] ) self.assertEqual(a , batch["labels"][0] ) def __a ( self ): # safety check on max_len default value so we are sure the test works UpperCamelCase__ = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(f'''{tokenizer.__class__.__name__}''' ): self.assertNotEqual(tokenizer.model_max_length , 42 ) # Now let's start the test UpperCamelCase__ = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(f'''{tokenizer.__class__.__name__}''' ): # Isolate this from the other tests because we save additional tokens/etc UpperCamelCase__ = tempfile.mkdtemp() UpperCamelCase__ = " He is very happy, UNwant\u00E9d,running" UpperCamelCase__ = tokenizer.encode(a , add_special_tokens=a ) tokenizer.save_pretrained(a ) UpperCamelCase__ = tokenizer.__class__.from_pretrained(a ) UpperCamelCase__ = after_tokenizer.encode(a , add_special_tokens=a ) self.assertListEqual(a , a ) shutil.rmtree(a ) UpperCamelCase__ = self.get_tokenizers(model_max_length=42 ) for tokenizer in tokenizers: with self.subTest(f'''{tokenizer.__class__.__name__}''' ): # Isolate this from the other tests because we save additional tokens/etc UpperCamelCase__ = tempfile.mkdtemp() UpperCamelCase__ = " He is very happy, UNwant\u00E9d,running" tokenizer.add_tokens(["bim", "bambam"] ) UpperCamelCase__ = tokenizer.additional_special_tokens additional_special_tokens.append("new_additional_special_token" ) tokenizer.add_special_tokens({"additional_special_tokens": additional_special_tokens} ) UpperCamelCase__ = tokenizer.encode(a , add_special_tokens=a ) tokenizer.save_pretrained(a ) UpperCamelCase__ = tokenizer.__class__.from_pretrained(a ) UpperCamelCase__ = after_tokenizer.encode(a , add_special_tokens=a ) self.assertListEqual(a , a ) self.assertIn("new_additional_special_token" , after_tokenizer.additional_special_tokens ) self.assertEqual(after_tokenizer.model_max_length , 42 ) UpperCamelCase__ = tokenizer.__class__.from_pretrained(a , model_max_length=43 ) self.assertEqual(tokenizer.model_max_length , 43 ) shutil.rmtree(a ) def __a ( self ): UpperCamelCase__ = [] if self.test_slow_tokenizer: tokenizer_list.append((self.tokenizer_class, self.get_tokenizer()) ) if self.test_rust_tokenizer: tokenizer_list.append((self.rust_tokenizer_class, self.get_rust_tokenizer()) ) for tokenizer_class, tokenizer_utils in tokenizer_list: with tempfile.TemporaryDirectory() as tmp_dir: tokenizer_utils.save_pretrained(a ) with open(os.path.join(a , "special_tokens_map.json" ) , encoding="utf-8" ) as json_file: UpperCamelCase__ = json.load(a ) with open(os.path.join(a , "tokenizer_config.json" ) , encoding="utf-8" ) as json_file: UpperCamelCase__ = json.load(a ) UpperCamelCase__ = [f'''<extra_id_{i}>''' for i in range(1_25 )] UpperCamelCase__ = added_tokens_extra_ids + [ "an_additional_special_token" ] UpperCamelCase__ = added_tokens_extra_ids + [ "an_additional_special_token" ] with open(os.path.join(a , "special_tokens_map.json" ) , "w" , encoding="utf-8" ) as outfile: json.dump(a , a ) with open(os.path.join(a , "tokenizer_config.json" ) , "w" , encoding="utf-8" ) as outfile: json.dump(a , a ) # the following checks allow us to verify that our test works as expected, i.e. that the tokenizer takes # into account the new value of additional_special_tokens given in the "tokenizer_config.json" and # "special_tokens_map.json" files UpperCamelCase__ = tokenizer_class.from_pretrained( a , ) self.assertIn( "an_additional_special_token" , tokenizer_without_change_in_init.additional_special_tokens ) # self.assertIn("an_additional_special_token",tokenizer_without_change_in_init.get_vocab()) # ByT5Tokenization no vocab self.assertEqual( ["an_additional_special_token"] , tokenizer_without_change_in_init.convert_ids_to_tokens( tokenizer_without_change_in_init.convert_tokens_to_ids(["an_additional_special_token"] ) ) , ) # Now we test that we can change the value of additional_special_tokens in the from_pretrained UpperCamelCase__ = added_tokens_extra_ids + [AddedToken("a_new_additional_special_token" , lstrip=a )] UpperCamelCase__ = tokenizer_class.from_pretrained( a , additional_special_tokens=a , ) self.assertIn("a_new_additional_special_token" , tokenizer.additional_special_tokens ) self.assertEqual( ["a_new_additional_special_token"] , tokenizer.convert_ids_to_tokens( tokenizer.convert_tokens_to_ids(["a_new_additional_special_token"] ) ) , ) def __a ( self ): UpperCamelCase__ = [] if self.test_slow_tokenizer: tokenizer_list.append((self.tokenizer_class, self.get_tokenizer()) ) if self.test_rust_tokenizer: tokenizer_list.append((self.rust_tokenizer_class, self.get_rust_tokenizer()) ) for tokenizer_class, tokenizer_utils in tokenizer_list: with tempfile.TemporaryDirectory() as tmp_dir: tokenizer_utils.save_pretrained(a ) UpperCamelCase__ = tokenizer_class.from_pretrained(a ) self.assertTrue(tokenizer.decode([2_55] ) == "" ) def __a ( self ): pass def __a ( self ): pass def __a ( self ): pass def __a ( self ): pass def __a ( self ): # The default common tokenizer tests uses invalid tokens for ByT5 that can only accept one-character strings # and special added tokens as tokens UpperCamelCase__ = self.get_tokenizers(fast=a , do_lower_case=a ) for tokenizer in tokenizers: with self.subTest(f'''{tokenizer.__class__.__name__}''' ): UpperCamelCase__ = ["t", "h", "i", "s", " ", "i", "s", " ", "a", " ", "t", "e", "x", "t", "</s>"] UpperCamelCase__ = tokenizer.convert_tokens_to_string(a ) self.assertIsInstance(a , a ) def __a ( self ): UpperCamelCase__ = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(f'''{tokenizer.__class__.__name__}''' ): UpperCamelCase__ = [ "bos_token", "eos_token", "unk_token", "sep_token", "pad_token", "cls_token", "mask_token", ] UpperCamelCase__ = 0 UpperCamelCase__ = tokenizer.convert_ids_to_tokens( a , skip_special_tokens=a ) for attr in attributes_list: setattr(a , attr + "_id" , a ) self.assertEqual(getattr(a , a ) , a ) self.assertEqual(getattr(a , attr + "_id" ) , a ) setattr(a , attr + "_id" , a ) self.assertEqual(getattr(a , a ) , a ) self.assertEqual(getattr(a , attr + "_id" ) , a ) setattr(a , "additional_special_tokens_ids" , [] ) self.assertListEqual(getattr(a , "additional_special_tokens" ) , [] ) self.assertListEqual(getattr(a , "additional_special_tokens_ids" ) , [] ) setattr(a , "additional_special_tokens_ids" , [token_id_to_test_setters] ) self.assertListEqual(getattr(a , "additional_special_tokens" ) , [token_to_test_setters] ) self.assertListEqual(getattr(a , "additional_special_tokens_ids" ) , [token_id_to_test_setters] )
80
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 _lowerCAmelCase : List[str] = logging.get_logger(__name__) _lowerCAmelCase : Optional[Any] = { '''facebook/data2vec-vision-base-ft''': ( '''https://huggingface.co/facebook/data2vec-vision-base-ft/resolve/main/config.json''' ), } class __magic_name__ ( lowerCamelCase__ ): """simple docstring""" __UpperCamelCase = '''data2vec-vision''' def __init__( self :int , snake_case :Optional[int]=768 , snake_case :Any=12 , snake_case :Any=12 , snake_case :Tuple=3_072 , snake_case :Any="gelu" , snake_case :Tuple=0.0 , snake_case :int=0.0 , snake_case :Any=0.02 , snake_case :str=1e-12 , snake_case :List[str]=224 , snake_case :Dict=16 , snake_case :int=3 , snake_case :int=False , snake_case :str=False , snake_case :List[Any]=False , snake_case :Optional[Any]=False , snake_case :Tuple=0.1 , snake_case :Optional[Any]=0.1 , snake_case :Any=True , snake_case :Optional[Any]=[3, 5, 7, 11] , snake_case :Dict=[1, 2, 3, 6] , snake_case :int=True , snake_case :List[Any]=0.4 , snake_case :Any=256 , snake_case :Union[str, Any]=1 , snake_case :Union[str, Any]=False , snake_case :Any=255 , **snake_case :int , ): '''simple docstring''' super().__init__(**snake_case ) A_ : Dict = hidden_size A_ : Tuple = num_hidden_layers A_ : List[str] = num_attention_heads A_ : Any = intermediate_size A_ : Optional[Any] = hidden_act A_ : Any = hidden_dropout_prob A_ : List[str] = attention_probs_dropout_prob A_ : Optional[Any] = initializer_range A_ : List[str] = layer_norm_eps A_ : str = image_size A_ : Optional[int] = patch_size A_ : int = num_channels A_ : Optional[Any] = use_mask_token A_ : Optional[Any] = use_absolute_position_embeddings A_ : Optional[int] = use_relative_position_bias A_ : Dict = use_shared_relative_position_bias A_ : Any = layer_scale_init_value A_ : Optional[Any] = drop_path_rate A_ : Dict = use_mean_pooling # decode head attributes (semantic segmentation) A_ : Tuple = out_indices A_ : Optional[Any] = pool_scales # auxiliary head attributes (semantic segmentation) A_ : str = use_auxiliary_head A_ : List[Any] = auxiliary_loss_weight A_ : List[str] = auxiliary_channels A_ : Dict = auxiliary_num_convs A_ : List[str] = auxiliary_concat_input A_ : Optional[int] = semantic_loss_ignore_index class __magic_name__ ( lowerCamelCase__ ): """simple docstring""" __UpperCamelCase = version.parse('''1.11''' ) @property def SCREAMING_SNAKE_CASE ( self :Union[str, Any] ): '''simple docstring''' return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ] ) @property def SCREAMING_SNAKE_CASE ( self :Tuple ): '''simple docstring''' return 1e-4
300
0