


from functools import partial
from typing import Any, Callable, Dict, List, Type, TypeVar, Union, overload

import torch
import torch.nn as nn
import torch.types

def       _a	(					SCREAMING_SNAKE_CASE						:					torch.Tensor     ,			SCREAMING_SNAKE_CASE						:					torch.Tensor     ,			SCREAMING_SNAKE_CASE						:					bool  ):







 """simple docstring"""



 if not inplace:
  UpperCamelCase__       :     str 							= ma + ma
 else:
  ma += ma

 return ma

def       _a	(					SCREAMING_SNAKE_CASE						:					torch.Tensor     ,			SCREAMING_SNAKE_CASE						:					List[int]  ):







 """simple docstring"""



 UpperCamelCase__       :     List[Any] 							= -1 * len(SCREAMING_SNAKE_CASE  )
 UpperCamelCase__       :     Optional[int] 							= list(range(len(tensor.shape[:zero_index]  )  )  )
 return tensor.permute(first_inds + [zero_index + i for i in inds]  )

def       _a	(					SCREAMING_SNAKE_CASE						:					torch.Tensor     ,			SCREAMING_SNAKE_CASE						:					int  ):







 """simple docstring"""



 return t.reshape(t.shape[:-no_dims] + (-1,)  )

def       _a	(					SCREAMING_SNAKE_CASE						:					torch.Tensor     ,			SCREAMING_SNAKE_CASE						:					torch.Tensor     ,			SCREAMING_SNAKE_CASE						:					int     ,			SCREAMING_SNAKE_CASE						:					float = 1E-4  ):







 """simple docstring"""



 UpperCamelCase__       :     Any 							= mask.expand(*value.shape  )
 return torch.sum(mask * value     ,			dim=SCREAMING_SNAKE_CASE  ) / (eps + torch.sum(SCREAMING_SNAKE_CASE     ,			dim=SCREAMING_SNAKE_CASE  ))

def       _a	(					SCREAMING_SNAKE_CASE						:					torch.Tensor     ,			SCREAMING_SNAKE_CASE						:					torch.types.Number = 2.3125     ,			SCREAMING_SNAKE_CASE						:					torch.types.Number = 21.6875     ,			SCREAMING_SNAKE_CASE						:					int = 64  ):







 """simple docstring"""



 UpperCamelCase__       :     Optional[Any] 							= torch.linspace(SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE     ,			no_bins - 1     ,			device=pts.device  )
 UpperCamelCase__       :     Union[str, Any] 							= torch.sqrt(torch.sum((pts.unsqueeze(-2  ) - pts.unsqueeze(-3  )) ** 2     ,			dim=-1  )  )
 return torch.bucketize(SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE  )

def       _a	(					SCREAMING_SNAKE_CASE						:					Callable[[list], Any]     ,			SCREAMING_SNAKE_CASE						:					List[dict]  ):







 """simple docstring"""



 UpperCamelCase__       :     Dict 							= dicts[0]
 UpperCamelCase__       :     List[Any] 							= {}
 for k, v in first.items():
  UpperCamelCase__       :     Optional[int] 							= [d[k] for d in dicts]
  if isinstance(SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE  ):
   UpperCamelCase__       :     Tuple 							= dict_multimap(SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE  )
  else:
   UpperCamelCase__       :     Union[str, Any] 							= fn(SCREAMING_SNAKE_CASE  )

 return new_dict

def       _a	(					SCREAMING_SNAKE_CASE						:					torch.Tensor     ,			SCREAMING_SNAKE_CASE						:					torch.Tensor  ):







 """simple docstring"""



 UpperCamelCase__       :     int 							= v_bins.view(((1,) * len(x.shape  )) + (len(SCREAMING_SNAKE_CASE  ),)  )
 UpperCamelCase__       :     List[Any] 							= x[..., None] - reshaped_bins
 UpperCamelCase__       :     Optional[int] 							= torch.argmin(torch.abs(SCREAMING_SNAKE_CASE  )     ,			dim=-1  )
 return nn.functional.one_hot(SCREAMING_SNAKE_CASE     ,			num_classes=len(SCREAMING_SNAKE_CASE  )  ).float()

