



"""simple docstring"""
from abc import ABC, abstractmethod
from typing import List, Optional


class 							lowerCamelCase_(   A__    ):





					'''simple docstring'''



					def __init__(    self   ):
									# test for the above condition
									self.test()

					def 			snake_case__      (    self   ):
									_lowerCamelCase        =				0
									_lowerCamelCase        =				False
									while not completed:
													if counter == 1:
																	self.reset()
													_lowerCamelCase        =				self.advance()
													if not self.does_advance(lowerCamelCase__   ):
																	raise Exception(
																	    '''Custom Constraint is not defined correctly. self.does_advance(self.advance()) must be true.'''   )

													_lowerCamelCase  ,     _lowerCamelCase  ,     _lowerCamelCase        =				self.update(lowerCamelCase__   )
													counter += 1

													if counter > 1_0_0_0_0:
																	raise Exception('''update() does not fulfill the constraint.'''   )

									if self.remaining() != 0:
													raise Exception('''Custom Constraint is not defined correctly.'''   )

					@abstractmethod
					def 			snake_case__      (    self   ):
									raise NotImplementedError(
									    F"""{self.__class__} is an abstract class. Only classes inheriting this class can be called."""   )

					@abstractmethod
					def 			snake_case__      (    self				,       lowerCamelCase__   ):
									raise NotImplementedError(
									    F"""{self.__class__} is an abstract class. Only classes inheriting this class can be called."""   )

					@abstractmethod
					def 			snake_case__      (    self				,       lowerCamelCase__   ):
									raise NotImplementedError(
									    F"""{self.__class__} is an abstract class. Only classes inheriting this class can be called."""   )

					@abstractmethod
					def 			snake_case__      (    self   ):
									raise NotImplementedError(
									    F"""{self.__class__} is an abstract class. Only classes inheriting this class can be called."""   )

					@abstractmethod
					def 			snake_case__      (    self   ):
									raise NotImplementedError(
									    F"""{self.__class__} is an abstract class. Only classes inheriting this class can be called."""   )





					@abstractmethod
					def 			snake_case__      (    self				,       lowerCamelCase__=False   ):
									raise NotImplementedError(
									    F"""{self.__class__} is an abstract class. Only classes inheriting this class can be called."""   )


class 							lowerCamelCase_(   A__    ):





					'''simple docstring'''



					def __init__(    self				,       lowerCamelCase__   ):
									super(lowerCamelCase__				,       self   ).__init__()

									if not isinstance(lowerCamelCase__				,       lowerCamelCase__   ) or len(lowerCamelCase__   ) == 0:
													raise ValueError(F"""`token_ids` has to be a non-empty list, but is {token_ids}."""   )
									if any((not isinstance(lowerCamelCase__				,       lowerCamelCase__   ) or token_id < 0) for token_id in token_ids   ):
													raise ValueError(F"""Each list in `token_ids` has to be a list of positive integers, but is {token_ids}."""   )

									_lowerCamelCase        =				token_ids

									_lowerCamelCase        =				len(self.token_ids   )
									_lowerCamelCase        =				-1  # the index of the currently fulfilled step
									_lowerCamelCase        =				False

					def 			snake_case__      (    self   ):
									if self.completed:
													return None
									return self.token_ids[self.fulfilled_idx + 1]

					def 			snake_case__      (    self				,       lowerCamelCase__   ):
									if not isinstance(lowerCamelCase__				,       lowerCamelCase__   ):
													raise ValueError(F"""`token_id` has to be an `int`, but is {token_id} of type {type(lowerCamelCase__   )}"""   )

									if self.completed:
													return False

									return token_id == self.token_ids[self.fulfilled_idx + 1]

					def 			snake_case__      (    self				,       lowerCamelCase__   ):
									if not isinstance(lowerCamelCase__				,       lowerCamelCase__   ):
													raise ValueError(F"""`token_id` has to be an `int`, but is {token_id} of type {type(lowerCamelCase__   )}"""   )

									_lowerCamelCase        =				False
									_lowerCamelCase        =				False
									_lowerCamelCase        =				False

									if self.does_advance(lowerCamelCase__   ):
													self.fulfilled_idx += 1
													_lowerCamelCase        =				True
													if self.fulfilled_idx == (self.seqlen - 1):
																	_lowerCamelCase        =				True
													_lowerCamelCase        =				completed
									else:
													# failed to make progress.
													_lowerCamelCase        =				True
													self.reset()
									return stepped, completed, reset

					def 			snake_case__      (    self   ):
									_lowerCamelCase        =				False
									_lowerCamelCase        =				0

					def 			snake_case__      (    self   ):
									return self.seqlen - (self.fulfilled_idx + 1)





					def 			snake_case__      (    self				,       lowerCamelCase__=False   ):
									_lowerCamelCase        =				PhrasalConstraint(self.token_ids   )

									if stateful:
													_lowerCamelCase        =				self.seqlen
													_lowerCamelCase        =				self.fulfilled_idx
													_lowerCamelCase        =				self.completed

									return new_constraint


