

'''simple docstring'''







import itertools
import json
import os
import unittest

from transformers import AddedToken, LongformerTokenizer, LongformerTokenizerFast
from transformers.models.longformer.tokenization_longformer import VOCAB_FILES_NAMES
from transformers.testing_utils import require_tokenizers, slow

from ...test_tokenization_common import TokenizerTesterMixin


@require_tokenizers
class 					A__      (      _snake_case						,						unittest.TestCase			):
    lowercase						=		LongformerTokenizer
    lowercase						=		True
    lowercase						=		LongformerTokenizerFast
    lowercase						=		True



    def 					snake_case_			(					self       )						->      int:





           '''simple docstring'''
           super().setUp()

           # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt
           A_						=      [
               """l""",
               """o""",
               """w""",
               """e""",
               """r""",
               """s""",
               """t""",
               """i""",
               """d""",
               """n""",
               """\u0120""",
               """\u0120l""",
               """\u0120n""",
               """\u0120lo""",
               """\u0120low""",
               """er""",
               """\u0120lowest""",
               """\u0120newer""",
               """\u0120wider""",
               """<unk>""",
           ]
           A_						=      dict(zip(UpperCamelCase__		,				range(len(UpperCamelCase__       )       )       )       )
           A_						=      ["""#version: 0.2""", """\u0120 l""", """\u0120l o""", """\u0120lo w""", """e r""", """"""]
           A_						=      {"""unk_token""": """<unk>"""}

           A_						=      os.path.join(self.tmpdirname		,				VOCAB_FILES_NAMES["""vocab_file"""]       )
           A_						=      os.path.join(self.tmpdirname		,				VOCAB_FILES_NAMES["""merges_file"""]       )
           with open(self.vocab_file		,				"""w"""		,				encoding="""utf-8"""       ) as fp:
                  fp.write(json.dumps(UpperCamelCase__       ) + """\n"""       )
           with open(self.merges_file		,				"""w"""		,				encoding="""utf-8"""       ) as fp:
                  fp.write("""\n""".join(UpperCamelCase__       )       )



    def 					snake_case_			(					self		,				**UpperCamelCase__       )						->      Optional[int]:





           '''simple docstring'''
           kwargs.update(self.special_tokens_map       )
           return self.tokenizer_class.from_pretrained(self.tmpdirname		,				**UpperCamelCase__       )



    def 					snake_case_			(					self		,				**UpperCamelCase__       )						->      Any:





           '''simple docstring'''
           kwargs.update(self.special_tokens_map       )
           return self.rust_tokenizer_class.from_pretrained(self.tmpdirname		,				**UpperCamelCase__       )



    def 					snake_case_			(					self		,				UpperCamelCase__       )						->      Any:





           '''simple docstring'''
           A_						=      """lower newer"""
           A_						=      """lower newer"""
           return input_text, output_text



    def 					snake_case_			(					self       )						->      Union[str, Any]:





           '''simple docstring'''
           A_						=      self.tokenizer_class(self.vocab_file		,				self.merges_file		,				**self.special_tokens_map       )
           A_						=      """lower newer"""
           A_						=      ["""l""", """o""", """w""", """er""", """\u0120""", """n""", """e""", """w""", """er"""]
           A_						=      tokenizer.tokenize(UpperCamelCase__       )  # , add_prefix_space=True)
           self.assertListEqual(UpperCamelCase__		,				UpperCamelCase__       )

           A_						=      tokens + [tokenizer.unk_token]
           A_						=      [0, 1, 2, 15, 10, 9, 3, 2, 15, 19]
           self.assertListEqual(tokenizer.convert_tokens_to_ids(UpperCamelCase__       )		,				UpperCamelCase__       )



    def 					snake_case_			(					self       )						->      Optional[Any]:





           '''simple docstring'''
           A_						=      self.get_tokenizer()

           self.assertListEqual(tokenizer.encode("""Hello world!"""		,				add_special_tokens=UpperCamelCase__       )		,				[0, 31414, 232, 328, 2]       )
           self.assertListEqual(
               tokenizer.encode("""Hello world! cécé herlolip 418"""		,				add_special_tokens=UpperCamelCase__       )		,				[0, 31414, 232, 328, 740, 1140, 12695, 69, 46078, 1588, 2]		,				)



    @slow
    def 					snake_case_			(					self       )						->      Dict:





           '''simple docstring'''
           A_						=      self.tokenizer_class.from_pretrained("""allenai/longformer-base-4096"""       )

           A_						=      tokenizer.encode("""sequence builders"""		,				add_special_tokens=UpperCamelCase__       )
           A_						=      tokenizer.encode("""multi-sequence build"""		,				add_special_tokens=UpperCamelCase__       )

           A_						=      tokenizer.encode(
               """sequence builders"""		,				add_special_tokens=UpperCamelCase__		,				add_prefix_space=UpperCamelCase__       )
           A_						=      tokenizer.encode(
               """sequence builders"""		,				"""multi-sequence build"""		,				add_special_tokens=UpperCamelCase__		,				add_prefix_space=UpperCamelCase__       )

           A_						=      tokenizer.build_inputs_with_special_tokens(UpperCamelCase__       )
           A_						=      tokenizer.build_inputs_with_special_tokens(UpperCamelCase__		,				UpperCamelCase__       )

           assert encoded_sentence == encoded_text_from_decode
           assert encoded_pair == encoded_pair_from_decode



    def 					snake_case_			(					self       )						->      int:





           '''simple docstring'''
           A_						=      self.get_tokenizer()

           A_						=      """Encode this sequence."""
           A_						=      tokenizer.byte_encoder[""" """.encode("""utf-8"""       )[0]]

           # Testing encoder arguments
           A_						=      tokenizer.encode(UpperCamelCase__		,				add_special_tokens=UpperCamelCase__		,				add_prefix_space=UpperCamelCase__       )
           A_						=      tokenizer.convert_ids_to_tokens(encoded[0]       )[0]
           self.assertNotEqual(UpperCamelCase__		,				UpperCamelCase__       )

           A_						=      tokenizer.encode(UpperCamelCase__		,				add_special_tokens=UpperCamelCase__		,				add_prefix_space=UpperCamelCase__       )
           A_						=      tokenizer.convert_ids_to_tokens(encoded[0]       )[0]
           self.assertEqual(UpperCamelCase__		,				UpperCamelCase__       )

           tokenizer.add_special_tokens({"""bos_token""": """<s>"""}       )
           A_						=      tokenizer.encode(UpperCamelCase__		,				add_special_tokens=UpperCamelCase__       )
           A_						=      tokenizer.convert_ids_to_tokens(encoded[1]       )[0]
           self.assertNotEqual(UpperCamelCase__		,				UpperCamelCase__       )

           # Testing spaces after special tokens
           A_						=      """<mask>"""
           tokenizer.add_special_tokens(
               {"""mask_token""": AddedToken(UpperCamelCase__		,				lstrip=UpperCamelCase__		,				rstrip=UpperCamelCase__       )}       )  # mask token has a left space
           A_						=      tokenizer.convert_tokens_to_ids(UpperCamelCase__       )

           A_						=      """Encode <mask> sequence"""
           A_						=      """Encode <mask>sequence"""

           A_						=      tokenizer.encode(UpperCamelCase__       )
           A_						=      encoded.index(UpperCamelCase__       )
           A_						=      tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1]       )[0]
           self.assertEqual(UpperCamelCase__		,				UpperCamelCase__       )

           A_						=      tokenizer.encode(UpperCamelCase__       )
           A_						=      encoded.index(UpperCamelCase__       )
           A_						=      tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1]       )[0]
           self.assertNotEqual(UpperCamelCase__		,				UpperCamelCase__       )



    def 					snake_case_			(					self       )						->      List[Any]:





           '''simple docstring'''
           pass



    def 					snake_case_			(					self       )						->      Optional[int]:





           '''simple docstring'''
           for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
                  with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})'''       ):
                         A_						=      self.rust_tokenizer_class.from_pretrained(UpperCamelCase__		,				**UpperCamelCase__       )
                         A_						=      self.tokenizer_class.from_pretrained(UpperCamelCase__		,				**UpperCamelCase__       )
                         A_						=      """A, <mask> AllenNLP sentence."""
                         A_						=      tokenizer_r.encode_plus(UpperCamelCase__		,				add_special_tokens=UpperCamelCase__		,				return_token_type_ids=UpperCamelCase__       )
                         A_						=      tokenizer_p.encode_plus(UpperCamelCase__		,				add_special_tokens=UpperCamelCase__		,				return_token_type_ids=UpperCamelCase__       )

                         # token_type_ids should put 0 everywhere
                         self.assertEqual(sum(tokens_r["""token_type_ids"""]       )		,				sum(tokens_p["""token_type_ids"""]       )       )

                         # attention_mask should put 1 everywhere, so sum over length should be 1
                         self.assertEqual(
                             sum(tokens_r["""attention_mask"""]       ) / len(tokens_r["""attention_mask"""]       )		,				sum(tokens_p["""attention_mask"""]       ) / len(tokens_p["""attention_mask"""]       )		,				)

                         A_						=      tokenizer_r.convert_ids_to_tokens(tokens_r["""input_ids"""]       )
                         A_						=      tokenizer_p.convert_ids_to_tokens(tokens_p["""input_ids"""]       )

                         # Rust correctly handles the space before the mask while python doesnt
                         self.assertSequenceEqual(tokens_p["""input_ids"""]		,				[0, 250, 6, 50264, 3823, 487, 21992, 3645, 4, 2]       )
                         self.assertSequenceEqual(tokens_r["""input_ids"""]		,				[0, 250, 6, 50264, 3823, 487, 21992, 3645, 4, 2]       )

                         self.assertSequenceEqual(
                             UpperCamelCase__		,				["""<s>""", """A""", """,""", """<mask>""", """ĠAllen""", """N""", """LP""", """Ġsentence""", """.""", """</s>"""]       )
                         self.assertSequenceEqual(
                             UpperCamelCase__		,				["""<s>""", """A""", """,""", """<mask>""", """ĠAllen""", """N""", """LP""", """Ġsentence""", """.""", """</s>"""]       )



    def 					snake_case_			(					self       )						->      List[Any]:





           '''simple docstring'''
           for trim_offsets, add_prefix_space in itertools.product([True, False]		,				repeat=2       ):
                  A_						=      self.rust_tokenizer_class.from_pretrained(
                      self.tmpdirname		,				use_fast=UpperCamelCase__		,				add_prefix_space=UpperCamelCase__		,				trim_offsets=UpperCamelCase__       )

                  A_						=      json.loads(tokenizer_r.backend_tokenizer.pre_tokenizer.__getstate__()       )
                  A_						=      json.loads(tokenizer_r.backend_tokenizer.post_processor.__getstate__()       )

                  self.assertEqual(pre_tokenizer_state["""add_prefix_space"""]		,				UpperCamelCase__       )

                  self.assertEqual(post_processor_state["""add_prefix_space"""]		,				UpperCamelCase__       )
                  self.assertEqual(post_processor_state["""trim_offsets"""]		,				UpperCamelCase__       )







    def 					snake_case_			(					self       )						->      List[Any]:





           '''simple docstring'''
           # Test which aims to verify that the offsets are well adapted to the argument `add_prefix_space` and
           # `trim_offsets`
           for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
                  with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})'''       ):
                         A_						=      """hello"""  # `hello` is a token in the vocabulary of `pretrained_name`
                         A_						=      f'''{text_of_1_token} {text_of_1_token}'''

                         A_						=      self.rust_tokenizer_class.from_pretrained(
                             UpperCamelCase__		,				use_fast=UpperCamelCase__		,				add_prefix_space=UpperCamelCase__		,				trim_offsets=UpperCamelCase__       )
                         A_						=      tokenizer_r(UpperCamelCase__		,				return_offsets_mapping=UpperCamelCase__		,				add_special_tokens=UpperCamelCase__       )
                         self.assertEqual(encoding.offset_mapping[0]		,				(0, len(UpperCamelCase__       ))       )
                         self.assertEqual(
                             encoding.offset_mapping[1]		,				(len(UpperCamelCase__       ) + 1, len(UpperCamelCase__       ) + 1 + len(UpperCamelCase__       ))		,				)

                         A_						=      self.rust_tokenizer_class.from_pretrained(
                             UpperCamelCase__		,				use_fast=UpperCamelCase__		,				add_prefix_space=UpperCamelCase__		,				trim_offsets=UpperCamelCase__       )
                         A_						=      tokenizer_r(UpperCamelCase__		,				return_offsets_mapping=UpperCamelCase__		,				add_special_tokens=UpperCamelCase__       )
                         self.assertEqual(encoding.offset_mapping[0]		,				(0, len(UpperCamelCase__       ))       )
                         self.assertEqual(
                             encoding.offset_mapping[1]		,				(len(UpperCamelCase__       ) + 1, len(UpperCamelCase__       ) + 1 + len(UpperCamelCase__       ))		,				)

                         A_						=      self.rust_tokenizer_class.from_pretrained(
                             UpperCamelCase__		,				use_fast=UpperCamelCase__		,				add_prefix_space=UpperCamelCase__		,				trim_offsets=UpperCamelCase__       )
                         A_						=      tokenizer_r(UpperCamelCase__		,				return_offsets_mapping=UpperCamelCase__		,				add_special_tokens=UpperCamelCase__       )
                         self.assertEqual(encoding.offset_mapping[0]		,				(0, len(UpperCamelCase__       ))       )
                         self.assertEqual(
                             encoding.offset_mapping[1]		,				(len(UpperCamelCase__       ), len(UpperCamelCase__       ) + 1 + len(UpperCamelCase__       ))		,				)

                         A_						=      self.rust_tokenizer_class.from_pretrained(
                             UpperCamelCase__		,				use_fast=UpperCamelCase__		,				add_prefix_space=UpperCamelCase__		,				trim_offsets=UpperCamelCase__       )
                         A_						=      tokenizer_r(UpperCamelCase__		,				return_offsets_mapping=UpperCamelCase__		,				add_special_tokens=UpperCamelCase__       )
                         self.assertEqual(encoding.offset_mapping[0]		,				(0, len(UpperCamelCase__       ))       )
                         self.assertEqual(
                             encoding.offset_mapping[1]		,				(len(UpperCamelCase__       ), len(UpperCamelCase__       ) + 1 + len(UpperCamelCase__       ))		,				)

                         A_						=      f''' {text}'''

                         # tokenizer_r = self.rust_tokenizer_class.from_pretrained(
                         #     pretrained_name, use_fast=True, add_prefix_space=True, trim_offsets=True
                         # )
                         # encoding = tokenizer_r(text, return_offsets_mapping=True, add_special_tokens=False)
                         # self.assertEqual(encoding.offset_mapping[0], (1, 1 + len(text_of_1_token)))
                         # self.assertEqual(
                         #     encoding.offset_mapping[1],
                         #     (1 + len(text_of_1_token) + 1, 1 + len(text_of_1_token) + 1 + len(text_of_1_token)),
                         # )

                         A_						=      self.rust_tokenizer_class.from_pretrained(
                             UpperCamelCase__		,				use_fast=UpperCamelCase__		,				add_prefix_space=UpperCamelCase__		,				trim_offsets=UpperCamelCase__       )
                         A_						=      tokenizer_r(UpperCamelCase__		,				return_offsets_mapping=UpperCamelCase__		,				add_special_tokens=UpperCamelCase__       )
                         self.assertEqual(encoding.offset_mapping[0]		,				(1, 1 + len(UpperCamelCase__       ))       )
                         self.assertEqual(
                             encoding.offset_mapping[1]		,				(1 + len(UpperCamelCase__       ) + 1, 1 + len(UpperCamelCase__       ) + 1 + len(UpperCamelCase__       ))		,				)

                         A_						=      self.rust_tokenizer_class.from_pretrained(
                             UpperCamelCase__		,				use_fast=UpperCamelCase__		,				add_prefix_space=UpperCamelCase__		,				trim_offsets=UpperCamelCase__       )
                         A_						=      tokenizer_r(UpperCamelCase__		,				return_offsets_mapping=UpperCamelCase__		,				add_special_tokens=UpperCamelCase__       )
                         self.assertEqual(encoding.offset_mapping[0]		,				(0, 1 + len(UpperCamelCase__       ))       )
                         self.assertEqual(
                             encoding.offset_mapping[1]		,				(1 + len(UpperCamelCase__       ), 1 + len(UpperCamelCase__       ) + 1 + len(UpperCamelCase__       ))		,				)

                         A_						=      self.rust_tokenizer_class.from_pretrained(
                             UpperCamelCase__		,				use_fast=UpperCamelCase__		,				add_prefix_space=UpperCamelCase__		,				trim_offsets=UpperCamelCase__       )
                         A_						=      tokenizer_r(UpperCamelCase__		,				return_offsets_mapping=UpperCamelCase__		,				add_special_tokens=UpperCamelCase__       )
                         self.assertEqual(encoding.offset_mapping[0]		,				(0, 1 + len(UpperCamelCase__       ))       )
                         self.assertEqual(
                             encoding.offset_mapping[1]		,				(1 + len(UpperCamelCase__       ), 1 + len(UpperCamelCase__       ) + 1 + len(UpperCamelCase__       ))		,				)



