import unittest

from transformers import BigBirdConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask


if is_flax_available():
    import jax

    from transformers.models.big_bird.modeling_flax_big_bird import (
        FlaxBigBirdForCausalLM,
        FlaxBigBirdForMaskedLM,
        FlaxBigBirdForMultipleChoice,
        FlaxBigBirdForPreTraining,
        FlaxBigBirdForQuestionAnswering,
        FlaxBigBirdForSequenceClassification,
        FlaxBigBirdForTokenClassification,
        FlaxBigBirdModel,
    )


class 			lowerCamelCase_  (     unittest.TestCase				):






    '''simple docstring'''






    def __init__(			self				,				__lowercase				,				__lowercase=2				,				__lowercase=56				,				__lowercase=True				,				__lowercase=True				,				__lowercase=True				,				__lowercase=True				,				__lowercase=99				,				__lowercase=32				,				__lowercase=2				,				__lowercase=2				,				__lowercase=7				,				__lowercase="gelu_new"				,				__lowercase=0.1				,				__lowercase=0.1				,				__lowercase=512				,				__lowercase=16				,				__lowercase=2				,				__lowercase=0.02				,				__lowercase=4				,				__lowercase="block_sparse"				,				__lowercase=True				,				__lowercase=False				,				__lowercase=2				,				__lowercase=3				,				) ->			int:
        __UpperCamelCase  :Optional[int]    						=	parent
        __UpperCamelCase  :List[Any]    						=	batch_size
        __UpperCamelCase  :str    						=	seq_length
        __UpperCamelCase  :Any    						=	is_training
        __UpperCamelCase  :Any    						=	use_attention_mask
        __UpperCamelCase  :int    						=	use_token_type_ids
        __UpperCamelCase  :List[str]    						=	use_labels
        __UpperCamelCase  :Optional[int]    						=	vocab_size
        __UpperCamelCase  :Dict    						=	hidden_size
        __UpperCamelCase  :Union[str, Any]    						=	num_hidden_layers
        __UpperCamelCase  :str    						=	num_attention_heads
        __UpperCamelCase  :Optional[int]    						=	intermediate_size
        __UpperCamelCase  :Optional[int]    						=	hidden_act
        __UpperCamelCase  :int    						=	hidden_dropout_prob
        __UpperCamelCase  :Optional[int]    						=	attention_probs_dropout_prob
        __UpperCamelCase  :List[Any]    						=	max_position_embeddings
        __UpperCamelCase  :List[Any]    						=	type_vocab_size
        __UpperCamelCase  :Optional[int]    						=	type_sequence_label_size
        __UpperCamelCase  :List[Any]    						=	initializer_range
        __UpperCamelCase  :List[Any]    						=	num_choices

        __UpperCamelCase  :Union[str, Any]    						=	rescale_embeddings
        __UpperCamelCase  :Union[str, Any]    						=	attention_type
        __UpperCamelCase  :List[Any]    						=	use_bias
        __UpperCamelCase  :Union[str, Any]    						=	block_size
        __UpperCamelCase  :str    						=	num_random_blocks







    def     UpperCamelCase__     (			self) ->			Tuple:
        __UpperCamelCase  :List[str]    						=	ids_tensor([self.batch_size, self.seq_length]				,				self.vocab_size)

        __UpperCamelCase  :Optional[int]    						=	None
        if self.use_attention_mask:
            __UpperCamelCase  :Dict    						=	random_attention_mask([self.batch_size, self.seq_length])

        __UpperCamelCase  :List[str]    						=	None
        if self.use_token_type_ids:
            __UpperCamelCase  :Dict    						=	ids_tensor([self.batch_size, self.seq_length]				,				self.type_vocab_size)

        __UpperCamelCase  :Union[str, Any]    						=	BigBirdConfig(
            vocab_size=self.vocab_size				,				hidden_size=self.hidden_size				,				num_hidden_layers=self.num_hidden_layers				,				num_attention_heads=self.num_attention_heads				,				intermediate_size=self.intermediate_size				,				hidden_act=self.hidden_act				,				hidden_dropout_prob=self.hidden_dropout_prob				,				attention_probs_dropout_prob=self.attention_probs_dropout_prob				,				max_position_embeddings=self.max_position_embeddings				,				type_vocab_size=self.type_vocab_size				,				is_decoder=__lowercase				,				initializer_range=self.initializer_range				,				attention_type=self.attention_type				,				block_size=self.block_size				,				num_random_blocks=self.num_random_blocks				,				use_bias=self.use_bias				,				rescale_embeddings=self.rescale_embeddings				,				)

        return config, input_ids, token_type_ids, attention_mask







    def     UpperCamelCase__     (			self) ->			Any:
        __UpperCamelCase  :str    						=	self.prepare_config_and_inputs()
        __UpperCamelCase						, __UpperCamelCase						, __UpperCamelCase						, __UpperCamelCase  :int    						=	config_and_inputs
        __UpperCamelCase  :str    						=	{
            '''input_ids''': input_ids,
            '''token_type_ids''': token_type_ids,
            '''attention_mask''': attention_mask,
        }
        return config, inputs_dict


