



from __future__ import absolute_import, division, print_function, unicode_literals

from torch import nn
from torch.nn import CrossEntropyLoss, MSELoss

from transformers import RobertaConfig
from transformers.file_utils import add_start_docstrings, add_start_docstrings_to_model_forward
from transformers.models.roberta.modeling_roberta import (
    ROBERTA_INPUTS_DOCSTRING,
    ROBERTA_START_DOCSTRING,
    RobertaEmbeddings,
)

from .modeling_highway_bert import BertPreTrainedModel, DeeBertModel, HighwayException, entropy





@add_start_docstrings(
    '''The RoBERTa Model transformer with early exiting (DeeRoBERTa). '''       ,						lowerCamelCase__       ,						)
class 					__lowerCAmelCase (     lowerCamelCase__       ):
				__lowerCamelCase =				RobertaConfig
				__lowerCamelCase =				'''roberta'''




				def __init__(							self						,				_snake_case		):



									"""simple docstring"""





									super().__init__(_snake_case		)

									_lowerCAmelCase						=    RobertaEmbeddings(_snake_case		)
									self.init_weights()





@add_start_docstrings(
    '''RoBERTa Model (with early exiting - DeeRoBERTa) with a classifier on top,
    also takes care of multi-layer training. '''       ,						lowerCamelCase__       ,						)
class 					__lowerCAmelCase (     lowerCamelCase__       ):
				__lowerCamelCase =				RobertaConfig
				__lowerCamelCase =				'''roberta'''




				def __init__(							self						,				_snake_case		):



									"""simple docstring"""





									super().__init__(_snake_case		)
									_lowerCAmelCase						=    config.num_labels
									_lowerCAmelCase						=    config.num_hidden_layers

									_lowerCAmelCase						=    DeeRobertaModel(_snake_case		)
									_lowerCAmelCase						=    nn.Dropout(config.hidden_dropout_prob		)
									_lowerCAmelCase						=    nn.Linear(config.hidden_size						,				self.config.num_labels		)




				@add_start_docstrings_to_model_forward(_snake_case		)
				def 							snake_case   (							self						,				_snake_case=None						,				_snake_case=None						,				_snake_case=None						,				_snake_case=None						,				_snake_case=None						,				_snake_case=None						,				_snake_case=None						,				_snake_case=-1						,				_snake_case=False						,				):



									"""simple docstring"""





									_lowerCAmelCase						=    self.num_layers
									try:
														_lowerCAmelCase						=    self.roberta(
														    _snake_case						,				attention_mask=_snake_case						,				token_type_ids=_snake_case						,				position_ids=_snake_case						,				head_mask=_snake_case						,				inputs_embeds=_snake_case						,				)

														_lowerCAmelCase						=    outputs[1]

														_lowerCAmelCase						=    self.dropout(_snake_case		)
														_lowerCAmelCase						=    self.classifier(_snake_case		)
														_lowerCAmelCase						=    (logits,) + outputs[2:]  # add hidden states and attention if they are here
									except HighwayException as e:
														_lowerCAmelCase						=    e.message
														_lowerCAmelCase						=    e.exit_layer
														_lowerCAmelCase						=    outputs[0]

									if not self.training:
														_lowerCAmelCase						=    entropy(_snake_case		)
														_lowerCAmelCase						=    []
														_lowerCAmelCase						=    []
									if labels is not None:
														if self.num_labels == 1:
																			#  We are doing regression
																			_lowerCAmelCase						=    MSELoss()
																			_lowerCAmelCase						=    loss_fct(logits.view(-1		)						,				labels.view(-1		)		)
														else:
																			_lowerCAmelCase						=    CrossEntropyLoss()
																			_lowerCAmelCase						=    loss_fct(logits.view(-1						,				self.num_labels		)						,				labels.view(-1		)		)

														# work with highway exits
														_lowerCAmelCase						=    []
														for highway_exit in outputs[-1]:
																			_lowerCAmelCase						=    highway_exit[0]
																			if not self.training:
																								highway_logits_all.append(_snake_case		)
																								highway_entropy.append(highway_exit[2]		)
																			if self.num_labels == 1:
																								#  We are doing regression
																								_lowerCAmelCase						=    MSELoss()
																								_lowerCAmelCase						=    loss_fct(highway_logits.view(-1		)						,				labels.view(-1		)		)
																			else:
																								_lowerCAmelCase						=    CrossEntropyLoss()
																								_lowerCAmelCase						=    loss_fct(highway_logits.view(-1						,				self.num_labels		)						,				labels.view(-1		)		)
																			highway_losses.append(_snake_case		)

														if train_highway:
																			_lowerCAmelCase						=    (sum(highway_losses[:-1]		),) + outputs
																			# exclude the final highway, of course
														else:
																			_lowerCAmelCase						=    (loss,) + outputs
									if not self.training:
														_lowerCAmelCase						=    outputs + ((original_entropy, highway_entropy), exit_layer)
														if output_layer >= 0:
																			_lowerCAmelCase						=    (
																			    (outputs[0],) + (highway_logits_all[output_layer],) + outputs[2:]
																			)  # use the highway of the last layer

									return outputs  # (loss), logits, (hidden_states), (attentions), entropy








# Copyright 2023 The HuggingFace Inc. 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.
from ..models.auto import AutoModelForSeqaSeqLM, AutoTokenizer
from .base import PipelineTool




class 					snake_case__						(       lowerCAmelCase_     ):






     """simple docstring"""


     _SCREAMING_SNAKE_CASE						=	"""philschmid/bart-large-cnn-samsum"""
     _SCREAMING_SNAKE_CASE						=	(
         """This is a tool that summarizes an English text. It takes an input `text` containing the text to summarize, """
         """and returns a summary of the text."""
     )
     _SCREAMING_SNAKE_CASE						=	"""summarizer"""
     _SCREAMING_SNAKE_CASE						=	AutoTokenizer
     _SCREAMING_SNAKE_CASE						=	AutoModelForSeqaSeqLM

     _SCREAMING_SNAKE_CASE						=	["""text"""]
     _SCREAMING_SNAKE_CASE						=	["""text"""]






     def    lowercase_   (       self  :  Optional[Any], _snake_case  :  str					)   ->Any:
            return self.pre_processor(_snake_case, return_tensors='pt', truncation=_snake_case					)






     def    lowercase_   (       self  :  int, _snake_case  :  List[Any]					)   ->Any:
            return self.model.generate(**_snake_case					)[0]






     def    lowercase_   (       self  :  int, _snake_case  :  int					)   ->str:
            return self.pre_processor.decode(_snake_case, skip_special_tokens=_snake_case, clean_up_tokenization_spaces=_snake_case					)






