




"""simple docstring"""







from collections import OrderedDict
from typing import TYPE_CHECKING, Any, List, Mapping, Optional, Union

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import TensorType, logging


if TYPE_CHECKING:
		from ...onnx.config import PatchingSpec
		from ...tokenization_utils_base import PreTrainedTokenizerBase


a      =							logging.get_logger(__name__)

a      =							{
    'allenai/longformer-base-4096': 'https://huggingface.co/allenai/longformer-base-4096/resolve/main/config.json',
    'allenai/longformer-large-4096': 'https://huggingface.co/allenai/longformer-large-4096/resolve/main/config.json',
    'allenai/longformer-large-4096-finetuned-triviaqa': (
        'https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/config.json'
    ),
    'allenai/longformer-base-4096-extra.pos.embd.only': (
        'https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/config.json'
    ),
    'allenai/longformer-large-4096-extra.pos.embd.only': (
        'https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/config.json'
    ),
}
class       SCREAMING_SNAKE_CASE__					(					_a					):
						_a    						=						'longformer'


						def __init__(							self				:			Any						,						lowerCAmelCase				:			Union[List[int], int] = 512						,						lowerCAmelCase				:			int = 2						,						lowerCAmelCase				:			int = 1						,						lowerCAmelCase				:			int = 0						,						lowerCAmelCase				:			int = 2						,						lowerCAmelCase				:			int = 3_0522						,						lowerCAmelCase				:			int = 768						,						lowerCAmelCase				:			int = 12						,						lowerCAmelCase				:			int = 12						,						lowerCAmelCase				:			int = 3072						,						lowerCAmelCase				:			str = "gelu"						,						lowerCAmelCase				:			float = 0.1						,						lowerCAmelCase				:			float = 0.1						,						lowerCAmelCase				:			int = 512						,						lowerCAmelCase				:			int = 2						,						lowerCAmelCase				:			float = 0.02						,						lowerCAmelCase				:			float = 1e-12						,						lowerCAmelCase				:			bool = False						,						**lowerCAmelCase				:			Optional[int]						,						):
										super().__init__(pad_token_id=lowerCAmelCase						,						**lowerCAmelCase					)

										lowerCAmelCase  =				attention_window
										lowerCAmelCase  =				sep_token_id
										lowerCAmelCase  =				bos_token_id
										lowerCAmelCase  =				eos_token_id
										lowerCAmelCase  =				vocab_size
										lowerCAmelCase  =				hidden_size
										lowerCAmelCase  =				num_hidden_layers
										lowerCAmelCase  =				num_attention_heads
										lowerCAmelCase  =				hidden_act
										lowerCAmelCase  =				intermediate_size
										lowerCAmelCase  =				hidden_dropout_prob
										lowerCAmelCase  =				attention_probs_dropout_prob
										lowerCAmelCase  =				max_position_embeddings
										lowerCAmelCase  =				type_vocab_size
										lowerCAmelCase  =				initializer_range
										lowerCAmelCase  =				layer_norm_eps
										lowerCAmelCase  =				onnx_export





class       SCREAMING_SNAKE_CASE__					(					_a					):


						def __init__(							self				:			List[str]						,						lowerCAmelCase				:			"PretrainedConfig"						,						lowerCAmelCase				:			str = "default"						,						lowerCAmelCase				:			"List[PatchingSpec]" = None					):
										super().__init__(lowerCAmelCase						,						lowerCAmelCase						,						lowerCAmelCase					)
										lowerCAmelCase  =				True


						@property
						def   __lowercase (							self				:			List[Any]					):
										if self.task == "multiple-choice":
														lowerCAmelCase  =				{0: """batch""", 1: """choice""", 2: """sequence"""}
										else:
														lowerCAmelCase  =				{0: """batch""", 1: """sequence"""}
										return OrderedDict(
										    [
										        ("""input_ids""", dynamic_axis),
										        ("""attention_mask""", dynamic_axis),
										        ("""global_attention_mask""", dynamic_axis),
										    ]					)


						@property
						def   __lowercase (							self				:			List[Any]					):
										lowerCAmelCase  =				super().outputs
										if self.task == "default":
														lowerCAmelCase  =				{0: """batch"""}
										return outputs


						@property
						def   __lowercase (							self				:			Any					):
										return 1e-4


						@property
						def   __lowercase (							self				:			int					):
										# needs to be >= 14 to support tril operator
										return max(super().default_onnx_opset						,						14					)




						def   __lowercase (							self				:			str						,						lowerCAmelCase				:			"PreTrainedTokenizerBase"						,						lowerCAmelCase				:			int = -1						,						lowerCAmelCase				:			int = -1						,						lowerCAmelCase				:			bool = False						,						lowerCAmelCase				:			Optional[TensorType] = None						,						):
										lowerCAmelCase  =				super().generate_dummy_inputs(
										    preprocessor=lowerCAmelCase						,						batch_size=lowerCAmelCase						,						seq_length=lowerCAmelCase						,						is_pair=lowerCAmelCase						,						framework=lowerCAmelCase					)
										import torch

										# for some reason, replacing this code by inputs["global_attention_mask"] = torch.randint(2, inputs["input_ids"].shape, dtype=torch.int64)
										# makes the export fail randomly
										lowerCAmelCase  =				torch.zeros_like(inputs["""input_ids"""]					)
										# make every second token global
										lowerCAmelCase  =				1

										return inputs











"""simple docstring"""







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

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






