



import torch
from torch import nn



class 	lowerCamelCase__      (							nn.Module):




		'''simple docstring'''



		def __init__(self		,__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase=1		,__lowerCamelCase=False			)					->      int:

						"""simple docstring"""






						super().__init__()

						lowerCAmelCase__					:    Union[str, Any]												=					n_token
						lowerCAmelCase__					:    List[str]												=					d_embed
						lowerCAmelCase__					:    str												=					d_proj

						lowerCAmelCase__					:    int												=					cutoffs + [n_token]
						lowerCAmelCase__					:    List[str]												=					[0] + self.cutoffs
						lowerCAmelCase__					:    Dict												=					div_val

						lowerCAmelCase__					:    List[Any]												=					self.cutoffs[0]
						lowerCAmelCase__					:    Union[str, Any]												=					len(self.cutoffs			) - 1
						lowerCAmelCase__					:    Any												=					self.shortlist_size + self.n_clusters

						if self.n_clusters > 0:
										lowerCAmelCase__					:    Optional[Any]												=					nn.Parameter(torch.zeros(self.n_clusters		,self.d_embed			)			)
										lowerCAmelCase__					:    List[Any]												=					nn.Parameter(torch.zeros(self.n_clusters			)			)

						lowerCAmelCase__					:    str												=					nn.ModuleList()
						lowerCAmelCase__					:    Optional[int]												=					nn.ParameterList()

						if div_val == 1:
										for i in range(len(self.cutoffs			)			):
														if d_proj != d_embed:
																		self.out_projs.append(nn.Parameter(torch.FloatTensor(__lowerCamelCase		,__lowerCamelCase			)			)			)
														else:
																		self.out_projs.append(__lowerCamelCase			)

										self.out_layers.append(nn.Linear(__lowerCamelCase		,__lowerCamelCase			)			)
						else:
										for i in range(len(self.cutoffs			)			):
														lowerCAmelCase__						,		lowerCAmelCase__					:    Tuple												=					self.cutoff_ends[i], self.cutoff_ends[i + 1]
														lowerCAmelCase__					:    str												=					d_embed // (div_val**i)

														self.out_projs.append(nn.Parameter(torch.FloatTensor(__lowerCamelCase		,__lowerCamelCase			)			)			)

														self.out_layers.append(nn.Linear(__lowerCamelCase		,r_idx - l_idx			)			)

						lowerCAmelCase__					:    Any												=					keep_order



		def 		lowerCAmelCase__				(self		,__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase			)					->      Union[str, Any]:

						"""simple docstring"""






						if proj is None:
										lowerCAmelCase__					:    List[Any]												=					nn.functional.linear(__lowerCamelCase		,__lowerCamelCase		,bias=__lowerCamelCase			)
						else:
										# if CUDA_MAJOR <= 9 and CUDA_MINOR <= 1:
										lowerCAmelCase__					:    Optional[Any]												=					nn.functional.linear(__lowerCamelCase		,proj.t().contiguous()			)
										lowerCAmelCase__					:    Optional[Any]												=					nn.functional.linear(__lowerCamelCase		,__lowerCamelCase		,bias=__lowerCamelCase			)
										# else:
										#     logit = torch.einsum('bd,de,ev->bv', (hidden, proj, weight.t()))
										#     if bias is not None:
										#         logit = logit + bias

						return logit



		def 		lowerCAmelCase__				(self		,__lowerCamelCase		,__lowerCamelCase=None		,__lowerCamelCase=False			)					->      int:

						"""simple docstring"""






						if labels is not None:
										# Shift so that tokens < n predict n
										lowerCAmelCase__					:    Union[str, Any]												=					hidden[..., :-1, :].contiguous()
										lowerCAmelCase__					:    Tuple												=					labels[..., 1:].contiguous()
										lowerCAmelCase__					:    Tuple												=					hidden.view(-1		,hidden.size(-1			)			)
										lowerCAmelCase__					:    List[Any]												=					labels.view(-1			)
										if hidden.size(0			) != labels.size(0			):
														raise RuntimeError('''Input and labels should have the same size in the batch dimension.'''			)
						else:
										lowerCAmelCase__					:    int												=					hidden.view(-1		,hidden.size(-1			)			)

						if self.n_clusters == 0:
										lowerCAmelCase__					:    Tuple												=					self._compute_logit(__lowerCamelCase		,self.out_layers[0].weight		,self.out_layers[0].bias		,self.out_projs[0]			)
										if labels is not None:
														lowerCAmelCase__					:    str												=					labels != -1_00
														lowerCAmelCase__					:    Union[str, Any]												=					torch.zeros_like(__lowerCamelCase		,dtype=hidden.dtype		,device=hidden.device			)
														lowerCAmelCase__					:    Optional[Any]												=					(
														    -nn.functional.log_softmax(__lowerCamelCase		,dim=-1			)[mask].gather(1		,labels[mask].unsqueeze(1			)			).squeeze(1			)
														)
										else:
														lowerCAmelCase__					:    List[Any]												=					nn.functional.log_softmax(__lowerCamelCase		,dim=-1			)
						else:
										# construct weights and biases
										lowerCAmelCase__						,		lowerCAmelCase__					:    Any												=					[], []
										for i in range(len(self.cutoffs			)			):
														if self.div_val == 1:
																		lowerCAmelCase__						,		lowerCAmelCase__					:    Optional[int]												=					self.cutoff_ends[i], self.cutoff_ends[i + 1]
																		lowerCAmelCase__					:    Optional[int]												=					self.out_layers[0].weight[l_idx:r_idx]
																		lowerCAmelCase__					:    Dict												=					self.out_layers[0].bias[l_idx:r_idx]
														else:
																		lowerCAmelCase__					:    str												=					self.out_layers[i].weight
																		lowerCAmelCase__					:    int												=					self.out_layers[i].bias

														if i == 0:
																		lowerCAmelCase__					:    List[Any]												=					torch.cat([weight_i, self.cluster_weight]		,dim=0			)
																		lowerCAmelCase__					:    Optional[int]												=					torch.cat([bias_i, self.cluster_bias]		,dim=0			)

														weights.append(__lowerCamelCase			)
														biases.append(__lowerCamelCase			)

										lowerCAmelCase__						,		lowerCAmelCase__						,		lowerCAmelCase__					:    Any												=					weights[0], biases[0], self.out_projs[0]

										lowerCAmelCase__					:    Tuple												=					self._compute_logit(__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase			)
										lowerCAmelCase__					:    List[str]												=					nn.functional.log_softmax(__lowerCamelCase		,dim=1			)

										if labels is None:
														lowerCAmelCase__					:    Tuple												=					hidden.new_empty((head_logit.size(0			), self.n_token)			)
										else:
														lowerCAmelCase__					:    List[str]												=					torch.zeros_like(__lowerCamelCase		,dtype=hidden.dtype		,device=hidden.device			)

										lowerCAmelCase__					:    Optional[int]												=					0
										lowerCAmelCase__					:    Any												=					[0] + self.cutoffs
										for i in range(len(__lowerCamelCase			) - 1			):
														lowerCAmelCase__						,		lowerCAmelCase__					:    Optional[Any]												=					cutoff_values[i], cutoff_values[i + 1]

														if labels is not None:
																		lowerCAmelCase__					:    Optional[int]												=					(labels >= l_idx) & (labels < r_idx)
																		lowerCAmelCase__					:    int												=					mask_i.nonzero().squeeze()

																		if indices_i.numel() == 0:
																						continue

																		lowerCAmelCase__					:    List[str]												=					labels.index_select(0		,__lowerCamelCase			) - l_idx
																		lowerCAmelCase__					:    Union[str, Any]												=					head_logprob.index_select(0		,__lowerCamelCase			)
																		lowerCAmelCase__					:    Dict												=					hidden.index_select(0		,__lowerCamelCase			)
														else:
																		lowerCAmelCase__					:    Optional[Any]												=					hidden

														if i == 0:
																		if labels is not None:
																						lowerCAmelCase__					:    List[Any]												=					head_logprob_i.gather(1		,target_i[:, None]			).squeeze(1			)
																		else:
																						lowerCAmelCase__					:    int												=					head_logprob[:, : self.cutoffs[0]]
														else:
																		lowerCAmelCase__						,		lowerCAmelCase__						,		lowerCAmelCase__					:    Optional[int]												=					weights[i], biases[i], self.out_projs[i]

																		lowerCAmelCase__					:    List[Any]												=					self._compute_logit(__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase			)
																		lowerCAmelCase__					:    Dict												=					nn.functional.log_softmax(__lowerCamelCase		,dim=1			)
																		lowerCAmelCase__					:    Any												=					self.cutoffs[0] + i - 1  # No probability for the head cluster
																		if labels is not None:
																						lowerCAmelCase__					:    Optional[Any]												=					head_logprob_i[:, cluster_prob_idx] + tail_logprob_i.gather(
																						    1		,target_i[:, None]			).squeeze(1			)
																		else:
																						lowerCAmelCase__					:    Any												=					head_logprob[:, cluster_prob_idx, None] + tail_logprob_i
																						lowerCAmelCase__					:    Union[str, Any]												=					logprob_i

														if labels is not None:
																		if (hasattr(self		,'''keep_order'''			) and self.keep_order) or keep_order:
																						out.index_copy_(0		,__lowerCamelCase		,-logprob_i			)
																		else:
																						out[offset : offset + logprob_i.size(0			)].copy_(-logprob_i			)
																		offset += logprob_i.size(0			)

						return out



		def 		lowerCAmelCase__				(self		,__lowerCamelCase			)					->      List[Any]:

						"""simple docstring"""






						if self.n_clusters == 0:
										lowerCAmelCase__					:    Tuple												=					self._compute_logit(__lowerCamelCase		,self.out_layers[0].weight		,self.out_layers[0].bias		,self.out_projs[0]			)
										return nn.functional.log_softmax(__lowerCamelCase		,dim=-1			)
						else:
										# construct weights and biases
										lowerCAmelCase__						,		lowerCAmelCase__					:    int												=					[], []
										for i in range(len(self.cutoffs			)			):
														if self.div_val == 1:
																		lowerCAmelCase__						,		lowerCAmelCase__					:    Any												=					self.cutoff_ends[i], self.cutoff_ends[i + 1]
																		lowerCAmelCase__					:    Optional[int]												=					self.out_layers[0].weight[l_idx:r_idx]
																		lowerCAmelCase__					:    Tuple												=					self.out_layers[0].bias[l_idx:r_idx]
														else:
																		lowerCAmelCase__					:    Any												=					self.out_layers[i].weight
																		lowerCAmelCase__					:    str												=					self.out_layers[i].bias

														if i == 0:
																		lowerCAmelCase__					:    List[str]												=					torch.cat([weight_i, self.cluster_weight]		,dim=0			)
																		lowerCAmelCase__					:    str												=					torch.cat([bias_i, self.cluster_bias]		,dim=0			)

														weights.append(__lowerCamelCase			)
														biases.append(__lowerCamelCase			)

										lowerCAmelCase__						,		lowerCAmelCase__						,		lowerCAmelCase__					:    List[str]												=					weights[0], biases[0], self.out_projs[0]
										lowerCAmelCase__					:    Optional[Any]												=					self._compute_logit(__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase			)

										lowerCAmelCase__					:    Union[str, Any]												=					hidden.new_empty((head_logit.size(0			), self.n_token)			)
										lowerCAmelCase__					:    Tuple												=					nn.functional.log_softmax(__lowerCamelCase		,dim=1			)

										lowerCAmelCase__					:    Dict												=					[0] + self.cutoffs
										for i in range(len(__lowerCamelCase			) - 1			):
														lowerCAmelCase__						,		lowerCAmelCase__					:    Tuple												=					cutoff_values[i], cutoff_values[i + 1]

														if i == 0:
																		lowerCAmelCase__					:    int												=					head_logprob[:, : self.cutoffs[0]]
														else:
																		lowerCAmelCase__						,		lowerCAmelCase__						,		lowerCAmelCase__					:    Optional[Any]												=					weights[i], biases[i], self.out_projs[i]

																		lowerCAmelCase__					:    List[str]												=					self._compute_logit(__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase			)
																		lowerCAmelCase__					:    Optional[Any]												=					nn.functional.log_softmax(__lowerCamelCase		,dim=1			)

																		lowerCAmelCase__					:    Optional[Any]												=					head_logprob[:, -i] + tail_logprob_i
																		lowerCAmelCase__					:    List[str]												=					logprob_i

										return out






