




import unittest

from transformers import JukeboxTokenizer
from transformers.testing_utils import require_torch
class       a__   ( unittest.TestCase				):
						_a							:   List[str] =   JukeboxTokenizer
						_a							:   Optional[int] =   {
						    """artist""": """Zac Brown Band""",
						    """genres""": """Country""",
						    """lyrics""": """I met a traveller from an antique land,
        Who said \"Two vast and trunkless legs of stone
        Stand in the desert. . . . Near them, on the sand,
        Half sunk a shattered visage lies, whose frown,
        And wrinkled lip, and sneer of cold command,
        Tell that its sculptor well those passions read
        Which yet survive, stamped on these lifeless things,
        The hand that mocked them, and the heart that fed;
        And on the pedestal, these words appear:
        My name is Ozymandias, King of Kings;
        Look on my Works, ye Mighty, and despair!
        Nothing beside remains. Round the decay
        Of that colossal Wreck, boundless and bare
        The lone and level sands stretch far away
        """,
						}






						@require_torch
						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											import torch

											__lowerCAmelCase           =	JukeboxTokenizer.from_pretrained("openai/jukebox-1b-lyrics"      )
											__lowerCAmelCase           =	tokenizer(**self.metas      )["input_ids"]
											# fmt: off
											__lowerCAmelCase           =	[
											    torch.tensor([[
											        0, 0, 0, 7_1_6_9, 5_0_7, 9, 7_6, 3_9, 3_1, 4_6, 7_6, 2_7,
											        7_6, 4_6, 4_4, 2_7, 4_8, 3_1, 3_8, 3_8, 3_1, 4_4, 7_6, 3_2,
											        4_4, 4_1, 3_9, 7_6, 2_7, 4_0, 7_6, 2_7, 4_0, 4_6, 3_5, 4_3,
											        4_7, 3_1, 7_6, 3_8, 2_7, 4_0, 3_0, 6_4, 7_8, 7_6, 7_6, 7_6,
											        7_6, 7_6, 7_6, 7_6, 7_6, 2_3, 3_4, 4_1, 7_6, 4_5, 2_7, 3_5,
											        3_0, 7_6, 7_1, 2_0, 4_9, 4_1, 7_6, 4_8, 2_7, 4_5, 4_6, 7_6,
											        2_7, 4_0, 3_0, 7_6, 4_6, 4_4, 4_7, 4_0, 3_7, 3_8, 3_1, 4_5,
											        4_5, 7_6, 3_8, 3_1, 3_3, 4_5, 7_6, 4_1, 3_2, 7_6, 4_5, 4_6,
											        4_1, 4_0, 3_1, 7_8, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6,
											        1_9, 4_6, 2_7, 4_0, 3_0, 7_6, 3_5, 4_0, 7_6, 4_6, 3_4, 3_1,
											        7_6, 3_0, 3_1, 4_5, 3_1, 4_4, 4_6, 6_3, 7_6, 6_3, 7_6, 6_3,
											        7_6, 6_3, 7_6, 1_4, 3_1, 2_7, 4_4, 7_6, 4_6, 3_4, 3_1, 3_9,
											        6_4, 7_6, 4_1, 4_0, 7_6, 4_6, 3_4, 3_1, 7_6, 4_5, 2_7, 4_0,
											        3_0, 6_4, 7_8, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 8,
											        2_7, 3_8, 3_2, 7_6, 4_5, 4_7, 4_0, 3_7, 7_6, 2_7, 7_6, 4_5,
											        3_4, 2_7, 4_6, 4_6, 3_1, 4_4, 3_1, 3_0, 7_6, 4_8, 3_5, 4_5,
											        2_7, 3_3, 3_1, 7_6, 3_8, 3_5, 3_1, 4_5, 6_4, 7_6, 4_9, 3_4,
											        4_1, 4_5, 3_1, 7_6, 3_2, 4_4, 4_1, 4_9, 4_0, 6_4, 7_8, 7_6,
											        7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 1, 4_0, 3_0, 7_6, 4_9,
											        4_4, 3_5, 4_0, 3_7, 3_8, 3_1, 3_0, 7_6, 3_8, 3_5, 4_2, 6_4,
											        7_6, 2_7, 4_0, 3_0, 7_6, 4_5, 4_0, 3_1, 3_1, 4_4, 7_6, 4_1,
											        3_2, 7_6, 2_9, 4_1, 3_8, 3_0, 7_6, 2_9, 4_1, 3_9, 3_9, 2_7,
											        4_0, 3_0, 6_4, 7_8, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6,
											        2_0, 3_1, 3_8, 3_8, 7_6, 4_6, 3_4, 2_7, 4_6, 7_6, 3_5, 4_6,
											        4_5, 7_6, 4_5, 2_9, 4_7, 3_8, 4_2, 4_6, 4_1, 4_4, 7_6, 4_9,
											        3_1, 3_8, 3_8, 7_6, 4_6, 3_4, 4_1, 4_5, 3_1, 7_6, 4_2, 2_7,
											        4_5, 4_5, 3_5, 4_1, 4_0, 4_5, 7_6, 4_4, 3_1, 2_7, 3_0, 7_8,
											        7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 2_3, 3_4, 3_5, 2_9,
											        3_4, 7_6, 5_1, 3_1, 4_6, 7_6, 4_5, 4_7, 4_4, 4_8, 3_5, 4_8,
											        3_1, 6_4, 7_6, 4_5, 4_6, 2_7, 3_9, 4_2, 3_1, 3_0, 7_6, 4_1,
											        4_0, 7_6, 4_6, 3_4, 3_1, 4_5, 3_1, 7_6, 3_8, 3_5, 3_2, 3_1,
											        3_8, 3_1, 4_5, 4_5, 7_6, 4_6, 3_4, 3_5, 4_0, 3_3, 4_5, 6_4,
											        7_8, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 2_0, 3_4, 3_1,
											        7_6, 3_4, 2_7, 4_0, 3_0, 7_6, 4_6, 3_4, 2_7, 4_6, 7_6, 3_9,
											        4_1, 2_9, 3_7, 3_1, 3_0, 7_6, 4_6, 3_4, 3_1, 3_9, 6_4, 7_6,
											        2_7, 4_0, 3_0, 7_6, 4_6, 3_4, 3_1, 7_6, 3_4, 3_1, 2_7, 4_4,
											        4_6, 7_6, 4_6, 3_4, 2_7, 4_6, 7_6, 3_2, 3_1, 3_0, 6_6, 7_8,
											        7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 1, 4_0, 3_0, 7_6,
											        4_1, 4_0, 7_6, 4_6, 3_4, 3_1, 7_6, 4_2, 3_1, 3_0, 3_1, 4_5,
											        4_6, 2_7, 3_8, 6_4, 7_6, 4_6, 3_4, 3_1, 4_5, 3_1, 7_6, 4_9,
											        4_1, 4_4, 3_0, 4_5, 7_6, 2_7, 4_2, 4_2, 3_1, 2_7, 4_4, 6_5,
											        7_8, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 1_3, 5_1, 7_6,
											        4_0, 2_7, 3_9, 3_1, 7_6, 3_5, 4_5, 7_6, 1_5, 5_2, 5_1, 3_9,
											        2_7, 4_0, 3_0, 3_5, 2_7, 4_5, 6_4, 7_6, 1_1, 3_5, 4_0, 3_3,
											        7_6, 4_1, 3_2, 7_6, 1_1, 3_5, 4_0, 3_3, 4_5, 6_6, 7_8, 7_6,
											        7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 1_2, 4_1, 4_1, 3_7, 7_6,
											        4_1, 4_0, 7_6, 3_9, 5_1, 7_6, 2_3, 4_1, 4_4, 3_7, 4_5, 6_4,
											        7_6, 5_1, 3_1, 7_6, 1_3, 3_5, 3_3, 3_4, 4_6, 5_1, 6_4, 7_6,
											        2_7, 4_0, 3_0, 7_6, 3_0, 3_1, 4_5, 4_2, 2_7, 3_5, 4_4, 6_7,
											        7_8, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 1_4, 4_1, 4_6,
											        3_4, 3_5, 4_0, 3_3, 7_6, 2_8, 3_1, 4_5, 3_5, 3_0, 3_1, 7_6,
											        4_4, 3_1, 3_9, 2_7, 3_5, 4_0, 4_5, 6_3, 7_6, 1_8, 4_1, 4_7,
											        4_0, 3_0, 7_6, 4_6, 3_4, 3_1, 7_6, 3_0, 3_1, 2_9, 2_7, 5_1,
											        7_8, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 1_5, 3_2, 7_6,
											        4_6, 3_4, 2_7, 4_6, 7_6, 2_9, 4_1, 3_8, 4_1, 4_5, 4_5, 2_7,
											        3_8, 7_6, 2_3, 4_4, 3_1, 2_9, 3_7, 6_4, 7_6, 2_8, 4_1, 4_7,
											        4_0, 3_0, 3_8, 3_1, 4_5, 4_5, 7_6, 2_7, 4_0, 3_0, 7_6, 2_8,
											        2_7, 4_4, 3_1, 7_8, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6,
											        2_0, 3_4, 3_1, 7_6, 3_8, 4_1, 4_0, 3_1, 7_6, 2_7, 4_0, 3_0,
											        7_6, 3_8, 3_1, 4_8, 3_1, 3_8, 7_6, 4_5, 2_7, 4_0, 3_0, 4_5,
											        7_6, 4_5, 4_6, 4_4, 3_1, 4_6, 2_9, 3_4, 7_6, 3_2, 2_7, 4_4,
											        7_6, 2_7, 4_9, 2_7, 5_1, 7_8, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6,
											        7_6, 7_6]]      ),
											    torch.tensor([[0, 0, 0, 1_0_6_9, 1_1]]      ),
											    torch.tensor([[0, 0, 0, 1_0_6_9, 1_1]]      ),
											]
											# fmt: on
											self.assertTrue(torch.allclose(tokens[0]		,      EXPECTED_OUTPUT[0]      )      )
											self.assertTrue(torch.allclose(tokens[1]		,      EXPECTED_OUTPUT[1]      )      )
											self.assertTrue(torch.allclose(tokens[2]		,      EXPECTED_OUTPUT[2]      )      )






						@require_torch
						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											import torch

											__lowerCAmelCase           =	JukeboxTokenizer.from_pretrained("openai/jukebox-5b-lyrics"      )
											__lowerCAmelCase           =	tokenizer(**self.metas      )["input_ids"]
											# fmt: off
											__lowerCAmelCase           =	[
											    torch.tensor([[
											        0, 0, 0, 1_0_6_9, 1_1, -1, -1, -1, -1, 9, 7_7, 3_9,
											        3_1, 4_6, 7_7, 2_7, 7_7, 4_6, 4_4, 2_7, 4_8, 3_1, 3_8, 3_8,
											        3_1, 4_4, 7_7, 3_2, 4_4, 4_1, 3_9, 7_7, 2_7, 4_0, 7_7, 2_7,
											        4_0, 4_6, 3_5, 4_3, 4_7, 3_1, 7_7, 3_8, 2_7, 4_0, 3_0, 6_4,
											        7_9, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 2_3, 3_4, 4_1,
											        7_7, 4_5, 2_7, 3_5, 3_0, 7_7, 7_2, 2_0, 4_9, 4_1, 7_7, 4_8,
											        2_7, 4_5, 4_6, 7_7, 2_7, 4_0, 3_0, 7_7, 4_6, 4_4, 4_7, 4_0,
											        3_7, 3_8, 3_1, 4_5, 4_5, 7_7, 3_8, 3_1, 3_3, 4_5, 7_7, 4_1,
											        3_2, 7_7, 4_5, 4_6, 4_1, 4_0, 3_1, 7_9, 7_7, 7_7, 7_7, 7_7,
											        7_7, 7_7, 7_7, 7_7, 1_9, 4_6, 2_7, 4_0, 3_0, 7_7, 3_5, 4_0,
											        7_7, 4_6, 3_4, 3_1, 7_7, 3_0, 3_1, 4_5, 3_1, 4_4, 4_6, 6_3,
											        7_7, 6_3, 7_7, 6_3, 7_7, 6_3, 7_7, 1_4, 3_1, 2_7, 4_4, 7_7,
											        4_6, 3_4, 3_1, 3_9, 6_4, 7_7, 4_1, 4_0, 7_7, 4_6, 3_4, 3_1,
											        7_7, 4_5, 2_7, 4_0, 3_0, 6_4, 7_9, 7_7, 7_7, 7_7, 7_7, 7_7,
											        7_7, 7_7, 7_7, 8, 2_7, 3_8, 3_2, 7_7, 4_5, 4_7, 4_0, 3_7,
											        7_7, 2_7, 7_7, 4_5, 3_4, 2_7, 4_6, 4_6, 3_1, 4_4, 3_1, 3_0,
											        7_7, 4_8, 3_5, 4_5, 2_7, 3_3, 3_1, 7_7, 3_8, 3_5, 3_1, 4_5,
											        6_4, 7_7, 4_9, 3_4, 4_1, 4_5, 3_1, 7_7, 3_2, 4_4, 4_1, 4_9,
											        4_0, 6_4, 7_9, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 1,
											        4_0, 3_0, 7_7, 4_9, 4_4, 3_5, 4_0, 3_7, 3_8, 3_1, 3_0, 7_7,
											        3_8, 3_5, 4_2, 6_4, 7_7, 2_7, 4_0, 3_0, 7_7, 4_5, 4_0, 3_1,
											        3_1, 4_4, 7_7, 4_1, 3_2, 7_7, 2_9, 4_1, 3_8, 3_0, 7_7, 2_9,
											        4_1, 3_9, 3_9, 2_7, 4_0, 3_0, 6_4, 7_9, 7_7, 7_7, 7_7, 7_7,
											        7_7, 7_7, 7_7, 7_7, 2_0, 3_1, 3_8, 3_8, 7_7, 4_6, 3_4, 2_7,
											        4_6, 7_7, 3_5, 4_6, 4_5, 7_7, 4_5, 2_9, 4_7, 3_8, 4_2, 4_6,
											        4_1, 4_4, 7_7, 4_9, 3_1, 3_8, 3_8, 7_7, 4_6, 3_4, 4_1, 4_5,
											        3_1, 7_7, 4_2, 2_7, 4_5, 4_5, 3_5, 4_1, 4_0, 4_5, 7_7, 4_4,
											        3_1, 2_7, 3_0, 7_9, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7,
											        2_3, 3_4, 3_5, 2_9, 3_4, 7_7, 5_1, 3_1, 4_6, 7_7, 4_5, 4_7,
											        4_4, 4_8, 3_5, 4_8, 3_1, 6_4, 7_7, 4_5, 4_6, 2_7, 3_9, 4_2,
											        3_1, 3_0, 7_7, 4_1, 4_0, 7_7, 4_6, 3_4, 3_1, 4_5, 3_1, 7_7,
											        3_8, 3_5, 3_2, 3_1, 3_8, 3_1, 4_5, 4_5, 7_7, 4_6, 3_4, 3_5,
											        4_0, 3_3, 4_5, 6_4, 7_9, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7,
											        7_7, 2_0, 3_4, 3_1, 7_7, 3_4, 2_7, 4_0, 3_0, 7_7, 4_6, 3_4,
											        2_7, 4_6, 7_7, 3_9, 4_1, 2_9, 3_7, 3_1, 3_0, 7_7, 4_6, 3_4,
											        3_1, 3_9, 6_4, 7_7, 2_7, 4_0, 3_0, 7_7, 4_6, 3_4, 3_1, 7_7,
											        3_4, 3_1, 2_7, 4_4, 4_6, 7_7, 4_6, 3_4, 2_7, 4_6, 7_7, 3_2,
											        3_1, 3_0, 6_6, 7_9, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7,
											        1, 4_0, 3_0, 7_7, 4_1, 4_0, 7_7, 4_6, 3_4, 3_1, 7_7, 4_2,
											        3_1, 3_0, 3_1, 4_5, 4_6, 2_7, 3_8, 6_4, 7_7, 4_6, 3_4, 3_1,
											        4_5, 3_1, 7_7, 4_9, 4_1, 4_4, 3_0, 4_5, 7_7, 2_7, 4_2, 4_2,
											        3_1, 2_7, 4_4, 6_5, 7_9, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7,
											        7_7, 1_3, 5_1, 7_7, 4_0, 2_7, 3_9, 3_1, 7_7, 3_5, 4_5, 7_7,
											        1_5, 5_2, 5_1, 3_9, 2_7, 4_0, 3_0, 3_5, 2_7, 4_5, 6_4, 7_7,
											        1_1, 3_5, 4_0, 3_3, 7_7, 4_1, 3_2, 7_7, 1_1, 3_5, 4_0, 3_3,
											        4_5, 6_6, 7_9, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 1_2,
											        4_1, 4_1, 3_7, 7_7, 4_1, 4_0, 7_7, 3_9, 5_1, 7_7, 2_3, 4_1,
											        4_4, 3_7, 4_5, 6_4, 7_7, 5_1, 3_1, 7_7, 1_3, 3_5, 3_3, 3_4,
											        4_6, 5_1, 6_4, 7_7, 2_7, 4_0, 3_0, 7_7, 3_0, 3_1, 4_5, 4_2,
											        2_7, 3_5, 4_4, 6_7, 7_9, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7,
											        7_7, 1_4, 4_1, 4_6, 3_4, 3_5, 4_0, 3_3, 7_7, 2_8, 3_1, 4_5,
											        3_5, 3_0, 3_1, 7_7, 4_4, 3_1, 3_9, 2_7, 3_5, 4_0, 4_5, 6_3,
											        7_7, 1_8, 4_1, 4_7, 4_0, 3_0, 7_7, 4_6, 3_4, 3_1, 7_7, 3_0,
											        3_1, 2_9, 2_7, 5_1, 7_9, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7,
											        7_7, 1_5, 3_2, 7_7, 4_6, 3_4, 2_7, 4_6, 7_7, 2_9, 4_1, 3_8,
											        4_1, 4_5, 4_5, 2_7, 3_8, 7_7, 2_3, 4_4, 3_1, 2_9, 3_7, 6_4,
											        7_7, 2_8, 4_1, 4_7, 4_0, 3_0, 3_8, 3_1, 4_5, 4_5, 7_7, 2_7,
											        4_0, 3_0, 7_7, 2_8, 2_7, 4_4, 3_1, 7_9, 7_7, 7_7, 7_7, 7_7,
											        7_7, 7_7, 7_7, 7_7, 2_0, 3_4, 3_1, 7_7, 3_8, 4_1, 4_0, 3_1,
											        7_7, 2_7, 4_0, 3_0, 7_7, 3_8, 3_1, 4_8, 3_1, 3_8, 7_7, 4_5,
											        2_7, 4_0, 3_0, 4_5, 7_7, 4_5, 4_6, 4_4, 3_1, 4_6, 2_9, 3_4,
											        7_7, 3_2, 2_7, 4_4, 7_7, 2_7, 4_9, 2_7, 5_1, 7_9, 7_7, 7_7,
											        7_7, 7_7, 7_7, 7_7, 7_7, 7_7]]      ),
											    torch.tensor([[0, 0, 0, 1_0_6_9, 1_1, -1, -1, -1, -1]]      ),
											    torch.tensor([[0, 0, 0, 1_0_6_9, 1_1, -1, -1, -1, -1]]      ),
											]
											# fmt: on
											self.assertTrue(torch.allclose(tokens[0]		,      EXPECTED_OUTPUT[0]      )      )
											self.assertTrue(torch.allclose(tokens[1]		,      EXPECTED_OUTPUT[1]      )      )
											self.assertTrue(torch.allclose(tokens[2]		,      EXPECTED_OUTPUT[2]      )      )










