code: 
  
  '''simple docstring'''
  
  
  
  import concurrent.futures
  import json
  import os
  import shutil
  import tempfile
  import unittest
  
  from transformers import AutoTokenizer, PreTrainedTokenizerFast
  from transformers.testing_utils import require_tokenizers
  
  from ..test_tokenization_common import TokenizerTesterMixin
  
  
  
  
  
  @require_tokenizers
  class 			A							(       __UpperCAmelCase		,						unittest.TestCase				):
  			lowerCamelCase							:							List[str]       		=  PreTrainedTokenizerFast
  			lowerCamelCase							:							Tuple       		=  False
  			lowerCamelCase							:							Dict       		=  True
  			lowerCamelCase							:							Any       		=  """tokenizer_file"""
  
  
  
  			def A__ (					self       )			->		Union[str, Any]:
  
  
  
  
  									'''simple docstring'''
  
  
  
  									lowercase__							 =       False  # because we don't have pretrained_vocab_files_map
  									super().setUp()
  									lowercase__							 =       True
  
  									lowercase__							 =       ["""robot-test/dummy-tokenizer-fast""", """robot-test/dummy-tokenizer-wordlevel"""]
  									lowercase__							 =       """SaulLu/dummy-tokenizer-bytelevel-bpe"""
  
  									# Inclusion of 2 tokenizers to test different types of models (Unigram and WordLevel for the moment)
  									lowercase__							 =       [(PreTrainedTokenizerFast, model_path, {}) for model_path in model_paths]
  
  									lowercase__							 =       PreTrainedTokenizerFast.from_pretrained(model_paths[0]       )
  									tokenizer.save_pretrained(self.tmpdirname       )
  
  
  
  			def A__ (					self       )			->		List[str]:
  
  
  
  
  									'''simple docstring'''
  
  
  
  									pass
  
  
  
  			def A__ (					self       )			->		int:
  
  
  
  
  									'''simple docstring'''
  
  
  
  									pass
  
  
  
  			def A__ (					self       )			->		Optional[int]:
  
  
  
  
  									'''simple docstring'''
  
  
  
  									pass
  
  
  
  			def A__ (					self       )			->		str:
  
  
  
  
  									'''simple docstring'''
  
  
  
  									pass
  
  
  
  			def A__ (					self       )			->		str:
  
  
  
  
  									'''simple docstring'''
  
  
  
  									lowercase__							 =       self.tmpdirname
  									# Here we want to test the 2 available tokenizers that use 2 different types of models: Unigram and WordLevel.
  									for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
  															with self.subTest(F'''{tokenizer.__class__.__name__} ({pretrained_name})'''       ):
  																					try:
  																											lowercase__							 =       tempfile.mkdtemp()
  																											lowercase__							 =       self.rust_tokenizer_class.from_pretrained(lowerCamelCase__       ,     **lowerCamelCase__       )
  
  																											tokenizer.save_pretrained(self.tmpdirname       )
  																											super().test_training_new_tokenizer()
  																					finally:
  																											# Even if the test fails, we must be sure that the folder is deleted and that the default tokenizer
  																											# is restored
  																											shutil.rmtree(self.tmpdirname       )
  																											lowercase__							 =       tmpdirname_orig
  
  
  
  			def A__ (					self       )			->		int:
  
  
  
  
  									'''simple docstring'''
  
  
  
  									lowercase__							 =       self.tmpdirname
  									# Here we want to test the 2 available tokenizers that use 2 different types of models: Unigram and WordLevel.
  									for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
  															with self.subTest(F'''{tokenizer.__class__.__name__} ({pretrained_name})'''       ):
  																					try:
  																											lowercase__							 =       tempfile.mkdtemp()
  																											lowercase__							 =       self.rust_tokenizer_class.from_pretrained(lowerCamelCase__       ,     **lowerCamelCase__       )
  
  																											tokenizer.save_pretrained(self.tmpdirname       )
  																											super().test_training_new_tokenizer_with_special_tokens_change()
  																					finally:
  																											# Even if the test fails, we must be sure that the folder is deleted and that the default tokenizer
  																											# is restored
  																											shutil.rmtree(self.tmpdirname       )
  																											lowercase__							 =       tmpdirname_orig
  
  
  
  			def A__ (					self       )			->		List[str]:
  
  
  
  
  									'''simple docstring'''
  
  
  
  									lowercase__							 =       self.rust_tokenizer_class.from_pretrained(self.bytelevel_bpe_model_name       )
  
  									lowercase__							 =       ("""a""" for _ in range(1_000       ))
  									lowercase__							 =       tokenizer.train_new_from_iterator(text_iterator=lowerCamelCase__       ,     length=1_000       ,     vocab_size=50       )
  
  									lowercase__							 =       new_tokenizer.encode("""a🤗"""       )
  									self.assertEqual(lowerCamelCase__       ,     [64, 172, 253, 97, 245]       )
  
  
  
  
  
  @require_tokenizers
  class 			A							(       unittest.TestCase				):
  
  
  
  			def A__ (					self       )			->		str:
  
  
  
  
  									'''simple docstring'''
  
  
  
  									lowercase__							 =       AutoTokenizer.from_pretrained("""bert-base-cased"""       )
  									lowercase__							 =       json.loads(tokenizer._tokenizer.to_str()       )
  									lowercase__							 =       len(lowerCamelCase__       )
  
  									with tempfile.TemporaryDirectory() as tmp_dir:
  															# Hack to save this in the tokenizer_config.json
  															lowercase__							 =       ["""tokenizer.4.0.0.json"""]
  															tokenizer.save_pretrained(lowerCamelCase__       )
  															json.dump(lowerCamelCase__       ,     open(os.path.join(lowerCamelCase__       ,     """tokenizer.4.0.0.json"""       )       ,     """w"""       )       )
  
  															# This should pick the new tokenizer file as the version of Transformers is > 4.0.0
  															lowercase__							 =       AutoTokenizer.from_pretrained(lowerCamelCase__       )
  															self.assertEqual(len(lowerCamelCase__       )       ,     len(lowerCamelCase__       ) + 1       )
  															lowercase__							 =       json.loads(new_tokenizer._tokenizer.to_str()       )
  															self.assertIn("""huggingface"""       ,     json_tokenizer["""model"""]["""vocab"""]       )
  
  															# Will need to be adjusted if we reach v42 and this test is still here.
  															# Should pick the old tokenizer file as the version of Transformers is < 4.0.0
  															shutil.move(os.path.join(lowerCamelCase__       ,     """tokenizer.4.0.0.json"""       )       ,     os.path.join(lowerCamelCase__       ,     """tokenizer.42.0.0.json"""       )       )
  															lowercase__							 =       ["""tokenizer.42.0.0.json"""]
  															tokenizer.save_pretrained(lowerCamelCase__       )
  															lowercase__							 =       AutoTokenizer.from_pretrained(lowerCamelCase__       )
  															self.assertEqual(len(lowerCamelCase__       )       ,     len(lowerCamelCase__       )       )
  															lowercase__							 =       json.loads(new_tokenizer._tokenizer.to_str()       )
  															self.assertNotIn("""huggingface"""       ,     json_tokenizer["""model"""]["""vocab"""]       )
  
  
  
  			def A__ (					self       )			->		Any:
  
  
  
  
  									'''simple docstring'''
  
  
  
  									lowercase__							 =       """hf-internal-testing/test-two-tokenizers"""
  
  									# This should pick the new tokenizer file as the version of Transformers is > 4.0.0
  									lowercase__							 =       AutoTokenizer.from_pretrained(lowerCamelCase__       )
  									self.assertEqual(len(lowerCamelCase__       )       ,     28_997       )
  									lowercase__							 =       json.loads(tokenizer._tokenizer.to_str()       )
  									self.assertIn("""huggingface"""       ,     json_tokenizer["""model"""]["""vocab"""]       )
  
  									# Testing an older version by monkey-patching the version in the module it's used.
  									import transformers as old_transformers
  
  									lowercase__							 =       """3.0.0"""
  									lowercase__							 =       old_transformers.models.auto.AutoTokenizer.from_pretrained(lowerCamelCase__       )
  									self.assertEqual(len(lowerCamelCase__       )       ,     28_996       )
  									lowercase__							 =       json.loads(old_tokenizer._tokenizer.to_str()       )
  									self.assertNotIn("""huggingface"""       ,     json_tokenizer["""model"""]["""vocab"""]       )
  
  
  
  
  
  @require_tokenizers
  class 			A							(       unittest.TestCase				):
  
  
  
  			def A__ (					self       )			->		List[Any]:
  
  
  
  
  									'''simple docstring'''
  
  
  
  									lowercase__							 =       PreTrainedTokenizerFast.from_pretrained("""robot-test/dummy-tokenizer-wordlevel"""       )
  									lowercase__							 =       """The Matrix is a 1999 science fiction action film."""
  
  									with concurrent.futures.ThreadPoolExecutor() as executor:
  															lowercase__							 =       [executor.submit(self.fetch       ,     lowerCamelCase__       ,     lowerCamelCase__       ) for i in range(10       )]
  															lowercase__							 =       [future.result() for future in futures]
  															self.assertEqual(lowerCamelCase__       ,     [[1, 10, 0, 8, 0, 18, 0, 0, 0, 2] for i in range(10       )]       )
  
  
  
  			def A__ (					self       ,     lowerCamelCase__       ,     lowerCamelCase__       )			->		Any:
  
  
  
  
  									'''simple docstring'''
  
  
  
  									return tokenizer.encode(lowerCamelCase__       ,     truncation="""longest_first"""       ,     padding="""longest"""       )
  
  
  
  
  
  
  
