


'''simple docstring'''







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). '''				,		__a				,		)
class 			_lowercase						(       __a					):
     _UpperCAmelCase										=					RobertaConfig
     _UpperCAmelCase										=					'''roberta'''







     def __init__( self    ,   A__    ) ->	Any:
      super().__init__(A__    )

      snake_case				=			RobertaEmbeddings(A__    )
      self.init_weights()




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







     def __init__( self    ,   A__    ) ->	str:
      super().__init__(A__    )
      snake_case				=			config.num_labels
      snake_case				=			config.num_hidden_layers

      snake_case				=			DeeRobertaModel(A__    )
      snake_case				=			nn.Dropout(config.hidden_dropout_prob    )
      snake_case				=			nn.Linear(config.hidden_size    ,   self.config.num_labels    )







     @add_start_docstrings_to_model_forward(A__    )
     def     UpperCamelCase			( self    ,   A__=None    ,   A__=None    ,   A__=None    ,   A__=None    ,   A__=None    ,   A__=None    ,   A__=None    ,   A__=-1    ,   A__=False    ,   ) ->	Union[str, Any]:
      snake_case				=			self.num_layers
      try:
       snake_case				=			self.roberta(
           A__    ,   attention_mask=A__    ,   token_type_ids=A__    ,   position_ids=A__    ,   head_mask=A__    ,   inputs_embeds=A__    ,   )

       snake_case				=			outputs[1]

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

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

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

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

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








'''simple docstring'''







import inspect
import re
from hashlib import shaaaa
from typing import Dict, List

from .arrow import arrow
from .audiofolder import audiofolder
from .csv import csv
from .imagefolder import imagefolder
from .json import json
from .pandas import pandas
from .parquet import parquet
from .sql import sql  # noqa F401
from .text import text

def    __UpperCamelCase	(	a    : List[str]     ) ->str:
 snake_case				=			[]
 for line in lines:
  snake_case				=			re.sub(R'''#.*'''					,   ''''''					,   a     )  # remove comments
  if line:
   filtered_lines.append(a     )
 snake_case				=			'''\n'''.join(a     )

 # Make a hash from all this code
 snake_case				=			full_str.encode('''utf-8'''     )
 return shaaaa(a     ).hexdigest()


# get importable module names and hash for caching
_lowercase       						=			{
    'csv': (csv.__name__, _hash_python_lines(inspect.getsource(csv).splitlines())),
    'json': (json.__name__, _hash_python_lines(inspect.getsource(json).splitlines())),
    'pandas': (pandas.__name__, _hash_python_lines(inspect.getsource(pandas).splitlines())),
    'parquet': (parquet.__name__, _hash_python_lines(inspect.getsource(parquet).splitlines())),
    'arrow': (arrow.__name__, _hash_python_lines(inspect.getsource(arrow).splitlines())),
    'text': (text.__name__, _hash_python_lines(inspect.getsource(text).splitlines())),
    'imagefolder': (imagefolder.__name__, _hash_python_lines(inspect.getsource(imagefolder).splitlines())),
    'audiofolder': (audiofolder.__name__, _hash_python_lines(inspect.getsource(audiofolder).splitlines())),
}

# Used to infer the module to use based on the data files extensions
_lowercase       						=			{
    '.csv': ('csv', {}),
    '.tsv': ('csv', {'sep': '\t'}),
    '.json': ('json', {}),
    '.jsonl': ('json', {}),
    '.parquet': ('parquet', {}),
    '.arrow': ('arrow', {}),
    '.txt': ('text', {}),
}
_EXTENSION_TO_MODULE.update({ext: ('imagefolder', {}) for ext in imagefolder.ImageFolder.EXTENSIONS})
_EXTENSION_TO_MODULE.update({ext.upper(): ('imagefolder', {}) for ext in imagefolder.ImageFolder.EXTENSIONS})
_EXTENSION_TO_MODULE.update({ext: ('audiofolder', {}) for ext in audiofolder.AudioFolder.EXTENSIONS})
_EXTENSION_TO_MODULE.update({ext.upper(): ('audiofolder', {}) for ext in audiofolder.AudioFolder.EXTENSIONS})
_lowercase       						=			{'imagefolder', 'audiofolder'}

# Used to filter data files based on extensions given a module name
_lowercase       						=			{}
for _ext, (_module, _) in _EXTENSION_TO_MODULE.items():
   _MODULE_TO_EXTENSIONS.setdefault(_module, []).append(_ext)

_MODULE_TO_EXTENSIONS["imagefolder"].append('.zip')






_MODULE_TO_EXTENSIONS["audiofolder"].append('.zip')