from typing import Any, Dict, Optional

import torch
import torch.nn.functional as F
from torch import nn

from ..utils import maybe_allow_in_graph
from .activations import get_activation
from .attention_processor import Attention
from .embeddings import CombinedTimestepLabelEmbeddings
@maybe_allow_in_graph
class       a__   ( nn.Module				):






						def __init__(       self		,      _A		,      _A		,      _A		,      _A=0.0		,      _A = None		,      _A = "geglu"		,      _A = None		,      _A = False		,      _A = False		,      _A = False		,      _A = False		,      _A = True		,      _A = "layer_norm"		,      _A = False		,      ):




											"""simple docstring"""



											super().__init__()
											__lowerCAmelCase           =	only_cross_attention

											__lowerCAmelCase           =	(num_embeds_ada_norm is not None) and norm_type == "ada_norm_zero"
											__lowerCAmelCase           =	(num_embeds_ada_norm is not None) and norm_type == "ada_norm"

											if norm_type in ("ada_norm", "ada_norm_zero") and num_embeds_ada_norm is None:
																raise ValueError(
																    f"""`norm_type` is set to {norm_type}, but `num_embeds_ada_norm` is not defined. Please make sure to"""
																    f""" define `num_embeds_ada_norm` if setting `norm_type` to {norm_type}."""      )

											# Define 3 blocks. Each block has its own normalization layer.
											# 1. Self-Attn
											if self.use_ada_layer_norm:
																__lowerCAmelCase           =	AdaLayerNorm(_A		,      _A      )
											elif self.use_ada_layer_norm_zero:
																__lowerCAmelCase           =	AdaLayerNormZero(_A		,      _A      )
											else:
																__lowerCAmelCase           =	nn.LayerNorm(_A		,      elementwise_affine=_A      )
											__lowerCAmelCase           =	Attention(
											    query_dim=_A		,      heads=_A		,      dim_head=_A		,      dropout=_A		,      bias=_A		,      cross_attention_dim=cross_attention_dim if only_cross_attention else None		,      upcast_attention=_A		,      )

											# 2. Cross-Attn
											if cross_attention_dim is not None or double_self_attention:
																# We currently only use AdaLayerNormZero for self attention where there will only be one attention block.
																# I.e. the number of returned modulation chunks from AdaLayerZero would not make sense if returned during
																# the second cross attention block.
																__lowerCAmelCase           =	(
																    AdaLayerNorm(_A		,      _A      )
																    if self.use_ada_layer_norm
																    else nn.LayerNorm(_A		,      elementwise_affine=_A      )
																)
																__lowerCAmelCase           =	Attention(
																    query_dim=_A		,      cross_attention_dim=cross_attention_dim if not double_self_attention else None		,      heads=_A		,      dim_head=_A		,      dropout=_A		,      bias=_A		,      upcast_attention=_A		,      )  # is self-attn if encoder_hidden_states is none
											else:
																__lowerCAmelCase           =	None
																__lowerCAmelCase           =	None

											# 3. Feed-forward
											__lowerCAmelCase           =	nn.LayerNorm(_A		,      elementwise_affine=_A      )
											__lowerCAmelCase           =	FeedForward(_A		,      dropout=_A		,      activation_fn=_A		,      final_dropout=_A      )

											# let chunk size default to None
											__lowerCAmelCase           =	None
											__lowerCAmelCase           =	0






						def     __SCREAMING_SNAKE_CASE(       self		,      _A		,      _A      ):




											"""simple docstring"""



											__lowerCAmelCase           =	chunk_size
											__lowerCAmelCase           =	dim






						def     __SCREAMING_SNAKE_CASE(       self		,      _A		,      _A = None		,      _A = None		,      _A = None		,      _A = None		,      _A = None		,      _A = None		,      ):




											"""simple docstring"""



											if self.use_ada_layer_norm:
																__lowerCAmelCase           =	self.norma(_A		,      _A      )
											elif self.use_ada_layer_norm_zero:
																__lowerCAmelCase       ,							__lowerCAmelCase       ,							__lowerCAmelCase       ,							__lowerCAmelCase       ,							__lowerCAmelCase           =	self.norma(
																    _A		,      _A		,      _A		,      hidden_dtype=hidden_states.dtype      )
											else:
																__lowerCAmelCase           =	self.norma(_A      )

											__lowerCAmelCase           =	cross_attention_kwargs if cross_attention_kwargs is not None else {}

											__lowerCAmelCase           =	self.attna(
											    _A		,      encoder_hidden_states=encoder_hidden_states if self.only_cross_attention else None		,      attention_mask=_A		,      **_A		,      )
											if self.use_ada_layer_norm_zero:
																__lowerCAmelCase           =	gate_msa.unsqueeze(1      ) * attn_output
											__lowerCAmelCase           =	attn_output + hidden_states

											# 2. Cross-Attention
											if self.attna is not None:
																__lowerCAmelCase           =	(
																    self.norma(_A		,      _A      ) if self.use_ada_layer_norm else self.norma(_A      )
																)

																__lowerCAmelCase           =	self.attna(
																    _A		,      encoder_hidden_states=_A		,      attention_mask=_A		,      **_A		,      )
																__lowerCAmelCase           =	attn_output + hidden_states

											# 3. Feed-forward
											__lowerCAmelCase           =	self.norma(_A      )

											if self.use_ada_layer_norm_zero:
																__lowerCAmelCase           =	norm_hidden_states * (1 + scale_mlp[:, None]) + shift_mlp[:, None]

											if self._chunk_size is not None:
																# "feed_forward_chunk_size" can be used to save memory
																if norm_hidden_states.shape[self._chunk_dim] % self._chunk_size != 0:
																					raise ValueError(
																					    f"""`hidden_states` dimension to be chunked: {norm_hidden_states.shape[self._chunk_dim]} has to be divisible by chunk size: {self._chunk_size}. Make sure to set an appropriate `chunk_size` when calling `unet.enable_forward_chunking`."""      )

																__lowerCAmelCase           =	norm_hidden_states.shape[self._chunk_dim] // self._chunk_size
																__lowerCAmelCase           =	torch.cat(
																    [self.ff(_A      ) for hid_slice in norm_hidden_states.chunk(_A		,      dim=self._chunk_dim      )]		,      dim=self._chunk_dim		,      )
											else:
																__lowerCAmelCase           =	self.ff(_A      )

											if self.use_ada_layer_norm_zero:
																__lowerCAmelCase           =	gate_mlp.unsqueeze(1      ) * ff_output

											__lowerCAmelCase           =	ff_output + hidden_states

											return hidden_states






