

"""simple docstring"""





import gc
import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    PNDMScheduler,
    StableDiffusionLDMaDPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import nightly, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS


enable_full_determinism()




class  _lowerCAmelCase  (     unittest.TestCase       ):







 """simple docstring"""







 __UpperCAmelCase     : Any       		=     StableDiffusionLDMaDPipeline
 __UpperCAmelCase     : Dict       		=     TEXT_TO_IMAGE_PARAMS
 __UpperCAmelCase     : List[Any]       		=     TEXT_TO_IMAGE_BATCH_PARAMS
 __UpperCAmelCase     : List[Any]       		=     TEXT_TO_IMAGE_IMAGE_PARAMS


 def  _lowercase			(      self							:      int     ):
    torch.manual_seed(0     )
    __lowercase								=						UNetaDConditionModel(
        block_out_channels=(3_2, 6_4), layers_per_block=2, sample_size=3_2, in_channels=4, out_channels=4, down_block_types=("DownBlock2D", "CrossAttnDownBlock2D"), up_block_types=("CrossAttnUpBlock2D", "UpBlock2D"), cross_attention_dim=3_2, )
    __lowercase								=						DDIMScheduler(
        beta_start=0.00_085, beta_end=0.012, beta_schedule="scaled_linear", clip_sample=UpperCAmelCase__, set_alpha_to_one=UpperCAmelCase__, )
    torch.manual_seed(0     )
    __lowercase								=						AutoencoderKL(
        block_out_channels=[3_2, 6_4], in_channels=6, out_channels=6, down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"], up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"], latent_channels=4, )
    torch.manual_seed(0     )
    __lowercase								=						CLIPTextConfig(
        bos_token_id=0, eos_token_id=2, hidden_size=3_2, intermediate_size=3_7, layer_norm_eps=1E-05, num_attention_heads=4, num_hidden_layers=5, pad_token_id=1, vocab_size=1_0_0_0, )
    __lowercase								=						CLIPTextModel(UpperCAmelCase__     )
    __lowercase								=						CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"     )

    __lowercase								=						{
        "unet": unet,
        "scheduler": scheduler,
        "vae": vae,
        "text_encoder": text_encoder,
        "tokenizer": tokenizer,
        "safety_checker": None,
        "feature_extractor": None,
    }
    return components


 def  _lowercase			(      self							:      List[Any], UpperCAmelCase__							:      Optional[Any], UpperCAmelCase__							:      Tuple=0     ):
    if str(UpperCAmelCase__     ).startswith("mps"     ):
       __lowercase								=						torch.manual_seed(UpperCAmelCase__     )
    else:
       __lowercase								=						torch.Generator(device=UpperCAmelCase__     ).manual_seed(UpperCAmelCase__     )
    __lowercase								=						{
        "prompt": "A painting of a squirrel eating a burger",
        "generator": generator,
        "num_inference_steps": 2,
        "guidance_scale": 6.0,
        "output_type": "numpy",
    }
    return inputs


 def  _lowercase			(      self							:      List[str]     ):
    __lowercase								=						"cpu"  # ensure determinism for the device-dependent torch.Generator

    __lowercase								=						self.get_dummy_components()
    __lowercase								=						StableDiffusionLDMaDPipeline(**UpperCAmelCase__     )
    __lowercase								=						ldmad_pipe.to(UpperCAmelCase__     )
    ldmad_pipe.set_progress_bar_config(disable=UpperCAmelCase__     )

    __lowercase								=						self.get_dummy_inputs(UpperCAmelCase__     )
    __lowercase								=						ldmad_pipe(**UpperCAmelCase__     )
    __lowercase		,__lowercase								=						output.rgb, output.depth

    __lowercase								=						rgb[0, -3:, -3:, -1]
    __lowercase								=						depth[0, -3:, -1]

    assert rgb.shape == (1, 6_4, 6_4, 3)
    assert depth.shape == (1, 6_4, 6_4)

    __lowercase								=						np.array(
        [0.37_338_176, 0.70_247, 0.74_203_193, 0.51_643_604, 0.58_256_793, 0.60_932_136, 0.4_181_095, 0.48_355_877, 0.46_535_262]     )
    __lowercase								=						np.array([103.46_727, 85.812_004, 87.849_236]     )

    assert np.abs(image_slice_rgb.flatten() - expected_slice_rgb     ).max() < 1E-2
    assert np.abs(image_slice_depth.flatten() - expected_slice_depth     ).max() < 1E-2


 def  _lowercase			(      self							:      Optional[Any]     ):
    __lowercase								=						self.get_dummy_components()
    __lowercase								=						StableDiffusionLDMaDPipeline(**UpperCAmelCase__     )
    __lowercase								=						ldmad_pipe.to(UpperCAmelCase__     )
    ldmad_pipe.set_progress_bar_config(disable=UpperCAmelCase__     )

    __lowercase								=						self.get_dummy_inputs(UpperCAmelCase__     )
    __lowercase								=						3 * [inputs["prompt"]]

    # forward
    __lowercase								=						ldmad_pipe(**UpperCAmelCase__     )
    __lowercase		,__lowercase								=						output.rgb, output.depth
    __lowercase								=						rgb_slice_a[0, -3:, -3:, -1]
    __lowercase								=						depth_slice_a[0, -3:, -1]

    __lowercase								=						self.get_dummy_inputs(UpperCAmelCase__     )
    __lowercase								=						3 * [inputs.pop("prompt"     )]

    __lowercase								=						ldmad_pipe.tokenizer(
        UpperCAmelCase__, padding="max_length", max_length=ldmad_pipe.tokenizer.model_max_length, truncation=UpperCAmelCase__, return_tensors="pt", )
    __lowercase								=						text_inputs["input_ids"].to(UpperCAmelCase__     )

    __lowercase								=						ldmad_pipe.text_encoder(UpperCAmelCase__     )[0]

    __lowercase								=						prompt_embeds

    # forward
    __lowercase								=						ldmad_pipe(**UpperCAmelCase__     )
    __lowercase		,__lowercase								=						output.rgb, output.depth
    __lowercase								=						rgb_slice_a[0, -3:, -3:, -1]
    __lowercase								=						depth_slice_a[0, -3:, -1]

    assert np.abs(rgb_slice_a.flatten() - rgb_slice_a.flatten()     ).max() < 1E-4
    assert np.abs(depth_slice_a.flatten() - depth_slice_a.flatten()     ).max() < 1E-4



 def  _lowercase			(      self							:      List[str]     ):
    __lowercase								=						"cpu"  # ensure determinism for the device-dependent torch.Generator
    __lowercase								=						self.get_dummy_components()
    __lowercase								=						PNDMScheduler(skip_prk_steps=UpperCAmelCase__     )
    __lowercase								=						StableDiffusionLDMaDPipeline(**UpperCAmelCase__     )
    __lowercase								=						ldmad_pipe.to(UpperCAmelCase__     )
    ldmad_pipe.set_progress_bar_config(disable=UpperCAmelCase__     )

    __lowercase								=						self.get_dummy_inputs(UpperCAmelCase__     )
    __lowercase								=						"french fries"
    __lowercase								=						ldmad_pipe(**UpperCAmelCase__, negative_prompt=UpperCAmelCase__     )

    __lowercase		,__lowercase								=						output.rgb, output.depth
    __lowercase								=						rgb[0, -3:, -3:, -1]
    __lowercase								=						depth[0, -3:, -1]

    assert rgb.shape == (1, 6_4, 6_4, 3)
    assert depth.shape == (1, 6_4, 6_4)

    __lowercase								=						np.array(
        [0.37_044, 0.71_811_503, 0.7_223_251, 0.48_603_675, 0.5_638_391, 0.6_364_948, 0.42_833_704, 0.4_901_315, 0.47_926_217]     )
    __lowercase								=						np.array([107.84_738, 84.62_802, 89.962_135]     )
    assert np.abs(rgb_slice.flatten() - expected_slice_rgb     ).max() < 1E-2
    assert np.abs(depth_slice.flatten() - expected_slice_depth     ).max() < 1E-2







