'''simple docstring'''

import subprocess
import sys

from transformers import BertConfig, BertModel, BertTokenizer, pipeline
from transformers.testing_utils import TestCasePlus, require_torch


class 				_lowercase	(      lowerCAmelCase   ):
      '''simple docstring'''


      @require_torch
      def 		lowerCAmelCase__					(				self							)			->			str:



         '''simple docstring'''



         UpperCAmelCase__							:				int   				=  '''
from transformers import BertConfig, BertModel, BertTokenizer, pipeline
        '''

         UpperCAmelCase__							:				Tuple   				=  '''
mname = "hf-internal-testing/tiny-random-bert"
BertConfig.from_pretrained(mname)
BertModel.from_pretrained(mname)
BertTokenizer.from_pretrained(mname)
pipe = pipeline(task="fill-mask", model=mname)
print("success")
        '''

         UpperCAmelCase__							:				Optional[Any]   				=  '''
import socket
def offline_socket(*args, **kwargs): raise RuntimeError("Offline mode is enabled, we shouldn\'t access internet")
socket.socket = offline_socket
        '''

         # Force fetching the files so that we can use the cache
         UpperCAmelCase__							:				Optional[Any]   				=  '''hf-internal-testing/tiny-random-bert'''
         BertConfig.from_pretrained(lowerCamelCase_							)
         BertModel.from_pretrained(lowerCamelCase_							)
         BertTokenizer.from_pretrained(lowerCamelCase_							)
         pipeline(task='''fill-mask'''					,model=lowerCamelCase_							)

         # baseline - just load from_pretrained with normal network
         UpperCAmelCase__							:				Optional[int]   				=  [sys.executable, '''-c''', '''\n'''.join([load, run, mock]							)]

         # should succeed
         UpperCAmelCase__							:				int   				=  self.get_env()
         # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
         UpperCAmelCase__							:				Optional[Any]   				=  '''1'''
         UpperCAmelCase__							:				Optional[int]   				=  subprocess.run(lowerCamelCase_					,env=lowerCamelCase_					,check=lowerCamelCase_					,capture_output=lowerCamelCase_							)
         self.assertEqual(result.returncode					,0					,result.stderr							)
         self.assertIn('''success'''					,result.stdout.decode()							)




      @require_torch
      def 		lowerCAmelCase__					(				self							)			->			Optional[Any]:



         '''simple docstring'''



         UpperCAmelCase__							:				Optional[Any]   				=  '''
from transformers import BertConfig, BertModel, BertTokenizer, pipeline
        '''

         UpperCAmelCase__							:				Optional[Any]   				=  '''
mname = "hf-internal-testing/tiny-random-bert"
BertConfig.from_pretrained(mname)
BertModel.from_pretrained(mname)
BertTokenizer.from_pretrained(mname)
pipe = pipeline(task="fill-mask", model=mname)
print("success")
        '''

         UpperCAmelCase__							:				str   				=  '''
import socket
def offline_socket(*args, **kwargs): raise socket.error("Faking flaky internet")
socket.socket = offline_socket
        '''

         # Force fetching the files so that we can use the cache
         UpperCAmelCase__							:				Dict   				=  '''hf-internal-testing/tiny-random-bert'''
         BertConfig.from_pretrained(lowerCamelCase_							)
         BertModel.from_pretrained(lowerCamelCase_							)
         BertTokenizer.from_pretrained(lowerCamelCase_							)
         pipeline(task='''fill-mask'''					,model=lowerCamelCase_							)

         # baseline - just load from_pretrained with normal network
         UpperCAmelCase__							:				int   				=  [sys.executable, '''-c''', '''\n'''.join([load, run, mock]							)]

         # should succeed
         UpperCAmelCase__							:				List[str]   				=  self.get_env()
         UpperCAmelCase__							:				Optional[Any]   				=  subprocess.run(lowerCamelCase_					,env=lowerCamelCase_					,check=lowerCamelCase_					,capture_output=lowerCamelCase_							)
         self.assertEqual(result.returncode					,0					,result.stderr							)
         self.assertIn('''success'''					,result.stdout.decode()							)




      @require_torch
      def 		lowerCAmelCase__					(				self							)			->			int:



         '''simple docstring'''



         UpperCAmelCase__							:				Tuple   				=  '''
from transformers import BertConfig, BertModel, BertTokenizer
        '''

         UpperCAmelCase__							:				List[str]   				=  '''
mname = "hf-internal-testing/tiny-random-bert-sharded"
BertConfig.from_pretrained(mname)
BertModel.from_pretrained(mname)
print("success")
        '''

         UpperCAmelCase__							:				str   				=  '''
import socket
def offline_socket(*args, **kwargs): raise ValueError("Offline mode is enabled")
socket.socket = offline_socket
        '''

         # baseline - just load from_pretrained with normal network
         UpperCAmelCase__							:				Any   				=  [sys.executable, '''-c''', '''\n'''.join([load, run]							)]

         # should succeed
         UpperCAmelCase__							:				Tuple   				=  self.get_env()
         UpperCAmelCase__							:				List[str]   				=  subprocess.run(lowerCamelCase_					,env=lowerCamelCase_					,check=lowerCamelCase_					,capture_output=lowerCamelCase_							)
         self.assertEqual(result.returncode					,0					,result.stderr							)
         self.assertIn('''success'''					,result.stdout.decode()							)

         # next emulate no network
         UpperCAmelCase__							:				int   				=  [sys.executable, '''-c''', '''\n'''.join([load, mock, run]							)]

         # Doesn't fail anymore since the model is in the cache due to other tests, so commenting this.
         # env["TRANSFORMERS_OFFLINE"] = "0"
         # result = subprocess.run(cmd, env=env, check=False, capture_output=True)
         # self.assertEqual(result.returncode, 1, result.stderr)

         # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
         UpperCAmelCase__							:				Optional[int]   				=  '''1'''
         UpperCAmelCase__							:				Optional[int]   				=  subprocess.run(lowerCamelCase_					,env=lowerCamelCase_					,check=lowerCamelCase_					,capture_output=lowerCamelCase_							)
         self.assertEqual(result.returncode					,0					,result.stderr							)
         self.assertIn('''success'''					,result.stdout.decode()							)




      @require_torch
      def 		lowerCAmelCase__					(				self							)			->			Dict:



         '''simple docstring'''



         UpperCAmelCase__							:				List[Any]   				=  '''
from transformers import pipeline
        '''
         UpperCAmelCase__							:				Any   				=  '''
mname = "hf-internal-testing/tiny-random-bert"
pipe = pipeline(model=mname)
        '''

         UpperCAmelCase__							:				str   				=  '''
import socket
def offline_socket(*args, **kwargs): raise socket.error("Offline mode is enabled")
socket.socket = offline_socket
        '''
         UpperCAmelCase__							:				Union[str, Any]   				=  self.get_env()
         UpperCAmelCase__							:				Optional[int]   				=  '''1'''
         UpperCAmelCase__							:				Optional[int]   				=  [sys.executable, '''-c''', '''\n'''.join([load, mock, run]							)]
         UpperCAmelCase__							:				int   				=  subprocess.run(lowerCamelCase_					,env=lowerCamelCase_					,check=lowerCamelCase_					,capture_output=lowerCamelCase_							)
         self.assertEqual(result.returncode					,1					,result.stderr							)
         self.assertIn(
             '''You cannot infer task automatically within `pipeline` when using offline mode'''					,result.stderr.decode().replace('''\n'''					,''''''							)					,)




      @require_torch
      def 		lowerCAmelCase__					(				self							)			->			List[str]:



         '''simple docstring'''



         UpperCAmelCase__							:				Tuple   				=  '''
from transformers import AutoModel
        '''
         UpperCAmelCase__							:				Union[str, Any]   				=  '''
mname = "hf-internal-testing/test_dynamic_model"
AutoModel.from_pretrained(mname, trust_remote_code=True)
print("success")
        '''

         # baseline - just load from_pretrained with normal network
         UpperCAmelCase__							:				Optional[Any]   				=  [sys.executable, '''-c''', '''\n'''.join([load, run]							)]

         # should succeed
         UpperCAmelCase__							:				Union[str, Any]   				=  self.get_env()
         UpperCAmelCase__							:				int   				=  subprocess.run(lowerCamelCase_					,env=lowerCamelCase_					,check=lowerCamelCase_					,capture_output=lowerCamelCase_							)
         self.assertEqual(result.returncode					,0					,result.stderr							)
         self.assertIn('''success'''					,result.stdout.decode()							)

         # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
         UpperCAmelCase__							:				str   				=  '''1'''
         UpperCAmelCase__							:				List[str]   				=  subprocess.run(lowerCamelCase_					,env=lowerCamelCase_					,check=lowerCamelCase_					,capture_output=lowerCamelCase_							)
         self.assertEqual(result.returncode					,0					,result.stderr							)
         self.assertIn('''success'''					,result.stdout.decode()							)





