






'''simple docstring'''



def 							lowercase_				(    __A :       Tuple			)       ->      Dict:



 """simple docstring"""




 lowercase					:   Tuple				  =0
 lowercase					:   Optional[int]				  =len(__A			)

 for i in range(n - 1			):
  for j in range(i + 1					,      __A			):
   if arr[i] > arr[j]:
    num_inversions += 1

 return num_inversions


def 							lowercase_				(    __A :       Dict			)       ->      Dict:



 """simple docstring"""




 if len(__A			) <= 1:
  return arr, 0
 lowercase					:   Optional[int]				  =len(__A			) // 2
 lowercase					:   Optional[Any]				  =arr[0:mid]
 lowercase					:   Optional[int]				  =arr[mid:]

 lowercase  ,		lowercase					:   str				  =count_inversions_recursive(__A			)
 lowercase  ,		lowercase					:   Optional[Any]				  =count_inversions_recursive(__A			)
 lowercase  ,		lowercase					:   Union[str, Any]				  =_count_cross_inversions(__A					,      __A			)

 lowercase					:   str				  =inversion_p + inversions_q + cross_inversions
 return c, num_inversions


def 							lowercase_				(    __A :       List[Any]					,      __A :       Dict			)       ->      Any:



 """simple docstring"""




 lowercase					:   Union[str, Any]				  =[]
 lowercase					:   Optional[Any]				  =0
 while i < len(__A			) and j < len(__A			):
  if p[i] > q[j]:
   # if P[1] > Q[j], then P[k] > Q[k] for all  i < k <= len(P)
   # These are all inversions. The claim emerges from the
   # property that P is sorted.
   num_inversion += len(__A			) - i
   r.append(q[j]			)
   j += 1
  else:
   r.append(p[i]			)
   i += 1

 if i < len(__A			):
  r.extend(p[i:]			)
 else:
  r.extend(q[j:]			)

 return r, num_inversion


def 							lowercase_				(    )       ->      Dict:



 """simple docstring"""




 lowercase					:   Tuple				  =[1_0, 2, 1, 5, 5, 2, 1_1]

 # this arr has 8 inversions:
 # (10, 2), (10, 1), (10, 5), (10, 5), (10, 2), (2, 1), (5, 2), (5, 2)

 lowercase					:   Optional[Any]				  =count_inversions_bf(__A			)
 lowercase  ,		lowercase					:   Union[str, Any]				  =count_inversions_recursive(__A			)

 assert num_inversions_bf == num_inversions_recursive == 8

 print('''number of inversions = '''					,      __A			)

 # testing an array with zero inversion (a sorted arr_1)

 arr_a.sort()
 lowercase					:   List[Any]				  =count_inversions_bf(__A			)
 lowercase  ,		lowercase					:   Optional[int]				  =count_inversions_recursive(__A			)

 assert num_inversions_bf == num_inversions_recursive == 0
 print('''number of inversions = '''					,      __A			)

 # an empty list should also have zero inversions
 lowercase					:   str				  =[]
 lowercase					:   Optional[Any]				  =count_inversions_bf(__A			)
 lowercase  ,		lowercase					:   Optional[int]				  =count_inversions_recursive(__A			)

 assert num_inversions_bf == num_inversions_recursive == 0
 print('''number of inversions = '''					,      __A			)


if __name__ == "__main__":
      main()











'''simple docstring'''



import subprocess
import sys