class       a__   ( nn.Module				):






						def __init__(       self		,      _A		,      _A = None		,      _A = 4		,      _A = 0.0		,      _A = "geglu"		,      _A = False		,      ):




											"""simple docstring"""



											super().__init__()
											__lowerCAmelCase           =	int(dim * mult      )
											__lowerCAmelCase           =	dim_out if dim_out is not None else dim

											if activation_fn == "gelu":
																__lowerCAmelCase           =	GELU(_A		,      _A      )
											if activation_fn == "gelu-approximate":
																__lowerCAmelCase           =	GELU(_A		,      _A		,      approximate="tanh"      )
											elif activation_fn == "geglu":
																__lowerCAmelCase           =	GEGLU(_A		,      _A      )
											elif activation_fn == "geglu-approximate":
																__lowerCAmelCase           =	ApproximateGELU(_A		,      _A      )

											__lowerCAmelCase           =	nn.ModuleList([]      )
											# project in
											self.net.append(_A      )
											# project dropout
											self.net.append(nn.Dropout(_A      )      )
											# project out
											self.net.append(nn.Linear(_A		,      _A      )      )
											# FF as used in Vision Transformer, MLP-Mixer, etc. have a final dropout
											if final_dropout:
																self.net.append(nn.Dropout(_A      )      )






						def     __SCREAMING_SNAKE_CASE(       self		,      _A      ):




											"""simple docstring"""



											for module in self.net:
																__lowerCAmelCase           =	module(_A      )
											return hidden_states






