import logging
from pathlib import Path

import numpy as np
import pytorch_lightning as pl
import torch
from pytorch_lightning.callbacks import EarlyStopping, ModelCheckpoint
from pytorch_lightning.utilities import rank_zero_only
from utils_rag import save_json







def 			A__      (	snake_case_ :		List[str]      ):
 SCREAMING_SNAKE_CASE__:   Optional[int]=      filter(lambda snake_case_      : p.requires_grad       ,				model.parameters()      )
 SCREAMING_SNAKE_CASE__:   Dict=      sum([np.prod(p.size()      ) for p in model_parameters]      )
 return params


lowercase_     :   str 				=				logging.getLogger(__name__)







def 			A__      (	snake_case_ :		Any       ,				snake_case_ :		List[str]      ):
 if metric == "rouge2":
  SCREAMING_SNAKE_CASE__:   int=      '''{val_avg_rouge2:.4f}-{step_count}'''
 elif metric == "bleu":
  SCREAMING_SNAKE_CASE__:   List[Any]=      '''{val_avg_bleu:.4f}-{step_count}'''
 elif metric == "em":
  SCREAMING_SNAKE_CASE__:   Union[str, Any]=      '''{val_avg_em:.4f}-{step_count}'''
 elif metric == "loss":
  SCREAMING_SNAKE_CASE__:   Union[str, Any]=      '''{val_avg_loss:.4f}-{step_count}'''
 else:
  raise NotImplementedError(
      F'seq2seq callbacks only support rouge2 and bleu, got {metric}, You can make your own by adding to this'
      ''' function.'''      )

 SCREAMING_SNAKE_CASE__:   Union[str, Any]=      ModelCheckpoint(
     dirpath=snake_case_       ,				filename=snake_case_       ,				monitor=F'val_{metric}'       ,				mode='''max'''       ,				save_top_k=1       ,				every_n_epochs=1       ,				)

 return checkpoint_callback







def 			A__      (	snake_case_ :		int       ,				snake_case_ :		List[str]      ):
 return EarlyStopping(
     monitor=F'val_{metric}'       ,				mode='''min''' if '''loss''' in metric else '''max'''       ,				patience=snake_case_       ,				verbose=snake_case_       ,				)






