


'''simple docstring'''

import gc
import random
import tempfile
import unittest

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

from diffusers import AutoencoderKL, DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler, UNetaDConditionModel
from diffusers.pipelines.semantic_stable_diffusion import SemanticStableDiffusionPipeline as StableDiffusionPipeline
from diffusers.utils import floats_tensor, nightly, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()







class 			UpperCAmelCase			(							unittest.TestCase):

 def 		lowercase_    (			self			:						Tuple       ):





    """simple docstring"""



    super().tearDown()
    gc.collect()
    torch.cuda.empty_cache()

 @property
 def 		lowercase_    (			self			:						List[str]       ):





    """simple docstring"""



    UpperCamelCase__			    =    1
    UpperCamelCase__			    =    3
    UpperCamelCase__			    =    (32, 32)

    UpperCamelCase__			    =    floats_tensor((batch_size, num_channels) + sizes,       rng=random.Random(0       )       ).to(a_       )
    return image

 @property
 def 		lowercase_    (			self			:						Optional[int]       ):





    """simple docstring"""



    torch.manual_seed(0       )
    UpperCamelCase__			    =    UNetaDConditionModel(
        block_out_channels=(32, 64),       layers_per_block=2,       sample_size=32,       in_channels=4,       out_channels=4,       down_block_types=("DownBlock2D", "CrossAttnDownBlock2D"),       up_block_types=("CrossAttnUpBlock2D", "UpBlock2D"),       cross_attention_dim=32,       )
    return model

 @property
 def 		lowercase_    (			self			:						Dict       ):





    """simple docstring"""



    torch.manual_seed(0       )
    UpperCamelCase__			    =    AutoencoderKL(
        block_out_channels=[32, 64],       in_channels=3,       out_channels=3,       down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"],       up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"],       latent_channels=4,       )
    return model

 @property
 def 		lowercase_    (			self			:						List[Any]       ):





    """simple docstring"""



    torch.manual_seed(0       )
    UpperCamelCase__			    =    CLIPTextConfig(
        bos_token_id=0,       eos_token_id=2,       hidden_size=32,       intermediate_size=37,       layer_norm_eps=1e-0_5,       num_attention_heads=4,       num_hidden_layers=5,       pad_token_id=1,       vocab_size=1000,       )
    return CLIPTextModel(a_       )

 @property
 def 		lowercase_    (			self			:						Dict       ):





    """simple docstring"""



    def extract(*a_			:						List[Any],       **a_			:						str       ):







       class 			UpperCAmelCase			:

        def __init__(			self			:						Dict       ):





           """simple docstring"""



           UpperCamelCase__			    =    torch.ones([0]       )

        def 		lowercase_    (			self			:						Optional[int],       a_			:						str       ):





           """simple docstring"""



           self.pixel_values.to(a_       )
           return self







       return Out()

    return extract

 def 		lowercase_    (			self			:						int       ):





    """simple docstring"""



    UpperCamelCase__			    =    "cpu"  # ensure determinism for the device-dependent torch.Generator
    UpperCamelCase__			    =    self.dummy_cond_unet
    UpperCamelCase__			    =    DDIMScheduler(
        beta_start=0.00_085,       beta_end=0.012,       beta_schedule="scaled_linear",       clip_sample=a_,       set_alpha_to_one=a_,       )

    UpperCamelCase__			    =    self.dummy_vae
    UpperCamelCase__			    =    self.dummy_text_encoder
    UpperCamelCase__			    =    CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"       )

    # make sure here that pndm scheduler skips prk
    UpperCamelCase__			    =    StableDiffusionPipeline(
        unet=a_,       scheduler=a_,       vae=a_,       text_encoder=a_,       tokenizer=a_,       safety_checker=a_,       feature_extractor=self.dummy_extractor,       )
    UpperCamelCase__			    =    sd_pipe.to(a_       )
    sd_pipe.set_progress_bar_config(disable=a_       )

    UpperCamelCase__			    =    "A painting of a squirrel eating a burger"

    UpperCamelCase__			    =    torch.Generator(device=a_       ).manual_seed(0       )
    UpperCamelCase__			    =    sd_pipe([prompt],       generator=a_,       guidance_scale=6.0,       num_inference_steps=2,       output_type="np"       )
    UpperCamelCase__			    =    output.images

    UpperCamelCase__			    =    torch.Generator(device=a_       ).manual_seed(0       )
    UpperCamelCase__			    =    sd_pipe(
        [prompt],       generator=a_,       guidance_scale=6.0,       num_inference_steps=2,       output_type="np",       return_dict=a_,       )[0]

    UpperCamelCase__			    =    image[0, -3:, -3:, -1]
    UpperCamelCase__			    =    image_from_tuple[0, -3:, -3:, -1]

    assert image.shape == (1, 64, 64, 3)
    UpperCamelCase__			    =    np.array([0.5_753, 0.6_114, 0.5_001, 0.5_034, 0.5_470, 0.4_729, 0.4_971, 0.4_867, 0.4_867]       )

    assert np.abs(image_slice.flatten() - expected_slice       ).max() < 1e-2
    assert np.abs(image_from_tuple_slice.flatten() - expected_slice       ).max() < 1e-2

 def 		lowercase_    (			self			:						Dict       ):





    """simple docstring"""



    UpperCamelCase__			    =    "cpu"  # ensure determinism for the device-dependent torch.Generator
    UpperCamelCase__			    =    self.dummy_cond_unet
    UpperCamelCase__			    =    PNDMScheduler(skip_prk_steps=a_       )
    UpperCamelCase__			    =    self.dummy_vae
    UpperCamelCase__			    =    self.dummy_text_encoder
    UpperCamelCase__			    =    CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"       )

    # make sure here that pndm scheduler skips prk
    UpperCamelCase__			    =    StableDiffusionPipeline(
        unet=a_,       scheduler=a_,       vae=a_,       text_encoder=a_,       tokenizer=a_,       safety_checker=a_,       feature_extractor=self.dummy_extractor,       )
    UpperCamelCase__			    =    sd_pipe.to(a_       )
    sd_pipe.set_progress_bar_config(disable=a_       )

    UpperCamelCase__			    =    "A painting of a squirrel eating a burger"
    UpperCamelCase__			    =    torch.Generator(device=a_       ).manual_seed(0       )
    UpperCamelCase__			    =    sd_pipe([prompt],       generator=a_,       guidance_scale=6.0,       num_inference_steps=2,       output_type="np"       )

    UpperCamelCase__			    =    output.images

    UpperCamelCase__			    =    torch.Generator(device=a_       ).manual_seed(0       )
    UpperCamelCase__			    =    sd_pipe(
        [prompt],       generator=a_,       guidance_scale=6.0,       num_inference_steps=2,       output_type="np",       return_dict=a_,       )[0]

    UpperCamelCase__			    =    image[0, -3:, -3:, -1]
    UpperCamelCase__			    =    image_from_tuple[0, -3:, -3:, -1]

    assert image.shape == (1, 64, 64, 3)
    UpperCamelCase__			    =    np.array([0.5_122, 0.5_712, 0.4_825, 0.5_053, 0.5_646, 0.4_769, 0.5_179, 0.4_894, 0.4_994]       )

    assert np.abs(image_slice.flatten() - expected_slice       ).max() < 1e-2
    assert np.abs(image_from_tuple_slice.flatten() - expected_slice       ).max() < 1e-2

 def 		lowercase_    (			self			:						Tuple       ):





    """simple docstring"""



    UpperCamelCase__			    =    StableDiffusionPipeline.from_pretrained(
        "hf-internal-testing/tiny-stable-diffusion-lms-pipe",       safety_checker=a_       )
    assert isinstance(a_,       a_       )
    assert isinstance(pipe.scheduler,       a_       )
    assert pipe.safety_checker is None

    UpperCamelCase__			    =    pipe("example prompt",       num_inference_steps=2       ).images[0]
    assert image is not None

    # check that there's no error when saving a pipeline with one of the models being None
    with tempfile.TemporaryDirectory() as tmpdirname:
       pipe.save_pretrained(a_       )
       UpperCamelCase__			    =    StableDiffusionPipeline.from_pretrained(a_       )

    # sanity check that the pipeline still works
    assert pipe.safety_checker is None
    UpperCamelCase__			    =    pipe("example prompt",       num_inference_steps=2       ).images[0]
    assert image is not None

 @unittest.skipIf(torch_device != "cuda",       "This test requires a GPU"       )
 def 		lowercase_    (			self			:						int       ):





    """simple docstring"""



    UpperCamelCase__			    =    self.dummy_cond_unet
    UpperCamelCase__			    =    PNDMScheduler(skip_prk_steps=a_       )
    UpperCamelCase__			    =    self.dummy_vae
    UpperCamelCase__			    =    self.dummy_text_encoder
    UpperCamelCase__			    =    CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"       )

    # put models in fp16
    UpperCamelCase__			    =    unet.half()
    UpperCamelCase__			    =    vae.half()
    UpperCamelCase__			    =    bert.half()

    # make sure here that pndm scheduler skips prk
    UpperCamelCase__			    =    StableDiffusionPipeline(
        unet=a_,       scheduler=a_,       vae=a_,       text_encoder=a_,       tokenizer=a_,       safety_checker=a_,       feature_extractor=self.dummy_extractor,       )
    UpperCamelCase__			    =    sd_pipe.to(a_       )
    sd_pipe.set_progress_bar_config(disable=a_       )

    UpperCamelCase__			    =    "A painting of a squirrel eating a burger"
    UpperCamelCase__			    =    sd_pipe([prompt],       num_inference_steps=2,       output_type="np"       ).images

    assert image.shape == (1, 64, 64, 3)







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

 def 		lowercase_    (			self			:						List[Any]       ):





    """simple docstring"""



    super().tearDown()
    gc.collect()
    torch.cuda.empty_cache()

 def 		lowercase_    (			self			:						Dict       ):





    """simple docstring"""



    UpperCamelCase__			    =    "cuda"
    UpperCamelCase__			    =    StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5"       )
    UpperCamelCase__			    =    pipe.to(a_       )
    pipe.set_progress_bar_config(disable=a_       )

    UpperCamelCase__			    =    "a photo of a cat"
    UpperCamelCase__			    =    {
        "editing_prompt": ["sunglasses"],
        "reverse_editing_direction": [False],
        "edit_warmup_steps": 10,
        "edit_guidance_scale": 6,
        "edit_threshold": 0.95,
        "edit_momentum_scale": 0.5,
        "edit_mom_beta": 0.6,
    }

    UpperCamelCase__			    =    3
    UpperCamelCase__			    =    7

    # no sega enabled
    UpperCamelCase__			    =    torch.Generator(a_       )
    generator.manual_seed(a_       )
    UpperCamelCase__			    =    pipe(
        [prompt],       generator=a_,       guidance_scale=a_,       num_inference_steps=50,       output_type="np",       width=512,       height=512,       )

    UpperCamelCase__			    =    output.images
    UpperCamelCase__			    =    image[0, -3:, -3:, -1]
    UpperCamelCase__			    =    [
        0.34_673_113,
        0.38_492_733,
        0.37_597_352,
        0.34_086_335,
        0.35_650_748,
        0.35_579_205,
        0.3_384_763,
        0.34_340_236,
        0.3_573_271,
    ]

    assert image.shape == (1, 512, 512, 3)

    assert np.abs(image_slice.flatten() - expected_slice       ).max() < 1e-2

    # with sega enabled
    # generator = torch.manual_seed(seed)
    generator.manual_seed(a_       )
    UpperCamelCase__			    =    pipe(
        [prompt],       generator=a_,       guidance_scale=a_,       num_inference_steps=50,       output_type="np",       width=512,       height=512,       **a_,       )

    UpperCamelCase__			    =    output.images
    UpperCamelCase__			    =    image[0, -3:, -3:, -1]
    UpperCamelCase__			    =    [
        0.41_887_826,
        0.37_728_766,
        0.30_138_272,
        0.41_416_335,
        0.41_664_985,
        0.36_283_392,
        0.36_191_246,
        0.43_364_465,
        0.43_001_732,
    ]

    assert image.shape == (1, 512, 512, 3)

    assert np.abs(image_slice.flatten() - expected_slice       ).max() < 1e-2

 def 		lowercase_    (			self			:						int       ):





    """simple docstring"""



    UpperCamelCase__			    =    "cuda"
    UpperCamelCase__			    =    StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5"       )
    UpperCamelCase__			    =    pipe.to(a_       )
    pipe.set_progress_bar_config(disable=a_       )

    UpperCamelCase__			    =    "an image of a crowded boulevard, realistic, 4k"
    UpperCamelCase__			    =    {
        "editing_prompt": "crowd, crowded, people",
        "reverse_editing_direction": True,
        "edit_warmup_steps": 10,
        "edit_guidance_scale": 8.3,
        "edit_threshold": 0.9,
        "edit_momentum_scale": 0.5,
        "edit_mom_beta": 0.6,
    }

    UpperCamelCase__			    =    9
    UpperCamelCase__			    =    7

    # no sega enabled
    UpperCamelCase__			    =    torch.Generator(a_       )
    generator.manual_seed(a_       )
    UpperCamelCase__			    =    pipe(
        [prompt],       generator=a_,       guidance_scale=a_,       num_inference_steps=50,       output_type="np",       width=512,       height=512,       )

    UpperCamelCase__			    =    output.images
    UpperCamelCase__			    =    image[0, -3:, -3:, -1]
    UpperCamelCase__			    =    [
        0.43_497_998,
        0.91_814_065,
        0.7_540_739,
        0.55_580_205,
        0.8_467_265,
        0.5_389_691,
        0.62_574_506,
        0.58_897_763,
        0.50_926_757,
    ]

    assert image.shape == (1, 512, 512, 3)

    assert np.abs(image_slice.flatten() - expected_slice       ).max() < 1e-2

    # with sega enabled
    # generator = torch.manual_seed(seed)
    generator.manual_seed(a_       )
    UpperCamelCase__			    =    pipe(
        [prompt],       generator=a_,       guidance_scale=a_,       num_inference_steps=50,       output_type="np",       width=512,       height=512,       **a_,       )

    UpperCamelCase__			    =    output.images
    UpperCamelCase__			    =    image[0, -3:, -3:, -1]
    UpperCamelCase__			    =    [
        0.3_089_719,
        0.30_500_144,
        0.29_016_042,
        0.30_630_964,
        0.325_687,
        0.29_419_225,
        0.2_908_091,
        0.28_723_598,
        0.27_696_294,
    ]

    assert image.shape == (1, 512, 512, 3)

    assert np.abs(image_slice.flatten() - expected_slice       ).max() < 1e-2

 def 		lowercase_    (			self			:						Dict       ):





    """simple docstring"""



    UpperCamelCase__			    =    "cuda"
    UpperCamelCase__			    =    StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5"       )
    UpperCamelCase__			    =    pipe.to(a_       )
    pipe.set_progress_bar_config(disable=a_       )

    UpperCamelCase__			    =    "a castle next to a river"
    UpperCamelCase__			    =    {
        "editing_prompt": ["boat on a river, boat", "monet, impression, sunrise"],
        "reverse_editing_direction": False,
        "edit_warmup_steps": [15, 18],
        "edit_guidance_scale": 6,
        "edit_threshold": [0.9, 0.8],
        "edit_momentum_scale": 0.5,
        "edit_mom_beta": 0.6,
    }

    UpperCamelCase__			    =    48
    UpperCamelCase__			    =    7

    # no sega enabled
    UpperCamelCase__			    =    torch.Generator(a_       )
    generator.manual_seed(a_       )
    UpperCamelCase__			    =    pipe(
        [prompt],       generator=a_,       guidance_scale=a_,       num_inference_steps=50,       output_type="np",       width=512,       height=512,       )

    UpperCamelCase__			    =    output.images
    UpperCamelCase__			    =    image[0, -3:, -3:, -1]
    UpperCamelCase__			    =    [
        0.75_163_555,
        0.76_037_145,
        0.61_785,
        0.9_189_673,
        0.8_627_701,
        0.85_189_694,
        0.8_512_813,
        0.87_012_076,
        0.8_312_857,
    ]

    assert image.shape == (1, 512, 512, 3)

    assert np.abs(image_slice.flatten() - expected_slice       ).max() < 1e-2

    # with sega enabled
    # generator = torch.manual_seed(seed)
    generator.manual_seed(a_       )
    UpperCamelCase__			    =    pipe(
        [prompt],       generator=a_,       guidance_scale=a_,       num_inference_steps=50,       output_type="np",       width=512,       height=512,       **a_,       )

    UpperCamelCase__			    =    output.images
    UpperCamelCase__			    =    image[0, -3:, -3:, -1]
    UpperCamelCase__			    =    [
        0.73_553_365,
        0.7_537_271,
        0.74_341_905,
        0.66_480_356,
        0.6_472_925,
        0.63_039_416,
        0.64_812_905,
        0.6_749_717,
        0.6_517_102,
    ]

    assert image.shape == (1, 512, 512, 3)

    assert np.abs(image_slice.flatten() - expected_slice       ).max() < 1e-2

 def 		lowercase_    (			self			:						int       ):





    """simple docstring"""



    UpperCamelCase__			    =    "cuda"
    UpperCamelCase__			    =    StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5",       torch_dtype=torch.floataa       )
    UpperCamelCase__			    =    pipe.to(a_       )
    pipe.set_progress_bar_config(disable=a_       )

    UpperCamelCase__			    =    "a photo of a cat"
    UpperCamelCase__			    =    {
        "editing_prompt": ["sunglasses"],
        "reverse_editing_direction": [False],
        "edit_warmup_steps": 10,
        "edit_guidance_scale": 6,
        "edit_threshold": 0.95,
        "edit_momentum_scale": 0.5,
        "edit_mom_beta": 0.6,
    }

    UpperCamelCase__			    =    3
    UpperCamelCase__			    =    7

    # no sega enabled
    UpperCamelCase__			    =    torch.Generator(a_       )
    generator.manual_seed(a_       )
    UpperCamelCase__			    =    pipe(
        [prompt],       generator=a_,       guidance_scale=a_,       num_inference_steps=50,       output_type="np",       width=512,       height=512,       )

    UpperCamelCase__			    =    output.images
    UpperCamelCase__			    =    image[0, -3:, -3:, -1]
    UpperCamelCase__			    =    [
        0.34_887_695,
        0.3_876_953,
        0.375,
        0.34_423_828,
        0.3_581_543,
        0.35_717_773,
        0.3_383_789,
        0.34_570_312,
        0.359_375,
    ]

    assert image.shape == (1, 512, 512, 3)

    assert np.abs(image_slice.flatten() - expected_slice       ).max() < 1e-2

    # with sega enabled
    # generator = torch.manual_seed(seed)
    generator.manual_seed(a_       )
    UpperCamelCase__			    =    pipe(
        [prompt],       generator=a_,       guidance_scale=a_,       num_inference_steps=50,       output_type="np",       width=512,       height=512,       **a_,       )

    UpperCamelCase__			    =    output.images
    UpperCamelCase__			    =    image[0, -3:, -3:, -1]
    UpperCamelCase__			    =    [
        0.42_285_156,
        0.36_914_062,
        0.29_077_148,
        0.42_041_016,
        0.41_918_945,
        0.35_498_047,
        0.3_618_164,
        0.4_423_828,
        0.43_115_234,
    ]

    assert image.shape == (1, 512, 512, 3)

    assert np.abs(image_slice.flatten() - expected_slice       ).max() < 1e-2



