





import argparse
import glob
import importlib.util
import os
import re

import black
from doc_builder.style_doc import style_docstrings_in_code


# All paths are set with the intent you should run this script from the root of the repo with the command
# python utils/check_copies.py
_a 						=      "src/diffusers"
_a 						=      "."


# This is to make sure the diffusers module imported is the one in the repo.
_a 						=      importlib.util.spec_from_file_location(
    "diffusers",
    os.path.join(DIFFUSERS_PATH, "__init__.py"),
    submodule_search_locations=[DIFFUSERS_PATH],
)
_a 						=      spec.loader.load_module()





def lowerCAmelCase__(__snake_case       ,__snake_case							)				->      int:







	'''simple docstring'''







	return line.startswith(__snake_case							) or len(__snake_case							) <= 1 or re.search(R'''^\s*\)(\s*->.*:|:)\s*$'''       ,__snake_case							) is not None





def lowerCAmelCase__(__snake_case							)				->      Tuple:







	'''simple docstring'''







	lowerCamelCase__								=		object_name.split('''.'''							)
	lowerCamelCase__								=		0

	# First let's find the module where our object lives.
	lowerCamelCase__								=		parts[i]
	while i < len(__snake_case							) and not os.path.isfile(os.path.join(__snake_case       ,F'{module}.py'							)							):
		i += 1
		if i < len(__snake_case							):
			lowerCamelCase__								=		os.path.join(__snake_case       ,parts[i]							)
	if i >= len(__snake_case							):
		raise ValueError(F'`object_name` should begin with the name of a module of diffusers but got {object_name}.'							)

	with open(os.path.join(__snake_case       ,F'{module}.py'							)       ,'''r'''       ,encoding='''utf-8'''       ,newline='''\n'''							) as f:
		lowerCamelCase__								=		f.readlines()

	# Now let's find the class / func in the code!
	lowerCamelCase__								=		''''''
	lowerCamelCase__								=		0
	for name in parts[i + 1 :]:
		while (
		    line_index < len(__snake_case							) and re.search(RF'^{indent}(class|def)\s+{name}(\(|\:)'       ,lines[line_index]							) is None
		):
			line_index += 1
		indent += "    "
		line_index += 1

	if line_index >= len(__snake_case							):
		raise ValueError(F' {object_name} does not match any function or class in {module}.'							)

	# We found the beginning of the class / func, now let's find the end (when the indent diminishes).
	lowerCamelCase__								=		line_index
	while line_index < len(__snake_case							) and _should_continue(lines[line_index]       ,__snake_case							):
		line_index += 1
	# Clean up empty lines at the end (if any).
	while len(lines[line_index - 1]							) <= 1:
		line_index -= 1

	lowerCamelCase__								=		lines[start_index:line_index]
	return "".join(__snake_case							)


_a 						=      re.compile(r"^(\s*)#\s*Copied from\s+diffusers\.(\S+\.\S+)\s*($|\S.*$)")
_a 						=      re.compile(r"^\s*(\S+)->(\S+)(\s+.*|$)")
_a 						=      re.compile(r"<FILL\s+[^>]*>")





def lowerCAmelCase__(__snake_case							)				->      Union[str, Any]:







	'''simple docstring'''







	lowerCamelCase__								=		code.split('''\n'''							)
	lowerCamelCase__								=		0
	while idx < len(__snake_case							) and len(lines[idx]							) == 0:
		idx += 1
	if idx < len(__snake_case							):
		return re.search(R'''^(\s*)\S'''       ,lines[idx]							).groups()[0]
	return ""





def lowerCAmelCase__(__snake_case							)				->      List[str]:







	'''simple docstring'''







	lowerCamelCase__								=		len(get_indent(__snake_case							)							) > 0
	if has_indent:
		lowerCamelCase__								=		F'class Bla:\n{code}'
	lowerCamelCase__								=		black.Mode(target_versions={black.TargetVersion.PYaa}       ,line_length=119       ,preview=__snake_case							)
	lowerCamelCase__								=		black.format_str(__snake_case       ,mode=__snake_case							)
	lowerCamelCase__				,	lowerCamelCase__								=		style_docstrings_in_code(__snake_case							)
	return result[len('''class Bla:\n'''							) :] if has_indent else result





