




'''simple docstring'''







import unittest

import numpy as np

from transformers import AlbertConfig, 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.numpy as jnp

   from transformers.models.albert.modeling_flax_albert import (
       FlaxAlbertForMaskedLM,
       FlaxAlbertForMultipleChoice,
       FlaxAlbertForPreTraining,
       FlaxAlbertForQuestionAnswering,
       FlaxAlbertForSequenceClassification,
       FlaxAlbertForTokenClassification,
       FlaxAlbertModel,
   )


class 			_a  (	unittest.TestCase ):
       def __init__(	self						:	List[Any]   ,				lowercase						:	int   ,				lowercase						:	str=13   ,				lowercase						:	Dict=7   ,				lowercase						:	int=True   ,				lowercase						:	List[Any]=True   ,				lowercase						:	Tuple=True   ,				lowercase						:	Optional[Any]=True   ,				lowercase						:	List[str]=99   ,				lowercase						:	Any=32   ,				lowercase						:	List[Any]=5   ,				lowercase						:	Dict=4   ,				lowercase						:	Dict=37   ,				lowercase						:	Any="gelu"   ,				lowercase						:	Optional[int]=0.1   ,				lowercase						:	Optional[Any]=0.1   ,				lowercase						:	int=512   ,				lowercase						:	Optional[Any]=16   ,				lowercase						:	Dict=2   ,				lowercase						:	Tuple=0.02   ,				lowercase						:	Tuple=4   ,				):



           '''simple docstring'''

           UpperCAmelCase			       =		parent
           UpperCAmelCase			       =		batch_size
           UpperCAmelCase			       =		seq_length
           UpperCAmelCase			       =		is_training
           UpperCAmelCase			       =		use_attention_mask
           UpperCAmelCase			       =		use_token_type_ids
           UpperCAmelCase			       =		use_labels
           UpperCAmelCase			       =		vocab_size
           UpperCAmelCase			       =		hidden_size
           UpperCAmelCase			       =		num_hidden_layers
           UpperCAmelCase			       =		num_attention_heads
           UpperCAmelCase			       =		intermediate_size
           UpperCAmelCase			       =		hidden_act
           UpperCAmelCase			       =		hidden_dropout_prob
           UpperCAmelCase			       =		attention_probs_dropout_prob
           UpperCAmelCase			       =		max_position_embeddings
           UpperCAmelCase			       =		type_vocab_size
           UpperCAmelCase			       =		type_sequence_label_size
           UpperCAmelCase			       =		initializer_range
           UpperCAmelCase			       =		num_choices
       def     A      (	self						:	List[Any]				):



           '''simple docstring'''

           UpperCAmelCase			       =		ids_tensor([self.batch_size, self.seq_length]   ,				self.vocab_size				)

           UpperCAmelCase			       =		None
           if self.use_attention_mask:
               UpperCAmelCase			       =		random_attention_mask([self.batch_size, self.seq_length]				)

           UpperCAmelCase			       =		None
           if self.use_token_type_ids:
               UpperCAmelCase			       =		ids_tensor([self.batch_size, self.seq_length]   ,				self.type_vocab_size				)

           UpperCAmelCase			       =		AlbertConfig(
               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=lowerCamelCase__   ,				initializer_range=self.initializer_range   ,				)

           return config, input_ids, token_type_ids, attention_mask







       def     A      (	self						:	Any				):



           '''simple docstring'''

           UpperCAmelCase			       =		self.prepare_config_and_inputs()
           UpperCAmelCase			       =		config_and_inputs
           UpperCAmelCase			       =		{"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": attention_mask}
           return config, inputs_dict






@require_flax
class 			_a  (	SCREAMING_SNAKE_CASE__							,							unittest.TestCase ):
       __a				:     Any   				=					(
           (
               FlaxAlbertModel,
               FlaxAlbertForPreTraining,
               FlaxAlbertForMaskedLM,
               FlaxAlbertForMultipleChoice,
               FlaxAlbertForQuestionAnswering,
               FlaxAlbertForSequenceClassification,
               FlaxAlbertForTokenClassification,
               FlaxAlbertForQuestionAnswering,
           )
           if is_flax_available()
           else ()
       )
       def     A      (	self						:	List[Any]				):



           '''simple docstring'''

           UpperCAmelCase			       =		FlaxAlbertModelTester(self				)







       @slow
       def     A      (	self						:	Union[str, Any]				):



           '''simple docstring'''

           for model_class_name in self.all_model_classes:
               UpperCAmelCase			       =		model_class_name.from_pretrained('''albert-base-v2'''				)
               UpperCAmelCase			       =		model(np.ones((1, 1)				)				)
               self.assertIsNotNone(lowerCamelCase__				)