@slow
@require_torch_gpu
class  _lowerCAmelCase  (     unittest.TestCase       ):







 """simple docstring"""







 def  _lowercase			(      self							:      Optional[Any]     ):
    super().tearDown()
    gc.collect()
    torch.cuda.empty_cache()


 def  _lowercase			(      self							:      int, UpperCAmelCase__							:      List[Any], UpperCAmelCase__							:      Dict="cpu", UpperCAmelCase__							:      List[Any]=torch.floataa, UpperCAmelCase__							:      str=0     ):
    __lowercase								=						torch.Generator(device=UpperCAmelCase__     ).manual_seed(UpperCAmelCase__     )
    __lowercase								=						np.random.RandomState(UpperCAmelCase__     ).standard_normal((1, 4, 6_4, 6_4)     )
    __lowercase								=						torch.from_numpy(UpperCAmelCase__     ).to(device=UpperCAmelCase__, dtype=UpperCAmelCase__     )
    __lowercase								=						{
        "prompt": "a photograph of an astronaut riding a horse",
        "latents": latents,
        "generator": generator,
        "num_inference_steps": 3,
        "guidance_scale": 7.5,
        "output_type": "numpy",
    }
    return inputs



 def  _lowercase			(      self							:      List[str]     ):
    __lowercase								=						StableDiffusionLDMaDPipeline.from_pretrained("Intel/ldm3d"     )
    __lowercase								=						ldmad_pipe.to(UpperCAmelCase__     )
    ldmad_pipe.set_progress_bar_config(disable=UpperCAmelCase__     )

    __lowercase								=						self.get_inputs(UpperCAmelCase__     )
    __lowercase								=						ldmad_pipe(**UpperCAmelCase__     )
    __lowercase		,__lowercase								=						output.rgb, output.depth
    __lowercase								=						rgb[0, -3:, -3:, -1].flatten()
    __lowercase								=						rgb[0, -3:, -1].flatten()

    assert rgb.shape == (1, 5_1_2, 5_1_2, 3)
    assert depth.shape == (1, 5_1_2, 5_1_2)

    __lowercase								=						np.array(
        [0.53_805_465, 0.56_707_305, 0.5_486_515, 0.57_012_236, 0.5_814_511, 0.56_253_487, 0.54_843_014, 0.55_092_263, 0.6_459_706]     )
    __lowercase								=						np.array(
        [0.9_263_781, 0.6_678_672, 0.5_486_515, 0.92_202_145, 0.67_831_135, 0.56_253_487, 0.9_241_694, 0.7_551_478, 0.6_459_706]     )
    assert np.abs(rgb_slice - expected_slice_rgb     ).max() < 3E-3
    assert np.abs(depth_slice - expected_slice_depth     ).max() < 3E-3







