




import random
import unittest

import numpy as np

import transformers
from transformers import is_flax_available, is_torch_available
from transformers.testing_utils import is_pt_flax_cross_test, require_flax


if is_flax_available():
					import os

					import jax.numpy as jnp
					from jax import jit

					from transformers import AutoTokenizer, FlaxAutoModelForCausalLM
					from transformers.modeling_flax_pytorch_utils import load_flax_weights_in_pytorch_model

					__lowerCamelCase   :						Tuple				=			'''0.12'''  # assumed parallelism: 8


if is_torch_available():
					import torch







def 		lowercase__   (		__A:	Any							,__A:	Dict							,__A:	str=None				):





					'''simple docstring'''






					if rng is None:
										__magic_name__							:	Optional[Any]    							=							random.Random()

					__magic_name__							:	Tuple    							=							1
					for dim in shape:
										total_dims *= dim

					__magic_name__							:	List[Any]    							=							[]
					for _ in range(__A				):
										values.append(rng.randint(0							,vocab_size - 1				)				)

					__magic_name__							:	Dict    							=							np.array(__A							,dtype=jnp.intaa				).reshape(__A				)

					return output







def 		lowercase__   (		__A:	List[Any]							,__A:	Any=None				):





					'''simple docstring'''






					__magic_name__							:	Optional[int]    							=							ids_tensor(__A							,vocab_size=2							,rng=__A				)
					# make sure that at least one token is attended to for each batch
					__magic_name__							:	List[str]    							=							1
					return attn_mask