'''simple docstring'''







from dataclasses import dataclass
from typing import Optional

import numpy as np
import torch
import torch.nn as nn

from ..utils import BaseOutput, is_torch_version, randn_tensor
from .attention_processor import SpatialNorm
from .unet_ad_blocks import UNetMidBlockaD, get_down_block, get_up_block


@dataclass
class 					A__      (      _snake_case			):
    lowercase						=		42


class 					A__      (      nn.Module			):



    def __init__(					self		,				UpperCamelCase__=3		,				UpperCamelCase__=3		,				UpperCamelCase__=("DownEncoderBlock2D",)		,				UpperCamelCase__=(64,)		,				UpperCamelCase__=2		,				UpperCamelCase__=32		,				UpperCamelCase__="silu"		,				UpperCamelCase__=True		,				)						->      Union[str, Any]:





           '''simple docstring'''
           super().__init__()
           A_						=      layers_per_block

           A_						=      torch.nn.Convad(
               UpperCamelCase__		,				block_out_channels[0]		,				kernel_size=3		,				stride=1		,				padding=1		,				)

           A_						=      None
           A_						=      nn.ModuleList([]       )

           # down
           A_						=      block_out_channels[0]
           for i, down_block_type in enumerate(UpperCamelCase__       ):
                  A_						=      output_channel
                  A_						=      block_out_channels[i]
                  A_						=      i == len(UpperCamelCase__       ) - 1

                  A_						=      get_down_block(
                      UpperCamelCase__		,				num_layers=self.layers_per_block		,				in_channels=UpperCamelCase__		,				out_channels=UpperCamelCase__		,				add_downsample=not is_final_block		,				resnet_eps=1e-6		,				downsample_padding=0		,				resnet_act_fn=UpperCamelCase__		,				resnet_groups=UpperCamelCase__		,				attention_head_dim=UpperCamelCase__		,				temb_channels=UpperCamelCase__		,				)
                  self.down_blocks.append(UpperCamelCase__       )

           # mid
           A_						=      UNetMidBlockaD(
               in_channels=block_out_channels[-1]		,				resnet_eps=1e-6		,				resnet_act_fn=UpperCamelCase__		,				output_scale_factor=1		,				resnet_time_scale_shift="""default"""		,				attention_head_dim=block_out_channels[-1]		,				resnet_groups=UpperCamelCase__		,				temb_channels=UpperCamelCase__		,				)

           # out
           A_						=      nn.GroupNorm(num_channels=block_out_channels[-1]		,				num_groups=UpperCamelCase__		,				eps=1e-6       )
           A_						=      nn.SiLU()

           A_						=      2 * out_channels if double_z else out_channels
           A_						=      nn.Convad(block_out_channels[-1]		,				UpperCamelCase__		,				3		,				padding=1       )

           A_						=      False







    def 					snake_case_			(					self		,				UpperCamelCase__       )						->      str:





           '''simple docstring'''
           A_						=      x
           A_						=      self.conv_in(UpperCamelCase__       )

           if self.training and self.gradient_checkpointing:

                  def create_custom_forward(UpperCamelCase__       ):
                         def custom_forward(*UpperCamelCase__       ):
                                return module(*UpperCamelCase__       )

                         return custom_forward

                  # down
                  if is_torch_version(""">="""		,				"""1.11.0"""       ):
                         for down_block in self.down_blocks:
                                A_						=      torch.utils.checkpoint.checkpoint(
                                    create_custom_forward(UpperCamelCase__       )		,				UpperCamelCase__		,				use_reentrant=UpperCamelCase__       )
                         # middle
                         A_						=      torch.utils.checkpoint.checkpoint(
                             create_custom_forward(self.mid_block       )		,				UpperCamelCase__		,				use_reentrant=UpperCamelCase__       )
                  else:
                         for down_block in self.down_blocks:
                                A_						=      torch.utils.checkpoint.checkpoint(create_custom_forward(UpperCamelCase__       )		,				UpperCamelCase__       )
                         # middle
                         A_						=      torch.utils.checkpoint.checkpoint(create_custom_forward(self.mid_block       )		,				UpperCamelCase__       )

           else:
                  # down
                  for down_block in self.down_blocks:
                         A_						=      down_block(UpperCamelCase__       )

                  # middle
                  A_						=      self.mid_block(UpperCamelCase__       )

           # post-process
           A_						=      self.conv_norm_out(UpperCamelCase__       )
           A_						=      self.conv_act(UpperCamelCase__       )
           A_						=      self.conv_out(UpperCamelCase__       )

           return sample


