






"""simple docstring"""



from __future__ import annotations




def        _lowerCAmelCase						(							UpperCAmelCase__    :   int )						->bool:
						A__  :					Tuple											=					str(UpperCAmelCase__ )
						return len(UpperCAmelCase__ ) == 9 and set(UpperCAmelCase__ ) == set("""123456789""" )




def        _lowerCAmelCase						(							)						->int | None:
						for base_num in range(9_9_9_9,  4_9_9_9,  -1 ):
												A__  :					Optional[int]											=					1_0_0_0_0_2 * base_num
												if is_9_pandigital(UpperCAmelCase__ ):
																		return candidate

						for base_num in range(3_3_3,  9_9,  -1 ):
												A__  :					Any											=					1_0_0_2_0_0_3 * base_num
												if is_9_pandigital(UpperCAmelCase__ ):
																		return candidate

						return None


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














"""simple docstring"""



import os
import unittest
from tempfile import TemporaryDirectory

import torch
import torch.nn as nn

from accelerate.utils import (
    OffloadedWeightsLoader,
    extract_submodules_state_dict,
    load_offloaded_weight,
    offload_state_dict,
    offload_weight,
)






class 							__SCREAMING_SNAKE_CASE  (			nn.Module						):


							def __init__(     self :  List[Any]							):
													'''simple docstring'''





													super().__init__()
													A__  :					int											=					nn.Linear(3     ,      4							)
													A__  :					Union[str, Any]											=					nn.BatchNormad(4							)
													A__  :					Union[str, Any]											=					nn.Linear(4     ,      5							)


							def   _UpperCamelCase    (     self :  str     ,      snake_case :  List[str]							):
													'''simple docstring'''





													return self.lineara(self.batchnorm(self.lineara(snake_case							)							)							)






class 							__SCREAMING_SNAKE_CASE  (			unittest.TestCase						):


							def   _UpperCamelCase    (     self :  str							):
													'''simple docstring'''





													A__  :					int											=					ModelForTest()
													with TemporaryDirectory() as tmp_dir:
																			offload_state_dict(snake_case     ,      model.state_dict()							)
																			A__  :					List[str]											=					os.path.join(snake_case     ,      """index.json"""							)
																			self.assertTrue(os.path.isfile(snake_case							)							)
																			# TODO: add tests on what is inside the index

																			for key in ["linear1.weight", "linear1.bias", "linear2.weight", "linear2.bias"]:
																									A__  :					List[str]											=					os.path.join(snake_case     ,      F'{key}.dat'							)
																									self.assertTrue(os.path.isfile(snake_case							)							)
																									# TODO: add tests on the fact weights are properly loaded


							def   _UpperCamelCase    (     self :  int							):
													'''simple docstring'''





													A__  :					Union[str, Any]											=					[torch.floataa, torch.floataa, torch.bfloataa]

													for dtype in dtypes:
																			A__  :					str											=					torch.randn(2     ,      3     ,      dtype=snake_case							)
																			with TemporaryDirectory() as tmp_dir:
																									A__  :					List[str]											=					offload_weight(snake_case     ,      """weight"""     ,      snake_case     ,      {}							)
																									A__  :					Union[str, Any]											=					os.path.join(snake_case     ,      """weight.dat"""							)
																									self.assertTrue(os.path.isfile(snake_case							)							)
																									self.assertDictEqual(snake_case     ,      {"""weight""": {"""shape""": [2, 3], """dtype""": str(snake_case							).split("""."""							)[1]}}							)

																									A__  :					str											=					load_offloaded_weight(snake_case     ,      index["""weight"""]							)
																									self.assertTrue(torch.equal(snake_case     ,      snake_case							)							)


							def   _UpperCamelCase    (     self :  List[str]							):
													'''simple docstring'''





													A__  :					str											=					ModelForTest()
													A__  :					Union[str, Any]											=					model.state_dict()
													A__  :					Optional[int]											=					{k: v for k, v in state_dict.items() if """linear2""" not in k}
													A__  :					List[Any]											=					{k: v for k, v in state_dict.items() if """linear2""" in k}

													with TemporaryDirectory() as tmp_dir:
																			offload_state_dict(snake_case     ,      snake_case							)
																			A__  :					Dict											=					OffloadedWeightsLoader(state_dict=snake_case     ,      save_folder=snake_case							)

																			# Every key is there with the right value
																			self.assertEqual(sorted(snake_case							)     ,      sorted(state_dict.keys()							)							)
																			for key, param in state_dict.items():
																									self.assertTrue(torch.allclose(snake_case     ,      weight_map[key]							)							)

													A__  :					int											=					{k: v for k, v in state_dict.items() if """weight""" in k}
													A__  :					Tuple											=					{k: v for k, v in state_dict.items() if """weight""" not in k}

													with TemporaryDirectory() as tmp_dir:
																			offload_state_dict(snake_case     ,      snake_case							)
																			A__  :					Optional[Any]											=					OffloadedWeightsLoader(state_dict=snake_case     ,      save_folder=snake_case							)

																			# Every key is there with the right value
																			self.assertEqual(sorted(snake_case							)     ,      sorted(state_dict.keys()							)							)
																			for key, param in state_dict.items():
																									self.assertTrue(torch.allclose(snake_case     ,      weight_map[key]							)							)

													with TemporaryDirectory() as tmp_dir:
																			offload_state_dict(snake_case     ,      snake_case							)
																			# Duplicates are removed
																			A__  :					int											=					OffloadedWeightsLoader(state_dict=snake_case     ,      save_folder=snake_case							)

																			# Every key is there with the right value
																			self.assertEqual(sorted(snake_case							)     ,      sorted(state_dict.keys()							)							)
																			for key, param in state_dict.items():
																									self.assertTrue(torch.allclose(snake_case     ,      weight_map[key]							)							)


							def   _UpperCamelCase    (     self :  Tuple							):
													'''simple docstring'''





													A__  :					List[str]											=					{"""a.1""": 0, """a.10""": 1, """a.2""": 2}
													A__  :					str											=					extract_submodules_state_dict(snake_case     ,      ["""a.1""", """a.2"""]							)
													self.assertDictEqual(snake_case     ,      {"""a.1""": 0, """a.2""": 2}							)

													A__  :					Dict											=					{"""a.1.a""": 0, """a.10.a""": 1, """a.2.a""": 2}
													A__  :					int											=					extract_submodules_state_dict(snake_case     ,      ["""a.1""", """a.2"""]							)
													self.assertDictEqual(snake_case     ,      {"""a.1.a""": 0, """a.2.a""": 2}							)






