



"""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

			__SCREAMING_SNAKE_CASE  :						Optional[int]    							=      '''0.12'''  # assumed parallelism: 8


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





					'''simple docstring'''



					@classmethod
					def 			snake_case__      (    cls   ):
									_lowerCamelCase        =				TOKEN
									HfFolder.save_token(lowerCamelCase__   )

					@classmethod
					def 			snake_case__      (    cls   ):
									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 			snake_case__      (    self   ):
									_lowerCamelCase        =				BertConfig(
									    vocab_size=9_9				,       hidden_size=3_2				,       num_hidden_layers=5				,       num_attention_heads=4				,       intermediate_size=3_7   )
									_lowerCamelCase        =				FlaxBertModel(lowerCamelCase__   )
									model.push_to_hub('''test-model-flax'''				,       use_auth_token=self._token   )

									_lowerCamelCase        =				FlaxBertModel.from_pretrained(F"""{USER}/test-model-flax"""   )

									_lowerCamelCase        =				flatten_dict(unfreeze(model.params   )   )
									_lowerCamelCase        =				flatten_dict(unfreeze(new_model.params   )   )

									for key in base_params.keys():
													_lowerCamelCase        =				(base_params[key] - new_params[key]).sum().item()
													self.assertLessEqual(lowerCamelCase__				,       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(lowerCamelCase__				,       repo_id='''test-model-flax'''				,       push_to_hub=lowerCamelCase__				,       use_auth_token=self._token   )

									_lowerCamelCase        =				FlaxBertModel.from_pretrained(F"""{USER}/test-model-flax"""   )

									_lowerCamelCase        =				flatten_dict(unfreeze(model.params   )   )
									_lowerCamelCase        =				flatten_dict(unfreeze(new_model.params   )   )

									for key in base_params.keys():
													_lowerCamelCase        =				(base_params[key] - new_params[key]).sum().item()
													self.assertLessEqual(lowerCamelCase__				,       1e-3				,       msg=F"""{key} not identical"""   )





					def 			snake_case__      (    self   ):
									_lowerCamelCase        =				BertConfig(
									    vocab_size=9_9				,       hidden_size=3_2				,       num_hidden_layers=5				,       num_attention_heads=4				,       intermediate_size=3_7   )
									_lowerCamelCase        =				FlaxBertModel(lowerCamelCase__   )
									model.push_to_hub('''valid_org/test-model-flax-org'''				,       use_auth_token=self._token   )

									_lowerCamelCase        =				FlaxBertModel.from_pretrained('''valid_org/test-model-flax-org'''   )

									_lowerCamelCase        =				flatten_dict(unfreeze(model.params   )   )
									_lowerCamelCase        =				flatten_dict(unfreeze(new_model.params   )   )

									for key in base_params.keys():
													_lowerCamelCase        =				(base_params[key] - new_params[key]).sum().item()
													self.assertLessEqual(lowerCamelCase__				,       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(
													    lowerCamelCase__				,       repo_id='''valid_org/test-model-flax-org'''				,       push_to_hub=lowerCamelCase__				,       use_auth_token=self._token   )

									_lowerCamelCase        =				FlaxBertModel.from_pretrained('''valid_org/test-model-flax-org'''   )

									_lowerCamelCase        =				flatten_dict(unfreeze(model.params   )   )
									_lowerCamelCase        =				flatten_dict(unfreeze(new_model.params   )   )

									for key in base_params.keys():
													_lowerCamelCase        =				(base_params[key] - new_params[key]).sum().item()
													self.assertLessEqual(lowerCamelCase__				,       1e-3				,       msg=F"""{key} not identical"""   )



def        lowerCAmelCase_(   lowercase_     :							Any						,   lowercase_     :							List[Any]							)	->       Optional[Any]:
				_lowerCamelCase        =				True
				_lowerCamelCase        =				flatten_dict(modela.params							)
				_lowerCamelCase        =				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:
												_lowerCamelCase        =				False

				return models_are_equal




@require_flax
class 							lowerCamelCase_(   unittest.TestCase    ):





					'''simple docstring'''



					def 			snake_case__      (    self   ):
									_lowerCamelCase        =				BertConfig.from_pretrained('''hf-internal-testing/tiny-bert-flax-only'''   )
									_lowerCamelCase        =				FlaxBertModel(lowerCamelCase__   )

									_lowerCamelCase        =				'''bert'''
									with tempfile.TemporaryDirectory() as tmp_dir:
													model.save_pretrained(os.path.join(lowerCamelCase__				,       lowerCamelCase__   )   )

													with self.assertRaises(lowerCamelCase__   ):
																	_lowerCamelCase        =				FlaxBertModel.from_pretrained(lowerCamelCase__   )

													_lowerCamelCase        =				FlaxBertModel.from_pretrained(lowerCamelCase__				,       subfolder=lowerCamelCase__   )

									self.assertTrue(check_models_equal(lowerCamelCase__				,       lowerCamelCase__   )   )

					def 			snake_case__      (    self   ):
									_lowerCamelCase        =				BertConfig.from_pretrained('''hf-internal-testing/tiny-bert-flax-only'''   )
									_lowerCamelCase        =				FlaxBertModel(lowerCamelCase__   )

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

													with self.assertRaises(lowerCamelCase__   ):
																	_lowerCamelCase        =				FlaxBertModel.from_pretrained(lowerCamelCase__   )

													_lowerCamelCase        =				FlaxBertModel.from_pretrained(lowerCamelCase__				,       subfolder=lowerCamelCase__   )

									self.assertTrue(check_models_equal(lowerCamelCase__				,       lowerCamelCase__   )   )

					def 			snake_case__      (    self   ):
									_lowerCamelCase        =				'''bert'''
									_lowerCamelCase        =				'''hf-internal-testing/tiny-random-bert-subfolder'''

									with self.assertRaises(lowerCamelCase__   ):
													_lowerCamelCase        =				FlaxBertModel.from_pretrained(lowerCamelCase__   )

									_lowerCamelCase        =				FlaxBertModel.from_pretrained(lowerCamelCase__				,       subfolder=lowerCamelCase__   )

									self.assertIsNotNone(lowerCamelCase__   )





					def 			snake_case__      (    self   ):
									_lowerCamelCase        =				'''bert'''
									_lowerCamelCase        =				'''hf-internal-testing/tiny-random-bert-sharded-subfolder'''
									with self.assertRaises(lowerCamelCase__   ):
													_lowerCamelCase        =				FlaxBertModel.from_pretrained(lowerCamelCase__   )

									_lowerCamelCase        =				FlaxBertModel.from_pretrained(lowerCamelCase__				,       subfolder=lowerCamelCase__   )

									self.assertIsNotNone(lowerCamelCase__   )





"""simple docstring"""
from __future__ import annotations

from typing import Any



def        lowerCAmelCase_(   lowercase_     :							list[Any]							)	->       None:
				create_state_space_tree(lowercase_						,   []						,   0							)



def        lowerCAmelCase_(   lowercase_     :							list[Any]						,   lowercase_     :							list[Any]						,   lowercase_     :							int							)	->       None:

				if index == len(lowercase_							):
								print(lowercase_							)
								return

				create_state_space_tree(lowercase_						,   lowercase_						,   index + 1							)
				current_subsequence.append(sequence[index]							)
				create_state_space_tree(lowercase_						,   lowercase_						,   index + 1							)
				current_subsequence.pop()


if __name__ == "__main__":
			__SCREAMING_SNAKE_CASE  :						list[Any]    							=      [3, 1, 2, 4]
			generate_all_subsequences(seq)

			seq.clear()
			seq.extend(['''A''', '''B''', '''C'''])
			generate_all_subsequences(seq)