class 		_lowerCamelCase		(   pl.Callback    ):
    def 						UpperCamelCase_				(  self , lowerCAmelCase , lowerCAmelCase   )       ->					Optional[int]:
     SCREAMING_SNAKE_CASE__:   Any=      {f'lr_group_{i}': param['''lr'''] for i, param in enumerate(pl_module.trainer.optimizers[0].param_groups   )}
     pl_module.logger.log_metrics(lowerCAmelCase   )
    @rank_zero_only
    def 						UpperCamelCase_				(  self , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase=True   )       ->					None:
     logger.info(f'***** {type_path} results at step {trainer.global_step:05d} *****'   )
     SCREAMING_SNAKE_CASE__:   Dict=      trainer.callback_metrics
     trainer.logger.log_metrics({k: v for k, v in metrics.items() if k not in ['''log''', '''progress_bar''', '''preds''']}   )
     # Log results
     SCREAMING_SNAKE_CASE__:   Union[str, Any]=      Path(pl_module.hparams.output_dir   )
     if type_path == "test":
      SCREAMING_SNAKE_CASE__:   str=      od / '''test_results.txt'''
      SCREAMING_SNAKE_CASE__:   int=      od / '''test_generations.txt'''
     else:
      # this never gets hit. I prefer not to save intermediate generations, and results are in metrics.json
      # If people want this it will be easy enough to add back.
      SCREAMING_SNAKE_CASE__:   int=      od / f'{type_path}_results/{trainer.global_step:05d}.txt'
      SCREAMING_SNAKE_CASE__:   Dict=      od / f'{type_path}_generations/{trainer.global_step:05d}.txt'
      results_file.parent.mkdir(exist_ok=lowerCAmelCase   )
      generations_file.parent.mkdir(exist_ok=lowerCAmelCase   )
     with open(lowerCAmelCase , '''a+'''   ) as writer:
      for key in sorted(lowerCAmelCase   ):
       if key in ["log", "progress_bar", "preds"]:
        continue
       SCREAMING_SNAKE_CASE__:   Tuple=      metrics[key]
       if isinstance(lowerCAmelCase , torch.Tensor   ):
        SCREAMING_SNAKE_CASE__:   List[str]=      val.item()
       SCREAMING_SNAKE_CASE__:   int=      f'{key}: {val:.6f}\n'
       writer.write(lowerCAmelCase   )

     if not save_generations:
      return

     if "preds" in metrics:
      SCREAMING_SNAKE_CASE__:   Optional[Any]=      '''\n'''.join(metrics['''preds''']   )
      generations_file.open('''w+'''   ).write(lowerCAmelCase   )
    @rank_zero_only
    def 						UpperCamelCase_				(  self , lowerCAmelCase , lowerCAmelCase   )       ->					Union[str, Any]:
     try:
      SCREAMING_SNAKE_CASE__:   Dict=      pl_module.model.model.num_parameters()
     except AttributeError:
      SCREAMING_SNAKE_CASE__:   Optional[int]=      pl_module.model.num_parameters()

     SCREAMING_SNAKE_CASE__:   Tuple=      count_trainable_parameters(lowerCAmelCase   )
     # mp stands for million parameters
     trainer.logger.log_metrics({'''n_params''': npars, '''mp''': npars / 1e6, '''grad_mp''': n_trainable_pars / 1e6}   )
    @rank_zero_only
    def 						UpperCamelCase_				(  self , lowerCAmelCase , lowerCAmelCase   )       ->					List[str]:
     save_json(pl_module.metrics , pl_module.metrics_save_path   )
     return self._write_logs(lowerCAmelCase , lowerCAmelCase , '''test'''   )



    @rank_zero_only
    def 						UpperCamelCase_				(  self , lowerCAmelCase , lowerCAmelCase   )       ->					int:
     save_json(pl_module.metrics , pl_module.metrics_save_path   )
     # Uncommenting this will save val generations
     # return self._write_logs(trainer, pl_module, "valid")


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 		_lowerCamelCase		(   UpperCamelCase_    ):
    __a  =      42
    __a  =      42






class 		_lowerCamelCase		(   nn.Module    ):
    __a  =      42
    __a  =      (16, 32, 96, 256)
    __a  =      jnp.floataa
    def 						UpperCamelCase_				(  self   )       ->					Optional[int]:
     SCREAMING_SNAKE_CASE__:   List[str]=      nn.Conv(
         self.block_out_channels[0] , kernel_size=(3, 3) , padding=((1, 1), (1, 1)) , dtype=self.dtype , )

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

     SCREAMING_SNAKE_CASE__:   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 , lowerCAmelCase   )       ->					Optional[Any]:
     SCREAMING_SNAKE_CASE__:   Dict=      self.conv_in(lowerCAmelCase   )
     SCREAMING_SNAKE_CASE__:   Optional[int]=      nn.silu(lowerCAmelCase   )

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

     SCREAMING_SNAKE_CASE__:   Any=      self.conv_out(lowerCAmelCase   )

     return embedding






