


# Copyright 2021 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse

from ...utils.dataclasses import (
    ComputeEnvironment,
    DistributedType,
    DynamoBackend,
    PrecisionType,
    SageMakerDistributedType,
)
from ..menu import BulletMenu


__UpperCAmelCase : int 			=				[
    "EAGER",
    "AOT_EAGER",
    "INDUCTOR",
    "NVFUSER",
    "AOT_NVFUSER",
    "AOT_CUDAGRAPHS",
    "OFI",
    "FX2TRT",
    "ONNXRT",
    "IPEX",
]


def      a	(							SCREAMING_SNAKE_CASE_      :		Any							,			SCREAMING_SNAKE_CASE_      :		List[str]=None							,			SCREAMING_SNAKE_CASE_      :		Dict=None							,			SCREAMING_SNAKE_CASE_      :		List[Any]=None       ):






       """simple docstring"""





       UpperCamelCase    :	Optional[Any]   	=		True
       while ask_again:
              UpperCamelCase    :	Dict   	=		input(SCREAMING_SNAKE_CASE_       )
              try:
                     if default is not None and len(SCREAMING_SNAKE_CASE_       ) == 0:
                            return default
                     return convert_value(SCREAMING_SNAKE_CASE_       ) if convert_value is not None else result
              except Exception:
                     if error_message is not None:
                            print(SCREAMING_SNAKE_CASE_       )


def      a	(							SCREAMING_SNAKE_CASE_      :		Dict							,			SCREAMING_SNAKE_CASE_      :		List[Any]=[]							,			SCREAMING_SNAKE_CASE_      :		Optional[int]=None							,			SCREAMING_SNAKE_CASE_      :		Optional[int]=0       ):






       """simple docstring"""





       UpperCamelCase    :	int   	=		BulletMenu(SCREAMING_SNAKE_CASE_							,			SCREAMING_SNAKE_CASE_       )
       UpperCamelCase    :	List[Any]   	=		menu.run(default_choice=SCREAMING_SNAKE_CASE_       )
       return convert_value(SCREAMING_SNAKE_CASE_       ) if convert_value is not None else result


def      a	(							SCREAMING_SNAKE_CASE_      :		Optional[Any]       ):






       """simple docstring"""





       UpperCamelCase    :	Optional[Any]   	=		int(SCREAMING_SNAKE_CASE_       )
       return ComputeEnvironment(['''LOCAL_MACHINE''', '''AMAZON_SAGEMAKER'''][value]       )


def      a	(							SCREAMING_SNAKE_CASE_      :		Union[str, Any]       ):






       """simple docstring"""





       UpperCamelCase    :	Optional[int]   	=		int(SCREAMING_SNAKE_CASE_       )
       return DistributedType(['''NO''', '''MULTI_CPU''', '''MULTI_XPU''', '''MULTI_GPU''', '''MULTI_NPU''', '''TPU'''][value]       )


def      a	(							SCREAMING_SNAKE_CASE_      :		Optional[Any]       ):






       """simple docstring"""





       UpperCamelCase    :	Union[str, Any]   	=		int(SCREAMING_SNAKE_CASE_       )
       return DynamoBackend(DYNAMO_BACKENDS[value]       ).value


def      a	(							SCREAMING_SNAKE_CASE_      :		str       ):






       """simple docstring"""





       UpperCamelCase    :	str   	=		int(SCREAMING_SNAKE_CASE_       )
       return PrecisionType(['''no''', '''fp16''', '''bf16''', '''fp8'''][value]       )


def      a	(							SCREAMING_SNAKE_CASE_      :		Union[str, Any]       ):






       """simple docstring"""





       UpperCamelCase    :	Dict   	=		int(SCREAMING_SNAKE_CASE_       )
       return SageMakerDistributedType(['''NO''', '''DATA_PARALLEL''', '''MODEL_PARALLEL'''][value]       )


def      a	(							SCREAMING_SNAKE_CASE_      :		Tuple       ):






       """simple docstring"""





       return {"yes": True, "no": False}[value.lower()]