import string

import numpy






def      lowerCAmelCase__		(   lowerCamelCase_		:		int	,lowerCamelCase_		:		int):





				'''simple docstring'''

				return b if a == 0 else greatest_common_divisor(b % a	,lowerCamelCase_)



class 	lowerCamelCase__      :




		'''simple docstring'''



		snake_case_     =string.ascii_uppercase + string.digits
		# This cipher takes alphanumerics into account
		# i.e. a total of 36 characters

		# take x and return x % len(key_string)
		snake_case_     =numpy.vectorize(lambda lowerCamelCase__: x % 36)

		snake_case_     =numpy.vectorize(lowerCamelCase__)



		def __init__(self		,__lowerCamelCase			)					->      None:

						"""simple docstring"""






						lowerCAmelCase__					:    Tuple												=					self.modulus(__lowerCamelCase			)  # mod36 calc's on the encrypt key
						self.check_determinant()  # validate the determinant of the encryption key
						lowerCAmelCase__					:    Optional[int]												=					encrypt_key.shape[0]



		def 		lowerCAmelCase__				(self		,__lowerCamelCase			)					->      int:

						"""simple docstring"""






						return self.key_string.index(__lowerCamelCase			)



		def 		lowerCAmelCase__				(self		,__lowerCamelCase			)					->      str:

						"""simple docstring"""






						return self.key_string[round(__lowerCamelCase			)]



		def 		lowerCAmelCase__				(self			)					->      None:

						"""simple docstring"""






						lowerCAmelCase__					:    Tuple												=					round(numpy.linalg.det(self.encrypt_key			)			)

						if det < 0:
										lowerCAmelCase__					:    str												=					det % len(self.key_string			)

						lowerCAmelCase__					:    Optional[Any]												=					len(self.key_string			)
						if greatest_common_divisor(__lowerCamelCase		,len(self.key_string			)			) != 1:
										lowerCAmelCase__					:    List[str]												=					(
										    f"""determinant modular {req_l} of encryption key({det}) """
										    f"""is not co prime w.r.t {req_l}.\nTry another key."""
										)
										raise ValueError(__lowerCamelCase			)



		def 		lowerCAmelCase__				(self		,__lowerCamelCase			)					->      str:

						"""simple docstring"""






						lowerCAmelCase__					:    Dict												=					[char for char in text.upper() if char in self.key_string]

						lowerCAmelCase__					:    int												=					chars[-1]
						while len(__lowerCamelCase			) % self.break_key != 0:
										chars.append(__lowerCamelCase			)

						return "".join(__lowerCamelCase			)



		def 		lowerCAmelCase__				(self		,__lowerCamelCase			)					->      str:

						"""simple docstring"""






						lowerCAmelCase__					:    List[str]												=					self.process_text(text.upper()			)
						lowerCAmelCase__					:    str												=					''''''

						for i in range(0		,len(__lowerCamelCase			) - self.break_key + 1		,self.break_key			):
										lowerCAmelCase__					:    Any												=					text[i : i + self.break_key]
										lowerCAmelCase__					:    Dict												=					[self.replace_letters(__lowerCamelCase			) for char in batch]
										lowerCAmelCase__					:    int												=					numpy.array([vec]			).T
										lowerCAmelCase__					:    Union[str, Any]												=					self.modulus(self.encrypt_key.dot(__lowerCamelCase			)			).T.tolist()[
										    0
										]
										lowerCAmelCase__					:    Union[str, Any]												=					''''''.join(
										    self.replace_digits(__lowerCamelCase			) for num in batch_encrypted			)
										encrypted += encrypted_batch

						return encrypted



		def 		lowerCAmelCase__				(self			)					->      numpy.ndarray:

						"""simple docstring"""






						lowerCAmelCase__					:    Tuple												=					round(numpy.linalg.det(self.encrypt_key			)			)

						if det < 0:
										lowerCAmelCase__					:    int												=					det % len(self.key_string			)
						lowerCAmelCase__					:    Dict												=					None
						for i in range(len(self.key_string			)			):
										if (det * i) % len(self.key_string			) == 1:
														lowerCAmelCase__					:    Optional[Any]												=					i
														break

						lowerCAmelCase__					:    Optional[Any]												=					(
						    det_inv
						    * numpy.linalg.det(self.encrypt_key			)
						    * numpy.linalg.inv(self.encrypt_key			)
						)

						return self.to_int(self.modulus(__lowerCamelCase			)			)



		def 		lowerCAmelCase__				(self		,__lowerCamelCase			)					->      str:

						"""simple docstring"""






						lowerCAmelCase__					:    str												=					self.make_decrypt_key()
						lowerCAmelCase__					:    List[str]												=					self.process_text(text.upper()			)
						lowerCAmelCase__					:    Optional[Any]												=					''''''

						for i in range(0		,len(__lowerCamelCase			) - self.break_key + 1		,self.break_key			):
										lowerCAmelCase__					:    List[Any]												=					text[i : i + self.break_key]
										lowerCAmelCase__					:    Tuple												=					[self.replace_letters(__lowerCamelCase			) for char in batch]
										lowerCAmelCase__					:    Optional[Any]												=					numpy.array([vec]			).T
										lowerCAmelCase__					:    Tuple												=					self.modulus(decrypt_key.dot(__lowerCamelCase			)			).T.tolist()[0]
										lowerCAmelCase__					:    Optional[int]												=					''''''.join(
										    self.replace_digits(__lowerCamelCase			) for num in batch_decrypted			)
										decrypted += decrypted_batch

						return decrypted