'''simple docstring'''

from typing import Dict, Tuple, overload

import torch
import torch.types
from torch import nn

from . import residue_constants as rc
from .rigid_utils import Rigid, Rotation
from .tensor_utils import batched_gather



@overload
def 							SCREAMING_SNAKE_CASE__(		_UpperCamelCase   :					torch.Tensor     ,		_UpperCamelCase   :					torch.Tensor     ,		_UpperCamelCase   :					None )		->       torch.Tensor:





   '''simple docstring'''







   ...



@overload
def 							SCREAMING_SNAKE_CASE__(		_UpperCamelCase   :					torch.Tensor     ,		_UpperCamelCase   :					torch.Tensor     ,		_UpperCamelCase   :					torch.Tensor )		->       Tuple[torch.Tensor, torch.Tensor]:





   '''simple docstring'''







   ...



def 							SCREAMING_SNAKE_CASE__(		_UpperCamelCase   :					int     ,		_UpperCamelCase   :					Dict     ,		_UpperCamelCase   :					str )		->       int:





   '''simple docstring'''







   UpperCamelCase__			    =    aatype == rc.restype_order["G"]
   UpperCamelCase__			    =    rc.atom_order["CA"]
   UpperCamelCase__			    =    rc.atom_order["CB"]
   UpperCamelCase__			    =    torch.where(
       is_gly[..., None].expand(*((-1,) * len(is_gly.shape ))     ,		3 )     ,		all_atom_positions[..., ca_idx, :]     ,		all_atom_positions[..., cb_idx, :]     ,		)

   if all_atom_masks is not None:
      UpperCamelCase__			    =    torch.where(
          _UpperCamelCase     ,		all_atom_masks[..., ca_idx]     ,		all_atom_masks[..., cb_idx]     ,		)
      return pseudo_beta, pseudo_beta_mask
   else:
      return pseudo_beta



