
import unittest

from transformers import AlbertTokenizer, AlbertTokenizerFast
from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow

from ...test_tokenization_common import TokenizerTesterMixin


lowerCamelCase_						     =					get_tests_dir('''fixtures/spiece.model''')




@require_sentencepiece
@require_tokenizers
class      __A(    __lowerCamelCase    ,				unittest.TestCase			):

  """simple docstring"""
  SCREAMING_SNAKE_CASE__         =      AlbertTokenizer
  SCREAMING_SNAKE_CASE__         =      AlbertTokenizerFast
  SCREAMING_SNAKE_CASE__         =      True
  SCREAMING_SNAKE_CASE__         =      True
  SCREAMING_SNAKE_CASE__         =      True







  def 	UpperCAmelCase_ (self						):
       super().setUp()

       # We have a SentencePiece fixture for testing
       UpperCamelCase__				=    AlbertTokenizer(SCREAMING_SNAKE_CASE_						)
       tokenizer.save_pretrained(self.tmpdirname						)







  def 	UpperCAmelCase_ (self	,      SCREAMING_SNAKE_CASE_						):
       UpperCamelCase__				=    """this is a test"""
       UpperCamelCase__				=    """this is a test"""
       return input_text, output_text







  def 	UpperCAmelCase_ (self						):
       UpperCamelCase__				=    """<pad>"""
       UpperCamelCase__				=    0

       self.assertEqual(self.get_tokenizer()._convert_token_to_id(SCREAMING_SNAKE_CASE_						)	,      SCREAMING_SNAKE_CASE_						)
       self.assertEqual(self.get_tokenizer()._convert_id_to_token(SCREAMING_SNAKE_CASE_						)	,      SCREAMING_SNAKE_CASE_						)







  def 	UpperCAmelCase_ (self						):
       UpperCamelCase__				=    list(self.get_tokenizer().get_vocab().keys()						)

       self.assertEqual(vocab_keys[0]	,      """<pad>"""						)
       self.assertEqual(vocab_keys[1]	,      """<unk>"""						)
       self.assertEqual(vocab_keys[-1]	,      """▁eloquent"""						)
       self.assertEqual(len(SCREAMING_SNAKE_CASE_						)	,      3_00_00						)







  def 	UpperCAmelCase_ (self						):
       self.assertEqual(self.get_tokenizer().vocab_size	,      3_00_00						)







  def 	UpperCAmelCase_ (self						):
       if not self.test_rust_tokenizer:
            return

       UpperCamelCase__				=    self.get_tokenizer()
       UpperCamelCase__				=    self.get_rust_tokenizer()

       UpperCamelCase__				=    """I was born in 92000, and this is falsé."""

       UpperCamelCase__				=    tokenizer.tokenize(SCREAMING_SNAKE_CASE_						)
       UpperCamelCase__				=    rust_tokenizer.tokenize(SCREAMING_SNAKE_CASE_						)
       self.assertListEqual(SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_						)

       UpperCamelCase__				=    tokenizer.encode(SCREAMING_SNAKE_CASE_	,      add_special_tokens=SCREAMING_SNAKE_CASE_						)
       UpperCamelCase__				=    rust_tokenizer.encode(SCREAMING_SNAKE_CASE_	,      add_special_tokens=SCREAMING_SNAKE_CASE_						)
       self.assertListEqual(SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_						)

       UpperCamelCase__				=    self.get_rust_tokenizer()
       UpperCamelCase__				=    tokenizer.encode(SCREAMING_SNAKE_CASE_						)
       UpperCamelCase__				=    rust_tokenizer.encode(SCREAMING_SNAKE_CASE_						)
       self.assertListEqual(SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_						)







  def 	UpperCAmelCase_ (self						):
       UpperCamelCase__				=    AlbertTokenizer(SCREAMING_SNAKE_CASE_	,      keep_accents=SCREAMING_SNAKE_CASE_						)

       UpperCamelCase__				=    tokenizer.tokenize("""This is a test"""						)
       self.assertListEqual(SCREAMING_SNAKE_CASE_	,      ["""▁this""", """▁is""", """▁a""", """▁test"""]						)

       self.assertListEqual(tokenizer.convert_tokens_to_ids(SCREAMING_SNAKE_CASE_						)	,      [48, 25, 21, 12_89]						)

       UpperCamelCase__				=    tokenizer.tokenize("""I was born in 92000, and this is falsé."""						)
       self.assertListEqual(
           SCREAMING_SNAKE_CASE_	,      ["""▁i""", """▁was""", """▁born""", """▁in""", """▁9""", """2000""", """,""", """▁and""", """▁this""", """▁is""", """▁fal""", """s""", """é""", """."""]						)
       UpperCamelCase__				=    tokenizer.convert_tokens_to_ids(SCREAMING_SNAKE_CASE_						)
       self.assertListEqual(SCREAMING_SNAKE_CASE_	,      [31, 23, 3_86, 19, 5_61, 30_50, 15, 17, 48, 25, 82_56, 18, 1, 9]						)

       UpperCamelCase__				=    tokenizer.convert_ids_to_tokens(SCREAMING_SNAKE_CASE_						)
       self.assertListEqual(
           SCREAMING_SNAKE_CASE_	,      ["""▁i""", """▁was""", """▁born""", """▁in""", """▁9""", """2000""", """,""", """▁and""", """▁this""", """▁is""", """▁fal""", """s""", """<unk>""", """."""]	,      )







  def 	UpperCAmelCase_ (self						):
       UpperCamelCase__				=    AlbertTokenizer(SCREAMING_SNAKE_CASE_						)

       UpperCamelCase__				=    tokenizer.encode("""sequence builders"""						)
       UpperCamelCase__				=    tokenizer.encode("""multi-sequence build"""						)

       UpperCamelCase__				=    tokenizer.build_inputs_with_special_tokens(SCREAMING_SNAKE_CASE_						)
       UpperCamelCase__				=    tokenizer.build_inputs_with_special_tokens(SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_						)

       assert encoded_sentence == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id]
       assert encoded_pair == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] + text_a + [
           tokenizer.sep_token_id
       ]







  @slow
  def 	UpperCAmelCase_ (self						):
       # fmt: off
       UpperCamelCase__				=    {"""attention_mask""": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], """input_ids""": [[2, 2_19_70, 13, 5, 60_92, 1_67, 28, 71_03, 21_53, 6_73, 8, 70_28, 1_20_51, 18, 17, 71_03, 21_53, 6_73, 8, 35_15, 1_86_84, 8, 44_61, 6, 19_27, 2_97, 8, 1_20_60, 26_07, 18, 13, 5, 44_61, 15, 1_05_38, 38, 8, 1_35, 15, 8_22, 58, 15, 9_93, 1_03_63, 15, 14_60, 80_05, 44_61, 15, 9_93, 2_55, 23_28, 9, 9, 9, 6, 26, 11_12, 8_16, 32_60, 13, 5, 1_03, 23_77, 6, 17, 11_12, 8_16, 27_82, 13, 5, 1_03, 1_06_41, 6, 29, 84, 25_12, 24_30, 7_82, 1_86_84, 27_61, 19, 8_08, 24_30, 25_56, 17, 8_55, 14_80, 94_77, 40_91, 1_28, 1_17_12, 15, 71_03, 21_53, 6_73, 17, 2_48_83, 99_90, 9, 3], [2, 1_15_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0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]}  # noqa: E501
       # fmt: on

       self.tokenizer_integration_test_util(
           expected_encoding=SCREAMING_SNAKE_CASE_	,      model_name="""albert-base-v2"""	,      revision="""6b6560eaf5ff2e250b00c50f380c5389a9c2d82e"""	,      )







