

'''simple docstring'''





import copy
import inspect
import unittest

from transformers import AutoBackbone
from transformers.configuration_utils import PretrainedConfig
from transformers.testing_utils import require_timm, require_torch, torch_device
from transformers.utils.import_utils import is_torch_available

from ...test_backbone_common import BackboneTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor


if is_torch_available():
							import torch

							from transformers import TimmBackbone, TimmBackboneConfig

from ...test_pipeline_mixin import PipelineTesterMixin







class 	lowercase    :


		"""simple docstring"""




		def __init__(				self	,       __snake_case	,       __snake_case=None	,       __snake_case=None	,       __snake_case=None	,       __snake_case="resnet50"	,       __snake_case=3	,       __snake_case=32	,       __snake_case=3	,       __snake_case=True	,       __snake_case=True	,       ):
			_UpperCamelCase   :		Optional[Any]			=						parent
			_UpperCamelCase   :		Dict			=						out_indices if out_indices is not None else [4]
			_UpperCamelCase   :		Optional[Any]			=						stage_names
			_UpperCamelCase   :		int			=						out_features
			_UpperCamelCase   :		Any			=						backbone
			_UpperCamelCase   :		Dict			=						batch_size
			_UpperCamelCase   :		Union[str, Any]			=						image_size
			_UpperCamelCase   :		List[Any]			=						num_channels
			_UpperCamelCase   :		Any			=						use_pretrained_backbone
			_UpperCamelCase   :		Dict			=						is_training


		def 		A__					(				self):
			_UpperCamelCase   :		Dict			=						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])
			_UpperCamelCase   :		Optional[Any]			=						self.get_config()

			return config, pixel_values


		def 		A__					(				self):
			return TimmBackboneConfig(
			    image_size=self.image_size	,       num_channels=self.num_channels	,       out_features=self.out_features	,       out_indices=self.out_indices	,       stage_names=self.stage_names	,       use_pretrained_backbone=self.use_pretrained_backbone	,       backbone=self.backbone	,       )


		def 		A__					(				self	,       __snake_case	,       __snake_case):
			_UpperCamelCase   :		Optional[int]			=						TimmBackbone(config=__snake_case)
			model.to(__snake_case)
			model.eval()
			with torch.no_grad():
				_UpperCamelCase   :		Any			=						model(__snake_case)
			self.parent.assertEqual(
			    result.feature_map[-1].shape	,       (self.batch_size, model.channels[-1], 14, 14)	,       )






		def 		A__					(				self):
			_UpperCamelCase   :		str			=						self.prepare_config_and_inputs()
			_UpperCamelCase   :		int			=						config_and_inputs
			_UpperCamelCase   :		Any			=						{'pixel_values': pixel_values}
			return config, inputs_dict