'''simple docstring'''

import torch

from transformers import AutoModel


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


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



         '''simple docstring'''



         super(lowerCamelCase_					,self							).__init__()

         UpperCAmelCase__							:				Union[str, Any]   				=  AutoModel.from_pretrained(lowerCamelCase_					,return_dict=lowerCamelCase_							)
         UpperCAmelCase__							:				str   				=  torch.nn.CosineSimilarity(3					,1e-08							)
         UpperCAmelCase__							:				int   				=  torch.nn.Softmax(dim=1							)




      def 		lowerCAmelCase__					(				self					,**lowerCamelCase_							)			->			Optional[Any]:



         '''simple docstring'''



         return self.bert(**lowerCamelCase_							).last_hidden_state




      def 		lowerCAmelCase__					(				self					,lowerCamelCase_							)			->			str:



         '''simple docstring'''



         return token_embeddings.sum(2					,keepdim=lowerCamelCase_							)




      def 		lowerCAmelCase__					(				self					,lowerCamelCase_					,lowerCamelCase_					,lowerCamelCase_=1							)			->			int:



         '''simple docstring'''



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




      def 		lowerCAmelCase__					(				self					,lowerCamelCase_					,lowerCamelCase_							)			->			Tuple:



         '''simple docstring'''



         UpperCAmelCase__							:				List[Any]   				=  W_supports['''sizes'''].tolist()
         UpperCAmelCase__							:				Union[str, Any]   				=  W_supports['''start_token_id'''].item()
         UpperCAmelCase__							:				int   				=  W_supports['''end_token_id'''].item()

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

         UpperCAmelCase__							:				Any   				=  self.BERT(**lowerCamelCase_							)
         UpperCAmelCase__							:				Any   				=  self.BERT(**lowerCamelCase_							)

         UpperCAmelCase__							:				Any   				=  None
         UpperCAmelCase__							:				Tuple   				=  None

         UpperCAmelCase__							:				Dict   				=  W_supports['''input_ids'''] == start_token_id
         UpperCAmelCase__							:				Any   				=  W_supports['''input_ids'''] == end_token_id

         for i, size in enumerate(lowerCamelCase_							):
            if i == 0:
               UpperCAmelCase__							:				int   				=  0
            else:
               UpperCAmelCase__							:				str   				=  support_sizes[i - 1]

            UpperCAmelCase__							:				Dict   				=  S[s : s + size][start_token_masks[s : s + size]]
            UpperCAmelCase__							:				Union[str, Any]   				=  S[s : s + size][end_token_masks[s : s + size]]

            UpperCAmelCase__							:				int   				=  torch.matmul(q[i]					,s_start.T							).sum(1							).softmax(0							)
            UpperCAmelCase__							:				int   				=  torch.matmul(q[i]					,s_end.T							).sum(1							).softmax(0							)

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

         return p_starts, p_ends




