
"""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    _UpperCAmelCase(   snake_case_     ):
		lowercase__      =				42






class    _UpperCAmelCase(   nn.Module     ):



		def __init__( self    ,		__a=3    ,		__a=3    ,		__a=("DownEncoderBlock2D",)    ,		__a=(64,)    ,		__a=2    ,		__a=32    ,		__a="silu"    ,		__a=True    ,		)	->	int:






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

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

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

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

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

							# mid
							_UpperCamelCase										=  UNetMidBlockaD(
							    in_channels=block_out_channels[-1]    ,		resnet_eps=1e-6    ,		resnet_act_fn=__a    ,		output_scale_factor=1    ,		resnet_time_scale_shift='''default'''    ,		attention_head_dim=block_out_channels[-1]    ,		resnet_groups=__a    ,		temb_channels=__a    ,		)

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

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

							_UpperCamelCase										=  False




		def 						UpperCAmelCase							( self    ,		__a)	->	Any:






							'''simple docstring'''
							_UpperCamelCase										=  x
							_UpperCamelCase										=  self.conv_in(__a)

							if self.training and self.gradient_checkpointing:

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

																	return custom_forward

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

							else:
												# down
												for down_block in self.down_blocks:
																	_UpperCamelCase										=  down_block(__a)

												# middle
												_UpperCamelCase										=  self.mid_block(__a)

							# post-process
							_UpperCamelCase										=  self.conv_norm_out(__a)
							_UpperCamelCase										=  self.conv_act(__a)
							_UpperCamelCase										=  self.conv_out(__a)

							return sample






class    _UpperCAmelCase(   nn.Module     ):



		def __init__( self    ,		__a=3    ,		__a=3    ,		__a=("UpDecoderBlock2D",)    ,		__a=(64,)    ,		__a=2    ,		__a=32    ,		__a="silu"    ,		__a="group"    ,		)	->	int:






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

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

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

							_UpperCamelCase										=  in_channels if norm_type == 'spatial' else None

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

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

												_UpperCamelCase										=  i == len(__a) - 1

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

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

							_UpperCamelCase										=  False




		def 						UpperCAmelCase							( self    ,		__a    ,		__a=None)	->	Optional[Any]:






							'''simple docstring'''
							_UpperCamelCase										=  z
							_UpperCamelCase										=  self.conv_in(__a)

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

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

																	return custom_forward

												if is_torch_version('''>='''    ,		'''1.11.0'''):
																	# middle
																	_UpperCamelCase										=  torch.utils.checkpoint.checkpoint(
																	    create_custom_forward(self.mid_block)    ,		__a    ,		__a    ,		use_reentrant=__a)
																	_UpperCamelCase										=  sample.to(__a)

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

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

												# up
												for up_block in self.up_blocks:
																	_UpperCamelCase										=  up_block(__a    ,		__a)

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

							return sample






class    _UpperCAmelCase(   nn.Module     ):



		def __init__( self    ,		__a    ,		__a    ,		__a    ,		__a=None    ,		__a="random"    ,		__a=False    ,		__a=True)	->	Dict:






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

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

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

							_UpperCamelCase										=  sane_index_shape



		def 						UpperCAmelCase							( self    ,		__a)	->	str:






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



		def 						UpperCAmelCase							( self    ,		__a)	->	Any:






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



		def 						UpperCAmelCase							( self    ,		__a)	->	Tuple:






							'''simple docstring'''
							_UpperCamelCase										=  z.permute(0    ,		2    ,		3    ,		1).contiguous()
							_UpperCamelCase										=  z.view(-1    ,		self.vq_embed_dim)

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

							_UpperCamelCase										=  self.embedding(__a).view(z.shape)
							_UpperCamelCase										=  None
							_UpperCamelCase										=  None

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

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

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

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

							if self.sane_index_shape:
												_UpperCamelCase										=  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 						UpperCAmelCase							( self    ,		__a    ,		__a)	->	Optional[Any]:






							'''simple docstring'''





							if self.remap is not None:
												_UpperCamelCase										=  indices.reshape(shape[0]    ,		-1)  # add batch axis
												_UpperCamelCase										=  self.unmap_to_all(__a)
												_UpperCamelCase										=  indices.reshape(-1)  # flatten again

							# get quantized latent vectors
							_UpperCamelCase										=  self.embedding(__a)

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

							return z_q






class    _UpperCAmelCase(   snake_case_     ):



		def __init__( self    ,		__a    ,		__a=False)	->	List[str]:






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



		def 						UpperCAmelCase							( self    ,		__a = None)	->	torch.FloatTensor:






							'''simple docstring'''
							_UpperCamelCase										=  randn_tensor(
							    self.mean.shape    ,		generator=__a    ,		device=self.parameters.device    ,		dtype=self.parameters.dtype)
							_UpperCamelCase										=  self.mean + self.std * sample
							return x



		def 						UpperCAmelCase							( self    ,		__a=None)	->	List[Any]:






							'''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 						UpperCAmelCase							( self    ,		__a    ,		__a=[1, 2, 3])	->	int:






							'''simple docstring'''





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




		def 						UpperCAmelCase							( self)	->	List[str]:






							'''simple docstring'''
							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    _UpperCAmelCase(   lowerCamelCase     ):
		lowercase__      =				(
		    '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.'
		)
		lowercase__      =				'CIDAS/clipseg-rd64-refined'
		lowercase__      =				'image_segmenter'
		lowercase__      =				CLIPSegForImageSegmentation

		lowercase__      =				['image', 'text']
		lowercase__      =				['image']



		def __init__( self    ,		*__a    ,		**__a)	->	Any:






							'''simple docstring'''
							requires_backends(self    ,		['''vision'''])
							super().__init__(*__a    ,		**__a)



		def 						UpperCAmelCase							( self    ,		__a    ,		__a)	->	Dict:






							'''simple docstring'''
							return self.pre_processor(text=[label]    ,		images=[image]    ,		padding=__a    ,		return_tensors='''pt''')



		def 						UpperCAmelCase							( self    ,		__a)	->	Tuple:






							'''simple docstring'''
							with torch.no_grad():
												_UpperCamelCase										=  self.model(**__a).logits
							return logits




		def 						UpperCAmelCase							( self    ,		__a)	->	Any:






							'''simple docstring'''
							_UpperCamelCase										=  outputs.cpu().detach().numpy()
							_UpperCamelCase										=  0
							_UpperCamelCase										=  1
							return Image.fromarray((array * 2_55).astype(np.uinta))