@require_torch
@require_timm
class 	lowercase    (				_lowercase    ,							_lowercase    ,							_lowercase    ,							unittest.TestCase							):


		"""simple docstring"""




		a__					       =  (TimmBackbone,) if is_torch_available() else ()
		a__					       =  {"feature-extraction": TimmBackbone} if is_torch_available() else {}
		a__					       =  False
		a__					       =  False
		a__					       =  False
		a__					       =  False


		def 		A__					(				self):
			_UpperCamelCase   :		List[str]			=						TimmBackboneModelTester(self)
			_UpperCamelCase   :		List[Any]			=						ConfigTester(self	,       config_class=__snake_case	,       has_text_modality=__snake_case)


		def 		A__					(				self):
			self.config_tester.create_and_test_config_to_json_string()
			self.config_tester.create_and_test_config_to_json_file()
			self.config_tester.create_and_test_config_from_and_save_pretrained()
			self.config_tester.create_and_test_config_with_num_labels()
			self.config_tester.check_config_can_be_init_without_params()
			self.config_tester.check_config_arguments_init()


		def 		A__					(				self):
			_UpperCamelCase   :		int			=						'resnet18'
			_UpperCamelCase   :		Tuple			=						'microsoft/resnet-18'

			_UpperCamelCase   :		int			=						AutoBackbone.from_pretrained(__snake_case	,       use_timm_backbone=__snake_case)
			_UpperCamelCase   :		List[Any]			=						AutoBackbone.from_pretrained(__snake_case)

			self.assertEqual(len(timm_model.out_features)	,       len(transformers_model.out_features))
			self.assertEqual(len(timm_model.stage_names)	,       len(transformers_model.stage_names))
			self.assertEqual(timm_model.channels	,       transformers_model.channels)
			# Out indices are set to the last layer by default. For timm models, we don't know
			# the number of layers in advance, so we set it to (-1,), whereas for transformers
			# models, we set it to [len(stage_names) - 1] (kept for backward compatibility).
			self.assertEqual(timm_model.out_indices	,       (-1,))
			self.assertEqual(transformers_model.out_indices	,       [len(timm_model.stage_names) - 1])

			_UpperCamelCase   :		Optional[Any]			=						AutoBackbone.from_pretrained(__snake_case	,       use_timm_backbone=__snake_case	,       out_indices=[1, 2, 3])
			_UpperCamelCase   :		Dict			=						AutoBackbone.from_pretrained(__snake_case	,       out_indices=[1, 2, 3])

			self.assertEqual(timm_model.out_indices	,       transformers_model.out_indices)
			self.assertEqual(len(timm_model.out_features)	,       len(transformers_model.out_features))
			self.assertEqual(timm_model.channels	,       transformers_model.channels)


		@unittest.skip('TimmBackbone doesn\'t support feed forward chunking')
		def 		A__					(				self):
			pass


		@unittest.skip('TimmBackbone doesn\'t have num_hidden_layers attribute')
		def 		A__					(				self):
			pass


		@unittest.skip('TimmBackbone initialization is managed on the timm side')
		def 		A__					(				self):
			pass


		@unittest.skip('TimmBackbone models doesn\'t have inputs_embeds')
		def 		A__					(				self):
			pass


		@unittest.skip('TimmBackbone models doesn\'t have inputs_embeds')
		def 		A__					(				self):
			pass


		@unittest.skip('TimmBackbone model cannot be created without specifying a backbone checkpoint')
		def 		A__					(				self):
			pass


		@unittest.skip('Only checkpoints on timm can be loaded into TimmBackbone')
		def 		A__					(				self):
			pass


		@unittest.skip('model weights aren\'t tied in TimmBackbone.')
		def 		A__					(				self):
			pass


		@unittest.skip('model weights aren\'t tied in TimmBackbone.')
		def 		A__					(				self):
			pass


		@unittest.skip('Only checkpoints on timm can be loaded into TimmBackbone')
		def 		A__					(				self):
			pass


		@unittest.skip('Only checkpoints on timm can be loaded into TimmBackbone')
		def 		A__					(				self):
			pass


		@unittest.skip('TimmBackbone doesn\'t have hidden size info in its configuration.')
		def 		A__					(				self):
			pass


		@unittest.skip('TimmBackbone doesn\'t support output_attentions.')
		def 		A__					(				self):
			pass


		@unittest.skip('Safetensors is not supported by timm.')
		def 		A__					(				self):
			pass


		@unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.')
		def 		A__					(				self):
			pass


		def 		A__					(				self):
			_UpperCamelCase   :		Optional[Any]			=						self.model_tester.prepare_config_and_inputs_for_common()

			for model_class in self.all_model_classes:
				_UpperCamelCase   :		str			=						model_class(__snake_case)
				_UpperCamelCase   :		Dict			=						inspect.signature(model.forward)
				# signature.parameters is an OrderedDict => so arg_names order is deterministic
				_UpperCamelCase   :		int			=						[*signature.parameters.keys()]

				_UpperCamelCase   :		Dict			=						['pixel_values']
				self.assertListEqual(arg_names[:1]	,       __snake_case)


		def 		A__					(				self):
			_UpperCamelCase   :		int			=						self.model_tester.prepare_config_and_inputs_for_common()
			_UpperCamelCase   :		Dict			=						True
			_UpperCamelCase   :		int			=						self.has_attentions

			# no need to test all models as different heads yield the same functionality
			_UpperCamelCase   :		Any			=						self.all_model_classes[0]
			_UpperCamelCase   :		List[Any]			=						model_class(__snake_case)
			model.to(__snake_case)

			_UpperCamelCase   :		List[Any]			=						self._prepare_for_class(__snake_case	,       __snake_case)
			_UpperCamelCase   :		Optional[Any]			=						model(**__snake_case)
			_UpperCamelCase   :		int			=						outputs[0][-1]

			# Encoder-/Decoder-only models
			_UpperCamelCase   :		List[Any]			=						outputs.hidden_states[0]
			hidden_states.retain_grad()

			if self.has_attentions:
				_UpperCamelCase   :		List[Any]			=						outputs.attentions[0]
				attentions.retain_grad()

			output.flatten()[0].backward(retain_graph=__snake_case)

			self.assertIsNotNone(hidden_states.grad)

			if self.has_attentions:
				self.assertIsNotNone(attentions.grad)






		def 		A__					(				self):
			_UpperCamelCase   :		str			=						self.model_tester.prepare_config_and_inputs_for_common()

			for model_class in self.all_model_classes:
				_UpperCamelCase   :		List[Any]			=						model_class(__snake_case)
				model.to(__snake_case)
				model.eval()
				_UpperCamelCase   :		str			=						model(**__snake_case)

				self.assertEqual(len(result.feature_maps)	,       len(config.out_indices))
				self.assertEqual(len(model.channels)	,       len(config.out_indices))

				# Check output of last stage is taken if out_features=None, out_indices=None
				_UpperCamelCase   :		int			=						copy.deepcopy(__snake_case)
				_UpperCamelCase   :		str			=						None
				_UpperCamelCase   :		Optional[Any]			=						model_class(__snake_case)
				model.to(__snake_case)
				model.eval()
				_UpperCamelCase   :		Tuple			=						model(**__snake_case)

				self.assertEqual(len(result.feature_maps)	,       1)
				self.assertEqual(len(model.channels)	,       1)

				# Check backbone can be initialized with fresh weights
				_UpperCamelCase   :		Union[str, Any]			=						copy.deepcopy(__snake_case)
				_UpperCamelCase   :		Optional[int]			=						False
				_UpperCamelCase   :		Optional[int]			=						model_class(__snake_case)
				model.to(__snake_case)
				model.eval()
				_UpperCamelCase   :		Any			=						model(**__snake_case)







