






"""simple docstring"""







import json
import os
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import BertTokenizer, BertTokenizerFast
from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES
from transformers.testing_utils import require_vision
from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available


if is_vision_available():
			from PIL import Image

			from transformers import AlignProcessor, EfficientNetImageProcessor


@require_vision
class    __UpperCAmelCase(       unittest.TestCase							):




						"""simple docstring"""




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				Any=	tempfile.mkdtemp()

												lowercase__    :				List[Any]=	[
												    '''[UNK]''',
												    '''[CLS]''',
												    '''[SEP]''',
												    '''[PAD]''',
												    '''[MASK]''',
												    '''want''',
												    '''##want''',
												    '''##ed''',
												    '''wa''',
												    '''un''',
												    '''runn''',
												    '''##ing''',
												    ''',''',
												    '''low''',
												    '''lowest''',
												]
												lowercase__    :				str=	os.path.join(self.tmpdirname	,  VOCAB_FILES_NAMES["vocab_file"]      )
												with open(self.vocab_file	,  "w"	,  encoding="utf-8"      ) as vocab_writer:
																		vocab_writer.write("".join([x + "\n" for x in vocab_tokens]      )      )

												lowercase__    :				Tuple=	{
												    '''do_resize''': True,
												    '''size''': 20,
												    '''do_center_crop''': True,
												    '''crop_size''': 18,
												    '''do_normalize''': True,
												    '''image_mean''': [0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73],
												    '''image_std''': [0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11],
												}
												lowercase__    :				List[Any]=	os.path.join(self.tmpdirname	,  _lowerCamelCase      )
												with open(self.image_processor_file	,  "w"	,  encoding="utf-8"      ) as fp:
																		json.dump(_lowerCamelCase	,  _lowerCamelCase      )




						def 		UpperCAmelCase_	(       self	,  **snake_case__      ):
												'''simple docstring'''
												return BertTokenizer.from_pretrained(self.tmpdirname	,  **_lowerCamelCase      )




						def 		UpperCAmelCase_	(       self	,  **snake_case__      ):
												'''simple docstring'''
												return BertTokenizerFast.from_pretrained(self.tmpdirname	,  **_lowerCamelCase      )




						def 		UpperCAmelCase_	(       self	,  **snake_case__      ):
												'''simple docstring'''
												return EfficientNetImageProcessor.from_pretrained(self.tmpdirname	,  **_lowerCamelCase      )




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												shutil.rmtree(self.tmpdirname      )




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				Union[str, Any]=	[np.random.randint(255	,  size=(3, 30, 400)	,  dtype=np.uinta      )]
												lowercase__    :				Any=	[Image.fromarray(np.moveaxis(_lowerCamelCase	,  0	,  -1      )      ) for x in image_inputs]
												return image_inputs




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				Tuple=	self.get_tokenizer()
												lowercase__    :				Tuple=	self.get_rust_tokenizer()
												lowercase__    :				Dict=	self.get_image_processor()

												lowercase__    :				List[Any]=	AlignProcessor(tokenizer=_lowerCamelCase	,  image_processor=_lowerCamelCase      )
												processor_slow.save_pretrained(self.tmpdirname      )
												lowercase__    :				str=	AlignProcessor.from_pretrained(self.tmpdirname	,  use_fast=_lowerCamelCase      )

												lowercase__    :				Any=	AlignProcessor(tokenizer=_lowerCamelCase	,  image_processor=_lowerCamelCase      )
												processor_fast.save_pretrained(self.tmpdirname      )
												lowercase__    :				List[Any]=	AlignProcessor.from_pretrained(self.tmpdirname      )

												self.assertEqual(processor_slow.tokenizer.get_vocab()	,  tokenizer_slow.get_vocab()      )
												self.assertEqual(processor_fast.tokenizer.get_vocab()	,  tokenizer_fast.get_vocab()      )
												self.assertEqual(tokenizer_slow.get_vocab()	,  tokenizer_fast.get_vocab()      )
												self.assertIsInstance(processor_slow.tokenizer	,  _lowerCamelCase      )
												self.assertIsInstance(processor_fast.tokenizer	,  _lowerCamelCase      )

												self.assertEqual(processor_slow.image_processor.to_json_string()	,  image_processor.to_json_string()      )
												self.assertEqual(processor_fast.image_processor.to_json_string()	,  image_processor.to_json_string()      )
												self.assertIsInstance(processor_slow.image_processor	,  _lowerCamelCase      )
												self.assertIsInstance(processor_fast.image_processor	,  _lowerCamelCase      )




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				List[str]=	AlignProcessor(tokenizer=self.get_tokenizer()	,  image_processor=self.get_image_processor()      )
												processor.save_pretrained(self.tmpdirname      )

												lowercase__    :				Optional[int]=	self.get_tokenizer(bos_token="(BOS)"	,  eos_token="(EOS)"      )
												lowercase__    :				Tuple=	self.get_image_processor(do_normalize=_lowerCamelCase	,  padding_value=1.0      )

												lowercase__    :				List[str]=	AlignProcessor.from_pretrained(
												    self.tmpdirname	,  bos_token="(BOS)"	,  eos_token="(EOS)"	,  do_normalize=_lowerCamelCase	,  padding_value=1.0      )

												self.assertEqual(processor.tokenizer.get_vocab()	,  tokenizer_add_kwargs.get_vocab()      )
												self.assertIsInstance(processor.tokenizer	,  _lowerCamelCase      )

												self.assertEqual(processor.image_processor.to_json_string()	,  image_processor_add_kwargs.to_json_string()      )
												self.assertIsInstance(processor.image_processor	,  _lowerCamelCase      )




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				Dict=	self.get_image_processor()
												lowercase__    :				Any=	self.get_tokenizer()

												lowercase__    :				List[str]=	AlignProcessor(tokenizer=_lowerCamelCase	,  image_processor=_lowerCamelCase      )

												lowercase__    :				Any=	self.prepare_image_inputs()

												lowercase__    :				List[Any]=	image_processor(_lowerCamelCase	,  return_tensors="np"      )
												lowercase__    :				str=	processor(images=_lowerCamelCase	,  return_tensors="np"      )

												for key in input_image_proc.keys():
																		self.assertAlmostEqual(input_image_proc[key].sum()	,  input_processor[key].sum()	,  delta=1e-2      )




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				str=	self.get_image_processor()
												lowercase__    :				List[str]=	self.get_tokenizer()

												lowercase__    :				Optional[int]=	AlignProcessor(tokenizer=_lowerCamelCase	,  image_processor=_lowerCamelCase      )

												lowercase__    :				int=	'''lower newer'''

												lowercase__    :				str=	processor(text=_lowerCamelCase      )

												lowercase__    :				Dict=	tokenizer(_lowerCamelCase	,  padding="max_length"	,  max_length=64      )

												for key in encoded_tok.keys():
																		self.assertListEqual(encoded_tok[key]	,  encoded_processor[key]      )




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				Optional[int]=	self.get_image_processor()
												lowercase__    :				Optional[Any]=	self.get_tokenizer()

												lowercase__    :				List[str]=	AlignProcessor(tokenizer=_lowerCamelCase	,  image_processor=_lowerCamelCase      )

												lowercase__    :				List[Any]=	'''lower newer'''
												lowercase__    :				Optional[int]=	self.prepare_image_inputs()

												lowercase__    :				List[Any]=	processor(text=_lowerCamelCase	,  images=_lowerCamelCase      )

												self.assertListEqual(list(inputs.keys()      )	,  ["input_ids", "token_type_ids", "attention_mask", "pixel_values"]      )

												# test if it raises when no input is passed
												with pytest.raises(_lowerCamelCase      ):
																		processor()




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				Optional[Any]=	self.get_image_processor()
												lowercase__    :				Optional[int]=	self.get_tokenizer()

												lowercase__    :				List[Any]=	AlignProcessor(tokenizer=_lowerCamelCase	,  image_processor=_lowerCamelCase      )

												lowercase__    :				str=	[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

												lowercase__    :				str=	processor.batch_decode(_lowerCamelCase      )
												lowercase__    :				Union[str, Any]=	tokenizer.batch_decode(_lowerCamelCase      )

												self.assertListEqual(_lowerCamelCase	,  _lowerCamelCase      )




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				str=	self.get_image_processor()
												lowercase__    :				Tuple=	self.get_tokenizer()

												lowercase__    :				Any=	AlignProcessor(tokenizer=_lowerCamelCase	,  image_processor=_lowerCamelCase      )

												lowercase__    :				str=	'''lower newer'''
												lowercase__    :				List[str]=	self.prepare_image_inputs()

												lowercase__    :				Tuple=	processor(text=_lowerCamelCase	,  images=_lowerCamelCase      )

												self.assertListEqual(list(inputs.keys()      )	,  processor.model_input_names      )














"""simple docstring"""







import os
from pathlib import Path





def 						lowercase__()    ->List[Any]:




						"""simple docstring"""



						from torch.utils.cpp_extension import load

						lowercase__    :				Any=	Path(A						).resolve().parent.parent.parent / "kernels" / "deformable_detr"
						lowercase__    :				Any=	[
						    root / filename
						    for filename in [
						        "vision.cpp",
						        os.path.join("cpu" ,		"ms_deform_attn_cpu.cpp"						),
						        os.path.join("cuda" ,		"ms_deform_attn_cuda.cu"						),
						    ]
						]

						load(
						    "MultiScaleDeformableAttention" ,		A ,		with_cuda=A ,		extra_include_paths=[str(A						)] ,		extra_cflags=["-DWITH_CUDA=1"] ,		extra_cuda_cflags=[
						        "-DCUDA_HAS_FP16=1",
						        "-D__CUDA_NO_HALF_OPERATORS__",
						        "-D__CUDA_NO_HALF_CONVERSIONS__",
						        "-D__CUDA_NO_HALF2_OPERATORS__",
						    ] ,		)

						import MultiScaleDeformableAttention as MSDA

						return MSDA