def 							SCREAMING_SNAKE_CASE__(		_UpperCamelCase   :					torch.Tensor     ,		_UpperCamelCase   :					Dict[str, torch.Tensor] )		->       torch.Tensor:





   '''simple docstring'''







   UpperCamelCase__			    =    batched_gather(
       _UpperCamelCase     ,		batch["residx_atom37_to_atom14"]     ,		dim=-2     ,		no_batch_dims=len(atomaa.shape[:-2] )     ,		)

   UpperCamelCase__			    =    atomaa_data * batch["atom37_atom_exists"][..., None]

   return atomaa_data



def 							SCREAMING_SNAKE_CASE__(		_UpperCamelCase   :					Dict[str, torch.Tensor] )		->       torch.Tensor:





   '''simple docstring'''







   UpperCamelCase__			    =    template_feats["template_aatype"]
   UpperCamelCase__			    =    template_feats["template_torsion_angles_sin_cos"]
   UpperCamelCase__			    =    template_feats["template_alt_torsion_angles_sin_cos"]
   UpperCamelCase__			    =    template_feats["template_torsion_angles_mask"]
   UpperCamelCase__			    =    torch.cat(
       [
           nn.functional.one_hot(_UpperCamelCase     ,		22 ),
           torsion_angles_sin_cos.reshape(*torsion_angles_sin_cos.shape[:-2]     ,		14 ),
           alt_torsion_angles_sin_cos.reshape(*alt_torsion_angles_sin_cos.shape[:-2]     ,		14 ),
           torsion_angles_mask,
       ]     ,		dim=-1     ,		)

   return template_angle_feat



