




import os
import unittest

from transformers import BatchEncoding
from transformers.models.bert.tokenization_bert import (
    BasicTokenizer,
    WordpieceTokenizer,
    _is_control,
    _is_punctuation,
    _is_whitespace,
)
from transformers.models.prophetnet.tokenization_prophetnet import VOCAB_FILES_NAMES, ProphetNetTokenizer
from transformers.testing_utils import require_torch, slow

from ...test_tokenization_common import TokenizerTesterMixin


class    SCREAMING_SNAKE_CASE_				(       _UpperCamelCase	,  unittest.TestCase   ):



	"""simple docstring"""


	__magic_name__   :       str      	=		ProphetNetTokenizer
	__magic_name__   :       Dict      	=		False






	def 					lowerCamelCase__     (					self		:						Union[str, Any]	)	->       List[str]:







						"""simple docstring"""







						super().setUp()

						__UpperCamelCase					:	Optional[Any]             =      [
						    """[UNK]""",
						    """[CLS]""",
						    """[SEP]""",
						    """[PAD]""",
						    """[MASK]""",
						    """want""",
						    """##want""",
						    """##ed""",
						    """wa""",
						    """un""",
						    """runn""",
						    """##ing""",
						    """,""",
						    """low""",
						    """lowest""",
						]
						__UpperCamelCase					:	List[Any]             =      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]	)	)






	def 					lowerCamelCase__     (					self		:						Tuple					,     lowerCAmelCase		:						int	)	->       List[str]:







						"""simple docstring"""







						__UpperCamelCase					:	Union[str, Any]             =      """UNwant\u00E9d,running"""
						__UpperCamelCase					:	List[str]             =      """unwanted, running"""
						return input_text, output_text






	def 					lowerCamelCase__     (					self		:						List[Any]	)	->       int:







						"""simple docstring"""







						__UpperCamelCase					:	Optional[int]             =      self.tokenizer_class(self.vocab_file	)

						__UpperCamelCase					:	List[str]             =      tokenizer.tokenize("""UNwant\u00E9d,running"""	)
						self.assertListEqual(lowerCAmelCase					,     ["""un""", """##want""", """##ed""", """,""", """runn""", """##ing"""]	)
						self.assertListEqual(tokenizer.convert_tokens_to_ids(lowerCAmelCase	)					,     [9, 6, 7, 12, 10, 11]	)






	def 					lowerCamelCase__     (					self		:						str	)	->       List[Any]:







						"""simple docstring"""







						__UpperCamelCase					:	List[str]             =      BasicTokenizer()

						self.assertListEqual(tokenizer.tokenize("""ah\u535A\u63A8zz"""	)					,     ["""ah""", """\u535A""", """\u63A8""", """zz"""]	)






	def 					lowerCamelCase__     (					self		:						int	)	->       Tuple:







						"""simple docstring"""







						__UpperCamelCase					:	Any             =      BasicTokenizer(do_lower_case=lowerCAmelCase	)

						self.assertListEqual(
						    tokenizer.tokenize(""" \tHeLLo!how  \n Are yoU?  """	)					,     ["""hello""", """!""", """how""", """are""", """you""", """?"""]	)
						self.assertListEqual(tokenizer.tokenize("""H\u00E9llo"""	)					,     ["""hello"""]	)






	def 					lowerCamelCase__     (					self		:						Dict	)	->       List[str]:







						"""simple docstring"""







						__UpperCamelCase					:	List[Any]             =      BasicTokenizer(do_lower_case=lowerCAmelCase					,     strip_accents=lowerCAmelCase	)

						self.assertListEqual(
						    tokenizer.tokenize(""" \tHäLLo!how  \n Are yoU?  """	)					,     ["""hällo""", """!""", """how""", """are""", """you""", """?"""]	)
						self.assertListEqual(tokenizer.tokenize("""H\u00E9llo"""	)					,     ["""h\u00E9llo"""]	)






	def 					lowerCamelCase__     (					self		:						Any	)	->       List[Any]:







						"""simple docstring"""







						__UpperCamelCase					:	Tuple             =      BasicTokenizer(do_lower_case=lowerCAmelCase					,     strip_accents=lowerCAmelCase	)

						self.assertListEqual(
						    tokenizer.tokenize(""" \tHäLLo!how  \n Are yoU?  """	)					,     ["""hallo""", """!""", """how""", """are""", """you""", """?"""]	)
						self.assertListEqual(tokenizer.tokenize("""H\u00E9llo"""	)					,     ["""hello"""]	)






	def 					lowerCamelCase__     (					self		:						List[str]	)	->       Optional[int]:







						"""simple docstring"""







						__UpperCamelCase					:	Dict             =      BasicTokenizer(do_lower_case=lowerCAmelCase	)

						self.assertListEqual(
						    tokenizer.tokenize(""" \tHäLLo!how  \n Are yoU?  """	)					,     ["""hallo""", """!""", """how""", """are""", """you""", """?"""]	)
						self.assertListEqual(tokenizer.tokenize("""H\u00E9llo"""	)					,     ["""hello"""]	)






	def 					lowerCamelCase__     (					self		:						List[Any]	)	->       Tuple:







						"""simple docstring"""







						__UpperCamelCase					:	List[str]             =      BasicTokenizer(do_lower_case=lowerCAmelCase	)

						self.assertListEqual(
						    tokenizer.tokenize(""" \tHeLLo!how  \n Are yoU?  """	)					,     ["""HeLLo""", """!""", """how""", """Are""", """yoU""", """?"""]	)






	def 					lowerCamelCase__     (					self		:						List[Any]	)	->       Tuple:







						"""simple docstring"""







						__UpperCamelCase					:	Tuple             =      BasicTokenizer(do_lower_case=lowerCAmelCase					,     strip_accents=lowerCAmelCase	)

						self.assertListEqual(
						    tokenizer.tokenize(""" \tHäLLo!how  \n Are yoU?  """	)					,     ["""HäLLo""", """!""", """how""", """Are""", """yoU""", """?"""]	)






	def 					lowerCamelCase__     (					self		:						Optional[int]	)	->       List[Any]:







						"""simple docstring"""







						__UpperCamelCase					:	List[Any]             =      BasicTokenizer(do_lower_case=lowerCAmelCase					,     strip_accents=lowerCAmelCase	)

						self.assertListEqual(
						    tokenizer.tokenize(""" \tHäLLo!how  \n Are yoU?  """	)					,     ["""HaLLo""", """!""", """how""", """Are""", """yoU""", """?"""]	)






	def 					lowerCamelCase__     (					self		:						Optional[Any]	)	->       str:







						"""simple docstring"""







						__UpperCamelCase					:	int             =      BasicTokenizer(do_lower_case=lowerCAmelCase					,     never_split=["""[UNK]"""]	)

						self.assertListEqual(
						    tokenizer.tokenize(""" \tHeLLo!how  \n Are yoU? [UNK]"""	)					,     ["""HeLLo""", """!""", """how""", """Are""", """yoU""", """?""", """[UNK]"""]	)






	def 					lowerCamelCase__     (					self		:						Union[str, Any]	)	->       Optional[Any]:







						"""simple docstring"""







						__UpperCamelCase					:	int             =      ["""[UNK]""", """[CLS]""", """[SEP]""", """want""", """##want""", """##ed""", """wa""", """un""", """runn""", """##ing"""]

						__UpperCamelCase					:	List[Any]             =      {}
						for i, token in enumerate(lowerCAmelCase	):
											__UpperCamelCase					:	Dict             =      i
						__UpperCamelCase					:	Any             =      WordpieceTokenizer(vocab=lowerCAmelCase					,     unk_token="""[UNK]"""	)

						self.assertListEqual(tokenizer.tokenize(""""""	)					,     []	)

						self.assertListEqual(tokenizer.tokenize("""unwanted running"""	)					,     ["""un""", """##want""", """##ed""", """runn""", """##ing"""]	)

						self.assertListEqual(tokenizer.tokenize("""unwantedX running"""	)					,     ["""[UNK]""", """runn""", """##ing"""]	)






	@require_torch
	def 					lowerCamelCase__     (					self		:						Optional[Any]	)	->       Dict:







						"""simple docstring"""







						__UpperCamelCase					:	str             =      self.tokenizer_class.from_pretrained("""microsoft/prophetnet-large-uncased"""	)

						__UpperCamelCase					:	int             =      ["""A long paragraph for summarization.""", """Another paragraph for summarization."""]
						__UpperCamelCase					:	Optional[int]             =      [1037, 2146, 20423, 2005, 7680, 7849, 3989, 1012, 102]
						__UpperCamelCase					:	Optional[int]             =      tokenizer(lowerCAmelCase					,     padding=lowerCAmelCase					,     return_tensors="""pt"""	)
						self.assertIsInstance(lowerCAmelCase					,     lowerCAmelCase	)
						__UpperCamelCase					:	int             =      list(batch.input_ids.numpy()[0]	)
						self.assertListEqual(lowerCAmelCase					,     lowerCAmelCase	)

						self.assertEqual((2, 9)					,     batch.input_ids.shape	)
						self.assertEqual((2, 9)					,     batch.attention_mask.shape	)






	def 					lowerCamelCase__     (					self		:						int	)	->       List[Any]:







						"""simple docstring"""







						self.assertTrue(_is_whitespace(""" """	)	)
						self.assertTrue(_is_whitespace("""\t"""	)	)
						self.assertTrue(_is_whitespace("""\r"""	)	)
						self.assertTrue(_is_whitespace("""\n"""	)	)
						self.assertTrue(_is_whitespace("""\u00A0"""	)	)

						self.assertFalse(_is_whitespace("""A"""	)	)
						self.assertFalse(_is_whitespace("""-"""	)	)






	def 					lowerCamelCase__     (					self		:						Dict	)	->       Optional[int]:







						"""simple docstring"""







						self.assertTrue(_is_control("""\u0005"""	)	)

						self.assertFalse(_is_control("""A"""	)	)
						self.assertFalse(_is_control(""" """	)	)
						self.assertFalse(_is_control("""\t"""	)	)
						self.assertFalse(_is_control("""\r"""	)	)






	def 					lowerCamelCase__     (					self		:						Tuple	)	->       Optional[Any]:







						"""simple docstring"""







						self.assertTrue(_is_punctuation("""-"""	)	)
						self.assertTrue(_is_punctuation("""$"""	)	)
						self.assertTrue(_is_punctuation("""`"""	)	)
						self.assertTrue(_is_punctuation("""."""	)	)

						self.assertFalse(_is_punctuation("""A"""	)	)
						self.assertFalse(_is_punctuation(""" """	)	)






	@slow
	def 					lowerCamelCase__     (					self		:						str	)	->       Tuple:







						"""simple docstring"""







						__UpperCamelCase					:	List[str]             =      self.tokenizer_class.from_pretrained("""microsoft/prophetnet-large-uncased"""	)

						__UpperCamelCase					:	Optional[int]             =      tokenizer.encode("""sequence builders"""					,     add_special_tokens=lowerCAmelCase	)
						__UpperCamelCase					:	List[Any]             =      tokenizer.encode("""multi-sequence build"""					,     add_special_tokens=lowerCAmelCase	)

						__UpperCamelCase					:	Optional[Any]             =      tokenizer.build_inputs_with_special_tokens(lowerCAmelCase	)
						__UpperCamelCase					:	Union[str, Any]             =      tokenizer.build_inputs_with_special_tokens(lowerCAmelCase					,     lowerCAmelCase	)

						assert encoded_sentence == text + [102]
						assert encoded_pair == text + [102] + text_a + [102]












