from collections import defaultdict
from typing import Optional

from ..image_utils import load_image
from ..utils import (
    add_end_docstrings,
    is_torch_available,
    logging,
    requires_backends,
)
from .base import PIPELINE_INIT_ARGS, ChunkPipeline


if is_torch_available():
    import torch

    from ..models.auto.modeling_auto import MODEL_FOR_MASK_GENERATION_MAPPING

lowerCAmelCase__     						=			logging.get_logger(__name__)
@add_end_docstrings(SCREAMING_SNAKE_CASE						)
class     a_			(						SCREAMING_SNAKE_CASE						):






 '''simple docstring'''






 def __init__(      self :  List[str]      ,   **lowercase__ :  Any):







        '''simple docstring'''



        super().__init__(**lowercase__)
        requires_backends(self      ,   'vision')
        requires_backends(self      ,   'torch')

        if self.framework != "pt":
               raise ValueError(F"""The {self.__class__} is only available in PyTorch.""")

        self.check_model_type(lowercase__)




 def 		__snake_case						(      self :  str      ,   **lowercase__ :  Optional[Any]):







        '''simple docstring'''



        lowerCAmelCase__					     = {}
        lowerCAmelCase__					     = {}
        lowerCAmelCase__					     = {}
        # preprocess args
        if "points_per_batch" in kwargs:
               lowerCAmelCase__					     = kwargs['points_per_batch']
        if "points_per_crop" in kwargs:
               lowerCAmelCase__					     = kwargs['points_per_crop']
        if "crops_n_layers" in kwargs:
               lowerCAmelCase__					     = kwargs['crops_n_layers']
        if "crop_overlap_ratio" in kwargs:
               lowerCAmelCase__					     = kwargs['crop_overlap_ratio']
        if "crop_n_points_downscale_factor" in kwargs:
               lowerCAmelCase__					     = kwargs['crop_n_points_downscale_factor']
        # postprocess args
        if "pred_iou_thresh" in kwargs:
               lowerCAmelCase__					     = kwargs['pred_iou_thresh']
        if "stability_score_offset" in kwargs:
               lowerCAmelCase__					     = kwargs['stability_score_offset']
        if "mask_threshold" in kwargs:
               lowerCAmelCase__					     = kwargs['mask_threshold']
        if "stability_score_thresh" in kwargs:
               lowerCAmelCase__					     = kwargs['stability_score_thresh']
        if "crops_nms_thresh" in kwargs:
               lowerCAmelCase__					     = kwargs['crops_nms_thresh']
        if "output_rle_mask" in kwargs:
               lowerCAmelCase__					     = kwargs['output_rle_mask']
        if "output_bboxes_mask" in kwargs:
               lowerCAmelCase__					     = kwargs['output_bboxes_mask']
        return preprocess_kwargs, forward_params, postprocess_kwargs




 def __call__(      self :  Union[str, Any]      ,   lowercase__ :  Tuple      ,   *lowercase__ :  List[Any]      ,   lowercase__ :  Optional[Any]=None      ,   lowercase__ :  Tuple=None      ,   **lowercase__ :  Any):







        '''simple docstring'''



        return super().__call__(lowercase__      ,   *lowercase__      ,   num_workers=lowercase__      ,   batch_size=lowercase__      ,   **lowercase__)




 def 		__snake_case						(      self :  Any      ,   lowercase__ :  int      ,   lowercase__ :  int=64      ,   lowercase__ :  int = 0      ,   lowercase__ :  float = 512 / 1_500      ,   lowercase__ :  Optional[int] = 32      ,   lowercase__ :  Optional[int] = 1      ,   ):







        '''simple docstring'''



        lowerCAmelCase__					     = load_image(lowercase__)
        lowerCAmelCase__					     = self.image_processor.size['longest_edge']
        lowerCAmelCase__     , lowerCAmelCase__     , lowerCAmelCase__     , lowerCAmelCase__					     = self.image_processor.generate_crop_boxes(
            lowercase__      ,   lowercase__      ,   lowercase__      ,   lowercase__      ,   lowercase__      ,   lowercase__)
        lowerCAmelCase__					     = self.image_processor(images=lowercase__      ,   return_tensors='pt')

        with self.device_placement():
               if self.framework == "pt":
                      lowerCAmelCase__					     = self.get_inference_context()
                      with inference_context():
                             lowerCAmelCase__					     = self._ensure_tensor_on_device(lowercase__      ,   device=self.device)
                             lowerCAmelCase__					     = self.model.get_image_embeddings(model_inputs.pop('pixel_values'))
                             lowerCAmelCase__					     = image_embeddings

        lowerCAmelCase__					     = grid_points.shape[1]
        lowerCAmelCase__					     = points_per_batch if points_per_batch is not None else n_points

        if points_per_batch <= 0:
               raise ValueError(
                   'Cannot have points_per_batch<=0. Must be >=1 to returned batched outputs. '
                   'To return all points at once, set points_per_batch to None')

        for i in range(0      ,   lowercase__      ,   lowercase__):
               lowerCAmelCase__					     = grid_points[:, i : i + points_per_batch, :, :]
               lowerCAmelCase__					     = input_labels[:, i : i + points_per_batch]
               lowerCAmelCase__					     = i == n_points - points_per_batch
               yield {
                   "input_points": batched_points,
                   "input_labels": labels,
                   "input_boxes": crop_boxes,
                   "is_last": is_last,
                   **model_inputs,
               }




 def 		__snake_case						(      self :  Optional[int]      ,   lowercase__ :  int      ,   lowercase__ :  str=0.88      ,   lowercase__ :  List[Any]=0.95      ,   lowercase__ :  Any=0      ,   lowercase__ :  Dict=1      ,   ):







        '''simple docstring'''



        lowerCAmelCase__					     = model_inputs.pop('input_boxes')
        lowerCAmelCase__					     = model_inputs.pop('is_last')
        lowerCAmelCase__					     = model_inputs.pop('original_sizes').tolist()
        lowerCAmelCase__					     = model_inputs.pop('reshaped_input_sizes').tolist()

        lowerCAmelCase__					     = self.model(**lowercase__)

        # post processing happens here in order to avoid CPU GPU copies of ALL the masks
        lowerCAmelCase__					     = model_outputs['pred_masks']
        lowerCAmelCase__					     = self.image_processor.post_process_masks(
            lowercase__      ,   lowercase__      ,   lowercase__      ,   lowercase__      ,   binarize=lowercase__)
        lowerCAmelCase__					     = model_outputs['iou_scores']
        lowerCAmelCase__     , lowerCAmelCase__     , lowerCAmelCase__					     = self.image_processor.filter_masks(
            masks[0]      ,   iou_scores[0]      ,   original_sizes[0]      ,   input_boxes[0]      ,   lowercase__      ,   lowercase__      ,   lowercase__      ,   lowercase__      ,   )
        return {
            "masks": masks,
            "is_last": is_last,
            "boxes": boxes,
            "iou_scores": iou_scores,
        }




 def 		__snake_case						(      self :  Optional[Any]      ,   lowercase__ :  Union[str, Any]      ,   lowercase__ :  Optional[Any]=False      ,   lowercase__ :  int=False      ,   lowercase__ :  Optional[Any]=0.7      ,   ):







        '''simple docstring'''



        lowerCAmelCase__					     = []
        lowerCAmelCase__					     = []
        lowerCAmelCase__					     = []
        for model_output in model_outputs:
               all_scores.append(model_output.pop('iou_scores'))
               all_masks.extend(model_output.pop('masks'))
               all_boxes.append(model_output.pop('boxes'))

        lowerCAmelCase__					     = torch.cat(lowercase__)
        lowerCAmelCase__					     = torch.cat(lowercase__)
        lowerCAmelCase__     , lowerCAmelCase__     , lowerCAmelCase__     , lowerCAmelCase__					     = self.image_processor.post_process_for_mask_generation(
            lowercase__      ,   lowercase__      ,   lowercase__      ,   lowercase__)

        lowerCAmelCase__					     = defaultdict(lowercase__)
        for output in model_outputs:
               for k, v in output.items():
                      extra[k].append(lowercase__)

        lowerCAmelCase__					     = {}
        if output_rle_mask:
               lowerCAmelCase__					     = rle_mask

        if output_bboxes_mask:
               lowerCAmelCase__					     = bounding_boxes

        return {"masks": output_masks, "scores": iou_scores, **optional, **extra}

