


'''simple docstring'''



import inspect
import unittest

import numpy as np

from tests.test_modeling_common import floats_tensor
from transformers import MaskaFormerConfig, is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device
from transformers.utils import cached_property

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
      import torch

      from transformers import MaskaFormerForUniversalSegmentation, MaskaFormerModel

      if is_vision_available():
            from transformers import MaskaFormerImageProcessor

if is_vision_available():
      from PIL import Image



class      a    :


     """simple docstring"""






     def __init__(    self					:		str							,			snake_case					:		Union[str, Any]							,			snake_case					:		Union[str, Any]=2							,			snake_case					:		Union[str, Any]=True							,			snake_case					:		List[Any]=False							,			snake_case					:		Tuple=10							,			snake_case					:		Any=3							,			snake_case					:		int=32 * 8							,			snake_case					:		str=32 * 8							,			snake_case					:		Dict=4							,			snake_case					:		Union[str, Any]=64							,			)						->							Tuple:
          __UpperCAmelCase     :			List[Any]  							=      parent
          __UpperCAmelCase     :			Union[str, Any]  							=      batch_size
          __UpperCAmelCase     :			Dict  							=      is_training
          __UpperCAmelCase     :			Optional[int]  							=      use_auxiliary_loss
          __UpperCAmelCase     :			List[str]  							=      num_queries
          __UpperCAmelCase     :			Dict  							=      num_channels
          __UpperCAmelCase     :			Union[str, Any]  							=      min_size
          __UpperCAmelCase     :			List[Any]  							=      max_size
          __UpperCAmelCase     :			Tuple  							=      num_labels
          __UpperCAmelCase     :			str  							=      hidden_dim
          __UpperCAmelCase     :			List[str]  							=      hidden_dim

     def 			lowerCamelCase__			(    self					:		Optional[int]      )						->							List[str]:
          __UpperCAmelCase     :			str  							=      floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size]      ).to(
              snake_case      )

          __UpperCAmelCase     :			Tuple  							=      torch.ones([self.batch_size, self.min_size, self.max_size]							,			device=snake_case      )

          __UpperCAmelCase     :			Dict  							=      (
              torch.rand([self.batch_size, self.num_labels, self.min_size, self.max_size]							,			device=snake_case      ) > 0.5
          ).float()
          __UpperCAmelCase     :			Dict  							=      (torch.rand((self.batch_size, self.num_labels)							,			device=snake_case      ) > 0.5).long()

          __UpperCAmelCase     :			Optional[int]  							=      self.get_config()
          return config, pixel_values, pixel_mask, mask_labels, class_labels

     def 			lowerCamelCase__			(    self					:		int      )						->							int:
          __UpperCAmelCase     :			Optional[int]  							=      MaskaFormerConfig(
              hidden_size=self.hidden_dim							,			)
          __UpperCAmelCase     :			Optional[Any]  							=      self.num_queries
          __UpperCAmelCase     :			List[str]  							=      self.num_labels

          __UpperCAmelCase     :			int  							=      [1, 1, 1, 1]
          __UpperCAmelCase     :			Dict  							=      self.num_channels

          __UpperCAmelCase     :			Tuple  							=      64
          __UpperCAmelCase     :			int  							=      128
          __UpperCAmelCase     :			Optional[Any]  							=      self.hidden_dim
          __UpperCAmelCase     :			Optional[Any]  							=      self.hidden_dim
          __UpperCAmelCase     :			int  							=      self.hidden_dim
          return config

     def 			lowerCamelCase__			(    self					:		List[Any]      )						->							List[str]:
          __UpperCAmelCase							,       __UpperCAmelCase							,       __UpperCAmelCase							,       __UpperCAmelCase							,       __UpperCAmelCase     :			int  							=      self.prepare_config_and_inputs()
          __UpperCAmelCase     :			Union[str, Any]  							=      {'''pixel_values''': pixel_values, '''pixel_mask''': pixel_mask}
          return config, inputs_dict

     def 			lowerCamelCase__			(    self					:		Optional[int]							,			snake_case					:		Any							,			snake_case					:		Optional[Any]      )						->							Tuple:
          __UpperCAmelCase     :			Union[str, Any]  							=      output.encoder_hidden_states
          __UpperCAmelCase     :			Any  							=      output.pixel_decoder_hidden_states
          __UpperCAmelCase     :			Union[str, Any]  							=      output.transformer_decoder_hidden_states

          self.parent.assertTrue(len(snake_case      )							,			len(config.backbone_config.depths      )      )
          self.parent.assertTrue(len(snake_case      )							,			len(config.backbone_config.depths      )      )
          self.parent.assertTrue(len(snake_case      )							,			config.decoder_layers      )

     def 			lowerCamelCase__			(    self					:		Optional[Any]							,			snake_case					:		Dict							,			snake_case					:		str							,			snake_case					:		Dict							,			snake_case					:		Any=False      )						->							Optional[int]:
          with torch.no_grad():
               __UpperCAmelCase     :			List[Any]  							=      MaskaFormerModel(config=snake_case      )
               model.to(snake_case      )
               model.eval()

               __UpperCAmelCase     :			Optional[Any]  							=      model(pixel_values=snake_case							,			pixel_mask=snake_case      )
               __UpperCAmelCase     :			Optional[Any]  							=      model(snake_case							,			output_hidden_states=snake_case      )

          self.parent.assertEqual(
              output.transformer_decoder_last_hidden_state.shape							,			(self.batch_size, self.num_queries, self.hidden_dim)							,			)
          # let's ensure the other two hidden state exists
          self.parent.assertTrue(output.pixel_decoder_last_hidden_state is not None      )
          self.parent.assertTrue(output.encoder_last_hidden_state is not None      )

          if output_hidden_states:
               self.check_output_hidden_state(snake_case							,			snake_case      )






     def 			lowerCamelCase__			(    self					:		Dict							,			snake_case					:		List[Any]							,			snake_case					:		Optional[Any]							,			snake_case					:		Any							,			snake_case					:		Optional[Any]							,			snake_case					:		Union[str, Any]      )						->							Union[str, Any]:
          __UpperCAmelCase     :			int  							=      MaskaFormerForUniversalSegmentation(config=snake_case      )
          model.to(snake_case      )
          model.eval()

          def comm_check_on_output(snake_case					:		List[Any]      ):
               # let's still check that all the required stuff is there
               self.parent.assertTrue(result.transformer_decoder_last_hidden_state is not None      )
               self.parent.assertTrue(result.pixel_decoder_last_hidden_state is not None      )
               self.parent.assertTrue(result.encoder_last_hidden_state is not None      )
               # okay, now we need to check the logits shape
               # due to the encoder compression, masks have a //4 spatial size
               self.parent.assertEqual(
                   result.masks_queries_logits.shape							,			(self.batch_size, self.num_queries, self.min_size // 4, self.max_size // 4)							,			)
               # + 1 for null class
               self.parent.assertEqual(
                   result.class_queries_logits.shape							,			(self.batch_size, self.num_queries, self.num_labels + 1)      )

          with torch.no_grad():
               __UpperCAmelCase     :			int  							=      model(pixel_values=snake_case							,			pixel_mask=snake_case      )
               __UpperCAmelCase     :			Any  							=      model(snake_case      )

               comm_check_on_output(snake_case      )

               __UpperCAmelCase     :			List[str]  							=      model(
                   pixel_values=snake_case							,			pixel_mask=snake_case							,			mask_labels=snake_case							,			class_labels=snake_case      )

          comm_check_on_output(snake_case      )

          self.parent.assertTrue(result.loss is not None      )
          self.parent.assertEqual(result.loss.shape							,			torch.Size([1]      )      )



@require_torch
class      a    (		_a     ,  _a     ,  unittest.TestCase	):


     """simple docstring"""






     SCREAMING_SNAKE_CASE	:			Any							       =       (MaskaFormerModel, MaskaFormerForUniversalSegmentation) if is_torch_available() else ()
     SCREAMING_SNAKE_CASE	:			Union[str, Any]							       =       {"feature-extraction": MaskaFormerModel} if is_torch_available() else {}

     SCREAMING_SNAKE_CASE	:			Optional[Any]							       =       False
     SCREAMING_SNAKE_CASE	:			Optional[int]							       =       False
     SCREAMING_SNAKE_CASE	:			Any							       =       False
     SCREAMING_SNAKE_CASE	:			Dict							       =       False

     def 			lowerCamelCase__			(    self					:		Optional[Any]      )						->							Tuple:
          __UpperCAmelCase     :			Optional[int]  							=      MaskaFormerModelTester(self      )
          __UpperCAmelCase     :			Optional[Any]  							=      ConfigTester(self							,			config_class=snake_case							,			has_text_modality=snake_case      )

     def 			lowerCamelCase__			(    self					:		Optional[Any]      )						->							int:
          self.config_tester.run_common_tests()

     def 			lowerCamelCase__			(    self					:		Dict      )						->							Union[str, Any]:
          __UpperCAmelCase							,       __UpperCAmelCase     :			Union[str, Any]  							=      self.model_tester.prepare_config_and_inputs_for_common()
          self.model_tester.create_and_check_maskaformer_model(snake_case							,			**snake_case							,			output_hidden_states=snake_case      )

     def 			lowerCamelCase__			(    self					:		List[Any]      )						->							Optional[int]:
          __UpperCAmelCase     :			int  							=      self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_maskaformer_instance_segmentation_head_model(*snake_case      )

     @unittest.skip(reason='''Mask2Former does not use inputs_embeds'''      )
     def 			lowerCamelCase__			(    self					:		List[Any]      )						->							Optional[Any]:
          pass

     @unittest.skip(reason='''Mask2Former does not have a get_input_embeddings method'''      )
     def 			lowerCamelCase__			(    self					:		str      )						->							List[Any]:
          pass

     @unittest.skip(reason='''Mask2Former is not a generative model'''      )
     def 			lowerCamelCase__			(    self					:		Optional[Any]      )						->							List[Any]:
          pass

     @unittest.skip(reason='''Mask2Former does not use token embeddings'''      )
     def 			lowerCamelCase__			(    self					:		Optional[Any]      )						->							Union[str, Any]:
          pass

     @require_torch_multi_gpu
     @unittest.skip(
         reason='''Mask2Former has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`'''      )
     def 			lowerCamelCase__			(    self					:		Union[str, Any]      )						->							Dict:
          pass

     @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''      )
     def 			lowerCamelCase__			(    self					:		Union[str, Any]      )						->							Optional[int]:
          pass

     def 			lowerCamelCase__			(    self					:		Tuple      )						->							int:
          __UpperCAmelCase							,       __UpperCAmelCase     :			Optional[Any]  							=      self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
               __UpperCAmelCase     :			List[str]  							=      model_class(snake_case      )
               __UpperCAmelCase     :			Dict  							=      inspect.signature(model.forward      )
               # signature.parameters is an OrderedDict => so arg_names order is deterministic
               __UpperCAmelCase     :			Optional[int]  							=      [*signature.parameters.keys()]

               __UpperCAmelCase     :			Tuple  							=      ['''pixel_values''']
               self.assertListEqual(arg_names[:1]							,			snake_case      )

     @slow
     def 			lowerCamelCase__			(    self					:		Union[str, Any]      )						->							Any:
          for model_name in ["facebook/mask2former-swin-small-coco-instance"]:
               __UpperCAmelCase     :			List[str]  							=      MaskaFormerModel.from_pretrained(snake_case      )
               self.assertIsNotNone(snake_case      )

     def 			lowerCamelCase__			(    self					:		List[str]      )						->							int:
          __UpperCAmelCase     :			str  							=      (self.model_tester.min_size,) * 2
          __UpperCAmelCase     :			Tuple  							=      {
              '''pixel_values''': torch.randn((2, 3, *size)							,			device=snake_case      ),
              '''mask_labels''': torch.randn((2, 10, *size)							,			device=snake_case      ),
              '''class_labels''': torch.zeros(2							,			10							,			device=snake_case      ).long(),
          }
          __UpperCAmelCase     :			Any  							=      self.model_tester.get_config()

          __UpperCAmelCase     :			Union[str, Any]  							=      MaskaFormerForUniversalSegmentation(snake_case      ).to(snake_case      )
          __UpperCAmelCase     :			Any  							=      model(**snake_case      )
          self.assertTrue(outputs.loss is not None      )

     def 			lowerCamelCase__			(    self					:		List[str]      )						->							Optional[Any]:
          __UpperCAmelCase							,       __UpperCAmelCase     :			int  							=      self.model_tester.prepare_config_and_inputs_for_common()
          self.model_tester.create_and_check_maskaformer_model(snake_case							,			**snake_case							,			output_hidden_states=snake_case      )

     def 			lowerCamelCase__			(    self					:		List[Any]      )						->							Union[str, Any]:
          __UpperCAmelCase							,       __UpperCAmelCase     :			Tuple  							=      self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
               __UpperCAmelCase     :			Any  							=      model_class(snake_case      ).to(snake_case      )
               __UpperCAmelCase     :			str  							=      model(**snake_case							,			output_attentions=snake_case      )
               self.assertTrue(outputs.attentions is not None      )

     def 			lowerCamelCase__			(    self					:		Tuple      )						->							List[str]:
          if not self.model_tester.is_training:
               return

          __UpperCAmelCase     :			Optional[Any]  							=      self.all_model_classes[1]
          __UpperCAmelCase							,       __UpperCAmelCase							,       __UpperCAmelCase							,       __UpperCAmelCase							,       __UpperCAmelCase     :			int  							=      self.model_tester.prepare_config_and_inputs()

          __UpperCAmelCase     :			List[Any]  							=      model_class(snake_case      )
          model.to(snake_case      )
          model.train()

          __UpperCAmelCase     :			Union[str, Any]  							=      model(snake_case							,			mask_labels=snake_case							,			class_labels=snake_case      ).loss
          loss.backward()






     def 			lowerCamelCase__			(    self					:		Optional[Any]      )						->							str:
          __UpperCAmelCase     :			List[Any]  							=      self.all_model_classes[1]
          __UpperCAmelCase							,       __UpperCAmelCase							,       __UpperCAmelCase							,       __UpperCAmelCase							,       __UpperCAmelCase     :			Optional[int]  							=      self.model_tester.prepare_config_and_inputs()
          __UpperCAmelCase     :			Optional[int]  							=      True
          __UpperCAmelCase     :			List[str]  							=      True

          __UpperCAmelCase     :			List[str]  							=      model_class(snake_case      ).to(snake_case      )
          model.train()

          __UpperCAmelCase     :			List[Any]  							=      model(snake_case							,			mask_labels=snake_case							,			class_labels=snake_case      )

          __UpperCAmelCase     :			str  							=      outputs.encoder_hidden_states[0]
          encoder_hidden_states.retain_grad()

          __UpperCAmelCase     :			List[str]  							=      outputs.pixel_decoder_hidden_states[0]
          pixel_decoder_hidden_states.retain_grad()

          __UpperCAmelCase     :			Optional[int]  							=      outputs.transformer_decoder_hidden_states[0]
          transformer_decoder_hidden_states.retain_grad()

          __UpperCAmelCase     :			Optional[Any]  							=      outputs.attentions[0]
          attentions.retain_grad()

          outputs.loss.backward(retain_graph=snake_case      )

          self.assertIsNotNone(encoder_hidden_states.grad      )
          self.assertIsNotNone(pixel_decoder_hidden_states.grad      )
          self.assertIsNotNone(transformer_decoder_hidden_states.grad      )
          self.assertIsNotNone(attentions.grad      )



__UpperCAmelCase      :List[str]			    =       1e-4




def      _a     (						):





     '''simple docstring'''






     __UpperCAmelCase     :			Optional[int]  							=      Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''  )
     return image



