





'''simple docstring'''






import tempfile
import unittest

import numpy as np
from huggingface_hub import HfFolder, delete_repo
from requests.exceptions import HTTPError

from transformers import BertConfig, is_flax_available
from transformers.testing_utils import TOKEN, USER, is_staging_test, require_flax


if is_flax_available():
		import os

		from flax.core.frozen_dict import unfreeze
		from flax.traverse_util import flatten_dict

		from transformers import FlaxBertModel

		A   : int             =			'0.12'  # assumed parallelism: 8







@require_flax
@is_staging_test
class 			lowerCamelCase					(						unittest.TestCase  ):


					@classmethod
					def    SCREAMING_SNAKE_CASE_		(					cls	:  int       ):





											'''simple docstring'''





											_snake_case:   Dict  					=		TOKEN
											HfFolder.save_token(__snake_case       )


					@classmethod
					def    SCREAMING_SNAKE_CASE_		(					cls	:  Union[str, Any]       ):





											'''simple docstring'''





											try:
																	delete_repo(token=cls._token       ,     repo_id='test-model-flax'       )
											except HTTPError:
																	pass

											try:
																	delete_repo(token=cls._token       ,     repo_id='valid_org/test-model-flax-org'       )
											except HTTPError:
																	pass


					def    SCREAMING_SNAKE_CASE_		(					self	:  Optional[Any]       ):





											'''simple docstring'''





											_snake_case:   List[Any]  					=		BertConfig(
											    vocab_size=99       ,     hidden_size=32       ,     num_hidden_layers=5       ,     num_attention_heads=4       ,     intermediate_size=37       )
											_snake_case:   Dict  					=		FlaxBertModel(__snake_case       )
											model.push_to_hub('test-model-flax'       ,     use_auth_token=self._token       )

											_snake_case:   Any  					=		FlaxBertModel.from_pretrained(f'''{USER}/test-model-flax'''       )

											_snake_case:   Optional[int]  					=		flatten_dict(unfreeze(model.params       )       )
											_snake_case:   Optional[int]  					=		flatten_dict(unfreeze(new_model.params       )       )

											for key in base_params.keys():
																	_snake_case:   List[Any]  					=		(base_params[key] - new_params[key]).sum().item()
																	self.assertLessEqual(__snake_case       ,     1e-3       ,     msg=f'''{key} not identical'''       )

											# Reset repo
											delete_repo(token=self._token       ,     repo_id='test-model-flax'       )

											# Push to hub via save_pretrained
											with tempfile.TemporaryDirectory() as tmp_dir:
																	model.save_pretrained(__snake_case       ,     repo_id='test-model-flax'       ,     push_to_hub=__snake_case       ,     use_auth_token=self._token       )

											_snake_case:   Any  					=		FlaxBertModel.from_pretrained(f'''{USER}/test-model-flax'''       )

											_snake_case:   Union[str, Any]  					=		flatten_dict(unfreeze(model.params       )       )
											_snake_case:   int  					=		flatten_dict(unfreeze(new_model.params       )       )

											for key in base_params.keys():
																	_snake_case:   List[Any]  					=		(base_params[key] - new_params[key]).sum().item()
																	self.assertLessEqual(__snake_case       ,     1e-3       ,     msg=f'''{key} not identical'''       )




					def    SCREAMING_SNAKE_CASE_		(					self	:  Dict       ):





											'''simple docstring'''





											_snake_case:   str  					=		BertConfig(
											    vocab_size=99       ,     hidden_size=32       ,     num_hidden_layers=5       ,     num_attention_heads=4       ,     intermediate_size=37       )
											_snake_case:   Dict  					=		FlaxBertModel(__snake_case       )
											model.push_to_hub('valid_org/test-model-flax-org'       ,     use_auth_token=self._token       )

											_snake_case:   List[Any]  					=		FlaxBertModel.from_pretrained('valid_org/test-model-flax-org'       )

											_snake_case:   Optional[int]  					=		flatten_dict(unfreeze(model.params       )       )
											_snake_case:   Tuple  					=		flatten_dict(unfreeze(new_model.params       )       )

											for key in base_params.keys():
																	_snake_case:   Any  					=		(base_params[key] - new_params[key]).sum().item()
																	self.assertLessEqual(__snake_case       ,     1e-3       ,     msg=f'''{key} not identical'''       )

											# Reset repo
											delete_repo(token=self._token       ,     repo_id='valid_org/test-model-flax-org'       )

											# Push to hub via save_pretrained
											with tempfile.TemporaryDirectory() as tmp_dir:
																	model.save_pretrained(
																	    __snake_case       ,     repo_id='valid_org/test-model-flax-org'       ,     push_to_hub=__snake_case       ,     use_auth_token=self._token       )

											_snake_case:   List[Any]  					=		FlaxBertModel.from_pretrained('valid_org/test-model-flax-org'       )

											_snake_case:   Union[str, Any]  					=		flatten_dict(unfreeze(model.params       )       )
											_snake_case:   int  					=		flatten_dict(unfreeze(new_model.params       )       )

											for key in base_params.keys():
																	_snake_case:   Any  					=		(base_params[key] - new_params[key]).sum().item()
																	self.assertLessEqual(__snake_case       ,     1e-3       ,     msg=f'''{key} not identical'''       )