class 					A__      (      nn.Module			):



    def __init__(					self		,				UpperCamelCase__=3		,				UpperCamelCase__=3		,				UpperCamelCase__=("UpDecoderBlock2D",)		,				UpperCamelCase__=(64,)		,				UpperCamelCase__=2		,				UpperCamelCase__=32		,				UpperCamelCase__="silu"		,				UpperCamelCase__="group"		,				)						->      List[Any]:





           '''simple docstring'''
           super().__init__()
           A_						=      layers_per_block

           A_						=      nn.Convad(
               UpperCamelCase__		,				block_out_channels[-1]		,				kernel_size=3		,				stride=1		,				padding=1		,				)

           A_						=      None
           A_						=      nn.ModuleList([]       )

           A_						=      in_channels if norm_type == """spatial""" else None

           # mid
           A_						=      UNetMidBlockaD(
               in_channels=block_out_channels[-1]		,				resnet_eps=1e-6		,				resnet_act_fn=UpperCamelCase__		,				output_scale_factor=1		,				resnet_time_scale_shift="""default""" if norm_type == """group""" else norm_type		,				attention_head_dim=block_out_channels[-1]		,				resnet_groups=UpperCamelCase__		,				temb_channels=UpperCamelCase__		,				)

           # up
           A_						=      list(reversed(UpperCamelCase__       )       )
           A_						=      reversed_block_out_channels[0]
           for i, up_block_type in enumerate(UpperCamelCase__       ):
                  A_						=      output_channel
                  A_						=      reversed_block_out_channels[i]

                  A_						=      i == len(UpperCamelCase__       ) - 1

                  A_						=      get_up_block(
                      UpperCamelCase__		,				num_layers=self.layers_per_block + 1		,				in_channels=UpperCamelCase__		,				out_channels=UpperCamelCase__		,				prev_output_channel=UpperCamelCase__		,				add_upsample=not is_final_block		,				resnet_eps=1e-6		,				resnet_act_fn=UpperCamelCase__		,				resnet_groups=UpperCamelCase__		,				attention_head_dim=UpperCamelCase__		,				temb_channels=UpperCamelCase__		,				resnet_time_scale_shift=UpperCamelCase__		,				)
                  self.up_blocks.append(UpperCamelCase__       )
                  A_						=      output_channel

           # out
           if norm_type == "spatial":
                  A_						=      SpatialNorm(block_out_channels[0]		,				UpperCamelCase__       )
           else:
                  A_						=      nn.GroupNorm(num_channels=block_out_channels[0]		,				num_groups=UpperCamelCase__		,				eps=1e-6       )
           A_						=      nn.SiLU()
           A_						=      nn.Convad(block_out_channels[0]		,				UpperCamelCase__		,				3		,				padding=1       )

           A_						=      False







    def 					snake_case_			(					self		,				UpperCamelCase__		,				UpperCamelCase__=None       )						->      Optional[Any]:





           '''simple docstring'''
           A_						=      z
           A_						=      self.conv_in(UpperCamelCase__       )

           A_						=      next(iter(self.up_blocks.parameters()       )       ).dtype
           if self.training and self.gradient_checkpointing:

                  def create_custom_forward(UpperCamelCase__       ):
                         def custom_forward(*UpperCamelCase__       ):
                                return module(*UpperCamelCase__       )

                         return custom_forward

                  if is_torch_version(""">="""		,				"""1.11.0"""       ):
                         # middle
                         A_						=      torch.utils.checkpoint.checkpoint(
                             create_custom_forward(self.mid_block       )		,				UpperCamelCase__		,				UpperCamelCase__		,				use_reentrant=UpperCamelCase__       )
                         A_						=      sample.to(UpperCamelCase__       )

                         # up
                         for up_block in self.up_blocks:
                                A_						=      torch.utils.checkpoint.checkpoint(
                                    create_custom_forward(UpperCamelCase__       )		,				UpperCamelCase__		,				UpperCamelCase__		,				use_reentrant=UpperCamelCase__       )
                  else:
                         # middle
                         A_						=      torch.utils.checkpoint.checkpoint(
                             create_custom_forward(self.mid_block       )		,				UpperCamelCase__		,				UpperCamelCase__       )
                         A_						=      sample.to(UpperCamelCase__       )

                         # up
                         for up_block in self.up_blocks:
                                A_						=      torch.utils.checkpoint.checkpoint(create_custom_forward(UpperCamelCase__       )		,				UpperCamelCase__		,				UpperCamelCase__       )
           else:
                  # middle
                  A_						=      self.mid_block(UpperCamelCase__		,				UpperCamelCase__       )
                  A_						=      sample.to(UpperCamelCase__       )

                  # up
                  for up_block in self.up_blocks:
                         A_						=      up_block(UpperCamelCase__		,				UpperCamelCase__       )

        # post-process
           if latent_embeds is None:
                  A_						=      self.conv_norm_out(UpperCamelCase__       )
           else:
                  A_						=      self.conv_norm_out(UpperCamelCase__		,				UpperCamelCase__       )
           A_						=      self.conv_act(UpperCamelCase__       )
           A_						=      self.conv_out(UpperCamelCase__       )

           return sample