@require_flax
class    lowerCamelCase    :






			'''simple docstring'''





			UpperCamelCase__								=None
			UpperCamelCase__								=()

			def 							UpperCAmelCase__       ( self			:   Dict  )						->   Optional[int]:
								__magic_name__							:	Any    							=							self.model_tester.prepare_config_and_inputs_for_common()

								# cut to half length & take max batch_size 3
								__magic_name__							:	Union[str, Any]    							=							2
								__magic_name__							:	int    							=							inputs['''input_ids'''].shape[-1] // 2
								__magic_name__							:	Union[str, Any]    							=							inputs['''input_ids'''][:max_batch_size, :sequence_length]

								__magic_name__							:	Optional[Any]    							=							jnp.ones_like(lowerCamelCase_  )
								__magic_name__							:	List[str]    							=							attention_mask[:max_batch_size, :sequence_length]

								# generate max 5 tokens
								__magic_name__							:	Any    							=							input_ids.shape[-1] + 5
								if config.eos_token_id is not None and config.pad_token_id is None:
													# hack to allow generate for models such as GPT2 as is done in `generate()`
													__magic_name__							:	List[Any]    							=							config.eos_token_id
								return config, input_ids, attention_mask, max_length

			@is_pt_flax_cross_test
			def 							UpperCAmelCase__       ( self			:   Optional[Any]  )						->   Any:
								__magic_name__							:	Optional[int]    							=							self._get_input_ids_and_config()
								__magic_name__							:	Dict    							=							False
								__magic_name__							:	str    							=							max_length
								__magic_name__							:	Tuple    							=							0

								for model_class in self.all_generative_model_classes:
													__magic_name__							:	Tuple    							=							model_class(lowerCamelCase_  )

													__magic_name__							:	Tuple    							=							model_class.__name__[4:]  # Skip the "Flax" at the beginning
													__magic_name__							:	Union[str, Any]    							=							getattr(lowerCamelCase_       ,						lowerCamelCase_  )
													__magic_name__							:	Any    							=							pt_model_class(lowerCamelCase_  ).eval()
													__magic_name__							:	str    							=							load_flax_weights_in_pytorch_model(lowerCamelCase_       ,						flax_model.params  )

													__magic_name__							:	Any    							=							flax_model.generate(lowerCamelCase_  ).sequences
													__magic_name__							:	Union[str, Any]    							=							pt_model.generate(torch.tensor(lowerCamelCase_       ,						dtype=torch.long  )  )

													if flax_generation_outputs.shape[-1] > pt_generation_outputs.shape[-1]:
																		__magic_name__							:	Union[str, Any]    							=							flax_generation_outputs[:, : pt_generation_outputs.shape[-1]]

													self.assertListEqual(pt_generation_outputs.numpy().tolist()       ,						flax_generation_outputs.tolist()  )

			def 							UpperCAmelCase__       ( self			:   List[str]  )						->   List[str]:
								__magic_name__							:	Dict    							=							self._get_input_ids_and_config()
								__magic_name__							:	Union[str, Any]    							=							False
								__magic_name__							:	List[Any]    							=							max_length

								for model_class in self.all_generative_model_classes:
													__magic_name__							:	str    							=							model_class(lowerCamelCase_  )

													__magic_name__							:	Any    							=							model.generate(lowerCamelCase_  ).sequences
													self.assertEqual(generation_outputs.shape[-1]       ,						lowerCamelCase_  )

													__magic_name__							:	Optional[int]    							=							jit(model.generate  )
													__magic_name__							:	Any    							=							jit_generate(lowerCamelCase_  ).sequences

													self.assertListEqual(generation_outputs.tolist()       ,						jit_generation_outputs.tolist()  )

			def 							UpperCAmelCase__       ( self			:   Union[str, Any]  )						->   Optional[int]:
								__magic_name__							:	Optional[Any]    							=							self._get_input_ids_and_config()
								__magic_name__							:	Optional[int]    							=							True
								__magic_name__							:	List[Any]    							=							max_length

								for model_class in self.all_generative_model_classes:
													__magic_name__							:	Tuple    							=							model_class(lowerCamelCase_  )

													__magic_name__							:	Any    							=							model.generate(lowerCamelCase_  ).sequences
													self.assertEqual(generation_outputs.shape[-1]       ,						lowerCamelCase_  )

													__magic_name__							:	List[Any]    							=							jit(model.generate  )
													__magic_name__							:	List[str]    							=							jit_generate(lowerCamelCase_  ).sequences

													self.assertListEqual(generation_outputs.tolist()       ,						jit_generation_outputs.tolist()  )

			def 							UpperCAmelCase__       ( self			:   List[Any]  )						->   List[str]:
								__magic_name__							:	Union[str, Any]    							=							self._get_input_ids_and_config()
								__magic_name__							:	int    							=							False
								__magic_name__							:	Any    							=							max_length
								__magic_name__							:	Optional[int]    							=							2

								for model_class in self.all_generative_model_classes:
													__magic_name__							:	Union[str, Any]    							=							model_class(lowerCamelCase_  )

													__magic_name__							:	Any    							=							model.generate(lowerCamelCase_  ).sequences
													self.assertEqual(generation_outputs.shape[-1]       ,						lowerCamelCase_  )

													__magic_name__							:	Optional[Any]    							=							jit(model.generate  )
													__magic_name__							:	Any    							=							jit_generate(lowerCamelCase_  ).sequences

													self.assertListEqual(generation_outputs.tolist()       ,						jit_generation_outputs.tolist()  )

			def 							UpperCAmelCase__       ( self			:   Union[str, Any]  )						->   Tuple:
								__magic_name__							:	Union[str, Any]    							=							self._get_input_ids_and_config()
								__magic_name__							:	Dict    							=							False
								__magic_name__							:	int    							=							max_length
								__magic_name__							:	Optional[int]    							=							2
								__magic_name__							:	List[str]    							=							2

								for model_class in self.all_generative_model_classes:
													__magic_name__							:	Optional[Any]    							=							model_class(lowerCamelCase_  )

													__magic_name__							:	Union[str, Any]    							=							model.generate(lowerCamelCase_  ).sequences
													self.assertEqual(generation_outputs.shape[0]       ,						input_ids.shape[0] * config.num_return_sequences  )

			def 							UpperCAmelCase__       ( self			:   str  )						->   Optional[int]:
								__magic_name__							:	str    							=							self._get_input_ids_and_config()
								__magic_name__							:	List[Any]    							=							True
								__magic_name__							:	Union[str, Any]    							=							max_length
								__magic_name__							:	List[str]    							=							0.8
								__magic_name__							:	Union[str, Any]    							=							10
								__magic_name__							:	Tuple    							=							0.3
								__magic_name__							:	Optional[Any]    							=							1
								__magic_name__							:	str    							=							8
								__magic_name__							:	Optional[Any]    							=							9

								for model_class in self.all_generative_model_classes:
													__magic_name__							:	Optional[Any]    							=							model_class(lowerCamelCase_  )

													__magic_name__							:	Optional[Any]    							=							model.generate(lowerCamelCase_  ).sequences
													self.assertEqual(generation_outputs.shape[-1]       ,						lowerCamelCase_  )

													__magic_name__							:	Tuple    							=							jit(model.generate  )
													__magic_name__							:	str    							=							jit_generate(lowerCamelCase_  ).sequences

													self.assertListEqual(generation_outputs.tolist()       ,						jit_generation_outputs.tolist()  )

			def 							UpperCAmelCase__       ( self			:   Tuple  )						->   Any:
								__magic_name__							:	Any    							=							self._get_input_ids_and_config()
								__magic_name__							:	List[str]    							=							max_length
								__magic_name__							:	List[Any]    							=							1
								__magic_name__							:	str    							=							8
								__magic_name__							:	str    							=							9

								for model_class in self.all_generative_model_classes:
													__magic_name__							:	List[Any]    							=							model_class(lowerCamelCase_  )

													__magic_name__							:	List[Any]    							=							model.generate(lowerCamelCase_  ).sequences
													self.assertEqual(generation_outputs.shape[-1]       ,						lowerCamelCase_  )

													__magic_name__							:	Tuple    							=							jit(model.generate  )
													__magic_name__							:	Tuple    							=							jit_generate(lowerCamelCase_  ).sequences

													self.assertListEqual(generation_outputs.tolist()       ,						jit_generation_outputs.tolist()  )

			def 							UpperCAmelCase__       ( self			:   Dict  )						->   Optional[Any]:
								__magic_name__							:	Any    							=							self._get_input_ids_and_config()
								__magic_name__							:	Any    							=							max_length
								__magic_name__							:	List[str]    							=							2
								__magic_name__							:	Any    							=							1
								__magic_name__							:	str    							=							8
								__magic_name__							:	Optional[Any]    							=							9

								for model_class in self.all_generative_model_classes:
													__magic_name__							:	int    							=							model_class(lowerCamelCase_  )

													__magic_name__							:	Tuple    							=							model.generate(lowerCamelCase_  ).sequences
													self.assertEqual(generation_outputs.shape[-1]       ,						lowerCamelCase_  )

													__magic_name__							:	Optional[int]    							=							jit(model.generate  )
													__magic_name__							:	int    							=							jit_generate(lowerCamelCase_  ).sequences

													self.assertListEqual(generation_outputs.tolist()       ,						jit_generation_outputs.tolist()  )

			def 							UpperCAmelCase__       ( self			:   Optional[int]  )						->   Optional[Any]:
								__magic_name__							:	Union[str, Any]    							=							self._get_input_ids_and_config()

								# pad attention mask on the left
								__magic_name__							:	Any    							=							attention_mask.at[(0, 0)].set(0  )

								__magic_name__							:	Union[str, Any]    							=							False
								__magic_name__							:	Optional[Any]    							=							max_length

								for model_class in self.all_generative_model_classes:
													__magic_name__							:	Any    							=							model_class(lowerCamelCase_  )

													__magic_name__							:	Any    							=							model.generate(lowerCamelCase_       ,						attention_mask=lowerCamelCase_  ).sequences
													self.assertEqual(generation_outputs.shape[-1]       ,						lowerCamelCase_  )

													__magic_name__							:	List[Any]    							=							jit(model.generate  )
													__magic_name__							:	Any    							=							jit_generate(lowerCamelCase_       ,						attention_mask=lowerCamelCase_  ).sequences

													self.assertListEqual(generation_outputs.tolist()       ,						jit_generation_outputs.tolist()  )

			def 							UpperCAmelCase__       ( self			:   Optional[int]  )						->   Union[str, Any]:
								__magic_name__							:	Optional[Any]    							=							self._get_input_ids_and_config()

								# pad attention mask on the left
								__magic_name__							:	Dict    							=							attention_mask.at[(0, 0)].set(0  )

								__magic_name__							:	str    							=							True
								__magic_name__							:	List[Any]    							=							max_length

								for model_class in self.all_generative_model_classes:
													__magic_name__							:	Optional[Any]    							=							model_class(lowerCamelCase_  )

													__magic_name__							:	Union[str, Any]    							=							model.generate(lowerCamelCase_       ,						attention_mask=lowerCamelCase_  ).sequences
													self.assertEqual(generation_outputs.shape[-1]       ,						lowerCamelCase_  )

													__magic_name__							:	Optional[int]    							=							jit(model.generate  )
													__magic_name__							:	Optional[Any]    							=							jit_generate(lowerCamelCase_       ,						attention_mask=lowerCamelCase_  ).sequences

													self.assertListEqual(generation_outputs.tolist()       ,						jit_generation_outputs.tolist()  )


			def 							UpperCAmelCase__       ( self			:   int  )						->   Optional[int]:
								__magic_name__							:	Any    							=							self._get_input_ids_and_config()

								# pad attention mask on the left
								__magic_name__							:	str    							=							attention_mask.at[(0, 0)].set(0  )

								__magic_name__							:	List[Any]    							=							2
								__magic_name__							:	Optional[Any]    							=							max_length

								for model_class in self.all_generative_model_classes:
													__magic_name__							:	Optional[int]    							=							model_class(lowerCamelCase_  )

													__magic_name__							:	Union[str, Any]    							=							model.generate(lowerCamelCase_       ,						attention_mask=lowerCamelCase_  ).sequences
													self.assertEqual(generation_outputs.shape[-1]       ,						lowerCamelCase_  )

													__magic_name__							:	Dict    							=							jit(model.generate  )
													__magic_name__							:	List[str]    							=							jit_generate(lowerCamelCase_       ,						attention_mask=lowerCamelCase_  ).sequences

													self.assertListEqual(generation_outputs.tolist()       ,						jit_generation_outputs.tolist()  )