def      lowerCAmelCase__		(   ):





				'''simple docstring'''

				lowerCAmelCase__					:    Any												=					int(input('''Enter the order of the encryption key: '''))
				lowerCAmelCase__					:    Union[str, Any]												=					[]

				print('''Enter each row of the encryption key with space separated integers''')
				for _ in range(lowerCamelCase_):
								lowerCAmelCase__					:    int												=					[int(lowerCamelCase_) for x in input().split()]
								hill_matrix.append(lowerCamelCase_)

				lowerCAmelCase__					:    List[str]												=					HillCipher(numpy.array(lowerCamelCase_))

				print('''Would you like to encrypt or decrypt some text? (1 or 2)''')
				lowerCAmelCase__					:    List[Any]												=					input('''\n1. Encrypt\n2. Decrypt\n''')
				if option == "1":
								lowerCAmelCase__					:    Optional[int]												=					input('''What text would you like to encrypt?: ''')
								print('''Your encrypted text is:''')
								print(hc.encrypt(lowerCamelCase_))
				elif option == "2":
								lowerCAmelCase__					:    Dict												=					input('''What text would you like to decrypt?: ''')
								print('''Your decrypted text is:''')
								print(hc.decrypt(lowerCamelCase_))


if __name__ == "__main__":
							import doctest

							doctest.testmod()

							main()