def 							SCREAMING_SNAKE_CASE__(		_UpperCamelCase   :					Dict[str, torch.Tensor]     ,		_UpperCamelCase   :					torch.types.Number     ,		_UpperCamelCase   :					torch.types.Number     ,		_UpperCamelCase   :					int     ,		_UpperCamelCase   :					bool = False     ,		_UpperCamelCase   :					float = 1e-20     ,		_UpperCamelCase   :					float = 1e8     ,		)		->       torch.Tensor:





   '''simple docstring'''







   UpperCamelCase__			    =    batch["template_pseudo_beta_mask"]
   UpperCamelCase__			    =    template_mask[..., None] * template_mask[..., None, :]

   # Compute distogram (this seems to differ slightly from Alg. 5)
   UpperCamelCase__			    =    batch["template_pseudo_beta"]
   UpperCamelCase__			    =    torch.sum((tpb[..., None, :] - tpb[..., None, :, :]) ** 2     ,		dim=-1     ,		keepdim=_UpperCamelCase )
   UpperCamelCase__			    =    torch.linspace(_UpperCamelCase     ,		_UpperCamelCase     ,		_UpperCamelCase     ,		device=tpb.device ) ** 2
   UpperCamelCase__			    =    torch.cat([lower[1:], lower.new_tensor([inf] )]     ,		dim=-1 )
   UpperCamelCase__			    =    ((dgram > lower) * (dgram < upper)).type(dgram.dtype )

   UpperCamelCase__			    =    [dgram, template_mask_ad[..., None]]

   UpperCamelCase__			    =    nn.functional.one_hot(
       batch["template_aatype"]     ,		rc.restype_num + 2     ,		)

   UpperCamelCase__			    =    batch["template_aatype"].shape[-1]
   to_concat.append(aatype_one_hot[..., None, :, :].expand(*aatype_one_hot.shape[:-2]     ,		_UpperCamelCase     ,		-1     ,		-1 ) )
   to_concat.append(aatype_one_hot[..., None, :].expand(*aatype_one_hot.shape[:-2]     ,		-1     ,		_UpperCamelCase     ,		-1 ) )

   UpperCamelCase__    ,		UpperCamelCase__    ,		UpperCamelCase__			    =    [rc.atom_order[a] for a in ["N", "CA", "C"]]
   UpperCamelCase__			    =    Rigid.make_transform_from_reference(
       n_xyz=batch["template_all_atom_positions"][..., n, :]     ,		ca_xyz=batch["template_all_atom_positions"][..., ca, :]     ,		c_xyz=batch["template_all_atom_positions"][..., c, :]     ,		eps=_UpperCamelCase     ,		)
   UpperCamelCase__			    =    rigids.get_trans()[..., None, :, :]
   UpperCamelCase__			    =    rigids[..., None].invert_apply(_UpperCamelCase )

   UpperCamelCase__			    =    torch.rsqrt(eps + torch.sum(rigid_vec**2     ,		dim=-1 ) )

   UpperCamelCase__			    =    batch["template_all_atom_mask"]
   UpperCamelCase__			    =    t_aa_masks[..., n] * t_aa_masks[..., ca] * t_aa_masks[..., c]
   UpperCamelCase__			    =    template_mask[..., None] * template_mask[..., None, :]

   UpperCamelCase__			    =    inv_distance_scalar * template_mask_ad
   UpperCamelCase__			    =    rigid_vec * inv_distance_scalar[..., None]

   if not use_unit_vector:
      UpperCamelCase__			    =    unit_vector * 0.0

   to_concat.extend(torch.unbind(unit_vector[..., None, :]     ,		dim=-1 ) )
   to_concat.append(template_mask_ad[..., None] )

   UpperCamelCase__			    =    torch.cat(_UpperCamelCase     ,		dim=-1 )
   UpperCamelCase__			    =    act * template_mask_ad[..., None]

   return act