@require_vision
@slow
class      a    (		unittest.TestCase	):


     """simple docstring"""






     @cached_property
     def 			lowerCamelCase__			(    self					:		Any      )						->							int:
          return "facebook/mask2former-swin-small-coco-instance"

     @cached_property
     def 			lowerCamelCase__			(    self					:		List[str]      )						->							str:
          return MaskaFormerImageProcessor.from_pretrained(self.model_checkpoints      ) if is_vision_available() else None

     def 			lowerCamelCase__			(    self					:		Any      )						->							Optional[int]:
          __UpperCAmelCase     :			Any  							=      MaskaFormerModel.from_pretrained(self.model_checkpoints      ).to(snake_case      )
          __UpperCAmelCase     :			Dict  							=      self.default_image_processor
          __UpperCAmelCase     :			Dict  							=      prepare_img()
          __UpperCAmelCase     :			Dict  							=      image_processor(snake_case							,			return_tensors='''pt'''      ).to(snake_case      )
          __UpperCAmelCase     :			int  							=      inputs['''pixel_values'''].shape
          # check size is divisible by 32
          self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0      )
          # check size
          self.assertEqual(snake_case							,			(1, 3, 384, 384)      )

          with torch.no_grad():
               __UpperCAmelCase     :			Tuple  							=      model(**snake_case      )

          __UpperCAmelCase     :			Dict  							=      torch.tensor(
              [[-0.2_790, -1.0_717, -1.1_668], [-0.5_128, -0.3_128, -0.4_987], [-0.5_832, 0.1_971, -0.0_197]]      ).to(snake_case      )
          self.assertTrue(
              torch.allclose(
                  outputs.encoder_last_hidden_state[0, 0, :3, :3]							,			snake_case							,			atol=snake_case      )      )

          __UpperCAmelCase     :			str  							=      torch.tensor(
              [[0.8_973, 1.1_847, 1.1_776], [1.1_934, 1.5_040, 1.5_128], [1.1_153, 1.4_486, 1.4_951]]      ).to(snake_case      )
          self.assertTrue(
              torch.allclose(
                  outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3]							,			snake_case							,			atol=snake_case      )      )

          __UpperCAmelCase     :			Tuple  							=      torch.tensor(
              [[2.1_152, 1.7_000, -0.8_603], [1.5_808, 1.8_004, -0.9_353], [1.6_043, 1.7_495, -0.5_999]]      ).to(snake_case      )
          self.assertTrue(
              torch.allclose(
                  outputs.transformer_decoder_last_hidden_state[0, :3, :3]							,			snake_case							,			atol=snake_case      )      )

     def 			lowerCamelCase__			(    self					:		Optional[int]      )						->							int:
          __UpperCAmelCase     :			List[Any]  							=      MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints      ).to(snake_case      ).eval()
          __UpperCAmelCase     :			List[Any]  							=      self.default_image_processor
          __UpperCAmelCase     :			Dict  							=      prepare_img()
          __UpperCAmelCase     :			Dict  							=      image_processor(snake_case							,			return_tensors='''pt'''      ).to(snake_case      )
          __UpperCAmelCase     :			List[str]  							=      inputs['''pixel_values'''].shape
          # check size is divisible by 32
          self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0      )
          # check size
          self.assertEqual(snake_case							,			(1, 3, 384, 384)      )

          with torch.no_grad():
               __UpperCAmelCase     :			Optional[int]  							=      model(**snake_case      )
          # masks_queries_logits
          __UpperCAmelCase     :			List[str]  							=      outputs.masks_queries_logits
          self.assertEqual(
              masks_queries_logits.shape							,			(1, model.config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4)      )
          __UpperCAmelCase     :			Optional[Any]  							=      [
              [-8.7_839, -9.0_056, -8.8_121],
              [-7.4_104, -7.0_313, -6.5_401],
              [-6.6_105, -6.3_427, -6.4_675],
          ]
          __UpperCAmelCase     :			Dict  							=      torch.tensor(snake_case      ).to(snake_case      )
          self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]							,			snake_case							,			atol=snake_case      )      )
          # class_queries_logits
          __UpperCAmelCase     :			Optional[Any]  							=      outputs.class_queries_logits
          self.assertEqual(class_queries_logits.shape							,			(1, model.config.num_queries, model.config.num_labels + 1)      )
          __UpperCAmelCase     :			Dict  							=      torch.tensor(
              [
                  [1.8_324, -8.0_835, -4.1_922],
                  [0.8_450, -9.0_050, -3.6_053],
                  [0.3_045, -7.7_293, -3.0_275],
              ]      ).to(snake_case      )
          self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]							,			snake_case							,			atol=snake_case      )      )






     def 			lowerCamelCase__			(    self					:		Optional[int]      )						->							List[Any]:
          __UpperCAmelCase     :			Union[str, Any]  							=      MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints      ).to(snake_case      ).eval()
          __UpperCAmelCase     :			Tuple  							=      self.default_image_processor

          __UpperCAmelCase     :			Union[str, Any]  							=      image_processor(
              [np.zeros((3, 800, 1333)      ), np.zeros((3, 800, 1333)      )]							,			segmentation_maps=[np.zeros((384, 384)      ).astype(np.floataa      ), np.zeros((384, 384)      ).astype(np.floataa      )]							,			return_tensors='''pt'''							,			)

          __UpperCAmelCase     :			List[Any]  							=      inputs['''pixel_values'''].to(snake_case      )
          __UpperCAmelCase     :			int  							=      [el.to(snake_case      ) for el in inputs['''mask_labels''']]
          __UpperCAmelCase     :			Optional[Any]  							=      [el.to(snake_case      ) for el in inputs['''class_labels''']]

          with torch.no_grad():
               __UpperCAmelCase     :			Tuple  							=      model(**snake_case      )

          self.assertTrue(outputs.loss is not None      )