@require_flax
class 			lowerCamelCase_  (     UpperCAmelCase_		,			unittest.TestCase				):






    '''simple docstring'''






    a__			:		Union[str, Any]      			=   (
        (
            FlaxBigBirdForCausalLM,
            FlaxBigBirdModel,
            FlaxBigBirdForPreTraining,
            FlaxBigBirdForMaskedLM,
            FlaxBigBirdForMultipleChoice,
            FlaxBigBirdForQuestionAnswering,
            FlaxBigBirdForSequenceClassification,
            FlaxBigBirdForTokenClassification,
        )
        if is_flax_available()
        else ()
    )

    a__			:		str      			=   False
    a__			:		str      			=   False







    def     UpperCamelCase__     (			self) ->			int:
        __UpperCamelCase  :str    						=	FlaxBigBirdModelTester(self)







    @slow
    # copied from `test_modeling_flax_common` because it takes much longer than other models
    def     UpperCamelCase__     (			self) ->			Dict:
        super().test_from_pretrained_save_pretrained()







    @slow
    # copied from `test_modeling_flax_common` because it takes much longer than other models
    def     UpperCamelCase__     (			self) ->			Any:
        super().test_from_pretrained_with_no_automatic_init()







    @slow
    # copied from `test_modeling_flax_common` because it takes much longer than other models
    def     UpperCamelCase__     (			self) ->			List[Any]:
        super().test_no_automatic_init()







    @slow
    # copied from `test_modeling_flax_common` because it takes much longer than other models
    def     UpperCamelCase__     (			self) ->			str:
        super().test_hidden_states_output()







    @slow
    def     UpperCamelCase__     (			self) ->			Union[str, Any]:
        for model_class_name in self.all_model_classes:
            __UpperCamelCase  :Optional[Any]    						=	model_class_name.from_pretrained('''google/bigbird-roberta-base''')
            self.assertIsNotNone(__lowercase)







    def     UpperCamelCase__     (			self) ->			str:
        if self.test_attn_probs:
            super().test_attention_outputs()







    @slow
    # copied from `test_modeling_flax_common` because it takes much longer than other models
    def     UpperCamelCase__     (			self) ->			Optional[int]:
        __UpperCamelCase						, __UpperCamelCase  :Union[str, Any]    						=	self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
            with self.subTest(model_class.__name__):
                __UpperCamelCase  :Tuple    						=	self._prepare_for_class(__lowercase				,				__lowercase)
                __UpperCamelCase  :Dict    						=	model_class(__lowercase)

                @jax.jit
                def model_jitted(__lowercase				,				__lowercase=None				,				**__lowercase):
                    return model(input_ids=__lowercase				,				attention_mask=__lowercase				,				**__lowercase)

                with self.subTest('''JIT Enabled'''):
                    __UpperCamelCase  :Any    						=	model_jitted(**__lowercase).to_tuple()

                with self.subTest('''JIT Disabled'''):
                    with jax.disable_jit():
                        __UpperCamelCase  :Optional[int]    						=	model_jitted(**__lowercase).to_tuple()

                self.assertEqual(len(__lowercase)				,				len(__lowercase))
                for jitted_output, output in zip(__lowercase				,				__lowercase):
                    self.assertEqual(jitted_output.shape				,				output.shape)







    def     UpperCamelCase__     (			self				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase=1E-5				,				__lowercase="outputs"				,				__lowercase=None) ->			List[str]:
        # `bigbird_block_sparse_attention` in `FlaxBigBird` returns `attention_probs = None`, while in PyTorch version,
        # an effort was done to return `attention_probs` (yet to be verified).
        if name.startswith('''outputs.attentions'''):
            return
        else:
            super().check_pt_flax_outputs(__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase				,				__lowercase)






def lowerCamelCase						(     SCREAMING_SNAKE_CASE			):
    '''simple docstring'''







    __UpperCamelCase  :Optional[int]    						=	generate_pascal_triangle(SCREAMING_SNAKE_CASE			)
    for row_idx in range(SCREAMING_SNAKE_CASE			):
        # Print left spaces
        for _ in range(num_rows - row_idx - 1			):
            print(end=''' '''			)
        # Print row values
        for col_idx in range(row_idx + 1			):
            if col_idx != row_idx:
                print(triangle[row_idx][col_idx]						,    end=''' '''			)
            else:
                print(triangle[row_idx][col_idx]						,    end=''''''			)
        print()