def 							SCREAMING_SNAKE_CASE__(		_UpperCamelCase   :					Dict[str, torch.Tensor] )		->       torch.Tensor:





   '''simple docstring'''







   UpperCamelCase__			    =    nn.functional.one_hot(batch["extra_msa"]     ,		23 )
   UpperCamelCase__			    =    [
       msa_ahot,
       batch["extra_has_deletion"].unsqueeze(-1 ),
       batch["extra_deletion_value"].unsqueeze(-1 ),
   ]
   return torch.cat(_UpperCamelCase     ,		dim=-1 )



def 							SCREAMING_SNAKE_CASE__(		_UpperCamelCase   :					Rigid     ,		_UpperCamelCase   :					torch.Tensor     ,		_UpperCamelCase   :					torch.Tensor     ,		_UpperCamelCase   :					torch.Tensor     ,		)		->       Rigid:





   '''simple docstring'''







   UpperCamelCase__			    =    rrgdf[aatype, ...]

   # [*, N, 8] transformations, i.e.
   #   One [*, N, 8, 3, 3] rotation matrix and
   #   One [*, N, 8, 3]    translation matrix
   UpperCamelCase__			    =    r.from_tensor_axa(_UpperCamelCase )

   UpperCamelCase__			    =    alpha.new_zeros((*((1,) * len(alpha.shape[:-1] )), 2) )
   UpperCamelCase__			    =    1

   # [*, N, 8, 2]
   UpperCamelCase__			    =    torch.cat([bb_rot.expand(*alpha.shape[:-2]     ,		-1     ,		-1 ), alpha]     ,		dim=-2 )

   # [*, N, 8, 3, 3]
   # Produces rotation matrices of the form:
   # [
   #   [1, 0  , 0  ],
   #   [0, a_2,-a_1],
   #   [0, a_1, a_2]
   # ]
   # This follows the original code rather than the supplement, which uses
   # different indices.

   UpperCamelCase__			    =    alpha.new_zeros(default_r.get_rots().get_rot_mats().shape )
   UpperCamelCase__			    =    1
   UpperCamelCase__			    =    alpha[..., 1]
   UpperCamelCase__			    =    -alpha[..., 0]
   UpperCamelCase__			    =    alpha

   UpperCamelCase__			    =    default_r.compose(Rigid(Rotation(rot_mats=_UpperCamelCase )     ,		_UpperCamelCase ) )

   UpperCamelCase__			    =    all_frames[..., 5]
   UpperCamelCase__			    =    all_frames[..., 6]
   UpperCamelCase__			    =    all_frames[..., 7]

   UpperCamelCase__			    =    all_frames[..., 4]
   UpperCamelCase__			    =    chia_frame_to_bb.compose(_UpperCamelCase )
   UpperCamelCase__			    =    chia_frame_to_bb.compose(_UpperCamelCase )
   UpperCamelCase__			    =    chia_frame_to_bb.compose(_UpperCamelCase )

   UpperCamelCase__			    =    Rigid.cat(
       [
           all_frames[..., :5],
           chia_frame_to_bb.unsqueeze(-1 ),
           chia_frame_to_bb.unsqueeze(-1 ),
           chia_frame_to_bb.unsqueeze(-1 ),
       ]     ,		dim=-1     ,		)

   UpperCamelCase__			    =    r[..., None].compose(_UpperCamelCase )

   return all_frames_to_global






