





import unittest

from transformers import AlbertConfig, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
							import torch

							from transformers import (
							    MODEL_FOR_PRETRAINING_MAPPING,
							    AlbertForMaskedLM,
							    AlbertForMultipleChoice,
							    AlbertForPreTraining,
							    AlbertForQuestionAnswering,
							    AlbertForSequenceClassification,
							    AlbertForTokenClassification,
							    AlbertModel,
							)
							from transformers.models.albert.modeling_albert import ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST
class     UpperCamelCase__					:






		'''simple docstring'''






		def __init__(							self,   snake_case__,   snake_case__=13,   snake_case__=7,   snake_case__=True,   snake_case__=True,   snake_case__=True,   snake_case__=True,   snake_case__=99,   snake_case__=16,   snake_case__=36,   snake_case__=6,   snake_case__=6,   snake_case__=6,   snake_case__=37,   snake_case__="gelu",   snake_case__=0.1,   snake_case__=0.1,   snake_case__=5_12,   snake_case__=16,   snake_case__=2,   snake_case__=0.02,   snake_case__=3,   snake_case__=4,   snake_case__=None,   )       -> int:






									"""simple docstring"""
									lowercase_	:   List[str]  				=     parent
									lowercase_	:   List[Any]  				=     batch_size
									lowercase_	:   Dict  				=     seq_length
									lowercase_	:   List[str]  				=     is_training
									lowercase_	:   Optional[Any]  				=     use_input_mask
									lowercase_	:   str  				=     use_token_type_ids
									lowercase_	:   Optional[int]  				=     use_labels
									lowercase_	:   Union[str, Any]  				=     vocab_size
									lowercase_	:   Optional[int]  				=     embedding_size
									lowercase_	:   Dict  				=     hidden_size
									lowercase_	:   Optional[int]  				=     num_hidden_layers
									lowercase_	:   Dict  				=     num_hidden_groups
									lowercase_	:   int  				=     num_attention_heads
									lowercase_	:   Tuple  				=     intermediate_size
									lowercase_	:   Tuple  				=     hidden_act
									lowercase_	:   Tuple  				=     hidden_dropout_prob
									lowercase_	:   Dict  				=     attention_probs_dropout_prob
									lowercase_	:   Any  				=     max_position_embeddings
									lowercase_	:   List[Any]  				=     type_vocab_size
									lowercase_	:   Tuple  				=     type_sequence_label_size
									lowercase_	:   Dict  				=     initializer_range
									lowercase_	:   List[Any]  				=     num_labels
									lowercase_	:   Optional[Any]  				=     num_choices
									lowercase_	:   Dict  				=     scope






		def 						snake_case__				(							self  )       -> List[str]:






									"""simple docstring"""
									lowercase_	:   str  				=     ids_tensor([self.batch_size, self.seq_length],   self.vocab_size  )

									lowercase_	:   Optional[int]  				=     None
									if self.use_input_mask:
																lowercase_	:   int  				=     random_attention_mask([self.batch_size, self.seq_length]  )

									lowercase_	:   Dict  				=     None
									if self.use_token_type_ids:
																lowercase_	:   List[str]  				=     ids_tensor([self.batch_size, self.seq_length],   self.type_vocab_size  )

									lowercase_	:   List[str]  				=     None
									lowercase_	:   Any  				=     None
									lowercase_	:   List[Any]  				=     None
									if self.use_labels:
																lowercase_	:   Optional[Any]  				=     ids_tensor([self.batch_size],   self.type_sequence_label_size  )
																lowercase_	:   Dict  				=     ids_tensor([self.batch_size, self.seq_length],   self.num_labels  )
																lowercase_	:   Any  				=     ids_tensor([self.batch_size],   self.num_choices  )

									lowercase_	:   Optional[int]  				=     self.get_config()

									return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels






		def 						snake_case__				(							self  )       -> List[Any]:






									"""simple docstring"""
									return AlbertConfig(
									    vocab_size=self.vocab_size,   hidden_size=self.hidden_size,   num_hidden_layers=self.num_hidden_layers,   num_attention_heads=self.num_attention_heads,   intermediate_size=self.intermediate_size,   hidden_act=self.hidden_act,   hidden_dropout_prob=self.hidden_dropout_prob,   attention_probs_dropout_prob=self.attention_probs_dropout_prob,   max_position_embeddings=self.max_position_embeddings,   type_vocab_size=self.type_vocab_size,   initializer_range=self.initializer_range,   num_hidden_groups=self.num_hidden_groups,   )






		def 						snake_case__				(							self,   snake_case__,   snake_case__,   snake_case__,   snake_case__,   snake_case__,   snake_case__,   snake_case__  )       -> Dict:






									"""simple docstring"""
									lowercase_	:   Optional[int]  				=     AlbertModel(config=snake_case__  )
									model.to(snake_case__  )
									model.eval()
									lowercase_	:   Optional[Any]  				=     model(snake_case__,   attention_mask=snake_case__,   token_type_ids=snake_case__  )
									lowercase_	:   List[Any]  				=     model(snake_case__,   token_type_ids=snake_case__  )
									lowercase_	:   Dict  				=     model(snake_case__  )
									self.parent.assertEqual(result.last_hidden_state.shape,   (self.batch_size, self.seq_length, self.hidden_size)  )
									self.parent.assertEqual(result.pooler_output.shape,   (self.batch_size, self.hidden_size)  )






		def 						snake_case__				(							self,   snake_case__,   snake_case__,   snake_case__,   snake_case__,   snake_case__,   snake_case__,   snake_case__  )       -> Optional[Any]:






									"""simple docstring"""
									lowercase_	:   Any  				=     AlbertForPreTraining(config=snake_case__  )
									model.to(snake_case__  )
									model.eval()
									lowercase_	:   List[str]  				=     model(
									    snake_case__,   attention_mask=snake_case__,   token_type_ids=snake_case__,   labels=snake_case__,   sentence_order_label=snake_case__,   )
									self.parent.assertEqual(result.prediction_logits.shape,   (self.batch_size, self.seq_length, self.vocab_size)  )
									self.parent.assertEqual(result.sop_logits.shape,   (self.batch_size, config.num_labels)  )






		def 						snake_case__				(							self,   snake_case__,   snake_case__,   snake_case__,   snake_case__,   snake_case__,   snake_case__,   snake_case__  )       -> Optional[Any]:






									"""simple docstring"""
									lowercase_	:   Optional[int]  				=     AlbertForMaskedLM(config=snake_case__  )
									model.to(snake_case__  )
									model.eval()
									lowercase_	:   Optional[Any]  				=     model(snake_case__,   attention_mask=snake_case__,   token_type_ids=snake_case__,   labels=snake_case__  )
									self.parent.assertEqual(result.logits.shape,   (self.batch_size, self.seq_length, self.vocab_size)  )






		def 						snake_case__				(							self,   snake_case__,   snake_case__,   snake_case__,   snake_case__,   snake_case__,   snake_case__,   snake_case__  )       -> Optional[int]:






									"""simple docstring"""
									lowercase_	:   List[Any]  				=     AlbertForQuestionAnswering(config=snake_case__  )
									model.to(snake_case__  )
									model.eval()
									lowercase_	:   List[str]  				=     model(
									    snake_case__,   attention_mask=snake_case__,   token_type_ids=snake_case__,   start_positions=snake_case__,   end_positions=snake_case__,   )
									self.parent.assertEqual(result.start_logits.shape,   (self.batch_size, self.seq_length)  )
									self.parent.assertEqual(result.end_logits.shape,   (self.batch_size, self.seq_length)  )






		def 						snake_case__				(							self,   snake_case__,   snake_case__,   snake_case__,   snake_case__,   snake_case__,   snake_case__,   snake_case__  )       -> Any:






									"""simple docstring"""
									lowercase_	:   Optional[Any]  				=     self.num_labels
									lowercase_	:   int  				=     AlbertForSequenceClassification(snake_case__  )
									model.to(snake_case__  )
									model.eval()
									lowercase_	:   str  				=     model(snake_case__,   attention_mask=snake_case__,   token_type_ids=snake_case__,   labels=snake_case__  )
									self.parent.assertEqual(result.logits.shape,   (self.batch_size, self.num_labels)  )






		def 						snake_case__				(							self,   snake_case__,   snake_case__,   snake_case__,   snake_case__,   snake_case__,   snake_case__,   snake_case__  )       -> Any:






									"""simple docstring"""
									lowercase_	:   Optional[Any]  				=     self.num_labels
									lowercase_	:   List[Any]  				=     AlbertForTokenClassification(config=snake_case__  )
									model.to(snake_case__  )
									model.eval()
									lowercase_	:   List[Any]  				=     model(snake_case__,   attention_mask=snake_case__,   token_type_ids=snake_case__,   labels=snake_case__  )
									self.parent.assertEqual(result.logits.shape,   (self.batch_size, self.seq_length, self.num_labels)  )






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






									"""simple docstring"""
									lowercase_	:   Dict  				=     self.num_choices
									lowercase_	:   Optional[int]  				=     AlbertForMultipleChoice(config=snake_case__  )
									model.to(snake_case__  )
									model.eval()
									lowercase_	:   str  				=     input_ids.unsqueeze(1  ).expand(-1,   self.num_choices,   -1  ).contiguous()
									lowercase_	:   Any  				=     token_type_ids.unsqueeze(1  ).expand(-1,   self.num_choices,   -1  ).contiguous()
									lowercase_	:   Dict  				=     input_mask.unsqueeze(1  ).expand(-1,   self.num_choices,   -1  ).contiguous()
									lowercase_	:   List[Any]  				=     model(
									    snake_case__,   attention_mask=snake_case__,   token_type_ids=snake_case__,   labels=snake_case__,   )
									self.parent.assertEqual(result.logits.shape,   (self.batch_size, self.num_choices)  )






		def 						snake_case__				(							self  )       -> List[str]:






									"""simple docstring"""
									lowercase_	:   Optional[Any]  				=     self.prepare_config_and_inputs()
									(
									    (
									    lowercase_
									)				,		(
									    lowercase_
									)				,		(
									    lowercase_
									)				,		(
									    lowercase_
									)				,		(
									    lowercase_
									)				,		(
									    lowercase_
									)				,		(
									    lowercase_
									)				,		
									)	:   List[str]  				=     config_and_inputs
									lowercase_	:   Any  				=     {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
									return config, inputs_dict




@require_torch
class     UpperCamelCase__					(     lowerCamelCase__				, lowerCamelCase__				, unittest.TestCase				):






		'''simple docstring'''






		__a       :       Optional[Any]       =	(
		    (
		        AlbertModel,
		        AlbertForPreTraining,
		        AlbertForMaskedLM,
		        AlbertForMultipleChoice,
		        AlbertForSequenceClassification,
		        AlbertForTokenClassification,
		        AlbertForQuestionAnswering,
		    )
		    if is_torch_available()
		    else ()
		)
		__a       :       int       =	(
		    {
		        """feature-extraction""": AlbertModel,
		        """fill-mask""": AlbertForMaskedLM,
		        """question-answering""": AlbertForQuestionAnswering,
		        """text-classification""": AlbertForSequenceClassification,
		        """token-classification""": AlbertForTokenClassification,
		        """zero-shot""": AlbertForSequenceClassification,
		    }
		    if is_torch_available()
		    else {}
		)
		__a       :       Dict       =	True






		def 						snake_case__				(							self,   snake_case__,   snake_case__,   snake_case__=False  )       -> int:






									"""simple docstring"""
									lowercase_	:   str  				=     super()._prepare_for_class(snake_case__,   snake_case__,   return_labels=snake_case__  )

									if return_labels:
																if model_class in get_values(snake_case__  ):
																							lowercase_	:   str  				=     torch.zeros(
																							    (self.model_tester.batch_size, self.model_tester.seq_length),   dtype=torch.long,   device=snake_case__  )
																							lowercase_	:   Tuple  				=     torch.zeros(
																							    self.model_tester.batch_size,   dtype=torch.long,   device=snake_case__  )
									return inputs_dict






		def 						snake_case__				(							self  )       -> str:






									"""simple docstring"""
									lowercase_	:   List[str]  				=     AlbertModelTester(self  )
									lowercase_	:   List[str]  				=     ConfigTester(self,   config_class=snake_case__,   hidden_size=37  )






		def 						snake_case__				(							self  )       -> List[str]:






									"""simple docstring"""
									self.config_tester.run_common_tests()






		def 						snake_case__				(							self  )       -> Dict:






									"""simple docstring"""
									lowercase_	:   Tuple  				=     self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*snake_case__  )






		def 						snake_case__				(							self  )       -> Optional[int]:






									"""simple docstring"""
									lowercase_	:   Optional[int]  				=     self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_pretraining(*snake_case__  )






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






									"""simple docstring"""
									lowercase_	:   Dict  				=     self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_masked_lm(*snake_case__  )






		def 						snake_case__				(							self  )       -> Dict:






									"""simple docstring"""
									lowercase_	:   List[Any]  				=     self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_multiple_choice(*snake_case__  )






		def 						snake_case__				(							self  )       -> Any:






									"""simple docstring"""
									lowercase_	:   List[Any]  				=     self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_question_answering(*snake_case__  )






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






									"""simple docstring"""
									lowercase_	:   List[Any]  				=     self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_sequence_classification(*snake_case__  )






		def 						snake_case__				(							self  )       -> Tuple:






									"""simple docstring"""
									lowercase_	:   Dict  				=     self.model_tester.prepare_config_and_inputs()
									for type in ["absolute", "relative_key", "relative_key_query"]:
																lowercase_	:   Optional[int]  				=     type
																self.model_tester.create_and_check_model(*snake_case__  )






		@slow
		def 						snake_case__				(							self  )       -> Union[str, Any]:






									"""simple docstring"""
									for model_name in ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																lowercase_	:   Dict  				=     AlbertModel.from_pretrained(snake_case__  )
																self.assertIsNotNone(snake_case__  )