class    UpperCAmelCase_	(							argparse.RawDescriptionHelpFormatter):
       '''simple docstring'''






       def  _lowercase   (     self ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE						):




              """simple docstring"""




              UpperCamelCase    :	Tuple   	=		super()._format_usage(__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE						)
              UpperCamelCase    :	Dict   	=		usage.replace('''<command> [<args>] ''' ,					''''''						)
              return usage





import torch

from transformers import AutoModel



class    UpperCAmelCase_	(							torch.nn.Module):
       '''simple docstring'''






       def __init__(     self ,					__SCREAMING_SNAKE_CASE="sayef/fsner-bert-base-uncased"						):




              """simple docstring"""




              super(__SCREAMING_SNAKE_CASE ,					self						).__init__()

              UpperCamelCase    :	List[str]   	=		AutoModel.from_pretrained(__SCREAMING_SNAKE_CASE ,					return_dict=__SCREAMING_SNAKE_CASE						)
              UpperCamelCase    :	Optional[int]   	=		torch.nn.CosineSimilarity(3 ,					1e-08						)
              UpperCamelCase    :	List[Any]   	=		torch.nn.Softmax(dim=1						)






       def  _lowercase   (     self ,					**__SCREAMING_SNAKE_CASE						):




              """simple docstring"""




              return self.bert(**__SCREAMING_SNAKE_CASE						).last_hidden_state






       def  _lowercase   (     self ,					__SCREAMING_SNAKE_CASE						):




              """simple docstring"""




              return token_embeddings.sum(2 ,					keepdim=__SCREAMING_SNAKE_CASE						)






       def  _lowercase   (     self ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE=1						):




              """simple docstring"""




              return self.softmax(T * self.cos(__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE						)						)






       def  _lowercase   (     self ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE						):




              """simple docstring"""




              UpperCamelCase    :	Any   	=		W_supports['''sizes'''].tolist()
              UpperCamelCase    :	Optional[int]   	=		W_supports['''start_token_id'''].item()
              UpperCamelCase    :	Any   	=		W_supports['''end_token_id'''].item()

              del W_supports["sizes"]
              del W_supports["start_token_id"]
              del W_supports["end_token_id"]

              UpperCamelCase    :	Union[str, Any]   	=		self.BERT(**__SCREAMING_SNAKE_CASE						)
              UpperCamelCase    :	List[str]   	=		self.BERT(**__SCREAMING_SNAKE_CASE						)

              UpperCamelCase    :	List[Any]   	=		None
              UpperCamelCase    :	Any   	=		None

              UpperCamelCase    :	Optional[Any]   	=		W_supports['''input_ids'''] == start_token_id
              UpperCamelCase    :	Any   	=		W_supports['''input_ids'''] == end_token_id

              for i, size in enumerate(__SCREAMING_SNAKE_CASE						):
                     if i == 0:
                            UpperCamelCase    :	Optional[int]   	=		0
                     else:
                            UpperCamelCase    :	Tuple   	=		support_sizes[i - 1]

                     UpperCamelCase    :	Tuple   	=		S[s : s + size][start_token_masks[s : s + size]]
                     UpperCamelCase    :	List[str]   	=		S[s : s + size][end_token_masks[s : s + size]]

                     UpperCamelCase    :	Dict   	=		torch.matmul(q[i] ,					s_start.T						).sum(1						).softmax(0						)
                     UpperCamelCase    :	Tuple   	=		torch.matmul(q[i] ,					s_end.T						).sum(1						).softmax(0						)

                     if p_starts is not None:
                            UpperCamelCase    :	List[str]   	=		torch.vstack((p_starts, p_start)						)
                            UpperCamelCase    :	Union[str, Any]   	=		torch.vstack((p_ends, p_end)						)
                     else:
                            UpperCamelCase    :	str   	=		p_start
                            UpperCamelCase    :	Optional[int]   	=		p_end

              return p_starts, p_ends

