


"""simple docstring"""



def      UpperCAmelCase     (       _lowercase :   int			,   _lowercase :   int			,   _lowercase :   list[list[int]]	)						-> int:


	"""simple docstring"""




	def update_area_of_max_square(_lowercase :   int			,   _lowercase :   int	) -> int:
		# BASE CASE
		if row >= rows or col >= cols:
			return 0

		lowerCAmelCase_      							=			update_area_of_max_square(_lowercase			,   col + 1	)
		lowerCAmelCase_      							=			update_area_of_max_square(row + 1			,   col + 1	)
		lowerCAmelCase_      							=			update_area_of_max_square(row + 1			,   _lowercase	)

		if mat[row][col]:
			lowerCAmelCase_      							=			1 + min([right, diagonal, down]	)
			lowerCAmelCase_      							=			max(largest_square_area[0]			,   _lowercase	)
			return sub_problem_sol
		else:
			return 0

	lowerCAmelCase_      							=			[0]
	update_area_of_max_square(0			,   0	)
	return largest_square_area[0]
def      UpperCAmelCase     (       _lowercase :   int			,   _lowercase :   int			,   _lowercase :   list[list[int]]	)						-> int:


	"""simple docstring"""




	def update_area_of_max_square_using_dp_array(
	    _lowercase :   int			,   _lowercase :   int			,   _lowercase :   list[list[int]]	) -> int:
		if row >= rows or col >= cols:
			return 0
		if dp_array[row][col] != -1:
			return dp_array[row][col]

		lowerCAmelCase_      							=			update_area_of_max_square_using_dp_array(_lowercase			,   col + 1			,   _lowercase	)
		lowerCAmelCase_      							=			update_area_of_max_square_using_dp_array(row + 1			,   col + 1			,   _lowercase	)
		lowerCAmelCase_      							=			update_area_of_max_square_using_dp_array(row + 1			,   _lowercase			,   _lowercase	)

		if mat[row][col]:
			lowerCAmelCase_      							=			1 + min([right, diagonal, down]	)
			lowerCAmelCase_      							=			max(largest_square_area[0]			,   _lowercase	)
			lowerCAmelCase_      							=			sub_problem_sol
			return sub_problem_sol
		else:
			return 0

	lowerCAmelCase_      							=			[0]
	lowerCAmelCase_      							=			[[-1] * cols for _ in range(_lowercase	)]
	update_area_of_max_square_using_dp_array(0			,   0			,   _lowercase	)

	return largest_square_area[0]
def      UpperCAmelCase     (       _lowercase :   int			,   _lowercase :   int			,   _lowercase :   list[list[int]]	)						-> int:


	"""simple docstring"""




	lowerCAmelCase_      							=			[[0] * (cols + 1) for _ in range(rows + 1	)]
	lowerCAmelCase_      							=			0
	for row in range(rows - 1			,   -1			,   -1	):
		for col in range(cols - 1			,   -1			,   -1	):
			lowerCAmelCase_      							=			dp_array[row][col + 1]
			lowerCAmelCase_      							=			dp_array[row + 1][col + 1]
			lowerCAmelCase_      							=			dp_array[row + 1][col]

			if mat[row][col] == 1:
				lowerCAmelCase_      							=			1 + min(_lowercase			,   _lowercase			,   _lowercase	)
				lowerCAmelCase_      							=			max(dp_array[row][col]			,   _lowercase	)
			else:
				lowerCAmelCase_      							=			0

	return largest_square_area
def      UpperCAmelCase     (       _lowercase :   int			,   _lowercase :   int			,   _lowercase :   list[list[int]]	)						-> int:


	"""simple docstring"""




	lowerCAmelCase_      							=			[0] * (cols + 1)
	lowerCAmelCase_      							=			[0] * (cols + 1)
	lowerCAmelCase_      							=			0
	for row in range(rows - 1			,   -1			,   -1	):
		for col in range(cols - 1			,   -1			,   -1	):
			lowerCAmelCase_      							=			current_row[col + 1]
			lowerCAmelCase_      							=			next_row[col + 1]
			lowerCAmelCase_      							=			next_row[col]

			if mat[row][col] == 1:
				lowerCAmelCase_      							=			1 + min(_lowercase			,   _lowercase			,   _lowercase	)
				lowerCAmelCase_      							=			max(current_row[col]			,   _lowercase	)
			else:
				lowerCAmelCase_      							=			0
		lowerCAmelCase_      							=			current_row

	return largest_square_area


if __name__ == "__main__":
		import doctest

		doctest.testmod()
		print(largest_square_area_in_matrix_bottom_up(2, 2, [[1, 1], [1, 1]]))



"""simple docstring"""



from typing import Optional, Tuple

