
from tempfile import TemporaryDirectory
from unittest import TestCase
from unittest.mock import MagicMock, patch

from transformers import AutoModel, TFAutoModel
from transformers.onnx import FeaturesManager
from transformers.testing_utils import SMALL_MODEL_IDENTIFIER, require_tf, require_torch





@require_torch
@require_tf
class       __A(				UpperCAmelCase       ):

					def    lowercase__      (	self					:     List[Any]   ):
												lowerCamelCase_							  =					SMALL_MODEL_IDENTIFIER
												lowerCamelCase_							  =					"""pt"""
												lowerCamelCase_							  =					"""tf"""

					def    lowercase__      (	self					:     Tuple   ,					__UpperCamelCase					:     Optional[int]   ):
												lowerCamelCase_							  =					AutoModel.from_pretrained(self.test_model   )
												model_pt.save_pretrained(__UpperCamelCase   )

					def    lowercase__      (	self					:     Any   ,					__UpperCamelCase					:     Tuple   ):
												lowerCamelCase_							  =					TFAutoModel.from_pretrained(self.test_model   ,					from_pt=__UpperCamelCase   )
												model_tf.save_pretrained(__UpperCamelCase   )

					def    lowercase__      (	self					:     Union[str, Any]   ):
												lowerCamelCase_							  =					"""mock_framework"""

												# Framework provided - return whatever the user provides
												lowerCamelCase_							  =					FeaturesManager.determine_framework(self.test_model   ,					__UpperCamelCase   )
												self.assertEqual(__UpperCamelCase   ,					__UpperCamelCase   )

												# Local checkpoint and framework provided - return provided framework
												# PyTorch checkpoint
												with TemporaryDirectory() as local_pt_ckpt:
																			self._setup_pt_ckpt(__UpperCamelCase   )
																			lowerCamelCase_							  =					FeaturesManager.determine_framework(__UpperCamelCase   ,					__UpperCamelCase   )
																			self.assertEqual(__UpperCamelCase   ,					__UpperCamelCase   )

												# TensorFlow checkpoint
												with TemporaryDirectory() as local_tf_ckpt:
																			self._setup_tf_ckpt(__UpperCamelCase   )
																			lowerCamelCase_							  =					FeaturesManager.determine_framework(__UpperCamelCase   ,					__UpperCamelCase   )
																			self.assertEqual(__UpperCamelCase   ,					__UpperCamelCase   )

					def    lowercase__      (	self					:     Optional[int]   ):
												# PyTorch checkpoint
												with TemporaryDirectory() as local_pt_ckpt:
																			self._setup_pt_ckpt(__UpperCamelCase   )
																			lowerCamelCase_							  =					FeaturesManager.determine_framework(__UpperCamelCase   )
																			self.assertEqual(__UpperCamelCase   ,					self.framework_pt   )

												# TensorFlow checkpoint
												with TemporaryDirectory() as local_tf_ckpt:
																			self._setup_tf_ckpt(__UpperCamelCase   )
																			lowerCamelCase_							  =					FeaturesManager.determine_framework(__UpperCamelCase   )
																			self.assertEqual(__UpperCamelCase   ,					self.framework_tf   )

												# Invalid local checkpoint
												with TemporaryDirectory() as local_invalid_ckpt:
																			with self.assertRaises(__UpperCamelCase   ):
																										lowerCamelCase_							  =					FeaturesManager.determine_framework(__UpperCamelCase   )



					def    lowercase__      (	self					:     List[str]   ):
												lowerCamelCase_							  =					MagicMock(return_value=__UpperCamelCase   )
												with patch("""transformers.onnx.features.is_tf_available"""   ,					__UpperCamelCase   ):
																			lowerCamelCase_							  =					FeaturesManager.determine_framework(self.test_model   )
																			self.assertEqual(__UpperCamelCase   ,					self.framework_pt   )

												# PyTorch not in environment -> use TensorFlow
												lowerCamelCase_							  =					MagicMock(return_value=__UpperCamelCase   )
												with patch("""transformers.onnx.features.is_torch_available"""   ,					__UpperCamelCase   ):
																			lowerCamelCase_							  =					FeaturesManager.determine_framework(self.test_model   )
																			self.assertEqual(__UpperCamelCase   ,					self.framework_tf   )

												# Both in environment -> use PyTorch
												lowerCamelCase_							  =					MagicMock(return_value=__UpperCamelCase   )
												lowerCamelCase_							  =					MagicMock(return_value=__UpperCamelCase   )
												with patch("""transformers.onnx.features.is_tf_available"""   ,					__UpperCamelCase   ), patch(
												    """transformers.onnx.features.is_torch_available"""   ,					__UpperCamelCase   ):
																			lowerCamelCase_							  =					FeaturesManager.determine_framework(self.test_model   )
																			self.assertEqual(__UpperCamelCase   ,					self.framework_pt   )

												# Both not in environment -> raise error
												lowerCamelCase_							  =					MagicMock(return_value=__UpperCamelCase   )
												lowerCamelCase_							  =					MagicMock(return_value=__UpperCamelCase   )
												with patch("""transformers.onnx.features.is_tf_available"""   ,					__UpperCamelCase   ), patch(
												    """transformers.onnx.features.is_torch_available"""   ,					__UpperCamelCase   ):
																			with self.assertRaises(__UpperCamelCase   ):
																										lowerCamelCase_							  =					FeaturesManager.determine_framework(self.test_model   )







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       __A(				unittest.TestCase       ):

					def    lowercase__      (	self					:     Optional[Any]   ):
												# For consistency across different places the DisjunctiveConstraint is called,
												# dc.token_ids is a list of integers. It is also initialized only by integers.

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

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

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

					def    lowercase__      (	self					:     List[Any]   ):
												# 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).
												lowerCamelCase_							  =					[[1, 2], [1, 2, 3, 4]]

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

					def    lowercase__      (	self					:     Union[str, Any]   ):
												lowerCamelCase_							  =					[[1, 2, 3], [1, 2, 4]]

												lowerCamelCase_							  =					DisjunctiveConstraint(__UpperCamelCase   )

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

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

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



					def    lowercase__      (	self					:     Tuple   ):
												lowerCamelCase_							  =					[[1, 2, 3], [1, 2, 4, 5], [1, 2, 5]]

												lowerCamelCase_							  =					DisjunctiveConstraint(__UpperCamelCase   )

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

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

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

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

												dc.reset()

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

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

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