class       a__   ( nn.Module				):






						def __init__(       self		,      _A		,      _A		,      _A = "none"      ):




											"""simple docstring"""



											super().__init__()
											__lowerCAmelCase           =	nn.Linear(_A		,      _A      )
											__lowerCAmelCase           =	approximate






						def     __SCREAMING_SNAKE_CASE(       self		,      _A      ):




											"""simple docstring"""



											if gate.device.type != "mps":
																return F.gelu(_A		,      approximate=self.approximate      )
											# mps: gelu is not implemented for float16
											return F.gelu(gate.to(dtype=torch.floataa      )		,      approximate=self.approximate      ).to(dtype=gate.dtype      )






						def     __SCREAMING_SNAKE_CASE(       self		,      _A      ):




											"""simple docstring"""



											__lowerCAmelCase           =	self.proj(_A      )
											__lowerCAmelCase           =	self.gelu(_A      )
											return hidden_states






class       a__   ( nn.Module				):






						def __init__(       self		,      _A		,      _A      ):




											"""simple docstring"""



											super().__init__()
											__lowerCAmelCase           =	nn.Linear(_A		,      dim_out * 2      )






						def     __SCREAMING_SNAKE_CASE(       self		,      _A      ):




											"""simple docstring"""



											if gate.device.type != "mps":
																return F.gelu(_A      )
											# mps: gelu is not implemented for float16
											return F.gelu(gate.to(dtype=torch.floataa      )      ).to(dtype=gate.dtype      )






						def     __SCREAMING_SNAKE_CASE(       self		,      _A      ):




											"""simple docstring"""



											__lowerCAmelCase       ,							__lowerCAmelCase           =	self.proj(_A      ).chunk(2		,      dim=-1      )
											return hidden_states * self.gelu(_A      )