@require_flax
class 			_a  (	unittest.TestCase ):
       @slow
       def     A      (	self						:	Optional[int]				):



           '''simple docstring'''

           UpperCAmelCase			       =		FlaxAlbertModel.from_pretrained('''albert-base-v2'''				)
           UpperCAmelCase			       =		np.array([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]				)
           UpperCAmelCase			       =		np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]				)
           UpperCAmelCase			       =		model(lowerCamelCase__   ,				attention_mask=lowerCamelCase__				)[0]
           UpperCAmelCase			       =		(1, 11, 768)
           self.assertEqual(output.shape   ,				lowerCamelCase__				)
           UpperCAmelCase			       =		np.array(
               [[[-0.6513, 1.5035, -0.2766], [-0.6515, 1.5046, -0.2780], [-0.6512, 1.5049, -0.2784]]]				)

           self.assertTrue(jnp.allclose(output[:, 1:4, 1:4]   ,				lowerCamelCase__   ,				atol=1E-4				)				)









from manim import *






class SCREAMING_SNAKE_CASE__     (		SCREAMING_SNAKE_CASE__    ):




    '''simple docstring'''






    def 			_lowerCAmelCase					(       self		):
        A :       Union[str, Any]          =   Rectangle(height=0.5,		width=0.5		)
        A :       Optional[int]          =   Rectangle(height=0.25,		width=0.25		)
        A :       Optional[Any]          =   Rectangle(height=0.46,		width=0.46		).set_stroke(width=0		)

        A :       List[str]          =   [mem.copy() for i in range(6		)]
        A :       Any          =   [mem.copy() for i in range(6		)]
        A :       int          =   VGroup(*lowerCamelCase__		).arrange(lowerCamelCase__,		buff=0		)
        A :       Tuple          =   VGroup(*lowerCamelCase__		).arrange(lowerCamelCase__,		buff=0		)
        A :       str          =   VGroup(lowerCamelCase__,		lowerCamelCase__		).arrange(lowerCamelCase__,		buff=0		)
        A :       List[Any]          =   Text("""CPU""",		font_size=24		)
        A :       Optional[int]          =   Group(lowerCamelCase__,		lowerCamelCase__		).arrange(lowerCamelCase__,		buff=0.5,		aligned_edge=lowerCamelCase__		)
        cpu.move_to([-2.5, -0.5, 0]		)
        self.add(lowerCamelCase__		)

        A :       List[Any]          =   [mem.copy() for i in range(4		)]
        A :       Optional[Any]          =   VGroup(*lowerCamelCase__		).arrange(lowerCamelCase__,		buff=0		)
        A :       Dict          =   Text("""GPU""",		font_size=24		)
        A :       Any          =   Group(lowerCamelCase__,		lowerCamelCase__		).arrange(lowerCamelCase__,		buff=0.5,		aligned_edge=lowerCamelCase__		)
        gpu.move_to([-1, -1, 0]		)
        self.add(lowerCamelCase__		)

        A :       Optional[int]          =   [mem.copy() for i in range(6		)]
        A :       List[str]          =   VGroup(*lowerCamelCase__		).arrange(lowerCamelCase__,		buff=0		)

        A :       Optional[int]          =   Text("""Model""",		font_size=24		)
        A :       List[Any]          =   Group(lowerCamelCase__,		lowerCamelCase__		).arrange(lowerCamelCase__,		buff=0.5,		aligned_edge=lowerCamelCase__		)
        model.move_to([3, -1.0, 0]		)
        self.add(lowerCamelCase__		)

        A :       Tuple          =   []
        A :       Tuple          =   []
        A :       Any          =   []

        for i, rect in enumerate(lowerCamelCase__		):
            rect.set_stroke(lowerCamelCase__		)

            A :       Any          =   Rectangle(height=0.46 / 4,		width=0.46 / 3		).set_stroke(width=0.0		).set_fill(lowerCamelCase__,		opacity=0.7		)

            if i == 0:
                cpu_target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT		),		buff=0.02,		direction=lowerCamelCase__		)
                cpu_target.set_x(cpu_target.get_x() + 0.1		)
            elif i == 3:
                cpu_target.next_to(model_cpu_arr[0],		direction=lowerCamelCase__,		buff=0.0		)
            else:
                cpu_target.next_to(model_cpu_arr[i - 1],		direction=lowerCamelCase__,		buff=0.0		)
            self.add(lowerCamelCase__		)
            model_cpu_arr.append(lowerCamelCase__		)

        self.add(*lowerCamelCase__,		*lowerCamelCase__,		*lowerCamelCase__		)

        A :       int          =   [mem.copy() for i in range(6		)]
        A :       List[Any]          =   VGroup(*lowerCamelCase__		).arrange(lowerCamelCase__,		buff=0		)

        A :       str          =   Text("""Loaded Checkpoint""",		font_size=24		)
        A :       List[str]          =   Group(lowerCamelCase__,		lowerCamelCase__		).arrange(lowerCamelCase__,		buff=0.5,		aligned_edge=lowerCamelCase__		)
        checkpoint.move_to([3, 0.5, 0]		)

        self.add(lowerCamelCase__		)

        A :       Optional[int]          =   []
        A :       List[Any]          =   []

        for i, rect in enumerate(lowerCamelCase__		):
            A :       int          =   fill.copy().set_fill(lowerCamelCase__,		opacity=0.7		)
            target.move_to(lowerCamelCase__		)
            ckpt_arr.append(lowerCamelCase__		)

            A :       List[Any]          =   target.copy()
            if i < 5:
                cpu_target.move_to(cpu_left_col_base[i + 1]		)
            else:
                cpu_target.move_to(cpu_right_col_base[i - 5]		)
            ckpt_cpu_arr.append(lowerCamelCase__		)
        self.add(*lowerCamelCase__,		*lowerCamelCase__		)

        A :       str          =   Square(side_length=2.2		)
        key.move_to([-5, 2, 0]		)

        A :       List[Any]          =   MarkupText(
            f'''<b>Key:</b>\n\n<span fgcolor=\'{YELLOW}\'>●</span> Empty Model''',		font_size=18,		)

        key_text.move_to([-5, 2.4, 0]		)

        self.add(lowerCamelCase__,		lowerCamelCase__		)

        A :       Union[str, Any]          =   MarkupText(
            f'''<span fgcolor=\'{BLUE}\'>●</span> Checkpoint''',		font_size=18,		)

        blue_text.next_to(lowerCamelCase__,		DOWN * 2.4,		aligned_edge=key_text.get_left()		)
        self.add(lowerCamelCase__		)

        A :       List[str]          =   MarkupText(
            f'''Based on the passed in configuration, weights are stored in\na variety of np.memmaps on disk or to a particular device.''',		font_size=24,		)
        step_a.move_to([2, 2, 0]		)

        A :       List[str]          =   [meta_mem.copy() for i in range(6		)]
        A :       List[Any]          =   [meta_mem.copy() for i in range(6		)]
        A :       List[Any]          =   VGroup(*lowerCamelCase__		).arrange(lowerCamelCase__,		buff=0		)
        A :       List[str]          =   VGroup(*lowerCamelCase__		).arrange(lowerCamelCase__,		buff=0		)
        A :       Dict          =   VGroup(lowerCamelCase__,		lowerCamelCase__		).arrange(lowerCamelCase__,		buff=0		)
        A :       Optional[Any]          =   Text("""Disk""",		font_size=24		)
        A :       List[str]          =   Group(lowerCamelCase__,		lowerCamelCase__		).arrange(lowerCamelCase__,		buff=0.5,		aligned_edge=lowerCamelCase__		)
        disk.move_to([-4.0, -1.25, 0]		)
        self.play(Write(lowerCamelCase__,		run_time=3		),		Write(lowerCamelCase__,		run_time=1		),		Create(lowerCamelCase__,		run_time=1		)		)

        A :       str          =   []
        for i, rect in enumerate(lowerCamelCase__		):
            A :       Optional[Any]          =   rect.copy()
            target.generate_target()
            target.target.move_to(disk_left_col_base[i]		).scale(0.5		)
            animations.append(MoveToTarget(lowerCamelCase__,		run_time=1.5		)		)
        self.play(*lowerCamelCase__		)

        self.play(FadeOut(lowerCamelCase__		)		)

        A :       List[str]          =   MarkupText(f'''Then, the checkpoint is removed from memory\nthrough garbage collection.''',		font_size=24		)
        step_a.move_to([2, 2, 0]		)

        self.play(Write(lowerCamelCase__,		run_time=3		)		)

        self.play(
            FadeOut(lowerCamelCase__,		lowerCamelCase__,		*lowerCamelCase__,		*lowerCamelCase__		),		)

        self.wait()