def lowerCAmelCase__(__snake_case       ,__snake_case=False							)				->      Tuple:







	'''simple docstring'''







	with open(__snake_case       ,'''r'''       ,encoding='''utf-8'''       ,newline='''\n'''							) as f:
		lowerCamelCase__								=		f.readlines()
	lowerCamelCase__								=		[]
	lowerCamelCase__								=		0
	# Not a for loop cause `lines` is going to change (if `overwrite=True`).
	while line_index < len(__snake_case							):
		lowerCamelCase__								=		_re_copy_warning.search(lines[line_index]							)
		if search is None:
			line_index += 1
			continue

		# There is some copied code here, let's retrieve the original.
		lowerCamelCase__				,	lowerCamelCase__				,	lowerCamelCase__								=		search.groups()
		lowerCamelCase__								=		find_code_in_diffusers(__snake_case							)
		lowerCamelCase__								=		get_indent(__snake_case							)

		lowerCamelCase__								=		line_index + 1 if indent == theoretical_indent else line_index + 2
		lowerCamelCase__								=		theoretical_indent
		lowerCamelCase__								=		start_index

		# Loop to check the observed code, stop when indentation diminishes or if we see a End copy comment.
		lowerCamelCase__								=		True
		while line_index < len(__snake_case							) and should_continue:
			line_index += 1
			if line_index >= len(__snake_case							):
				break
			lowerCamelCase__								=		lines[line_index]
			lowerCamelCase__								=		_should_continue(__snake_case       ,__snake_case							) and re.search(F'^{indent}# End copy'       ,__snake_case							) is None
		# Clean up empty lines at the end (if any).
		while len(lines[line_index - 1]							) <= 1:
			line_index -= 1

		lowerCamelCase__								=		lines[start_index:line_index]
		lowerCamelCase__								=		''''''.join(__snake_case							)

		# Remove any nested `Copied from` comments to avoid circular copies
		lowerCamelCase__								=		[line for line in theoretical_code.split('''\n'''							) if _re_copy_warning.search(__snake_case							) is None]
		lowerCamelCase__								=		'''\n'''.join(__snake_case							)

		# Before comparing, use the `replace_pattern` on the original code.
		if len(__snake_case							) > 0:
			lowerCamelCase__								=		replace_pattern.replace('''with'''       ,''''''							).split(''','''							)
			lowerCamelCase__								=		[_re_replace_pattern.search(__snake_case							) for p in patterns]
			for pattern in patterns:
				if pattern is None:
					continue
				lowerCamelCase__				,	lowerCamelCase__				,	lowerCamelCase__								=		pattern.groups()
				lowerCamelCase__								=		re.sub(__snake_case       ,__snake_case       ,__snake_case							)
				if option.strip() == "all-casing":
					lowerCamelCase__								=		re.sub(obja.lower()       ,obja.lower()       ,__snake_case							)
					lowerCamelCase__								=		re.sub(obja.upper()       ,obja.upper()       ,__snake_case							)

            # Blackify after replacement. To be able to do that, we need the header (class or function definition)
            # from the previous line
			lowerCamelCase__								=		blackify(lines[start_index - 1] + theoretical_code							)
			lowerCamelCase__								=		theoretical_code[len(lines[start_index - 1]							) :]

		# Test for a diff and act accordingly.
		if observed_code != theoretical_code:
			diffs.append([object_name, start_index]							)
			if overwrite:
				lowerCamelCase__								=		lines[:start_index] + [theoretical_code] + lines[line_index:]
				lowerCamelCase__								=		start_index + 1

	if overwrite and len(__snake_case							) > 0:
		# Warn the user a file has been modified.
		print(F'Detected changes, rewriting {filename}.'							)
		with open(__snake_case       ,'''w'''       ,encoding='''utf-8'''       ,newline='''\n'''							) as f:
			f.writelines(__snake_case							)
	return diffs





