



"""simple docstring"""







import unittest

from transformers import AutoTokenizer, NystromformerConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
     import torch

     from transformers import (
         NystromformerForMaskedLM,
         NystromformerForMultipleChoice,
         NystromformerForQuestionAnswering,
         NystromformerForSequenceClassification,
         NystromformerForTokenClassification,
         NystromformerModel,
     )
     from transformers.models.nystromformer.modeling_nystromformer import NYSTROMFORMER_PRETRAINED_MODEL_ARCHIVE_LIST







class       UpperCamelCase__   :




      """simple docstring"""




      def __init__(      self				:      List[str]    ,			SCREAMING_SNAKE_CASE_				:      int    ,			SCREAMING_SNAKE_CASE_				:      Optional[int]=1_3    ,			SCREAMING_SNAKE_CASE_				:      Dict=7    ,			SCREAMING_SNAKE_CASE_				:      int=True    ,			SCREAMING_SNAKE_CASE_				:      int=True    ,			SCREAMING_SNAKE_CASE_				:      int=True    ,			SCREAMING_SNAKE_CASE_				:      List[Any]=True    ,			SCREAMING_SNAKE_CASE_				:      List[str]=9_9    ,			SCREAMING_SNAKE_CASE_				:      Dict=3_2    ,			SCREAMING_SNAKE_CASE_				:      Optional[int]=5    ,			SCREAMING_SNAKE_CASE_				:      Tuple=4    ,			SCREAMING_SNAKE_CASE_				:      Dict=3_7    ,			SCREAMING_SNAKE_CASE_				:      Dict="gelu"    ,			SCREAMING_SNAKE_CASE_				:      str=0.1    ,			SCREAMING_SNAKE_CASE_				:      Union[str, Any]=0.1    ,			SCREAMING_SNAKE_CASE_				:      Any=5_1_2    ,			SCREAMING_SNAKE_CASE_				:      Dict=1_6    ,			SCREAMING_SNAKE_CASE_				:      str=2    ,			SCREAMING_SNAKE_CASE_				:      Any=0.02    ,			SCREAMING_SNAKE_CASE_				:      List[Any]=3    ,			SCREAMING_SNAKE_CASE_				:      Dict=4    ,			SCREAMING_SNAKE_CASE_				:      str=None    ,			):
        lowerCAmelCase_   :				Dict								= parent
        lowerCAmelCase_   :				int								= batch_size
        lowerCAmelCase_   :				List[Any]								= seq_length
        lowerCAmelCase_   :				str								= is_training
        lowerCAmelCase_   :				Tuple								= use_input_mask
        lowerCAmelCase_   :				int								= use_token_type_ids
        lowerCAmelCase_   :				Tuple								= use_labels
        lowerCAmelCase_   :				Union[str, Any]								= vocab_size
        lowerCAmelCase_   :				Union[str, Any]								= hidden_size
        lowerCAmelCase_   :				Optional[Any]								= num_hidden_layers
        lowerCAmelCase_   :				int								= num_attention_heads
        lowerCAmelCase_   :				Tuple								= intermediate_size
        lowerCAmelCase_   :				Tuple								= hidden_act
        lowerCAmelCase_   :				List[Any]								= hidden_dropout_prob
        lowerCAmelCase_   :				List[str]								= attention_probs_dropout_prob
        lowerCAmelCase_   :				Tuple								= max_position_embeddings
        lowerCAmelCase_   :				List[Any]								= type_vocab_size
        lowerCAmelCase_   :				Union[str, Any]								= type_sequence_label_size
        lowerCAmelCase_   :				List[Any]								= initializer_range
        lowerCAmelCase_   :				Optional[int]								= num_labels
        lowerCAmelCase_   :				Any								= num_choices
        lowerCAmelCase_   :				Optional[Any]								= scope



      def      SCREAMING_SNAKE_CASE__ (      self				:      Any       ):
        lowerCAmelCase_   :				List[Any]								= ids_tensor([self.batch_size, self.seq_length]    ,			self.vocab_size       )

        lowerCAmelCase_   :				Any								= None
        if self.use_input_mask:
          lowerCAmelCase_   :				Any								= random_attention_mask([self.batch_size, self.seq_length]       )

        lowerCAmelCase_   :				List[str]								= None
        if self.use_token_type_ids:
          lowerCAmelCase_   :				List[str]								= ids_tensor([self.batch_size, self.seq_length]    ,			self.type_vocab_size       )

        lowerCAmelCase_   :				Union[str, Any]								= None
        lowerCAmelCase_   :				int								= None
        lowerCAmelCase_   :				Union[str, Any]								= None
        if self.use_labels:
          lowerCAmelCase_   :				Tuple								= ids_tensor([self.batch_size]    ,			self.type_sequence_label_size       )
          lowerCAmelCase_   :				Union[str, Any]								= ids_tensor([self.batch_size, self.seq_length]    ,			self.num_labels       )
          lowerCAmelCase_   :				Optional[Any]								= ids_tensor([self.batch_size]    ,			self.num_choices       )

        lowerCAmelCase_   :				Dict								= self.get_config()

        return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels



      def      SCREAMING_SNAKE_CASE__ (      self				:      Optional[int]       ):
        return NystromformerConfig(
            vocab_size=self.vocab_size    ,			hidden_size=self.hidden_size    ,			num_hidden_layers=self.num_hidden_layers    ,			num_attention_heads=self.num_attention_heads    ,			intermediate_size=self.intermediate_size    ,			hidden_act=self.hidden_act    ,			hidden_dropout_prob=self.hidden_dropout_prob    ,			attention_probs_dropout_prob=self.attention_probs_dropout_prob    ,			max_position_embeddings=self.max_position_embeddings    ,			type_vocab_size=self.type_vocab_size    ,			is_decoder=SCREAMING_SNAKE_CASE_    ,			initializer_range=self.initializer_range    ,			)



      def      SCREAMING_SNAKE_CASE__ (      self				:      Dict    ,			SCREAMING_SNAKE_CASE_				:      Optional[Any]    ,			SCREAMING_SNAKE_CASE_				:      Optional[int]    ,			SCREAMING_SNAKE_CASE_				:      List[str]    ,			SCREAMING_SNAKE_CASE_				:      Any    ,			SCREAMING_SNAKE_CASE_				:      Any    ,			SCREAMING_SNAKE_CASE_				:      Tuple    ,			SCREAMING_SNAKE_CASE_				:      Optional[int]       ):
        lowerCAmelCase_   :				Optional[int]								= NystromformerModel(config=SCREAMING_SNAKE_CASE_       )
        model.to(SCREAMING_SNAKE_CASE_       )
        model.eval()
        lowerCAmelCase_   :				List[Any]								= model(SCREAMING_SNAKE_CASE_    ,			attention_mask=SCREAMING_SNAKE_CASE_    ,			token_type_ids=SCREAMING_SNAKE_CASE_       )
        lowerCAmelCase_   :				Union[str, Any]								= model(SCREAMING_SNAKE_CASE_    ,			token_type_ids=SCREAMING_SNAKE_CASE_       )
        lowerCAmelCase_   :				Tuple								= model(SCREAMING_SNAKE_CASE_       )
        self.parent.assertEqual(result.last_hidden_state.shape    ,			(self.batch_size, self.seq_length, self.hidden_size)       )



      def      SCREAMING_SNAKE_CASE__ (      self				:      List[Any]    ,			SCREAMING_SNAKE_CASE_				:      Optional[int]    ,			SCREAMING_SNAKE_CASE_				:      str    ,			SCREAMING_SNAKE_CASE_				:      Tuple    ,			SCREAMING_SNAKE_CASE_				:      Optional[Any]    ,			SCREAMING_SNAKE_CASE_				:      str    ,			SCREAMING_SNAKE_CASE_				:      Union[str, Any]    ,			SCREAMING_SNAKE_CASE_				:      Any       ):
        lowerCAmelCase_   :				Dict								= NystromformerForMaskedLM(config=SCREAMING_SNAKE_CASE_       )
        model.to(SCREAMING_SNAKE_CASE_       )
        model.eval()
        lowerCAmelCase_   :				Union[str, Any]								= model(SCREAMING_SNAKE_CASE_    ,			attention_mask=SCREAMING_SNAKE_CASE_    ,			token_type_ids=SCREAMING_SNAKE_CASE_    ,			labels=SCREAMING_SNAKE_CASE_       )
        self.parent.assertEqual(result.logits.shape    ,			(self.batch_size, self.seq_length, self.vocab_size)       )



      def      SCREAMING_SNAKE_CASE__ (      self				:      List[str]    ,			SCREAMING_SNAKE_CASE_				:      int    ,			SCREAMING_SNAKE_CASE_				:      Optional[Any]    ,			SCREAMING_SNAKE_CASE_				:      Union[str, Any]    ,			SCREAMING_SNAKE_CASE_				:      int    ,			SCREAMING_SNAKE_CASE_				:      int    ,			SCREAMING_SNAKE_CASE_				:      List[str]    ,			SCREAMING_SNAKE_CASE_				:      Union[str, Any]       ):
        lowerCAmelCase_   :				List[str]								= NystromformerForQuestionAnswering(config=SCREAMING_SNAKE_CASE_       )
        model.to(SCREAMING_SNAKE_CASE_       )
        model.eval()
        lowerCAmelCase_   :				List[str]								= model(
            SCREAMING_SNAKE_CASE_    ,			attention_mask=SCREAMING_SNAKE_CASE_    ,			token_type_ids=SCREAMING_SNAKE_CASE_    ,			start_positions=SCREAMING_SNAKE_CASE_    ,			end_positions=SCREAMING_SNAKE_CASE_    ,			)
        self.parent.assertEqual(result.start_logits.shape    ,			(self.batch_size, self.seq_length)       )
        self.parent.assertEqual(result.end_logits.shape    ,			(self.batch_size, self.seq_length)       )



      def      SCREAMING_SNAKE_CASE__ (      self				:      int    ,			SCREAMING_SNAKE_CASE_				:      List[str]    ,			SCREAMING_SNAKE_CASE_				:      Dict    ,			SCREAMING_SNAKE_CASE_				:      str    ,			SCREAMING_SNAKE_CASE_				:      Optional[int]    ,			SCREAMING_SNAKE_CASE_				:      Optional[Any]    ,			SCREAMING_SNAKE_CASE_				:      Optional[int]    ,			SCREAMING_SNAKE_CASE_				:      str       ):
        lowerCAmelCase_   :				Optional[int]								= self.num_labels
        lowerCAmelCase_   :				Dict								= NystromformerForSequenceClassification(SCREAMING_SNAKE_CASE_       )
        model.to(SCREAMING_SNAKE_CASE_       )
        model.eval()
        lowerCAmelCase_   :				Union[str, Any]								= model(SCREAMING_SNAKE_CASE_    ,			attention_mask=SCREAMING_SNAKE_CASE_    ,			token_type_ids=SCREAMING_SNAKE_CASE_    ,			labels=SCREAMING_SNAKE_CASE_       )
        self.parent.assertEqual(result.logits.shape    ,			(self.batch_size, self.num_labels)       )



      def      SCREAMING_SNAKE_CASE__ (      self				:      Any    ,			SCREAMING_SNAKE_CASE_				:      Optional[int]    ,			SCREAMING_SNAKE_CASE_				:      str    ,			SCREAMING_SNAKE_CASE_				:      Optional[Any]    ,			SCREAMING_SNAKE_CASE_				:      List[Any]    ,			SCREAMING_SNAKE_CASE_				:      str    ,			SCREAMING_SNAKE_CASE_				:      Dict    ,			SCREAMING_SNAKE_CASE_				:      Dict       ):
        lowerCAmelCase_   :				List[Any]								= self.num_labels
        lowerCAmelCase_   :				Dict								= NystromformerForTokenClassification(config=SCREAMING_SNAKE_CASE_       )
        model.to(SCREAMING_SNAKE_CASE_       )
        model.eval()
        lowerCAmelCase_   :				List[Any]								= model(SCREAMING_SNAKE_CASE_    ,			attention_mask=SCREAMING_SNAKE_CASE_    ,			token_type_ids=SCREAMING_SNAKE_CASE_    ,			labels=SCREAMING_SNAKE_CASE_       )
        self.parent.assertEqual(result.logits.shape    ,			(self.batch_size, self.seq_length, self.num_labels)       )



      def      SCREAMING_SNAKE_CASE__ (      self				:      Optional[Any]    ,			SCREAMING_SNAKE_CASE_				:      Any    ,			SCREAMING_SNAKE_CASE_				:      Union[str, Any]    ,			SCREAMING_SNAKE_CASE_				:      Union[str, Any]    ,			SCREAMING_SNAKE_CASE_				:      Any    ,			SCREAMING_SNAKE_CASE_				:      Optional[Any]    ,			SCREAMING_SNAKE_CASE_				:      List[str]    ,			SCREAMING_SNAKE_CASE_				:      Union[str, Any]       ):
        lowerCAmelCase_   :				List[str]								= self.num_choices
        lowerCAmelCase_   :				Optional[Any]								= NystromformerForMultipleChoice(config=SCREAMING_SNAKE_CASE_       )
        model.to(SCREAMING_SNAKE_CASE_       )
        model.eval()
        lowerCAmelCase_   :				int								= input_ids.unsqueeze(1       ).expand(-1    ,			self.num_choices    ,			-1       ).contiguous()
        lowerCAmelCase_   :				Dict								= token_type_ids.unsqueeze(1       ).expand(-1    ,			self.num_choices    ,			-1       ).contiguous()
        lowerCAmelCase_   :				Dict								= input_mask.unsqueeze(1       ).expand(-1    ,			self.num_choices    ,			-1       ).contiguous()
        lowerCAmelCase_   :				Union[str, Any]								= model(
            SCREAMING_SNAKE_CASE_    ,			attention_mask=SCREAMING_SNAKE_CASE_    ,			token_type_ids=SCREAMING_SNAKE_CASE_    ,			labels=SCREAMING_SNAKE_CASE_    ,			)
        self.parent.assertEqual(result.logits.shape    ,			(self.batch_size, self.num_choices)       )





      def      SCREAMING_SNAKE_CASE__ (      self				:      List[str]       ):
        lowerCAmelCase_   :				Optional[Any]								= self.prepare_config_and_inputs()
        (
            (
            lowerCAmelCase_
        )						,(
            lowerCAmelCase_
        )						,(
            lowerCAmelCase_
        )						,(
            lowerCAmelCase_
        )						,(
            lowerCAmelCase_
        )						,(
            lowerCAmelCase_
        )						,(
            lowerCAmelCase_
        )						,
        )   :				Dict								= config_and_inputs
        lowerCAmelCase_   :				List[str]								= {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
        return config, inputs_dict







@require_torch
class       UpperCamelCase__   (	lowercase_,					lowercase_,					unittest.TestCase  ):




      """simple docstring"""




      _SCREAMING_SNAKE_CASE  				=     (
          (
              NystromformerModel,
              NystromformerForMaskedLM,
              NystromformerForMultipleChoice,
              NystromformerForQuestionAnswering,
              NystromformerForSequenceClassification,
              NystromformerForTokenClassification,
          )
          if is_torch_available()
          else ()
      )
      _SCREAMING_SNAKE_CASE  				=     (
          {
              """feature-extraction""": NystromformerModel,
              """fill-mask""": NystromformerForMaskedLM,
              """question-answering""": NystromformerForQuestionAnswering,
              """text-classification""": NystromformerForSequenceClassification,
              """token-classification""": NystromformerForTokenClassification,
              """zero-shot""": NystromformerForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )
      _SCREAMING_SNAKE_CASE  				=     False
      _SCREAMING_SNAKE_CASE  				=     False



      def      SCREAMING_SNAKE_CASE__ (      self				:      int       ):
        lowerCAmelCase_   :				str								= NystromformerModelTester(self       )
        lowerCAmelCase_   :				List[Any]								= ConfigTester(self    ,			config_class=SCREAMING_SNAKE_CASE_    ,			hidden_size=3_7       )



      def      SCREAMING_SNAKE_CASE__ (      self				:      Optional[int]       ):
        self.config_tester.run_common_tests()



      def      SCREAMING_SNAKE_CASE__ (      self				:      Dict       ):
        lowerCAmelCase_   :				List[str]								= self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_       )



      def      SCREAMING_SNAKE_CASE__ (      self				:      List[Any]       ):
        lowerCAmelCase_   :				List[Any]								= self.model_tester.prepare_config_and_inputs()
        for type in ["absolute", "relative_key", "relative_key_query"]:
          lowerCAmelCase_   :				Tuple								= type
          self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_       )



      def      SCREAMING_SNAKE_CASE__ (      self				:      List[Any]       ):
        lowerCAmelCase_   :				Optional[Any]								= self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_masked_lm(*SCREAMING_SNAKE_CASE_       )



      def      SCREAMING_SNAKE_CASE__ (      self				:      List[Any]       ):
        lowerCAmelCase_   :				Dict								= self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_multiple_choice(*SCREAMING_SNAKE_CASE_       )



      def      SCREAMING_SNAKE_CASE__ (      self				:      Optional[Any]       ):
        lowerCAmelCase_   :				Union[str, Any]								= self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_question_answering(*SCREAMING_SNAKE_CASE_       )



      def      SCREAMING_SNAKE_CASE__ (      self				:      Tuple       ):
        lowerCAmelCase_   :				List[Any]								= self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_sequence_classification(*SCREAMING_SNAKE_CASE_       )



      def      SCREAMING_SNAKE_CASE__ (      self				:      Dict       ):
        lowerCAmelCase_   :				Tuple								= self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_token_classification(*SCREAMING_SNAKE_CASE_       )





      @slow
      def      SCREAMING_SNAKE_CASE__ (      self				:      List[Any]       ):
        for model_name in NYSTROMFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          lowerCAmelCase_   :				str								= NystromformerModel.from_pretrained(SCREAMING_SNAKE_CASE_       )
          self.assertIsNotNone(SCREAMING_SNAKE_CASE_       )