lowerCAmelCase__								=		range(2, 2_0 + 1)
lowerCAmelCase__								=		[1_0**k for k in range(ks[-1] + 1)]
lowerCAmelCase__								=		{}






def 					lowerCamelCase_     (							UpperCAmelCase_							:     Optional[Any]							,       UpperCAmelCase_							:     List[str]							,       UpperCAmelCase_							:     Any							,       UpperCAmelCase_							:     int	)						->				Tuple:




	'''simple docstring'''




	_UpperCamelCase   :		Dict			=						sum(a_i[j] for j in range(UpperCAmelCase_							,       len(UpperCAmelCase_	)	)	)
	_UpperCamelCase   :		Optional[int]			=						sum(a_i[j] * base[j] for j in range(min(len(UpperCAmelCase_	)							,       UpperCAmelCase_	)	)	)

	_UpperCamelCase	,		_UpperCamelCase   :		Dict			=						0, 0
	_UpperCamelCase   :		Optional[int]			=						n - i

	_UpperCamelCase   :		Union[str, Any]			=						memo.get(UpperCAmelCase_	)

	if sub_memo is not None:
		_UpperCamelCase   :		str			=						sub_memo.get(UpperCAmelCase_	)

		if jumps is not None and len(UpperCAmelCase_	) > 0:
			# find and make the largest jump without going over
			_UpperCamelCase   :		str			=						-1
			for _k in range(len(UpperCAmelCase_	) - 1							,       -1							,       -1	):
				if jumps[_k][2] <= k and jumps[_k][1] <= max_dn:
					_UpperCamelCase   :		Optional[Any]			=						_k
					break

			if max_jump >= 0:
				_UpperCamelCase	,		_UpperCamelCase	,		_UpperCamelCase   :		Optional[Any]			=						jumps[max_jump]
				# since the difference between jumps is cached, add c
				_UpperCamelCase   :		Tuple			=						diff + c
				for j in range(min(UpperCAmelCase_							,       len(UpperCAmelCase_	)	)	):
					_UpperCamelCase	,		_UpperCamelCase   :		Dict			=						divmod(UpperCAmelCase_							,       1_0	)
				if new_c > 0:
					add(UpperCAmelCase_							,       UpperCAmelCase_							,       UpperCAmelCase_	)

		else:
			_UpperCamelCase   :		Union[str, Any]			=						[]
	else:
		_UpperCamelCase   :		List[Any]			=						{c: []}
		_UpperCamelCase   :		Optional[int]			=						sub_memo

	if dn >= max_dn or c + diff >= base[k]:
		return diff, dn

	if k > ks[0]:
		while True:
			# keep doing smaller jumps
			_UpperCamelCase	,		_UpperCamelCase   :		Optional[Any]			=						next_term(UpperCAmelCase_							,       k - 1							,       i + dn							,       UpperCAmelCase_	)
			diff += _diff
			dn += terms_jumped

			if dn >= max_dn or c + diff >= base[k]:
				break
	else:
		# would be too small a jump, just compute sequential terms instead
		_UpperCamelCase	,		_UpperCamelCase   :		Any			=						compute(UpperCAmelCase_							,       UpperCAmelCase_							,       i + dn							,       UpperCAmelCase_	)
		diff += _diff
		dn += terms_jumped

	_UpperCamelCase   :		List[str]			=						sub_memo[c]

	# keep jumps sorted by # of terms skipped
	_UpperCamelCase   :		Union[str, Any]			=						0
	while j < len(UpperCAmelCase_	):
		if jumps[j][1] > dn:
			break
		j += 1

	# cache the jump for this value digitsum(b) and c
	sub_memo[c].insert(UpperCAmelCase_							,       (diff, dn, k)	)
	return (diff, dn)






