
"""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 			__magic_name__       (       UpperCamelCase_ ):
       UpperCamelCase_   =		42
       UpperCamelCase_   =		42


class 			__magic_name__       (       nn.Module ):
       UpperCamelCase_   =		42
       UpperCamelCase_   =		(16, 32, 96, 256)
       UpperCamelCase_   =		jnp.floataa
       def 							lowercase_							(     self       )     ->	Optional[int]:





         """simple docstring"""




         _lowercase:			List[str]       =     nn.Conv(
             self.block_out_channels[0] ,				kernel_size=(3, 3) ,				padding=((1, 1), (1, 1)) ,				dtype=self.dtype ,				)

         _lowercase:			Optional[int]       =     []
         for i in range(len(self.block_out_channels       ) - 1       ):
           _lowercase:			str       =     self.block_out_channels[i]
           _lowercase:			List[str]       =     self.block_out_channels[i + 1]
           _lowercase:			List[Any]       =     nn.Conv(
               A_ ,				kernel_size=(3, 3) ,				padding=((1, 1), (1, 1)) ,				dtype=self.dtype ,				)
           blocks.append(A_       )
           _lowercase:			str       =     nn.Conv(
               A_ ,				kernel_size=(3, 3) ,				strides=(2, 2) ,				padding=((1, 1), (1, 1)) ,				dtype=self.dtype ,				)
           blocks.append(A_       )
         _lowercase:			str       =     blocks

         _lowercase:			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 ,				A_       )     ->	Optional[Any]:





         """simple docstring"""




         _lowercase:			Dict       =     self.conv_in(A_       )
         _lowercase:			Optional[int]       =     nn.silu(A_       )

         for block in self.blocks:
           _lowercase:			Optional[int]       =     block(A_       )
           _lowercase:			Optional[Any]       =     nn.silu(A_       )

         _lowercase:			Any       =     self.conv_out(A_       )

         return embedding


@flax_register_to_config
class 			__magic_name__       (       nn.Module   ,					UpperCamelCase_   ,					UpperCamelCase_ ):
       UpperCamelCase_   =		32
       UpperCamelCase_   =		4
       UpperCamelCase_   =		(
           '''CrossAttnDownBlock2D''',
           '''CrossAttnDownBlock2D''',
           '''CrossAttnDownBlock2D''',
           '''DownBlock2D''',
       )
       UpperCamelCase_   =		False
       UpperCamelCase_   =		(320, 640, 1280, 1280)
       UpperCamelCase_   =		2
       UpperCamelCase_   =		8
       UpperCamelCase_   =		None
       UpperCamelCase_   =		1280
       UpperCamelCase_   =		0.0
       UpperCamelCase_   =		False
       UpperCamelCase_   =		jnp.floataa
       UpperCamelCase_   =		True
       UpperCamelCase_   =		0
       UpperCamelCase_   =		'''rgb'''
       UpperCamelCase_   =		(16, 32, 96, 256)
       def 							lowercase_							(     self ,				A_       )     ->	FrozenDict:





         """simple docstring"""




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

         _lowercase:			Any       =     jax.random.split(A_       )
         _lowercase:			Optional[int]       =     {'''params''': params_rng, '''dropout''': dropout_rng}

         return self.init(A_ ,				A_ ,				A_ ,				A_ ,				A_       )["params"]
       def 							lowercase_							(     self       )     ->	Optional[Any]:





         """simple docstring"""




         _lowercase:			Dict       =     self.block_out_channels
         _lowercase:			str       =     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.
         _lowercase:			int       =     self.num_attention_heads or self.attention_head_dim

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

         # time
         _lowercase:			str       =     FlaxTimesteps(
             block_out_channels[0] ,				flip_sin_to_cos=self.flip_sin_to_cos ,				freq_shift=self.config.freq_shift       )
         _lowercase:			Optional[Any]       =     FlaxTimestepEmbedding(A_ ,				dtype=self.dtype       )

         _lowercase:			str       =     FlaxControlNetConditioningEmbedding(
             conditioning_embedding_channels=block_out_channels[0] ,				block_out_channels=self.conditioning_embedding_out_channels ,				)

         _lowercase:			List[str]       =     self.only_cross_attention
         if isinstance(A_ ,				A_       ):
           _lowercase:			str       =     (only_cross_attention,) * len(self.down_block_types       )

         if isinstance(A_ ,				A_       ):
           _lowercase:			Dict       =     (num_attention_heads,) * len(self.down_block_types       )

         # down
         _lowercase:			Optional[int]       =     []
         _lowercase:			List[str]       =     []

         _lowercase:			Union[str, Any]       =     block_out_channels[0]

         _lowercase:			Dict       =     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       ):
           _lowercase:			Tuple       =     output_channel
           _lowercase:			Any       =     block_out_channels[i]
           _lowercase:			Optional[Any]       =     i == len(A_       ) - 1

           if down_block_type == "CrossAttnDownBlock2D":
             _lowercase:			Any       =     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:
             _lowercase:			Union[str, Any]       =     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       ):
             _lowercase:			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:
             _lowercase:			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_       )

         _lowercase:			int       =     down_blocks
         _lowercase:			int       =     controlnet_down_blocks

         # mid
         _lowercase:			Optional[int]       =     block_out_channels[-1]
         _lowercase:			int       =     FlaxUNetMidBlockaDCrossAttn(
             in_channels=A_ ,				dropout=self.dropout ,				num_attention_heads=num_attention_heads[-1] ,				use_linear_projection=self.use_linear_projection ,				dtype=self.dtype ,				)

         _lowercase:			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 ,				)



       def __call__(     self ,				A_ ,				A_ ,				A_ ,				A_ ,				A_ = 1.0 ,				A_ = True ,				A_ = False ,				)     ->	Union[FlaxControlNetOutput, Tuple]:





         """simple docstring"""




         _lowercase:			Dict       =     self.controlnet_conditioning_channel_order
         if channel_order == "bgr":
           _lowercase:			Optional[Any]       =     jnp.flip(A_ ,				axis=1       )

         # 1. time
         if not isinstance(A_ ,				jnp.ndarray       ):
           _lowercase:			Optional[Any]       =     jnp.array([timesteps] ,				dtype=jnp.intaa       )
         elif isinstance(A_ ,				jnp.ndarray       ) and len(timesteps.shape       ) == 0:
           _lowercase:			int       =     timesteps.astype(dtype=jnp.floataa       )
           _lowercase:			Any       =     jnp.expand_dims(A_ ,				0       )

         _lowercase:			Dict       =     self.time_proj(A_       )
         _lowercase:			Dict       =     self.time_embedding(A_       )

         # 2. pre-process
         _lowercase:			int       =     jnp.transpose(A_ ,				(0, 2, 3, 1)       )
         _lowercase:			str       =     self.conv_in(A_       )

         _lowercase:			Optional[Any]       =     jnp.transpose(A_ ,				(0, 2, 3, 1)       )
         _lowercase:			str       =     self.controlnet_cond_embedding(A_       )
         sample += controlnet_cond

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

         # 4. mid
         _lowercase:			Optional[Any]       =     self.mid_block(A_ ,				A_ ,				A_ ,				deterministic=not train       )

         # 5. contronet blocks
         _lowercase:			Optional[int]       =     ()
         for down_block_res_sample, controlnet_block in zip(A_ ,				self.controlnet_down_blocks       ):
           _lowercase:			List[str]       =     controlnet_block(A_       )
           controlnet_down_block_res_samples += (down_block_res_sample,)

         _lowercase:			Optional[Any]       =     controlnet_down_block_res_samples

         _lowercase:			Optional[int]       =     self.controlnet_mid_block(A_       )

         # 6. scaling
         _lowercase:			Tuple       =     [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 pandas as pd
from matplotlib import pyplot as plt
from sklearn.linear_model import LinearRegression

# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split

# Fitting Polynomial Regression to the dataset
from sklearn.preprocessing import PolynomialFeatures

# Importing the dataset
A__   :					Tuple							=   pd.read_csv(
    'https://s3.us-west-2.amazonaws.com/public.gamelab.fun/dataset/'
    'position_salaries.csv'
)
A__   :					Optional[int]							=   dataset.iloc[:, 1:2].values
A__   :					Union[str, Any]							=   dataset.iloc[:, 2].values


A__							, A__							, A__							, A__   :					Dict							=   train_test_split(X, y, test_size=0.2, random_state=0)


A__   :					Union[str, Any]							=   PolynomialFeatures(degree=4)
A__   :					List[Any]							=   poly_reg.fit_transform(X)
A__   :					Dict							=   LinearRegression()
pol_reg.fit(X_poly, y)




def  _lowerCAmelCase		(		):






  """simple docstring"""



  plt.scatter(_UpperCamelCase ,				_UpperCamelCase ,				color='''red'''						)
  plt.plot(_UpperCamelCase ,				pol_reg.predict(poly_reg.fit_transform(_UpperCamelCase						)						) ,				color='''blue'''						)
  plt.title('''Truth or Bluff (Linear Regression)'''						)
  plt.xlabel('''Position level'''						)
  plt.ylabel('''Salary'''						)
  plt.show()


if __name__ == "__main__":
  viz_polymonial()

  # Predicting a new result with Polymonial Regression
  pol_reg.predict(poly_reg.fit_transform([[5.5]]))
  # output should be 132148.43750003