import jax
import jax.numpy as jnp
from flax import linen as nn
from flax.core.frozen_dict import FrozenDict
from transformers import CLIPConfig, FlaxPreTrainedModel
from transformers.models.clip.modeling_flax_clip import FlaxCLIPVisionModule
def      UpperCAmelCase     (       _lowercase :   str			,   _lowercase :   int			,   _lowercase :   Optional[int]=1E-12	)						-> List[str]:


	"""simple docstring"""




	lowerCAmelCase_      							=			jnp.divide(emb_a.T			,   jnp.clip(jnp.linalg.norm(_lowercase			,   axis=1	)			,   a_min=_lowercase	)	).T
	lowerCAmelCase_      							=			jnp.divide(emb_a.T			,   jnp.clip(jnp.linalg.norm(_lowercase			,   axis=1	)			,   a_min=_lowercase	)	).T
	return jnp.matmul(_lowercase			,   norm_emb_a.T	)







class    __a						( nn.Module							):
				lowerCamelCase     :       CLIPConfig
				lowerCamelCase     :       jnp.dtype			       =jnp.floataa







				def      lowerCamelCase_     ( self   ):

					'''simple docstring'''






					lowerCAmelCase_      							=			FlaxCLIPVisionModule(self.config.vision_config   )
					lowerCAmelCase_      							=			nn.Dense(self.config.projection_dim      ,	use_bias=UpperCAmelCase      ,	dtype=self.dtype   )

					lowerCAmelCase_      							=			self.param('''concept_embeds'''      ,	jax.nn.initializers.ones      ,	(17, self.config.projection_dim)   )
					lowerCAmelCase_      							=			self.param(
					    '''special_care_embeds'''      ,	jax.nn.initializers.ones      ,	(3, self.config.projection_dim)   )

					lowerCAmelCase_      							=			self.param('''concept_embeds_weights'''      ,	jax.nn.initializers.ones      ,	(17,)   )
					lowerCAmelCase_      							=			self.param('''special_care_embeds_weights'''      ,	jax.nn.initializers.ones      ,	(3,)   )







				def __call__( self      ,	UpperCAmelCase   ):

					'''simple docstring'''






					lowerCAmelCase_      							=			self.vision_model(UpperCAmelCase   )[1]
					lowerCAmelCase_      							=			self.visual_projection(UpperCAmelCase   )

					lowerCAmelCase_      							=			jax_cosine_distance(UpperCAmelCase      ,	self.special_care_embeds   )
					lowerCAmelCase_      							=			jax_cosine_distance(UpperCAmelCase      ,	self.concept_embeds   )

					# increase this value to create a stronger `nfsw` filter
					# at the cost of increasing the possibility of filtering benign image inputs
					lowerCAmelCase_      							=			0.0

					lowerCAmelCase_      							=			special_cos_dist - self.special_care_embeds_weights[None, :] + adjustment
					lowerCAmelCase_      							=			jnp.round(UpperCAmelCase      ,	3   )
					lowerCAmelCase_      							=			jnp.any(special_scores > 0      ,	axis=1      ,	keepdims=UpperCAmelCase   )
					# Use a lower threshold if an image has any special care concept
					lowerCAmelCase_      							=			is_special_care * 0.0_1

					lowerCAmelCase_      							=			cos_dist - self.concept_embeds_weights[None, :] + special_adjustment
					lowerCAmelCase_      							=			jnp.round(UpperCAmelCase      ,	3   )
					lowerCAmelCase_      							=			jnp.any(concept_scores > 0      ,	axis=1   )

					return has_nsfw_concepts







class    __a						( __snake_case							):
				lowerCamelCase     :       Optional[Any]			       =CLIPConfig
				lowerCamelCase     :       List[str]			       ='clip_input'
				lowerCamelCase     :       int			       =FlaxStableDiffusionSafetyCheckerModule







				def __init__( self      ,	UpperCAmelCase      ,	UpperCAmelCase = None      ,	UpperCAmelCase = 0      ,	UpperCAmelCase = jnp.floataa      ,	UpperCAmelCase = True      ,	**UpperCAmelCase      ,	):

					'''simple docstring'''






					if input_shape is None:
						lowerCAmelCase_      							=			(1, 224, 224, 3)
					lowerCAmelCase_      							=			self.module_class(config=UpperCAmelCase      ,	dtype=UpperCAmelCase      ,	**UpperCAmelCase   )
					super().__init__(UpperCAmelCase      ,	UpperCAmelCase      ,	input_shape=UpperCAmelCase      ,	seed=UpperCAmelCase      ,	dtype=UpperCAmelCase      ,	_do_init=_do_init   )







				def      lowerCamelCase_     ( self      ,	UpperCAmelCase      ,	UpperCAmelCase      ,	UpperCAmelCase = None   ):

					'''simple docstring'''






					lowerCAmelCase_      							=			jax.random.normal(UpperCAmelCase      ,	UpperCAmelCase   )

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

					lowerCAmelCase_      							=			self.module.init(UpperCAmelCase      ,	UpperCAmelCase   )['''params''']

					return random_params







				def __call__( self      ,	UpperCAmelCase      ,	UpperCAmelCase = None      ,	):

					'''simple docstring'''






					lowerCAmelCase_      							=			jnp.transpose(UpperCAmelCase      ,	(0, 2, 3, 1)   )

					return self.module.apply(
					    {'''params''': params or self.params}      ,	jnp.array(UpperCAmelCase      ,	dtype=jnp.floataa   )      ,	rngs={}      ,	)