class 							lowerCamelCase_:





					'''simple docstring'''



					def __init__(    self				,       lowerCamelCase__				,       lowerCamelCase__=True   ):
									_lowerCamelCase        =				max([len(lowerCamelCase__   ) for one in nested_token_ids]   )

									_lowerCamelCase        =				{}
									for token_ids in nested_token_ids:
													_lowerCamelCase        =				root
													for tidx, token_id in enumerate(lowerCamelCase__   ):
																	if token_id not in level:
																					_lowerCamelCase        =				{}

																	_lowerCamelCase        =				level[token_id]

									if no_subsets and self.has_subsets(lowerCamelCase__				,       lowerCamelCase__   ):
													raise ValueError(
													    '''Each list in `nested_token_ids` can\'t be a complete subset of another list, but is'''
													    F""" {nested_token_ids}."""   )

									_lowerCamelCase        =				root

					def 			snake_case__      (    self				,       lowerCamelCase__   ):
									_lowerCamelCase        =				self.trie

									for current_token in current_seq:
													_lowerCamelCase        =				start[current_token]

									_lowerCamelCase        =				list(start.keys()   )

									return next_tokens

					def 			snake_case__      (    self				,       lowerCamelCase__   ):
									_lowerCamelCase        =				self.next_tokens(lowerCamelCase__   )

									return len(lowerCamelCase__   ) == 0

					def 			snake_case__      (    self				,       lowerCamelCase__   ):
									_lowerCamelCase        =				list(root.values()   )
									if len(lowerCamelCase__   ) == 0:
													return 1
									else:
													return sum([self.count_leaves(lowerCamelCase__   ) for nn in next_nodes]   )





					def 			snake_case__      (    self				,       lowerCamelCase__				,       lowerCamelCase__   ):
									_lowerCamelCase        =				self.count_leaves(lowerCamelCase__   )
									return len(lowerCamelCase__   ) != leaf_count


