"""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_      (     __UpperCamelCase			):
					UpperCamelCase_ :	torch.FloatTensor

class       a_      (     nn.Module			):




					def __init__(	self						:     Dict  ,						snake_case__						:     str=3  ,						snake_case__						:     str=3  ,						snake_case__						:     List[str]=("DownEncoderBlock2D",)  ,						snake_case__						:     Tuple=(64,)  ,						snake_case__						:     str=2  ,						snake_case__						:     Tuple=32  ,						snake_case__						:     List[str]="silu"  ,						snake_case__						:     int=True  ,						):
							super().__init__()
							lowerCAmelCase__        =   layers_per_block

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

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

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

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

							# mid
							lowerCAmelCase__        =   UNetMidBlockaD(
							    in_channels=block_out_channels[-1]  ,						resnet_eps=1E-6  ,						resnet_act_fn=snake_case__  ,						output_scale_factor=1  ,						resnet_time_scale_shift="""default"""  ,						attention_head_dim=block_out_channels[-1]  ,						resnet_groups=snake_case__  ,						temb_channels=snake_case__  ,						)

							# out
							lowerCAmelCase__        =   nn.GroupNorm(num_channels=block_out_channels[-1]  ,						num_groups=snake_case__  ,						eps=1E-6     )
							lowerCAmelCase__        =   nn.SiLU()

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

							lowerCAmelCase__        =   False




					def 						_SCREAMING_SNAKE_CASE						(	self						:     Optional[Any]  ,						snake_case__						:     Optional[int]     ):
							lowerCAmelCase__        =   x
							lowerCAmelCase__        =   self.conv_in(snake_case__     )

							if self.training and self.gradient_checkpointing:

									def create_custom_forward(snake_case__						:     List[Any]     ):
											def custom_forward(*snake_case__						:     str     ):
													return module(*snake_case__     )

											return custom_forward

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

							else:
									# down
									for down_block in self.down_blocks:
											lowerCAmelCase__        =   down_block(snake_case__     )

									# middle
									lowerCAmelCase__        =   self.mid_block(snake_case__     )

							# post-process
							lowerCAmelCase__        =   self.conv_norm_out(snake_case__     )
							lowerCAmelCase__        =   self.conv_act(snake_case__     )
							lowerCAmelCase__        =   self.conv_out(snake_case__     )

							return sample

class       a_      (     nn.Module			):




					def __init__(	self						:     Union[str, Any]  ,						snake_case__						:     Any=3  ,						snake_case__						:     int=3  ,						snake_case__						:     List[str]=("UpDecoderBlock2D",)  ,						snake_case__						:     Tuple=(64,)  ,						snake_case__						:     List[Any]=2  ,						snake_case__						:     str=32  ,						snake_case__						:     Optional[Any]="silu"  ,						snake_case__						:     str="group"  ,						):
							super().__init__()
							lowerCAmelCase__        =   layers_per_block

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

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

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

							# mid
							lowerCAmelCase__        =   UNetMidBlockaD(
							    in_channels=block_out_channels[-1]  ,						resnet_eps=1E-6  ,						resnet_act_fn=snake_case__  ,						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=snake_case__  ,						temb_channels=snake_case__  ,						)

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

									lowerCAmelCase__        =   i == len(snake_case__     ) - 1

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

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

							lowerCAmelCase__        =   False




					def 						_SCREAMING_SNAKE_CASE						(	self						:     Any  ,						snake_case__						:     Union[str, Any]  ,						snake_case__						:     Optional[Any]=None     ):
							lowerCAmelCase__        =   z
							lowerCAmelCase__        =   self.conv_in(snake_case__     )

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

									def create_custom_forward(snake_case__						:     str     ):
											def custom_forward(*snake_case__						:     Union[str, Any]     ):
													return module(*snake_case__     )

											return custom_forward

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

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

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

									# up
									for up_block in self.up_blocks:
											lowerCAmelCase__        =   up_block(snake_case__  ,						snake_case__     )

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

							return sample

class       a_      (     nn.Module			):




					def __init__(	self						:     Optional[Any]  ,						snake_case__						:     Tuple  ,						snake_case__						:     List[Any]  ,						snake_case__						:     int  ,						snake_case__						:     Any=None  ,						snake_case__						:     str="random"  ,						snake_case__						:     Optional[int]=False  ,						snake_case__						:     List[Any]=True     ):
							super().__init__()
							lowerCAmelCase__        =   n_e
							lowerCAmelCase__        =   vq_embed_dim
							lowerCAmelCase__        =   beta
							lowerCAmelCase__        =   legacy

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

							lowerCAmelCase__        =   remap
							if self.remap is not None:
									self.register_buffer("""used"""  ,						torch.tensor(np.load(self.remap     )     )     )
									lowerCAmelCase__        =   self.used.shape[0]
									lowerCAmelCase__        =   unknown_index  # "random" or "extra" or integer
									if self.unknown_index == "extra":
											lowerCAmelCase__        =   self.re_embed
											lowerCAmelCase__        =   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:
									lowerCAmelCase__        =   n_e

							lowerCAmelCase__        =   sane_index_shape




					def 						_SCREAMING_SNAKE_CASE						(	self						:     int  ,						snake_case__						:     Dict     ):
							lowerCAmelCase__        =   inds.shape
							assert len(snake_case__     ) > 1
							lowerCAmelCase__        =   inds.reshape(ishape[0]  ,						-1     )
							lowerCAmelCase__        =   self.used.to(snake_case__     )
							lowerCAmelCase__        =   (inds[:, :, None] == used[None, None, ...]).long()
							lowerCAmelCase__        =   match.argmax(-1     )
							lowerCAmelCase__        =   match.sum(2     ) < 1
							if self.unknown_index == "random":
									lowerCAmelCase__        =   torch.randint(0  ,						self.re_embed  ,						size=new[unknown].shape     ).to(device=new.device     )
							else:
									lowerCAmelCase__        =   self.unknown_index
							return new.reshape(snake_case__     )




					def 						_SCREAMING_SNAKE_CASE						(	self						:     Optional[int]  ,						snake_case__						:     str     ):
							lowerCAmelCase__        =   inds.shape
							assert len(snake_case__     ) > 1
							lowerCAmelCase__        =   inds.reshape(ishape[0]  ,						-1     )
							lowerCAmelCase__        =   self.used.to(snake_case__     )
							if self.re_embed > self.used.shape[0]:  # extra token
									lowerCAmelCase__        =   0  # simply set to zero
							lowerCAmelCase__        =   torch.gather(used[None, :][inds.shape[0] * [0], :]  ,						1  ,						snake_case__     )
							return back.reshape(snake_case__     )




					def 						_SCREAMING_SNAKE_CASE						(	self						:     str  ,						snake_case__						:     List[Any]     ):
							# reshape z -> (batch, height, width, channel) and flatten
							lowerCAmelCase__        =   z.permute(0  ,						2  ,						3  ,						1     ).contiguous()
							lowerCAmelCase__        =   z.view(-1  ,						self.vq_embed_dim     )

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

							lowerCAmelCase__        =   self.embedding(snake_case__     ).view(z.shape     )
							lowerCAmelCase__        =   None
							lowerCAmelCase__        =   None

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

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

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

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

							if self.sane_index_shape:
									lowerCAmelCase__        =   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 						_SCREAMING_SNAKE_CASE						(	self						:     Optional[int]  ,						snake_case__						:     Tuple  ,						snake_case__						:     Union[str, Any]     ):
							# shape specifying (batch, height, width, channel)
							if self.remap is not None:
									lowerCAmelCase__        =   indices.reshape(shape[0]  ,						-1     )  # add batch axis
									lowerCAmelCase__        =   self.unmap_to_all(snake_case__     )
									lowerCAmelCase__        =   indices.reshape(-1     )  # flatten again

							# get quantized latent vectors
							lowerCAmelCase__        =   self.embedding(snake_case__     )

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

							return z_q

class       a_      (     __UpperCamelCase			):




					def __init__(	self						:     Dict  ,						snake_case__						:     Any  ,						snake_case__						:     List[str]=False     ):
							lowerCAmelCase__        =   parameters
							lowerCAmelCase__   ,					lowerCAmelCase__        =   torch.chunk(snake_case__  ,						2  ,						dim=1     )
							lowerCAmelCase__        =   torch.clamp(self.logvar  ,						-30.0  ,						20.0     )
							lowerCAmelCase__        =   deterministic
							lowerCAmelCase__        =   torch.exp(0.5 * self.logvar     )
							lowerCAmelCase__        =   torch.exp(self.logvar     )
							if self.deterministic:
									lowerCAmelCase__        =   lowerCAmelCase__        =   torch.zeros_like(
									    self.mean  ,						device=self.parameters.device  ,						dtype=self.parameters.dtype     )




					def 						_SCREAMING_SNAKE_CASE						(	self						:     Optional[Any]  ,						snake_case__						:     Optional[torch.Generator] = None     ):
							# make sure sample is on the same device as the parameters and has same dtype
							lowerCAmelCase__        =   randn_tensor(
							    self.mean.shape  ,						generator=snake_case__  ,						device=self.parameters.device  ,						dtype=self.parameters.dtype     )
							lowerCAmelCase__        =   self.mean + self.std * sample
							return x




					def 						_SCREAMING_SNAKE_CASE						(	self						:     Any  ,						snake_case__						:     Dict=None     ):
							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 						_SCREAMING_SNAKE_CASE						(	self						:     List[Any]  ,						snake_case__						:     int  ,						snake_case__						:     int=[1, 2, 3]     ):
							if self.deterministic:
									return torch.Tensor([0.0]     )
							lowerCAmelCase__        =   np.log(2.0 * np.pi     )
							return 0.5 * torch.sum(logtwopi + self.logvar + torch.pow(sample - self.mean  ,						2     ) / self.var  ,						dim=snake_case__     )




					def 						_SCREAMING_SNAKE_CASE						(	self						:     Optional[Any]     ):
							return self.mean





"""simple docstring"""




# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import numpy as np
import torch

from ..models.clipseg import CLIPSegForImageSegmentation
from ..utils import is_vision_available, requires_backends
from .base import PipelineTool


if is_vision_available():
			from PIL import Image

class       a_      (     __UpperCamelCase			):
					UpperCamelCase_ :	List[str]			=    (
					    "This is a tool that creates a segmentation mask of an image according to a label. It cannot create an image."
					    "It takes two arguments named `image` which should be the original image, and `label` which should be a text "
					    "describing the elements what should be identified in the segmentation mask. The tool returns the mask."
					)
					UpperCamelCase_ :	str			=    "CIDAS/clipseg-rd64-refined"
					UpperCamelCase_ :	Any			=    "image_segmenter"
					UpperCamelCase_ :	Optional[Any]			=    CLIPSegForImageSegmentation

					UpperCamelCase_ :	List[str]			=    ["image", "text"]
					UpperCamelCase_ :	int			=    ["image"]




					def __init__(	self						:     Tuple  ,						*snake_case__						:     str  ,						**snake_case__						:     Optional[Any]     ):
							requires_backends(self  ,						["""vision"""]     )
							super().__init__(*snake_case__  ,						**snake_case__     )




					def 						_SCREAMING_SNAKE_CASE						(	self						:     Optional[Any]  ,						snake_case__						:     "Image"  ,						snake_case__						:     str     ):
							return self.pre_processor(text=[label]  ,						images=[image]  ,						padding=snake_case__  ,						return_tensors="""pt"""     )




					def 						_SCREAMING_SNAKE_CASE						(	self						:     Dict  ,						snake_case__						:     Tuple     ):
							with torch.no_grad():
									lowerCAmelCase__        =   self.model(**snake_case__     ).logits
							return logits




					def 						_SCREAMING_SNAKE_CASE						(	self						:     Dict  ,						snake_case__						:     List[Any]     ):
							lowerCAmelCase__        =   outputs.cpu().detach().numpy()
							lowerCAmelCase__        =   0
							lowerCAmelCase__        =   1
							return Image.fromarray((array * 255).astype(np.uinta     )     )