def 							SCREAMING_SNAKE_CASE__(		_UpperCamelCase   :					Rigid     ,		_UpperCamelCase   :					torch.Tensor     ,		_UpperCamelCase   :					torch.Tensor     ,		_UpperCamelCase   :					torch.Tensor     ,		_UpperCamelCase   :					torch.Tensor     ,		_UpperCamelCase   :					torch.Tensor     ,		)		->       torch.Tensor:





   '''simple docstring'''







   UpperCamelCase__			    =    group_idx[aatype, ...]

   # [*, N, 14, 8]
   UpperCamelCase__			    =    nn.functional.one_hot(
       _UpperCamelCase     ,		num_classes=default_frames.shape[-3]     ,		)

   # [*, N, 14, 8]
   UpperCamelCase__			    =    r[..., None, :] * group_mask_one_hot

   # [*, N, 14]
   UpperCamelCase__			    =    t_atoms_to_global.map_tensor_fn(lambda _UpperCamelCase : torch.sum(_UpperCamelCase     ,		dim=-1 ) )

   # [*, N, 14, 1]
   UpperCamelCase__			    =    atom_mask[aatype, ...].unsqueeze(-1 )

   # [*, N, 14, 3]
   UpperCamelCase__			    =    lit_positions[aatype, ...]
   UpperCamelCase__			    =    t_atoms_to_global.apply(_UpperCamelCase )
   UpperCamelCase__			    =    pred_positions * atom_mask

   return pred_positions