class 					A__      (      nn.Module			):



    def __init__(					self		,				UpperCamelCase__		,				UpperCamelCase__		,				UpperCamelCase__		,				UpperCamelCase__=None		,				UpperCamelCase__="random"		,				UpperCamelCase__=False		,				UpperCamelCase__=True       )						->      str:





           '''simple docstring'''
           super().__init__()
           A_						=      n_e
           A_						=      vq_embed_dim
           A_						=      beta
           A_						=      legacy

           A_						=      nn.Embedding(self.n_e		,				self.vq_embed_dim       )
           self.embedding.weight.data.uniform_(-1.0 / self.n_e		,				1.0 / self.n_e       )

           A_						=      remap
           if self.remap is not None:
                  self.register_buffer("""used"""		,				torch.tensor(np.load(self.remap       )       )       )
                  A_						=      self.used.shape[0]
                  A_						=      unknown_index  # "random" or "extra" or integer
                  if self.unknown_index == "extra":
                         A_						=      self.re_embed
                         A_						=      self.re_embed + 1
                  print(
                      f'''Remapping {self.n_e} indices to {self.re_embed} indices. '''
                      f'''Using {self.unknown_index} for unknown indices.'''       )
           else:
                  A_						=      n_e

           A_						=      sane_index_shape



    def 					snake_case_			(					self		,				UpperCamelCase__       )						->      Optional[Any]:





           '''simple docstring'''
           A_						=      inds.shape
           assert len(UpperCamelCase__       ) > 1
           A_						=      inds.reshape(ishape[0]		,				-1       )
           A_						=      self.used.to(UpperCamelCase__       )
           A_						=      (inds[:, :, None] == used[None, None, ...]).long()
           A_						=      match.argmax(-1       )
           A_						=      match.sum(2       ) < 1
           if self.unknown_index == "random":
                  A_						=      torch.randint(0		,				self.re_embed		,				size=new[unknown].shape       ).to(device=new.device       )
           else:
                  A_						=      self.unknown_index
           return new.reshape(UpperCamelCase__       )



    def 					snake_case_			(					self		,				UpperCamelCase__       )						->      Dict:





           '''simple docstring'''
           A_						=      inds.shape
           assert len(UpperCamelCase__       ) > 1
           A_						=      inds.reshape(ishape[0]		,				-1       )
           A_						=      self.used.to(UpperCamelCase__       )
           if self.re_embed > self.used.shape[0]:  # extra token
                  A_						=      0  # simply set to zero
           A_						=      torch.gather(used[None, :][inds.shape[0] * [0], :]		,				1		,				UpperCamelCase__       )
           return back.reshape(UpperCamelCase__       )



    def 					snake_case_			(					self		,				UpperCamelCase__       )						->      str:





           '''simple docstring'''
           # reshape z -> (batch, height, width, channel) and flatten
           A_						=      z.permute(0		,				2		,				3		,				1       ).contiguous()
           A_						=      z.view(-1		,				self.vq_embed_dim       )

           # distances from z to embeddings e_j (z - e)^2 = z^2 + e^2 - 2 e * z
           A_						=      torch.argmin(torch.cdist(UpperCamelCase__		,				self.embedding.weight       )		,				dim=1       )

           A_						=      self.embedding(UpperCamelCase__       ).view(z.shape       )
           A_						=      None
           A_						=      None

           # compute loss for embedding
           if not self.legacy:
                  A_						=      self.beta * torch.mean((z_q.detach() - z) ** 2       ) + torch.mean((z_q - z.detach()) ** 2       )
           else:
                  A_						=      torch.mean((z_q.detach() - z) ** 2       ) + self.beta * torch.mean((z_q - z.detach()) ** 2       )

           # preserve gradients
           A_						=      z + (z_q - z).detach()

           # reshape back to match original input shape
           A_						=      z_q.permute(0		,				3		,				1		,				2       ).contiguous()

           if self.remap is not None:
                  A_						=      min_encoding_indices.reshape(z.shape[0]		,				-1       )  # add batch axis
                  A_						=      self.remap_to_used(UpperCamelCase__       )
                  A_						=      min_encoding_indices.reshape(-1		,				1       )  # flatten

           if self.sane_index_shape:
                  A_						=      min_encoding_indices.reshape(z_q.shape[0]		,				z_q.shape[2]		,				z_q.shape[3]       )

           return z_q, loss, (perplexity, min_encodings, min_encoding_indices)







    def 					snake_case_			(					self		,				UpperCamelCase__		,				UpperCamelCase__       )						->      Optional[int]:





           '''simple docstring'''
           # shape specifying (batch, height, width, channel)
           if self.remap is not None:
                  A_						=      indices.reshape(shape[0]		,				-1       )  # add batch axis
                  A_						=      self.unmap_to_all(UpperCamelCase__       )
                  A_						=      indices.reshape(-1       )  # flatten again

           # get quantized latent vectors
           A_						=      self.embedding(UpperCamelCase__       )

           if shape is not None:
                  A_						=      z_q.view(UpperCamelCase__       )
                  # reshape back to match original input shape
                  A_						=      z_q.permute(0		,				3		,				1		,				2       ).contiguous()

           return z_q