class       a__   ( nn.Module				):






						def __init__(       self		,      _A		,      _A      ):




											"""simple docstring"""



											super().__init__()
											__lowerCAmelCase           =	nn.Linear(_A		,      _A      )






						def     __SCREAMING_SNAKE_CASE(       self		,      _A      ):




											"""simple docstring"""



											__lowerCAmelCase           =	self.proj(_A      )
											return x * torch.sigmoid(1.7_02 * x      )






class       a__   ( nn.Module				):






						def __init__(       self		,      _A		,      _A      ):




											"""simple docstring"""



											super().__init__()
											__lowerCAmelCase           =	nn.Embedding(_A		,      _A      )
											__lowerCAmelCase           =	nn.SiLU()
											__lowerCAmelCase           =	nn.Linear(_A		,      embedding_dim * 2      )
											__lowerCAmelCase           =	nn.LayerNorm(_A		,      elementwise_affine=_A      )






						def     __SCREAMING_SNAKE_CASE(       self		,      _A		,      _A      ):




											"""simple docstring"""



											__lowerCAmelCase           =	self.linear(self.silu(self.emb(_A      )      )      )
											__lowerCAmelCase       ,							__lowerCAmelCase           =	torch.chunk(_A		,      2      )
											__lowerCAmelCase           =	self.norm(_A      ) * (1 + scale) + shift
											return x