@require_torch
class     UpperCamelCase__					(     unittest.TestCase				):






		'''simple docstring'''






		@slow
		def 						snake_case__				(							self  )       -> Any:






									"""simple docstring"""
									lowercase_	:   str  				=     AlbertModel.from_pretrained("""albert-base-v2"""  )
									lowercase_	:   List[str]  				=     torch.tensor([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]]  )
									lowercase_	:   int  				=     torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]  )
									with torch.no_grad():
																lowercase_	:   Any  				=     model(snake_case__,   attention_mask=snake_case__  )[0]
									lowercase_	:   Optional[Any]  				=     torch.Size((1, 11, 7_68)  )
									self.assertEqual(output.shape,   snake_case__  )
									lowercase_	:   Optional[int]  				=     torch.tensor(
									    [[[-0.6513, 1.5035, -0.2766], [-0.6515, 1.5046, -0.2780], [-0.6512, 1.5049, -0.2784]]]  )

									self.assertTrue(torch.allclose(output[:, 1:4, 1:4],   snake_case__,   atol=1E-4  )  )






from argparse import ArgumentParser, Namespace
from typing import Any, List, Optional

from ..pipelines import Pipeline, get_supported_tasks, pipeline
from ..utils import logging
from . import BaseTransformersCLICommand