from typing import Optional, Tuple, Union

import flax
import flax.linen as nn
import jax
import jax.numpy as jnp
from flax.core.frozen_dict import FrozenDict

from ..configuration_utils import ConfigMixin, flax_register_to_config
from ..utils import BaseOutput
from .embeddings_flax import FlaxTimestepEmbedding, FlaxTimesteps
from .modeling_flax_utils import FlaxModelMixin
from .unet_ad_blocks_flax import (
    FlaxCrossAttnDownBlockaD,
    FlaxCrossAttnUpBlockaD,
    FlaxDownBlockaD,
    FlaxUNetMidBlockaDCrossAttn,
    FlaxUpBlockaD,
)
@flax.struct.dataclass
class     a_			(						SCREAMING_SNAKE_CASE						):






 '''simple docstring'''






 UpperCAmelCase_     			= 42






@flax_register_to_config
class     a_			(						nn.Module	,					SCREAMING_SNAKE_CASE	,					SCREAMING_SNAKE_CASE						):






 '''simple docstring'''






 UpperCAmelCase_     			= 32
 UpperCAmelCase_     			= 4
 UpperCAmelCase_     			= 4
 UpperCAmelCase_     			= (
     "CrossAttnDownBlock2D",
     "CrossAttnDownBlock2D",
     "CrossAttnDownBlock2D",
     "DownBlock2D",
 )
 UpperCAmelCase_     			= ("UpBlock2D", "CrossAttnUpBlock2D", "CrossAttnUpBlock2D", "CrossAttnUpBlock2D")
 UpperCAmelCase_     			= False
 UpperCAmelCase_     			= (320, 640, 1_280, 1_280)
 UpperCAmelCase_     			= 2
 UpperCAmelCase_     			= 8
 UpperCAmelCase_     			= None
 UpperCAmelCase_     			= 1_280
 UpperCAmelCase_     			= 0.0
 UpperCAmelCase_     			= False
 UpperCAmelCase_     			= jnp.floataa
 UpperCAmelCase_     			= True
 UpperCAmelCase_     			= 0
 UpperCAmelCase_     			= False




 def 		__snake_case						(      self :  Optional[int]      ,   lowercase__ :  jax.random.KeyArray):







        '''simple docstring'''



        lowerCAmelCase__					     = (1, self.in_channels, self.sample_size, self.sample_size)
        lowerCAmelCase__					     = jnp.zeros(lowercase__      ,   dtype=jnp.floataa)
        lowerCAmelCase__					     = jnp.ones((1,)      ,   dtype=jnp.intaa)
        lowerCAmelCase__					     = jnp.zeros((1, 1, self.cross_attention_dim)      ,   dtype=jnp.floataa)

        lowerCAmelCase__     , lowerCAmelCase__					     = jax.random.split(lowercase__)
        lowerCAmelCase__					     = {'params': params_rng, 'dropout': dropout_rng}

        return self.init(lowercase__      ,   lowercase__      ,   lowercase__      ,   lowercase__)["params"]




 def 		__snake_case						(      self :  Union[str, Any]):







        '''simple docstring'''



        lowerCAmelCase__					     = self.block_out_channels
        lowerCAmelCase__					     = block_out_channels[0] * 4

        if self.num_attention_heads is not None:
               raise ValueError(
                   'At the moment it is not possible to define the number of attention heads via `num_attention_heads` because of a naming issue as described in https://github.com/huggingface/diffusers/issues/2011#issuecomment-1547958131. Passing `num_attention_heads` will only be supported in diffusers v0.19.')

        # If `num_attention_heads` is not defined (which is the case for most models)
        # it will default to `attention_head_dim`. This looks weird upon first reading it and it is.
        # The reason for this behavior is to correct for incorrectly named variables that were introduced
        # when this library was created. The incorrect naming was only discovered much later in https://github.com/huggingface/diffusers/issues/2011#issuecomment-1547958131
        # Changing `attention_head_dim` to `num_attention_heads` for 40,000+ configurations is too backwards breaking
        # which is why we correct for the naming here.
        lowerCAmelCase__					     = self.num_attention_heads or self.attention_head_dim

        # input
        lowerCAmelCase__					     = nn.Conv(
            block_out_channels[0]      ,   kernel_size=(3, 3)      ,   strides=(1, 1)      ,   padding=((1, 1), (1, 1))      ,   dtype=self.dtype      ,   )

        # time
        lowerCAmelCase__					     = FlaxTimesteps(
            block_out_channels[0]      ,   flip_sin_to_cos=self.flip_sin_to_cos      ,   freq_shift=self.config.freq_shift)
        lowerCAmelCase__					     = FlaxTimestepEmbedding(lowercase__      ,   dtype=self.dtype)

        lowerCAmelCase__					     = self.only_cross_attention
        if isinstance(lowercase__      ,   lowercase__):
               lowerCAmelCase__					     = (only_cross_attention,) * len(self.down_block_types)

        if isinstance(lowercase__      ,   lowercase__):
               lowerCAmelCase__					     = (num_attention_heads,) * len(self.down_block_types)

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

               if down_block_type == "CrossAttnDownBlock2D":
                      lowerCAmelCase__					     = FlaxCrossAttnDownBlockaD(
                          in_channels=lowercase__      ,   out_channels=lowercase__      ,   dropout=self.dropout      ,   num_layers=self.layers_per_block      ,   num_attention_heads=num_attention_heads[i]      ,   add_downsample=not is_final_block      ,   use_linear_projection=self.use_linear_projection      ,   only_cross_attention=only_cross_attention[i]      ,   use_memory_efficient_attention=self.use_memory_efficient_attention      ,   dtype=self.dtype      ,   )
               else:
                      lowerCAmelCase__					     = FlaxDownBlockaD(
                          in_channels=lowercase__      ,   out_channels=lowercase__      ,   dropout=self.dropout      ,   num_layers=self.layers_per_block      ,   add_downsample=not is_final_block      ,   dtype=self.dtype      ,   )

               down_blocks.append(lowercase__)
        lowerCAmelCase__					     = down_blocks

        # mid
        lowerCAmelCase__					     = FlaxUNetMidBlockaDCrossAttn(
            in_channels=block_out_channels[-1]      ,   dropout=self.dropout      ,   num_attention_heads=num_attention_heads[-1]      ,   use_linear_projection=self.use_linear_projection      ,   use_memory_efficient_attention=self.use_memory_efficient_attention      ,   dtype=self.dtype      ,   )

        # up
        lowerCAmelCase__					     = []
        lowerCAmelCase__					     = list(reversed(lowercase__))
        lowerCAmelCase__					     = list(reversed(lowercase__))
        lowerCAmelCase__					     = list(reversed(lowercase__))
        lowerCAmelCase__					     = reversed_block_out_channels[0]
        for i, up_block_type in enumerate(self.up_block_types):
               lowerCAmelCase__					     = output_channel
               lowerCAmelCase__					     = reversed_block_out_channels[i]
               lowerCAmelCase__					     = reversed_block_out_channels[min(i + 1      ,   len(lowercase__) - 1)]

               lowerCAmelCase__					     = i == len(lowercase__) - 1

               if up_block_type == "CrossAttnUpBlock2D":
                      lowerCAmelCase__					     = FlaxCrossAttnUpBlockaD(
                          in_channels=lowercase__      ,   out_channels=lowercase__      ,   prev_output_channel=lowercase__      ,   num_layers=self.layers_per_block + 1      ,   num_attention_heads=reversed_num_attention_heads[i]      ,   add_upsample=not is_final_block      ,   dropout=self.dropout      ,   use_linear_projection=self.use_linear_projection      ,   only_cross_attention=only_cross_attention[i]      ,   use_memory_efficient_attention=self.use_memory_efficient_attention      ,   dtype=self.dtype      ,   )
               else:
                      lowerCAmelCase__					     = FlaxUpBlockaD(
                          in_channels=lowercase__      ,   out_channels=lowercase__      ,   prev_output_channel=lowercase__      ,   num_layers=self.layers_per_block + 1      ,   add_upsample=not is_final_block      ,   dropout=self.dropout      ,   dtype=self.dtype      ,   )

               up_blocks.append(lowercase__)
               lowerCAmelCase__					     = output_channel
        lowerCAmelCase__					     = up_blocks

        # out
        lowerCAmelCase__					     = nn.GroupNorm(num_groups=32      ,   epsilon=1e-5)
        lowerCAmelCase__					     = nn.Conv(
            self.out_channels      ,   kernel_size=(3, 3)      ,   strides=(1, 1)      ,   padding=((1, 1), (1, 1))      ,   dtype=self.dtype      ,   )




 def __call__(      self :  Optional[Any]      ,   lowercase__ :  Optional[int]      ,   lowercase__ :  List[str]      ,   lowercase__ :  int      ,   lowercase__ :  List[Any]=None      ,   lowercase__ :  Union[str, Any]=None      ,   lowercase__ :  bool = True      ,   lowercase__ :  bool = False      ,   ):







        '''simple docstring'''



        if not isinstance(lowercase__      ,   jnp.ndarray):
               lowerCAmelCase__					     = jnp.array([timesteps]      ,   dtype=jnp.intaa)
        elif isinstance(lowercase__      ,   jnp.ndarray) and len(timesteps.shape) == 0:
               lowerCAmelCase__					     = timesteps.astype(dtype=jnp.floataa)
               lowerCAmelCase__					     = jnp.expand_dims(lowercase__      ,   0)

        lowerCAmelCase__					     = self.time_proj(lowercase__)
        lowerCAmelCase__					     = self.time_embedding(lowercase__)

        # 2. pre-process
        lowerCAmelCase__					     = jnp.transpose(lowercase__      ,   (0, 2, 3, 1))
        lowerCAmelCase__					     = self.conv_in(lowercase__)

        # 3. down
        lowerCAmelCase__					     = (sample,)
        for down_block in self.down_blocks:
               if isinstance(lowercase__      ,   lowercase__):
                      lowerCAmelCase__     , lowerCAmelCase__					     = down_block(lowercase__      ,   lowercase__      ,   lowercase__      ,   deterministic=not train)
               else:
                      lowerCAmelCase__     , lowerCAmelCase__					     = down_block(lowercase__      ,   lowercase__      ,   deterministic=not train)
               down_block_res_samples += res_samples

        if down_block_additional_residuals is not None:
               lowerCAmelCase__					     = ()

               for down_block_res_sample, down_block_additional_residual in zip(
                   lowercase__      ,   lowercase__):
                      down_block_res_sample += down_block_additional_residual
                      new_down_block_res_samples += (down_block_res_sample,)

               lowerCAmelCase__					     = new_down_block_res_samples

        # 4. mid
        lowerCAmelCase__					     = self.mid_block(lowercase__      ,   lowercase__      ,   lowercase__      ,   deterministic=not train)

        if mid_block_additional_residual is not None:
               sample += mid_block_additional_residual

        # 5. up
        for up_block in self.up_blocks:
               lowerCAmelCase__					     = down_block_res_samples[-(self.layers_per_block + 1) :]
               lowerCAmelCase__					     = down_block_res_samples[: -(self.layers_per_block + 1)]
               if isinstance(lowercase__      ,   lowercase__):
                      lowerCAmelCase__					     = up_block(
                          lowercase__      ,   temb=lowercase__      ,   encoder_hidden_states=lowercase__      ,   res_hidden_states_tuple=lowercase__      ,   deterministic=not train      ,   )
               else:
                      lowerCAmelCase__					     = up_block(lowercase__      ,   temb=lowercase__      ,   res_hidden_states_tuple=lowercase__      ,   deterministic=not train)

        # 6. post-process
        lowerCAmelCase__					     = self.conv_norm_out(lowercase__)
        lowerCAmelCase__					     = nn.silu(lowercase__)
        lowerCAmelCase__					     = self.conv_out(lowercase__)
        lowerCAmelCase__					     = jnp.transpose(lowercase__      ,   (0, 3, 1, 2))

        if not return_dict:
               return (sample,)

        return FlaxUNetaDConditionOutput(sample=lowercase__)
