






import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch


if is_torch_available():
				import torch

				from transformers.generation import DisjunctiveConstraint

@require_torch
class 		_snake_case	(	unittest.TestCase       ):






			def  SCREAMING_SNAKE_CASE__     (							self)   ->							Dict:
						# For consistency across different places the DisjunctiveConstraint is called,
						# dc.token_ids is a list of integers. It is also initialized only by integers.

						SCREAMING_SNAKE_CASE							=			[[1, 2, 4], [1, 2, 3, 4]]
						SCREAMING_SNAKE_CASE							=			DisjunctiveConstraint(a)
						self.assertTrue(isinstance(dc.token_ids		,       a))

						with self.assertRaises(a):
									DisjunctiveConstraint(torch.LongTensor([[1, 2, 4], [1, 2, 3]]))

						with self.assertRaises(a):
									DisjunctiveConstraint([torch.LongTensor([1, 2, 4]), torch.LongTensor([1, 2, 3, 4, 5])])






			def  SCREAMING_SNAKE_CASE__     (							self)   ->							int:
						# We can't have constraints that are complete subsets of another. This leads to a preverse
						# interpretation of "constraint fulfillment": does generating [1,2,3] fulfill the constraint?
						# It would mean that it generated [1,2] which fulfills it, but it's in the middle of potentially
						# fulfilling [1,2,3,4]. If we believe that [1,2,3] does fulfill the constraint, then the algorithm
						# will necessarily never reach [1,2,3,4], giving users a false sense of control (better to just not allow it).
						SCREAMING_SNAKE_CASE							=			[[1, 2], [1, 2, 3, 4]]

						with self.assertRaises(a):
									DisjunctiveConstraint(a)  # fails here






			def  SCREAMING_SNAKE_CASE__     (							self)   ->							Union[str, Any]:
						SCREAMING_SNAKE_CASE							=			[[1, 2, 3], [1, 2, 4]]

						SCREAMING_SNAKE_CASE							=			DisjunctiveConstraint(a)

						SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE							=			dc.update(1)
						SCREAMING_SNAKE_CASE							=			stepped is True and completed is False and reset is False
						self.assertTrue(a)
						self.assertTrue(not dc.completed)
						self.assertTrue(dc.current_seq == [1])

						SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE							=			dc.update(2)
						SCREAMING_SNAKE_CASE							=			stepped is True and completed is False and reset is False
						self.assertTrue(a)
						self.assertTrue(not dc.completed)
						self.assertTrue(dc.current_seq == [1, 2])

						SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE							=			dc.update(3)
						SCREAMING_SNAKE_CASE							=			stepped is True and completed is True and reset is False
						self.assertTrue(a)
						self.assertTrue(dc.completed)  # Completed!
						self.assertTrue(dc.current_seq == [1, 2, 3])







			def  SCREAMING_SNAKE_CASE__     (							self)   ->							List[str]:
						SCREAMING_SNAKE_CASE							=			[[1, 2, 3], [1, 2, 4, 5], [1, 2, 5]]

						SCREAMING_SNAKE_CASE							=			DisjunctiveConstraint(a)

						SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE							=			dc.update(1)
						self.assertTrue(not dc.completed)
						self.assertTrue(dc.current_seq == [1])

						SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE							=			dc.update(2)
						self.assertTrue(not dc.completed)
						self.assertTrue(dc.current_seq == [1, 2])

						SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE							=			dc.update(4)
						self.assertTrue(not dc.completed)
						self.assertTrue(dc.current_seq == [1, 2, 4])

						SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE							=			dc.update(5)
						self.assertTrue(dc.completed)  # Completed!
						self.assertTrue(dc.current_seq == [1, 2, 4, 5])

						dc.reset()

						SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE							=			dc.update(1)
						self.assertTrue(not dc.completed)
						self.assertTrue(dc.remaining() == 3)
						self.assertTrue(dc.current_seq == [1])

						SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE							=			dc.update(2)
						self.assertTrue(not dc.completed)
						self.assertTrue(dc.remaining() == 2)
						self.assertTrue(dc.current_seq == [1, 2])

						SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE							=			dc.update(5)
						self.assertTrue(dc.completed)  # Completed!
						self.assertTrue(dc.remaining() == 0)
						self.assertTrue(dc.current_seq == [1, 2, 5])










import torch
from transformers import PreTrainedModel, XLMRobertaConfig, XLMRobertaModel





class 			lowerCamelCase_				( _SCREAMING_SNAKE_CASE   ):


 '''simple docstring'''







 lowercase_							       =       "M-CLIP"



 def __init__(				self    :						Tuple							,							_lowerCAmelCase    :						List[str]=1_024							,							_lowerCAmelCase    :						str=768							,							**_lowerCAmelCase    :						Union[str, Any]		):
       SCREAMING_SNAKE_CASE_      		=     transformerDimSize
       SCREAMING_SNAKE_CASE_      		=     imageDimSize
       super().__init__(**_lowerCAmelCase		)





class 			lowerCamelCase_				( _SCREAMING_SNAKE_CASE   ):


 '''simple docstring'''







 lowercase_							       =       MCLIPConfig



 def __init__(				self    :						Dict							,							_lowerCAmelCase    :						Union[str, Any]							,							*_lowerCAmelCase    :						str							,							**_lowerCAmelCase    :						str		):
       super().__init__(_lowerCAmelCase							,							*_lowerCAmelCase							,							**_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     XLMRobertaModel(_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     torch.nn.Linear(
           in_features=config.transformerDimensions							,							out_features=config.numDims		)




 def  lowerCAmelCase_    (				self    :						List[Any]							,							_lowerCAmelCase    :						Any							,							_lowerCAmelCase    :						Union[str, Any]		):
       SCREAMING_SNAKE_CASE_      		=     self.transformer(input_ids=_lowerCAmelCase							,							attention_mask=_lowerCAmelCase		)[0]
       SCREAMING_SNAKE_CASE_      		=     (embs * attention_mask.unsqueeze(2		)).sum(dim=1		) / attention_mask.sum(dim=1		)[:, None]
       return self.LinearTransformation(_lowerCAmelCase		), embs