def       _a	(					SCREAMING_SNAKE_CASE						:					torch.Tensor     ,			SCREAMING_SNAKE_CASE						:					torch.Tensor     ,			SCREAMING_SNAKE_CASE						:					int = 0     ,			SCREAMING_SNAKE_CASE						:					int = 0  ):







 """simple docstring"""



 UpperCamelCase__       :     List[Union[slice, torch.Tensor]] 							= []
 for i, s in enumerate(data.shape[:no_batch_dims]  ):
  UpperCamelCase__       :     List[Any] 							= torch.arange(SCREAMING_SNAKE_CASE  )
  UpperCamelCase__       :     Optional[Any] 							= r.view(*(*((1,) * i), -1, *((1,) * (len(inds.shape  ) - i - 1)))  )
  ranges.append(SCREAMING_SNAKE_CASE  )

 UpperCamelCase__       :     List[Union[slice, torch.Tensor]] 							= [slice(SCREAMING_SNAKE_CASE  ) for _ in range(len(data.shape  ) - no_batch_dims  )]
 UpperCamelCase__       :     Union[str, Any] 							= inds
 ranges.extend(SCREAMING_SNAKE_CASE  )
 # Matt note: Editing this to get around the behaviour of using a list as an array index changing
 # in recent Numpy versions
 return data[tuple(SCREAMING_SNAKE_CASE  )]


__UpperCamelCase							:							int    							=					TypeVar("T")

def       _a	(					SCREAMING_SNAKE_CASE						:					Callable[[T], Any]     ,			SCREAMING_SNAKE_CASE						:					Dict[Any, Union[dict, list, tuple, T]]     ,			SCREAMING_SNAKE_CASE						:					Type[T]  ):







 """simple docstring"""



 UpperCamelCase__       :     Dict[Any, Union[dict, list, tuple, Any]] 							= {}
 for k, v in dic.items():
  if isinstance(SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE  ):
   UpperCamelCase__       :     int 							= dict_map(SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE  )
  else:
   UpperCamelCase__       :     Optional[int] 							= tree_map(SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE  )

 return new_dict

@overload
def       _a	(					SCREAMING_SNAKE_CASE						:					Callable[[T], Any]     ,			SCREAMING_SNAKE_CASE						:					T     ,			SCREAMING_SNAKE_CASE						:					Type[T]  ):







 """simple docstring"""



 ...

@overload
def       _a	(					SCREAMING_SNAKE_CASE						:					Callable[[T], Any]     ,			SCREAMING_SNAKE_CASE						:					dict     ,			SCREAMING_SNAKE_CASE						:					Type[T]  ):







 """simple docstring"""



 ...

@overload
def       _a	(					SCREAMING_SNAKE_CASE						:					Callable[[T], Any]     ,			SCREAMING_SNAKE_CASE						:					list     ,			SCREAMING_SNAKE_CASE						:					Type[T]  ):







 """simple docstring"""



 ...

@overload
def       _a	(					SCREAMING_SNAKE_CASE						:					Callable[[T], Any]     ,			SCREAMING_SNAKE_CASE						:					tuple     ,			SCREAMING_SNAKE_CASE						:					Type[T]  ):







 """simple docstring"""



 ...

def       _a	(					SCREAMING_SNAKE_CASE						:					Tuple     ,			SCREAMING_SNAKE_CASE						:					str     ,			SCREAMING_SNAKE_CASE						:					Any  ):







 """simple docstring"""



 if isinstance(SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE  ):
  return dict_map(SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE  )
 elif isinstance(SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE  ):
  return [tree_map(SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE  ) for x in tree]
 elif isinstance(SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE  ):
  return tuple(tree_map(SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE  ) for x in tree  )
 elif isinstance(SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE  ):
  return fn(SCREAMING_SNAKE_CASE  )
 else:
  print(type(SCREAMING_SNAKE_CASE  )  )
  raise ValueError('''Not supported'''  )






__UpperCamelCase							:							Optional[int]    							=					partial(tree_map, leaf_type=torch.Tensor)






import numpy as np

def       _a	(					SCREAMING_SNAKE_CASE						:					Tuple  ):







 """simple docstring"""



 UpperCamelCase__       :     str 							= np.exp(SCREAMING_SNAKE_CASE  )

 # Add up the all the exponentials
 UpperCamelCase__       :     Optional[int] 							= np.sum(SCREAMING_SNAKE_CASE  )

 # Divide every exponent by the sum of all exponents
 UpperCamelCase__       :     Optional[Any] 							= exponent_vector / sum_of_exponents

 return softmax_vector


if __name__ == "__main__":
       print(softmax((0,)))