@nightly
@require_torch_gpu
class  _lowerCAmelCase  (     unittest.TestCase       ):







 """simple docstring"""







 def  _lowercase			(      self							:      Optional[int]     ):
    super().tearDown()
    gc.collect()
    torch.cuda.empty_cache()


 def  _lowercase			(      self							:      Union[str, Any], UpperCAmelCase__							:      List[Any], UpperCAmelCase__							:      List[str]="cpu", UpperCAmelCase__							:      Any=torch.floataa, UpperCAmelCase__							:      Optional[Any]=0     ):
    __lowercase								=						torch.Generator(device=UpperCAmelCase__     ).manual_seed(UpperCAmelCase__     )
    __lowercase								=						np.random.RandomState(UpperCAmelCase__     ).standard_normal((1, 4, 6_4, 6_4)     )
    __lowercase								=						torch.from_numpy(UpperCAmelCase__     ).to(device=UpperCAmelCase__, dtype=UpperCAmelCase__     )
    __lowercase								=						{
        "prompt": "a photograph of an astronaut riding a horse",
        "latents": latents,
        "generator": generator,
        "num_inference_steps": 5_0,
        "guidance_scale": 7.5,
        "output_type": "numpy",
    }
    return inputs


 def  _lowercase			(      self							:      Dict     ):
    __lowercase								=						StableDiffusionLDMaDPipeline.from_pretrained("Intel/ldm3d"     ).to(UpperCAmelCase__     )
    ldmad_pipe.set_progress_bar_config(disable=UpperCAmelCase__     )

    __lowercase								=						self.get_inputs(UpperCAmelCase__     )
    __lowercase								=						ldmad_pipe(**UpperCAmelCase__     )
    __lowercase		,__lowercase								=						output.rgb, output.depth

    __lowercase								=						0.495_586
    __lowercase								=						0.33_795_515
    __lowercase								=						112.48_518
    __lowercase								=						98.489_746
    assert np.abs(expected_rgb_mean - rgb.mean()     ) < 1E-3
    assert np.abs(expected_rgb_std - rgb.std()     ) < 1E-3
    assert np.abs(expected_depth_mean - depth.mean()     ) < 1E-3
    assert np.abs(expected_depth_std - depth.std()     ) < 1E-3



 def  _lowercase			(      self							:      str     ):
    __lowercase								=						StableDiffusionLDMaDPipeline.from_pretrained("Intel/ldm3d-4c"     ).to(UpperCAmelCase__     )
    ldmad_pipe.set_progress_bar_config(disable=UpperCAmelCase__     )

    __lowercase								=						self.get_inputs(UpperCAmelCase__     )
    __lowercase								=						ldmad_pipe(**UpperCAmelCase__     )
    __lowercase		,__lowercase								=						output.rgb, output.depth

    __lowercase								=						0.4_194_127
    __lowercase								=						0.35_375_586
    __lowercase								=						0.5_638_502
    __lowercase								=						0.34_686_103

    assert rgb.shape == (1, 5_1_2, 5_1_2, 3)
    assert depth.shape == (1, 5_1_2, 5_1_2, 1)
    assert np.abs(expected_rgb_mean - rgb.mean()     ) < 1E-3
    assert np.abs(expected_rgb_std - rgb.std()     ) < 1E-3
    assert np.abs(expected_depth_mean - depth.mean()     ) < 1E-3
    assert np.abs(expected_depth_std - depth.std()     ) < 1E-3