def 					lowerCamelCase_     (							UpperCAmelCase_							:     int							,       UpperCAmelCase_							:     Dict							,       UpperCAmelCase_							:     Any							,       UpperCAmelCase_							:     Any	)						->				Dict:




	'''simple docstring'''




	if i >= n:
		return 0, i
	if k > len(UpperCAmelCase_	):
		a_i.extend([0 for _ in range(k - len(UpperCAmelCase_	)	)]	)

	# note: a_i -> b * 10^k + c
	# ds_b -> digitsum(b)
	# ds_c -> digitsum(c)
	_UpperCamelCase   :		Any			=						i
	_UpperCamelCase	,		_UpperCamelCase	,		_UpperCamelCase   :		Any			=						0, 0, 0
	for j in range(len(UpperCAmelCase_	)	):
		if j >= k:
			ds_b += a_i[j]
		else:
			ds_c += a_i[j]

	while i < n:
		i += 1
		_UpperCamelCase   :		Union[str, Any]			=						ds_c + ds_b
		diff += addend
		_UpperCamelCase   :		Union[str, Any]			=						0
		for j in range(UpperCAmelCase_	):
			_UpperCamelCase   :		Union[str, Any]			=						a_i[j] + addend
			_UpperCamelCase	,		_UpperCamelCase   :		Any			=						divmod(UpperCAmelCase_							,       1_0	)

			ds_c += a_i[j]

		if addend > 0:
			break

	if addend > 0:
		add(UpperCAmelCase_							,       UpperCAmelCase_							,       UpperCAmelCase_	)
	return diff, i - start_i






def 					lowerCamelCase_     (							UpperCAmelCase_							:     Union[str, Any]							,       UpperCAmelCase_							:     Optional[int]							,       UpperCAmelCase_							:     Any	)						->				Dict:




	'''simple docstring'''




	for j in range(UpperCAmelCase_							,       len(UpperCAmelCase_	)	):
		_UpperCamelCase   :		List[str]			=						digits[j] + addend
		if s >= 1_0:
			_UpperCamelCase	,		_UpperCamelCase   :		Any			=						divmod(UpperCAmelCase_							,       1_0	)
			_UpperCamelCase   :		Union[str, Any]			=						addend // 1_0 + quotient
		else:
			_UpperCamelCase   :		Dict			=						s
			_UpperCamelCase   :		Optional[Any]			=						addend // 1_0

		if addend == 0:
			break

	while addend > 0:
		_UpperCamelCase	,		_UpperCamelCase   :		Dict			=						divmod(UpperCAmelCase_							,       1_0	)
		digits.append(UpperCAmelCase_	)






def 					lowerCamelCase_     (							UpperCAmelCase_							:     int = 1_0**1_5	)						->				int:




	'''simple docstring'''




	_UpperCamelCase   :		Optional[Any]			=						[1]
	_UpperCamelCase   :		Optional[int]			=						1
	_UpperCamelCase   :		int			=						0
	while True:
		_UpperCamelCase	,		_UpperCamelCase   :		List[Any]			=						next_term(UpperCAmelCase_							,       2_0							,       i + dn							,       UpperCAmelCase_	)
		dn += terms_jumped
		if dn == n - i:
			break

	_UpperCamelCase   :		str			=						0
	for j in range(len(UpperCAmelCase_	)	):
		a_n += digits[j] * 1_0**j
	return a_n


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