def       lowercase_      (		lowercase__				,  lowercase__						)   ->List[Any]:
						_snake_case:   Optional[int]  					=		True
						_snake_case:   Optional[Any]  					=		flatten_dict(modela.params						)
						_snake_case:   Optional[Any]  					=		flatten_dict(modela.params						)
						for key in flat_params_a.keys():
												if np.sum(np.abs(flat_params_a[key] - flat_params_a[key]						)						) > 1e-4:
																		_snake_case:   Optional[Any]  					=		False

						return models_are_equal







@require_flax
class 			lowerCamelCase					(						unittest.TestCase  ):


					def    SCREAMING_SNAKE_CASE_		(					self	:  Tuple       ):





											'''simple docstring'''





											_snake_case:   int  					=		BertConfig.from_pretrained('hf-internal-testing/tiny-bert-flax-only'       )
											_snake_case:   Tuple  					=		FlaxBertModel(__snake_case       )

											_snake_case:   List[Any]  					=		'bert'
											with tempfile.TemporaryDirectory() as tmp_dir:
																	model.save_pretrained(os.path.join(__snake_case       ,     __snake_case       )       )

																	with self.assertRaises(__snake_case       ):
																							_snake_case:   int  					=		FlaxBertModel.from_pretrained(__snake_case       )

																	_snake_case:   Optional[int]  					=		FlaxBertModel.from_pretrained(__snake_case       ,     subfolder=__snake_case       )

											self.assertTrue(check_models_equal(__snake_case       ,     __snake_case       )       )


					def    SCREAMING_SNAKE_CASE_		(					self	:  int       ):





											'''simple docstring'''





											_snake_case:   str  					=		BertConfig.from_pretrained('hf-internal-testing/tiny-bert-flax-only'       )
											_snake_case:   Optional[int]  					=		FlaxBertModel(__snake_case       )

											_snake_case:   Tuple  					=		'bert'
											with tempfile.TemporaryDirectory() as tmp_dir:
																	model.save_pretrained(os.path.join(__snake_case       ,     __snake_case       )       ,     max_shard_size='10KB'       )

																	with self.assertRaises(__snake_case       ):
																							_snake_case:   Tuple  					=		FlaxBertModel.from_pretrained(__snake_case       )

																	_snake_case:   Union[str, Any]  					=		FlaxBertModel.from_pretrained(__snake_case       ,     subfolder=__snake_case       )

											self.assertTrue(check_models_equal(__snake_case       ,     __snake_case       )       )


					def    SCREAMING_SNAKE_CASE_		(					self	:  str       ):





											'''simple docstring'''





											_snake_case:   int  					=		'bert'
											_snake_case:   str  					=		'hf-internal-testing/tiny-random-bert-subfolder'

											with self.assertRaises(__snake_case       ):
																	_snake_case:   Dict  					=		FlaxBertModel.from_pretrained(__snake_case       )

											_snake_case:   Optional[int]  					=		FlaxBertModel.from_pretrained(__snake_case       ,     subfolder=__snake_case       )

											self.assertIsNotNone(__snake_case       )




					def    SCREAMING_SNAKE_CASE_		(					self	:  Dict       ):





											'''simple docstring'''





											_snake_case:   Optional[int]  					=		'bert'
											_snake_case:   Union[str, Any]  					=		'hf-internal-testing/tiny-random-bert-sharded-subfolder'
											with self.assertRaises(__snake_case       ):
																	_snake_case:   Optional[int]  					=		FlaxBertModel.from_pretrained(__snake_case       )

											_snake_case:   int  					=		FlaxBertModel.from_pretrained(__snake_case       ,     subfolder=__snake_case       )

											self.assertIsNotNone(__snake_case       )












'''simple docstring'''






def       lowercase_      (		lowercase__ = 50						)   ->int:
						_snake_case:   Union[str, Any]  					=		[[0] * 3 for _ in range(length + 1						)]

						for row_length in range(length + 1						):
												for tile_length in range(2				,  5						):
																		for tile_start in range(row_length - tile_length + 1						):
																								different_colour_ways_number[row_length][tile_length - 2] += (
																								    different_colour_ways_number[row_length - tile_start - tile_length][
																								        tile_length - 2
																								    ]
																								    + 1
																								)

						return sum(different_colour_ways_number[length]						)


if __name__ == "__main__":
		print(F'{solution() = }')