try:
							from fastapi import Body, FastAPI, HTTPException
							from fastapi.routing import APIRoute
							from pydantic import BaseModel
							from starlette.responses import JSONResponse
							from uvicorn import run

							UpperCAmelCase_							=				True
except (ImportError, AttributeError):
							UpperCAmelCase_							=				object





							def  __magic_name__  (				*lowercase				,       **lowercase				)				-> Optional[Any]:

														"""simple docstring"""



														pass




							UpperCAmelCase_							=				False


UpperCAmelCase_							=				logging.get_logger("""transformers-cli/serving""")





def  __magic_name__  (				lowercase				)				-> int:

							"""simple docstring"""



							lowercase_	:   Any  				=     pipeline(
							    task=args.task				,       model=args.model if args.model else None				,       config=args.config				,       tokenizer=args.tokenizer				,       device=args.device				,       )
							return ServeCommand(lowercase				,       args.host				,       args.port				,       args.workers				)
class     UpperCamelCase__					(     lowerCamelCase__				):






		'''simple docstring'''






		__a       :       dict




class     UpperCamelCase__					(     lowerCamelCase__				):






		'''simple docstring'''






		__a       :       List[str]
		__a       :       Optional[List[int]]




class     UpperCamelCase__					(     lowerCamelCase__				):






		'''simple docstring'''






		__a       :       str