"""simple docstring"""





import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_funnel import FunnelTokenizer


_a							=							logging.get_logger(__name__)

_a							=							{'vocab_file': 'vocab.txt', 'tokenizer_file': 'tokenizer.json'}

_a							=							[
    'small',
    'small-base',
    'medium',
    'medium-base',
    'intermediate',
    'intermediate-base',
    'large',
    'large-base',
    'xlarge',
    'xlarge-base',
]

_a							=							{
    'vocab_file': {
        'funnel-transformer/small': 'https://huggingface.co/funnel-transformer/small/resolve/main/vocab.txt',
        'funnel-transformer/small-base': 'https://huggingface.co/funnel-transformer/small-base/resolve/main/vocab.txt',
        'funnel-transformer/medium': 'https://huggingface.co/funnel-transformer/medium/resolve/main/vocab.txt',
        'funnel-transformer/medium-base': (
            'https://huggingface.co/funnel-transformer/medium-base/resolve/main/vocab.txt'
        ),
        'funnel-transformer/intermediate': (
            'https://huggingface.co/funnel-transformer/intermediate/resolve/main/vocab.txt'
        ),
        'funnel-transformer/intermediate-base': (
            'https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/vocab.txt'
        ),
        'funnel-transformer/large': 'https://huggingface.co/funnel-transformer/large/resolve/main/vocab.txt',
        'funnel-transformer/large-base': 'https://huggingface.co/funnel-transformer/large-base/resolve/main/vocab.txt',
        'funnel-transformer/xlarge': 'https://huggingface.co/funnel-transformer/xlarge/resolve/main/vocab.txt',
        'funnel-transformer/xlarge-base': (
            'https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/vocab.txt'
        ),
    },
    'tokenizer_file': {
        'funnel-transformer/small': 'https://huggingface.co/funnel-transformer/small/resolve/main/tokenizer.json',
        'funnel-transformer/small-base': (
            'https://huggingface.co/funnel-transformer/small-base/resolve/main/tokenizer.json'
        ),
        'funnel-transformer/medium': 'https://huggingface.co/funnel-transformer/medium/resolve/main/tokenizer.json',
        'funnel-transformer/medium-base': (
            'https://huggingface.co/funnel-transformer/medium-base/resolve/main/tokenizer.json'
        ),
        'funnel-transformer/intermediate': (
            'https://huggingface.co/funnel-transformer/intermediate/resolve/main/tokenizer.json'
        ),
        'funnel-transformer/intermediate-base': (
            'https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/tokenizer.json'
        ),
        'funnel-transformer/large': 'https://huggingface.co/funnel-transformer/large/resolve/main/tokenizer.json',
        'funnel-transformer/large-base': (
            'https://huggingface.co/funnel-transformer/large-base/resolve/main/tokenizer.json'
        ),
        'funnel-transformer/xlarge': 'https://huggingface.co/funnel-transformer/xlarge/resolve/main/tokenizer.json',
        'funnel-transformer/xlarge-base': (
            'https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/tokenizer.json'
        ),
    },
}
_a							=							{F"funnel-transformer/{name}": 5_12 for name in _model_names}
_a							=							{F"funnel-transformer/{name}": {'do_lower_case': True} for name in _model_names}