'''simple docstring'''



from abc import ABC, abstractmethod
from typing import List, Optional



class      a    (		_a	):


     """simple docstring"""






     def __init__(    self					:		Optional[Any]      )						->							int:
          # test for the above condition
          self.test()

     def 			lowerCamelCase__			(    self					:		Dict      )						->							List[str]:
          __UpperCAmelCase     :			Optional[int]  							=      0
          __UpperCAmelCase     :			Any  							=      False
          while not completed:
               if counter == 1:
                    self.reset()
               __UpperCAmelCase     :			Optional[int]  							=      self.advance()
               if not self.does_advance(snake_case      ):
                    raise Exception(
                        '''Custom Constraint is not defined correctly. self.does_advance(self.advance()) must be true.'''      )

               __UpperCAmelCase							,       __UpperCAmelCase							,       __UpperCAmelCase     :			int  							=      self.update(snake_case      )
               counter += 1

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

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

     @abstractmethod
     def 			lowerCamelCase__			(    self					:		Union[str, Any]      )						->							List[str]:
          raise NotImplementedError(
              f'{self.__class__} is an abstract class. Only classes inheriting this class can be called.'      )

     @abstractmethod
     def 			lowerCamelCase__			(    self					:		Optional[int]							,			snake_case					:		int      )						->							Optional[int]:
          raise NotImplementedError(
              f'{self.__class__} is an abstract class. Only classes inheriting this class can be called.'      )

     @abstractmethod
     def 			lowerCamelCase__			(    self					:		List[Any]							,			snake_case					:		int      )						->							int:
          raise NotImplementedError(
              f'{self.__class__} is an abstract class. Only classes inheriting this class can be called.'      )

     @abstractmethod
     def 			lowerCamelCase__			(    self					:		int      )						->							Optional[int]:
          raise NotImplementedError(
              f'{self.__class__} is an abstract class. Only classes inheriting this class can be called.'      )

     @abstractmethod
     def 			lowerCamelCase__			(    self					:		int      )						->							Tuple:
          raise NotImplementedError(
              f'{self.__class__} is an abstract class. Only classes inheriting this class can be called.'      )






     @abstractmethod
     def 			lowerCamelCase__			(    self					:		Union[str, Any]							,			snake_case					:		List[Any]=False      )						->							Any:
          raise NotImplementedError(
              f'{self.__class__} is an abstract class. Only classes inheriting this class can be called.'      )