class       a__   ( nn.Module				):






						def __init__(       self		,      _A		,      _A      ):




											"""simple docstring"""



											super().__init__()

											__lowerCAmelCase           =	CombinedTimestepLabelEmbeddings(_A		,      _A      )

											__lowerCAmelCase           =	nn.SiLU()
											__lowerCAmelCase           =	nn.Linear(_A		,      6 * embedding_dim		,      bias=_A      )
											__lowerCAmelCase           =	nn.LayerNorm(_A		,      elementwise_affine=_A		,      eps=1E-6      )






						def     __SCREAMING_SNAKE_CASE(       self		,      _A		,      _A		,      _A		,      _A=None      ):




											"""simple docstring"""



											__lowerCAmelCase           =	self.linear(self.silu(self.emb(_A		,      _A		,      hidden_dtype=_A      )      )      )
											__lowerCAmelCase       ,							__lowerCAmelCase       ,							__lowerCAmelCase       ,							__lowerCAmelCase       ,							__lowerCAmelCase       ,							__lowerCAmelCase           =	emb.chunk(6		,      dim=1      )
											__lowerCAmelCase           =	self.norm(_A      ) * (1 + scale_msa[:, None]) + shift_msa[:, None]
											return x, gate_msa, shift_mlp, scale_mlp, gate_mlp






class       a__   ( nn.Module				):






						def __init__(       self		,      _A		,      _A		,      _A		,      _A = None		,      _A = 1E-5      ):




											"""simple docstring"""



											super().__init__()
											__lowerCAmelCase           =	num_groups
											__lowerCAmelCase           =	eps

											if act_fn is None:
																__lowerCAmelCase           =	None
											else:
																__lowerCAmelCase           =	get_activation(_A      )

											__lowerCAmelCase           =	nn.Linear(_A		,      out_dim * 2      )






						def     __SCREAMING_SNAKE_CASE(       self		,      _A		,      _A      ):




											"""simple docstring"""



											if self.act:
																__lowerCAmelCase           =	self.act(_A      )
											__lowerCAmelCase           =	self.linear(_A      )
											__lowerCAmelCase           =	emb[:, :, None, None]
											__lowerCAmelCase       ,							__lowerCAmelCase           =	emb.chunk(2		,      dim=1      )

											__lowerCAmelCase           =	F.group_norm(_A		,      self.num_groups		,      eps=self.eps      )
											__lowerCAmelCase           =	x * (1 + scale) + shift
											return x