class  _lowerCAmelCase  (     lowercase       ):







 """simple docstring"""







 __UpperCAmelCase     : Optional[Any]       		=     VOCAB_FILES_NAMES
 __UpperCAmelCase     : Tuple       		=     PRETRAINED_VOCAB_FILES_MAP
 __UpperCAmelCase     : Union[str, Any]       		=     PRETRAINED_INIT_CONFIGURATION
 __UpperCAmelCase     : Optional[Any]       		=     FunnelTokenizer
 __UpperCAmelCase     : List[str]       		=     PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
 __UpperCAmelCase     : int       		=     2


 def __init__(      self							:      Dict, UpperCAmelCase__							:      Dict=None, UpperCAmelCase__							:      Optional[int]=None, UpperCAmelCase__							:      Dict=True, UpperCAmelCase__							:      List[str]="<unk>", UpperCAmelCase__							:      Optional[Any]="<sep>", UpperCAmelCase__							:      Optional[Any]="<pad>", UpperCAmelCase__							:      Union[str, Any]="<cls>", UpperCAmelCase__							:      str="<mask>", UpperCAmelCase__							:      Optional[Any]="<s>", UpperCAmelCase__							:      Tuple="</s>", UpperCAmelCase__							:      Union[str, Any]=True, UpperCAmelCase__							:      Optional[int]=True, UpperCAmelCase__							:      Optional[Any]=None, UpperCAmelCase__							:      Dict="##", **UpperCAmelCase__							:      List[str], ):
    super().__init__(
        UpperCAmelCase__, tokenizer_file=UpperCAmelCase__, do_lower_case=UpperCAmelCase__, unk_token=UpperCAmelCase__, sep_token=UpperCAmelCase__, pad_token=UpperCAmelCase__, cls_token=UpperCAmelCase__, mask_token=UpperCAmelCase__, bos_token=UpperCAmelCase__, eos_token=UpperCAmelCase__, clean_text=UpperCAmelCase__, tokenize_chinese_chars=UpperCAmelCase__, strip_accents=UpperCAmelCase__, wordpieces_prefix=UpperCAmelCase__, **UpperCAmelCase__, )

    __lowercase								=						json.loads(self.backend_tokenizer.normalizer.__getstate__()     )
    if (
        normalizer_state.get("lowercase", UpperCAmelCase__     ) != do_lower_case
        or normalizer_state.get("strip_accents", UpperCAmelCase__     ) != strip_accents
        or normalizer_state.get("handle_chinese_chars", UpperCAmelCase__     ) != tokenize_chinese_chars
    ):
       __lowercase								=						getattr(UpperCAmelCase__, normalizer_state.pop("type"     )     )
       __lowercase								=						do_lower_case
       __lowercase								=						strip_accents
       __lowercase								=						tokenize_chinese_chars
       __lowercase								=						normalizer_class(**UpperCAmelCase__     )

    __lowercase								=						do_lower_case


 def  _lowercase			(      self							:      Tuple, UpperCAmelCase__							:      Any, UpperCAmelCase__							:      Any=None     ):
    __lowercase								=						[self.cls_token_id] + token_ids_a + [self.sep_token_id]

    if token_ids_a:
       output += token_ids_a + [self.sep_token_id]

    return output


 def  _lowercase			(      self							:      str, UpperCAmelCase__							:      List[int], UpperCAmelCase__							:      Optional[List[int]] = None     ):
    __lowercase								=						[self.sep_token_id]
    __lowercase								=						[self.cls_token_id]
    if token_ids_a is None:
       return len(cls     ) * [self.cls_token_type_id] + len(token_ids_a + sep     ) * [0]
    return len(cls     ) * [self.cls_token_type_id] + len(token_ids_a + sep     ) * [0] + len(token_ids_a + sep     ) * [1]



 def  _lowercase			(      self							:      Optional[Any], UpperCAmelCase__							:      str, UpperCAmelCase__							:      Optional[str] = None     ):
    __lowercase								=						self._tokenizer.model.save(UpperCAmelCase__, name=UpperCAmelCase__     )
    return tuple(UpperCAmelCase__     )