@flax_register_to_config
class 		_lowerCamelCase		(   nn.Module    ,     UpperCamelCase_    ,     UpperCamelCase_    ):
    __a  =      32
    __a  =      4
    __a  =      (
        "CrossAttnDownBlock2D",
        "CrossAttnDownBlock2D",
        "CrossAttnDownBlock2D",
        "DownBlock2D",
    )
    __a  =      False
    __a  =      (320, 640, 1280, 1280)
    __a  =      2
    __a  =      8
    __a  =      None
    __a  =      1280
    __a  =      0.0
    __a  =      False
    __a  =      jnp.floataa
    __a  =      True
    __a  =      0
    __a  =      "rgb"
    __a  =      (16, 32, 96, 256)
    def 						UpperCamelCase_				(  self , lowerCAmelCase   )       ->					FrozenDict:
     # init input tensors
     SCREAMING_SNAKE_CASE__:   Any=      (1, self.in_channels, self.sample_size, self.sample_size)
     SCREAMING_SNAKE_CASE__:   Union[str, Any]=      jnp.zeros(lowerCAmelCase , dtype=jnp.floataa   )
     SCREAMING_SNAKE_CASE__:   Union[str, Any]=      jnp.ones((1,) , dtype=jnp.intaa   )
     SCREAMING_SNAKE_CASE__:   Optional[Any]=      jnp.zeros((1, 1, self.cross_attention_dim) , dtype=jnp.floataa   )
     SCREAMING_SNAKE_CASE__:   int=      (1, 3, self.sample_size * 8, self.sample_size * 8)
     SCREAMING_SNAKE_CASE__:   Optional[int]=      jnp.zeros(lowerCAmelCase , dtype=jnp.floataa   )

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

     return self.init(lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase   )["params"]
    def 						UpperCamelCase_				(  self   )       ->					Optional[Any]:
     SCREAMING_SNAKE_CASE__:   Dict=      self.block_out_channels
     SCREAMING_SNAKE_CASE__:   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.
     SCREAMING_SNAKE_CASE__:   int=      self.num_attention_heads or self.attention_head_dim

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

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

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

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

     if isinstance(lowerCAmelCase , lowerCAmelCase   ):
      SCREAMING_SNAKE_CASE__:   Dict=      (num_attention_heads,) * len(self.down_block_types   )

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

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

     SCREAMING_SNAKE_CASE__:   Dict=      nn.Conv(
         lowerCAmelCase , 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(lowerCAmelCase   )

     for i, down_block_type in enumerate(self.down_block_types   ):
      SCREAMING_SNAKE_CASE__:   Tuple=      output_channel
      SCREAMING_SNAKE_CASE__:   Any=      block_out_channels[i]
      SCREAMING_SNAKE_CASE__:   Optional[Any]=      i == len(lowerCAmelCase   ) - 1

      if down_block_type == "CrossAttnDownBlock2D":
       SCREAMING_SNAKE_CASE__:   Any=      FlaxCrossAttnDownBlockaD(
           in_channels=lowerCAmelCase , out_channels=lowerCAmelCase , 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:
       SCREAMING_SNAKE_CASE__:   Union[str, Any]=      FlaxDownBlockaD(
           in_channels=lowerCAmelCase , out_channels=lowerCAmelCase , dropout=self.dropout , num_layers=self.layers_per_block , add_downsample=not is_final_block , dtype=self.dtype , )

      down_blocks.append(lowerCAmelCase   )

      for _ in range(self.layers_per_block   ):
       SCREAMING_SNAKE_CASE__:   str=      nn.Conv(
           lowerCAmelCase , 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(lowerCAmelCase   )

      if not is_final_block:
       SCREAMING_SNAKE_CASE__:   str=      nn.Conv(
           lowerCAmelCase , 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(lowerCAmelCase   )

     SCREAMING_SNAKE_CASE__:   int=      down_blocks
     SCREAMING_SNAKE_CASE__:   int=      controlnet_down_blocks

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

     SCREAMING_SNAKE_CASE__:   str=      nn.Conv(
         lowerCAmelCase , kernel_size=(1, 1) , padding='''VALID''' , kernel_init=nn.initializers.zeros_init() , bias_init=nn.initializers.zeros_init() , dtype=self.dtype , )



    def __call__(  self , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase = 1.0 , lowerCAmelCase = True , lowerCAmelCase = False , )       ->					Union[FlaxControlNetOutput, Tuple]:
     SCREAMING_SNAKE_CASE__:   Dict=      self.controlnet_conditioning_channel_order
     if channel_order == "bgr":
      SCREAMING_SNAKE_CASE__:   Optional[Any]=      jnp.flip(lowerCAmelCase , axis=1   )

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

     SCREAMING_SNAKE_CASE__:   Dict=      self.time_proj(lowerCAmelCase   )
     SCREAMING_SNAKE_CASE__:   Dict=      self.time_embedding(lowerCAmelCase   )

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

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

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

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

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

     SCREAMING_SNAKE_CASE__:   Optional[Any]=      controlnet_down_block_res_samples

     SCREAMING_SNAKE_CASE__:   Optional[int]=      self.controlnet_mid_block(lowerCAmelCase   )

     # 6. scaling
     SCREAMING_SNAKE_CASE__:   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=lowerCAmelCase , mid_block_res_sample=lowerCAmelCase   )

