code: 
  
  
  
  
  
  
  from dataclasses import dataclass
  from typing import Optional, Tuple, Union
  
  import flax
  import jax.numpy as jnp
  from scipy import integrate
  
  from ..configuration_utils import ConfigMixin, register_to_config
  from .scheduling_utils_flax import (
      CommonSchedulerState,
      FlaxKarrasDiffusionSchedulers,
      FlaxSchedulerMixin,
      FlaxSchedulerOutput,
      broadcast_to_shape_from_left,
  )
  
  
  
  
  
  
  
  @flax.struct.dataclass
  class 							UpperCAmelCase_     :
       __lowerCamelCase										=     42
  
       # setable values
       __lowerCamelCase										=     42
       __lowerCamelCase										=     42
       __lowerCamelCase										=     42
       __lowerCamelCase										=     None
  
       # running values
       __lowerCamelCase										=     None
  
  
  
  
       @classmethod
       def      __UpperCAmelCase     (							cls  ,	_lowerCAmelCase  ,	_lowerCAmelCase  ,	_lowerCAmelCase  ,	_lowerCAmelCase		):
            return cls(common=_lowerCAmelCase  ,	init_noise_sigma=_lowerCAmelCase  ,	timesteps=_lowerCAmelCase  ,	sigmas=_lowerCAmelCase		)
  
  
  
  
  
  
  
  @dataclass
  class 							UpperCAmelCase_     (		__lowerCamelCase		):
       __lowerCamelCase										=     42
  
  
  
  
  
  
  
  class 							UpperCAmelCase_     (		__lowerCamelCase       ,			__lowerCamelCase		):
       __lowerCamelCase										=     [e.name for e in FlaxKarrasDiffusionSchedulers]
  
       __lowerCamelCase										=     42
  
  
  
  
       @property
       def      __UpperCAmelCase     (							self		):
            return True
  
  
  
  
       @register_to_config
       def __init__(							self  ,	_lowerCAmelCase = 1000  ,	_lowerCAmelCase = 0.0_0_0_1  ,	_lowerCAmelCase = 0.0_2  ,	_lowerCAmelCase = "linear"  ,	_lowerCAmelCase = None  ,	_lowerCAmelCase = "epsilon"  ,	_lowerCAmelCase = jnp.floataa  ,	):
            UpperCAmelCase__			:     Optional[Any]										= dtype
  
  
  
  
       def      __UpperCAmelCase     (							self  ,	_lowerCAmelCase = None		):
            if common is None:
                 UpperCAmelCase__			:     Optional[int]										= CommonSchedulerState.create(self		)
  
            UpperCAmelCase__			:     Any										= jnp.arange(0  ,	self.config.num_train_timesteps		).round()[::-1]
            UpperCAmelCase__			:     Optional[int]										= ((1 - common.alphas_cumprod) / common.alphas_cumprod) ** 0.5
  
            # standard deviation of the initial noise distribution
            UpperCAmelCase__			:     List[str]										= sigmas.max()
  
            return LMSDiscreteSchedulerState.create(
                common=_lowerCAmelCase  ,	init_noise_sigma=_lowerCAmelCase  ,	timesteps=_lowerCAmelCase  ,	sigmas=_lowerCAmelCase  ,	)
  
  
  
  
       def      __UpperCAmelCase     (							self  ,	_lowerCAmelCase  ,	_lowerCAmelCase  ,	_lowerCAmelCase		):
            ((UpperCAmelCase__)			,		)			:     Optional[int]										= jnp.where(state.timesteps == timestep  ,	size=1		)
            UpperCAmelCase__			:     Tuple										= step_index[0]
  
            UpperCAmelCase__			:     List[str]										= state.sigmas[step_index]
            UpperCAmelCase__			:     Optional[Any]										= sample / ((sigma**2 + 1) ** 0.5)
            return sample
  
  
  
  
       def      __UpperCAmelCase     (							self  ,	_lowerCAmelCase  ,	_lowerCAmelCase  ,	_lowerCAmelCase  ,	_lowerCAmelCase		):
  
            def lms_derivative(_lowerCAmelCase		):
                 UpperCAmelCase__			:     List[Any]										= 1.0
                 for k in range(_lowerCAmelCase		):
                      if current_order == k:
                           continue
                      prod *= (tau - state.sigmas[t - k]) / (state.sigmas[t - current_order] - state.sigmas[t - k])
                 return prod
  
            UpperCAmelCase__			:     Optional[Any]										= integrate.quad(_lowerCAmelCase  ,	state.sigmas[t]  ,	state.sigmas[t + 1]  ,	epsrel=1e-4		)[0]
  
            return integrated_coeff
  
  
  
  
       def      __UpperCAmelCase     (							self  ,	_lowerCAmelCase  ,	_lowerCAmelCase  ,	_lowerCAmelCase = ()		):
            UpperCAmelCase__			:     List[Any]										= jnp.linspace(self.config.num_train_timesteps - 1  ,	0  ,	_lowerCAmelCase  ,	dtype=self.dtype		)
  
            UpperCAmelCase__			:     Tuple										= jnp.floor(_lowerCAmelCase		).astype(jnp.intaa		)
            UpperCAmelCase__			:     Optional[Any]										= jnp.ceil(_lowerCAmelCase		).astype(jnp.intaa		)
  
            UpperCAmelCase__			:     List[Any]										= jnp.mod(_lowerCAmelCase  ,	1.0		)
  
            UpperCAmelCase__			:     int										= ((1 - state.common.alphas_cumprod) / state.common.alphas_cumprod) ** 0.5
            UpperCAmelCase__			:     Union[str, Any]										= (1 - frac) * sigmas[low_idx] + frac * sigmas[high_idx]
            UpperCAmelCase__			:     str										= jnp.concatenate([sigmas, jnp.array([0.0]  ,	dtype=self.dtype		)]		)
  
            UpperCAmelCase__			:     str										= timesteps.astype(jnp.intaa		)
  
            # initial running values
            UpperCAmelCase__			:     Tuple										= jnp.zeros((0,) + shape  ,	dtype=self.dtype		)
  
            return state.replace(
                timesteps=_lowerCAmelCase  ,	sigmas=_lowerCAmelCase  ,	num_inference_steps=_lowerCAmelCase  ,	derivatives=_lowerCAmelCase  ,	)
  
  
  
  
       def      __UpperCAmelCase     (							self  ,	_lowerCAmelCase  ,	_lowerCAmelCase  ,	_lowerCAmelCase  ,	_lowerCAmelCase  ,	_lowerCAmelCase = 4  ,	_lowerCAmelCase = True  ,	):
            if state.num_inference_steps is None:
                 raise ValueError(
                     """Number of inference steps is 'None', you need to run 'set_timesteps' after creating the scheduler"""		)
  
            UpperCAmelCase__			:     Union[str, Any]										= state.sigmas[timestep]
  
            # 1. compute predicted original sample (x_0) from sigma-scaled predicted noise
            if self.config.prediction_type == "epsilon":
                 UpperCAmelCase__			:     Optional[Any]										= sample - sigma * model_output
            elif self.config.prediction_type == "v_prediction":
                 # * c_out + input * c_skip
                 UpperCAmelCase__			:     Dict										= model_output * (-sigma / (sigma**2 + 1) ** 0.5) + (sample / (sigma**2 + 1))
            else:
                 raise ValueError(
                     f"prediction_type given as {self.config.prediction_type} must be one of `epsilon`, or `v_prediction`"		)
  
            # 2. Convert to an ODE derivative
            UpperCAmelCase__			:     Dict										= (sample - pred_original_sample) / sigma
            UpperCAmelCase__			:     Union[str, Any]										= state.replace(derivatives=jnp.append(state.derivatives  ,	_lowerCAmelCase		)		)
            if len(state.derivatives		) > order:
                 UpperCAmelCase__			:     Optional[int]										= state.replace(derivatives=jnp.delete(state.derivatives  ,	0		)		)
  
            # 3. Compute linear multistep coefficients
            UpperCAmelCase__			:     Union[str, Any]										= min(timestep + 1  ,	_lowerCAmelCase		)
            UpperCAmelCase__			:     List[Any]										= [self.get_lms_coefficient(_lowerCAmelCase  ,	_lowerCAmelCase  ,	_lowerCAmelCase  ,	_lowerCAmelCase		) for curr_order in range(_lowerCAmelCase		)]
  
            # 4. Compute previous sample based on the derivatives path
            UpperCAmelCase__			:     Dict										= sample + sum(
                coeff * derivative for coeff, derivative in zip(_lowerCAmelCase  ,	reversed(state.derivatives		)		)		)
  
            if not return_dict:
                 return (prev_sample, state)
  
            return FlaxLMSSchedulerOutput(prev_sample=_lowerCAmelCase  ,	state=_lowerCAmelCase		)
  
  
  
  
       def      __UpperCAmelCase     (							self  ,	_lowerCAmelCase  ,	_lowerCAmelCase  ,	_lowerCAmelCase  ,	_lowerCAmelCase  ,	):
            UpperCAmelCase__			:     int										= state.sigmas[timesteps].flatten()
            UpperCAmelCase__			:     Dict										= broadcast_to_shape_from_left(_lowerCAmelCase  ,	noise.shape		)
  
            UpperCAmelCase__			:     Any										= original_samples + noise * sigma
  
            return noisy_samples
  
  
  
  
       def __len__(							self		):
            return self.config.num_train_timesteps
  
  
  
  
  
  
  