class      a    (		_a	):


     """simple docstring"""






     def __init__(    self					:		int							,			snake_case					:		List[int]      )						->							Tuple:
          super(snake_case							,			self      ).__init__()

          if not isinstance(snake_case							,			snake_case      ) or len(snake_case      ) == 0:
               raise ValueError(f'`token_ids` has to be a non-empty list, but is {token_ids}.'      )
          if any((not isinstance(snake_case							,			snake_case      ) 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}.'      )

          __UpperCAmelCase     :			Dict  							=      token_ids

          __UpperCAmelCase     :			Tuple  							=      len(self.token_ids      )
          __UpperCAmelCase     :			List[str]  							=      -1  # the index of the currently fulfilled step
          __UpperCAmelCase     :			int  							=      False

     def 			lowerCamelCase__			(    self					:		List[str]      )						->							str:
          if self.completed:
               return None
          return self.token_ids[self.fulfilled_idx + 1]

     def 			lowerCamelCase__			(    self					:		Any							,			snake_case					:		int      )						->							Optional[int]:
          if not isinstance(snake_case							,			snake_case      ):
               raise ValueError(f'`token_id` has to be an `int`, but is {token_id} of type {type(snake_case      )}'      )

          if self.completed:
               return False

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

     def 			lowerCamelCase__			(    self					:		Union[str, Any]							,			snake_case					:		int      )						->							Optional[int]:
          if not isinstance(snake_case							,			snake_case      ):
               raise ValueError(f'`token_id` has to be an `int`, but is {token_id} of type {type(snake_case      )}'      )

          __UpperCAmelCase     :			Any  							=      False
          __UpperCAmelCase     :			Tuple  							=      False
          __UpperCAmelCase     :			Tuple  							=      False

          if self.does_advance(snake_case      ):
               self.fulfilled_idx += 1
               __UpperCAmelCase     :			Union[str, Any]  							=      True
               if self.fulfilled_idx == (self.seqlen - 1):
                    __UpperCAmelCase     :			List[Any]  							=      True
               __UpperCAmelCase     :			Union[str, Any]  							=      completed
          else:
               # failed to make progress.
               __UpperCAmelCase     :			List[str]  							=      True
               self.reset()
          return stepped, completed, reset

     def 			lowerCamelCase__			(    self					:		int      )						->							List[Any]:
          __UpperCAmelCase     :			Union[str, Any]  							=      False
          __UpperCAmelCase     :			Union[str, Any]  							=      0

     def 			lowerCamelCase__			(    self					:		str      )						->							Optional[int]:
          return self.seqlen - (self.fulfilled_idx + 1)






     def 			lowerCamelCase__			(    self					:		int							,			snake_case					:		Dict=False      )						->							List[str]:
          __UpperCAmelCase     :			List[str]  							=      PhrasalConstraint(self.token_ids      )

          if stateful:
               __UpperCAmelCase     :			int  							=      self.seqlen
               __UpperCAmelCase     :			Optional[Any]  							=      self.fulfilled_idx
               __UpperCAmelCase     :			List[Any]  							=      self.completed

          return new_constraint



class      a    :


     """simple docstring"""






     def __init__(    self					:		List[str]							,			snake_case					:		List[List[int]]							,			snake_case					:		Dict=True      )						->							Any:
          __UpperCAmelCase     :			List[Any]  							=      max([len(snake_case      ) for one in nested_token_ids]      )

          __UpperCAmelCase     :			Union[str, Any]  							=      {}
          for token_ids in nested_token_ids:
               __UpperCAmelCase     :			List[str]  							=      root
               for tidx, token_id in enumerate(snake_case      ):
                    if token_id not in level:
                         __UpperCAmelCase     :			List[Any]  							=      {}

                    __UpperCAmelCase     :			Tuple  							=      level[token_id]

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

          __UpperCAmelCase     :			Tuple  							=      root

     def 			lowerCamelCase__			(    self					:		Dict							,			snake_case					:		List[str]      )						->							List[Any]:
          __UpperCAmelCase     :			Dict  							=      self.trie

          for current_token in current_seq:
               __UpperCAmelCase     :			List[str]  							=      start[current_token]

          __UpperCAmelCase     :			str  							=      list(start.keys()      )

          return next_tokens

     def 			lowerCamelCase__			(    self					:		Optional[Any]							,			snake_case					:		List[str]      )						->							Any:
          __UpperCAmelCase     :			Optional[Any]  							=      self.next_tokens(snake_case      )

          return len(snake_case      ) == 0

     def 			lowerCamelCase__			(    self					:		Union[str, Any]							,			snake_case					:		int      )						->							Optional[int]:
          __UpperCAmelCase     :			str  							=      list(root.values()      )
          if len(snake_case      ) == 0:
               return 1
          else:
               return sum([self.count_leaves(snake_case      ) for nn in next_nodes]      )






     def 			lowerCamelCase__			(    self					:		Optional[int]							,			snake_case					:		int							,			snake_case					:		Dict      )						->							str:
          __UpperCAmelCase     :			Dict  							=      self.count_leaves(snake_case      )
          return len(snake_case      ) != leaf_count



class      a    (		_a	):


     """simple docstring"""






     def __init__(    self					:		Union[str, Any]							,			snake_case					:		List[List[int]]      )						->							str:
          super(snake_case							,			self      ).__init__()

          if not isinstance(snake_case							,			snake_case      ) or len(snake_case      ) == 0:
               raise ValueError(f'`nested_token_ids` has to be a non-empty list, but is {nested_token_ids}.'      )
          if any(not isinstance(snake_case							,			snake_case      ) 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(snake_case							,			snake_case      ) 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}.'      )

          __UpperCAmelCase     :			Optional[int]  							=      DisjunctiveTrie(snake_case      )
          __UpperCAmelCase     :			Tuple  							=      nested_token_ids

          __UpperCAmelCase     :			List[Any]  							=      self.trie.max_height
          __UpperCAmelCase     :			List[Any]  							=      []
          __UpperCAmelCase     :			Union[str, Any]  							=      False

     def 			lowerCamelCase__			(    self					:		int      )						->							List[Any]:
          __UpperCAmelCase     :			Optional[int]  							=      self.trie.next_tokens(self.current_seq      )

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

     def 			lowerCamelCase__			(    self					:		Tuple							,			snake_case					:		int      )						->							Dict:
          if not isinstance(snake_case							,			snake_case      ):
               raise ValueError(f'`token_id` is supposed to be type `int`, but is {token_id} of type {type(snake_case      )}'      )

          __UpperCAmelCase     :			List[str]  							=      self.trie.next_tokens(self.current_seq      )

          return token_id in next_tokens

     def 			lowerCamelCase__			(    self					:		Any							,			snake_case					:		int      )						->							Tuple:
          if not isinstance(snake_case							,			snake_case      ):
               raise ValueError(f'`token_id` is supposed to be type `int`, but is {token_id} of type {type(snake_case      )}'      )

          __UpperCAmelCase     :			Dict  							=      False
          __UpperCAmelCase     :			Optional[Any]  							=      False
          __UpperCAmelCase     :			str  							=      False

          if self.does_advance(snake_case      ):
               self.current_seq.append(snake_case      )
               __UpperCAmelCase     :			int  							=      True
          else:
               __UpperCAmelCase     :			Optional[Any]  							=      True
               self.reset()

          __UpperCAmelCase     :			Optional[Any]  							=      self.trie.reached_leaf(self.current_seq      )
          __UpperCAmelCase     :			Tuple  							=      completed

          return stepped, completed, reset

     def 			lowerCamelCase__			(    self					:		Dict      )						->							Optional[Any]:
          __UpperCAmelCase     :			str  							=      False
          __UpperCAmelCase     :			Tuple  							=      []

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






     def 			lowerCamelCase__			(    self					:		Any							,			snake_case					:		Optional[int]=False      )						->							Tuple:
          __UpperCAmelCase     :			str  							=      DisjunctiveConstraint(self.token_ids      )

          if stateful:
               __UpperCAmelCase     :			Tuple  							=      self.seqlen
               __UpperCAmelCase     :			Dict  							=      self.current_seq
               __UpperCAmelCase     :			str  							=      self.completed

          return new_constraint



class      a    :


     """simple docstring"""






     def __init__(    self					:		Union[str, Any]							,			snake_case					:		List[Constraint]      )						->							Union[str, Any]:
          __UpperCAmelCase     :			Optional[Any]  							=      constraints

          # max # of steps required to fulfill a given constraint
          __UpperCAmelCase     :			int  							=      max([c.seqlen for c in constraints]      )
          __UpperCAmelCase     :			int  							=      len(snake_case      )
          __UpperCAmelCase     :			Optional[int]  							=      False

          self.init_state()

     def 			lowerCamelCase__			(    self					:		List[str]      )						->							List[str]:
          __UpperCAmelCase     :			List[Any]  							=      []
          __UpperCAmelCase     :			List[str]  							=      None
          __UpperCAmelCase     :			str  							=      [constraint.copy(stateful=snake_case      ) for constraint in self.constraints]

     def 			lowerCamelCase__			(    self					:		Optional[int]      )						->							Optional[Any]:
          __UpperCAmelCase     :			Dict  							=      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 			lowerCamelCase__			(    self					:		Tuple      )						->							int:
          __UpperCAmelCase     :			int  							=      []
          if self.inprogress_constraint is None:
               for constraint in self.pending_constraints:  # "pending" == "unfulfilled yet"
                    __UpperCAmelCase     :			Optional[int]  							=      constraint.advance()
                    if isinstance(snake_case							,			snake_case      ):
                         token_list.append(snake_case      )
                    elif isinstance(snake_case							,			snake_case      ):
                         token_list.extend(snake_case      )
          else:
               __UpperCAmelCase     :			Optional[Any]  							=      self.inprogress_constraint.advance()
               if isinstance(snake_case							,			snake_case      ):
                    token_list.append(snake_case      )
               elif isinstance(snake_case							,			snake_case      ):
                    token_list.extend(snake_case      )

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

     def 			lowerCamelCase__			(    self					:		List[str]							,			snake_case					:		Optional[List[int]]      )						->							Optional[int]:
          self.init_state()

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

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

     def 			lowerCamelCase__			(    self					:		List[str]							,			snake_case					:		int      )						->							List[str]:
          if not isinstance(snake_case							,			snake_case      ):
               raise ValueError(f'`token_id` should be an `int`, but is `{token_id}`.'      )

          __UpperCAmelCase							,       __UpperCAmelCase     :			str  							=      False, False

          if self.completed:
               __UpperCAmelCase     :			Union[str, Any]  							=      True
               __UpperCAmelCase     :			str  							=      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

               __UpperCAmelCase							,       __UpperCAmelCase							,       __UpperCAmelCase     :			Any  							=      self.inprogress_constraint.update(snake_case      )
               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=snake_case      )      )
                    __UpperCAmelCase     :			Optional[Any]  							=      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      )
                    __UpperCAmelCase     :			str  							=      None

                    if len(self.pending_constraints      ) == 0:
                         # we're done!
                         __UpperCAmelCase     :			Optional[int]  							=      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(snake_case      ):
                         __UpperCAmelCase							,       __UpperCAmelCase							,       __UpperCAmelCase     :			Union[str, Any]  							=      pending_constraint.update(snake_case      )

                         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(snake_case      )
                              __UpperCAmelCase     :			Tuple  							=      None

                         if not complete and stepped:
                              __UpperCAmelCase     :			List[Any]  							=      pending_constraint

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

                              __UpperCAmelCase     :			Optional[int]  							=      (
                                  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.

                                   __UpperCAmelCase     :			Any  							=      True

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

          return complete, stepped






     def 			lowerCamelCase__			(    self					:		int							,			snake_case					:		Optional[int]=True      )						->							Optional[int]:
          __UpperCAmelCase     :			Union[str, Any]  							=      ConstraintListState(self.constraints      )  # we actually never though self.constraints objects
          # throughout this process. So it's at initialization state.

          if stateful:
               __UpperCAmelCase     :			str  							=      [
                   constraint.copy(stateful=snake_case      ) for constraint in self.complete_constraints
               ]
               if self.inprogress_constraint is not None:
                    __UpperCAmelCase     :			Union[str, Any]  							=      self.inprogress_constraint.copy(stateful=snake_case      )
               __UpperCAmelCase     :			Tuple  							=      [constraint.copy() for constraint in self.pending_constraints]

          return new_state