



"""simple docstring"""

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,
    FlaxDownBlockaD,
    FlaxUNetMidBlockaDCrossAttn,
)




@flax.struct.dataclass
class SCREAMING_SNAKE_CASE__					(		__SCREAMING_SNAKE_CASE      ):
						_lowerCAmelCase							=					42
						_lowerCAmelCase							=					42







class SCREAMING_SNAKE_CASE__					(		nn.Module      ):
						_lowerCAmelCase							=					42
						_lowerCAmelCase							=					(1_6, 3_2, 9_6, 2_5_6)
						_lowerCAmelCase							=					jnp.floataa



						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													__a		:		Optional[Any]									=       nn.Conv(
													    self.block_out_channels[0]						,     kernel_size=(3, 3)						,     padding=((1, 1), (1, 1))						,     dtype=self.dtype						,     )

													__a		:		int									=       []
													for i in range(len(self.block_out_channels							) - 1							):
																				__a		:		Tuple									=       self.block_out_channels[i]
																				__a		:		List[str]									=       self.block_out_channels[i + 1]
																				__a		:		List[str]									=       nn.Conv(
																				    _a						,     kernel_size=(3, 3)						,     padding=((1, 1), (1, 1))						,     dtype=self.dtype						,     )
																				blocks.append(_a							)
																				__a		:		List[str]									=       nn.Conv(
																				    _a						,     kernel_size=(3, 3)						,     strides=(2, 2)						,     padding=((1, 1), (1, 1))						,     dtype=self.dtype						,     )
																				blocks.append(_a							)
													__a		:		Dict									=       blocks

													__a		:		Optional[Any]									=       nn.Conv(
													    self.conditioning_embedding_channels						,     kernel_size=(3, 3)						,     padding=((1, 1), (1, 1))						,     kernel_init=nn.initializers.zeros_init()						,     bias_init=nn.initializers.zeros_init()						,     dtype=self.dtype						,     )





						def __call__(self						,     _lowercase							):





													'''simple docstring'''

													__a		:		str									=       self.conv_in(_a							)
													__a		:		Optional[Any]									=       nn.silu(_a							)

													for block in self.blocks:
																				__a		:		Optional[Any]									=       block(_a							)
																				__a		:		str									=       nn.silu(_a							)

													__a		:		int									=       self.conv_out(_a							)

													return embedding