def lowerCamelCase						(     SCREAMING_SNAKE_CASE			):
    '''simple docstring'''







    if not isinstance(SCREAMING_SNAKE_CASE						,    SCREAMING_SNAKE_CASE			):
        raise TypeError('''The input value of \'num_rows\' should be \'int\''''			)

    if num_rows == 0:
        return []
    elif num_rows < 0:
        raise ValueError(
            '''The input value of \'num_rows\' should be greater than or equal to 0'''			)

    __UpperCamelCase  :list[list[int]]    						=	[]
    for current_row_idx in range(SCREAMING_SNAKE_CASE			):
        __UpperCamelCase  :List[Any]    						=	populate_current_row(SCREAMING_SNAKE_CASE						,    SCREAMING_SNAKE_CASE			)
        triangle.append(SCREAMING_SNAKE_CASE			)
    return triangle



def lowerCamelCase						(     SCREAMING_SNAKE_CASE						,    SCREAMING_SNAKE_CASE			):
    '''simple docstring'''







    __UpperCamelCase  :int    						=	[-1] * (current_row_idx + 1)
    # first and last elements of current row are equal to 1
    __UpperCamelCase						, __UpperCamelCase  :int    						=	1, 1
    for current_col_idx in range(1						,    SCREAMING_SNAKE_CASE			):
        calculate_current_element(
            SCREAMING_SNAKE_CASE						,    SCREAMING_SNAKE_CASE						,    SCREAMING_SNAKE_CASE						,    SCREAMING_SNAKE_CASE			)
    return current_row



def lowerCamelCase						(     SCREAMING_SNAKE_CASE						,    SCREAMING_SNAKE_CASE						,    SCREAMING_SNAKE_CASE						,    SCREAMING_SNAKE_CASE						,    ):
    '''simple docstring'''







    __UpperCamelCase  :Dict    						=	triangle[current_row_idx - 1][current_col_idx - 1]
    __UpperCamelCase  :List[Any]    						=	triangle[current_row_idx - 1][current_col_idx]
    __UpperCamelCase  :List[Any]    						=	above_to_left_elt + above_to_right_elt



def lowerCamelCase						(     SCREAMING_SNAKE_CASE			):
    '''simple docstring'''







    if not isinstance(SCREAMING_SNAKE_CASE						,    SCREAMING_SNAKE_CASE			):
        raise TypeError('''The input value of \'num_rows\' should be \'int\''''			)

    if num_rows == 0:
        return []
    elif num_rows < 0:
        raise ValueError(
            '''The input value of \'num_rows\' should be greater than or equal to 0'''			)

    __UpperCamelCase  :list[list[int]]    						=	[[1]]

    for row_index in range(1						,    SCREAMING_SNAKE_CASE			):
        __UpperCamelCase  :Optional[Any]    						=	[0] + result[-1] + [0]
        __UpperCamelCase  :Any    						=	row_index + 1
        # Calculate the number of distinct elements in a row
        __UpperCamelCase  :Optional[Any]    						=	sum(divmod(SCREAMING_SNAKE_CASE						,    2			)			)
        __UpperCamelCase  :Union[str, Any]    						=	[
            temp_row[i - 1] + temp_row[i] for i in range(1						,    distinct_elements + 1			)
        ]
        __UpperCamelCase  :List[Any]    						=	row_first_half[: (row_index + 1) // 2]
        row_second_half.reverse()
        __UpperCamelCase  :List[str]    						=	row_first_half + row_second_half
        result.append(SCREAMING_SNAKE_CASE			)

    return result



def lowerCamelCase						(     ):
    '''simple docstring'''







    from collections.abc import Callable
    from timeit import timeit

    def benchmark_a_function(SCREAMING_SNAKE_CASE						,    SCREAMING_SNAKE_CASE			) -> None:
        __UpperCamelCase  :List[str]    						=	f"""{func.__name__}({value})"""
        __UpperCamelCase  :Optional[int]    						=	timeit(f"""__main__.{call}"""						,    setup='''import __main__'''			)
        # print(f"{call:38} = {func(value)} -- {timing:.4f} seconds")
        print(f"""{call:38} -- {timing:.4f} seconds"""			)

    for value in range(15			):  # (1, 7, 14):
        for func in (generate_pascal_triangle, generate_pascal_triangle_optimized):
            benchmark_a_function(SCREAMING_SNAKE_CASE						,    SCREAMING_SNAKE_CASE			)
        print()


if __name__ == "__main__":
    import doctest

    doctest.testmod()
    benchmark()