class 							lowerCamelCase_(   A__    ):





					'''simple docstring'''



					def __init__(    self				,       lowerCamelCase__   ):
									super(lowerCamelCase__				,       self   ).__init__()

									if not isinstance(lowerCamelCase__				,       lowerCamelCase__   ) or len(lowerCamelCase__   ) == 0:
													raise ValueError(F"""`nested_token_ids` has to be a non-empty list, but is {nested_token_ids}."""   )
									if any(not isinstance(lowerCamelCase__				,       lowerCamelCase__   ) for token_ids in nested_token_ids   ):
													raise ValueError(F"""`nested_token_ids` has to be a list of lists, but is {nested_token_ids}."""   )
									if any(
									    any((not isinstance(lowerCamelCase__				,       lowerCamelCase__   ) or token_id < 0) for token_id in token_ids   )
									    for token_ids in nested_token_ids   ):
													raise ValueError(
													    F"""Each list in `nested_token_ids` has to be a list of positive integers, but is {nested_token_ids}."""   )

									_lowerCamelCase        =				DisjunctiveTrie(lowerCamelCase__   )
									_lowerCamelCase        =				nested_token_ids

									_lowerCamelCase        =				self.trie.max_height
									_lowerCamelCase        =				[]
									_lowerCamelCase        =				False

					def 			snake_case__      (    self   ):
									_lowerCamelCase        =				self.trie.next_tokens(self.current_seq   )

									if len(lowerCamelCase__   ) == 0:
													return None
									else:
													return token_list

					def 			snake_case__      (    self				,       lowerCamelCase__   ):
									if not isinstance(lowerCamelCase__				,       lowerCamelCase__   ):
													raise ValueError(F"""`token_id` is supposed to be type `int`, but is {token_id} of type {type(lowerCamelCase__   )}"""   )

									_lowerCamelCase        =				self.trie.next_tokens(self.current_seq   )

									return token_id in next_tokens

					def 			snake_case__      (    self				,       lowerCamelCase__   ):
									if not isinstance(lowerCamelCase__				,       lowerCamelCase__   ):
													raise ValueError(F"""`token_id` is supposed to be type `int`, but is {token_id} of type {type(lowerCamelCase__   )}"""   )

									_lowerCamelCase        =				False
									_lowerCamelCase        =				False
									_lowerCamelCase        =				False

									if self.does_advance(lowerCamelCase__   ):
													self.current_seq.append(lowerCamelCase__   )
													_lowerCamelCase        =				True
									else:
													_lowerCamelCase        =				True
													self.reset()

									_lowerCamelCase        =				self.trie.reached_leaf(self.current_seq   )
									_lowerCamelCase        =				completed

									return stepped, completed, reset

					def 			snake_case__      (    self   ):
									_lowerCamelCase        =				False
									_lowerCamelCase        =				[]

					def 			snake_case__      (    self   ):
									if self.completed:
													# since this can be completed without reaching max height
													return 0
									else:
													return self.seqlen - len(self.current_seq   )





					def 			snake_case__      (    self				,       lowerCamelCase__=False   ):
									_lowerCamelCase        =				DisjunctiveConstraint(self.token_ids   )

									if stateful:
													_lowerCamelCase        =				self.seqlen
													_lowerCamelCase        =				self.current_seq
													_lowerCamelCase        =				self.completed

									return new_constraint