from collections import defaultdict
from typing import Optional

from ..image_utils import load_image
from ..utils import (
    add_end_docstrings,
    is_torch_available,
    logging,
    requires_backends,
)
from .base import PIPELINE_INIT_ARGS, ChunkPipeline


if is_torch_available():
    import torch

    from ..models.auto.modeling_auto import MODEL_FOR_MASK_GENERATION_MAPPING

lowerCamelCase_						     =					logging.get_logger(__name__)




@add_end_docstrings(__lowerCamelCase			)
class      __A(    __lowerCamelCase			):

  """simple docstring"""







  def __init__(self	,      **SCREAMING_SNAKE_CASE_						):
       super().__init__(**SCREAMING_SNAKE_CASE_						)
       requires_backends(self	,      """vision"""						)
       requires_backends(self	,      """torch"""						)

       if self.framework != "pt":
            raise ValueError(F"The {self.__class__} is only available in PyTorch."						)

       self.check_model_type(SCREAMING_SNAKE_CASE_						)







  def 	UpperCAmelCase_ (self	,      **SCREAMING_SNAKE_CASE_						):
       UpperCamelCase__				=    {}
       UpperCamelCase__				=    {}
       UpperCamelCase__				=    {}
       # preprocess args
       if "points_per_batch" in kwargs:
            UpperCamelCase__				=    kwargs["""points_per_batch"""]
       if "points_per_crop" in kwargs:
            UpperCamelCase__				=    kwargs["""points_per_crop"""]
       if "crops_n_layers" in kwargs:
            UpperCamelCase__				=    kwargs["""crops_n_layers"""]
       if "crop_overlap_ratio" in kwargs:
            UpperCamelCase__				=    kwargs["""crop_overlap_ratio"""]
       if "crop_n_points_downscale_factor" in kwargs:
            UpperCamelCase__				=    kwargs["""crop_n_points_downscale_factor"""]
       # postprocess args
       if "pred_iou_thresh" in kwargs:
            UpperCamelCase__				=    kwargs["""pred_iou_thresh"""]
       if "stability_score_offset" in kwargs:
            UpperCamelCase__				=    kwargs["""stability_score_offset"""]
       if "mask_threshold" in kwargs:
            UpperCamelCase__				=    kwargs["""mask_threshold"""]
       if "stability_score_thresh" in kwargs:
            UpperCamelCase__				=    kwargs["""stability_score_thresh"""]
       if "crops_nms_thresh" in kwargs:
            UpperCamelCase__				=    kwargs["""crops_nms_thresh"""]
       if "output_rle_mask" in kwargs:
            UpperCamelCase__				=    kwargs["""output_rle_mask"""]
       if "output_bboxes_mask" in kwargs:
            UpperCamelCase__				=    kwargs["""output_bboxes_mask"""]
       return preprocess_kwargs, forward_params, postprocess_kwargs







  def __call__(self	,      SCREAMING_SNAKE_CASE_	,      *SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_=None	,      SCREAMING_SNAKE_CASE_=None	,      **SCREAMING_SNAKE_CASE_						):
       return super().__call__(SCREAMING_SNAKE_CASE_	,      *SCREAMING_SNAKE_CASE_	,      num_workers=SCREAMING_SNAKE_CASE_	,      batch_size=SCREAMING_SNAKE_CASE_	,      **SCREAMING_SNAKE_CASE_						)







  def 	UpperCAmelCase_ (self	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_=64	,      SCREAMING_SNAKE_CASE_ = 0	,      SCREAMING_SNAKE_CASE_ = 5_12 / 15_00	,      SCREAMING_SNAKE_CASE_ = 32	,      SCREAMING_SNAKE_CASE_ = 1	,      ):
       UpperCamelCase__				=    load_image(SCREAMING_SNAKE_CASE_						)
       UpperCamelCase__				=    self.image_processor.size["""longest_edge"""]
       UpperCamelCase__		,	UpperCamelCase__		,	UpperCamelCase__		,	UpperCamelCase__				=    self.image_processor.generate_crop_boxes(
           SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_						)
       UpperCamelCase__				=    self.image_processor(images=SCREAMING_SNAKE_CASE_	,      return_tensors="""pt"""						)

       with self.device_placement():
            if self.framework == "pt":
                 UpperCamelCase__				=    self.get_inference_context()
                 with inference_context():
                      UpperCamelCase__				=    self._ensure_tensor_on_device(SCREAMING_SNAKE_CASE_	,      device=self.device						)
                      UpperCamelCase__				=    self.model.get_image_embeddings(model_inputs.pop("""pixel_values"""						)						)
                      UpperCamelCase__				=    image_embeddings

       UpperCamelCase__				=    grid_points.shape[1]
       UpperCamelCase__				=    points_per_batch if points_per_batch is not None else n_points

       if points_per_batch <= 0:
            raise ValueError(
                """Cannot have points_per_batch<=0. Must be >=1 to returned batched outputs. """
                """To return all points at once, set points_per_batch to None"""						)

       for i in range(0	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_						):
            UpperCamelCase__				=    grid_points[:, i : i + points_per_batch, :, :]
            UpperCamelCase__				=    input_labels[:, i : i + points_per_batch]
            UpperCamelCase__				=    i == n_points - points_per_batch
            yield {
                "input_points": batched_points,
                "input_labels": labels,
                "input_boxes": crop_boxes,
                "is_last": is_last,
                **model_inputs,
            }







  def 	UpperCAmelCase_ (self	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_=0.88	,      SCREAMING_SNAKE_CASE_=0.95	,      SCREAMING_SNAKE_CASE_=0	,      SCREAMING_SNAKE_CASE_=1	,      ):
       UpperCamelCase__				=    model_inputs.pop("""input_boxes"""						)
       UpperCamelCase__				=    model_inputs.pop("""is_last"""						)
       UpperCamelCase__				=    model_inputs.pop("""original_sizes"""						).tolist()
       UpperCamelCase__				=    model_inputs.pop("""reshaped_input_sizes"""						).tolist()

       UpperCamelCase__				=    self.model(**SCREAMING_SNAKE_CASE_						)

       # post processing happens here in order to avoid CPU GPU copies of ALL the masks
       UpperCamelCase__				=    model_outputs["""pred_masks"""]
       UpperCamelCase__				=    self.image_processor.post_process_masks(
           SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      binarize=SCREAMING_SNAKE_CASE_						)
       UpperCamelCase__				=    model_outputs["""iou_scores"""]
       UpperCamelCase__		,	UpperCamelCase__		,	UpperCamelCase__				=    self.image_processor.filter_masks(
           masks[0]	,      iou_scores[0]	,      original_sizes[0]	,      input_boxes[0]	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      )
       return {
           "masks": masks,
           "is_last": is_last,
           "boxes": boxes,
           "iou_scores": iou_scores,
       }







  def 	UpperCAmelCase_ (self	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_=False	,      SCREAMING_SNAKE_CASE_=False	,      SCREAMING_SNAKE_CASE_=0.7	,      ):
       UpperCamelCase__				=    []
       UpperCamelCase__				=    []
       UpperCamelCase__				=    []
       for model_output in model_outputs:
            all_scores.append(model_output.pop("""iou_scores"""						)						)
            all_masks.extend(model_output.pop("""masks"""						)						)
            all_boxes.append(model_output.pop("""boxes"""						)						)

       UpperCamelCase__				=    torch.cat(SCREAMING_SNAKE_CASE_						)
       UpperCamelCase__				=    torch.cat(SCREAMING_SNAKE_CASE_						)
       UpperCamelCase__		,	UpperCamelCase__		,	UpperCamelCase__		,	UpperCamelCase__				=    self.image_processor.post_process_for_mask_generation(
           SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_						)

       UpperCamelCase__				=    defaultdict(SCREAMING_SNAKE_CASE_						)
       for output in model_outputs:
            for k, v in output.items():
                 extra[k].append(SCREAMING_SNAKE_CASE_						)

       UpperCamelCase__				=    {}
       if output_rle_mask:
            UpperCamelCase__				=    rle_mask

       if output_bboxes_mask:
            UpperCamelCase__				=    bounding_boxes

       return {"masks": output_masks, "scores": iou_scores, **optional, **extra}





