


import colorsys
import io

import cva
import matplotlib as mpl
import matplotlib.colors as mplc
import matplotlib.figure as mplfigure
import numpy as np
import torch
from matplotlib.backends.backend_agg import FigureCanvasAgg

from utils import img_tensorize


_A		:					str          =							1_000



class 							a       :





     def __init__(     self       :					int     ,						SCREAMING_SNAKE_CASE_       :					Optional[int]     ,						SCREAMING_SNAKE_CASE_       :					Dict=1.2     ,						SCREAMING_SNAKE_CASE_       :					List[str]="g"     ,						SCREAMING_SNAKE_CASE_       :					List[str]=0.5     ,						SCREAMING_SNAKE_CASE_       :					Dict="-"     ,						SCREAMING_SNAKE_CASE_       :					str="test_out.jpg"     ,						SCREAMING_SNAKE_CASE_       :					Any=True     ,						SCREAMING_SNAKE_CASE_       :					List[Any]=False     ,						SCREAMING_SNAKE_CASE_       :					Optional[int]=None     ,						SCREAMING_SNAKE_CASE_       :					List[str]=None     ,						SCREAMING_SNAKE_CASE_       :					Dict=0.7     ,						):
        if isinstance(SCREAMING_SNAKE_CASE_     ,						torch.Tensor				):
           __lowerCamelCase:					List[Any]	 =						img.numpy().astype("""np.uint8"""				)
        if isinstance(SCREAMING_SNAKE_CASE_     ,						SCREAMING_SNAKE_CASE_				):
           __lowerCamelCase:					Union[str, Any]	 =						img_tensorize(SCREAMING_SNAKE_CASE_				)
        assert isinstance(SCREAMING_SNAKE_CASE_     ,						np.ndarray				)

        __lowerCamelCase ,  __lowerCamelCase:					str	 =						img.shape[1], img.shape[0]
        __lowerCamelCase:					Union[str, Any]	 =						mplfigure.Figure(frameon=SCREAMING_SNAKE_CASE_				)
        __lowerCamelCase:					Any	 =						fig.get_dpi()
        __lowerCamelCase:					List[Any]	 =						(width * scale + 1E-2) / dpi
        __lowerCamelCase:					str	 =						(height * scale + 1E-2) / dpi
        fig.set_size_inches(SCREAMING_SNAKE_CASE_     ,						SCREAMING_SNAKE_CASE_				)
        __lowerCamelCase:					str	 =						fig.add_axes([0.0, 0.0, 1.0, 1.0]				)
        ax.axis("""off"""				)
        ax.set_xlim(0.0     ,						SCREAMING_SNAKE_CASE_				)
        ax.set_ylim(SCREAMING_SNAKE_CASE_				)

        __lowerCamelCase:					str	 =						saveas
        __lowerCamelCase:					List[str]	 =						rgb
        __lowerCamelCase:					int	 =						pynb
        __lowerCamelCase:					List[Any]	 =						img
        __lowerCamelCase:					Optional[int]	 =						edgecolor
        __lowerCamelCase:					Optional[int]	 =						0.5
        __lowerCamelCase:					int	 =						linestyle
        __lowerCamelCase:					List[str]	 =						int(np.sqrt(min(SCREAMING_SNAKE_CASE_     ,						SCREAMING_SNAKE_CASE_				)				) * scale // 3				)
        __lowerCamelCase:					int	 =						width
        __lowerCamelCase:					Dict	 =						height
        __lowerCamelCase:					Union[str, Any]	 =						scale
        __lowerCamelCase:					Union[str, Any]	 =						fig
        __lowerCamelCase:					Optional[int]	 =						ax
        __lowerCamelCase:					Dict	 =						pad
        __lowerCamelCase:					List[Any]	 =						idaobj
        __lowerCamelCase:					str	 =						idaattr
        __lowerCamelCase:					str	 =						FigureCanvasAgg(SCREAMING_SNAKE_CASE_				)





     def 			SCREAMING_SNAKE_CASE__							(     self       :					int     ,						SCREAMING_SNAKE_CASE_       :					Dict     ,						SCREAMING_SNAKE_CASE_       :					Any=None				):
        if color is None:
           __lowerCamelCase:					int	 =						self.edgecolor
        ((__lowerCamelCase) ,  (__lowerCamelCase) ,  (__lowerCamelCase) ,  (__lowerCamelCase)):					Optional[Any]	 =						box
        __lowerCamelCase:					Optional[int]	 =						xa - xa
        __lowerCamelCase:					Optional[Any]	 =						ya - ya
        self.ax.add_patch(
            mpl.patches.Rectangle(
                (xa, ya)     ,						SCREAMING_SNAKE_CASE_     ,						SCREAMING_SNAKE_CASE_     ,						fill=SCREAMING_SNAKE_CASE_     ,						edgecolor=SCREAMING_SNAKE_CASE_     ,						linewidth=self.font_size // 3     ,						alpha=self.alpha     ,						linestyle=self.linestyle     ,						)				)





     def 			SCREAMING_SNAKE_CASE__							(     self       :					Optional[Any]     ,						SCREAMING_SNAKE_CASE_       :					Optional[Any]     ,						SCREAMING_SNAKE_CASE_       :					Optional[Any]=None     ,						SCREAMING_SNAKE_CASE_       :					Tuple=None     ,						SCREAMING_SNAKE_CASE_       :					Optional[Any]=None     ,						SCREAMING_SNAKE_CASE_       :					Optional[Any]=None				):
        if len(boxes.shape				) > 2:
           __lowerCamelCase:					Optional[Any]	 =						boxes[0]
        if len(obj_ids.shape				) > 1:
           __lowerCamelCase:					str	 =						obj_ids[0]
        if len(obj_scores.shape				) > 1:
           __lowerCamelCase:					Tuple	 =						obj_scores[0]
        if len(attr_ids.shape				) > 1:
           __lowerCamelCase:					Optional[Any]	 =						attr_ids[0]
        if len(attr_scores.shape				) > 1:
           __lowerCamelCase:					Optional[int]	 =						attr_scores[0]
        if isinstance(SCREAMING_SNAKE_CASE_     ,						torch.Tensor				):
           __lowerCamelCase:					str	 =						boxes.numpy()
        if isinstance(SCREAMING_SNAKE_CASE_     ,						SCREAMING_SNAKE_CASE_				):
           __lowerCamelCase:					List[str]	 =						np.array(SCREAMING_SNAKE_CASE_				)
        assert isinstance(SCREAMING_SNAKE_CASE_     ,						np.ndarray				)
        __lowerCamelCase:					List[str]	 =						np.prod(boxes[:, 2:] - boxes[:, :2]     ,						axis=1				)
        __lowerCamelCase:					str	 =						np.argsort(-areas				).tolist()
        __lowerCamelCase:					Dict	 =						boxes[sorted_idxs] if boxes is not None else None
        __lowerCamelCase:					Tuple	 =						obj_ids[sorted_idxs] if obj_ids is not None else None
        __lowerCamelCase:					int	 =						obj_scores[sorted_idxs] if obj_scores is not None else None
        __lowerCamelCase:					List[Any]	 =						attr_ids[sorted_idxs] if attr_ids is not None else None
        __lowerCamelCase:					str	 =						attr_scores[sorted_idxs] if attr_scores is not None else None

        __lowerCamelCase:					Any	 =						[self._random_color(maximum=1				) for _ in range(len(SCREAMING_SNAKE_CASE_				)				)]
        __lowerCamelCase:					Union[str, Any]	 =						[assigned_colors[idx] for idx in sorted_idxs]
        if obj_ids is not None:
           __lowerCamelCase:					List[Any]	 =						self._create_text_labels_attr(SCREAMING_SNAKE_CASE_     ,						SCREAMING_SNAKE_CASE_     ,						SCREAMING_SNAKE_CASE_     ,						SCREAMING_SNAKE_CASE_				)
           for i in range(len(SCREAMING_SNAKE_CASE_				)				):
              __lowerCamelCase:					List[str]	 =						assigned_colors[i]
              self.add_box(boxes[i]     ,						SCREAMING_SNAKE_CASE_				)
              self.draw_labels(labels[i]     ,						boxes[i]     ,						SCREAMING_SNAKE_CASE_				)





     def 			SCREAMING_SNAKE_CASE__							(     self       :					str     ,						SCREAMING_SNAKE_CASE_       :					Union[str, Any]     ,						SCREAMING_SNAKE_CASE_       :					Dict     ,						SCREAMING_SNAKE_CASE_       :					Optional[Any]				):
        __lowerCamelCase ,  __lowerCamelCase ,  __lowerCamelCase ,  __lowerCamelCase:					Optional[Any]	 =						box
        __lowerCamelCase:					Dict	 =						(xa, ya)
        __lowerCamelCase:					Union[str, Any]	 =						(ya - ya) * (xa - xa)
        __lowerCamelCase:					Any	 =						_SMALL_OBJ * self.scale
        if instance_area < small or ya - ya < 40 * self.scale:
           if ya >= self.height - 5:
              __lowerCamelCase:					Tuple	 =						(xa, ya)
           else:
              __lowerCamelCase:					int	 =						(xa, ya)

        __lowerCamelCase:					int	 =						(ya - ya) / np.sqrt(self.height * self.width				)
        __lowerCamelCase:					Any	 =						self._change_color_brightness(SCREAMING_SNAKE_CASE_     ,						brightness_factor=0.7				)
        __lowerCamelCase:					List[Any]	 =						np.clip((height_ratio - 0.02) / 0.08 + 1     ,						1.2     ,						2				)
        font_size *= 0.75 * self.font_size

        self.draw_text(
            text=SCREAMING_SNAKE_CASE_     ,						position=SCREAMING_SNAKE_CASE_     ,						color=SCREAMING_SNAKE_CASE_     ,						)





     def 			SCREAMING_SNAKE_CASE__							(     self       :					str     ,						SCREAMING_SNAKE_CASE_       :					Optional[Any]     ,						SCREAMING_SNAKE_CASE_       :					Dict     ,						SCREAMING_SNAKE_CASE_       :					List[str]="g"     ,						SCREAMING_SNAKE_CASE_       :					List[str]="left"     ,						):
        __lowerCamelCase:					Optional[int]	 =						0
        __lowerCamelCase:					Optional[Any]	 =						self.font_size
        __lowerCamelCase:					Dict	 =						np.maximum(list(mplc.to_rgb(SCREAMING_SNAKE_CASE_				)				)     ,						0.2				)
        __lowerCamelCase:					Union[str, Any]	 =						max(0.8     ,						np.max(SCREAMING_SNAKE_CASE_				)				)
        __lowerCamelCase:					Optional[Any]	 =						{
            """facecolor""": """black""",
            """alpha""": self.alpha,
            """pad""": self.pad,
            """edgecolor""": """none""",
        }
        __lowerCamelCase ,  __lowerCamelCase:					Any	 =						position
        self.ax.text(
            SCREAMING_SNAKE_CASE_     ,						SCREAMING_SNAKE_CASE_     ,						SCREAMING_SNAKE_CASE_     ,						size=font_size * self.scale     ,						family="""sans-serif"""     ,						bbox=SCREAMING_SNAKE_CASE_     ,						verticalalignment="""top"""     ,						horizontalalignment=SCREAMING_SNAKE_CASE_     ,						color=SCREAMING_SNAKE_CASE_     ,						zorder=10     ,						rotation=SCREAMING_SNAKE_CASE_     ,						)





     def 			SCREAMING_SNAKE_CASE__							(     self       :					Union[str, Any]     ,						SCREAMING_SNAKE_CASE_       :					List[Any]=None				):
        if saveas is None:
           __lowerCamelCase:					Optional[int]	 =						self.saveas
        if saveas.lower().endswith(""".jpg"""				) or saveas.lower().endswith(""".png"""				):
           cva.imwrite(
               SCREAMING_SNAKE_CASE_     ,						self._get_buffer()[:, :, ::-1]     ,						)
        else:
           self.fig.savefig(SCREAMING_SNAKE_CASE_				)





     def 			SCREAMING_SNAKE_CASE__							(     self       :					List[str]     ,						SCREAMING_SNAKE_CASE_       :					str     ,						SCREAMING_SNAKE_CASE_       :					str     ,						SCREAMING_SNAKE_CASE_       :					List[Any]     ,						SCREAMING_SNAKE_CASE_       :					List[Any]				):
        __lowerCamelCase:					Optional[Any]	 =						[self.idaobj[i] for i in classes]
        __lowerCamelCase:					Optional[Any]	 =						[self.idaattr[i] for i in attr_classes]
        __lowerCamelCase:					Optional[int]	 =						[
            F'{label} {score:.2f} {attr} {attr_score:.2f}'
            for label, score, attr, attr_score in zip(SCREAMING_SNAKE_CASE_     ,						SCREAMING_SNAKE_CASE_     ,						SCREAMING_SNAKE_CASE_     ,						SCREAMING_SNAKE_CASE_				)
        ]
        return labels





     def 			SCREAMING_SNAKE_CASE__							(     self       :					Tuple     ,						SCREAMING_SNAKE_CASE_       :					Dict     ,						SCREAMING_SNAKE_CASE_       :					Tuple				):
        __lowerCamelCase:					int	 =						[self.idaobj[i] for i in classes]
        if scores is not None:
           if labels is None:
              __lowerCamelCase:					Tuple	 =						["""{:.0f}%""".format(s * 100				) for s in scores]
           else:
              __lowerCamelCase:					int	 =						["""{} {:.0f}%""".format(SCREAMING_SNAKE_CASE_     ,						s * 100				) for li, s in zip(SCREAMING_SNAKE_CASE_     ,						SCREAMING_SNAKE_CASE_				)]
        return labels





     def 			SCREAMING_SNAKE_CASE__							(     self       :					Any     ,						SCREAMING_SNAKE_CASE_       :					List[str]=255				):
        __lowerCamelCase:					Tuple	 =						np.random.randint(0     ,						len(_COLORS				)				)
        __lowerCamelCase:					Tuple	 =						_COLORS[idx] * maximum
        if not self.rgb:
           __lowerCamelCase:					str	 =						ret[::-1]
        return ret





     def 			SCREAMING_SNAKE_CASE__							(     self       :					Optional[int]				):
        if not self.pynb:
           __lowerCamelCase ,  __lowerCamelCase:					Optional[Any]	 =						self.canvas.print_to_buffer()
           if (width, height) != (self.width, self.height):
              __lowerCamelCase:					Optional[int]	 =						cva.resize(self.img     ,						(width, height)				)
           else:
              __lowerCamelCase:					Any	 =						self.img
        else:
           __lowerCamelCase:					int	 =						io.BytesIO()  # works for cairo backend
           self.canvas.print_rgba(SCREAMING_SNAKE_CASE_				)
           __lowerCamelCase ,  __lowerCamelCase:					str	 =						self.width, self.height
           __lowerCamelCase:					Dict	 =						buf.getvalue()
           __lowerCamelCase:					str	 =						self.img

        __lowerCamelCase:					Any	 =						np.frombuffer(SCREAMING_SNAKE_CASE_     ,						dtype="""uint8"""				)
        __lowerCamelCase:					Optional[Any]	 =						buffer.reshape(SCREAMING_SNAKE_CASE_     ,						SCREAMING_SNAKE_CASE_     ,						4				)
        __lowerCamelCase ,  __lowerCamelCase:					Union[str, Any]	 =						np.split(SCREAMING_SNAKE_CASE_     ,						[3]     ,						axis=2				)

        try:
           import numexpr as ne  # fuse them with numexpr

           __lowerCamelCase:					Any	 =						ne.evaluate("""img * (1 - alpha / 255.0) + rgb * (alpha / 255.0)"""				)
        except ImportError:
           __lowerCamelCase:					Optional[int]	 =						alpha.astype("""float32"""				) / 255.0
           __lowerCamelCase:					str	 =						img * (1 - alpha) + rgb * alpha

        return visualized_image.astype("""uint8"""				)







     def 			SCREAMING_SNAKE_CASE__							(     self       :					Optional[Any]     ,						SCREAMING_SNAKE_CASE_       :					Any     ,						SCREAMING_SNAKE_CASE_       :					List[Any]				):
        assert brightness_factor >= -1.0 and brightness_factor <= 1.0
        __lowerCamelCase:					List[Any]	 =						mplc.to_rgb(SCREAMING_SNAKE_CASE_				)
        __lowerCamelCase:					List[Any]	 =						colorsys.rgb_to_hls(*mplc.to_rgb(SCREAMING_SNAKE_CASE_				)				)
        __lowerCamelCase:					str	 =						polygon_color[1] + (brightness_factor * polygon_color[1])
        __lowerCamelCase:					Union[str, Any]	 =						0.0 if modified_lightness < 0.0 else modified_lightness
        __lowerCamelCase:					Any	 =						1.0 if modified_lightness > 1.0 else modified_lightness
        __lowerCamelCase:					Any	 =						colorsys.hls_to_rgb(polygon_color[0]     ,						SCREAMING_SNAKE_CASE_     ,						polygon_color[2]				)
        return modified_color



_A		:					Dict          =							(
    np.array(
        [
            0.0_00,
            0.4_47,
            0.7_41,
            0.8_50,
            0.3_25,
            0.0_98,
            0.9_29,
            0.6_94,
            0.1_25,
            0.4_94,
            0.1_84,
            0.5_56,
            0.4_66,
            0.6_74,
            0.1_88,
            0.3_01,
            0.7_45,
            0.9_33,
            0.6_35,
            0.0_78,
            0.1_84,
            0.3_00,
            0.3_00,
            0.3_00,
            0.6_00,
            0.6_00,
            0.6_00,
            1.0_00,
            0.0_00,
            0.0_00,
            1.0_00,
            0.5_00,
            0.0_00,
            0.7_49,
            0.7_49,
            0.0_00,
            0.0_00,
            1.0_00,
            0.0_00,
            0.0_00,
            0.0_00,
            1.0_00,
            0.6_67,
            0.0_00,
            1.0_00,
            0.3_33,
            0.3_33,
            0.0_00,
            0.3_33,
            0.6_67,
            0.0_00,
            0.3_33,
            1.0_00,
            0.0_00,
            0.6_67,
            0.3_33,
            0.0_00,
            0.6_67,
            0.6_67,
            0.0_00,
            0.6_67,
            1.0_00,
            0.0_00,
            1.0_00,
            0.3_33,
            0.0_00,
            1.0_00,
            0.6_67,
            0.0_00,
            1.0_00,
            1.0_00,
            0.0_00,
            0.0_00,
            0.3_33,
            0.5_00,
            0.0_00,
            0.6_67,
            0.5_00,
            0.0_00,
            1.0_00,
            0.5_00,
            0.3_33,
            0.0_00,
            0.5_00,
            0.3_33,
            0.3_33,
            0.5_00,
            0.3_33,
            0.6_67,
            0.5_00,
            0.3_33,
            1.0_00,
            0.5_00,
            0.6_67,
            0.0_00,
            0.5_00,
            0.6_67,
            0.3_33,
            0.5_00,
            0.6_67,
            0.6_67,
            0.5_00,
            0.6_67,
            1.0_00,
            0.5_00,
            1.0_00,
            0.0_00,
            0.5_00,
            1.0_00,
            0.3_33,
            0.5_00,
            1.0_00,
            0.6_67,
            0.5_00,
            1.0_00,
            1.0_00,
            0.5_00,
            0.0_00,
            0.3_33,
            1.0_00,
            0.0_00,
            0.6_67,
            1.0_00,
            0.0_00,
            1.0_00,
            1.0_00,
            0.3_33,
            0.0_00,
            1.0_00,
            0.3_33,
            0.3_33,
            1.0_00,
            0.3_33,
            0.6_67,
            1.0_00,
            0.3_33,
            1.0_00,
            1.0_00,
            0.6_67,
            0.0_00,
            1.0_00,
            0.6_67,
            0.3_33,
            1.0_00,
            0.6_67,
            0.6_67,
            1.0_00,
            0.6_67,
            1.0_00,
            1.0_00,
            1.0_00,
            0.0_00,
            1.0_00,
            1.0_00,
            0.3_33,
            1.0_00,
            1.0_00,
            0.6_67,
            1.0_00,
            0.3_33,
            0.0_00,
            0.0_00,
            0.5_00,
            0.0_00,
            0.0_00,
            0.6_67,
            0.0_00,
            0.0_00,
            0.8_33,
            0.0_00,
            0.0_00,
            1.0_00,
            0.0_00,
            0.0_00,
            0.0_00,
            0.1_67,
            0.0_00,
            0.0_00,
            0.3_33,
            0.0_00,
            0.0_00,
            0.5_00,
            0.0_00,
            0.0_00,
            0.6_67,
            0.0_00,
            0.0_00,
            0.8_33,
            0.0_00,
            0.0_00,
            1.0_00,
            0.0_00,
            0.0_00,
            0.0_00,
            0.1_67,
            0.0_00,
            0.0_00,
            0.3_33,
            0.0_00,
            0.0_00,
            0.5_00,
            0.0_00,
            0.0_00,
            0.6_67,
            0.0_00,
            0.0_00,
            0.8_33,
            0.0_00,
            0.0_00,
            1.0_00,
            0.0_00,
            0.0_00,
            0.0_00,
            0.1_43,
            0.1_43,
            0.1_43,
            0.8_57,
            0.8_57,
            0.8_57,
            1.0_00,
            1.0_00,
            1.0_00,
        ]
    )
    .astype(np.floataa)
    .reshape(-1, 3)
)






import math
from collections import OrderedDict

import torch
from packaging import version
from torch import Tensor, nn

from .utils import logging


_A		:					Optional[int]          =							logging.get_logger(__name__)



class 							a       (      nn.Module ):





     def __init__(     self       :					Dict				):
        super().__init__()
        if version.parse(torch.__version__				) < version.parse("""1.12.0"""				):
           raise ImportError(
               F'You are using torch=={torch.__version__}, but torch>=1.12.0 is required to use '
               """PytorchGELUTanh. Please upgrade torch."""				)







     def 			SCREAMING_SNAKE_CASE__							(     self       :					int     ,						SCREAMING_SNAKE_CASE_       :					Tensor				):
        return nn.functional.gelu(SCREAMING_SNAKE_CASE_     ,						approximate="""tanh"""				)



class 							a       (      nn.Module ):





     def 			SCREAMING_SNAKE_CASE__							(     self       :					Any     ,						SCREAMING_SNAKE_CASE_       :					Tensor				):
        return 0.5 * input * (1.0 + torch.tanh(math.sqrt(2.0 / math.pi				) * (input + 0.04_4715 * torch.pow(SCREAMING_SNAKE_CASE_     ,						3.0				))				))



class 							a       (      nn.Module ):





     def __init__(     self       :					List[Any]     ,						SCREAMING_SNAKE_CASE_       :					bool = False				):
        super().__init__()
        if use_gelu_python:
           __lowerCamelCase:					Union[str, Any]	 =						self._gelu_python
        else:
           __lowerCamelCase:					Tuple	 =						nn.functional.gelu





     def 			SCREAMING_SNAKE_CASE__							(     self       :					str     ,						SCREAMING_SNAKE_CASE_       :					Tensor				):
        return input * 0.5 * (1.0 + torch.erf(input / math.sqrt(2.0				)				))







     def 			SCREAMING_SNAKE_CASE__							(     self       :					Union[str, Any]     ,						SCREAMING_SNAKE_CASE_       :					Tensor				):
        return self.act(SCREAMING_SNAKE_CASE_				)



class 							a       (      nn.Module ):





     def 			SCREAMING_SNAKE_CASE__							(     self       :					List[str]     ,						SCREAMING_SNAKE_CASE_       :					Tensor				):
        return 0.5 * input * (1.0 + torch.tanh(input * 0.79_7884_5608 * (1.0 + 0.04_4715 * input * input)				))



class 							a       (      nn.Module ):





     def 			SCREAMING_SNAKE_CASE__							(     self       :					List[Any]     ,						SCREAMING_SNAKE_CASE_       :					Tensor				):
        return input * torch.sigmoid(1.702 * input				)



class 							a       (      nn.Module ):





     def __init__(     self       :					List[str]     ,						SCREAMING_SNAKE_CASE_       :					float     ,						SCREAMING_SNAKE_CASE_       :					float				):
        if min > max:
           raise ValueError(F'min should be < max (got min: {min}, max: {max})'				)

        super().__init__()
        __lowerCamelCase:					Optional[int]	 =						min
        __lowerCamelCase:					str	 =						max







     def 			SCREAMING_SNAKE_CASE__							(     self       :					List[str]     ,						SCREAMING_SNAKE_CASE_       :					Tensor				):
        return torch.clip(gelu(SCREAMING_SNAKE_CASE_				)     ,						self.min     ,						self.max				)



class 							a       (      nn.Module ):





     def __init__(     self       :					int				):
        super().__init__()
        __lowerCamelCase:					List[str]	 =						math.sqrt(2 / math.pi				)







     def 			SCREAMING_SNAKE_CASE__							(     self       :					Tuple     ,						SCREAMING_SNAKE_CASE_       :					Tensor				):
        return 0.5 * input * (1 + torch.tanh(self.precomputed_constant * (input + 0.04_4715 * torch.pow(SCREAMING_SNAKE_CASE_     ,						3				))				))



class 							a       (      nn.Module ):





     def 			SCREAMING_SNAKE_CASE__							(     self       :					Any     ,						SCREAMING_SNAKE_CASE_       :					Tensor				):
        return nn.functional.silu(SCREAMING_SNAKE_CASE_				)



class 							a       (      nn.Module ):





     def __init__(     self       :					Any				):
        super().__init__()
        if version.parse(torch.__version__				) < version.parse("""1.9.0"""				):
           __lowerCamelCase:					str	 =						self._mish_python
        else:
           __lowerCamelCase:					Any	 =						nn.functional.mish





     def 			SCREAMING_SNAKE_CASE__							(     self       :					int     ,						SCREAMING_SNAKE_CASE_       :					Tensor				):
        return input * torch.tanh(nn.functional.softplus(SCREAMING_SNAKE_CASE_				)				)







     def 			SCREAMING_SNAKE_CASE__							(     self       :					List[str]     ,						SCREAMING_SNAKE_CASE_       :					Tensor				):
        return self.act(SCREAMING_SNAKE_CASE_				)



class 							a       (      nn.Module ):





     def 			SCREAMING_SNAKE_CASE__							(     self       :					List[str]     ,						SCREAMING_SNAKE_CASE_       :					Tensor				):
        return input



class 							a       (      nn.Module ):





     def 			SCREAMING_SNAKE_CASE__							(     self       :					Optional[int]     ,						SCREAMING_SNAKE_CASE_       :					str     ,						SCREAMING_SNAKE_CASE_       :					Optional[int]=0.70_7107     ,						SCREAMING_SNAKE_CASE_       :					Tuple=0.28_2095				):
        __lowerCamelCase:					Optional[Any]	 =						(input - mu).div(sigma * math.sqrt(2.0				)				)
        return 0.5 * (1.0 + torch.erf(SCREAMING_SNAKE_CASE_				))



class 							a       (      nn.Module ):





     def 			SCREAMING_SNAKE_CASE__							(     self       :					int     ,						SCREAMING_SNAKE_CASE_       :					str				):
        __lowerCamelCase:					Any	 =						nn.functional.relu(SCREAMING_SNAKE_CASE_				)
        __lowerCamelCase:					List[str]	 =						torch.square(SCREAMING_SNAKE_CASE_				)
        return squared



class 							a       (      _UpperCAmelCase ):





     def __getitem__(     self       :					str     ,						SCREAMING_SNAKE_CASE_       :					Tuple				):
        __lowerCamelCase:					Optional[Any]	 =						super().__getitem__(SCREAMING_SNAKE_CASE_				)
        __lowerCamelCase ,  __lowerCamelCase:					Optional[Any]	 =						content if isinstance(SCREAMING_SNAKE_CASE_     ,						SCREAMING_SNAKE_CASE_				) else (content, {})
        return cls(**SCREAMING_SNAKE_CASE_				)



_A		:					Optional[Any]          =							{
    '''gelu''': GELUActivation,
    '''gelu_10''': (ClippedGELUActivation, {'''min''': -10, '''max''': 10}),
    '''gelu_fast''': FastGELUActivation,
    '''gelu_new''': NewGELUActivation,
    '''gelu_python''': (GELUActivation, {'''use_gelu_python''': True}),
    '''gelu_pytorch_tanh''': PytorchGELUTanh,
    '''gelu_accurate''': AccurateGELUActivation,
    '''laplace''': LaplaceActivation,
    '''linear''': LinearActivation,
    '''mish''': MishActivation,
    '''quick_gelu''': QuickGELUActivation,
    '''relu''': nn.ReLU,
    '''relu2''': ReLUSquaredActivation,
    '''relu6''': nn.ReLUa,
    '''sigmoid''': nn.Sigmoid,
    '''silu''': SiLUActivation,
    '''swish''': SiLUActivation,
    '''tanh''': nn.Tanh,
}
_A		:					Optional[int]          =							ClassInstantier(ACTaCLS)



def      __lowerCAmelCase    (				snake_case						:     Any			)							->				int:
   if activation_string in ACTaFN:
      return ACTaFN[activation_string]
   else:
      raise KeyError(f'function {activation_string} not found in ACT2FN mapping {list(ACTaFN.keys()			)}'			)


# For backwards compatibility with: from activations import gelu_python
_A		:					Optional[int]          =							get_activation('''gelu_python''')
_A		:					Dict          =							get_activation('''gelu_new''')
_A		:					List[str]          =							get_activation('''gelu''')
_A		:					Tuple          =							get_activation('''gelu_fast''')
_A		:					int          =							get_activation('''quick_gelu''')
_A		:					Tuple          =							get_activation('''silu''')
_A		:					Optional[Any]          =							get_activation('''mish''')







_A		:					Dict          =							get_activation('''linear''')