def lowerCAmelCase__(__snake_case = False							)				->      Optional[Any]:







	'''simple docstring'''







	lowerCamelCase__								=		glob.glob(os.path.join(__snake_case       ,'''**/*.py'''							)       ,recursive=__snake_case							)
	lowerCamelCase__								=		[]
	for filename in all_files:
		lowerCamelCase__								=		is_copy_consistent(__snake_case       ,__snake_case							)
		diffs += [F'- {filename}: copy does not match {d[0]} at line {d[1]}' for d in new_diffs]
	if not overwrite and len(__snake_case							) > 0:
		lowerCamelCase__								=		'''\n'''.join(__snake_case							)
		raise Exception(
		    '''Found the following copy inconsistencies:\n'''
		    + diff
		    + '''\nRun `make fix-copies` or `python utils/check_copies.py --fix_and_overwrite` to fix them.'''							)


if __name__ == "__main__":
			_a 						=      argparse.ArgumentParser()
			parser.add_argument("--fix_and_overwrite", action="store_true", help="Whether to fix inconsistencies.")
			_a 						=      parser.parse_args()

			check_copies(args.fix_and_overwrite)










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 					__A    (    lowerCAmelCase ):

			'''simple docstring'''







			lowerCAmelCase_							     =						42
			lowerCAmelCase_							     =						42






class 					__A    (    nn.Module ):

			'''simple docstring'''







			lowerCAmelCase_							     =						42
			lowerCAmelCase_							     =						(16, 32, 96, 256)
			lowerCAmelCase_							     =						jnp.floataa





			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				lowerCamelCase__								=		nn.Conv(
				    self.block_out_channels[0]       ,  kernel_size=(3, 3)       ,  padding=((1, 1), (1, 1))       ,  dtype=self.dtype       ,  )

				lowerCamelCase__								=		[]
				for i in range(len(self.block_out_channels							) - 1							):
					lowerCamelCase__								=		self.block_out_channels[i]
					lowerCamelCase__								=		self.block_out_channels[i + 1]
					lowerCamelCase__								=		nn.Conv(
					    __lowerCAmelCase       ,  kernel_size=(3, 3)       ,  padding=((1, 1), (1, 1))       ,  dtype=self.dtype       ,  )
					blocks.append(__lowerCAmelCase							)
					lowerCamelCase__								=		nn.Conv(
					    __lowerCAmelCase       ,  kernel_size=(3, 3)       ,  strides=(2, 2)       ,  padding=((1, 1), (1, 1))       ,  dtype=self.dtype       ,  )
					blocks.append(__lowerCAmelCase							)
				lowerCamelCase__								=		blocks

				lowerCamelCase__								=		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							):



				'''simple docstring'''







				lowerCamelCase__								=		self.conv_in(__lowerCAmelCase							)
				lowerCamelCase__								=		nn.silu(__lowerCAmelCase							)

				for block in self.blocks:
					lowerCamelCase__								=		block(__lowerCAmelCase							)
					lowerCamelCase__								=		nn.silu(__lowerCAmelCase							)

				lowerCamelCase__								=		self.conv_out(__lowerCAmelCase							)

				return embedding






