from typing import Optional, Tuple

import flax.linen as nn
import jax
import jax.numpy as jnp
from configuration_hybrid_clip import HybridCLIPConfig
from flax.core.frozen_dict import FrozenDict

from transformers import FLAX_MODEL_MAPPING, FlaxCLIPVisionModel
from transformers.modeling_flax_utils import FlaxPreTrainedModel
from transformers.models.clip.modeling_flax_clip import FlaxCLIPOutput
from transformers.utils import logging


lowerCAmelCase__      		=					logging.get_logger(__name__)
class 							a_					(		nn.Module		):






 '''simple docstring'''






 UpperCAmelCase_      					=   42
 UpperCAmelCase_      					=   jnp.floataa




 def 						__snake_case		(     self   :    Optional[int]):







        '''simple docstring'''



        lowerCAmelCase__	      =   self.config.text_config
        lowerCAmelCase__	      =   self.config.vision_config

        lowerCAmelCase__	      =   self.config.projection_dim
        lowerCAmelCase__	      =   text_config.hidden_size
        lowerCAmelCase__	      =   vision_config.hidden_size

        lowerCAmelCase__	      =   FLAX_MODEL_MAPPING[self.config.text_config.__class__].module_class
        lowerCAmelCase__	      =   FLAX_MODEL_MAPPING.get(self.config.vision_config.__class__     ,       snake_case__).module_class

        lowerCAmelCase__	      =   text_module(snake_case__     ,       dtype=self.dtype)
        lowerCAmelCase__	      =   vision_module(snake_case__     ,       dtype=self.dtype)

        lowerCAmelCase__	      =   nn.Dense(
            self.projection_dim     ,       dtype=self.dtype     ,       kernel_init=jax.nn.initializers.normal(0.02)     ,       use_bias=snake_case__     ,       )
        lowerCAmelCase__	      =   nn.Dense(
            self.projection_dim     ,       dtype=self.dtype     ,       kernel_init=jax.nn.initializers.normal(0.02)     ,       use_bias=snake_case__     ,       )
        lowerCAmelCase__	      =   self.param('logit_scale'     ,       jax.nn.initializers.ones     ,       [])




 def __call__(     self   :    Tuple     ,       lowercase__   :    Optional[int]=None     ,       lowercase__   :    int=None     ,       lowercase__   :    Tuple=None     ,       lowercase__   :    int=None     ,       lowercase__   :    Union[str, Any]=None     ,       lowercase__   :    List[Any] = True     ,       lowercase__   :    Optional[Any]=None     ,       lowercase__   :    Tuple=None     ,       lowercase__   :    Dict=None     ,       ):







        '''simple docstring'''



        lowerCAmelCase__	      =   return_dict if return_dict is not None else self.config.return_dict

        lowerCAmelCase__	      =   self.vision_model(
            pixel_values=snake_case__     ,       deterministic=snake_case__     ,       output_attentions=snake_case__     ,       output_hidden_states=snake_case__     ,       return_dict=snake_case__     ,       )

        lowerCAmelCase__	      =   self.text_model(
            input_ids=snake_case__     ,       attention_mask=snake_case__     ,       token_type_ids=snake_case__     ,       position_ids=snake_case__     ,       deterministic=snake_case__     ,       output_attentions=snake_case__     ,       output_hidden_states=snake_case__     ,       return_dict=snake_case__     ,       )

        lowerCAmelCase__	      =   vision_outputs[1]
        lowerCAmelCase__	      =   self.visual_projection(snake_case__)

        lowerCAmelCase__	      =   text_outputs[1]
        lowerCAmelCase__	      =   self.text_projection(snake_case__)

        # normalized features
        lowerCAmelCase__	      =   image_embeds / jnp.linalg.norm(snake_case__     ,       axis=-1     ,       keepdims=snake_case__)
        lowerCAmelCase__	      =   text_embeds / jnp.linalg.norm(snake_case__     ,       axis=-1     ,       keepdims=snake_case__)

        # cosine similarity as logits
        lowerCAmelCase__	      =   jnp.exp(self.logit_scale)
        lowerCAmelCase__	      =   jnp.matmul(snake_case__     ,       image_embeds.T) * logit_scale
        lowerCAmelCase__	      =   logits_per_text.T

        if not return_dict:
               return (logits_per_image, logits_per_text, text_embeds, image_embeds, text_outputs, vision_outputs)

        return FlaxCLIPOutput(
            logits_per_image=snake_case__     ,       logits_per_text=snake_case__     ,       text_embeds=snake_case__     ,       image_embeds=snake_case__     ,       text_model_output=snake_case__     ,       vision_model_output=snake_case__     ,       )






