
import flax.linen as nn
import jax
import jax.numpy as jnp
class  lowercase__(				nn.Module							):



	"""simple docstring"""




	a			:int
	a			:jnp.dtype         =	jnp.floataa
	def 						_lowercase  (       self  :			List[Any]       )       ->     Union[str, Any]:
					lowercase_       =					nn.Conv(
					    self.out_channels       ,							kernel_size=(3, 3)       ,							strides=(1, 1)       ,							padding=((1, 1), (1, 1))       ,							dtype=self.dtype       ,							)




	def __call__(       self  :			str       ,							SCREAMING_SNAKE_CASE_  :			Optional[Any]       )       ->     Optional[int]:
					lowercase_  ,	lowercase_  ,	lowercase_  ,	lowercase_       =					hidden_states.shape
					lowercase_       =					jax.image.resize(
					    SCREAMING_SNAKE_CASE_       ,							shape=(batch, height * 2, width * 2, channels)       ,							method='''nearest'''       ,							)
					lowercase_       =					self.conv(SCREAMING_SNAKE_CASE_       )
					return hidden_states

class  lowercase__(				nn.Module							):



	"""simple docstring"""




	a			:int
	a			:jnp.dtype         =	jnp.floataa
	def 						_lowercase  (       self  :			Union[str, Any]       )       ->     Union[str, Any]:
					lowercase_       =					nn.Conv(
					    self.out_channels       ,							kernel_size=(3, 3)       ,							strides=(2, 2)       ,							padding=((1, 1), (1, 1))       ,							dtype=self.dtype       ,							)




	def __call__(       self  :			Tuple       ,							SCREAMING_SNAKE_CASE_  :			Dict       )       ->     List[str]:
					# pad = ((0, 0), (0, 1), (0, 1), (0, 0))  # pad height and width dim
					# hidden_states = jnp.pad(hidden_states, pad_width=pad)
					lowercase_       =					self.conv(SCREAMING_SNAKE_CASE_       )
					return hidden_states

class  lowercase__(				nn.Module							):



	"""simple docstring"""




	a			:int
	a			:int         =	None
	a			:float         =	0.0
	a			:bool         =	None
	a			:jnp.dtype         =	jnp.floataa
	def 						_lowercase  (       self  :			Any       )       ->     List[str]:
					lowercase_       =					self.in_channels if self.out_channels is None else self.out_channels

					lowercase_       =					nn.GroupNorm(num_groups=3_2       ,							epsilon=1e-5       )
					lowercase_       =					nn.Conv(
					    SCREAMING_SNAKE_CASE_       ,							kernel_size=(3, 3)       ,							strides=(1, 1)       ,							padding=((1, 1), (1, 1))       ,							dtype=self.dtype       ,							)

					lowercase_       =					nn.Dense(SCREAMING_SNAKE_CASE_       ,							dtype=self.dtype       )

					lowercase_       =					nn.GroupNorm(num_groups=3_2       ,							epsilon=1e-5       )
					lowercase_       =					nn.Dropout(self.dropout_prob       )
					lowercase_       =					nn.Conv(
					    SCREAMING_SNAKE_CASE_       ,							kernel_size=(3, 3)       ,							strides=(1, 1)       ,							padding=((1, 1), (1, 1))       ,							dtype=self.dtype       ,							)

					lowercase_       =					self.in_channels != out_channels if self.use_nin_shortcut is None else self.use_nin_shortcut

					lowercase_       =					None
					if use_nin_shortcut:
									lowercase_       =					nn.Conv(
									    SCREAMING_SNAKE_CASE_       ,							kernel_size=(1, 1)       ,							strides=(1, 1)       ,							padding='''VALID'''       ,							dtype=self.dtype       ,							)




	def __call__(       self  :			Union[str, Any]       ,							SCREAMING_SNAKE_CASE_  :			Dict       ,							SCREAMING_SNAKE_CASE_  :			Any       ,							SCREAMING_SNAKE_CASE_  :			Optional[int]=True       )       ->     Tuple:
					lowercase_       =					hidden_states
					lowercase_       =					self.norma(SCREAMING_SNAKE_CASE_       )
					lowercase_       =					nn.swish(SCREAMING_SNAKE_CASE_       )
					lowercase_       =					self.conva(SCREAMING_SNAKE_CASE_       )

					lowercase_       =					self.time_emb_proj(nn.swish(SCREAMING_SNAKE_CASE_       )       )
					lowercase_       =					jnp.expand_dims(jnp.expand_dims(SCREAMING_SNAKE_CASE_       ,							1       )       ,							1       )
					lowercase_       =					hidden_states + temb

					lowercase_       =					self.norma(SCREAMING_SNAKE_CASE_       )
					lowercase_       =					nn.swish(SCREAMING_SNAKE_CASE_       )
					lowercase_       =					self.dropout(SCREAMING_SNAKE_CASE_       ,							SCREAMING_SNAKE_CASE_       )
					lowercase_       =					self.conva(SCREAMING_SNAKE_CASE_       )

					if self.conv_shortcut is not None:
									lowercase_       =					self.conv_shortcut(SCREAMING_SNAKE_CASE_       )

					return hidden_states + residual






import unittest

import numpy as np
import torch