# Copyright 2023 The HuggingFace 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 torch

from accelerate import PartialState
from accelerate.utils.operations import broadcast, gather, gather_object, pad_across_processes, reduce





def 				A__ (snake_case   :		List[Any]				)     ->						Optional[Any]:
					return (torch.arange(state.num_processes				) + 1.0 + (state.num_processes * state.process_index)).to(state.device				)





def 				A__ (snake_case   :		int				)     ->						Any:
					__UpperCamelCase					:	List[str]             =      create_tensor(snake_case				)
					__UpperCamelCase					:	List[Any]             =      gather(snake_case				)
					assert gathered_tensor.tolist() == list(range(1		,	state.num_processes**2 + 1				)				)





def 				A__ (snake_case   :		Optional[Any]				)     ->						Dict:
					__UpperCamelCase					:	Any             =      [state.process_index]
					__UpperCamelCase					:	int             =      gather_object(snake_case				)
					assert len(snake_case				) == state.num_processes, F'''{gathered_obj}, {len(snake_case				)} != {state.num_processes}'''
					assert gathered_obj == list(range(state.num_processes				)				), F'''{gathered_obj} != {list(range(state.num_processes				)				)}'''





def 				A__ (snake_case   :		Any				)     ->						Optional[Any]:
					__UpperCamelCase					:	Optional[Any]             =      create_tensor(snake_case				)
					__UpperCamelCase					:	Any             =      broadcast(snake_case				)
					assert broadcasted_tensor.shape == torch.Size([state.num_processes]				)
					assert broadcasted_tensor.tolist() == list(range(1		,	state.num_processes + 1				)				)





