






'''simple docstring'''





import torch
from torch import nn
from transformers import CLIPPreTrainedModel, CLIPVisionModel

from ...models.attention import BasicTransformerBlock
from ...utils import logging


_UpperCamelCase						:						str  =					logging.get_logger(__name__)  # pylint: disable=invalid-name


class  snake_case__      (					UpperCamelCase):




					def __init__(  self  :		Optional[int]				, _A  :		Dict				, _A  :		List[str]=7_68      )    ->							str:
								super().__init__(_A      )
								UpperCAmelCase_      :       Dict          =       proj_size

								UpperCAmelCase_      :       Any          =       CLIPVisionModel(_A      )
								UpperCAmelCase_      :       Optional[int]          =       PaintByExampleMapper(_A      )
								UpperCAmelCase_      :       Any          =       nn.LayerNorm(config.hidden_size      )
								UpperCAmelCase_      :       List[str]          =       nn.Linear(config.hidden_size				, self.proj_size      )

								# uncondition for scaling
								UpperCAmelCase_      :       Any          =       nn.Parameter(torch.randn((1, 1, self.proj_size)      )      )





					def 		A      (  self  :		List[Any]				, _A  :		Optional[Any]				, _A  :		List[str]=False      )    ->							Dict:
								UpperCAmelCase_      :       Optional[Any]          =       self.model(pixel_values=_A      )
								UpperCAmelCase_      :       int          =       clip_output.pooler_output
								UpperCAmelCase_      :       Dict          =       self.mapper(latent_states[:, None]      )
								UpperCAmelCase_      :       Dict          =       self.final_layer_norm(_A      )
								UpperCAmelCase_      :       List[str]          =       self.proj_out(_A      )
								if return_uncond_vector:
											return latent_states, self.uncond_vector

								return latent_states






class  snake_case__      (					nn.Module):




					def __init__(  self  :		Dict				, _A  :		Tuple      )    ->							str:
								super().__init__()
								UpperCAmelCase_      :       str          =       (config.num_hidden_layers + 1) // 5
								UpperCAmelCase_      :       Any          =       config.hidden_size
								UpperCAmelCase_      :       Optional[Any]          =       1
								UpperCAmelCase_      :       str          =       nn.ModuleList(
								    [
								        BasicTransformerBlock(_A				, _A				, _A				, activation_fn='''gelu'''				, attention_bias=_A      )
								        for _ in range(_A      )
								    ]      )





					def 		A      (  self  :		Optional[Any]				, _A  :		List[Any]      )    ->							List[Any]:
								for block in self.blocks:
											UpperCAmelCase_      :       Optional[int]          =       block(_A      )

								return hidden_states










'''simple docstring'''





_UpperCamelCase						:						Dict  =					range(2, 20 + 1)
_UpperCamelCase						:						str  =					[10**k for k in range(ks[-1] + 1)]
_UpperCamelCase						:						dict[int, dict[int, list[list[int]]]]  =					{}
def __UpperCAmelCase   (					A					:			List[Any]			,       A					:			str			,       A					:			Union[str, Any]			,       A					:			Union[str, Any]		)    ->   Optional[Any]:
			UpperCAmelCase_      :       Optional[Any]          =       sum(a_i[j] for j in range(A			,       len(A		)		)		)
			UpperCAmelCase_      :       Union[str, Any]          =       sum(a_i[j] * base[j] for j in range(min(len(A		)			,       A		)		)		)

			UpperCAmelCase_    ,      UpperCAmelCase_      :       Optional[int]          =       0, 0
			UpperCAmelCase_      :       str          =       n - i

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

			if sub_memo is not None:
						UpperCAmelCase_      :       Dict          =       sub_memo.get(A		)

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

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

						else:
									UpperCAmelCase_      :       Tuple          =       []
			else:
						UpperCAmelCase_      :       List[str]          =       {c: []}
						UpperCAmelCase_      :       List[str]          =       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(A			,       k - 1			,       i + dn			,       A		)
									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_      :       int          =       compute(A			,       A			,       i + dn			,       A		)
						diff += _diff
						dn += terms_jumped

			UpperCAmelCase_      :       List[str]          =       sub_memo[c]

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

			# cache the jump for this value digitsum(b) and c
			sub_memo[c].insert(A			,       (diff, dn, k)		)
			return (diff, dn)
def __UpperCAmelCase   (					A					:			List[str]			,       A					:			Dict			,       A					:			List[str]			,       A					:			Union[str, Any]		)    ->   str:
			if i >= n:
						return 0, i
			if k > len(A		):
						a_i.extend([0 for _ in range(k - len(A		)		)]		)

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

			while i < n:
						i += 1
						UpperCAmelCase_      :       Optional[int]          =       ds_c + ds_b
						diff += addend
						UpperCAmelCase_      :       Any          =       0
						for j in range(A		):
									UpperCAmelCase_      :       Dict          =       a_i[j] + addend
									UpperCAmelCase_    ,      UpperCAmelCase_      :       Tuple          =       divmod(A			,       1_0		)

									ds_c += a_i[j]

						if addend > 0:
									break

			if addend > 0:
						add(A			,       A			,       A		)
			return diff, i - start_i
def __UpperCAmelCase   (					A					:			Dict			,       A					:			Union[str, Any]			,       A					:			str		)    ->   List[Any]:
			for j in range(A			,       len(A		)		):
						UpperCAmelCase_      :       List[str]          =       digits[j] + addend
						if s >= 1_0:
									UpperCAmelCase_    ,      UpperCAmelCase_      :       Any          =       divmod(A			,       1_0		)
									UpperCAmelCase_      :       str          =       addend // 1_0 + quotient
						else:
									UpperCAmelCase_      :       Any          =       s
									UpperCAmelCase_      :       Any          =       addend // 1_0

						if addend == 0:
									break

			while addend > 0:
						UpperCAmelCase_    ,      UpperCAmelCase_      :       Union[str, Any]          =       divmod(A			,       1_0		)
						digits.append(A		)
def __UpperCAmelCase   (					A					:			int = 1_0**1_5		)    ->   int:
			UpperCAmelCase_      :       Any          =       [1]
			UpperCAmelCase_      :       Union[str, Any]          =       1
			UpperCAmelCase_      :       List[Any]          =       0
			while True:
						UpperCAmelCase_    ,      UpperCAmelCase_      :       str          =       next_term(A			,       2_0			,       i + dn			,       A		)
						dn += terms_jumped
						if dn == n - i:
									break

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


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