class 							a_					(		SCREAMING_SNAKE_CASE		):






 '''simple docstring'''






 UpperCAmelCase_      					=   HybridCLIPConfig
 UpperCAmelCase_      					=   FlaxHybridCLIPModule




 def __init__(     self   :    Dict     ,       lowercase__   :    Dict     ,       lowercase__   :    int = None     ,       lowercase__   :    List[str] = 0     ,       lowercase__   :    List[str] = jnp.floataa     ,       **lowercase__   :    Optional[Any]     ,       ):







        '''simple docstring'''



        if input_shape is None:
               lowerCAmelCase__	      =   ((1, 1), (1, config.vision_config.image_size, config.vision_config.image_size, 3))

        lowerCAmelCase__	      =   self.module_class(config=snake_case__     ,       dtype=snake_case__     ,       **snake_case__)
        super().__init__(snake_case__     ,       snake_case__     ,       input_shape=snake_case__     ,       seed=snake_case__     ,       dtype=snake_case__)




 def 						__snake_case		(     self   :    Union[str, Any]     ,       lowercase__   :    Any     ,       lowercase__   :    int     ,       lowercase__   :    Dict = None):







        '''simple docstring'''



        lowerCAmelCase__	      =   jnp.zeros(input_shape[0]     ,       dtype='i4')
        lowerCAmelCase__	      =   jnp.broadcast_to(jnp.arange(jnp.atleast_ad(snake_case__).shape[-1])     ,       input_shape[0])
        lowerCAmelCase__	      =   jnp.ones_like(snake_case__)
        lowerCAmelCase__	      =   jnp.ones_like(snake_case__)

        lowerCAmelCase__	      =   jax.random.normal(snake_case__     ,       input_shape[1])

        lowerCAmelCase__	      =   jax.random.split(snake_case__)
        lowerCAmelCase__	      =   {'params': params_rng, 'dropout': dropout_rng}

        return self.module.init(snake_case__     ,       snake_case__     ,       snake_case__     ,       snake_case__     ,       snake_case__     ,       snake_case__)["params"]




 def __call__(     self   :    List[Any]     ,       lowercase__   :    Optional[int]     ,       lowercase__   :    Any     ,       lowercase__   :    List[str]=None     ,       lowercase__   :    Tuple=None     ,       lowercase__   :    Optional[int]=None     ,       lowercase__   :    Optional[Any] = None     ,       lowercase__   :    Union[str, Any] = None     ,       lowercase__   :    List[str] = False     ,       lowercase__   :    Optional[int] = None     ,       lowercase__   :    List[Any] = None     ,       lowercase__   :    List[str] = None     ,       ):







        '''simple docstring'''



        lowerCAmelCase__	      =   output_attentions if output_attentions is not None else self.config.output_attentions
        lowerCAmelCase__	      =   (
            output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
        )
        lowerCAmelCase__	      =   return_dict if return_dict is not None else self.config.return_dict

        if position_ids is None:
               lowerCAmelCase__	      =   jnp.broadcast_to(jnp.arange(jnp.atleast_ad(snake_case__).shape[-1])     ,       input_ids.shape)

        if token_type_ids is None:
               lowerCAmelCase__	      =   jnp.zeros_like(snake_case__)

        if attention_mask is None:
               lowerCAmelCase__	      =   jnp.ones_like(snake_case__)

        # Handle any PRNG if needed
        lowerCAmelCase__	      =   {}
        if dropout_rng is not None:
               lowerCAmelCase__	      =   dropout_rng

        return self.module.apply(
            {'params': params or self.params}     ,       jnp.array(snake_case__     ,       dtype='i4')     ,       jnp.array(snake_case__     ,       dtype=jnp.floataa)     ,       jnp.array(snake_case__     ,       dtype='i4')     ,       jnp.array(snake_case__     ,       dtype='i4')     ,       jnp.array(snake_case__     ,       dtype='i4')     ,       not train     ,       snake_case__     ,       snake_case__     ,       snake_case__     ,       rngs=snake_case__     ,       )




 def 						__snake_case		(     self   :    int     ,       lowercase__   :    int     ,       lowercase__   :    Union[str, Any]=None     ,       lowercase__   :    Union[str, Any]=None     ,       lowercase__   :    Optional[Any]=None     ,       lowercase__   :    Optional[int] = None     ,       lowercase__   :    Optional[Any] = None     ,       lowercase__   :    str=False     ,       ):







        '''simple docstring'''



        if position_ids is None:
               lowerCAmelCase__	      =   jnp.broadcast_to(jnp.arange(jnp.atleast_ad(snake_case__).shape[-1])     ,       input_ids.shape)

        if token_type_ids is None:
               lowerCAmelCase__	      =   jnp.zeros_like(snake_case__)

        if attention_mask is None:
               lowerCAmelCase__	      =   jnp.ones_like(snake_case__)

        # Handle any PRNG if needed
        lowerCAmelCase__	      =   {}
        if dropout_rng is not None:
               lowerCAmelCase__	      =   dropout_rng

        def _get_features(lowercase__   :    List[str]     ,       lowercase__   :    List[str]     ,       lowercase__   :    str     ,       lowercase__   :    List[Any]     ,       lowercase__   :    Tuple     ,       lowercase__   :    Dict):
               lowerCAmelCase__	      =   module.text_model(
                   input_ids=snake_case__     ,       attention_mask=snake_case__     ,       position_ids=snake_case__     ,       token_type_ids=snake_case__     ,       deterministic=snake_case__     ,       )
               lowerCAmelCase__	      =   text_outputs[1]
               lowerCAmelCase__	      =   module.text_projection(snake_case__)
               return text_features

        return self.module.apply(
            {'params': params or self.params}     ,       jnp.array(snake_case__     ,       dtype='i4')     ,       jnp.array(snake_case__     ,       dtype='i4')     ,       jnp.array(snake_case__     ,       dtype='i4')     ,       jnp.array(snake_case__     ,       dtype='i4')     ,       not train     ,       method=_get_features     ,       rngs=snake_case__     ,       )




 def 						__snake_case		(     self   :    Optional[Any]     ,       lowercase__   :    Optional[int]     ,       lowercase__   :    Dict = None     ,       lowercase__   :    Tuple = None     ,       lowercase__   :    str=False):







        '''simple docstring'''



        lowerCAmelCase__	      =   {}
        if dropout_rng is not None:
               lowerCAmelCase__	      =   dropout_rng

        def _get_features(lowercase__   :    Optional[Any]     ,       lowercase__   :    Optional[int]     ,       lowercase__   :    Any):
               lowerCAmelCase__	      =   module.vision_model(pixel_values=snake_case__     ,       deterministic=snake_case__)
               lowerCAmelCase__	      =   vision_outputs[1]  # pooled_output
               lowerCAmelCase__	      =   module.visual_projection(snake_case__)
               return image_features

        return self.module.apply(
            {'params': params or self.params}     ,       jnp.array(snake_case__     ,       dtype=jnp.floataa)     ,       not train     ,       method=_get_features     ,       rngs=snake_case__     ,       )




 @classmethod
 def 						__snake_case		(     cls   :    Any     ,       lowercase__   :    List[Any] = None     ,       lowercase__   :    List[str] = None     ,       *lowercase__   :    Optional[int]     ,       **lowercase__   :    Union[str, Any]     ,       ):







        '''simple docstring'''



        lowerCAmelCase__	      =   {
            argument[len('text_') :]: value for argument, value in kwargs.items() if argument.startswith('text_')
        }

        lowerCAmelCase__	      =   {
            argument[len('vision_') :]: value for argument, value in kwargs.items() if argument.startswith('vision_')
        }

        # remove text, vision kwargs from kwargs
        for key in kwargs_text.keys():
               del kwargs["text_" + key]
        for key in kwargs_vision.keys():
               del kwargs["vision_" + key]

        # Load and initialize the text and vision model
        lowerCAmelCase__	      =   kwargs_text.pop('model'     ,       snake_case__)
        if text_model is None:
               assert (
                   text_model_name_or_path is not None
               ), "If `model` is not defined as an argument, a `text_model_name_or_path` has to be defined"
               from transformers import FlaxAutoModel

               if "config" not in kwargs_text:
                      from transformers import AutoConfig

                      lowerCAmelCase__	      =   AutoConfig.from_pretrained(snake_case__)
                      lowerCAmelCase__	      =   text_config

               lowerCAmelCase__	      =   FlaxAutoModel.from_pretrained(snake_case__     ,       *snake_case__     ,       **snake_case__)

        lowerCAmelCase__	      =   kwargs_vision.pop('model'     ,       snake_case__)
        if vision_model is None:
               assert (
                   vision_model_name_or_path is not None
               ), "If `model` is not defined as an argument, a `vision_model_name_or_path` has to be defined"
               from transformers import FlaxAutoModel

               if "config" not in kwargs_vision:
                      from transformers import AutoConfig

                      lowerCAmelCase__	      =   AutoConfig.from_pretrained(snake_case__)
                      lowerCAmelCase__	      =   vision_config

               lowerCAmelCase__	      =   FlaxAutoModel.from_pretrained(snake_case__     ,       *snake_case__     ,       **snake_case__)

        # instantiate config with corresponding kwargs
        lowerCAmelCase__	      =   kwargs.pop('dtype'     ,       jnp.floataa)
        lowerCAmelCase__	      =   HybridCLIPConfig.from_text_vision_configs(text_model.config     ,       vision_model.config     ,       **snake_case__)

        # init model
        lowerCAmelCase__	      =   cls(snake_case__     ,       *snake_case__     ,       dtype=snake_case__     ,       **snake_case__)

        if vision_config.model_type == "clip":
               lowerCAmelCase__	      =   vision_model.params['vision_model']
               lowerCAmelCase__	      =   vision_model.params['visual_projection']['kernel']
        else:
               lowerCAmelCase__	      =   vision_model.params

        lowerCAmelCase__	      =   text_model.params

        return model