class 							lowerCamelCase_:





					'''simple docstring'''



					def __init__(    self				,       lowerCamelCase__   ):
									_lowerCamelCase        =				constraints

									# max # of steps required to fulfill a given constraint
									_lowerCamelCase        =				max([c.seqlen for c in constraints]   )
									_lowerCamelCase        =				len(lowerCamelCase__   )
									_lowerCamelCase        =				False

									self.init_state()

					def 			snake_case__      (    self   ):
									_lowerCamelCase        =				[]
									_lowerCamelCase        =				None
									_lowerCamelCase        =				[constraint.copy(stateful=lowerCamelCase__   ) for constraint in self.constraints]

					def 			snake_case__      (    self   ):
									_lowerCamelCase        =				0
									if self.inprogress_constraint:
													# extra points for having a constraint mid-fulfilled
													add += self.max_seqlen - self.inprogress_constraint.remaining()

									return (len(self.complete_constraints   ) * self.max_seqlen) + add

					def 			snake_case__      (    self   ):
									_lowerCamelCase        =				[]
									if self.inprogress_constraint is None:
													for constraint in self.pending_constraints:  # "pending" == "unfulfilled yet"
																	_lowerCamelCase        =				constraint.advance()
																	if isinstance(lowerCamelCase__				,       lowerCamelCase__   ):
																					token_list.append(lowerCamelCase__   )
																	elif isinstance(lowerCamelCase__				,       lowerCamelCase__   ):
																					token_list.extend(lowerCamelCase__   )
									else:
													_lowerCamelCase        =				self.inprogress_constraint.advance()
													if isinstance(lowerCamelCase__				,       lowerCamelCase__   ):
																	token_list.append(lowerCamelCase__   )
													elif isinstance(lowerCamelCase__				,       lowerCamelCase__   ):
																	token_list.extend(lowerCamelCase__   )

									if len(lowerCamelCase__   ) == 0:
													return None
									else:
													return token_list

					def 			snake_case__      (    self				,       lowerCamelCase__   ):
									self.init_state()

									if token_ids is not None:
													for token in token_ids:
																	# completes or steps **one** constraint
																	_lowerCamelCase  ,     _lowerCamelCase        =				self.add(lowerCamelCase__   )

																	# the entire list of constraints are fulfilled
																	if self.completed:
																					break

					def 			snake_case__      (    self				,       lowerCamelCase__   ):
									if not isinstance(lowerCamelCase__				,       lowerCamelCase__   ):
													raise ValueError(F"""`token_id` should be an `int`, but is `{token_id}`."""   )

									_lowerCamelCase  ,     _lowerCamelCase        =				False, False

									if self.completed:
													_lowerCamelCase        =				True
													_lowerCamelCase        =				False
													return complete, stepped

									if self.inprogress_constraint is not None:
													# In the middle of fulfilling a constraint. If the `token_id` *does* makes an incremental progress to current
													# job, simply update the state

													_lowerCamelCase  ,     _lowerCamelCase  ,     _lowerCamelCase        =				self.inprogress_constraint.update(lowerCamelCase__   )
													if reset:
																	# 1. If the next token breaks the progress, then we must restart.
																	#     e.g. constraint = "I love pies" and sequence so far is "I love" but `token_id` == "books".

																	#     But that doesn't mean we self.init_state(), since we only reset the state for this particular
																	#     constraint, not the full list of constraints.

																	self.pending_constraints.append(self.inprogress_constraint.copy(stateful=lowerCamelCase__   )   )
																	_lowerCamelCase        =				None

													if complete:
																	# 2. If the next token completes the constraint, move it to completed list, set
																	#     inprogress to None. If there are no pending constraints either, then this full list of constraints
																	#     is complete.

																	self.complete_constraints.append(self.inprogress_constraint   )
																	_lowerCamelCase        =				None

																	if len(self.pending_constraints   ) == 0:
																					# we're done!
																					_lowerCamelCase        =				True

									else:
													# Not in the middle of fulfilling a constraint. So does this `token_id` helps us step towards any of our list
													# of constraints?

													for cidx, pending_constraint in enumerate(self.pending_constraints   ):
																	if pending_constraint.does_advance(lowerCamelCase__   ):
																					_lowerCamelCase  ,     _lowerCamelCase  ,     _lowerCamelCase        =				pending_constraint.update(lowerCamelCase__   )

																					if not stepped:
																									raise Exception(
																									    '''`constraint.update(token_id)` is not yielding incremental progress, '''
																									    '''even though `constraint.does_advance(token_id)` is true.'''   )

																					if complete:
																									self.complete_constraints.append(lowerCamelCase__   )
																									_lowerCamelCase        =				None

																					if not complete and stepped:
																									_lowerCamelCase        =				pending_constraint

																					if complete or stepped:
																									# If we made any progress at all, then it's at least not a "pending constraint".

																									_lowerCamelCase        =				(
																									    self.pending_constraints[:cidx] + self.pending_constraints[cidx + 1 :]
																									)

																									if len(self.pending_constraints   ) == 0 and self.inprogress_constraint is None:
																													# If there's no longer any pending after this and no inprogress either, then we must be
																													# complete.

																													_lowerCamelCase        =				True

																									break  # prevent accidentally stepping through multiple constraints with just one token.

									return complete, stepped





					def 			snake_case__      (    self				,       lowerCamelCase__=True   ):
									_lowerCamelCase        =				ConstraintListState(self.constraints   )  # we actually never though self.constraints objects
									# throughout this process. So it's at initialization state.

									if stateful:
													_lowerCamelCase        =				[
													    constraint.copy(stateful=lowerCamelCase__   ) for constraint in self.complete_constraints
													]
													if self.inprogress_constraint is not None:
																	_lowerCamelCase        =				self.inprogress_constraint.copy(stateful=lowerCamelCase__   )
													_lowerCamelCase        =				[constraint.copy() for constraint in self.pending_constraints]

									return new_state





"""simple docstring"""
from ...utils import (
    OptionalDependencyNotAvailable,
    is_torch_available,
    is_transformers_available,
    is_transformers_version,
)


try:
			if not (is_transformers_available() and is_torch_available()):
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			from ...utils.dummy_torch_and_transformers_objects import KandinskyPipeline, KandinskyPriorPipeline
else:
			from .pipeline_kandinsky import KandinskyPipeline
			from .pipeline_kandinsky_imgaimg import KandinskyImgaImgPipeline
			from .pipeline_kandinsky_inpaint import KandinskyInpaintPipeline
			from .pipeline_kandinsky_prior import KandinskyPriorPipeline, KandinskyPriorPipelineOutput
			from .text_encoder import MultilingualCLIP