from .utils_summarization import build_mask, compute_token_type_ids, process_story, truncate_or_pad
class  lowercase__(				unittest.TestCase							):



	"""simple docstring"""




	def 						_lowercase  (       self  :			List[str]       )       ->     List[Any]:
					lowercase_       =					1_0
	def 						_lowercase  (       self  :			int       )       ->     List[str]:
					lowercase_       =					[1, 2, 3, 4]
					lowercase_       =					[1, 2, 3, 4, 0, 0, 0, 0, 0, 0]
					self.assertEqual(truncate_or_pad(SCREAMING_SNAKE_CASE_       ,							self.block_size       ,							0       )       ,							SCREAMING_SNAKE_CASE_       )
	def 						_lowercase  (       self  :			int       )       ->     Optional[Any]:
					lowercase_       =					[1, 2, 3, 4, 5, 6, 7, 8, 9, 1_0]
					lowercase_       =					[1, 2, 3, 4, 5, 6, 7, 8, 9, 1_0]
					self.assertEqual(truncate_or_pad(SCREAMING_SNAKE_CASE_       ,							self.block_size       ,							0       )       ,							SCREAMING_SNAKE_CASE_       )
	def 						_lowercase  (       self  :			Union[str, Any]       )       ->     Optional[int]:
					lowercase_       =					[1, 2, 3, 4, 5, 6, 7, 8, 9, 1_0, 1_1, 1_2, 1_3]
					lowercase_       =					[1, 2, 3, 4, 5, 6, 7, 8, 9, 1_0]
					self.assertEqual(truncate_or_pad(SCREAMING_SNAKE_CASE_       ,							self.block_size       ,							0       )       ,							SCREAMING_SNAKE_CASE_       )
	def 						_lowercase  (       self  :			Any       )       ->     List[Any]:
					lowercase_       =					'''It was the year of Our Lord one thousand seven hundred and
        seventy-five.\n\nSpiritual revelations were conceded to England at that
        favoured period, as at this.'''
					lowercase_  ,	lowercase_       =					process_story(SCREAMING_SNAKE_CASE_       )
					self.assertEqual(SCREAMING_SNAKE_CASE_       ,							[]       )
	def 						_lowercase  (       self  :			List[str]       )       ->     List[str]:
					lowercase_       =					''''''
					lowercase_  ,	lowercase_       =					process_story(SCREAMING_SNAKE_CASE_       )
					self.assertEqual(SCREAMING_SNAKE_CASE_       ,							[]       )
					self.assertEqual(SCREAMING_SNAKE_CASE_       ,							[]       )
	def 						_lowercase  (       self  :			Union[str, Any]       )       ->     Union[str, Any]:
					lowercase_       =					(
					    '''It was the year of Our Lord one thousand seven hundred and '''
					    '''seventy-five\n\nSpiritual revelations were conceded to England '''
					    '''at that favoured period, as at this.\n@highlight\n\nIt was the best of times'''
					)
					lowercase_  ,	lowercase_       =					process_story(SCREAMING_SNAKE_CASE_       )

					lowercase_       =					[
					    '''It was the year of Our Lord one thousand seven hundred and seventy-five.''',
					    '''Spiritual revelations were conceded to England at that favoured period, as at this.''',
					]
					self.assertEqual(SCREAMING_SNAKE_CASE_       ,							SCREAMING_SNAKE_CASE_       )

					lowercase_       =					['''It was the best of times.''']
					self.assertEqual(SCREAMING_SNAKE_CASE_       ,							SCREAMING_SNAKE_CASE_       )
	def 						_lowercase  (       self  :			Union[str, Any]       )       ->     Optional[Any]:
					lowercase_       =					torch.tensor([1, 2, 3, 4]       )
					lowercase_       =					torch.tensor([1, 1, 1, 1]       )
					np.testing.assert_array_equal(build_mask(SCREAMING_SNAKE_CASE_       ,							0       ).numpy()       ,							expected.numpy()       )
	def 						_lowercase  (       self  :			List[Any]       )       ->     Tuple:
					lowercase_       =					torch.tensor([1, 2, 3, 4, 2_3, 2_3, 2_3]       )
					lowercase_       =					torch.tensor([1, 1, 1, 1, 0, 0, 0]       )
					np.testing.assert_array_equal(build_mask(SCREAMING_SNAKE_CASE_       ,							2_3       ).numpy()       ,							expected.numpy()       )
	def 						_lowercase  (       self  :			int       )       ->     Dict:
					lowercase_       =					torch.tensor([8, 2, 3, 4, 1, 1, 1]       )
					lowercase_       =					torch.tensor([1, 1, 1, 1, 0, 0, 0]       )
					np.testing.assert_array_equal(build_mask(SCREAMING_SNAKE_CASE_       ,							1       ).numpy()       ,							expected.numpy()       )




	def 						_lowercase  (       self  :			List[str]       )       ->     Tuple:
					lowercase_       =					1_0_1
					lowercase_       =					torch.tensor([[1, 2, 3, 4, 5, 6], [1, 2, 3, 1_0_1, 5, 6], [1, 1_0_1, 3, 4, 1_0_1, 6]]       )
					lowercase_       =					torch.tensor([[1, 1, 1, 1, 1, 1], [1, 1, 1, 0, 0, 0], [1, 0, 0, 0, 1, 1]]       )

					lowercase_       =					compute_token_type_ids(SCREAMING_SNAKE_CASE_       ,							SCREAMING_SNAKE_CASE_       )
					np.testing.assert_array_equal(SCREAMING_SNAKE_CASE_       ,							SCREAMING_SNAKE_CASE_       )




