






import flax.linen as nn
import jax.numpy as jnp

from .attention_flax import FlaxTransformeraDModel
from .resnet_flax import FlaxDownsampleaD, FlaxResnetBlockaD, FlaxUpsampleaD


class 							__UpperCamelCase     (  nn.Module ):
	__snake_case						:int
	__snake_case						:int
	__snake_case						:float =							0.0
	__snake_case						:int =							1
	__snake_case						:int =							1
	__snake_case						:bool =							True
	__snake_case						:bool =							False
	__snake_case						:bool =							False
	__snake_case						:bool =							False
	__snake_case						:jnp.dtype =							jnp.floataa







	def 			_a (	self   :				Union[str, Any]      )		->			Dict:
		"""simple docstring"""







		__lowercase   							=	[]
		__lowercase   							=	[]

		for i in range(self.num_layers      ):
			__lowercase   							=	self.in_channels if i == 0 else self.out_channels

			__lowercase   							=	FlaxResnetBlockaD(
			    in_channels=_lowerCAmelCase							,					out_channels=self.out_channels							,					dropout_prob=self.dropout							,					dtype=self.dtype							,					)
			resnets.append(_lowerCAmelCase      )

			__lowercase   							=	FlaxTransformeraDModel(
			    in_channels=self.out_channels							,					n_heads=self.num_attention_heads							,					d_head=self.out_channels // self.num_attention_heads							,					depth=1							,					use_linear_projection=self.use_linear_projection							,					only_cross_attention=self.only_cross_attention							,					use_memory_efficient_attention=self.use_memory_efficient_attention							,					dtype=self.dtype							,					)
			attentions.append(_lowerCAmelCase      )

		__lowercase   							=	resnets
		__lowercase   							=	attentions

		if self.add_downsample:
			__lowercase   							=	FlaxDownsampleaD(self.out_channels							,					dtype=self.dtype      )







	def __call__(	self   :				Union[str, Any]							,					_lowerCAmelCase   :				Optional[int]							,					_lowerCAmelCase   :				int							,					_lowerCAmelCase   :				List[str]							,					_lowerCAmelCase   :				int=True      )		->			Dict:
		"""simple docstring"""







		__lowercase   							=	()

		for resnet, attn in zip(self.resnets							,					self.attentions      ):
			__lowercase   							=	resnet(_lowerCAmelCase							,					_lowerCAmelCase							,					deterministic=_lowerCAmelCase      )
			__lowercase   							=	attn(_lowerCAmelCase							,					_lowerCAmelCase							,					deterministic=_lowerCAmelCase      )
			output_states += (hidden_states,)

		if self.add_downsample:
			__lowercase   							=	self.downsamplers_a(_lowerCAmelCase      )
			output_states += (hidden_states,)

		return hidden_states, output_states


class 							__UpperCamelCase     (  nn.Module ):
	__snake_case						:int
	__snake_case						:int
	__snake_case						:float =							0.0
	__snake_case						:int =							1
	__snake_case						:bool =							True
	__snake_case						:jnp.dtype =							jnp.floataa







	def 			_a (	self   :				Optional[Any]      )		->			Optional[Any]:
		"""simple docstring"""







		__lowercase   							=	[]

		for i in range(self.num_layers      ):
			__lowercase   							=	self.in_channels if i == 0 else self.out_channels

			__lowercase   							=	FlaxResnetBlockaD(
			    in_channels=_lowerCAmelCase							,					out_channels=self.out_channels							,					dropout_prob=self.dropout							,					dtype=self.dtype							,					)
			resnets.append(_lowerCAmelCase      )
		__lowercase   							=	resnets

		if self.add_downsample:
			__lowercase   							=	FlaxDownsampleaD(self.out_channels							,					dtype=self.dtype      )







	def __call__(	self   :				Union[str, Any]							,					_lowerCAmelCase   :				List[str]							,					_lowerCAmelCase   :				Tuple							,					_lowerCAmelCase   :				Optional[int]=True      )		->			Union[str, Any]:
		"""simple docstring"""







		__lowercase   							=	()

		for resnet in self.resnets:
			__lowercase   							=	resnet(_lowerCAmelCase							,					_lowerCAmelCase							,					deterministic=_lowerCAmelCase      )
			output_states += (hidden_states,)

		if self.add_downsample:
			__lowercase   							=	self.downsamplers_a(_lowerCAmelCase      )
			output_states += (hidden_states,)

		return hidden_states, output_states


class 							__UpperCamelCase     (  nn.Module ):
	__snake_case						:int
	__snake_case						:int
	__snake_case						:int
	__snake_case						:float =							0.0
	__snake_case						:int =							1
	__snake_case						:int =							1
	__snake_case						:bool =							True
	__snake_case						:bool =							False
	__snake_case						:bool =							False
	__snake_case						:bool =							False
	__snake_case						:jnp.dtype =							jnp.floataa







	def 			_a (	self   :				List[Any]      )		->			Optional[Any]:
		"""simple docstring"""







		__lowercase   							=	[]
		__lowercase   							=	[]

		for i in range(self.num_layers      ):
			__lowercase   							=	self.in_channels if (i == self.num_layers - 1) else self.out_channels
			__lowercase   							=	self.prev_output_channel if i == 0 else self.out_channels

			__lowercase   							=	FlaxResnetBlockaD(
			    in_channels=resnet_in_channels + res_skip_channels							,					out_channels=self.out_channels							,					dropout_prob=self.dropout							,					dtype=self.dtype							,					)
			resnets.append(_lowerCAmelCase      )

			__lowercase   							=	FlaxTransformeraDModel(
			    in_channels=self.out_channels							,					n_heads=self.num_attention_heads							,					d_head=self.out_channels // self.num_attention_heads							,					depth=1							,					use_linear_projection=self.use_linear_projection							,					only_cross_attention=self.only_cross_attention							,					use_memory_efficient_attention=self.use_memory_efficient_attention							,					dtype=self.dtype							,					)
			attentions.append(_lowerCAmelCase      )

		__lowercase   							=	resnets
		__lowercase   							=	attentions

		if self.add_upsample:
			__lowercase   							=	FlaxUpsampleaD(self.out_channels							,					dtype=self.dtype      )







	def __call__(	self   :				List[str]							,					_lowerCAmelCase   :				int							,					_lowerCAmelCase   :				List[Any]							,					_lowerCAmelCase   :				Tuple							,					_lowerCAmelCase   :				List[str]							,					_lowerCAmelCase   :				Dict=True      )		->			str:
		"""simple docstring"""







		for resnet, attn in zip(self.resnets							,					self.attentions      ):
			# pop res hidden states
			__lowercase   							=	res_hidden_states_tuple[-1]
			__lowercase   							=	res_hidden_states_tuple[:-1]
			__lowercase   							=	jnp.concatenate((hidden_states, res_hidden_states)							,					axis=-1      )

			__lowercase   							=	resnet(_lowerCAmelCase							,					_lowerCAmelCase							,					deterministic=_lowerCAmelCase      )
			__lowercase   							=	attn(_lowerCAmelCase							,					_lowerCAmelCase							,					deterministic=_lowerCAmelCase      )

		if self.add_upsample:
			__lowercase   							=	self.upsamplers_a(_lowerCAmelCase      )

		return hidden_states


class 							__UpperCamelCase     (  nn.Module ):
	__snake_case						:int
	__snake_case						:int
	__snake_case						:int
	__snake_case						:float =							0.0
	__snake_case						:int =							1
	__snake_case						:bool =							True
	__snake_case						:jnp.dtype =							jnp.floataa







	def 			_a (	self   :				int      )		->			Any:
		"""simple docstring"""







		__lowercase   							=	[]

		for i in range(self.num_layers      ):
			__lowercase   							=	self.in_channels if (i == self.num_layers - 1) else self.out_channels
			__lowercase   							=	self.prev_output_channel if i == 0 else self.out_channels

			__lowercase   							=	FlaxResnetBlockaD(
			    in_channels=resnet_in_channels + res_skip_channels							,					out_channels=self.out_channels							,					dropout_prob=self.dropout							,					dtype=self.dtype							,					)
			resnets.append(_lowerCAmelCase      )

		__lowercase   							=	resnets

		if self.add_upsample:
			__lowercase   							=	FlaxUpsampleaD(self.out_channels							,					dtype=self.dtype      )







	def __call__(	self   :				Optional[int]							,					_lowerCAmelCase   :				Optional[Any]							,					_lowerCAmelCase   :				Optional[int]							,					_lowerCAmelCase   :				Tuple							,					_lowerCAmelCase   :				List[str]=True      )		->			Tuple:
		"""simple docstring"""







		for resnet in self.resnets:
			# pop res hidden states
			__lowercase   							=	res_hidden_states_tuple[-1]
			__lowercase   							=	res_hidden_states_tuple[:-1]
			__lowercase   							=	jnp.concatenate((hidden_states, res_hidden_states)							,					axis=-1      )

			__lowercase   							=	resnet(_lowerCAmelCase							,					_lowerCAmelCase							,					deterministic=_lowerCAmelCase      )

		if self.add_upsample:
			__lowercase   							=	self.upsamplers_a(_lowerCAmelCase      )

		return hidden_states


class 							__UpperCamelCase     (  nn.Module ):
	__snake_case						:int
	__snake_case						:float =							0.0
	__snake_case						:int =							1
	__snake_case						:int =							1
	__snake_case						:bool =							False
	__snake_case						:bool =							False
	__snake_case						:jnp.dtype =							jnp.floataa







	def 			_a (	self   :				str      )		->			List[Any]:
		"""simple docstring"""







		__lowercase   							=	[
		    FlaxResnetBlockaD(
		        in_channels=self.in_channels							,					out_channels=self.in_channels							,					dropout_prob=self.dropout							,					dtype=self.dtype							,					)
		]

		__lowercase   							=	[]

		for _ in range(self.num_layers      ):
			__lowercase   							=	FlaxTransformeraDModel(
			    in_channels=self.in_channels							,					n_heads=self.num_attention_heads							,					d_head=self.in_channels // self.num_attention_heads							,					depth=1							,					use_linear_projection=self.use_linear_projection							,					use_memory_efficient_attention=self.use_memory_efficient_attention							,					dtype=self.dtype							,					)
			attentions.append(_lowerCAmelCase      )

			__lowercase   							=	FlaxResnetBlockaD(
			    in_channels=self.in_channels							,					out_channels=self.in_channels							,					dropout_prob=self.dropout							,					dtype=self.dtype							,					)
			resnets.append(_lowerCAmelCase      )

		__lowercase   							=	resnets
		__lowercase   							=	attentions







	def __call__(	self   :				str							,					_lowerCAmelCase   :				str							,					_lowerCAmelCase   :				List[str]							,					_lowerCAmelCase   :				List[str]							,					_lowerCAmelCase   :				List[Any]=True      )		->			List[str]:
		"""simple docstring"""







		__lowercase   							=	self.resnets[0](_lowerCAmelCase							,					_lowerCAmelCase      )
		for attn, resnet in zip(self.attentions							,					self.resnets[1:]      ):
			__lowercase   							=	attn(_lowerCAmelCase							,					_lowerCAmelCase							,					deterministic=_lowerCAmelCase      )
			__lowercase   							=	resnet(_lowerCAmelCase							,					_lowerCAmelCase							,					deterministic=_lowerCAmelCase      )

		return hidden_states


'''simple docstring'''






import os
from pathlib import Path
from unittest.mock import patch

import pytest
import zstandard as zstd

from datasets.download.download_config import DownloadConfig
from datasets.utils.file_utils import (
    OfflineModeIsEnabled,
    cached_path,
    fsspec_get,
    fsspec_head,
    ftp_get,
    ftp_head,
    get_from_cache,
    http_get,
    http_head,
)


_UpperCAmelCase    :     Optional[int]         =       '''\
    Text data.
    Second line of data.'''

_UpperCAmelCase    :     Optional[Any]         =       '''file'''





@pytest.fixture(scope='session'				)
def  _SCREAMING_SNAKE_CASE		(      __snake_case							:			Any				):
     _A    		=  tmp_path_factory.mktemp('data'				) / (FILE_PATH + '.zstd')
     _A    		=  bytes(__snake_case   ,		'utf-8'				)
     with zstd.open(__snake_case   ,		'wb'				) as f:
          f.write(__snake_case				)
     return path





@pytest.fixture
def  _SCREAMING_SNAKE_CASE		(      __snake_case							:			Union[str, Any]				):
     with open(os.path.join(tmpfs.local_root_dir   ,		__snake_case				)   ,		'w'				) as f:
          f.write(__snake_case				)
     return FILE_PATH





@pytest.mark.parametrize('compression_format'   ,		['gzip', 'xz', 'zstd']				)
def  _SCREAMING_SNAKE_CASE		(      __snake_case							:			Dict   ,		__snake_case							:			str   ,		__snake_case							:			Any   ,		__snake_case							:			Any   ,		__snake_case							:			List[str]   ,		__snake_case							:			List[str]				):
     _A    		=  {'gzip': gz_file, 'xz': xz_file, 'zstd': zstd_path}
     _A    		=  input_paths[compression_format]
     _A    		=  tmp_path / 'cache'
     _A    		=  DownloadConfig(cache_dir=__snake_case   ,		extract_compressed_file=__snake_case				)
     _A    		=  cached_path(__snake_case   ,		download_config=__snake_case				)
     with open(__snake_case				) as f:
          _A    		=  f.read()
     with open(__snake_case				) as f:
          _A    		=  f.read()
     assert extracted_file_content == expected_file_content





@pytest.mark.parametrize('default_extracted'   ,		[True, False]				)
@pytest.mark.parametrize('default_cache_dir'   ,		[True, False]				)
def  _SCREAMING_SNAKE_CASE		(      __snake_case							:			Union[str, Any]   ,		__snake_case							:			List[str]   ,		__snake_case							:			Union[str, Any]   ,		__snake_case							:			int   ,		__snake_case							:			List[str]				):
     _A    		=  'custom_cache'
     _A    		=  'custom_extracted_dir'
     _A    		=  tmp_path / 'custom_extracted_path'
     if default_extracted:
          _A    		=  ('downloads' if default_cache_dir else custom_cache_dir, 'extracted')
     else:
          monkeypatch.setattr('datasets.config.EXTRACTED_DATASETS_DIR'   ,		__snake_case				)
          monkeypatch.setattr('datasets.config.EXTRACTED_DATASETS_PATH'   ,		str(__snake_case				)				)
          _A    		=  custom_extracted_path.parts[-2:] if default_cache_dir else (custom_cache_dir, custom_extracted_dir)

     _A    		=  xz_file
     _A    		=  (
         DownloadConfig(extract_compressed_file=__snake_case				)
         if default_cache_dir
         else DownloadConfig(cache_dir=tmp_path / custom_cache_dir   ,		extract_compressed_file=__snake_case				)
     )
     _A    		=  cached_path(__snake_case   ,		download_config=__snake_case				)
     assert Path(__snake_case				).parent.parts[-2:] == expected





def  _SCREAMING_SNAKE_CASE		(      __snake_case							:			str				):
     # absolute path
     _A    		=  str(Path(__snake_case				).resolve()				)
     assert cached_path(__snake_case				) == text_file
     # relative path
     _A    		=  str(Path(__snake_case				).resolve().relative_to(Path(os.getcwd()				)				)				)
     assert cached_path(__snake_case				) == text_file





def  _SCREAMING_SNAKE_CASE		(      __snake_case							:			List[str]				):
     # absolute path
     _A    		=  str(tmp_path.resolve() / '__missing_file__.txt'				)
     with pytest.raises(__snake_case				):
          cached_path(__snake_case				)
     # relative path
     _A    		=  './__missing_file__.txt'
     with pytest.raises(__snake_case				):
          cached_path(__snake_case				)





def  _SCREAMING_SNAKE_CASE		(      __snake_case							:			int				):
     _A    		=  get_from_cache(F'tmp://{tmpfs_file}'				)
     with open(__snake_case				) as f:
          _A    		=  f.read()
     assert output_file_content == FILE_CONTENT





@patch('datasets.config.HF_DATASETS_OFFLINE'   ,		__snake_case				)
def  _SCREAMING_SNAKE_CASE		(      ):
     with pytest.raises(__snake_case				):
          cached_path('https://huggingface.co'				)





@patch('datasets.config.HF_DATASETS_OFFLINE'   ,		__snake_case				)
def  _SCREAMING_SNAKE_CASE		(      __snake_case							:			Tuple				):
     _A    		=  tmp_path_factory.mktemp('data'				) / 'file.html'
     with pytest.raises(__snake_case				):
          http_get('https://huggingface.co'   ,		temp_file=__snake_case				)
     with pytest.raises(__snake_case				):
          http_head('https://huggingface.co'				)





@patch('datasets.config.HF_DATASETS_OFFLINE'   ,		__snake_case				)
def  _SCREAMING_SNAKE_CASE		(      __snake_case							:			Optional[int]				):
     _A    		=  tmp_path_factory.mktemp('data'				) / 'file.html'
     with pytest.raises(__snake_case				):
          ftp_get('ftp://huggingface.co'   ,		temp_file=__snake_case				)
     with pytest.raises(__snake_case				):
          ftp_head('ftp://huggingface.co'				)





@patch('datasets.config.HF_DATASETS_OFFLINE'   ,		__snake_case				)
def  _SCREAMING_SNAKE_CASE		(      __snake_case							:			List[str]				):
     _A    		=  tmp_path_factory.mktemp('data'				) / 'file.html'
     with pytest.raises(__snake_case				):
          fsspec_get('s3://huggingface.co'   ,		temp_file=__snake_case				)
     with pytest.raises(__snake_case				):
          fsspec_head('s3://huggingface.co'				)