from transformers import BertConfig, BertModel, BertTokenizer, pipeline
from transformers.testing_utils import TestCasePlus, require_torch
class UpperCAmelCase_					(    __A				):




      """simple docstring"""




      @require_torch
      def 				A__      ( self			:		Tuple		)  ->  Optional[int]:


       '''simple docstring'''

       lowercase					:   Any				  ='''
from transformers import BertConfig, BertModel, BertTokenizer, pipeline
        '''

       lowercase					:   Optional[int]				  ='''
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")
        '''

       lowercase					:   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
       lowercase					:   Tuple				  ='''hf-internal-testing/tiny-random-bert'''
       BertConfig.from_pretrained(UpperCAmelCase		)
       BertModel.from_pretrained(UpperCAmelCase		)
       BertTokenizer.from_pretrained(UpperCAmelCase		)
       pipeline(task='''fill-mask'''     ,      model=UpperCAmelCase		)

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

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






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


       '''simple docstring'''

       lowercase					:   str				  ='''
from transformers import BertConfig, BertModel, BertTokenizer, pipeline
        '''

       lowercase					:   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")
        '''

       lowercase					:   Optional[int]				  ='''
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
       lowercase					:   Optional[Any]				  ='''hf-internal-testing/tiny-random-bert'''
       BertConfig.from_pretrained(UpperCAmelCase		)
       BertModel.from_pretrained(UpperCAmelCase		)
       BertTokenizer.from_pretrained(UpperCAmelCase		)
       pipeline(task='''fill-mask'''     ,      model=UpperCAmelCase		)

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

       # should succeed
       lowercase					:   str				  =self.get_env()
       lowercase					:   Any				  =subprocess.run(UpperCAmelCase     ,      env=UpperCAmelCase     ,      check=UpperCAmelCase     ,      capture_output=UpperCAmelCase		)
       self.assertEqual(result.returncode     ,      0     ,      result.stderr		)
       self.assertIn('''success'''     ,      result.stdout.decode()		)






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


       '''simple docstring'''

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

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

       lowercase					:   int				  ='''
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
       lowercase					:   Tuple				  =[sys.executable, '''-c''', '''\n'''.join([load, run]		)]

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

       # next emulate no network
       lowercase					:   Tuple				  =[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
       lowercase					:   Any				  ='''1'''
       lowercase					:   Optional[Any]				  =subprocess.run(UpperCAmelCase     ,      env=UpperCAmelCase     ,      check=UpperCAmelCase     ,      capture_output=UpperCAmelCase		)
       self.assertEqual(result.returncode     ,      0     ,      result.stderr		)
       self.assertIn('''success'''     ,      result.stdout.decode()		)






      @require_torch
      def 				A__      ( self			:		Tuple		)  ->  Optional[int]:


       '''simple docstring'''

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

       lowercase					:   Tuple				  ='''
import socket
def offline_socket(*args, **kwargs): raise socket.error("Offline mode is enabled")
socket.socket = offline_socket
        '''
       lowercase					:   Tuple				  =self.get_env()
       lowercase					:   Optional[int]				  ='''1'''
       lowercase					:   Union[str, Any]				  =[sys.executable, '''-c''', '''\n'''.join([load, mock, run]		)]
       lowercase					:   Dict				  =subprocess.run(UpperCAmelCase     ,      env=UpperCAmelCase     ,      check=UpperCAmelCase     ,      capture_output=UpperCAmelCase		)
       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 				A__      ( self			:		int		)  ->  Optional[int]:


       '''simple docstring'''

       lowercase					:   List[str]				  ='''
from transformers import AutoModel
        '''
       lowercase					:   Dict				  ='''
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
       lowercase					:   Dict				  =[sys.executable, '''-c''', '''\n'''.join([load, run]		)]

       # should succeed
       lowercase					:   Optional[Any]				  =self.get_env()
       lowercase					:   int				  =subprocess.run(UpperCAmelCase     ,      env=UpperCAmelCase     ,      check=UpperCAmelCase     ,      capture_output=UpperCAmelCase		)
       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
       lowercase					:   List[str]				  ='''1'''
       lowercase					:   List[Any]				  =subprocess.run(UpperCAmelCase     ,      env=UpperCAmelCase     ,      check=UpperCAmelCase     ,      capture_output=UpperCAmelCase		)
       self.assertEqual(result.returncode     ,      0     ,      result.stderr		)
       self.assertIn('''success'''     ,      result.stdout.decode()		)