@require_torch
class       UpperCamelCase__   (	unittest.TestCase  ):




      """simple docstring"""




      @slow
      def      SCREAMING_SNAKE_CASE__ (      self				:      List[str]       ):
        lowerCAmelCase_   :				Optional[int]								= NystromformerModel.from_pretrained('uw-madison/nystromformer-512'       )
        lowerCAmelCase_   :				Tuple								= torch.tensor([[0, 1, 2, 3, 4, 5]]       )

        with torch.no_grad():
          lowerCAmelCase_   :				List[str]								= model(SCREAMING_SNAKE_CASE_       )[0]

        lowerCAmelCase_   :				str								= torch.Size((1, 6, 7_6_8)       )
        self.assertEqual(output.shape    ,			SCREAMING_SNAKE_CASE_       )

        lowerCAmelCase_   :				List[Any]								= torch.tensor(
            [[[-0.45_32, -0.09_36, 0.51_37], [-0.26_76, 0.06_28, 0.61_86], [-0.36_29, -0.17_26, 0.47_16]]]       )

        self.assertTrue(torch.allclose(output[:, :3, :3]    ,			SCREAMING_SNAKE_CASE_    ,			atol=1E-4       )       )





      @slow
      def      SCREAMING_SNAKE_CASE__ (      self				:      Any       ):
        lowerCAmelCase_   :				List[str]								= 'the [MASK] of Belgium is Brussels'

        lowerCAmelCase_   :				Dict								= AutoTokenizer.from_pretrained('uw-madison/nystromformer-512'       )
        lowerCAmelCase_   :				Optional[int]								= NystromformerForMaskedLM.from_pretrained('uw-madison/nystromformer-512'       )

        lowerCAmelCase_   :				str								= tokenizer(SCREAMING_SNAKE_CASE_    ,			return_tensors='pt'       )

        with torch.no_grad():
          lowerCAmelCase_   :				Any								= model(encoding.input_ids       ).logits

        lowerCAmelCase_   :				Any								= token_logits[:, 2, :].argmax(-1       )[0]

        self.assertEqual(tokenizer.decode(SCREAMING_SNAKE_CASE_       )    ,			'capital'       )










