





'''simple docstring'''






import unittest

from transformers import load_tool

from .test_tools_common import ToolTesterMixin


A__       :     List[Any]          ='''
Hugging Face was founded in 2016 by French entrepreneurs Clément Delangue, Julien Chaumond, and Thomas Wolf originally as a company that developed a chatbot app targeted at teenagers.[2] After open-sourcing the model behind the chatbot, the company pivoted to focus on being a platform for machine learning.

In March 2021, Hugging Face raised $40 million in a Series B funding round.[3]

On April 28, 2021, the company launched the BigScience Research Workshop in collaboration with several other research groups to release an open large language model.[4] In 2022, the workshop concluded with the announcement of BLOOM, a multilingual large language model with 176 billion parameters.[5]
'''




class 				UpperCAmelCase  (    unittest.TestCase							,	a_     ):




      def        lowercase__					(    self    :				int						)       ->   Optional[Any]:
            _lowerCAmelCase							  =							load_tool("""summarization"""						)
            self.tool.setup()
            _lowerCAmelCase							  =							load_tool("""summarization"""    ,    remote=__snake_case						)




      def        lowercase__					(    self    :				str						)       ->   int:
            _lowerCAmelCase							  =							self.tool(__snake_case						)
            self.assertEqual(
                __snake_case    ,    """Hugging Face was founded in 2016 by French entrepreneurs Clément Delangue, Julien Chaumond, and Thomas Wolf. In March 2021, Hugging Face raised $40 million in a Series B funding round. On April 28, 2021, the company launched the BigScience Research Workshop in collaboration with several other research groups to release an open large language model. In 2022, the workshop concluded with the announcement of BLOOM."""    ,    )




      def        lowercase__					(    self    :				List[Any]						)       ->   Dict:
            _lowerCAmelCase							  =							self.remote_tool(__snake_case						)
            self.assertEqual(
                __snake_case    ,    """Hugging Face was founded in 2016 by French entrepreneurs Clément Delangue, Julien Chaumond, and Thomas Wolf. In March 2021, Hugging Face raised $40 million in a Series B funding round. On April 28, 2021, the company launched the BigScience Research Workshop in collaboration with several other research groups to release an open large language model. In 2022, the workshop concluded with the announcement of BLOOM."""    ,    )




      def        lowercase__					(    self    :				Tuple						)       ->   List[str]:
            _lowerCAmelCase							  =							self.tool(text=__snake_case						)
            self.assertEqual(
                __snake_case    ,    """Hugging Face was founded in 2016 by French entrepreneurs Clément Delangue, Julien Chaumond, and Thomas Wolf. In March 2021, Hugging Face raised $40 million in a Series B funding round. On April 28, 2021, the company launched the BigScience Research Workshop in collaboration with several other research groups to release an open large language model. In 2022, the workshop concluded with the announcement of BLOOM."""    ,    )




      def        lowercase__					(    self    :				Optional[int]						)       ->   Optional[int]:
            _lowerCAmelCase							  =							self.remote_tool(text=__snake_case						)
            self.assertEqual(
                __snake_case    ,    """Hugging Face was founded in 2016 by French entrepreneurs Clément Delangue, Julien Chaumond, and Thomas Wolf. In March 2021, Hugging Face raised $40 million in a Series B funding round. On April 28, 2021, the company launched the BigScience Research Workshop in collaboration with several other research groups to release an open large language model. In 2022, the workshop concluded with the announcement of BLOOM."""    ,    )








'''simple docstring'''






import math

import torch
from torch import nn
from torch.nn import init

from .binarizer import MagnitudeBinarizer, ThresholdBinarizer, TopKBinarizer




class 				UpperCAmelCase  (    nn.Linear     ):




      def __init__(    self    :				str    ,    __snake_case    :				int    ,    __snake_case    :				int    ,    __snake_case    :				bool = True    ,    __snake_case    :				str = "constant"    ,    __snake_case    :				float = 0.0    ,    __snake_case    :				str = "topK"    ,    )       ->   int:
            super(__snake_case    ,    self						).__init__(in_features=__snake_case    ,    out_features=__snake_case    ,    bias=__snake_case						)
            assert pruning_method in ["topK", "threshold", "sigmoied_threshold", "magnitude", "l0"]
            _lowerCAmelCase							  =							pruning_method

            if self.pruning_method in ["topK", "threshold", "sigmoied_threshold", "l0"]:
                  _lowerCAmelCase							  =							mask_scale
                  _lowerCAmelCase							  =							mask_init
                  _lowerCAmelCase							  =							nn.Parameter(torch.empty(self.weight.size()						)						)
                  self.init_mask()




      def        lowercase__					(    self    :				Any						)       ->   str:
            if self.mask_init == "constant":
                  init.constant_(self.mask_scores    ,    val=self.mask_scale						)
            elif self.mask_init == "uniform":
                  init.uniform_(self.mask_scores    ,    a=-self.mask_scale    ,    b=self.mask_scale						)
            elif self.mask_init == "kaiming":
                  init.kaiming_uniform_(self.mask_scores    ,    a=math.sqrt(5						)						)




      def        lowercase__					(    self    :				Dict    ,    __snake_case    :				torch.tensor    ,    __snake_case    :				float						)       ->   Union[str, Any]:
            # Get the mask
            if self.pruning_method == "topK":
                  _lowerCAmelCase							  =							TopKBinarizer.apply(self.mask_scores    ,    __snake_case						)
            elif self.pruning_method in ["threshold", "sigmoied_threshold"]:
                  _lowerCAmelCase							  =							"""sigmoied""" in self.pruning_method
                  _lowerCAmelCase							  =							ThresholdBinarizer.apply(self.mask_scores    ,    __snake_case    ,    __snake_case						)
            elif self.pruning_method == "magnitude":
                  _lowerCAmelCase							  =							MagnitudeBinarizer.apply(self.weight    ,    __snake_case						)
            elif self.pruning_method == "l0":
                  _lowerCAmelCase  ,							_lowerCAmelCase  ,							_lowerCAmelCase							  =							-0.1, 1.1, 2 / 3
                  if self.training:
                        _lowerCAmelCase							  =							torch.zeros_like(self.mask_scores						).uniform_().clamp(0.00_01    ,    0.99_99						)
                        _lowerCAmelCase							  =							torch.sigmoid((u.log() - (1 - u).log() + self.mask_scores) / b						)
                  else:
                        _lowerCAmelCase							  =							torch.sigmoid(self.mask_scores						)
                  _lowerCAmelCase							  =							s * (r - l) + l
                  _lowerCAmelCase							  =							s_bar.clamp(min=0.0    ,    max=1.0						)
            # Mask weights with computed mask
            _lowerCAmelCase							  =							mask * self.weight
            # Compute output (linear layer) with masked weights
            return nn.functional.linear(__snake_case    ,    __snake_case    ,    self.bias						)