@flax_register_to_config
class SCREAMING_SNAKE_CASE__					(		nn.Module     ,					__SCREAMING_SNAKE_CASE     ,					__SCREAMING_SNAKE_CASE      ):
						_lowerCAmelCase							=					3_2
						_lowerCAmelCase							=					4
						_lowerCAmelCase							=					(
						    "CrossAttnDownBlock2D",
						    "CrossAttnDownBlock2D",
						    "CrossAttnDownBlock2D",
						    "DownBlock2D",
						)
						_lowerCAmelCase							=					False
						_lowerCAmelCase							=					(3_2_0, 6_4_0, 1_2_8_0, 1_2_8_0)
						_lowerCAmelCase							=					2
						_lowerCAmelCase							=					8
						_lowerCAmelCase							=					None
						_lowerCAmelCase							=					1_2_8_0
						_lowerCAmelCase							=					0.0
						_lowerCAmelCase							=					False
						_lowerCAmelCase							=					jnp.floataa
						_lowerCAmelCase							=					True
						_lowerCAmelCase							=					0
						_lowerCAmelCase							=					"rgb"
						_lowerCAmelCase							=					(1_6, 3_2, 9_6, 2_5_6)



						def     lowerCAmelCase__(self						,     _lowercase							):





													'''simple docstring'''

													__a		:		str									=       (1, self.in_channels, self.sample_size, self.sample_size)
													__a		:		int									=       jnp.zeros(_a						,     dtype=jnp.floataa							)
													__a		:		Optional[Any]									=       jnp.ones((1,)						,     dtype=jnp.intaa							)
													__a		:		List[str]									=       jnp.zeros((1, 1, self.cross_attention_dim)						,     dtype=jnp.floataa							)
													__a		:		str									=       (1, 3, self.sample_size * 8, self.sample_size * 8)
													__a		:		str									=       jnp.zeros(_a						,     dtype=jnp.floataa							)

													__a							,     __a		:		Optional[int]									=       jax.random.split(_a							)
													__a		:		Dict									=       {"""params""": params_rng, """dropout""": dropout_rng}

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



						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													__a		:		Union[str, Any]									=       self.block_out_channels
													__a		:		Union[str, Any]									=       block_out_channels[0] * 4

													# 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.
													__a		:		Union[str, Any]									=       self.num_attention_heads or self.attention_head_dim

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

													# time
													__a		:		Optional[int]									=       FlaxTimesteps(
													    block_out_channels[0]						,     flip_sin_to_cos=self.flip_sin_to_cos						,     freq_shift=self.config.freq_shift							)
													__a		:		Optional[int]									=       FlaxTimestepEmbedding(_a						,     dtype=self.dtype							)

													__a		:		Union[str, Any]									=       FlaxControlNetConditioningEmbedding(
													    conditioning_embedding_channels=block_out_channels[0]						,     block_out_channels=self.conditioning_embedding_out_channels						,     )

													__a		:		Union[str, Any]									=       self.only_cross_attention
													if isinstance(_a						,     _a							):
																				__a		:		List[Any]									=       (only_cross_attention,) * len(self.down_block_types							)

													if isinstance(_a						,     _a							):
																				__a		:		int									=       (num_attention_heads,) * len(self.down_block_types							)

													# down
													__a		:		str									=       []
													__a		:		Union[str, Any]									=       []

													__a		:		Optional[Any]									=       block_out_channels[0]

													__a		:		Union[str, Any]									=       nn.Conv(
													    _a						,     kernel_size=(1, 1)						,     padding="""VALID"""						,     kernel_init=nn.initializers.zeros_init()						,     bias_init=nn.initializers.zeros_init()						,     dtype=self.dtype						,     )
													controlnet_down_blocks.append(_a							)

													for i, down_block_type in enumerate(self.down_block_types							):
																				__a		:		List[str]									=       output_channel
																				__a		:		Dict									=       block_out_channels[i]
																				__a		:		Optional[Any]									=       i == len(_a							) - 1

																				if down_block_type == "CrossAttnDownBlock2D":
																											__a		:		Tuple									=       FlaxCrossAttnDownBlockaD(
																											    in_channels=_a						,     out_channels=_a						,     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]						,     dtype=self.dtype						,     )
																				else:
																											__a		:		Tuple									=       FlaxDownBlockaD(
																											    in_channels=_a						,     out_channels=_a						,     dropout=self.dropout						,     num_layers=self.layers_per_block						,     add_downsample=not is_final_block						,     dtype=self.dtype						,     )

																				down_blocks.append(_a							)

																				for _ in range(self.layers_per_block							):
																											__a		:		List[str]									=       nn.Conv(
																											    _a						,     kernel_size=(1, 1)						,     padding="""VALID"""						,     kernel_init=nn.initializers.zeros_init()						,     bias_init=nn.initializers.zeros_init()						,     dtype=self.dtype						,     )
																											controlnet_down_blocks.append(_a							)

																				if not is_final_block:
																											__a		:		Optional[Any]									=       nn.Conv(
																											    _a						,     kernel_size=(1, 1)						,     padding="""VALID"""						,     kernel_init=nn.initializers.zeros_init()						,     bias_init=nn.initializers.zeros_init()						,     dtype=self.dtype						,     )
																											controlnet_down_blocks.append(_a							)

													__a		:		Optional[int]									=       down_blocks
													__a		:		int									=       controlnet_down_blocks

													# mid
													__a		:		Optional[int]									=       block_out_channels[-1]
													__a		:		Union[str, Any]									=       FlaxUNetMidBlockaDCrossAttn(
													    in_channels=_a						,     dropout=self.dropout						,     num_attention_heads=num_attention_heads[-1]						,     use_linear_projection=self.use_linear_projection						,     dtype=self.dtype						,     )

													__a		:		Tuple									=       nn.Conv(
													    _a						,     kernel_size=(1, 1)						,     padding="""VALID"""						,     kernel_init=nn.initializers.zeros_init()						,     bias_init=nn.initializers.zeros_init()						,     dtype=self.dtype						,     )





						def __call__(self						,     _lowercase						,     _lowercase						,     _lowercase						,     _lowercase						,     _lowercase = 1.0						,     _lowercase = True						,     _lowercase = False						,     ):





													'''simple docstring'''

													__a		:		Dict									=       self.controlnet_conditioning_channel_order
													if channel_order == "bgr":
																				__a		:		int									=       jnp.flip(_a						,     axis=1							)

													# 1. time
													if not isinstance(_a						,     jnp.ndarray							):
																				__a		:		Dict									=       jnp.array([timesteps]						,     dtype=jnp.intaa							)
													elif isinstance(_a						,     jnp.ndarray							) and len(timesteps.shape							) == 0:
																				__a		:		Tuple									=       timesteps.astype(dtype=jnp.floataa							)
																				__a		:		Union[str, Any]									=       jnp.expand_dims(_a						,     0							)

													__a		:		Optional[Any]									=       self.time_proj(_a							)
													__a		:		Dict									=       self.time_embedding(_a							)

													# 2. pre-process
													__a		:		Optional[int]									=       jnp.transpose(_a						,     (0, 2, 3, 1)							)
													__a		:		Union[str, Any]									=       self.conv_in(_a							)

													__a		:		Dict									=       jnp.transpose(_a						,     (0, 2, 3, 1)							)
													__a		:		str									=       self.controlnet_cond_embedding(_a							)
													sample += controlnet_cond

													# 3. down
													__a		:		Dict									=       (sample,)
													for down_block in self.down_blocks:
																				if isinstance(_a						,     _a							):
																											__a							,     __a		:		Dict									=       down_block(_a						,     _a						,     _a						,     deterministic=not train							)
																				else:
																											__a							,     __a		:		Optional[Any]									=       down_block(_a						,     _a						,     deterministic=not train							)
																				down_block_res_samples += res_samples

													# 4. mid
													__a		:		Any									=       self.mid_block(_a						,     _a						,     _a						,     deterministic=not train							)

													# 5. contronet blocks
													__a		:		List[Any]									=       ()
													for down_block_res_sample, controlnet_block in zip(_a						,     self.controlnet_down_blocks							):
																				__a		:		Tuple									=       controlnet_block(_a							)
																				controlnet_down_block_res_samples += (down_block_res_sample,)

													__a		:		str									=       controlnet_down_block_res_samples

													__a		:		Dict									=       self.controlnet_mid_block(_a							)

													# 6. scaling
													__a		:		Any									=       [sample * conditioning_scale for sample in down_block_res_samples]
													mid_block_res_sample *= conditioning_scale

													if not return_dict:
																				return (down_block_res_samples, mid_block_res_sample)

													return FlaxControlNetOutput(
													    down_block_res_samples=_a						,     mid_block_res_sample=_a							)