@require_flax
class    lowerCamelCase    (		unittest.TestCase						):






			'''simple docstring'''





			def 							UpperCAmelCase__       ( self			:   Any  )						->   Any:
								__magic_name__							:	Any    							=							AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-bert'''  )
								__magic_name__							:	List[Any]    							=							FlaxAutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-bert-flax-only'''  )

								__magic_name__							:	str    							=							'''Hello world'''
								__magic_name__							:	Any    							=							tokenizer(lowerCamelCase_       ,						return_tensors='''np'''  ).input_ids

								# typos are quickly detected (the correct argument is `do_sample`)
								with self.assertRaisesRegex(lowerCamelCase_       ,						'''do_samples'''  ):
													model.generate(lowerCamelCase_       ,						do_samples=lowerCamelCase_  )

								# arbitrary arguments that will not be used anywhere are also not accepted
								with self.assertRaisesRegex(lowerCamelCase_       ,						'''foo'''  ):
													__magic_name__							:	Any    							=							{'''foo''': '''bar'''}
													model.generate(lowerCamelCase_       ,						**lowerCamelCase_  )









import requests







def 		lowercase__   (		__A:	str							,__A:	str				):





					'''simple docstring'''






					__magic_name__							:	Tuple    							=							{'''Content-Type''': '''application/json'''}
					__magic_name__							:	Union[str, Any]    							=							requests.post(__A							,json={'''text''': message_body}							,headers=__A				)
					if response.status_code != 2_0_0:
										__magic_name__							:	int    							=							(
										    '''Request to slack returned an error '''
										    F'''{response.status_code}, the response is:\n{response.text}'''
										)
										raise ValueError(__A				)


if __name__ == "__main__":
	# Set the slack url to the one provided by Slack when you create the webhook at
	# https://my.slack.com/services/new/incoming-webhook/
	send_slack_message('''<YOUR MESSAGE BODY>''', '''<SLACK CHANNEL URL>''')