style_context: 
  
  
  
  
  
  
  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 							UpperCAmelCase_     (		__lowerCamelCase       ,			unittest.TestCase		):
       __lowerCamelCase										=     PreTrainedTokenizerFast
       __lowerCamelCase										=     False
       __lowerCamelCase										=     True
       __lowerCamelCase										=     'tokenizer_file'
  
  
  
  
       def      __UpperCAmelCase     (							self		):
            UpperCAmelCase__			:     Union[str, Any]										= False  # because we don't have pretrained_vocab_files_map
            super().setUp()
            UpperCAmelCase__			:     Dict										= True
  
            UpperCAmelCase__			:     int										= ["""robot-test/dummy-tokenizer-fast""", """robot-test/dummy-tokenizer-wordlevel"""]
            UpperCAmelCase__			:     List[Any]										= """SaulLu/dummy-tokenizer-bytelevel-bpe"""
  
            # Inclusion of 2 tokenizers to test different types of models (Unigram and WordLevel for the moment)
            UpperCAmelCase__			:     int										= [(PreTrainedTokenizerFast, model_path, {}) for model_path in model_paths]
  
            UpperCAmelCase__			:     Union[str, Any]										= PreTrainedTokenizerFast.from_pretrained(model_paths[0]		)
            tokenizer.save_pretrained(self.tmpdirname		)
  
  
  
  
       def      __UpperCAmelCase     (							self		):
            # We disable this test for PreTrainedTokenizerFast because it is the only tokenizer that is not linked to any
            # model
            pass
  
  
  
  
       def      __UpperCAmelCase     (							self		):
            # We disable this test for PreTrainedTokenizerFast because it is the only tokenizer that is not linked to any
            # model
            pass
  
  
  
  
       def      __UpperCAmelCase     (							self		):
            # We disable this test for PreTrainedTokenizerFast because it is the only tokenizer that is not linked to any
            # model
            pass
  
  
  
  
       def      __UpperCAmelCase     (							self		):
            # PreTrainedTokenizerFast doesn't have tokenizer_file in its signature
            pass
  
  
  
  
       def      __UpperCAmelCase     (							self		):
            UpperCAmelCase__			:     Optional[int]										= 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:
                           UpperCAmelCase__			:     str										= tempfile.mkdtemp()
                           UpperCAmelCase__			:     Any										= 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		)
                           UpperCAmelCase__			:     str										= tmpdirname_orig
  
  
  
  
       def      __UpperCAmelCase     (							self		):
            UpperCAmelCase__			:     Tuple										= 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:
                           UpperCAmelCase__			:     Optional[Any]										= tempfile.mkdtemp()
                           UpperCAmelCase__			:     Any										= 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		)
                           UpperCAmelCase__			:     str										= tmpdirname_orig
  
  
  
  
       def      __UpperCAmelCase     (							self		):
            UpperCAmelCase__			:     Tuple										= self.rust_tokenizer_class.from_pretrained(self.bytelevel_bpe_model_name		)
  
            UpperCAmelCase__			:     Dict										= ("""a""" for _ in range(1000		))
            UpperCAmelCase__			:     int										= tokenizer.train_new_from_iterator(text_iterator=_lowerCAmelCase  ,	length=1000  ,	vocab_size=50		)
  
            UpperCAmelCase__			:     Optional[Any]										= new_tokenizer.encode("""a🤗"""		)
            self.assertEqual(_lowerCAmelCase  ,	[64, 172, 253, 97, 245]		)
  
  
  
  
  
  
  
  @require_tokenizers
  class 							UpperCAmelCase_     (		unittest.TestCase		):
  
  
  
  
       def      __UpperCAmelCase     (							self		):
            UpperCAmelCase__			:     List[str]										= AutoTokenizer.from_pretrained("""bert-base-cased"""		)
            UpperCAmelCase__			:     List[Any]										= json.loads(tokenizer._tokenizer.to_str()		)
            UpperCAmelCase__			:     Optional[Any]										= len(_lowerCAmelCase		)
  
            with tempfile.TemporaryDirectory() as tmp_dir:
                 # Hack to save this in the tokenizer_config.json
                 UpperCAmelCase__			:     Tuple										= ["""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
                 UpperCAmelCase__			:     Optional[int]										= AutoTokenizer.from_pretrained(_lowerCAmelCase		)
                 self.assertEqual(len(_lowerCAmelCase		)  ,	len(_lowerCAmelCase		) + 1		)
                 UpperCAmelCase__			:     Any										= 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"""		)		)
                 UpperCAmelCase__			:     Union[str, Any]										= ["""tokenizer.42.0.0.json"""]
                 tokenizer.save_pretrained(_lowerCAmelCase		)
                 UpperCAmelCase__			:     Optional[Any]										= AutoTokenizer.from_pretrained(_lowerCAmelCase		)
                 self.assertEqual(len(_lowerCAmelCase		)  ,	len(_lowerCAmelCase		)		)
                 UpperCAmelCase__			:     List[Any]										= json.loads(new_tokenizer._tokenizer.to_str()		)
                 self.assertNotIn("""huggingface"""  ,	json_tokenizer["""model"""]["""vocab"""]		)
  
  
  
  
       def      __UpperCAmelCase     (							self		):
            # This repo has two tokenizer files, one for v4.0.0 and above with an added token, one for versions lower.
            UpperCAmelCase__			:     List[str]										= """hf-internal-testing/test-two-tokenizers"""
  
            # This should pick the new tokenizer file as the version of Transformers is > 4.0.0
            UpperCAmelCase__			:     int										= AutoTokenizer.from_pretrained(_lowerCAmelCase		)
            self.assertEqual(len(_lowerCAmelCase		)  ,	28997		)
            UpperCAmelCase__			:     List[Any]										= 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
  
            UpperCAmelCase__			:     Any										= """3.0.0"""
            UpperCAmelCase__			:     Optional[int]										= old_transformers.models.auto.AutoTokenizer.from_pretrained(_lowerCAmelCase		)
            self.assertEqual(len(_lowerCAmelCase		)  ,	28996		)
            UpperCAmelCase__			:     Optional[int]										= json.loads(old_tokenizer._tokenizer.to_str()		)
            self.assertNotIn("""huggingface"""  ,	json_tokenizer["""model"""]["""vocab"""]		)
  
  
  
  
  
  
  
  @require_tokenizers
  class 							UpperCAmelCase_     (		unittest.TestCase		):
  
  
  
  
       def      __UpperCAmelCase     (							self		):
            # See https://github.com/huggingface/transformers/pull/12550
            # and https://github.com/huggingface/tokenizers/issues/537
            UpperCAmelCase__			:     int										= PreTrainedTokenizerFast.from_pretrained("""robot-test/dummy-tokenizer-wordlevel"""		)
            UpperCAmelCase__			:     List[str]										= """The Matrix is a 1999 science fiction action film."""
  
            with concurrent.futures.ThreadPoolExecutor() as executor:
                 UpperCAmelCase__			:     List[str]										= [executor.submit(self.fetch  ,	_lowerCAmelCase  ,	_lowerCAmelCase		) for i in range(10		)]
                 UpperCAmelCase__			:     Dict										= [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      __UpperCAmelCase     (							self  ,	_lowerCAmelCase  ,	_lowerCAmelCase		):
            return tokenizer.encode(_lowerCAmelCase  ,	truncation="""longest_first"""  ,	padding="""longest"""		)
  
  
  
  
  
  
  