


"""simple docstring"""


from math import ceil


def _lowerCamelCase      (						_UpperCamelCase       ,					_UpperCamelCase ):

			'''simple docstring'''




			__lowerCAmelCase							       =     list(range(0       ,					_UpperCamelCase ) )

			__lowerCAmelCase							       =     [item for sublist in list(device_map.values() ) for item in sublist]

			# Duplicate check
			__lowerCAmelCase							       =     []
			for i in device_map_blocks:
						if device_map_blocks.count(_UpperCamelCase ) > 1 and i not in duplicate_blocks:
									duplicate_blocks.append(_UpperCamelCase )
    # Missing blocks
			__lowerCAmelCase							       =     [i for i in blocks if i not in device_map_blocks]
			__lowerCAmelCase							       =     [i for i in device_map_blocks if i not in blocks]

			if len(_UpperCamelCase ) != 0:
						raise ValueError(
						    "Duplicate attention blocks specified in device_map. Attention blocks must be specified to one device."
						    " These attention blocks were specified more than once: " + str(_UpperCamelCase ) )
			if len(_UpperCamelCase ) != 0:
						raise ValueError(
						    "There are attention blocks for this model that are not specified in the device_map. Add these attention "
						    "blocks to a device on the device_map: " + str(_UpperCamelCase ) )
			if len(_UpperCamelCase ) != 0:
						raise ValueError(
						    "The device_map contains more attention blocks than this model has. Remove these from the device_map:"
						    + str(_UpperCamelCase ) )


def _lowerCamelCase      (						_UpperCamelCase       ,					_UpperCamelCase ):

			'''simple docstring'''




			__lowerCAmelCase							       =     list(range(_UpperCamelCase ) )
			__lowerCAmelCase							       =     int(ceil(n_layers / len(_UpperCamelCase ) ) )
			__lowerCAmelCase							       =     [layers[i : i + n_blocks] for i in range(0       ,					_UpperCamelCase       ,					_UpperCamelCase )]

			return dict(zip(_UpperCamelCase       ,					_UpperCamelCase ) )








"""simple docstring"""


import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch


if is_torch_available():
	import torch

	from transformers.activations import gelu_new, gelu_python, get_activation
@require_torch
class     _UpperCamelCase    (  unittest.TestCase						):
							'''simple docstring'''







							def      snake_case							(	self      ):
										__lowerCAmelCase							       =     torch.tensor([-1_00, -1, -0.1, 0, 0.1, 1.0, 1_00]      )
										__lowerCAmelCase							       =     get_activation("gelu"      )
										self.assertTrue(torch.allclose(gelu_python(__a      )       ,  torch_builtin(__a      )      )      )
										self.assertFalse(torch.allclose(gelu_python(__a      )       ,  gelu_new(__a      )      )      )

							def      snake_case							(	self      ):
										__lowerCAmelCase							       =     torch.tensor([-1_00, -1, -0.1, 0, 0.1, 1.0, 1_00]      )
										__lowerCAmelCase							       =     get_activation("gelu"      )
										__lowerCAmelCase							       =     get_activation("gelu_10"      )

										__lowerCAmelCase							       =     torch_builtin(__a      )
										__lowerCAmelCase							       =     geluaa(__a      )

										__lowerCAmelCase							       =     torch.where(y_gelu_aa < 1_0.0       ,  1       ,  0      )

										self.assertTrue(torch.max(__a      ).item() == 1_0.0      )
										self.assertTrue(torch.allclose(y_gelu * clipped_mask       ,  y_gelu_aa * clipped_mask      )      )

							def      snake_case							(	self      ):
										get_activation("gelu"      )
										get_activation("gelu_10"      )
										get_activation("gelu_fast"      )
										get_activation("gelu_new"      )
										get_activation("gelu_python"      )
										get_activation("gelu_pytorch_tanh"      )
										get_activation("linear"      )
										get_activation("mish"      )
										get_activation("quick_gelu"      )
										get_activation("relu"      )
										get_activation("sigmoid"      )
										get_activation("silu"      )
										get_activation("swish"      )
										get_activation("tanh"      )
										with self.assertRaises(__a      ):
													get_activation("bogus"      )
										with self.assertRaises(__a      ):
													get_activation(__a      )



							def      snake_case							(	self      ):
										__lowerCAmelCase							       =     get_activation("gelu"      )
										__lowerCAmelCase							       =     1
										__lowerCAmelCase							       =     get_activation("gelu"      )
										self.assertEqual(acta.a       ,  1      )
										with self.assertRaises(__a      ):
													__lowerCAmelCase							       =     acta.a