class 					A__      (      _snake_case			):



    def __init__(					self		,				UpperCamelCase__		,				UpperCamelCase__=False       )						->      Dict:





           '''simple docstring'''
           A_						=      parameters
           A_					,			A_						=      torch.chunk(UpperCamelCase__		,				2		,				dim=1       )
           A_						=      torch.clamp(self.logvar		,				-30.0		,				20.0       )
           A_						=      deterministic
           A_						=      torch.exp(0.5 * self.logvar       )
           A_						=      torch.exp(self.logvar       )
           if self.deterministic:
                  A_						=      A_						=      torch.zeros_like(
                      self.mean		,				device=self.parameters.device		,				dtype=self.parameters.dtype       )



    def 					snake_case_			(					self		,				UpperCamelCase__ = None       )						->      torch.FloatTensor:





           '''simple docstring'''
           # make sure sample is on the same device as the parameters and has same dtype
           A_						=      randn_tensor(
               self.mean.shape		,				generator=UpperCamelCase__		,				device=self.parameters.device		,				dtype=self.parameters.dtype       )
           A_						=      self.mean + self.std * sample
           return x



    def 					snake_case_			(					self		,				UpperCamelCase__=None       )						->      int:





           '''simple docstring'''
           if self.deterministic:
                  return torch.Tensor([0.0]       )
           else:
                  if other is None:
                         return 0.5 * torch.sum(torch.pow(self.mean		,				2       ) + self.var - 1.0 - self.logvar		,				dim=[1, 2, 3]       )
                  else:
                         return 0.5 * torch.sum(
                             torch.pow(self.mean - other.mean		,				2       ) / other.var
                             + self.var / other.var
                             - 1.0
                             - self.logvar
                             + other.logvar		,				dim=[1, 2, 3]		,				)



    def 					snake_case_			(					self		,				UpperCamelCase__		,				UpperCamelCase__=[1, 2, 3]       )						->      Optional[Any]:





           '''simple docstring'''
           if self.deterministic:
                  return torch.Tensor([0.0]       )
           A_						=      np.log(2.0 * np.pi       )
           return 0.5 * torch.sum(logtwopi + self.logvar + torch.pow(sample - self.mean		,				2       ) / self.var		,				dim=UpperCamelCase__       )







    def 					snake_case_			(					self       )						->      List[Any]:





           '''simple docstring'''
           return self.mean