'''simple docstring'''




import math
from collections.abc import Generator

def       __UpperCamelCase     (     _A     :					int					)     ->   Generator[int, None, None]:




	"""simple docstring"""





	lowerCAmelCase       :     Generator   				=			(i for i in range(1       , (max_n + 1)					))
	for i in (n for n in numbers if n > 1):
		for j in range(2       , _A					):
			if (i % j) == 0:
				break
		else:
			yield i

def       __UpperCamelCase     (     _A     :					int					)     ->   Generator[int, None, None]:




	"""simple docstring"""





	lowerCAmelCase       :     Generator   				=			(i for i in range(1       , (max_n + 1)					))
	for i in (n for n in numbers if n > 1):
		# only need to check for factors up to sqrt(i)
		lowerCAmelCase       :     List[str]   				=			int(math.sqrt(_A					)					) + 1
		for j in range(2       , _A					):
			if (i % j) == 0:
				break
		else:
			yield i

def       __UpperCamelCase     (     _A     :					int					)     ->   Generator[int, None, None]:




	"""simple docstring"""





	lowerCAmelCase       :     Generator   				=			(i for i in range(1       , (max_n + 1)       , 2					))
	# It's useless to test even numbers as they will not be prime
	if max_n > 2:
		yield 2  # Because 2 will not be tested, it's necessary to yield it now
	for i in (n for n in numbers if n > 1):
		lowerCAmelCase       :     int   				=			int(math.sqrt(_A					)					) + 1
		for j in range(3       , _A       , 2					):
			# As we removed the even numbers, we don't need them now
			if (i % j) == 0:
				break
		else:
			yield i

def       __UpperCamelCase     (     )     ->   Dict:




	"""simple docstring"""





	from timeit import timeit

	lowerCAmelCase       :     Tuple   				=			'from __main__ import slow_primes, primes, fast_primes'
	print(timeit('slow_primes(1_000_000_000_000)'       , setup=_A       , number=1_00_00_00					)					)
	print(timeit('primes(1_000_000_000_000)'       , setup=_A       , number=1_00_00_00					)					)
	print(timeit('fast_primes(1_000_000_000_000)'       , setup=_A       , number=1_00_00_00					)					)


if __name__ == "__main__":
				_lowerCAmelCase	:		Optional[Any]		  =						int(input('Calculate primes up to:\n>> ').strip())
				for ret in primes(number):
								print(ret)
				benchmark()