@flax_register_to_config
class 					__A    (    nn.Module			,				lowerCAmelCase			,				lowerCAmelCase ):

			'''simple docstring'''







			lowerCAmelCase_							     =						32
			lowerCAmelCase_							     =						4
			lowerCAmelCase_							     =						(
			    "CrossAttnDownBlock2D",
			    "CrossAttnDownBlock2D",
			    "CrossAttnDownBlock2D",
			    "DownBlock2D",
			)
			lowerCAmelCase_							     =						False
			lowerCAmelCase_							     =						(320, 640, 1280, 1280)
			lowerCAmelCase_							     =						2
			lowerCAmelCase_							     =						8
			lowerCAmelCase_							     =						None
			lowerCAmelCase_							     =						1280
			lowerCAmelCase_							     =						0.0
			lowerCAmelCase_							     =						False
			lowerCAmelCase_							     =						jnp.floataa
			lowerCAmelCase_							     =						True
			lowerCAmelCase_							     =						0
			lowerCAmelCase_							     =						"rgb"
			lowerCAmelCase_							     =						(16, 32, 96, 256)





			def 		__lowerCamelCase				(    self       ,  __lowerCAmelCase							):



				'''simple docstring'''







				lowerCamelCase__								=		(1, self.in_channels, self.sample_size, self.sample_size)
				lowerCamelCase__								=		jnp.zeros(__lowerCAmelCase       ,  dtype=jnp.floataa							)
				lowerCamelCase__								=		jnp.ones((1,)       ,  dtype=jnp.intaa							)
				lowerCamelCase__								=		jnp.zeros((1, 1, self.cross_attention_dim)       ,  dtype=jnp.floataa							)
				lowerCamelCase__								=		(1, 3, self.sample_size * 8, self.sample_size * 8)
				lowerCamelCase__								=		jnp.zeros(__lowerCAmelCase       ,  dtype=jnp.floataa							)

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

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





			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				lowerCamelCase__								=		self.block_out_channels
				lowerCamelCase__								=		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.
				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(__lowerCAmelCase       ,  dtype=self.dtype							)

				lowerCamelCase__								=		FlaxControlNetConditioningEmbedding(
				    conditioning_embedding_channels=block_out_channels[0]       ,  block_out_channels=self.conditioning_embedding_out_channels       ,  )

				lowerCamelCase__								=		self.only_cross_attention
				if isinstance(__lowerCAmelCase       ,  __lowerCAmelCase							):
					lowerCamelCase__								=		(only_cross_attention,) * len(self.down_block_types							)

				if isinstance(__lowerCAmelCase       ,  __lowerCAmelCase							):
					lowerCamelCase__								=		(num_attention_heads,) * len(self.down_block_types							)

				# down
				lowerCamelCase__								=		[]
				lowerCamelCase__								=		[]

				lowerCamelCase__								=		block_out_channels[0]

				lowerCamelCase__								=		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							):
					lowerCamelCase__								=		output_channel
					lowerCamelCase__								=		block_out_channels[i]
					lowerCamelCase__								=		i == len(__lowerCAmelCase							) - 1

					if down_block_type == "CrossAttnDownBlock2D":
						lowerCamelCase__								=		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:
						lowerCamelCase__								=		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							):
						lowerCamelCase__								=		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:
						lowerCamelCase__								=		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							)

				lowerCamelCase__								=		down_blocks
				lowerCamelCase__								=		controlnet_down_blocks

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

				lowerCamelCase__								=		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       ,  ):



				'''simple docstring'''







				lowerCamelCase__								=		self.controlnet_conditioning_channel_order
				if channel_order == "bgr":
					lowerCamelCase__								=		jnp.flip(__lowerCAmelCase       ,  axis=1							)

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

				lowerCamelCase__								=		self.time_proj(__lowerCAmelCase							)
				lowerCamelCase__								=		self.time_embedding(__lowerCAmelCase							)

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

				lowerCamelCase__								=		jnp.transpose(__lowerCAmelCase       ,  (0, 2, 3, 1)							)
				lowerCamelCase__								=		self.controlnet_cond_embedding(__lowerCAmelCase							)
				sample += controlnet_cond

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

				# 4. mid
				lowerCamelCase__								=		self.mid_block(__lowerCAmelCase       ,  __lowerCAmelCase       ,  __lowerCAmelCase       ,  deterministic=not train							)

				# 5. contronet blocks
				lowerCamelCase__								=		()
				for down_block_res_sample, controlnet_block in zip(__lowerCAmelCase       ,  self.controlnet_down_blocks							):
					lowerCamelCase__								=		controlnet_block(__lowerCAmelCase							)
					controlnet_down_block_res_samples += (down_block_res_sample,)

				lowerCamelCase__								=		controlnet_down_block_res_samples

				lowerCamelCase__								=		self.controlnet_mid_block(__lowerCAmelCase							)

				# 6. scaling
				lowerCamelCase__								=		[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							)