def 				A__ (snake_case   :		Tuple				)     ->						Dict:
					# We need to pad the tensor with one more element if we are the main process
					# to ensure that we can pad
					if state.is_main_process:
										__UpperCamelCase					:	List[Any]             =      torch.arange(state.num_processes + 1				).to(state.device				)
					else:
										__UpperCamelCase					:	List[str]             =      torch.arange(state.num_processes				).to(state.device				)
					__UpperCamelCase					:	List[str]             =      pad_across_processes(snake_case				)
					assert padded_tensor.shape == torch.Size([state.num_processes + 1]				)
					if not state.is_main_process:
										assert padded_tensor.tolist() == list(range(0		,	state.num_processes				)				) + [0]





def 				A__ (snake_case   :		Tuple				)     ->						Dict:
					# For now runs on only two processes
					if state.num_processes != 2:
										return
					__UpperCamelCase					:	Optional[int]             =      create_tensor(snake_case				)
					__UpperCamelCase					:	str             =      reduce(snake_case		,	"""sum"""				)
					__UpperCamelCase					:	Optional[int]             =      torch.tensor([4.0, 6]				).to(state.device				)
					assert torch.allclose(snake_case		,	snake_case				), F'''{reduced_tensor} != {truth_tensor}'''





def 				A__ (snake_case   :		Optional[Any]				)     ->						Optional[Any]:
					# For now runs on only two processes
					if state.num_processes != 2:
										return
					__UpperCamelCase					:	List[Any]             =      create_tensor(snake_case				)
					__UpperCamelCase					:	str             =      reduce(snake_case		,	"""mean"""				)
					__UpperCamelCase					:	Union[str, Any]             =      torch.tensor([2.0, 3]				).to(state.device				)
					assert torch.allclose(snake_case		,	snake_case				), F'''{reduced_tensor} != {truth_tensor}'''





def 				A__ (snake_case   :		Tuple				)     ->						int:
					# For xla_spawn (TPUs)
					main()





def 				A__ ()     ->						List[Any]:
					__UpperCamelCase					:	List[Any]             =      PartialState()
					state.print(F'''State: {state}'''				)
					state.print("""testing gather"""				)
					test_gather(snake_case				)
					state.print("""testing gather_object"""				)
					test_gather_object(snake_case				)
					state.print("""testing broadcast"""				)
					test_broadcast(snake_case				)
					state.print("""testing pad_across_processes"""				)
					test_pad_across_processes(snake_case				)
					state.print("""testing reduce_sum"""				)
					test_reduce_sum(snake_case				)
					state.print("""testing reduce_mean"""				)
					test_reduce_mean(snake_case				)


if __name__ == "__main__":
	main()