"""simple docstring"""







# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import numpy as np
import torch

from ..models.clipseg import CLIPSegForImageSegmentation
from ..utils import is_vision_available, requires_backends
from .base import PipelineTool


if is_vision_available():
     from PIL import Image







class       UpperCamelCase__   (	lowercase_  ):




      """simple docstring"""




      _SCREAMING_SNAKE_CASE  				=     (
          """This is a tool that creates a segmentation mask of an image according to a label. It cannot create an image."""
          """It takes two arguments named `image` which should be the original image, and `label` which should be a text """
          """describing the elements what should be identified in the segmentation mask. The tool returns the mask."""
      )
      _SCREAMING_SNAKE_CASE  				=     """CIDAS/clipseg-rd64-refined"""
      _SCREAMING_SNAKE_CASE  				=     """image_segmenter"""
      _SCREAMING_SNAKE_CASE  				=     CLIPSegForImageSegmentation

      _SCREAMING_SNAKE_CASE  				=     ["""image""", """text"""]
      _SCREAMING_SNAKE_CASE  				=     ["""image"""]



      def __init__(      self				:      Dict    ,			*SCREAMING_SNAKE_CASE_				:      Tuple    ,			**SCREAMING_SNAKE_CASE_				:      Optional[int]       ):
        requires_backends(self    ,			['vision']       )
        super().__init__(*SCREAMING_SNAKE_CASE_    ,			**SCREAMING_SNAKE_CASE_       )



      def      SCREAMING_SNAKE_CASE__ (      self				:      Optional[int]    ,			SCREAMING_SNAKE_CASE_				:      "Image"    ,			SCREAMING_SNAKE_CASE_				:      str       ):
        return self.pre_processor(text=[label]    ,			images=[image]    ,			padding=SCREAMING_SNAKE_CASE_    ,			return_tensors='pt'       )



      def      SCREAMING_SNAKE_CASE__ (      self				:      List[str]    ,			SCREAMING_SNAKE_CASE_				:      int       ):
        with torch.no_grad():
          lowerCAmelCase_   :				List[str]								= self.model(**SCREAMING_SNAKE_CASE_       ).logits
        return logits





      def      SCREAMING_SNAKE_CASE__ (      self				:      Optional[int]    ,			SCREAMING_SNAKE_CASE_				:      Optional[Any]       ):
        lowerCAmelCase_   :				Dict								= outputs.cpu().detach().numpy()
        lowerCAmelCase_   :				Optional[Any]								= 0
        lowerCAmelCase_   :				Optional[Any]								= 1
        return Image.fromarray((array * 2_5_5).astype(np.uinta       )       )