class     UpperCamelCase__					(     lowerCamelCase__				):






		'''simple docstring'''






		__a       :       Any




class     UpperCamelCase__					(     lowerCamelCase__				):






		'''simple docstring'''






		@staticmethod
		def 						snake_case__				(							snake_case__  )       -> Optional[int]:






									"""simple docstring"""
									lowercase_	:   List[str]  				=     parser.add_parser(
									    """serve""",   help="""CLI tool to run inference requests through REST and GraphQL endpoints."""  )
									serve_parser.add_argument(
									    """--task""",   type=snake_case__,   choices=get_supported_tasks(),   help="""The task to run the pipeline on""",   )
									serve_parser.add_argument("""--host""",   type=snake_case__,   default="""localhost""",   help="""Interface the server will listen on."""  )
									serve_parser.add_argument("""--port""",   type=snake_case__,   default=88_88,   help="""Port the serving will listen to."""  )
									serve_parser.add_argument("""--workers""",   type=snake_case__,   default=1,   help="""Number of http workers"""  )
									serve_parser.add_argument("""--model""",   type=snake_case__,   help="""Model's name or path to stored model."""  )
									serve_parser.add_argument("""--config""",   type=snake_case__,   help="""Model's config name or path to stored model."""  )
									serve_parser.add_argument("""--tokenizer""",   type=snake_case__,   help="""Tokenizer name to use."""  )
									serve_parser.add_argument(
									    """--device""",   type=snake_case__,   default=-1,   help="""Indicate the device to run onto, -1 indicates CPU, >= 0 indicates GPU (default: -1)""",   )
									serve_parser.set_defaults(func=snake_case__  )






		def __init__(							self,   snake_case__,   snake_case__,   snake_case__,   snake_case__  )       -> Optional[Any]:






									"""simple docstring"""
									lowercase_	:   str  				=     pipeline

									lowercase_	:   List[str]  				=     host
									lowercase_	:   int  				=     port
									lowercase_	:   Optional[Any]  				=     workers

									if not _serve_dependencies_installed:
																raise RuntimeError(
																    """Using serve command requires FastAPI and uvicorn. """
																    """Please install transformers with [serving]: pip install \"transformers[serving]\"."""
																    """Or install FastAPI and uvicorn separately."""  )
									else:
																logger.info(f"""Serving model over {host}:{port}"""  )
																lowercase_	:   Union[str, Any]  				=     FastAPI(
																    routes=[
																        APIRoute(
																            """/""",   self.model_info,   response_model=snake_case__,   response_class=snake_case__,   methods=["""GET"""],   ),
																        APIRoute(
																            """/tokenize""",   self.tokenize,   response_model=snake_case__,   response_class=snake_case__,   methods=["""POST"""],   ),
																        APIRoute(
																            """/detokenize""",   self.detokenize,   response_model=snake_case__,   response_class=snake_case__,   methods=["""POST"""],   ),
																        APIRoute(
																            """/forward""",   self.forward,   response_model=snake_case__,   response_class=snake_case__,   methods=["""POST"""],   ),
																    ],   timeout=6_00,   )






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






									"""simple docstring"""
									run(self._app,   host=self.host,   port=self.port,   workers=self.workers  )






		def 						snake_case__				(							self  )       -> Dict:






									"""simple docstring"""
									return ServeModelInfoResult(infos=vars(self._pipeline.model.config  )  )






		def 						snake_case__				(							self,   snake_case__ = Body(snake_case__,   embed=snake_case__  ),   snake_case__ = Body(snake_case__,   embed=snake_case__  )  )       -> Optional[int]:






									"""simple docstring"""
									try:
																lowercase_	:   Tuple  				=     self._pipeline.tokenizer.tokenize(snake_case__  )

																if return_ids:
																							lowercase_	:   Union[str, Any]  				=     self._pipeline.tokenizer.convert_tokens_to_ids(snake_case__  )
																							return ServeTokenizeResult(tokens=snake_case__,   tokens_ids=snake_case__  )
																else:
																							return ServeTokenizeResult(tokens=snake_case__  )

									except Exception as e:
																raise HTTPException(status_code=5_00,   detail={"""model""": """""", """error""": str(snake_case__  )}  )






		def 						snake_case__				(							self,   snake_case__ = Body(snake_case__,   embed=snake_case__  ),   snake_case__ = Body(snake_case__,   embed=snake_case__  ),   snake_case__ = Body(snake_case__,   embed=snake_case__  ),   )       -> Dict:






									"""simple docstring"""
									try:
																lowercase_	:   List[Any]  				=     self._pipeline.tokenizer.decode(snake_case__,   snake_case__,   snake_case__  )
																return ServeDeTokenizeResult(model="""""",   text=snake_case__  )
									except Exception as e:
																raise HTTPException(status_code=5_00,   detail={"""model""": """""", """error""": str(snake_case__  )}  )






		async def 						snake_case__				(							self,   snake_case__=Body(snake_case__,   embed=snake_case__  )  )       -> Tuple:






									"""simple docstring"""

									# Check we don't have empty string
									if len(snake_case__  ) == 0:
																return ServeForwardResult(output=[],   attention=[]  )

									try:
																# Forward through the model
																lowercase_	:   Optional[int]  				=     self._pipeline(snake_case__  )
																return ServeForwardResult(output=snake_case__  )
									except Exception as e:
																raise HTTPException(5_00,   {"""error""": str(snake_case__  )}  )