class       SCREAMING_SNAKE_CASE__					(					nn.Module					):
						_a    						=						42
						_a    						=						42
						_a    						=						0.0
						_a    						=						1
						_a    						=						1
						_a    						=						True
						_a    						=						False
						_a    						=						False
						_a    						=						False
						_a    						=						jnp.floataa


						def   __lowercase (							self				:			List[str]					):
										lowerCAmelCase  =				[]
										lowerCAmelCase  =				[]

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

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

														lowerCAmelCase  =				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					)

										lowerCAmelCase  =				resnets
										lowerCAmelCase  =				attentions

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




						def __call__(							self				:			Tuple						,						lowerCAmelCase				:			str						,						lowerCAmelCase				:			Union[str, Any]						,						lowerCAmelCase				:			Tuple						,						lowerCAmelCase				:			int=True					):
										lowerCAmelCase  =				()

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

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

										return hidden_states, output_states






class       SCREAMING_SNAKE_CASE__					(					nn.Module					):
						_a    						=						42
						_a    						=						42
						_a    						=						0.0
						_a    						=						1
						_a    						=						True
						_a    						=						jnp.floataa


						def   __lowercase (							self				:			Union[str, Any]					):
										lowerCAmelCase  =				[]

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

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

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




						def __call__(							self				:			int						,						lowerCAmelCase				:			Any						,						lowerCAmelCase				:			Any						,						lowerCAmelCase				:			List[str]=True					):
										lowerCAmelCase  =				()

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

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

										return hidden_states, output_states






class       SCREAMING_SNAKE_CASE__					(					nn.Module					):
						_a    						=						42
						_a    						=						42
						_a    						=						42
						_a    						=						0.0
						_a    						=						1
						_a    						=						1
						_a    						=						True
						_a    						=						False
						_a    						=						False
						_a    						=						False
						_a    						=						jnp.floataa


						def   __lowercase (							self				:			List[str]					):
										lowerCAmelCase  =				[]
										lowerCAmelCase  =				[]

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

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

														lowerCAmelCase  =				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					)

										lowerCAmelCase  =				resnets
										lowerCAmelCase  =				attentions

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




						def __call__(							self				:			Optional[Any]						,						lowerCAmelCase				:			Tuple						,						lowerCAmelCase				:			Dict						,						lowerCAmelCase				:			List[Any]						,						lowerCAmelCase				:			Any						,						lowerCAmelCase				:			Dict=True					):
										for resnet, attn in zip(self.resnets						,						self.attentions					):
														# pop res hidden states
														lowerCAmelCase  =				res_hidden_states_tuple[-1]
														lowerCAmelCase  =				res_hidden_states_tuple[:-1]
														lowerCAmelCase  =				jnp.concatenate((hidden_states, res_hidden_states)						,						axis=-1					)

														lowerCAmelCase  =				resnet(lowerCAmelCase						,						lowerCAmelCase						,						deterministic=lowerCAmelCase					)
														lowerCAmelCase  =				attn(lowerCAmelCase						,						lowerCAmelCase						,						deterministic=lowerCAmelCase					)

										if self.add_upsample:
														lowerCAmelCase  =				self.upsamplers_a(lowerCAmelCase					)

										return hidden_states






class       SCREAMING_SNAKE_CASE__					(					nn.Module					):
						_a    						=						42
						_a    						=						42
						_a    						=						42
						_a    						=						0.0
						_a    						=						1
						_a    						=						True
						_a    						=						jnp.floataa


						def   __lowercase (							self				:			Tuple					):
										lowerCAmelCase  =				[]

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

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

										lowerCAmelCase  =				resnets

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




						def __call__(							self				:			Optional[Any]						,						lowerCAmelCase				:			Optional[Any]						,						lowerCAmelCase				:			List[str]						,						lowerCAmelCase				:			List[str]						,						lowerCAmelCase				:			Dict=True					):
										for resnet in self.resnets:
														# pop res hidden states
														lowerCAmelCase  =				res_hidden_states_tuple[-1]
														lowerCAmelCase  =				res_hidden_states_tuple[:-1]
														lowerCAmelCase  =				jnp.concatenate((hidden_states, res_hidden_states)						,						axis=-1					)

														lowerCAmelCase  =				resnet(lowerCAmelCase						,						lowerCAmelCase						,						deterministic=lowerCAmelCase					)

										if self.add_upsample:
														lowerCAmelCase  =				self.upsamplers_a(lowerCAmelCase					)

										return hidden_states






class       SCREAMING_SNAKE_CASE__					(					nn.Module					):
						_a    						=						42
						_a    						=						0.0
						_a    						=						1
						_a    						=						1
						_a    						=						False
						_a    						=						False
						_a    						=						jnp.floataa


						def   __lowercase (							self				:			List[Any]					):
										# there is always at least one resnet
										lowerCAmelCase  =				[
										    FlaxResnetBlockaD(
										        in_channels=self.in_channels						,						out_channels=self.in_channels						,						dropout_prob=self.dropout						,						dtype=self.dtype						,						)
										]

										lowerCAmelCase  =				[]

										for _ in range(self.num_layers					):
														lowerCAmelCase  =				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					)

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

										lowerCAmelCase  =				resnets
										lowerCAmelCase  =				attentions




						def __call__(							self				:			Optional[int]						,						lowerCAmelCase				:			List[str]						,						lowerCAmelCase				:			int						,						lowerCAmelCase				:			Dict						,						lowerCAmelCase				:			List[Any]=True					):
										lowerCAmelCase  =				self.resnets[0](lowerCAmelCase						,						lowerCAmelCase					)
										for attn, resnet in zip(self.attentions						,						self.resnets[1:]					):
														lowerCAmelCase  =				attn(lowerCAmelCase						,						lowerCAmelCase						,						deterministic=lowerCAmelCase					)
														lowerCAmelCase  =				resnet(lowerCAmelCase						,						lowerCAmelCase						,						deterministic=lowerCAmelCase					)

										return hidden_states