"""simple docstring"""

import unittest

from transformers import (
    MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
    TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
    TextClassificationPipeline,
    pipeline,
)
from transformers.testing_utils import is_pipeline_test, nested_simplify, require_tf, require_torch, slow

from .test_pipelines_common import ANY


# These 2 model types require different inputs than those of the usual text models.
lowercase__			   =  {"LayoutLMv2Config", "LayoutLMv3Config"}




@is_pipeline_test
class SCREAMING_SNAKE_CASE__					(		unittest.TestCase      ):
						_lowerCAmelCase							=					MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING
						_lowerCAmelCase							=					TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING

						if model_mapping is not None:
												_lowerCAmelCase							=					{config: model for config, model in model_mapping.items() if config.__name__ not in _TO_SKIP}
						if tf_model_mapping is not None:
												_lowerCAmelCase							=					{
												    config: model for config, model in tf_model_mapping.items() if config.__name__ not in _TO_SKIP
												}



						@require_torch
						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													__a		:		int									=       pipeline(
													    task="""text-classification"""						,     model="""hf-internal-testing/tiny-random-distilbert"""						,     framework="""pt"""							)

													__a		:		Tuple									=       text_classifier("""This is great !"""							)
													self.assertEqual(nested_simplify(_lowercase							)						,     [{"""label""": """LABEL_0""", """score""": 0.504}]							)

													__a		:		Optional[Any]									=       text_classifier("""This is great !"""						,     top_k=2							)
													self.assertEqual(
													    nested_simplify(_lowercase							)						,     [{"""label""": """LABEL_0""", """score""": 0.504}, {"""label""": """LABEL_1""", """score""": 0.496}]							)

													__a		:		int									=       text_classifier(["""This is great !""", """This is bad"""]						,     top_k=2							)
													self.assertEqual(
													    nested_simplify(_lowercase							)						,     [
													        [{"""label""": """LABEL_0""", """score""": 0.504}, {"""label""": """LABEL_1""", """score""": 0.496}],
													        [{"""label""": """LABEL_0""", """score""": 0.504}, {"""label""": """LABEL_1""", """score""": 0.496}],
													    ]						,     )

													__a		:		List[str]									=       text_classifier("""This is great !"""						,     top_k=1							)
													self.assertEqual(nested_simplify(_lowercase							)						,     [{"""label""": """LABEL_0""", """score""": 0.504}]							)

													# Legacy behavior
													__a		:		Optional[int]									=       text_classifier("""This is great !"""						,     return_all_scores=_lowercase							)
													self.assertEqual(nested_simplify(_lowercase							)						,     [{"""label""": """LABEL_0""", """score""": 0.504}]							)

													__a		:		Tuple									=       text_classifier("""This is great !"""						,     return_all_scores=_lowercase							)
													self.assertEqual(
													    nested_simplify(_lowercase							)						,     [[{"""label""": """LABEL_0""", """score""": 0.504}, {"""label""": """LABEL_1""", """score""": 0.496}]]							)

													__a		:		Any									=       text_classifier(["""This is great !""", """Something else"""]						,     return_all_scores=_lowercase							)
													self.assertEqual(
													    nested_simplify(_lowercase							)						,     [
													        [{"""label""": """LABEL_0""", """score""": 0.504}, {"""label""": """LABEL_1""", """score""": 0.496}],
													        [{"""label""": """LABEL_0""", """score""": 0.504}, {"""label""": """LABEL_1""", """score""": 0.496}],
													    ]						,     )

													__a		:		Union[str, Any]									=       text_classifier(["""This is great !""", """Something else"""]						,     return_all_scores=_lowercase							)
													self.assertEqual(
													    nested_simplify(_lowercase							)						,     [
													        {"""label""": """LABEL_0""", """score""": 0.504},
													        {"""label""": """LABEL_0""", """score""": 0.504},
													    ]						,     )



						@require_torch
						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													import torch

													__a		:		Any									=       pipeline(
													    task="""text-classification"""						,     model="""hf-internal-testing/tiny-random-distilbert"""						,     framework="""pt"""						,     device=torch.device("""cpu"""							)						,     )

													__a		:		Optional[int]									=       text_classifier("""This is great !"""							)
													self.assertEqual(nested_simplify(_lowercase							)						,     [{"""label""": """LABEL_0""", """score""": 0.504}]							)



						@require_tf
						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													__a		:		List[Any]									=       pipeline(
													    task="""text-classification"""						,     model="""hf-internal-testing/tiny-random-distilbert"""						,     framework="""tf"""							)

													__a		:		List[str]									=       text_classifier("""This is great !"""							)
													self.assertEqual(nested_simplify(_lowercase							)						,     [{"""label""": """LABEL_0""", """score""": 0.504}]							)



						@slow
						@require_torch
						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													__a		:		Tuple									=       pipeline("""text-classification"""							)

													__a		:		Tuple									=       text_classifier("""This is great !"""							)
													self.assertEqual(nested_simplify(_lowercase							)						,     [{"""label""": """POSITIVE""", """score""": 1.0}]							)
													__a		:		Optional[int]									=       text_classifier("""This is bad !"""							)
													self.assertEqual(nested_simplify(_lowercase							)						,     [{"""label""": """NEGATIVE""", """score""": 1.0}]							)
													__a		:		Union[str, Any]									=       text_classifier("""Birds are a type of animal"""							)
													self.assertEqual(nested_simplify(_lowercase							)						,     [{"""label""": """POSITIVE""", """score""": 0.988}]							)



						@slow
						@require_tf
						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													__a		:		List[str]									=       pipeline("""text-classification"""						,     framework="""tf"""							)

													__a		:		str									=       text_classifier("""This is great !"""							)
													self.assertEqual(nested_simplify(_lowercase							)						,     [{"""label""": """POSITIVE""", """score""": 1.0}]							)
													__a		:		Tuple									=       text_classifier("""This is bad !"""							)
													self.assertEqual(nested_simplify(_lowercase							)						,     [{"""label""": """NEGATIVE""", """score""": 1.0}]							)
													__a		:		str									=       text_classifier("""Birds are a type of animal"""							)
													self.assertEqual(nested_simplify(_lowercase							)						,     [{"""label""": """POSITIVE""", """score""": 0.988}]							)



						def     lowerCAmelCase__(self						,     _lowercase						,     _lowercase						,     _lowercase							):





													'''simple docstring'''

													__a		:		Dict									=       TextClassificationPipeline(model=_lowercase						,     tokenizer=_lowercase							)
													return text_classifier, ["HuggingFace is in", "This is another test"]





						def     lowerCAmelCase__(self						,     _lowercase						,     _lowercase							):





													'''simple docstring'''

													__a		:		List[str]									=       text_classifier.model
													# Small inputs because BartTokenizer tiny has maximum position embeddings = 22
													__a		:		Union[str, Any]									=       """HuggingFace is in"""
													__a		:		List[str]									=       text_classifier(_lowercase							)

													self.assertEqual(nested_simplify(_lowercase							)						,     [{"""label""": ANY(_lowercase							), """score""": ANY(_lowercase							)}]							)
													self.assertTrue(outputs[0]["""label"""] in model.config.idalabel.values()							)

													__a		:		Optional[int]									=       ["""HuggingFace is in """, """Paris is in France"""]
													__a		:		Dict									=       text_classifier(_lowercase							)
													self.assertEqual(
													    nested_simplify(_lowercase							)						,     [{"""label""": ANY(_lowercase							), """score""": ANY(_lowercase							)}, {"""label""": ANY(_lowercase							), """score""": ANY(_lowercase							)}]						,     )
													self.assertTrue(outputs[0]["""label"""] in model.config.idalabel.values()							)
													self.assertTrue(outputs[1]["""label"""] in model.config.idalabel.values()							)

													# Forcing to get all results with `top_k=None`
													# This is NOT the legacy format
													__a		:		Dict									=       text_classifier(_lowercase						,     top_k=_lowercase							)
													__a		:		Dict									=       len(model.config.idalabel.values()							)
													self.assertEqual(
													    nested_simplify(_lowercase							)						,     [[{"""label""": ANY(_lowercase							), """score""": ANY(_lowercase							)}] * N, [{"""label""": ANY(_lowercase							), """score""": ANY(_lowercase							)}] * N]						,     )

													__a		:		Dict									=       {"""text""": """HuggingFace is in """, """text_pair""": """Paris is in France"""}
													__a		:		Any									=       text_classifier(_lowercase							)
													self.assertEqual(
													    nested_simplify(_lowercase							)						,     {"""label""": ANY(_lowercase							), """score""": ANY(_lowercase							)}						,     )
													self.assertTrue(outputs["""label"""] in model.config.idalabel.values()							)

													# This might be used a text pair, but tokenizer + pipe interaction
													# makes it hard to understand that it's not using the pair properly
													# https://github.com/huggingface/transformers/issues/17305
													# We disabled this usage instead as it was outputting wrong outputs.
													__a		:		Dict									=       [["""HuggingFace is in """, """Paris is in France"""]]
													with self.assertRaises(_lowercase							):
																				text_classifier(_lowercase							)

													# This used to be valid for doing text pairs
													# We're keeping it working because of backward compatibility
													__a		:		Optional[int]									=       text_classifier([[["""HuggingFace is in """, """Paris is in France"""]]]							)
													self.assertEqual(
													    nested_simplify(_lowercase							)						,     [{"""label""": ANY(_lowercase							), """score""": ANY(_lowercase							)}]						,     )
													self.assertTrue(outputs[0]["""label"""] in model.config.idalabel.values()							)