style_context: 
  '''simple docstring'''
  
  
  
  # DISCLAIMER: This file is strongly influenced by https://github.com/yang-song/score_sde_pytorch
  
  from dataclasses import dataclass
  from typing import Optional, Tuple, Union
  
  import flax
  import jax.numpy as jnp
  from jax import random
  
  from ..configuration_utils import ConfigMixin, register_to_config
  from .scheduling_utils_flax import FlaxSchedulerMixin, FlaxSchedulerOutput, broadcast_to_shape_from_left
  
  
  
  
  
  @flax.struct.dataclass
  class 			A							:
  			# setable values
  			lowerCamelCase							:							Optional[jnp.ndarray]       		=  None
  			lowerCamelCase							:							Optional[jnp.ndarray]       		=  None
  			lowerCamelCase							:							Optional[jnp.ndarray]       		=  None
  
  
  
  			@classmethod
  			def A__ (					cls       )			->		Optional[int]:
  
  
  
  
  									'''simple docstring'''
  
  
  
  									return cls()
  
  
  
  
  
  @dataclass
  class 			A							(       __UpperCAmelCase				):
  			lowerCamelCase							:							ScoreSdeVeSchedulerState
  			lowerCamelCase							:							jnp.ndarray
  			lowerCamelCase							:							Optional[jnp.ndarray]       		=  None
  
  
  
  
  
  class 			A							(       __UpperCAmelCase		,						__UpperCAmelCase				):
  
  
  
  			@property
  			def A__ (					self       )			->		Tuple:
  
  
  
  
  									'''simple docstring'''
  
  
  
  									return True
  
  
  
  			@register_to_config
  			def __init__(					self       ,     lowerCamelCase__ = 2_000       ,     lowerCamelCase__ = 0.15       ,     lowerCamelCase__ = 0.01       ,     lowerCamelCase__ = 13_48.0       ,     lowerCamelCase__ = 1e-5       ,     lowerCamelCase__ = 1       ,     )			->		str:
  
  
  
  
  									'''simple docstring'''
  
  
  
  									pass
  
  
  
  			def A__ (					self       )			->		List[Any]:
  
  
  
  
  									'''simple docstring'''
  
  
  
  									lowercase__							 =       ScoreSdeVeSchedulerState.create()
  									return self.set_sigmas(
  									    lowerCamelCase__       ,     self.config.num_train_timesteps       ,     self.config.sigma_min       ,     self.config.sigma_max       ,     self.config.sampling_eps       ,     )
  
  
  
  			def A__ (					self       ,     lowerCamelCase__       ,     lowerCamelCase__       ,     lowerCamelCase__ = ()       ,     lowerCamelCase__ = None       )			->		ScoreSdeVeSchedulerState:
  
  
  
  
  									'''simple docstring'''
  
  
  
  									lowercase__							 =       sampling_eps if sampling_eps is not None else self.config.sampling_eps
  
  									lowercase__							 =       jnp.linspace(1       ,     lowerCamelCase__       ,     lowerCamelCase__       )
  									return state.replace(timesteps=lowerCamelCase__       )
  
  
  
  			def A__ (					self       ,     lowerCamelCase__       ,     lowerCamelCase__       ,     lowerCamelCase__ = None       ,     lowerCamelCase__ = None       ,     lowerCamelCase__ = None       ,     )			->		ScoreSdeVeSchedulerState:
  
  
  
  
  									'''simple docstring'''
  
  
  
  									lowercase__							 =       sigma_min if sigma_min is not None else self.config.sigma_min
  									lowercase__							 =       sigma_max if sigma_max is not None else self.config.sigma_max
  									lowercase__							 =       sampling_eps if sampling_eps is not None else self.config.sampling_eps
  									if state.timesteps is None:
  															lowercase__							 =       self.set_timesteps(lowerCamelCase__       ,     lowerCamelCase__       ,     lowerCamelCase__       )
  
  									lowercase__							 =       jnp.exp(jnp.linspace(jnp.log(lowerCamelCase__       )       ,     jnp.log(lowerCamelCase__       )       ,     lowerCamelCase__       )       )
  									lowercase__							 =       jnp.array([sigma_min * (sigma_max / sigma_min) ** t for t in state.timesteps]       )
  
  									return state.replace(discrete_sigmas=lowerCamelCase__       ,     sigmas=lowerCamelCase__       )
  
  
  
  			def A__ (					self       ,     lowerCamelCase__       ,     lowerCamelCase__       ,     lowerCamelCase__       )			->		List[str]:
  
  
  
  
  									'''simple docstring'''
  
  
  
  									return jnp.where(timesteps == 0       ,     jnp.zeros_like(lowerCamelCase__       )       ,     state.discrete_sigmas[timesteps - 1]       )
  
  
  
  			def A__ (					self       ,     lowerCamelCase__       ,     lowerCamelCase__       ,     lowerCamelCase__       ,     lowerCamelCase__       ,     lowerCamelCase__       ,     lowerCamelCase__ = True       ,     )			->		Union[FlaxSdeVeOutput, Tuple]:
  
  
  
  
  									'''simple docstring'''
  
  
  
  									if state.timesteps is None:
  															raise ValueError(
  															    """`state.timesteps` is not set, you need to run 'set_timesteps' after creating the scheduler"""       )
  
  									lowercase__							 =       timestep * jnp.ones(
  									    sample.shape[0]       ,     )
  									lowercase__							 =       (timestep * (len(state.timesteps       ) - 1)).long()
  
  									lowercase__							 =       state.discrete_sigmas[timesteps]
  									lowercase__							 =       self.get_adjacent_sigma(lowerCamelCase__       ,     lowerCamelCase__       ,     lowerCamelCase__       )
  									lowercase__							 =       jnp.zeros_like(lowerCamelCase__       )
  									lowercase__							 =       (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
  									lowercase__							 =       diffusion.flatten()
  									lowercase__							 =       broadcast_to_shape_from_left(lowerCamelCase__       ,     sample.shape       )
  									lowercase__							 =       drift - diffusion**2 * model_output
  
  									#  equation 6: sample noise for the diffusion term of
  									lowercase__							 =       random.split(lowerCamelCase__       ,     num=1       )
  									lowercase__							 =       random.normal(key=lowerCamelCase__       ,     shape=sample.shape       )
  									lowercase__							 =       sample - drift  # subtract because `dt` is a small negative timestep
  									# TODO is the variable diffusion the correct scaling term for the noise?
  									lowercase__							 =       prev_sample_mean + diffusion * noise  # add impact of diffusion field g
  
  									if not return_dict:
  															return (prev_sample, prev_sample_mean, state)
  
  									return FlaxSdeVeOutput(prev_sample=lowerCamelCase__       ,     prev_sample_mean=lowerCamelCase__       ,     state=lowerCamelCase__       )
  
  
  
  			def A__ (					self       ,     lowerCamelCase__       ,     lowerCamelCase__       ,     lowerCamelCase__       ,     lowerCamelCase__       ,     lowerCamelCase__ = True       ,     )			->		Union[FlaxSdeVeOutput, Tuple]:
  
  
  
  
  									'''simple docstring'''
  
  
  
  									if state.timesteps is None:
  															raise ValueError(
  															    """`state.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
  									lowercase__							 =       random.split(lowerCamelCase__       ,     num=1       )
  									lowercase__							 =       random.normal(key=lowerCamelCase__       ,     shape=sample.shape       )
  
  									# compute step size from the model_output, the noise, and the snr
  									lowercase__							 =       jnp.linalg.norm(lowerCamelCase__       )
  									lowercase__							 =       jnp.linalg.norm(lowerCamelCase__       )
  									lowercase__							 =       (self.config.snr * noise_norm / grad_norm) ** 2 * 2
  									lowercase__							 =       step_size * jnp.ones(sample.shape[0]       )
  
  									# compute corrected sample: model_output term and noise term
  									lowercase__							 =       step_size.flatten()
  									lowercase__							 =       broadcast_to_shape_from_left(lowerCamelCase__       ,     sample.shape       )
  									lowercase__							 =       sample + step_size * model_output
  									lowercase__							 =       prev_sample_mean + ((step_size * 2) ** 0.5) * noise
  
  									if not return_dict:
  															return (prev_sample, state)
  
  									return FlaxSdeVeOutput(prev_sample=lowerCamelCase__       ,     state=lowerCamelCase__       )
  
  
  
  			def __len__(					self       )			->		Union[str, Any]:
  
  
  
  
  									'''simple docstring'''
  
  
  
  									return self.config.num_train_timesteps
  
  
  
  
  
  
  